APIERR MNetGroupGetInfo( const TCHAR FAR * pszServer, TCHAR FAR * pszGroupName, UINT Level, BYTE FAR ** ppbBuffer ) { APIERR err; USHORT cbTotalAvail; // get a small buffer *ppbBuffer = MNetApiBufferAlloc(LITTLE_BUFFER_SIZE); if (*ppbBuffer == NULL) { return(ERROR_NOT_ENOUGH_MEMORY); } err = NetGroupGetInfo(pszServer, pszGroupName, Level, *ppbBuffer, LITTLE_BUFFER_SIZE, & cbTotalAvail); // If we're returning an error that's not moredata, free the buffer first if (err && err != ERROR_MORE_DATA && err != NERR_BufTooSmall) { MNetApiBufferFree(ppbBuffer); } return (err); } // MNetGroupGetInfo
USHORT MNetGroupGetInfo ( const CHAR FAR * pszServer, CHAR FAR * pszGroupName, SHORT Level, CHAR FAR ** ppBuffer ) { USHORT usReturnCode, cbTotalAvail; // get a small buffer *ppBuffer = MGetBuffer(LITTLE_BUFFER_SIZE); if (*ppBuffer == NULL) { return(ERROR_NOT_ENOUGH_MEMORY); } usReturnCode = NetGroupGetInfo(pszServer, pszGroupName, Level, *ppBuffer, LITTLE_BUFFER_SIZE, & cbTotalAvail); // If we're returning an error that's not moredata, free the buffer first if (usReturnCode && usReturnCode != ERROR_MORE_DATA && usReturnCode != NERR_BufTooSmall) { NetApiBufferFree(*ppBuffer); } return (usReturnCode); }
NET_API_STATUS netapitest_group(struct libnetapi_ctx *ctx, const char *hostname) { NET_API_STATUS status = 0; const char *username, *groupname, *groupname2; uint8_t *buffer = NULL; struct GROUP_INFO_0 g0; uint32_t parm_err = 0; uint32_t levels[] = { 0, 1, 2, 3}; uint32_t enum_levels[] = { 0, 1, 2, 3}; uint32_t getmem_levels[] = { 0, 1}; int i; printf("NetGroup tests\n"); username = "******"; groupname = "torture_test_group"; groupname2 = "torture_test_group2"; /* cleanup */ NetGroupDel(hostname, groupname); NetGroupDel(hostname, groupname2); NetUserDel(hostname, username); /* add a group */ g0.grpi0_name = groupname; printf("testing NetGroupAdd\n"); status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err); if (status) { NETAPI_STATUS(ctx, status, "NetGroupAdd"); goto out; } /* 2nd add must fail */ status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err); if (status == 0) { NETAPI_STATUS(ctx, status, "NetGroupAdd"); goto out; } /* test enum */ for (i=0; i<ARRAY_SIZE(enum_levels); i++) { status = test_netgroupenum(hostname, enum_levels[i], groupname); if (status) { NETAPI_STATUS(ctx, status, "NetGroupEnum"); goto out; } } /* basic queries */ for (i=0; i<ARRAY_SIZE(levels); i++) { printf("testing NetGroupGetInfo level %d\n", levels[i]); status = NetGroupGetInfo(hostname, groupname, levels[i], &buffer); if (status && status != 124) { NETAPI_STATUS(ctx, status, "NetGroupGetInfo"); goto out; } } /* group rename */ g0.grpi0_name = groupname2; printf("testing NetGroupSetInfo level 0\n"); status = NetGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err); switch (status) { case 0: break; case 50: /* not supported */ case 124: /* not implemented */ groupname2 = groupname; goto skip_rename; default: NETAPI_STATUS(ctx, status, "NetGroupSetInfo"); goto out; } /* should not exist anymore */ status = NetGroupDel(hostname, groupname); if (status == 0) { NETAPI_STATUS(ctx, status, "NetGroupDel"); goto out; } skip_rename: /* query info */ for (i=0; i<ARRAY_SIZE(levels); i++) { status = NetGroupGetInfo(hostname, groupname2, levels[i], &buffer); if (status && status != 124) { NETAPI_STATUS(ctx, status, "NetGroupGetInfo"); goto out; } } /* add user to group */ status = test_netuseradd(hostname, username); if (status) { NETAPI_STATUS(ctx, status, "NetUserAdd"); goto out; } /* should not be member */ for (i=0; i<ARRAY_SIZE(getmem_levels); i++) { status = test_netgroupgetusers(hostname, getmem_levels[i], groupname2, NULL); if (status) { NETAPI_STATUS(ctx, status, "NetGroupGetUsers"); goto out; } } printf("testing NetGroupAddUser\n"); status = NetGroupAddUser(hostname, groupname2, username); if (status) { NETAPI_STATUS(ctx, status, "NetGroupAddUser"); goto out; } /* should be member */ for (i=0; i<ARRAY_SIZE(getmem_levels); i++) { status = test_netgroupgetusers(hostname, getmem_levels[i], groupname2, username); if (status) { NETAPI_STATUS(ctx, status, "NetGroupGetUsers"); goto out; } } printf("testing NetGroupDelUser\n"); status = NetGroupDelUser(hostname, groupname2, username); if (status) { NETAPI_STATUS(ctx, status, "NetGroupDelUser"); goto out; } /* should not be member */ status = test_netgroupgetusers(hostname, 0, groupname2, NULL); if (status) { NETAPI_STATUS(ctx, status, "NetGroupGetUsers"); goto out; } /* set it again via exlicit member set */ status = test_netgroupsetusers(hostname, groupname2, 0, 1, &username); if (status) { NETAPI_STATUS(ctx, status, "NetGroupSetUsers"); goto out; } /* should be member */ status = test_netgroupgetusers(hostname, 0, groupname2, username); if (status) { NETAPI_STATUS(ctx, status, "NetGroupGetUsers"); goto out; } #if 0 /* wipe out member list */ status = test_netgroupsetusers(hostname, groupname2, 0, 0, NULL); if (status) { NETAPI_STATUS(ctx, status, "NetGroupSetUsers"); goto out; } /* should not be member */ status = test_netgroupgetusers(hostname, 0, groupname2, NULL); if (status) { NETAPI_STATUS(ctx, status, "NetGroupGetUsers"); goto out; } #endif status = NetUserDel(hostname, username); if (status) { NETAPI_STATUS(ctx, status, "NetUserDel"); goto out; } /* delete */ printf("testing NetGroupDel\n"); status = NetGroupDel(hostname, groupname2); if (status) { NETAPI_STATUS(ctx, status, "NetGroupDel"); goto out; }; /* should not exist anymore */ status = NetGroupGetInfo(hostname, groupname2, 0, &buffer); if (status == 0) { NETAPI_STATUS_MSG(ctx, status, "NetGroupGetInfo", "expected failure and error code"); goto out; }; status = 0; printf("NetGroup tests succeeded\n"); out: if (status != 0) { printf("NetGroup testsuite failed with: %s\n", libnetapi_get_error_string(ctx, status)); } return status; }
static void enum_groups (domlist_t *mach, const char *sep, DWORD id_offset, char *disp_groupname, int print_current) { WCHAR machine[INTERNET_MAX_HOST_NAME_LENGTH + 1]; GROUP_INFO_2 *buffer; DWORD entriesread = 0; DWORD totalentries = 0; DWORD_PTR resume_handle = 0; WCHAR gname[GNLEN + 1]; DWORD rc; int ret = mbstowcs (machine, mach->str, INTERNET_MAX_HOST_NAME_LENGTH + 1); if (ret < 1 || ret >= INTERNET_MAX_HOST_NAME_LENGTH + 1) { fprintf (stderr, "%s: Invalid machine name '%s'. Skipping...\n", program_invocation_short_name, mach->str); return; } do { DWORD i; if (disp_groupname != NULL) { mbstowcs (gname, disp_groupname, GNLEN + 1); rc = NetGroupGetInfo (machine, (LPWSTR) & gname, 2, (void *) &buffer); entriesread=1; /* Avoid annoying error messages just because the group hasn't been found. */ if (rc == NERR_GroupNotFound) return; } else rc = NetGroupEnum (machine, 2, (void *) & buffer, MAX_PREFERRED_LENGTH, &entriesread, &totalentries, &resume_handle); switch (rc) { case ERROR_ACCESS_DENIED: print_win_error (rc); return; case ERROR_MORE_DATA: case ERROR_SUCCESS: break; default: print_win_error (rc); return; } for (i = 0; i < entriesread; i++) { WCHAR domain_name[MAX_DOMAIN_NAME_LEN + 1]; DWORD domname_len = MAX_DOMAIN_NAME_LEN + 1; char psid_buffer[MAX_SID_LEN]; PSID psid = (PSID) psid_buffer; DWORD sid_length = MAX_SID_LEN; SID_NAME_USE acc_type; int gid = buffer[i].grpi2_group_id; if (!LookupAccountNameW (machine, buffer[i].grpi2_name, psid, &sid_length, domain_name, &domname_len, &acc_type)) { print_win_error (GetLastError ()); fprintf(stderr, " (%ls)\n", buffer[i].grpi2_name); continue; } else if (acc_type == SidTypeDomain) { WCHAR domname[MAX_DOMAIN_NAME_LEN + GNLEN + 2]; wcscpy (domname, machine); wcscat (domname, L"\\"); wcscat (domname, buffer[i].grpi2_name); sid_length = MAX_SID_LEN; domname_len = MAX_DOMAIN_NAME_LEN + 1; if (!LookupAccountNameW (machine, domname, psid, &sid_length, domain_name, &domname_len, &acc_type)) { print_win_error (GetLastError ()); fprintf(stderr, " (%ls)\n", domname); continue; } } if (!print_current) /* fall through */; else if (EqualSid (curr_pgrp.psid, psid)) got_curr_pgrp = TRUE; printf ("%ls%s%ls:%s:%" PRIu32 ":\n", mach->with_dom ? domain_name : L"", mach->with_dom ? sep : "", buffer[i].grpi2_name, put_sid (psid), (unsigned int) (id_offset + gid)); } NetApiBufferFree (buffer); } while (rc == ERROR_MORE_DATA); }