From: John Hodge Date: Wed, 5 Jan 2011 08:22:37 +0000 (+0800) Subject: Restuctured cokebank X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=229e4bcc0d38421e8baf266d2bf585ad3e88a327;p=tpg%2Fopendispense2.git Restuctured cokebank Moved all references to Unix UIDs out to bank.c where it can be safely removed if required. - The password code is still in main.c, because that doesn't matter (if LDAP goes down, users will just be unable to use non-trusted machines. --- diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c index cc09fa5..d78f90f 100644 --- a/src/cokebank_basic/bank.c +++ b/src/cokebank_basic/bank.c @@ -10,7 +10,9 @@ */ #include #include +#include #include +#include #include "common.h" enum { @@ -21,6 +23,9 @@ enum { USER_TYPE_GOD = 0x03 }; +// === PROTOTYPES === +static int GetUnixID(const char *Name); + // === GLOBALS === tUser *gaBank_Users; int giBank_NumUsers; @@ -95,20 +100,13 @@ int Bank_GetMinAllowedBalance(int ID) } } -int Bank_GetUserUnixID(int ID) -{ - if( ID < 0 || ID >= giBank_NumUsers ) - return -1; - - return gaBank_Users[ID].UnixID; -} - /** * \brief Create a new user in our database */ -int Bank_AddUser(int UnixID) +int Bank_AddUser(const char *Username) { void *tmp; + int uid = GetUnixID(Username); // Can has moar space plz? tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0])); @@ -116,7 +114,7 @@ int Bank_AddUser(int UnixID) gaBank_Users = tmp; // Crete new user - gaBank_Users[giBank_NumUsers].UnixID = UnixID; + gaBank_Users[giBank_NumUsers].UnixID = uid; gaBank_Users[giBank_NumUsers].Balance = 0; gaBank_Users[giBank_NumUsers].Flags = 0; @@ -129,3 +127,46 @@ int Bank_AddUser(int UnixID) return 0; } + +// --- +// Unix user dependent code +// TODO: Modify to keep its own list of usernames +// --- +char *Bank_GetUserName(int ID) +{ + struct passwd *pwd; + + if( ID < 0 || ID >= giBank_NumUsers ) + return NULL; + + if( gaBank_Users[ID].UnixID == -1 ) + return strdup(">sales"); + + if( gaBank_Users[ID].UnixID == -2 ) + return strdup(">liability"); + + pwd = getpwuid(gaBank_Users[ID].UnixID); + if( !pwd ) return NULL; + + return strdup(pwd->pw_name); +} + +static int GetUnixID(const char *Username) +{ + int uid; + + if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into + uid = -1; + } + else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from + uid = -2; + } + else { + struct passwd *pwd; + // Get user ID + pwd = getpwnam(Username); + if( !pwd ) return -1; + uid = pwd->pw_uid; + } + return uid; +} diff --git a/src/cokebank_basic/common.h b/src/cokebank_basic/common.h index e6e37c6..fb1e3e9 100644 --- a/src/cokebank_basic/common.h +++ b/src/cokebank_basic/common.h @@ -16,4 +16,15 @@ typedef struct sUser { int Flags; } tUser; +// === IMPORTS === +extern int Bank_GetMinAllowedBalance(int ID); +extern int Bank_GetUserBalance(int ID); +extern int Bank_AlterUserBalance(int ID, int Delta); +extern char *Bank_GetUserName(int ID); +extern int Bank_GetUserByName(const char *Username); +extern int Bank_AddUser(const char *Username); +extern FILE *gBank_File; +extern tUser *gaBank_Users; +extern int giBank_NumUsers; + #endif diff --git a/src/cokebank_basic/main.c b/src/cokebank_basic/main.c index e635b91..6bbc3f2 100644 --- a/src/cokebank_basic/main.c +++ b/src/cokebank_basic/main.c @@ -9,22 +9,10 @@ */ #include #include -#include #include #include #include "common.h" -// === IMPORTS === -extern int Bank_GetMinAllowedBalance(int ID); -extern int Bank_GetUserBalance(int ID); -extern int Bank_AlterUserBalance(int ID, int Delta); -extern int Bank_GetUserByUnixID(int UnixID); -extern int Bank_GetUserUnixID(int ID); -extern int Bank_AddUser(int UnixID); -extern FILE *gBank_File; -extern tUser *gaBank_Users; -extern int giBank_NumUsers; - // === PROTOTYPES === void Init_Cokebank(const char *Argument); int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); @@ -96,19 +84,7 @@ int GetBalance(int User) */ char *GetUserName(int User) { - struct passwd *pwd; - int unixid = Bank_GetUserUnixID(User); - - if( unixid == -1 ) - return strdup(">sales"); - - if( unixid == -2 ) - return strdup(">liability"); - - pwd = getpwuid(unixid); - if( !pwd ) return NULL; - - return strdup(pwd->pw_name); + return Bank_GetUserName(User); } /** @@ -116,26 +92,12 @@ char *GetUserName(int User) */ int GetUserID(const char *Username) { - int ret, uid; - - if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into - uid = -1; - } - else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from - uid = -2; - } - else { - struct passwd *pwd; - // Get user ID - pwd = getpwnam(Username); - if( !pwd ) return -1; - uid = pwd->pw_uid; - } + int ret; // Get internal ID (or create new user) - ret = Bank_GetUserByUnixID(uid); + ret = Bank_GetUserByName(Username); if( ret == -1 ) { - ret = Bank_AddUser(uid); + ret = Bank_AddUser(Username); } return ret;