1526fd984Sopenharmony_ci/* 2526fd984Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License. 5526fd984Sopenharmony_ci * You may obtain a copy of the License at 6526fd984Sopenharmony_ci * 7526fd984Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8526fd984Sopenharmony_ci * 9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and 13526fd984Sopenharmony_ci * limitations under the License. 14526fd984Sopenharmony_ci */ 15526fd984Sopenharmony_ci 16526fd984Sopenharmony_ci#include <dirent.h> 17526fd984Sopenharmony_ci#include <errno.h> 18526fd984Sopenharmony_ci#include <unistd.h> 19526fd984Sopenharmony_ci#include <sys/types.h> 20526fd984Sopenharmony_ci#include <stdio.h> 21526fd984Sopenharmony_ci#include <sys/stat.h> 22526fd984Sopenharmony_ci#include <stdbool.h> 23526fd984Sopenharmony_ci 24526fd984Sopenharmony_ci#include <securec.h> 25526fd984Sopenharmony_ci 26526fd984Sopenharmony_ci/** 27526fd984Sopenharmony_ci * This bin file is used in l2 device triggered when device initializing 28526fd984Sopenharmony_ci*/ 29526fd984Sopenharmony_ci 30526fd984Sopenharmony_ci#include "hilog/log.h" 31526fd984Sopenharmony_ci#define LOG_ENGINE LOG_CORE 32526fd984Sopenharmony_ci#define LOG_PUBLIC "{public}" 33526fd984Sopenharmony_ci#undef LOG_DOMAIN 34526fd984Sopenharmony_ci#define LOG_DOMAIN 0xD002F00 /* Security subsystem's domain id */ 35526fd984Sopenharmony_ci 36526fd984Sopenharmony_ci#define HKS_LOG_E(fmt, arg...) HILOG_ERROR(LOG_ENGINE, "%" LOG_PUBLIC "s[%" LOG_PUBLIC "u]: " fmt "\n", \ 37526fd984Sopenharmony_ci __func__, __LINE__, ##arg) 38526fd984Sopenharmony_ci 39526fd984Sopenharmony_ci#define HUKS_SERVICE_UID 3510 40526fd984Sopenharmony_ci#define DIR_TYPE 4 41526fd984Sopenharmony_ci#define DEFAULT_PATH_LEN 1024 42526fd984Sopenharmony_ci#define DEFAULT_HUKS_PATH_PERMISSION 0700 43526fd984Sopenharmony_ci 44526fd984Sopenharmony_ci#include <stdio.h> 45526fd984Sopenharmony_ci 46526fd984Sopenharmony_ci#include <errno.h> 47526fd984Sopenharmony_ci 48526fd984Sopenharmony_cistatic void ChangeDirAndFilesPerm(const char *path) 49526fd984Sopenharmony_ci{ 50526fd984Sopenharmony_ci HKS_LOG_E("enter ChangeDirAndFilesPerm"); 51526fd984Sopenharmony_ci DIR *dir; 52526fd984Sopenharmony_ci struct dirent *ptr; 53526fd984Sopenharmony_ci dir = opendir(path); 54526fd984Sopenharmony_ci if (dir == NULL) { 55526fd984Sopenharmony_ci return; 56526fd984Sopenharmony_ci } 57526fd984Sopenharmony_ci int ret = EOK; 58526fd984Sopenharmony_ci while ((ptr = readdir(dir)) != NULL) { 59526fd984Sopenharmony_ci if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { 60526fd984Sopenharmony_ci continue; 61526fd984Sopenharmony_ci } 62526fd984Sopenharmony_ci char curPath[DEFAULT_PATH_LEN] = { 0 }; 63526fd984Sopenharmony_ci 64526fd984Sopenharmony_ci ret = strcpy_s(curPath, DEFAULT_PATH_LEN, path); 65526fd984Sopenharmony_ci if (ret != EOK) { 66526fd984Sopenharmony_ci break; 67526fd984Sopenharmony_ci } 68526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/"); 69526fd984Sopenharmony_ci if (ret != EOK) { 70526fd984Sopenharmony_ci break; 71526fd984Sopenharmony_ci } 72526fd984Sopenharmony_ci ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name); 73526fd984Sopenharmony_ci if (ret != EOK) { 74526fd984Sopenharmony_ci break; 75526fd984Sopenharmony_ci } 76526fd984Sopenharmony_ci 77526fd984Sopenharmony_ci ret = chown(curPath, HUKS_SERVICE_UID, HUKS_SERVICE_UID); 78526fd984Sopenharmony_ci if (ret != EOK) { 79526fd984Sopenharmony_ci break; 80526fd984Sopenharmony_ci } 81526fd984Sopenharmony_ci if ((ptr->d_type != DIR_TYPE)) { 82526fd984Sopenharmony_ci if (chmod(curPath, S_IRUSR | S_IWUSR) < 0) { 83526fd984Sopenharmony_ci break; 84526fd984Sopenharmony_ci } 85526fd984Sopenharmony_ci } else { 86526fd984Sopenharmony_ci if (chmod(curPath, S_IRWXU) < 0) { 87526fd984Sopenharmony_ci break; 88526fd984Sopenharmony_ci } 89526fd984Sopenharmony_ci ChangeDirAndFilesPerm(curPath); 90526fd984Sopenharmony_ci } 91526fd984Sopenharmony_ci } 92526fd984Sopenharmony_ci if (ret != EOK) { 93526fd984Sopenharmony_ci printf("chmod dir and file failed! errno = 0x%x \n", errno); 94526fd984Sopenharmony_ci } 95526fd984Sopenharmony_ci (void)closedir(dir); 96526fd984Sopenharmony_ci} 97526fd984Sopenharmony_ci 98526fd984Sopenharmony_ciint main(void) 99526fd984Sopenharmony_ci{ 100526fd984Sopenharmony_ci const char *path = "/data/data/huks_service"; 101526fd984Sopenharmony_ci // change directories and files permission 102526fd984Sopenharmony_ci ChangeDirAndFilesPerm(path); 103526fd984Sopenharmony_ci} 104