3 * UCC (University [of WA] Computer Club) Electronic Accounting System
4 * - Cokebank (Basic Version)
6 * bank.c - Actual bank database
8 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
19 #define USE_UNIX_GROUPS 1
22 static int GetUnixID(const char *Username);
30 int Bank_GetUserByName(const char *Username)
34 uid = GetUnixID(Username);
36 // Expensive search :(
37 for( i = 0; i < giBank_NumUsers; i ++ )
39 if( gaBank_Users[i].UnixID == uid )
46 int Bank_GetUserBalance(int ID)
48 if( ID < 0 || ID >= giBank_NumUsers )
51 return gaBank_Users[ID].Balance;
54 int Bank_GetUserFlags(int ID)
56 if( ID < 0 || ID >= giBank_NumUsers )
60 if( gaBank_Users[ID].UnixID == 0 ) {
61 gaBank_Users[ID].Flags |= USER_FLAG_WHEEL|USER_FLAG_COKE;
65 // TODO: Implement checking the PAM groups and status instead, then
66 // fall back on the database. (and update if there is a difference)
67 if( gaBank_Users[ID].UnixID > 0 )
74 pwd = getpwuid( gaBank_Users[ID].UnixID );
76 // Check for additions to the "coke" group
77 grp = getgrnam("coke");
79 for( i = 0; grp->gr_mem[i]; i ++ )
81 if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
82 gaBank_Users[ID].Flags |= USER_FLAG_COKE;
88 // Check for additions to the "wheel" group
89 grp = getgrnam("wheel");
91 for( i = 0; grp->gr_mem[i]; i ++ )
93 if( strcmp(grp->gr_mem[i], pwd->pw_name) == 0 ) {
94 gaBank_Users[ID].Flags |= USER_FLAG_WHEEL;
102 return gaBank_Users[ID].Flags;
105 int Bank_SetUserFlags(int ID, int Mask, int Value)
108 if( ID < 0 || ID >= giBank_NumUsers )
111 // Silently ignore changes to root and meta accounts
112 if( gaBank_Users[ID].UnixID <= 0 ) return 0;
114 gaBank_Users[ID].Flags &= ~Mask;
115 gaBank_Users[ID].Flags |= Value;
120 int Bank_AlterUserBalance(int ID, int Delta)
123 if( ID < 0 || ID >= giBank_NumUsers )
127 gaBank_Users[ID].Balance += Delta;
130 fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
131 fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
136 int Bank_SetUserBalance(int ID, int Value)
139 if( ID < 0 || ID >= giBank_NumUsers )
143 gaBank_Users[ID].Balance = Value;
146 fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
147 fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
152 int Bank_GetMinAllowedBalance(int ID)
155 if( ID < 0 || ID >= giBank_NumUsers )
158 flags = Bank_GetUserFlags(ID);
160 // Internal accounts have no limit
161 if( (flags & USER_FLAG_INTERNAL) )
164 // Wheel is allowed to go to -$100
165 if( (flags & USER_FLAG_WHEEL) )
168 // Coke is allowed to go to -$20
169 if( (flags & USER_FLAG_COKE) )
172 // For everyone else, no negative
177 * \brief Create a new user in our database
179 int Bank_AddUser(const char *Username)
182 int uid = GetUnixID(Username);
184 // Can has moar space plz?
185 tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
186 if( !tmp ) return -1;
190 gaBank_Users[giBank_NumUsers].UnixID = uid;
191 gaBank_Users[giBank_NumUsers].Balance = 0;
192 gaBank_Users[giBank_NumUsers].Flags = 0;
194 if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
195 gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
197 else if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {
198 gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_INTERNAL;
200 else if( strcmp(Username, "root") == 0 ) {
201 gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE;
205 fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
206 fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
215 // Unix user dependent code
216 // TODO: Modify to keep its own list of usernames
218 char *Bank_GetUserName(int ID)
222 if( ID < 0 || ID >= giBank_NumUsers )
225 if( gaBank_Users[ID].UnixID == -1 )
226 return strdup(COKEBANK_SALES_ACCT);
228 if( gaBank_Users[ID].UnixID == -2 )
229 return strdup(COKEBANK_DEBT_ACCT);
231 pwd = getpwuid(gaBank_Users[ID].UnixID);
232 if( !pwd ) return NULL;
234 return strdup(pwd->pw_name);
237 static int GetUnixID(const char *Username)
241 if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) { // Pseudo account that sales are made into
244 else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) { // Pseudo acount that money is added from
250 pwd = getpwnam(Username);
251 if( !pwd ) return -1;