static int Bank_int_ReadDatabase(void) { int i; if( gaBank_Users ) return 1; // Get size fseek(gBank_File, 0, SEEK_END); giBank_NumUsers = ftell(gBank_File) / sizeof(tFileUser); fseek(gBank_File, 0, SEEK_SET); // Allocate structures gaBank_Users = malloc( giBank_NumUsers * sizeof(tUser) ); gaBank_UsersByName = malloc( giBank_NumUsers * sizeof(tUser*) ); gaBank_UsersByBalance = malloc( giBank_NumUsers * sizeof(tUser*) ); // Read data for( i = 0; i < giBank_NumUsers; i ++ ) { tFileUser fu; fread(&fu, sizeof(tFileUser), 1, gBank_File); gaBank_Users[i].Name = NULL; gaBank_Users[i].UnixID = fu.UnixID; gaBank_Users[i].Balance = fu.Balance; gaBank_Users[i].Flags = fu.Flags; gaBank_Users[i].Name = Bank_GetAcctName(i); gaBank_UsersByName[i] = &gaBank_Users[i]; // Add to name index gaBank_UsersByBalance[i] = &gaBank_Users[i]; // Add to balance index } // Sort indexes qsort(gaBank_UsersByName, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareNames); qsort(gaBank_UsersByBalance, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareBalance); return 0; }
void _SendUserInfo(tClient *Client, int UserID) { char *type, *disabled="", *door=""; int flags = Bank_GetFlags(UserID); if( flags & USER_FLAG_INTERNAL ) { type = "internal"; } else if( flags & USER_FLAG_COKE ) { if( flags & USER_FLAG_ADMIN ) type = "coke,admin"; else type = "coke"; } else if( flags & USER_FLAG_ADMIN ) { type = "admin"; } else { type = "user"; } if( flags & USER_FLAG_DISABLED ) disabled = ",disabled"; if( flags & USER_FLAG_DOORGROUP ) door = ",door"; // TODO: User flags/type sendf( Client->Socket, "202 User %s %i %s%s%s\n", Bank_GetAcctName(UserID), Bank_GetBalance(UserID), type, disabled, door ); }
/* * Create a new user in our database */ int Bank_int_AddUser(const char *Username) { void *tmp; int uid = Bank_int_GetUnixID(Username); // Can has moar space plz? // - Structures tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0])); if( !tmp ) return -1; gaBank_Users = tmp; // - Name index tmp = realloc(gaBank_UsersByName, (giBank_NumUsers+1)*sizeof(tUser*)); if( !tmp ) return -1; gaBank_UsersByName = tmp; // - Balance index tmp = realloc(gaBank_UsersByBalance, (giBank_NumUsers+1)*sizeof(tUser*)); if( !tmp ) return -1; gaBank_UsersByBalance = tmp; // Crete new user gaBank_Users[giBank_NumUsers].Name = NULL; gaBank_Users[giBank_NumUsers].UnixID = uid; gaBank_Users[giBank_NumUsers].Balance = 0; gaBank_Users[giBank_NumUsers].Flags = 0; gaBank_UsersByName[giBank_NumUsers] = &gaBank_Users[giBank_NumUsers]; gaBank_UsersByBalance[giBank_NumUsers] = &gaBank_Users[giBank_NumUsers]; // Set default flags 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_ADMIN|USER_FLAG_COKE; } // Increment count giBank_NumUsers ++; // Get name gaBank_Users[giBank_NumUsers-1].Name = Bank_GetAcctName(giBank_NumUsers-1); // Update indexes qsort(gaBank_UsersByName, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareNames); qsort(gaBank_UsersByBalance, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareBalance); // Save Bank_int_WriteEntry(giBank_NumUsers - 1); return 0; }
void Server_Cmd_USERADD(tClient *Client, char *Args) { char *username; // Parse arguments if( Server_int_ParseArgs(0, Args, &username, NULL) ) { sendf(Client->Socket, "407 USER_ADD takes 1 argument\n"); return ; } // Check authentication if( !Client->bIsAuthed ) { sendf(Client->Socket, "401 Not Authenticated\n"); return ; } // Check permissions if( !(Bank_GetFlags(Client->UID) & USER_FLAG_ADMIN) ) { sendf(Client->Socket, "403 Not a coke admin\n"); return ; } // Try to create user if( Bank_CreateAcct(username) == -1 ) { sendf(Client->Socket, "404 User exists\n"); return ; } { char *thisName = Bank_GetAcctName(Client->UID); Log_Info("Account '%s' created by '%s'", username, thisName); free(thisName); } sendf(Client->Socket, "200 User Added\n"); }