int list_groups_for_user_main( int argc, char* argv[] ) { DWORD dwError = 0; HANDLE hLsaConnection = (HANDLE)NULL; PCSTR pszUserName = NULL; DWORD dwNumGroups = 0; DWORD iGroup = 0; LSA_FIND_FLAGS FindFlags = 0; DWORD dwGroupInfoLevel = 0; PVOID* ppGroupInfoList = NULL; DWORD dwId = 0; BOOLEAN bShowSid = FALSE; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; ParseArgs(argc, argv, &pszUserName, &dwId, &bShowSid); if (pszUserName) { dwError = LsaValidateUserName(pszUserName); BAIL_ON_LSA_ERROR(dwError); } dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); if (pszUserName) { dwError = LsaGetGroupsForUserByName( hLsaConnection, pszUserName, FindFlags, dwGroupInfoLevel, &dwNumGroups, &ppGroupInfoList); BAIL_ON_LSA_ERROR(dwError); } else { dwError = LsaGetGroupsForUserById( hLsaConnection, dwId, FindFlags, dwGroupInfoLevel, &dwNumGroups, &ppGroupInfoList); BAIL_ON_LSA_ERROR(dwError); } if (pszUserName) { printf("Number of groups found for user '%s' : %u\n", pszUserName, dwNumGroups); } else { printf("Number of groups found for uid %u : %u\n", dwId, dwNumGroups); } switch(dwGroupInfoLevel) { case 0: for (iGroup = 0; iGroup < dwNumGroups; iGroup++) { PLSA_GROUP_INFO_0* pGroupInfoList = (PLSA_GROUP_INFO_0*)ppGroupInfoList; if (bShowSid) { fprintf(stdout, "Group[%u of %u] name = %s (gid = %u, sid = %s)\n", iGroup+1, dwNumGroups, pGroupInfoList[iGroup]->pszName, (unsigned int) pGroupInfoList[iGroup]->gid, pGroupInfoList[iGroup]->pszSid); } else { fprintf(stdout, "Group[%u of %u] name = %s (gid = %u)\n", iGroup+1, dwNumGroups, pGroupInfoList[iGroup]->pszName, (unsigned int) pGroupInfoList[iGroup]->gid); } } break; default: dwError = LW_ERROR_UNSUPPORTED_GROUP_LEVEL; BAIL_ON_LSA_ERROR(dwError); break; } cleanup: if (ppGroupInfoList) { LsaFreeGroupInfoList(dwGroupInfoLevel, ppGroupInfoList, dwNumGroups); } if (hLsaConnection) { LsaCloseServer(hLsaConnection); } return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { if (pszUserName) { fprintf( stderr, "Failed to find groups for user '%s'. Error code %u (%s).\n%s\n", pszUserName, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); } else { fprintf( stderr, "Failed to find groups for uid %u. Error code %u (%s).\n%s\n", dwId, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); } bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { if (pszUserName) { fprintf( stderr, "Failed to find groups for user '%s'. Error code %u (%s).\n", pszUserName, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } else { fprintf( stderr, "Failed to find groups for uid %u. Error code %u (%s).\n", dwId, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } } goto cleanup; }
int set_machine_sid_main( int argc, char* argv[] ) { DWORD dwError = 0; PSTR pszMachineSid = NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; if (geteuid() != 0) { fprintf(stderr, "This program requires super-user privileges.\n"); dwError = LW_ERROR_ACCESS_DENIED; BAIL_ON_LSA_ERROR(dwError); } dwError = ParseArgs(argc, argv, &pszMachineSid); BAIL_ON_LSA_ERROR(dwError); dwError = ValidateParameters(pszMachineSid); BAIL_ON_LSA_ERROR(dwError); dwError = SetMachineSid(pszMachineSid); BAIL_ON_LSA_ERROR(dwError); cleanup: if (pszMachineSid) { LW_SAFE_FREE_STRING(pszMachineSid); } return dwError; error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = 0; dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to modify SID. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to modify SID. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int set_log_level_main( int argc, char* argv[] ) { DWORD dwError = 0; LsaLogLevel logLevel = LSA_LOG_LEVEL_ERROR; HANDLE hLsaConnection = (HANDLE)NULL; PLSA_LOG_INFO pLogInfo = NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; if (geteuid() != 0) { fprintf(stderr, "This program requires super-user privileges.\n"); dwError = LW_ERROR_ACCESS_DENIED; BAIL_ON_LSA_ERROR(dwError); } dwError = ParseArgs(argc, argv, &logLevel); BAIL_ON_LSA_ERROR(dwError); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); dwError = LsaSetLogLevel( hLsaConnection, logLevel); BAIL_ON_LSA_ERROR(dwError); fprintf(stdout, "The log level was set successfully\n\n"); dwError = LsaGetLogInfo( hLsaConnection, &pLogInfo); BAIL_ON_LSA_ERROR(dwError); dwError = PrintLogInfo(pLogInfo); BAIL_ON_LSA_ERROR(dwError); cleanup: if (pLogInfo) { LsaFreeLogInfo(pLogInfo); } if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to set log level. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to set log level. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int ad_cache_main( int argc, char* argv[] ) { DWORD dwError = 0; HANDLE hLsaConnection = (HANDLE)NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; PSTR pszOperation = "complete operation"; DWORD dwAction = ACTION_NONE; PSTR pszDomainName = NULL; PSTR pszName = NULL; uid_t uid = 0; gid_t gid = 0; bool bForceOfflineDelete; DWORD dwBatchSize = 10; if (argc < 2 || (strcmp(argv[1], "--help") == 0) || (strcmp(argv[1], "-h") == 0)) { ShowUsage(GetProgramName(argv[0])); exit(0); } if (geteuid() != 0) { fprintf(stderr, "This program requires super-user privileges.\n"); dwError = LW_ERROR_ACCESS_DENIED; BAIL_ON_LSA_ERROR(dwError); } dwError = ParseArgs( argc, argv, &dwAction, &pszDomainName, &pszName, &uid, &gid, &bForceOfflineDelete, &dwBatchSize); BAIL_ON_LSA_ERROR(dwError); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); switch (dwAction) { case ACTION_DELETE_ALL: pszOperation = "empty cache"; dwError = LsaAdEmptyCache( hLsaConnection, pszDomainName, bForceOfflineDelete); BAIL_ON_LSA_ERROR(dwError); fprintf(stdout, "The cache has been emptied successfully.\n"); break; case ACTION_DELETE_USER: pszOperation = "delete user"; if ( pszName ) { dwError = LsaAdRemoveUserByNameFromCache( hLsaConnection, pszDomainName, pszName); BAIL_ON_LSA_ERROR(dwError); } else { dwError = LsaAdRemoveUserByIdFromCache( hLsaConnection, pszDomainName, uid); BAIL_ON_LSA_ERROR(dwError); } fprintf(stdout, "The user has been deleted from the cache successfully.\n"); break; case ACTION_DELETE_GROUP: pszOperation = "delete group"; if ( pszName ) { dwError = LsaAdRemoveGroupByNameFromCache( hLsaConnection, pszDomainName, pszName); BAIL_ON_LSA_ERROR(dwError); } else { dwError = LsaAdRemoveGroupByIdFromCache( hLsaConnection, pszDomainName, gid); BAIL_ON_LSA_ERROR(dwError); } fprintf(stdout, "The group has been deleted from the cache successfully.\n"); break; case ACTION_ENUM_USERS: pszOperation = "enumerate users"; dwError = EnumerateUsers( hLsaConnection, pszDomainName, dwBatchSize); BAIL_ON_LSA_ERROR(dwError); break; case ACTION_ENUM_GROUPS: pszOperation = "enumerate groups"; dwError = EnumerateGroups( hLsaConnection, pszDomainName, dwBatchSize); BAIL_ON_LSA_ERROR(dwError); break; } cleanup: if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } return dwError; error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to %s. Error code %u (%s).\n%s\n", pszOperation, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to %s. Error code %u (%s).\n", pszOperation, dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int get_metrics_main( int argc, char* argv[] ) { DWORD dwError = 0; DWORD dwInfoLevel = 0; PVOID pMetricPack = NULL; HANDLE hLsaConnection = (HANDLE)NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; ParseArgs(argc, argv, &dwInfoLevel); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); dwError = LsaGetMetrics( hLsaConnection, dwInfoLevel, &pMetricPack); BAIL_ON_LSA_ERROR(dwError); switch (dwInfoLevel) { case 0: PrintMetricPack_0( (PLSA_METRIC_PACK_0)pMetricPack); break; case 1: PrintMetricPack_1( (PLSA_METRIC_PACK_1)pMetricPack); break; } cleanup: LW_SAFE_FREE_MEMORY(pMetricPack); if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf( stderr, "Failed to query metrics from LSA service. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf( stderr, "Failed to query metrics from LSA service. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
DWORD LsaAddGroupMain( int argc, char* argv[] ) { DWORD dwError = 0; PSTR pszGid = NULL; PSTR pszGroup = NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; if (geteuid() != 0) { fprintf(stderr, "This program requires super-user privileges.\n"); dwError = LW_ERROR_ACCESS_DENIED; BAIL_ON_LSA_ERROR(dwError); } dwError = ParseArgs( argc, argv, &pszGid, &pszGroup); BAIL_ON_LSA_ERROR(dwError); dwError = AddGroup( pszGid, pszGroup); BAIL_ON_LSA_ERROR(dwError); fprintf(stdout, "Successfully added group %s\n", pszGroup); cleanup: LW_SAFE_FREE_STRING(pszGid); LW_SAFE_FREE_STRING(pszGroup); return dwError; error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to add group. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to add group. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int enum_users_main( int argc, char* argv[] ) { DWORD dwError = 0; DWORD dwUserInfoLevel = 0; DWORD dwBatchSize = 10; HANDLE hLsaConnection = (HANDLE)NULL; HANDLE hResume = (HANDLE)NULL; PVOID* ppUserInfoList = NULL; DWORD dwNumUsersFound = 0; DWORD dwTotalUsersFound = 0; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; BOOLEAN bCheckUserInList = FALSE; dwError = ParseArgs(argc, argv, &dwUserInfoLevel, &dwBatchSize, &bCheckUserInList); BAIL_ON_LSA_ERROR(dwError); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); dwError = LsaBeginEnumUsers( hLsaConnection, dwUserInfoLevel, dwBatchSize, 0, &hResume); BAIL_ON_LSA_ERROR(dwError); do { DWORD iUser = 0; if (ppUserInfoList) { LsaFreeUserInfoList(dwUserInfoLevel, ppUserInfoList, dwNumUsersFound); ppUserInfoList = NULL; } dwError = LsaEnumUsers( hLsaConnection, hResume, &dwNumUsersFound, &ppUserInfoList); BAIL_ON_LSA_ERROR(dwError); if (!dwNumUsersFound) { break; } dwTotalUsersFound+=dwNumUsersFound; for (iUser = 0; iUser < dwNumUsersFound; iUser++) { BOOLEAN bAllowedLogon = TRUE; PVOID pUserInfo = *(ppUserInfoList + iUser); if (bCheckUserInList) { dwError = LsaCheckUserInList( hLsaConnection, ((PLSA_USER_INFO_0)pUserInfo)->pszName, NULL); if (dwError) { bAllowedLogon = FALSE; } } switch(dwUserInfoLevel) { case 0: PrintUserInfo_0((PLSA_USER_INFO_0)pUserInfo, bCheckUserInList, bAllowedLogon); break; case 1: PrintUserInfo_1((PLSA_USER_INFO_1)pUserInfo, bCheckUserInList, bAllowedLogon); break; case 2: PrintUserInfo_2((PLSA_USER_INFO_2)pUserInfo, bCheckUserInList, bAllowedLogon); break; default: fprintf(stderr, "Error: Invalid user info level %u\n", dwUserInfoLevel); break; } } } while (dwNumUsersFound); fprintf(stdout, "TotalNumUsersFound: %u\n", dwTotalUsersFound); cleanup: if (ppUserInfoList) { LsaFreeUserInfoList(dwUserInfoLevel, ppUserInfoList, dwNumUsersFound); } if ((hResume != (HANDLE)NULL) && (hLsaConnection != (HANDLE)NULL)) { LsaEndEnumUsers(hLsaConnection, hResume); } if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to enumerate users. Error code %u (%s).\n" "%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } if (dwError == ERROR_INVALID_DATA) { fprintf(stderr, "The users list has changed while enumerating. " "Try again.\n"); } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to enumerate users. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int lw_ypcat_main( int argc, char* argv[] ) { DWORD dwError = 0; HANDLE hLsaConnection = (HANDLE)NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; PSTR pszMapName = NULL; PSTR pszDomain = NULL; BOOLEAN bPrintKeys = FALSE; BOOLEAN bPrintNicknameTable = FALSE; BOOLEAN bUseNicknameTable = TRUE; PDLINKEDLIST pNISNicknameList = NULL; BOOLEAN bNoNicknameFile = FALSE; PCSTR pszNicknameFilePath = "/var/yp/nicknames"; BOOLEAN bCheckGroupMembersOnline = FALSE; BOOLEAN bIndexById = FALSE; dwError = ParseArgs( argc, argv, &pszMapName, &pszDomain, &bPrintKeys, &bPrintNicknameTable, &bUseNicknameTable, &bCheckGroupMembersOnline); BAIL_ON_LSA_ERROR(dwError); dwError = LsaNISGetNicknames( pszNicknameFilePath, &pNISNicknameList); if (dwError == ENOENT) { bNoNicknameFile = TRUE; dwError = 0; } if (bPrintNicknameTable) { if (bNoNicknameFile) { printf("nickname file %s does not exist.\n", pszNicknameFilePath); goto cleanup; } if (pNISNicknameList) { PrintNicknameTable(pNISNicknameList); } goto cleanup; } if (bUseNicknameTable) { PCSTR pszLookupName = NULL; if (bNoNicknameFile) { printf("nickname file %s does not exist.\n", pszNicknameFilePath); } if (pNISNicknameList) { pszLookupName = LsaNISLookupAlias( pNISNicknameList, pszMapName); if (pszLookupName) { LW_SAFE_FREE_STRING(pszMapName); dwError = LwAllocateString( pszLookupName, &pszMapName); BAIL_ON_LSA_ERROR(dwError); } } } dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); if (!strcasecmp(pszMapName, "passwd.byname") || !strcasecmp(pszMapName, "passwd")) { dwError = EnumerateUsers(hLsaConnection, bPrintKeys, bIndexById); } else if (!strcasecmp(pszMapName, "passwd.byid") || !strcasecmp(pszMapName, "passwd.byuid")) { bIndexById = TRUE; dwError = EnumerateUsers(hLsaConnection, bPrintKeys, bIndexById); } else if (!strcasecmp(pszMapName, "group.byname") || !strcasecmp(pszMapName, "group")) { dwError = EnumerateGroups( hLsaConnection, bCheckGroupMembersOnline, bPrintKeys, bIndexById); } else if (!strcasecmp(pszMapName, "group.byid") || !strcasecmp(pszMapName, "group.bygid")) { bIndexById = TRUE; dwError = EnumerateGroups( hLsaConnection, bCheckGroupMembersOnline, bPrintKeys, bIndexById); } else { dwError = EnumerateMaps( hLsaConnection, pszMapName, bPrintKeys); } BAIL_ON_LSA_ERROR(dwError); cleanup: if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } if (pNISNicknameList) { LsaNISFreeNicknameList(pNISNicknameList); } LW_SAFE_FREE_STRING(pszMapName); LW_SAFE_FREE_STRING(pszDomain); return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to enumerate maps. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to enumerate maps. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } dwError = 1; goto cleanup; }
int enum_groups_main( int argc, char* argv[] ) { DWORD dwError = 0; DWORD dwGroupInfoLevel = 0; DWORD dwBatchSize = 10; HANDLE hLsaConnection = (HANDLE)NULL; HANDLE hResume = (HANDLE)NULL; PVOID* ppGroupInfoList = NULL; DWORD dwNumGroupsFound = 0; DWORD dwTotalGroupsFound = 0; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = FALSE; BOOLEAN bCheckGroupMembersOnline = FALSE; dwError = ParseArgs(argc, argv, &dwGroupInfoLevel, &dwBatchSize, &bCheckGroupMembersOnline); BAIL_ON_LSA_ERROR(dwError); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); dwError = LsaBeginEnumGroupsWithCheckOnlineOption( hLsaConnection, dwGroupInfoLevel, dwBatchSize, bCheckGroupMembersOnline, 0, &hResume); BAIL_ON_LSA_ERROR(dwError); do { DWORD iGroup = 0; if (ppGroupInfoList) { LsaFreeGroupInfoList(dwGroupInfoLevel, ppGroupInfoList, dwNumGroupsFound); ppGroupInfoList = NULL; } dwError = LsaEnumGroups( hLsaConnection, hResume, &dwNumGroupsFound, &ppGroupInfoList); BAIL_ON_LSA_ERROR(dwError); if (!dwNumGroupsFound) { break; } dwTotalGroupsFound+=dwNumGroupsFound; for (iGroup = 0; iGroup < dwNumGroupsFound; iGroup++) { PVOID pGroupInfo = *(ppGroupInfoList + iGroup); switch(dwGroupInfoLevel) { case 0: PrintGroupInfo_0((PLSA_GROUP_INFO_0)pGroupInfo); break; case 1: PrintGroupInfo_1((PLSA_GROUP_INFO_1)pGroupInfo); break; default: fprintf(stderr, "Error: Invalid Group info level %u\n", dwGroupInfoLevel); break; } } } while (dwNumGroupsFound); fprintf(stdout, "TotalNumGroupsFound: %u\n", dwTotalGroupsFound); cleanup: if (ppGroupInfoList) { LsaFreeGroupInfoList(dwGroupInfoLevel, ppGroupInfoList, dwNumGroupsFound); } if ((hResume != (HANDLE)NULL) && (hLsaConnection != (HANDLE)NULL)) { LsaEndEnumGroups(hLsaConnection, hResume); } if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to enumerate groups. Error code %u (%s).\n" "%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } if (dwError == ERROR_INVALID_DATA) { fprintf(stderr, "The groups list has changed while enumerating. " "Try again.\n"); } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to enumerate groups. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int find_group_by_name_main( int argc, char* argv[] ) { DWORD dwError = 0; PSTR pszGroupId = NULL; DWORD dwInfoLevel = 0; HANDLE hLsaConnection = (HANDLE)NULL; PVOID pGroupInfo = NULL; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; BOOLEAN bCountOnly = FALSE; LSA_FIND_FLAGS FindFlags = 0; dwError = ParseArgs(argc, argv, &pszGroupId, &FindFlags, &dwInfoLevel, &bCountOnly); BAIL_ON_LSA_ERROR(dwError); dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); dwError = LsaFindGroupByName( hLsaConnection, pszGroupId, FindFlags, dwInfoLevel, &pGroupInfo); BAIL_ON_LSA_ERROR(dwError); switch(dwInfoLevel) { case 0: PrintGroupInfo_0((PLSA_GROUP_INFO_0)pGroupInfo); break; case 1: PrintGroupInfo_1((PLSA_GROUP_INFO_1)pGroupInfo, bCountOnly); break; default: fprintf(stderr, "Error: Invalid group info level [%u]\n", dwInfoLevel); break; } cleanup: if (pGroupInfo) { LsaFreeGroupInfo(dwInfoLevel, pGroupInfo); } if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } LW_SAFE_FREE_STRING(pszGroupId); return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf(stderr, "Failed to locate group. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf(stderr, "Failed to locate group. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }
int trace_info_main( int argc, char* argv[] ) { DWORD dwError = 0; HANDLE hLsaConnection = (HANDLE)NULL; DWORD dwTraceFlag = 0; PLSA_TRACE_INFO pTraceFlag = NULL; PLSA_TRACE_INFO pTraceFlagArray = NULL; DWORD dwNumFlags = 0; size_t dwErrorBufferSize = 0; BOOLEAN bPrintOrigError = TRUE; if (argc > 1) { dwError = ParseArgs( argc, argv, &pTraceFlagArray, &dwNumFlags, &dwTraceFlag); BAIL_ON_LSA_ERROR(dwError); } if (pTraceFlagArray) { if (geteuid() != 0) { fprintf(stderr, "This program requires super-user privileges.\n"); dwError = LW_ERROR_ACCESS_DENIED; BAIL_ON_LSA_ERROR(dwError); } } dwError = LsaOpenServer(&hLsaConnection); BAIL_ON_LSA_ERROR(dwError); if (pTraceFlagArray) { dwError = LsaSetTraceFlags( hLsaConnection, pTraceFlagArray, dwNumFlags); BAIL_ON_LSA_ERROR(dwError); printf("The trace levels were set successfully\n\n"); } if (dwTraceFlag) { dwError = LsaGetTraceFlag( hLsaConnection, dwTraceFlag, &pTraceFlag); BAIL_ON_LSA_ERROR(dwError); dwError = PrintTraceInfo(pTraceFlag); BAIL_ON_LSA_ERROR(dwError); } if (dwTraceFlag && pTraceFlagArray) { DWORD iFlag = 0; dwError = LsaEnumTraceFlags( hLsaConnection, &pTraceFlagArray, &dwNumFlags); BAIL_ON_LSA_ERROR(dwError); for(; iFlag < dwNumFlags; iFlag++) { PLSA_TRACE_INFO pInfo = &pTraceFlagArray[iFlag]; dwError = PrintTraceInfo(pInfo); BAIL_ON_LSA_ERROR(dwError); } } cleanup: if (hLsaConnection != (HANDLE)NULL) { LsaCloseServer(hLsaConnection); } LW_SAFE_FREE_MEMORY(pTraceFlag); LW_SAFE_FREE_MEMORY(pTraceFlagArray); return (dwError); error: dwError = MapErrorCode(dwError); dwErrorBufferSize = LwGetErrorString(dwError, NULL, 0); if (dwErrorBufferSize > 0) { DWORD dwError2 = 0; PSTR pszErrorBuffer = NULL; dwError2 = LwAllocateMemory( dwErrorBufferSize, (PVOID*)&pszErrorBuffer); if (!dwError2) { DWORD dwLen = LwGetErrorString(dwError, pszErrorBuffer, dwErrorBufferSize); if ((dwLen == dwErrorBufferSize) && !LW_IS_NULL_OR_EMPTY_STR(pszErrorBuffer)) { fprintf( stderr, "Failed to manage trace flags. Error code %u (%s).\n%s\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError)), pszErrorBuffer); bPrintOrigError = FALSE; } } LW_SAFE_FREE_STRING(pszErrorBuffer); } if (bPrintOrigError) { fprintf( stderr, "Failed to manage trace flags. Error code %u (%s).\n", dwError, LW_PRINTF_STRING(LwWin32ExtErrorToName(dwError))); } goto cleanup; }