in src/utils/extension_utils/src/extension_utils.c [422:542]
bool RegisterExtension(const char* extensionDir, const char* extensionFilePath)
{
Log_Debug("Registering an extension, target dir: %s, file: %s", extensionDir, extensionFilePath);
bool success = false;
char* hash = NULL;
STRING_HANDLE content = NULL;
STRING_HANDLE outFilePath = NULL;
struct passwd* pwd = NULL;
struct group* grp = NULL;
FILE* outFile = NULL;
if (IsNullOrEmpty(extensionDir))
{
Log_Error("Invalid target directory.");
return false;
}
if (IsNullOrEmpty(extensionFilePath))
{
Log_Error("Invalid extension file path.");
return false;
}
// Note: the return value may point to a static area,
// and may be overwritten by subsequent calls to getpwent(3), getpwnam(), or getpwuid().
// (Do not pass the returned pointer to free(3).)
pwd = ADUCPAL_getpwnam(ADUC_FILE_USER);
if (pwd == NULL)
{
Log_Error("Cannot verify credential of 'adu' user.");
goto done;
}
uid_t aduUserId = pwd->pw_uid;
pwd = NULL;
// Note: The return value may point to a static area,
// and may be overwritten by subsequent calls to getgrent(3), getgrgid(), or getgrnam().
// (Do not pass the returned pointer to free(3).)
grp = ADUCPAL_getgrnam(ADUC_FILE_GROUP);
if (grp == NULL)
{
Log_Error("Cannot get 'adu' group info.");
goto done;
}
gid_t aduGroupId = grp->gr_gid;
grp = NULL;
Log_Debug("Creating the extension folder ('%s'), uid:%d, gid:%d", extensionDir, aduUserId, aduGroupId);
int dir_result =
ADUC_SystemUtils_MkDirRecursive(extensionDir, aduUserId, aduGroupId, S_IRWXU | S_IRGRP | S_IWGRP | S_IXGRP);
if (dir_result != 0)
{
Log_Error("Cannot create a folder for registration file. ('%s')", extensionDir);
goto done;
}
struct stat bS;
if (stat(extensionFilePath, &bS) != 0)
{
goto done;
}
long fileSize = bS.st_size;
if (!ADUC_HashUtils_GetFileHash(extensionFilePath, SHA256, &hash))
{
goto done;
}
content = STRING_construct_sprintf(
"{\n"
" \"fileName\":\"%s\",\n"
" \"sizeInBytes\":%lld,\n"
" \"hashes\": {\n"
" \"sha256\":\"%s\"\n"
" }\n"
"}\n",
extensionFilePath,
fileSize,
hash);
if (content == NULL)
{
Log_Error("Cannot construct an extension information.");
goto done;
}
outFilePath = STRING_construct_sprintf("%s/%s", extensionDir, ADUC_EXTENSION_REG_FILENAME);
outFile = fopen(STRING_c_str(outFilePath), "w");
if (outFile == NULL)
{
Log_Error("Cannot open file: %s", STRING_c_str(outFilePath));
goto done;
}
int ref = fputs(STRING_c_str(content), outFile);
if (ref < 0)
{
Log_Error(
"Failed to write an extension info to file. File:%s, Content:%s", extensionDir, STRING_c_str(content));
goto done;
}
printf("Successfully registered an extension. Info: %s\n", STRING_c_str(outFilePath));
success = true;
done:
if (outFile != NULL)
{
fclose(outFile);
}
STRING_delete(content);
STRING_delete(outFilePath);
free(hash);
return success;
}