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, "Transfer %ic #%i{%i} > #%i{%i} [%i, %i] (%s)\n",
138 Ammount, SourceUser, srcBal, DestUser, dstBal,
139 srcBal - Ammount, dstBal + Ammount, Reason);
143 int GetFlags(int User)
145 return Bank_GetUserFlags(User);
148 int SetFlags(int User, int Mask, int Flags)
150 return Bank_SetUserFlags(User, Mask, Flags);
154 * \brief Get the balance of the passed user
156 int GetBalance(int User)
158 return Bank_GetUserBalance(User);
162 * \brief Return the name the passed user
164 char *GetUserName(int User)
166 return Bank_GetUserName(User);
170 * \brief Get the User ID of the named user
172 int GetUserID(const char *Username)
174 return Bank_GetUserByName(Username);
177 int CreateUser(const char *Username)
181 ret = Bank_GetUserByName(Username);
182 if( ret != -1 ) return -1;
184 return Bank_AddUser(Username);
189 return giBank_NumUsers;
193 * \brief Authenticate a user
194 * \return User ID, or -1 if authentication failed
196 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
201 int ofs = strlen(Username) + strlen(Salt);
202 char input[ ofs + 40 + 1];
203 char tmp[4 + strlen(Username) + 1]; // uid=%s
208 if( strcmp(Username, "tpg") == 0 )
209 return GetUserID("tpg");
212 if( strcmp(Username, "root") == 0 ) {
213 int ret = GetUserID("root");
215 return CreateUser("root");
221 HexBin(hash, 20, PasswordString);
223 // Build string to hash
224 strcpy(input, Username);
227 // TODO: Get user's SHA-1 hash
228 sprintf(tmp, "uid=%s", Username);
229 printf("tmp = '%s'\n", tmp);
230 passhash = ReadLDAPValue(tmp, "userPassword");
234 printf("LDAP hash '%s'\n", passhash);
236 sprintf(input+ofs, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
237 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
238 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
240 // Then create the hash from the provided salt
241 // Compare that with the provided hash
246 printf("Password hash ");
248 printf("%02x", hash[i]&0xFF);
259 char *ReadLDAPValue(const char *Filter, char *Value)
261 LDAPMessage *res, *res2;
262 struct berval **attrValues;
263 char *attrNames[] = {Value,NULL};
265 struct timeval timeout;
271 rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
272 attrNames, 0, NULL, NULL, &timeout, 1, &res
274 printf("ReadLDAPValue: rv = %i\n", rv);
276 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
283 res2 = ldap_first_entry(gpLDAP, res);
284 attrValues = ldap_get_values_len(gpLDAP, res2, Value);
286 ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
288 ldap_value_free_len(attrValues);
295 // TODO: Move to another file
296 void HexBin(uint8_t *Dest, int BufSize, const char *Src)
299 for( i = 0; i < BufSize; i ++ )
303 if('0' <= *Src && *Src <= '9')
304 val |= (*Src-'0') << 4;
305 else if('A' <= *Src && *Src <= 'F')
306 val |= (*Src-'A'+10) << 4;
307 else if('a' <= *Src && *Src <= 'f')
308 val |= (*Src-'a'+10) << 4;
313 if('0' <= *Src && *Src <= '9')
315 else if('A' <= *Src && *Src <= 'F')
316 val |= (*Src-'A'+10);
317 else if('a' <= *Src && *Src <= 'f')
318 val |= (*Src-'a'+10);
325 for( ; i < BufSize; i++ )