fix the CRLF issue, review comments related to allow/deny user/groups#861

* fix the CRLF issue
* fixing the allow/deny group review comments
This commit is contained in:
bagajjal 2017-09-05 12:41:46 -07:00 committed by Yanbing
parent a38dfedb68
commit 6e7210d3c1
3 changed files with 91 additions and 65 deletions

View File

@ -10,7 +10,7 @@ typedef enum {
} group_type; } group_type;
char ** getusergroups(const char *user, int *numgroups); char ** getusergroups(const char *user, int *numgroups);
void populate_user_groups(char **gr_name, int *group_index, DWORD groupsread, DWORD totalgroups, LPBYTE buf, group_type groupType); void populate_user_groups(char **group_name, int *group_index, DWORD groupsread, DWORD totalgroups, LPBYTE buf, group_type groupType);
void print_user_groups(const char *user, char **user_groups, int num_user_groups); void print_user_groups(const char *user, char **user_groups, int num_user_groups);
#endif #endif

View File

@ -1073,7 +1073,7 @@ get_machine_domain_name(wchar_t *domain, int size)
if (pBuf != NULL) if (pBuf != NULL)
NetApiBufferFree(pBuf); NetApiBufferFree(pBuf);
return 1; return 1;
} }
@ -1099,13 +1099,20 @@ getusergroups(const char *user, int *ngroups)
DWORD flags = LG_INCLUDE_INDIRECT; DWORD flags = LG_INCLUDE_INDIRECT;
NET_API_STATUS nStatus; NET_API_STATUS nStatus;
wchar_t *w_username = NULL; wchar_t *user_name_utf16 = NULL;
char *user_domain = NULL; char *user_domain = NULL;
LPWSTR wszDCName = NULL; LPWSTR dc_name_utf16 = NULL;
char *user_name = malloc(strlen(user)+1); char **user_groups = NULL;
int num_user_groups = 0;
wchar_t machine_domain_name_utf16[DNLEN + 1] = { 0 };
wchar_t local_user_fmt_utf16[UNLEN + DNLEN + 2] = { 0 };
size_t local_user_fmt_len = UNLEN + DNLEN + 2;
char *user_name = NULL;
user_name = malloc(strlen(user)+1);
if(!user_name) { if(!user_name) {
error("failed to allocate memory!"); error("failed to allocate memory!");
return 0; goto cleanup;
} }
memcpy(user_name, user, strlen(user)+1); memcpy(user_name, user, strlen(user)+1);
@ -1116,46 +1123,46 @@ getusergroups(const char *user, int *ngroups)
*t='\0'; *t='\0';
} }
w_username = utf8_to_utf16(user_name); user_name_utf16 = utf8_to_utf16(user_name);
if (!w_username) { if (!user_name_utf16) {
error("utf8_to_utf16 failed! for %s", user_name); error("utf8_to_utf16 failed! for %s", user_name);
return NULL; goto cleanup;
} }
/* Fetch groups on the Local machine */ /* Fetch groups on the Local machine */
wchar_t w_machine_domain_name[64] = { 0, }; if(get_machine_domain_name(machine_domain_name_utf16, DNLEN+1)) {
if (get_machine_domain_name(w_machine_domain_name, 64)) { if (machine_domain_name_utf16) {
if(!machine_domain_name) if(!machine_domain_name)
machine_domain_name = utf16_to_utf8(w_machine_domain_name); machine_domain_name = utf16_to_utf8(machine_domain_name_utf16);
if (user_domain) {
wcscpy_s(local_user_fmt_utf16, local_user_fmt_len, machine_domain_name_utf16);
wcscat_s(local_user_fmt_utf16, local_user_fmt_len, L"\\");
}
wchar_t local_user_fmt[256] = { 0, }; wcscat_s(local_user_fmt_utf16, local_user_fmt_len, user_name_utf16);
if (user_domain) { nStatus = NetUserGetLocalGroups(NULL,
wcscpy_s(local_user_fmt, 256, w_machine_domain_name); local_user_fmt_utf16,
wcscat_s(local_user_fmt, 256, L"\\"); 0,
flags,
(LPBYTE *)&local_groups,
MAX_PREFERRED_LENGTH,
&num_local_groups_read,
&total_local_groups);
if (NERR_Success != nStatus)
error("Failed to get local groups on this machine, error: %d\n", nStatus);
} }
wcscat_s(local_user_fmt, 256, w_username);
nStatus = NetUserGetLocalGroups(NULL,
local_user_fmt,
0,
flags,
(LPBYTE *)&local_groups,
MAX_PREFERRED_LENGTH,
&num_local_groups_read,
&total_local_groups);
if (NERR_Success != nStatus)
error("Failed to get local groups on this machine, error: %d\n", nStatus);
} }
if (user_domain) { if (user_domain) {
/* Fetch Domain groups */ /* Fetch Domain groups */
nStatus = NetGetDCName(NULL, w_machine_domain_name, (LPBYTE *)&wszDCName); nStatus = NetGetDCName(NULL, machine_domain_name_utf16, (LPBYTE *)&dc_name_utf16);
if (NERR_Success == nStatus) { if (NERR_Success == nStatus) {
debug3("domain controller name: %ls", wszDCName); debug3("domain controller name: %ls", dc_name_utf16);
nStatus = NetUserGetLocalGroups(wszDCName, nStatus = NetUserGetLocalGroups(dc_name_utf16,
w_username, user_name_utf16,
0, 0,
flags, flags,
(LPBYTE *)&domain_groups, (LPBYTE *)&domain_groups,
@ -1164,14 +1171,14 @@ getusergroups(const char *user, int *ngroups)
&total_domain_groups); &total_domain_groups);
if (NERR_Success != nStatus) if (NERR_Success != nStatus)
error("Failed to get domain groups from DC:%s error: %d\n", wszDCName, nStatus); error("Failed to get domain groups from DC:%s error: %d\n", dc_name_utf16, nStatus);
} }
else else
error("Failed to get the domain controller name, error: %d\n", nStatus); error("Failed to get the domain controller name, error: %d\n", nStatus);
/* Fetch global, universal groups */ /* Fetch global, universal groups */
nStatus = NetUserGetGroups(wszDCName, nStatus = NetUserGetGroups(dc_name_utf16,
w_username, user_name_utf16,
0, 0,
(LPBYTE *)&global_universal_groups, (LPBYTE *)&global_universal_groups,
MAX_PREFERRED_LENGTH, MAX_PREFERRED_LENGTH,
@ -1179,14 +1186,12 @@ getusergroups(const char *user, int *ngroups)
&total_global_universal_groups); &total_global_universal_groups);
if (NERR_Success != nStatus) if (NERR_Success != nStatus)
error("Failed to get global,universal groups from DC:%ls error: %d\n", wszDCName, nStatus); error("Failed to get global,universal groups from DC:%ls error: %d\n", dc_name_utf16, nStatus);
} }
int total_user_groups = num_local_groups_read + num_domain_groups_read + num_global_universal_groups_read; int total_user_groups = num_local_groups_read + num_domain_groups_read + num_global_universal_groups_read;
/* populate the output */ /* populate the output */
char **user_groups = NULL;
int num_user_groups = 0;
user_groups = malloc(total_user_groups * sizeof(*user_groups)); user_groups = malloc(total_user_groups * sizeof(*user_groups));
populate_user_groups(user_groups, &num_user_groups, num_local_groups_read, total_local_groups, (LPBYTE) local_groups, LOCAL_GROUP); populate_user_groups(user_groups, &num_user_groups, num_local_groups_read, total_local_groups, (LPBYTE) local_groups, LOCAL_GROUP);
@ -1200,17 +1205,27 @@ getusergroups(const char *user, int *ngroups)
print_user_groups(user, user_groups, num_user_groups); print_user_groups(user, user_groups, num_user_groups);
if(local_groups) cleanup:
NetApiBufferFree(local_groups); if(local_groups)
NetApiBufferFree(local_groups);
if(domain_groups) if(domain_groups)
NetApiBufferFree(domain_groups); NetApiBufferFree(domain_groups);
if(global_universal_groups) if(global_universal_groups)
NetApiBufferFree(global_universal_groups); NetApiBufferFree(global_universal_groups);
if(dc_name_utf16)
NetApiBufferFree(dc_name_utf16);
*ngroups = num_user_groups; if(user_name_utf16)
return user_groups; free(user_name_utf16);
if(user_name)
free(user_name);
*ngroups = num_user_groups;
return user_groups;
} }
/* This method will return in "group@domain" format */ /* This method will return in "group@domain" format */
@ -1221,6 +1236,10 @@ append_domain_to_groupname(char *groupname)
int len = (int) strlen(machine_domain_name) + (int) strlen(groupname) + 2; int len = (int) strlen(machine_domain_name) + (int) strlen(groupname) + 2;
char *groupname_with_domain = malloc(len); char *groupname_with_domain = malloc(len);
if(!groupname_with_domain) {
error("failed to allocate memory!");
return NULL;
}
strcpy_s(groupname_with_domain, len, groupname); strcpy_s(groupname_with_domain, len, groupname);
strcat_s(groupname_with_domain, len, "@"); strcat_s(groupname_with_domain, len, "@");
@ -1232,31 +1251,38 @@ append_domain_to_groupname(char *groupname)
} }
void void
populate_user_groups(char **gr_name, int *group_index, DWORD groupsread, DWORD totalgroups, LPBYTE buf, group_type groupType) populate_user_groups(char **group_name, int *group_index, DWORD groupsread, DWORD totalgroups, LPBYTE buf, group_type groupType)
{ {
if(0 == groupsread) return; if(0 == groupsread) return;
char *user_group_name = NULL;
if (groupType == GLOBAL_UNIVERSAL_GROUP) { if (groupType == GLOBAL_UNIVERSAL_GROUP) {
LPGROUP_USERS_INFO_0 pTmpBuf = (LPGROUP_USERS_INFO_0)buf; LPGROUP_USERS_INFO_0 pTmpBuf = (LPGROUP_USERS_INFO_0)buf;
for (DWORD i = 0; (i < groupsread) && pTmpBuf; i++, pTmpBuf++) { for (DWORD i = 0; (i < groupsread) && pTmpBuf; i++, pTmpBuf++) {
gr_name[*group_index] = append_domain_to_groupname(utf16_to_utf8(pTmpBuf->grui0_name)); if (!(user_group_name = utf16_to_utf8(pTmpBuf->grui0_name))) {
if(gr_name[*group_index])
(*group_index)++;
else
error("utf16_to_utf8 failed to convert:%ls", pTmpBuf->grui0_name); error("utf16_to_utf8 failed to convert:%ls", pTmpBuf->grui0_name);
return;
}
group_name[*group_index] = append_domain_to_groupname(user_group_name);
if(group_name[*group_index])
(*group_index)++;
} }
} else { } else {
LPLOCALGROUP_USERS_INFO_0 pTmpBuf = (LPLOCALGROUP_USERS_INFO_0)buf; LPLOCALGROUP_USERS_INFO_0 pTmpBuf = (LPLOCALGROUP_USERS_INFO_0)buf;
for (DWORD i = 0; (i < groupsread) && pTmpBuf; i++, pTmpBuf++) { for (DWORD i = 0; (i < groupsread) && pTmpBuf; i++, pTmpBuf++) {
if(groupType == DOMAIN_GROUP) if (!(user_group_name = utf16_to_utf8(pTmpBuf->lgrui0_name))) {
gr_name[*group_index] = append_domain_to_groupname(utf16_to_utf8(pTmpBuf->lgrui0_name));
else
gr_name[*group_index] = utf16_to_utf8(pTmpBuf->lgrui0_name);
if (gr_name[*group_index])
(*group_index)++;
else
error("utf16_to_utf8 failed to convert:%ls", pTmpBuf->lgrui0_name); error("utf16_to_utf8 failed to convert:%ls", pTmpBuf->lgrui0_name);
return;
}
if(groupType == DOMAIN_GROUP)
group_name[*group_index] = append_domain_to_groupname(user_group_name);
else
group_name[*group_index] = user_group_name;
if (group_name[*group_index])
(*group_index)++;
} }
} }

View File

@ -155,7 +155,7 @@ ReadConsoleForTermEmul(HANDLE hInput, char *destin, int destinlen)
switch (InputRecord.Event.KeyEvent.uChar.UnicodeChar) { switch (InputRecord.Event.KeyEvent.uChar.UnicodeChar) {
case 0xd: case 0xd:
if (pParams->nReceiveCRLF == ENUM_LF) if (pParams->nReceiveCRLF == ENUM_LF)
NetWriteString2(pParams->Socket, "\n", 1, 0); NetWriteString2(pParams->Socket, "\r", 1, 0);
else else
NetWriteString2(pParams->Socket, "\r\n", 2, 0); NetWriteString2(pParams->Socket, "\r\n", 2, 0);
break; break;