1/* 2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <dirent.h> 17#include <errno.h> 18#include <unistd.h> 19#include <sys/types.h> 20#include <stdio.h> 21#include <sys/stat.h> 22#include <stdbool.h> 23 24#include <securec.h> 25 26/** 27 * This bin file is used in l2 device triggered when device initializing 28*/ 29 30#include "hilog/log.h" 31#define LOG_ENGINE LOG_CORE 32#define LOG_PUBLIC "{public}" 33#undef LOG_DOMAIN 34#define LOG_DOMAIN 0xD002F00 /* Security subsystem's domain id */ 35 36#define HKS_LOG_E(fmt, arg...) HILOG_ERROR(LOG_ENGINE, "%" LOG_PUBLIC "s[%" LOG_PUBLIC "u]: " fmt "\n", \ 37 __func__, __LINE__, ##arg) 38 39#define HUKS_SERVICE_UID 3510 40#define DIR_TYPE 4 41#define DEFAULT_PATH_LEN 1024 42#define DEFAULT_HUKS_PATH_PERMISSION 0700 43 44#include <stdio.h> 45 46#include <errno.h> 47 48static void ChangeDirAndFilesPerm(const char *path) 49{ 50 HKS_LOG_E("enter ChangeDirAndFilesPerm"); 51 DIR *dir; 52 struct dirent *ptr; 53 dir = opendir(path); 54 if (dir == NULL) { 55 return; 56 } 57 int ret = EOK; 58 while ((ptr = readdir(dir)) != NULL) { 59 if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) { 60 continue; 61 } 62 char curPath[DEFAULT_PATH_LEN] = { 0 }; 63 64 ret = strcpy_s(curPath, DEFAULT_PATH_LEN, path); 65 if (ret != EOK) { 66 break; 67 } 68 ret = strcat_s(curPath, DEFAULT_PATH_LEN, "/"); 69 if (ret != EOK) { 70 break; 71 } 72 ret = strcat_s(curPath, DEFAULT_PATH_LEN, ptr->d_name); 73 if (ret != EOK) { 74 break; 75 } 76 77 ret = chown(curPath, HUKS_SERVICE_UID, HUKS_SERVICE_UID); 78 if (ret != EOK) { 79 break; 80 } 81 if ((ptr->d_type != DIR_TYPE)) { 82 if (chmod(curPath, S_IRUSR | S_IWUSR) < 0) { 83 break; 84 } 85 } else { 86 if (chmod(curPath, S_IRWXU) < 0) { 87 break; 88 } 89 ChangeDirAndFilesPerm(curPath); 90 } 91 } 92 if (ret != EOK) { 93 printf("chmod dir and file failed! errno = 0x%x \n", errno); 94 } 95 (void)closedir(dir); 96} 97 98int main(void) 99{ 100 const char *path = "/data/data/huks_service"; 101 // change directories and files permission 102 ChangeDirAndFilesPerm(path); 103} 104