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>
18 #define HACK_TPG_NOAUTH 1
19 #define HACK_ROOT_NOAUTH 1
22 void Init_Cokebank(const char *Argument);
23 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
24 int GetBalance(int User);
25 char *GetUserName(int User);
26 int GetUserID(const char *Username);
28 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
30 char *ReadLDAPValue(const char *Filter, char *Value);
32 void HexBin(uint8_t *Dest, int BufSize, char *Src);
37 char *gsLDAPServer = "mussel";
44 * \brief Load the cokebank database
46 void Init_Cokebank(const char *Argument)
53 gBank_File = fopen(Argument, "rb+");
55 gBank_File = fopen(Argument, "wb+");
58 perror("Opening coke bank");
62 // TODO: Do I need this?
63 gBank_LogFile = fopen("cokebank.log", "a");
64 if( !gBank_LogFile ) gBank_LogFile = stdout;
67 fseek(gBank_File, 0, SEEK_END);
68 giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
69 fseek(gBank_File, 0, SEEK_SET);
70 gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
71 fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
75 rv = ldap_create(&gpLDAP);
77 fprintf(stderr, "ldap_create: %s\n", ldap_err2string(rv));
80 rv = ldap_initialize(&gpLDAP, "ldap://mussel:389");
82 fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rv));
85 { int ver = LDAP_VERSION3; ldap_set_option(gpLDAP, LDAP_OPT_PROTOCOL_VERSION, &ver); }
87 rv = ldap_start_tls_s(gpLDAP, NULL, NULL);
89 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
95 struct berval *servcred;
96 cred.bv_val = "secret";
98 rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
99 "", &cred, NULL, NULL, NULL);
101 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
109 * \brief Transfers money from one user to another
110 * \param SourceUser Source user
111 * \param DestUser Destination user
112 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
113 * \param Reason Reason for the transfer (essentially a comment)
114 * \return Boolean failure
116 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
118 int srcBal = Bank_GetUserBalance(SourceUser);
119 int dstBal = Bank_GetUserBalance(DestUser);
121 if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
123 if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
125 Bank_AlterUserBalance(DestUser, Ammount);
126 Bank_AlterUserBalance(SourceUser, -Ammount);
127 fprintf(gBank_LogFile, "ACCT #%i{%i} -= %ic [to #%i] (%s)\n", SourceUser, srcBal, Ammount, DestUser, Reason);
128 fprintf(gBank_LogFile, "ACCT #%i{%i} += %ic [from #%i] (%s)\n", DestUser, dstBal, Ammount, SourceUser, Reason);
132 int GetFlags(int User)
134 return Bank_GetUserFlags(User);
137 int SetFlags(int User, int Mask, int Flags)
139 return Bank_SetUserFlags(User, Mask, Flags);
143 * \brief Get the balance of the passed user
145 int GetBalance(int User)
147 return Bank_GetUserBalance(User);;
151 * \brief Return the name the passed user
153 char *GetUserName(int User)
155 return Bank_GetUserName(User);
159 * \brief Get the User ID of the named user
161 int GetUserID(const char *Username)
163 return Bank_GetUserByName(Username);
166 int CreateUser(const char *Username)
170 ret = Bank_GetUserByName(Username);
171 if( ret != -1 ) return -1;
173 return Bank_AddUser(Username);
178 return giBank_NumUsers;
182 * \brief Authenticate a user
183 * \return User ID, or -1 if authentication failed
185 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
190 int ofs = strlen(Username) + strlen(Salt);
191 char input[ ofs + 40 + 1];
192 char tmp[4 + strlen(Username) + 1]; // uid=%s
197 if( strcmp(Username, "tpg") == 0 )
198 return GetUserID("tpg");
201 if( strcmp(Username, "root") == 0 ) {
202 int ret = GetUserID("root");
204 return CreateUser("root");
210 HexBin(hash, 20, PasswordString);
212 // Build string to hash
213 strcpy(input, Username);
216 // TODO: Get user's SHA-1 hash
217 sprintf(tmp, "uid=%s", Username);
218 printf("tmp = '%s'\n", tmp);
219 passhash = ReadLDAPValue(tmp, "userPassword");
223 printf("LDAP hash '%s'\n", passhash);
225 sprintf(input+ofs, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
226 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
227 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
229 // Then create the hash from the provided salt
230 // Compare that with the provided hash
235 printf("Client %i: Password hash ", Client->ID);
236 for(i=0;i<HASH_LENGTH;i++)
237 printf("%02x", hash[i]&0xFF);
247 char *ReadLDAPValue(const char *Filter, char *Value)
249 LDAPMessage *res, *res2;
250 struct berval **attrValues;
251 char *attrNames[] = {Value,NULL};
253 struct timeval timeout;
259 rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
260 attrNames, 0, NULL, NULL, &timeout, 1, &res
262 printf("ReadLDAPValue: rv = %i\n", rv);
264 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
271 res2 = ldap_first_entry(gpLDAP, res);
272 attrValues = ldap_get_values_len(gpLDAP, res2, Value);
274 ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
276 ldap_value_free_len(attrValues);
283 // TODO: Move to another file
284 void HexBin(uint8_t *Dest, int BufSize, char *Src)
287 for( i = 0; i < BufSize; i ++ )
291 if('0' <= *Src && *Src <= '9')
292 val |= (*Src-'0') << 4;
293 else if('A' <= *Src && *Src <= 'F')
294 val |= (*Src-'A'+10) << 4;
295 else if('a' <= *Src && *Src <= 'f')
296 val |= (*Src-'a'+10) << 4;
301 if('0' <= *Src && *Src <= '9')
303 else if('A' <= *Src && *Src <= 'F')
304 val |= (*Src-'A'+10);
305 else if('a' <= *Src && *Src <= 'f')
306 val |= (*Src-'a'+10);
313 for( ; i < BufSize; i++ )