3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * cokebank.c - Coke-Bank management
7 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
14 #include <openssl/sha.h>
21 #define HACK_TPG_NOAUTH 1
22 #define HACK_ROOT_NOAUTH 1
25 void Init_Cokebank(const char *Argument);
26 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
27 int GetBalance(int User);
28 char *GetUserName(int User);
29 int GetUserID(const char *Username);
31 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
33 char *ReadLDAPValue(const char *Filter, char *Value);
35 void HexBin(uint8_t *Dest, int BufSize, const char *Src);
40 char *gsLDAPPath = "ldapi:///";
46 * \brief Load the cokebank database
48 void Init_Cokebank(const char *Argument)
55 gBank_File = fopen(Argument, "rb+");
57 gBank_File = fopen(Argument, "wb+");
60 perror("Opening coke bank");
64 // TODO: Do I need this?
65 gBank_LogFile = fopen("cokebank.log", "a");
66 if( !gBank_LogFile ) gBank_LogFile = stdout;
69 fseek(gBank_File, 0, SEEK_END);
70 giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
71 fseek(gBank_File, 0, SEEK_SET);
72 gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
73 fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
77 rv = ldap_create(&gpLDAP);
79 fprintf(stderr, "ldap_create: %s\n", ldap_err2string(rv));
82 rv = ldap_initialize(&gpLDAP, gsLDAPPath);
84 fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rv));
87 { int ver = LDAP_VERSION3; ldap_set_option(gpLDAP, LDAP_OPT_PROTOCOL_VERSION, &ver); }
89 rv = ldap_start_tls_s(gpLDAP, NULL, NULL);
91 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
97 struct berval *servcred;
98 cred.bv_val = "secret";
100 rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
101 "", &cred, NULL, NULL, &servcred);
103 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
111 * \brief Transfers money from one user to another
112 * \param SourceUser Source user
113 * \param DestUser Destination user
114 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
115 * \param Reason Reason for the transfer (essentially a comment)
116 * \return Boolean failure
118 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
120 int srcBal = Bank_GetUserBalance(SourceUser);
121 int dstBal = Bank_GetUserBalance(DestUser);
123 if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
125 if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
127 Bank_AlterUserBalance(DestUser, Ammount);
128 Bank_AlterUserBalance(SourceUser, -Ammount);
129 fprintf(gBank_LogFile, "ACCT #%i{%i} -= %ic [to #%i] (%s)\n", SourceUser, srcBal, Ammount, DestUser, Reason);
130 fprintf(gBank_LogFile, "ACCT #%i{%i} += %ic [from #%i] (%s)\n", DestUser, dstBal, Ammount, SourceUser, Reason);
134 int GetFlags(int User)
136 return Bank_GetUserFlags(User);
139 int SetFlags(int User, int Mask, int Flags)
141 return Bank_SetUserFlags(User, Mask, Flags);
145 * \brief Get the balance of the passed user
147 int GetBalance(int User)
149 return Bank_GetUserBalance(User);;
153 * \brief Return the name the passed user
155 char *GetUserName(int User)
157 return Bank_GetUserName(User);
161 * \brief Get the User ID of the named user
163 int GetUserID(const char *Username)
165 return Bank_GetUserByName(Username);
168 int CreateUser(const char *Username)
172 ret = Bank_GetUserByName(Username);
173 if( ret != -1 ) return -1;
175 return Bank_AddUser(Username);
180 return giBank_NumUsers;
184 * \brief Authenticate a user
185 * \return User ID, or -1 if authentication failed
187 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
192 int ofs = strlen(Username) + strlen(Salt);
193 char input[ ofs + 40 + 1];
194 char tmp[4 + strlen(Username) + 1]; // uid=%s
199 if( strcmp(Username, "tpg") == 0 )
200 return GetUserID("tpg");
203 if( strcmp(Username, "root") == 0 ) {
204 int ret = GetUserID("root");
206 return CreateUser("root");
212 HexBin(hash, 20, PasswordString);
214 // Build string to hash
215 strcpy(input, Username);
218 // TODO: Get user's SHA-1 hash
219 sprintf(tmp, "uid=%s", Username);
220 printf("tmp = '%s'\n", tmp);
221 passhash = ReadLDAPValue(tmp, "userPassword");
225 printf("LDAP hash '%s'\n", passhash);
227 sprintf(input+ofs, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
228 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
229 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
231 // Then create the hash from the provided salt
232 // Compare that with the provided hash
237 printf("Password hash ");
239 printf("%02x", hash[i]&0xFF);
250 char *ReadLDAPValue(const char *Filter, char *Value)
252 LDAPMessage *res, *res2;
253 struct berval **attrValues;
254 char *attrNames[] = {Value,NULL};
256 struct timeval timeout;
262 rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
263 attrNames, 0, NULL, NULL, &timeout, 1, &res
265 printf("ReadLDAPValue: rv = %i\n", rv);
267 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
274 res2 = ldap_first_entry(gpLDAP, res);
275 attrValues = ldap_get_values_len(gpLDAP, res2, Value);
277 ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
279 ldap_value_free_len(attrValues);
286 // TODO: Move to another file
287 void HexBin(uint8_t *Dest, int BufSize, const char *Src)
290 for( i = 0; i < BufSize; i ++ )
294 if('0' <= *Src && *Src <= '9')
295 val |= (*Src-'0') << 4;
296 else if('A' <= *Src && *Src <= 'F')
297 val |= (*Src-'A'+10) << 4;
298 else if('a' <= *Src && *Src <= 'f')
299 val |= (*Src-'a'+10) << 4;
304 if('0' <= *Src && *Src <= '9')
306 else if('A' <= *Src && *Src <= 'F')
307 val |= (*Src-'A'+10);
308 else if('a' <= *Src && *Src <= 'f')
309 val |= (*Src-'a'+10);
316 for( ; i < BufSize; i++ )