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>
23 * http://linuxdevcenter.com/pub/a/linux/2003/08/14/libldap.html
24 * - Using libldap, the LDAP Client Library
29 #define HACK_TPG_NOAUTH 1
30 #define HACK_ROOT_NOAUTH 1
33 void Init_Cokebank(const char *Argument);
34 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
35 int GetBalance(int User);
36 char *GetUserName(int User);
37 int GetUserID(const char *Username);
39 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
41 char *ReadLDAPValue(const char *Filter, char *Value);
43 void HexBin(uint8_t *Dest, int BufSize, const char *Src);
48 char *gsLDAPPath = "ldapi:///";
54 * \brief Load the cokebank database
56 void Init_Cokebank(const char *Argument)
63 gBank_File = fopen(Argument, "rb+");
65 gBank_File = fopen(Argument, "wb+");
68 perror("Opening coke bank");
72 // TODO: Do I need this?
73 gBank_LogFile = fopen("cokebank.log", "a");
74 if( !gBank_LogFile ) gBank_LogFile = stdout;
77 fseek(gBank_File, 0, SEEK_END);
78 giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
79 fseek(gBank_File, 0, SEEK_SET);
80 gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
81 fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
85 rv = ldap_create(&gpLDAP);
87 fprintf(stderr, "ldap_create: %s\n", ldap_err2string(rv));
90 rv = ldap_initialize(&gpLDAP, gsLDAPPath);
92 fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rv));
95 { int ver = LDAP_VERSION3; ldap_set_option(gpLDAP, LDAP_OPT_PROTOCOL_VERSION, &ver); }
97 rv = ldap_start_tls_s(gpLDAP, NULL, NULL);
99 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
105 struct berval *servcred;
106 cred.bv_val = "secret";
108 rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
109 "", &cred, NULL, NULL, &servcred);
111 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
119 * \brief Transfers money from one user to another
120 * \param SourceUser Source user
121 * \param DestUser Destination user
122 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
123 * \param Reason Reason for the transfer (essentially a comment)
124 * \return Boolean failure
126 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
128 int srcBal = Bank_GetUserBalance(SourceUser);
129 int dstBal = Bank_GetUserBalance(DestUser);
131 if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
133 if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
135 Bank_AlterUserBalance(DestUser, Ammount);
136 Bank_AlterUserBalance(SourceUser, -Ammount);
137 fprintf(gBank_LogFile, "ACCT #%i{%i} -= %ic [to #%i] (%s)\n", SourceUser, srcBal, Ammount, DestUser, Reason);
138 fprintf(gBank_LogFile, "ACCT #%i{%i} += %ic [from #%i] (%s)\n", DestUser, dstBal, Ammount, SourceUser, Reason);
142 int GetFlags(int User)
144 return Bank_GetUserFlags(User);
147 int SetFlags(int User, int Mask, int Flags)
149 return Bank_SetUserFlags(User, Mask, Flags);
153 * \brief Get the balance of the passed user
155 int GetBalance(int User)
157 return Bank_GetUserBalance(User);;
161 * \brief Return the name the passed user
163 char *GetUserName(int User)
165 return Bank_GetUserName(User);
169 * \brief Get the User ID of the named user
171 int GetUserID(const char *Username)
173 return Bank_GetUserByName(Username);
176 int CreateUser(const char *Username)
180 ret = Bank_GetUserByName(Username);
181 if( ret != -1 ) return -1;
183 return Bank_AddUser(Username);
188 return giBank_NumUsers;
192 * \brief Authenticate a user
193 * \return User ID, or -1 if authentication failed
195 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
200 int ofs = strlen(Username) + strlen(Salt);
201 char input[ ofs + 40 + 1];
202 char tmp[4 + strlen(Username) + 1]; // uid=%s
207 if( strcmp(Username, "tpg") == 0 )
208 return GetUserID("tpg");
211 if( strcmp(Username, "root") == 0 ) {
212 int ret = GetUserID("root");
214 return CreateUser("root");
220 HexBin(hash, 20, PasswordString);
222 // Build string to hash
223 strcpy(input, Username);
226 // TODO: Get user's SHA-1 hash
227 sprintf(tmp, "uid=%s", Username);
228 printf("tmp = '%s'\n", tmp);
229 passhash = ReadLDAPValue(tmp, "userPassword");
233 printf("LDAP hash '%s'\n", passhash);
235 sprintf(input+ofs, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
236 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
237 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
239 // Then create the hash from the provided salt
240 // Compare that with the provided hash
245 printf("Password hash ");
247 printf("%02x", hash[i]&0xFF);
258 char *ReadLDAPValue(const char *Filter, char *Value)
260 LDAPMessage *res, *res2;
261 struct berval **attrValues;
262 char *attrNames[] = {Value,NULL};
264 struct timeval timeout;
270 rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
271 attrNames, 0, NULL, NULL, &timeout, 1, &res
273 printf("ReadLDAPValue: rv = %i\n", rv);
275 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
282 res2 = ldap_first_entry(gpLDAP, res);
283 attrValues = ldap_get_values_len(gpLDAP, res2, Value);
285 ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
287 ldap_value_free_len(attrValues);
294 // TODO: Move to another file
295 void HexBin(uint8_t *Dest, int BufSize, const char *Src)
298 for( i = 0; i < BufSize; i ++ )
302 if('0' <= *Src && *Src <= '9')
303 val |= (*Src-'0') << 4;
304 else if('A' <= *Src && *Src <= 'F')
305 val |= (*Src-'A'+10) << 4;
306 else if('a' <= *Src && *Src <= 'f')
307 val |= (*Src-'a'+10) << 4;
312 if('0' <= *Src && *Src <= '9')
314 else if('A' <= *Src && *Src <= 'F')
315 val |= (*Src-'A'+10);
316 else if('a' <= *Src && *Src <= 'f')
317 val |= (*Src-'a'+10);
324 for( ; i < BufSize; i++ )