From 0a4c98ea7934d4fb61121fb8cf8c4d94f973b950 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 8 Jan 2011 17:08:33 +0800 Subject: [PATCH] Cleanup - Multiple changes, see description - Enabled -Wextra and -Werror - Added Bank_ prefix to cokebank exports - Merged cokebank into one file, cleaned up duplication of functions and direct abstractions. - Added doorgroup checking to the door handler. --- src/cokebank.h | 18 +-- src/cokebank_basic/Makefile | 11 +- src/cokebank_basic/bank.c | 250 ------------------------------ src/cokebank_basic/common.h | 13 -- src/cokebank_basic/main.c | 298 +++++++++++++++++++++++++++++++----- src/server/Makefile | 10 +- src/server/common.h | 2 + src/server/dispense.c | 33 ++-- src/server/handler_coke.c | 4 +- src/server/handler_door.c | 6 +- src/server/handler_snack.c | 4 +- src/server/server.c | 55 ++++--- 12 files changed, 343 insertions(+), 361 deletions(-) delete mode 100644 src/cokebank_basic/bank.c diff --git a/src/cokebank.h b/src/cokebank.h index 25a3566..77ac110 100644 --- a/src/cokebank.h +++ b/src/cokebank.h @@ -29,49 +29,49 @@ enum eCokebank_Flags { * \param Ammount Amount of money (in cents) to transfer * \param Reason Reason for the transfer */ -extern int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); +extern int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); /** * \brief Get flags on an account * \param User UID to get flags from * \see eCokebank_Flags */ -extern int GetFlags(int User); +extern int Bank_GetFlags(int User); /** * \brief Set an account's flags * \param User UID to set flags on * \param Mask Mask of flags changed * \param Value Final value of changed flags */ -extern int SetFlags(int User, int Mask, int Value); +extern int Bank_SetFlags(int User, int Mask, int Value); /** * \brief Get an account's balance * \param User UID to query */ -extern int GetBalance(int User); +extern int Bank_GetBalance(int User); /** * \brief Get the name associated with an account * \return Heap string */ -extern char *GetUserName(int User); +extern char *Bank_GetUserName(int User); /** * \brief Get a UID from a passed name */ -extern int GetUserID(const char *Username); +extern int Bank_GetUserID(const char *Username); /** * \brief Create a new account */ -extern int CreateUser(const char *Username); +extern int Bank_CreateUser(const char *Username); /** * \brief Get the maximum UID * \note Used for iterating accounts */ -extern int GetMaxID(void); +extern int Bank_GetMaxID(void); /** * \brief Validates a user's authentication * \param Salt Salt given to the client for hashing the password * \param Username Username used * \param Password Password sent by the client */ -extern int GetUserAuth(const char *Salt, const char *Username, const char *Password); +extern int Bank_GetUserAuth(const char *Salt, const char *Username, const char *Password); #endif diff --git a/src/cokebank_basic/Makefile b/src/cokebank_basic/Makefile index 7a0d815..6145889 100644 --- a/src/cokebank_basic/Makefile +++ b/src/cokebank_basic/Makefile @@ -1,9 +1,9 @@ BIN := ../../cokebank.so -OBJ := main.o bank.o +OBJ := main.o CPPFLAGS := -CFLAGS := -Wall -Werror -g -fPIC +CFLAGS := -Wall -Wextra -Werror -g -fPIC -Wmissing-prototypes -Wstrict-prototypes LDFLAGS := -shared -Wl,-soname,cokebank.so ifneq ($(USE_LDAP),) @@ -11,15 +11,20 @@ ifneq ($(USE_LDAP),) LDFLAGS += -lldap endif +DEPFILES := $(OBJ:%.o=%.d) + .PHONY: all clean all: $(BIN) clean: - $(RM) $(BIN) $(OBJ) + $(RM) $(BIN) $(OBJ) $(DEPFILES) $(BIN): $(OBJ) $(CC) $(LDFLAGS) -o $(BIN) $(OBJ) %.o: %.c $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + @cpp $< -MM -MF $*.d + +-include $(DEPFILES) diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c deleted file mode 100644 index 56db422..0000000 --- a/src/cokebank_basic/bank.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * OpenDispense 2 - * UCC (University [of WA] Computer Club) Electronic Accounting System - * - Cokebank (Basic Version) - * - * bank.c - Actual bank database - * - * This file is licenced under the 3-clause BSD Licence. See the file COPYING - * for full details. - */ -#include -#include -#include -#include -#include -#include -#include "common.h" - -#define USE_UNIX_GROUPS 1 - -// === PROTOTYPES === -static int GetUnixID(const char *Username); - -// === GLOBALS === -tUser *gaBank_Users; - int giBank_NumUsers; -FILE *gBank_File; - -// === CODE === -static int Bank_int_WriteEntry(int ID) -{ - if( ID < 0 || ID >= giBank_NumUsers ) { - return -1; - } - - // Commit to file - fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET); - fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File); - - return 0; -} - -int Bank_GetUserByName(const char *Username) -{ - int i, uid; - - uid = GetUnixID(Username); - - // Expensive search :( - for( i = 0; i < giBank_NumUsers; i ++ ) - { - if( gaBank_Users[i].UnixID == uid ) - return i; - } - - return -1; -} - -int Bank_GetUserBalance(int ID) -{ - if( ID < 0 || ID >= giBank_NumUsers ) - return INT_MIN; - - return gaBank_Users[ID].Balance; -} - -int Bank_GetUserFlags(int ID) -{ - if( ID < 0 || ID >= giBank_NumUsers ) - return -1; - - // root - if( gaBank_Users[ID].UnixID == 0 ) { - gaBank_Users[ID].Flags |= USER_FLAG_WHEEL|USER_FLAG_COKE; - } - - #if USE_UNIX_GROUPS - // TODO: Implement checking the PAM groups and status instead, then - // fall back on the database. (and update if there is a difference) - if( gaBank_Users[ID].UnixID > 0 ) - { - struct passwd *pwd; - struct group *grp; - int i; - - // Get username - pwd = getpwuid( gaBank_Users[ID].UnixID ); - - // Check for additions to the "coke" group - grp = getgrnam("coke"); - if( grp ) { - for( i = 0; grp->gr_mem[i]; i ++ ) - { - if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) { - gaBank_Users[ID].Flags |= USER_FLAG_COKE; - break ; - } - } - } - - // Check for additions to the "wheel" group - grp = getgrnam("wheel"); - if( grp ) { - for( i = 0; grp->gr_mem[i]; i ++ ) - { - if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) { - gaBank_Users[ID].Flags |= USER_FLAG_WHEEL; - break ; - } - } - } - } - #endif - - return gaBank_Users[ID].Flags; -} - -int Bank_SetUserFlags(int ID, int Mask, int Value) -{ - // Sanity - if( ID < 0 || ID >= giBank_NumUsers ) - return -1; - - // Silently ignore changes to root and meta accounts - if( gaBank_Users[ID].UnixID <= 0 ) return 0; - - gaBank_Users[ID].Flags &= ~Mask; - gaBank_Users[ID].Flags |= Value; - - Bank_int_WriteEntry(ID); - - return 0; -} - -int Bank_AlterUserBalance(int ID, int Delta) -{ - // Sanity - if( ID < 0 || ID >= giBank_NumUsers ) - return -1; - - // Update - gaBank_Users[ID].Balance += Delta; - - Bank_int_WriteEntry(ID); - - return 0; -} - -int Bank_GetMinAllowedBalance(int ID) -{ - int flags; - if( ID < 0 || ID >= giBank_NumUsers ) - return 0; - - flags = Bank_GetUserFlags(ID); - - // Internal accounts have no limit - if( (flags & USER_FLAG_INTERNAL) ) - return INT_MIN; - - // Wheel is allowed to go to -$100 - if( (flags & USER_FLAG_WHEEL) ) - return -10000; - - // Coke is allowed to go to -$20 - if( (flags & USER_FLAG_COKE) ) - return -2000; - - // For everyone else, no negative - return 0; -} - -/** - * \brief Create a new user in our database - */ -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])); - if( !tmp ) return -1; - gaBank_Users = tmp; - - // Crete new user - gaBank_Users[giBank_NumUsers].UnixID = uid; - gaBank_Users[giBank_NumUsers].Balance = 0; - gaBank_Users[giBank_NumUsers].Flags = 0; - - if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) { - gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL; - } - else if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) { - gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL; - } - else if( strcmp(Username, "root") == 0 ) { - gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE; - } - - // Increment count - giBank_NumUsers ++; - - Bank_int_WriteEntry(giBank_NumUsers - 1); - - 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(COKEBANK_SALES_ACCT); - - if( gaBank_Users[ID].UnixID == -2 ) - return strdup(COKEBANK_DEBT_ACCT); - - 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, COKEBANK_SALES_ACCT) == 0 ) { // Pseudo account that sales are made into - uid = -1; - } - else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 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 83480b7..8f9a406 100644 --- a/src/cokebank_basic/common.h +++ b/src/cokebank_basic/common.h @@ -18,17 +18,4 @@ 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_GetUserFlags(int ID); -extern int Bank_SetUserFlags(int ID, int Mask, int Value); -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 f7d32f0..21f4e06 100644 --- a/src/cokebank_basic/main.c +++ b/src/cokebank_basic/main.c @@ -11,6 +11,10 @@ #include #include #include +#include +#include +#include +#include #include #include "common.h" #if USE_LDAP @@ -25,18 +29,26 @@ * */ -// === HACKS === +#define USE_UNIX_GROUPS 1 #define HACK_TPG_NOAUTH 1 #define HACK_ROOT_NOAUTH 1 // === PROTOTYPES === void Init_Cokebank(const char *Argument); - int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); - int GetBalance(int User); -char *GetUserName(int User); - int GetUserID(const char *Username); - int GetMaxID(void); - int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString); + int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); + int Bank_CreateUser(const char *Username); + int Bank_GetMaxID(void); +static int Bank_int_WriteEntry(int ID); + int Bank_GetUserID(const char *Username); + int Bank_GetBalance(int User); + int Bank_GetFlags(int User); + int Bank_SetFlags(int User, int Mask, int Value); + int Bank_int_AlterUserBalance(int ID, int Delta); + int Bank_int_GetMinAllowedBalance(int ID); + int Bank_int_AddUser(const char *Username); +char *Bank_GetUserName(int User); + int Bank_int_GetUnixID(const char *Username); + int Bank_GetUserAuth(const char *Salt, const char *Username, const char *PasswordString); #if USE_LDAP char *ReadLDAPValue(const char *Filter, char *Value); #endif @@ -48,6 +60,9 @@ FILE *gBank_LogFile; char *gsLDAPPath = "ldapi:///"; LDAP *gpLDAP; #endif +tUser *gaBank_Users; + int giBank_NumUsers; +FILE *gBank_File; // === CODE === /** @@ -123,77 +138,272 @@ void Init_Cokebank(const char *Argument) * \param Reason Reason for the transfer (essentially a comment) * \return Boolean failure */ -int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason) +int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason) { - int srcBal = Bank_GetUserBalance(SourceUser); - int dstBal = Bank_GetUserBalance(DestUser); + int srcBal = Bank_GetBalance(SourceUser); + int dstBal = Bank_GetBalance(DestUser); - if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) ) + if( srcBal - Ammount < Bank_int_GetMinAllowedBalance(SourceUser) ) return 1; - if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) ) + if( dstBal + Ammount < Bank_int_GetMinAllowedBalance(DestUser) ) return 1; - Bank_AlterUserBalance(DestUser, Ammount); - Bank_AlterUserBalance(SourceUser, -Ammount); + Bank_int_AlterUserBalance(DestUser, Ammount); + Bank_int_AlterUserBalance(SourceUser, -Ammount); fprintf(gBank_LogFile, "Transfer %ic #%i{%i} > #%i{%i} [%i, %i] (%s)\n", Ammount, SourceUser, srcBal, DestUser, dstBal, srcBal - Ammount, dstBal + Ammount, Reason); return 0; } -int GetFlags(int User) +int Bank_CreateUser(const char *Username) { - return Bank_GetUserFlags(User); + int ret; + + ret = Bank_GetUserID(Username); + if( ret != -1 ) return -1; + + return Bank_int_AddUser(Username); } -int SetFlags(int User, int Mask, int Flags) +int Bank_GetMaxID(void) { - return Bank_SetUserFlags(User, Mask, Flags); + return giBank_NumUsers; } -/** - * \brief Get the balance of the passed user - */ -int GetBalance(int User) +static int Bank_int_WriteEntry(int ID) { - return Bank_GetUserBalance(User); + if( ID < 0 || ID >= giBank_NumUsers ) { + return -1; + } + + // Commit to file + fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET); + fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File); + + return 0; } /** - * \brief Return the name the passed user + * \brief Get the User ID of the named user */ -char *GetUserName(int User) +int Bank_GetUserID(const char *Username) +{ + int i, uid; + + uid = Bank_int_GetUnixID(Username); + + // Expensive search :( + for( i = 0; i < giBank_NumUsers; i ++ ) + { + if( gaBank_Users[i].UnixID == uid ) + return i; + } + + return -1; +} + +int Bank_GetBalance(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) + return INT_MIN; + + return gaBank_Users[ID].Balance; +} + +int Bank_GetFlags(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + // root + if( gaBank_Users[ID].UnixID == 0 ) { + gaBank_Users[ID].Flags |= USER_FLAG_WHEEL|USER_FLAG_COKE; + } + + #if USE_UNIX_GROUPS + // TODO: Implement checking the PAM groups and status instead, then + // fall back on the database. (and update if there is a difference) + if( gaBank_Users[ID].UnixID > 0 ) + { + struct passwd *pwd; + struct group *grp; + int i; + + // Get username + pwd = getpwuid( gaBank_Users[ID].UnixID ); + + // Check for additions to the "coke" group + grp = getgrnam("coke"); + if( grp ) { + for( i = 0; grp->gr_mem[i]; i ++ ) + { + if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) { + gaBank_Users[ID].Flags |= USER_FLAG_COKE; + break ; + } + } + } + + // Check for additions to the "wheel" group + grp = getgrnam("wheel"); + if( grp ) { + for( i = 0; grp->gr_mem[i]; i ++ ) + { + if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) { + gaBank_Users[ID].Flags |= USER_FLAG_WHEEL; + break ; + } + } + } + } + #endif + + return gaBank_Users[ID].Flags; +} + +int Bank_SetFlags(int ID, int Mask, int Value) +{ + // Sanity + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + // Silently ignore changes to root and meta accounts + if( gaBank_Users[ID].UnixID <= 0 ) return 0; + + gaBank_Users[ID].Flags &= ~Mask; + gaBank_Users[ID].Flags |= Value; + + Bank_int_WriteEntry(ID); + + return 0; +} + +int Bank_int_AlterUserBalance(int ID, int Delta) { - return Bank_GetUserName(User); + // Sanity + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + // Update + gaBank_Users[ID].Balance += Delta; + + Bank_int_WriteEntry(ID); + + return 0; +} + +int Bank_int_GetMinAllowedBalance(int ID) +{ + int flags; + if( ID < 0 || ID >= giBank_NumUsers ) + return 0; + + flags = Bank_GetFlags(ID); + + // Internal accounts have no limit + if( (flags & USER_FLAG_INTERNAL) ) + return INT_MIN; + + // Wheel is allowed to go to -$100 + if( (flags & USER_FLAG_WHEEL) ) + return -10000; + + // Coke is allowed to go to -$20 + if( (flags & USER_FLAG_COKE) ) + return -2000; + + // For everyone else, no negative + return 0; } /** - * \brief Get the User ID of the named user + * \brief Create a new user in our database */ -int GetUserID(const char *Username) +int Bank_int_AddUser(const char *Username) { - return Bank_GetUserByName(Username); + void *tmp; + int uid = Bank_int_GetUnixID(Username); + + // Can has moar space plz? + tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0])); + if( !tmp ) return -1; + gaBank_Users = tmp; + + // Crete new user + gaBank_Users[giBank_NumUsers].UnixID = uid; + gaBank_Users[giBank_NumUsers].Balance = 0; + gaBank_Users[giBank_NumUsers].Flags = 0; + + if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) { + gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL; + } + else if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) { + gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL; + } + else if( strcmp(Username, "root") == 0 ) { + gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE; + } + + // Increment count + giBank_NumUsers ++; + + Bank_int_WriteEntry(giBank_NumUsers - 1); + + return 0; } -int CreateUser(const char *Username) +// --- +// Unix user dependent code +// TODO: Modify to keep its own list of usernames +// --- +/** + * \brief Return the name the passed user + */ +char *Bank_GetUserName(int ID) { - int ret; + struct passwd *pwd; - ret = Bank_GetUserByName(Username); - if( ret != -1 ) return -1; + if( ID < 0 || ID >= giBank_NumUsers ) + return NULL; - return Bank_AddUser(Username); + if( gaBank_Users[ID].UnixID == -1 ) + return strdup(COKEBANK_SALES_ACCT); + + if( gaBank_Users[ID].UnixID == -2 ) + return strdup(COKEBANK_DEBT_ACCT); + + pwd = getpwuid(gaBank_Users[ID].UnixID); + if( !pwd ) return NULL; + + return strdup(pwd->pw_name); } -int GetMaxID(void) +int Bank_int_GetUnixID(const char *Username) { - return giBank_NumUsers; + int uid; + + if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) { // Pseudo account that sales are made into + uid = -1; + } + else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 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; } + /** * \brief Authenticate a user * \return User ID, or -1 if authentication failed */ -int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString) +int Bank_GetUserAuth(const char *Salt, const char *Username, const char *PasswordString) { #if USE_LDAP uint8_t hash[20]; @@ -204,15 +414,23 @@ int GetUserAuth(const char *Salt, const char *Username, const char *PasswordStri char *passhash; #endif + #if 1 + // Only here to shut GCC up (until password auth is implemented + if( Salt == NULL ) + return -1; + if( PasswordString == NULL ) + return -1; + #endif + #if HACK_TPG_NOAUTH if( strcmp(Username, "tpg") == 0 ) - return GetUserID("tpg"); + return Bank_GetUserID("tpg"); #endif #if HACK_ROOT_NOAUTH if( strcmp(Username, "root") == 0 ) { - int ret = GetUserID("root"); + int ret = Bank_GetUserID("root"); if( ret == -1 ) - return CreateUser("root"); + return Bank_CreateUser("root"); return ret; } #endif diff --git a/src/server/Makefile b/src/server/Makefile index 85678a8..a773eb2 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -6,9 +6,11 @@ OBJ += dispense.o itemdb.o OBJ += handler_coke.o handler_snack.o handler_door.o BIN := ../../dispsrv +DEPFILES := $(OBJ:%.o=%.d) + LINKFLAGS := -g ../../cokebank.so CPPFLAGS := -CFLAGS := -Wall -g +CFLAGS := -Wall -Wextra -Werror -g .PHONY: all clean @@ -20,3 +22,9 @@ clean: $(BIN): $(OBJ) $(CC) -o $(BIN) $(LINKFLAGS) $(OBJ) +%.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + @cpp $< -MM -MF $*.d + +-include $(DEPFILES) + diff --git a/src/server/common.h b/src/server/common.h index 92d1dfc..ec9e38a 100644 --- a/src/server/common.h +++ b/src/server/common.h @@ -20,6 +20,8 @@ #define _EXPSTR(x) #x #define EXPSTR(x) _EXPSTR(x) +#define UNUSED(var) unused__##var __attribute__((__unused__)) + // === STRUCTURES === typedef struct sItem tItem; typedef struct sUser tUser; diff --git a/src/server/dispense.c b/src/server/dispense.c index 3c18b58..137aab7 100644 --- a/src/server/dispense.c +++ b/src/server/dispense.c @@ -27,12 +27,12 @@ int DispenseItem(int ActualUser, int User, tItem *Item) // Subtract the balance reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name); if( !reason ) reason = Item->Name; // TODO: Should I instead return an error? - ret = Transfer( User, GetUserID(COKEBANK_SALES_ACCT), Item->Price, reason); + ret = Bank_Transfer( User, Bank_GetUserID(COKEBANK_SALES_ACCT), Item->Price, reason); free(reason); if(ret) return 2; // 2: No balance // Get username for debugging - username = GetUserName(User); + username = Bank_GetUserName(User); // Actually do the dispense if( handler->DoDispense ) { @@ -40,18 +40,18 @@ int DispenseItem(int ActualUser, int User, tItem *Item) if(ret) { Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)", username, Item->Name, Item->Price); - Transfer( GetUserID(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" ); + Bank_Transfer( Bank_GetUserID(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" ); free( username ); return -1; // 1: Unkown Error again } } - actualUsername = GetUserName(ActualUser); + actualUsername = Bank_GetUserName(ActualUser); // And log that it happened Log_Info("dispense '%s' (%s:%i) for %s by %s [cost %i, balance %i cents]", Item->Name, handler->Name, Item->ID, - username, actualUsername, Item->Price, GetBalance(User) + username, actualUsername, Item->Price, Bank_GetBalance(User) ); free( username ); @@ -70,16 +70,17 @@ int DispenseGive(int ActualUser, int SrcUser, int DestUser, int Ammount, const c if( Ammount < 0 ) return 1; // Um... negative give? Not on my watch! - ret = Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); + ret = Bank_Transfer( SrcUser, DestUser, Ammount, ReasonGiven ); if(ret) return 2; // No Balance - srcName = GetUserName(SrcUser); - dstName = GetUserName(DestUser); - actualUsername = GetUserName(ActualUser); + srcName = Bank_GetUserName(SrcUser); + dstName = Bank_GetUserName(DestUser); + actualUsername = Bank_GetUserName(ActualUser); - Log_Info("give %i to %s from %s by %s (%s)", - Ammount, dstName, srcName, actualUsername, ReasonGiven + Log_Info("give %i to %s from %s by %s (%s) [balances %i, %i]", + Ammount, dstName, srcName, actualUsername, ReasonGiven, + Bank_GetBalance(SrcUser), Bank_GetBalance(DestUser) ); free(srcName); @@ -97,14 +98,14 @@ int DispenseAdd(int User, int ByUser, int Ammount, const char *ReasonGiven) int ret; char *dstName, *byName; - ret = Transfer( GetUserID(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven ); + ret = Bank_Transfer( Bank_GetUserID(COKEBANK_DEBT_ACCT), User, Ammount, ReasonGiven ); if(ret) return 2; - byName = GetUserName(ByUser); - dstName = GetUserName(User); + byName = Bank_GetUserName(ByUser); + dstName = Bank_GetUserName(User); - Log_Info("add %i to %s by %s (%s)", - Ammount, dstName, byName, ReasonGiven + Log_Info("add %i to %s by %s (%s) [balance %i]", + Ammount, dstName, byName, ReasonGiven, Bank_GetBalance(User) ); free(byName); diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index 030740c..5729f06 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -52,7 +52,7 @@ int Coke_InitHandler() return 0; } -int Coke_CanDispense(int User, int Item) +int Coke_CanDispense(int UNUSED(User), int Item) { char tmp[40], *status; regmatch_t matches[4]; @@ -118,7 +118,7 @@ int Coke_CanDispense(int User, int Item) /** * \brief Actually do a dispense from the coke machine */ -int Coke_DoDispense(int User, int Item) +int Coke_DoDispense(int UNUSED(User), int Item) { char tmp[32]; int i, ret; diff --git a/src/server/handler_door.c b/src/server/handler_door.c index 1ffcc85..a819d77 100644 --- a/src/server/handler_door.c +++ b/src/server/handler_door.c @@ -49,6 +49,9 @@ int Door_CanDispense(int User, int Item) // Sanity please if( Item == 0 ) return -1; + if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) ) + return 1; + return 0; } @@ -57,11 +60,12 @@ int Door_CanDispense(int User, int Item) */ int Door_DoDispense(int User, int Item) { - // Sanity please if( Item != 0 ) return -1; // Check if user is in door + if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) ) + return 1; // llogin or other diff --git a/src/server/handler_snack.c b/src/server/handler_snack.c index eab7e28..e8b29ec 100644 --- a/src/server/handler_snack.c +++ b/src/server/handler_snack.c @@ -46,7 +46,7 @@ int Snack_InitHandler() return 0; } -int Snack_CanDispense(int User, int Item) +int Snack_CanDispense(int UNUSED(User), int Item) { // Sanity please if( Item < 0 || Item > 99 ) return -1; @@ -59,7 +59,7 @@ int Snack_CanDispense(int User, int Item) /** * \brief Actually do a dispense from the coke machine */ -int Snack_DoDispense(int User, int Item) +int Snack_DoDispense(int UNUSED(User), int Item) { char tmp[32]; regmatch_t matches[4]; diff --git a/src/server/server.c b/src/server/server.c index 86c63d6..ec30a03 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -88,7 +88,7 @@ const struct sClientCommand { {"USER_ADD", Server_Cmd_USERADD}, {"USER_FLAGS", Server_Cmd_USERFLAGS} }; -#define NUM_COMMANDS (sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0])) +#define NUM_COMMANDS ((int)(sizeof(gaServer_Commands)/sizeof(gaServer_Commands[0]))) // === GLOBALS === int giServer_Port = 1020; @@ -194,7 +194,9 @@ void Server_HandleClient(int Socket, int bTrusted) char *buf = inbuf; int remspace = INPUT_BUFFER_SIZE-1; int bytes = -1; - tClient clientInfo = {0}; + tClient clientInfo; + + memset(&clientInfo, 0, sizeof(clientInfo)); // Initialise Client info clientInfo.Socket = Socket; @@ -363,7 +365,7 @@ void Server_Cmd_PASS(tClient *Client, char *Args) if(space) *space = '\0'; // Remove characters after the ' ' // Pass on to cokebank - Client->UID = GetUserAuth(Client->Salt, Client->Username, Args); + Client->UID = Bank_GetUserAuth(Client->Salt, Client->Username, Args); if( Client->UID != -1 ) { Client->bIsAuthed = 1; @@ -393,7 +395,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args) } // Get UID - Client->UID = GetUserID( Args ); + Client->UID = Bank_GetUserID( Args ); if( Client->UID < 0 ) { if(giDebugLevel) printf("Client %i: Unknown user '%s'\n", Client->ID, Args); @@ -402,7 +404,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args) } // You can't be an internal account - if( GetFlags(Client->UID) & USER_FLAG_INTERNAL ) { + if( Bank_GetFlags(Client->UID) & USER_FLAG_INTERNAL ) { Client->UID = -1; sendf(Client->Socket, "401 Auth Failure\n"); return ; @@ -431,20 +433,20 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args) } // Check user permissions - if( !(GetFlags(Client->UID) & USER_FLAG_COKE) ) { + if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE) ) { sendf(Client->Socket, "403 Not in coke\n"); return ; } // Set id - Client->EffectiveUID = GetUserID(Args); + Client->EffectiveUID = Bank_GetUserID(Args); if( Client->EffectiveUID == -1 ) { sendf(Client->Socket, "404 User not found\n"); return ; } // You can't be an internal account - if( GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) { + if( Bank_GetFlags(Client->EffectiveUID) & USER_FLAG_INTERNAL ) { Client->EffectiveUID = -1; sendf(Client->Socket, "404 User not found\n"); return ; @@ -460,6 +462,11 @@ void Server_Cmd_ENUMITEMS(tClient *Client, char *Args) { int i; + if( Args != NULL || strlen(Args) ) { + sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n"); + return ; + } + sendf(Client->Socket, "201 Items %i\n", giNumItems); for( i = 0; i < giNumItems; i ++ ) { @@ -592,14 +599,14 @@ void Server_Cmd_GIVE(tClient *Client, char *Args) reason ++; // Get recipient - uid = GetUserID(recipient); + uid = Bank_GetUserID(recipient); if( uid == -1 ) { sendf(Client->Socket, "404 Invalid target user\n"); return ; } // You can't alter an internal account - if( GetFlags(uid) & USER_FLAG_INTERNAL ) { + if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) { sendf(Client->Socket, "404 Invalid target user\n"); return ; } @@ -662,20 +669,20 @@ void Server_Cmd_ADD(tClient *Client, char *Args) reason ++; // Check user permissions - if( !(GetFlags(Client->UID) & USER_FLAG_COKE) ) { + if( !(Bank_GetFlags(Client->UID) & USER_FLAG_COKE) ) { sendf(Client->Socket, "403 Not in coke\n"); return ; } // Get recipient - uid = GetUserID(user); + uid = Bank_GetUserID(user); if( uid == -1 ) { sendf(Client->Socket, "404 Invalid user\n"); return ; } // You can't alter an internal account - if( GetFlags(uid) & USER_FLAG_INTERNAL ) { + if( Bank_GetFlags(uid) & USER_FLAG_INTERNAL ) { sendf(Client->Socket, "404 Invalid user\n"); return ; } @@ -706,7 +713,7 @@ void Server_Cmd_ENUMUSERS(tClient *Client, char *Args) { int i, numRet = 0; int maxBal = INT_MAX, minBal = INT_MIN; - int numUsr = GetMaxID(); + int numUsr = Bank_GetMaxID(); // Parse arguments if( Args && strlen(Args) ) @@ -730,7 +737,7 @@ void Server_Cmd_ENUMUSERS(tClient *Client, char *Args) // Get return number for( i = 0; i < numUsr; i ++ ) { - int bal = GetBalance(i); + int bal = Bank_GetBalance(i); if( bal == INT_MIN ) continue; @@ -745,7 +752,7 @@ void Server_Cmd_ENUMUSERS(tClient *Client, char *Args) for( i = 0; i < numUsr; i ++ ) { - int bal = GetBalance(i); + int bal = Bank_GetBalance(i); if( bal == INT_MIN ) continue; @@ -768,7 +775,7 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args) if(space) *space = '\0'; // Get recipient - uid = GetUserID(user); + uid = Bank_GetUserID(user); if( uid == -1 ) { sendf(Client->Socket, "404 Invalid user"); return ; @@ -780,7 +787,7 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args) void _SendUserInfo(tClient *Client, int UserID) { char *type, *disabled="", *door=""; - int flags = GetFlags(UserID); + int flags = Bank_GetFlags(UserID); if( flags & USER_FLAG_INTERNAL ) { type = "internal"; @@ -806,7 +813,7 @@ void _SendUserInfo(tClient *Client, int UserID) // TODO: User flags/type sendf( Client->Socket, "202 User %s %i %s%s\n", - GetUserName(UserID), GetBalance(UserID), + Bank_GetUserName(UserID), Bank_GetBalance(UserID), type, disabled ); } @@ -816,7 +823,7 @@ void Server_Cmd_USERADD(tClient *Client, char *Args) char *username, *space; // Check permissions - if( !(GetFlags(Client->UID) & USER_FLAG_WHEEL) ) { + if( !(Bank_GetFlags(Client->UID) & USER_FLAG_WHEEL) ) { sendf(Client->Socket, "403 Not Wheel\n"); return ; } @@ -828,7 +835,7 @@ void Server_Cmd_USERADD(tClient *Client, char *Args) if(space) *space = '\0'; // Try to create user - if( CreateUser(username) == -1 ) { + if( Bank_CreateUser(username) == -1 ) { sendf(Client->Socket, "404 User exists\n"); return ; } @@ -844,7 +851,7 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args) int uid; // Check permissions - if( !(GetFlags(Client->UID) & USER_FLAG_WHEEL) ) { + if( !(Bank_GetFlags(Client->UID) & USER_FLAG_WHEEL) ) { sendf(Client->Socket, "403 Not Wheel\n"); return ; } @@ -866,7 +873,7 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args) if(space) *space = '\0'; // Get UID - uid = GetUserID(username); + uid = Bank_GetUserID(username); if( uid == -1 ) { sendf(Client->Socket, "404 User '%s' not found\n", username); return ; @@ -924,7 +931,7 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args) } while(space); // Apply flags - SetFlags(uid, mask, value); + Bank_SetFlags(uid, mask, value); // Return OK sendf(Client->Socket, "200 User Updated\n"); -- 2.20.1