Skip to content

Commit

Permalink
Fix UserInfo and GroupInfo structures
Browse files Browse the repository at this point in the history
Signed-off-by: Leonty Chudinov <[email protected]>
  • Loading branch information
Leonty Chudinov committed Oct 3, 2020
1 parent 58eac80 commit 65010e0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 78 deletions.
88 changes: 36 additions & 52 deletions c/zosaccounts.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ static int accountTrace = FALSE;


/* Obtain the user information structure from user name */
int gidGetUserInfo(const char *userName, UserInfo * info,
int *returnCode, int *reasonCode) {
UserInfo *gidGetUserInfo(const char *userName, int *returnCode, int *reasonCode) {
int nameLength = strlen(userName);
int *reasonCodePtr;
int returnValue;
int retValue = -1;

UserInfo *ptrInfo;
UserInfo *info = NULL;

#ifndef _LP64
reasonCodePtr = (int*) (0x80000000 | ((int)reasonCode));
Expand All @@ -89,8 +87,7 @@ int gidGetUserInfo(const char *userName, UserInfo * info,

/* Copy returned structure */
if (returnValue != 0) {
memcpy (info, (char *)returnValue, sizeof (UserInfo));
retValue = 0;
info = (UserInfo*) returnValue;
}

if (accountTrace) {
Expand All @@ -112,16 +109,16 @@ int gidGetUserInfo(const char *userName, UserInfo * info,
}
}

return retValue;
return info;
}

/* Obtain the user information structure using UID */
int getUserInfo(int uid, UserInfo *info, int *returnCode, int *reasonCode) {
UserInfo *getUserInfo(int uid, int *returnCode, int *reasonCode) {
int *reasonCodePtr;
int returnValue;
int retValue = -1;

UserInfo *ptrInfo;
UserInfo *info = NULL;

#ifndef _LP64
reasonCodePtr = (int*) (0x80000000 | ((int)reasonCode));
Expand All @@ -135,8 +132,7 @@ int getUserInfo(int uid, UserInfo *info, int *returnCode, int *reasonCode) {
reasonCodePtr);

if (returnValue != 0) {
memcpy (info, (char *)returnValue, sizeof (UserInfo));
retValue = 0;
info = (UserInfo*) returnValue;
}

if (accountTrace) {
Expand All @@ -157,7 +153,7 @@ int getUserInfo(int uid, UserInfo *info, int *returnCode, int *reasonCode) {
}
}

return retValue;
return info;
}

/* Obtain the user information structure from user name */
Expand Down Expand Up @@ -208,13 +204,13 @@ int resetZosUserPassword(const char *userName, const char *password, const char
}

/* Obtain the group information structure from group name */
int gidGetGroupInfo(const char *groupName, GroupInfo *info,
int *returnCode, int *reasonCode) {
GroupInfo *gidGetGroupInfo(const char *groupName, int *returnCode, int *reasonCode) {
int groupLength = strlen(groupName);
int *reasonCodePtr;
int returnValue;
int retValue = -1;
*returnCode = *reasonCode = 0;
GroupInfo *info = NULL;

#ifndef _LP64
reasonCodePtr = (int*) (0x80000000 | ((int)reasonCode));
Expand All @@ -230,8 +226,7 @@ int gidGetGroupInfo(const char *groupName, GroupInfo *info,

/* Copy returned structure */
if (returnValue > 0) {
memcpy (info, (char *)returnValue, sizeof (GroupInfo));
retValue = 0;
info = (GroupInfo *) returnValue;
}

if (accountTrace) {
Expand All @@ -252,15 +247,16 @@ int gidGetGroupInfo(const char *groupName, GroupInfo *info,
}
}

return retValue;
return info;
}

/* Obtain the group information structure using GID */
int getGroupInfo(int gid, GroupInfo *info, int *returnCode, int *reasonCode) {
GroupInfo *getGroupInfo(int gid, int *returnCode, int *reasonCode) {
int *reasonCodePtr;
int returnValue;
int retValue = -1;
*returnCode = *reasonCode = 0;
GroupInfo *info = NULL;

#ifndef _LP64
reasonCodePtr = (int*) (0x80000000 | ((int)reasonCode));
Expand All @@ -274,8 +270,7 @@ int getGroupInfo(int gid, GroupInfo *info, int *returnCode, int *reasonCode) {
reasonCodePtr);

if (returnValue > 0) {
memcpy (info, (char *)returnValue, sizeof (GroupInfo));
retValue = 0;
info = (GroupInfo*) returnValue;
}

if (accountTrace) {
Expand All @@ -296,40 +291,31 @@ int getGroupInfo(int gid, GroupInfo *info, int *returnCode, int *reasonCode) {
}
}

return retValue;
return info;
}

/* Return userId from user info structure */
int userInfoGetUserId (UserInfo *info) {
int *temp = (int *)info;
int unameLength = info->GIDN_U_LEN;
int unameLengthindex = (unameLength + 3) /4;
int userId = temp[unameLengthindex + 2];
return userId;
return info->GIDN_USERID;
}

/* Copy user name from User Info structure into userNameBuffer */
void userInfoGetUserName (UserInfo *info, char *userNameBuffer) {
memcpy(userNameBuffer, (const char*)&info->GIDN_U_NAME, info->GIDN_U_LEN);
memcpy(userNameBuffer, info->GIDN_U_NAME, info->GIDN_U_LEN);
}

/* Return groupId from group info structure */
int groupInfoGetGroupId (GroupInfo *info) {
int *temp = (int *)info;
int groupLength = info->GIDS_G_LEN;
int groupLengthindex = (groupLength + 3) /4;
int groupId = temp[groupLengthindex + 2];
return groupId;
return info->GIDS_GROUPID;
}

/* Copy group name from Group Info structure into groupNameBuffer*/
void groupInfoGetGroupName (GroupInfo *info, char *groupNameBuffer) {
memcpy(groupNameBuffer, (const char*)&info->GIDS_G_NAME, info->GIDS_G_LEN);
memcpy(groupNameBuffer, info->GIDS_G_NAME, info->GIDS_G_LEN);
}

/* Obtain userId from character string */
int userIdGet (char *string, int *returnCode, int *reasonCode) {
UserInfo userInfo = {0};
int userId = -1;
int status;

Expand All @@ -342,30 +328,28 @@ int userIdGet (char *string, int *returnCode, int *reasonCode) {
}
else {
/* get user info by name */
status = gidGetUserInfo(string, &userInfo, returnCode, reasonCode);
if (status == 0) {
userId = userInfoGetUserId ( &userInfo);
UserInfo *userInfo = gidGetUserInfo(string, returnCode, reasonCode);
if (userInfo != NULL) {
userId = userInfoGetUserId ( userInfo);
}
}
return userId;
}

/* Obtain user name by UID. userNameBuffer must be at least 8 chars long */
int userGetName(int uid, char *userNameBuffer, int *returnCode, int *reasonCode) {
int status = 0;
UserInfo userInfo = {0};
status = getUserInfo(uid, &userInfo, returnCode, reasonCode);
if (status == 0) {
userInfoGetUserName(&userInfo, userNameBuffer);
int status = -1;
UserInfo *userInfo = getUserInfo(uid, returnCode, reasonCode);
if (userInfo != NULL) {
status = 0;
userInfoGetUserName(userInfo, userNameBuffer);
}
return status;
}

/* Obtain groupId from character string */
int groupIdGet (char *string, int *returnCode, int *reasonCode) {
GroupInfo groupInfo = {0};
int groupId = -1;
int status;

/* Evaluate user ID */
if (string == NULL) {
Expand All @@ -376,21 +360,21 @@ int groupIdGet (char *string, int *returnCode, int *reasonCode) {
}
else {
/* get group info by name */
status = gidGetGroupInfo(string, &groupInfo, returnCode, reasonCode);
if (status == 0) {
groupId = groupInfoGetGroupId (&groupInfo);
GroupInfo *groupInfo = gidGetGroupInfo(string, returnCode, reasonCode);
if (groupInfo != NULL) {
groupId = groupInfoGetGroupId (groupInfo);
}
}
return groupId;
}

/* Obtain group name by GID, groupNameBuffer must be at least 8 chars long */
int groupGetName(int gid, char *groupNameBuffer, int *returnCode, int *reasonCode) {
int status = 0;
GroupInfo groupInfo = {0};
status = getGroupInfo(gid, &groupInfo, returnCode, reasonCode);
if (status == 0) {
groupInfoGetGroupName(&groupInfo, groupNameBuffer);
int status = -1;
GroupInfo *groupInfo = getGroupInfo(gid, returnCode, reasonCode);
if (groupInfo != NULL) {
status = 0;
groupInfoGetGroupName(groupInfo, groupNameBuffer);
}
return status;
}
Expand Down
64 changes: 38 additions & 26 deletions h/zosaccounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,55 @@
#include "charsets.h"
#include "unixfile.h"

#define USER_NAME_LEN 8
#define GROUP_NAME_LEN 8

/* Structure definitioss */
#pragma pack(packed)
typedef struct BPXYGIDS_tag {
int GIDS_G_LEN; // Group name length
char GIDS_G_NAME; // Group name
int GIDS_G_LEN_ID; // Group ID length
int GIDS_GROUPID; // Group ID
int GIDS_COUNT; // Count of array element
int GIDS_M_LEN; // Length of member names
char GIDS_M_NAME; // Name of members
int GIDS_G_LEN; // Group name length (always 8)
char GIDS_G_NAME[GROUP_NAME_LEN]; // Group name
int GIDS_G_LEN_ID; // Group ID length (always 4)
int GIDS_GROUPID; // Group ID
int GIDS_COUNT; // Count of array element
struct {
int GIDS_M_LEN; // Length of member name (always 8)
char GIDS_M_NAME[USER_NAME_LEN]; // Name of member
} GIDS_MEMBERS[];
}BPXYGIDS;

typedef struct BPXYGIDN_tag {
int GIDN_U_LEN; // User name length
char GIDN_U_NAME; // User name
int GIDN_U_LEN_ID; // User ID length
int GIDN_USERID; // User ID
int GIDN_G_LEN; // Group name length
int GIDN_GROUPID; // Group ID
int GIDN_D_LEN; // Length of GIDN_D_NAME
char GIDN_D_NAME; // Initial working directory name
int GIDN_P_LEN; // Length of GIDN_P_NAME
char GIDN_P_NAME; // Initial user program name
int GIDN_U_LEN; // User name length (always 8)
char GIDN_U_NAME[USER_NAME_LEN]; // User name
int GIDN_U_LEN_ID; // User ID length (always 4)
int GIDN_USERID; // User ID
int GIDN_G_LEN; // Group ID length (always 4)
int GIDN_GROUPID; // Group ID
int GIDN_D_LEN; // Length of GIDN_D_NAME
char GIDN_D_NAME[]; // Initial working directory name
/*
GIDN_P_LEN and GIDN_P_NAME cannot be included into the struct.
GIDN_D_NAME is a flexible array member and it must be the last member.
To access GIDN_P_LEN and GIDN_P_NAME use:
int GIDN_P_LEN = *(int*)(userInfo->GIDN_D_NAME + userInfo->GIDN_D_LEN);
char *GIDN_P_NAME = userInfo->GIDN_D_NAME + userInfo->GIDN_D_LEN + 4;
int GIDN_P_LEN; // Length of GIDN_P_NAME (always 8)
char GIDN_P_NAME[]; // Initial user program name
*/
}BPXYGIDN;
#pragma pack(reset)

typedef BPXYGIDS GroupInfo;
typedef BPXYGIDN UserInfo;

#define USER_NAME_LEN 8
#define GROUP_NAME_LEN 8

/* Function Prototype */
int gidGetUserInfo(const char *userName, UserInfo * info,
int *returnCode, int *reasonCode);
int getUserInfo(int uid, UserInfo *info, int *returnCode, int *reasonCode);
int gidGetGroupInfo(const char *groupName, GroupInfo *info,
int *returnCode, int *reasonCode);
int getGroupInfo(int gid, GroupInfo *info, int *returnCode, int *reasonCode);
UserInfo *gidGetUserInfo(const char *userName, int *returnCode, int *reasonCode);
UserInfo *getUserInfo(int uid, int *returnCode, int *reasonCode);
GroupInfo *gidGetGroupInfo(const char *groupName, int *returnCode, int *reasonCode);
GroupInfo *getGroupInfo(int gid, int *returnCode, int *reasonCode);
int userInfoGetUserId (UserInfo *info);
void userInfoGetUserName (UserInfo *info, char *userNameBuffer);
int groupInfoGetGroupId (GroupInfo *info);
Expand Down

0 comments on commit 65010e0

Please sign in to comment.