static void TestCovertToUserName(void) { char userName[100]; LE_ASSERT(user_AppNameToUserName(APP_NAME, userName, sizeof(userName)) == LE_OK); LE_ASSERT(strcmp(userName, APP_USER_NAME) == 0); }
//-------------------------------------------------------------------------------------------------- static void GetAppIds ( app_Ref_t appRef, ///< [IN] App ref. const char* appNamePtr, ///< [IN] App name. uid_t* uidPtr, ///< [OUT] Location to store the app's user ID. gid_t* gidPtr, ///< [OUT] Location to store the app's primary group ID. char* userNamePtr, ///< [OUT] Location to store the app user name size_t userNameLen ///< [IN] Size of userNamePtr ) { INTERNAL_ERR_IF(user_AppNameToUserName(appNamePtr, userNamePtr, userNameLen) != LE_OK, "userName buffer too small."); *uidPtr = app_GetUid(appRef); *gidPtr = app_GetGid(appRef); }
//-------------------------------------------------------------------------------------------------- static le_result_t GetServerUid ( le_cfg_IteratorRef_t i, ///< [in] Config tree iterator positioned at binding config. uid_t* uidPtr ///< [out] The application's user ID. ) //-------------------------------------------------------------------------------------------------- { le_result_t result; char userName[LIMIT_MAX_USER_NAME_BYTES]; // If an app name is present in the binding config, if (le_cfg_NodeExists(i, "app")) { // Make sure there isn't also a user name. if (le_cfg_NodeExists(i, "user")) { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "", path, sizeof(path)); LE_CRIT("Both server user and app nodes appear under binding (@ %s)", path); return LE_DUPLICATE; } // Get the app name. char appName[LIMIT_MAX_APP_NAME_BYTES]; result = le_cfg_GetString(i, "app", appName, sizeof(appName), ""); if (result != LE_OK) { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "app", path, sizeof(path)); LE_CRIT("Server app name too big (@ %s)", path); return result; } if (appName[0] == '\0') { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "app", path, sizeof(path)); LE_CRIT("Server app name empty (@ %s)", path); return LE_NOT_FOUND; } // Find out if the server app is sandboxed. If not, it runs as root. char path[LIMIT_MAX_PATH_BYTES]; if (snprintf(path, sizeof(path), "/apps/%s/sandboxed", appName) >= sizeof(path)) { LE_CRIT("Config node path too long (app name '%s').", appName); return LE_OVERFLOW; } if (!le_cfg_GetBool(i, path, true)) { *uidPtr = 0; return LE_OK; } // It is not sandboxed. Convert the app name into a user name. result = user_AppNameToUserName(appName, userName, sizeof(userName)); if (result != LE_OK) { LE_CRIT("Failed to convert app name '%s' into a user name.", appName); return result; } } // If a server app name is not present in the binding config, else { // Get the server user name instead. result = le_cfg_GetString(i, "user", userName, sizeof(userName), ""); if (result != LE_OK) { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "user", path, sizeof(path)); LE_CRIT("Server user name too big (@ %s)", path); return result; } if (userName[0] == '\0') { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "", path, sizeof(path)); LE_CRIT("Server user name or app name missing (@ %s)", path); return LE_NOT_FOUND; } } // Convert the server's user name into a user ID. result = user_GetUid(userName, uidPtr); if (result != LE_OK) { // Note: This can happen if the server application isn't installed yet. // When the server application is installed, sdir load will be run // again and the bindings will be correctly set up at that time. if (strncmp(userName, "app", 3) == 0) { LE_DEBUG("Couldn't get UID for application '%s'. Perhaps it is not installed yet?", userName + 3); } else { char path[LIMIT_MAX_PATH_BYTES]; le_cfg_GetPath(i, "", path, sizeof(path)); LE_CRIT("Couldn't convert server user name '%s' to UID (%s @ %s)", userName, LE_RESULT_TXT(result), path); } return result; } return LE_OK; }