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, char *Src);
40 char *gsLDAPServer = "mussel";
47 * \brief Load the cokebank database
49 void Init_Cokebank(const char *Argument)
56 gBank_File = fopen(Argument, "rb+");
58 gBank_File = fopen(Argument, "wb+");
61 perror("Opening coke bank");
65 // TODO: Do I need this?
66 gBank_LogFile = fopen("cokebank.log", "a");
67 if( !gBank_LogFile ) gBank_LogFile = stdout;
70 fseek(gBank_File, 0, SEEK_END);
71 giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]);
72 fseek(gBank_File, 0, SEEK_SET);
73 gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) );
74 fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File);
78 rv = ldap_create(&gpLDAP);
80 fprintf(stderr, "ldap_create: %s\n", ldap_err2string(rv));
83 rv = ldap_initialize(&gpLDAP, "ldap://mussel:389");
85 fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rv));
88 { int ver = LDAP_VERSION3; ldap_set_option(gpLDAP, LDAP_OPT_PROTOCOL_VERSION, &ver); }
90 rv = ldap_start_tls_s(gpLDAP, NULL, NULL);
92 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
98 struct berval *servcred;
99 cred.bv_val = "secret";
101 rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
102 "", &cred, NULL, NULL, NULL);
104 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
112 * \brief Transfers money from one user to another
113 * \param SourceUser Source user
114 * \param DestUser Destination user
115 * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser
116 * \param Reason Reason for the transfer (essentially a comment)
117 * \return Boolean failure
119 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
121 int srcBal = Bank_GetUserBalance(SourceUser);
122 int dstBal = Bank_GetUserBalance(DestUser);
124 if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
126 if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
128 Bank_AlterUserBalance(DestUser, Ammount);
129 Bank_AlterUserBalance(SourceUser, -Ammount);
130 fprintf(gBank_LogFile, "ACCT #%i{%i} -= %ic [to #%i] (%s)\n", SourceUser, srcBal, Ammount, DestUser, Reason);
131 fprintf(gBank_LogFile, "ACCT #%i{%i} += %ic [from #%i] (%s)\n", DestUser, dstBal, Ammount, SourceUser, Reason);
135 int GetFlags(int User)
137 return Bank_GetUserFlags(User);
140 int SetFlags(int User, int Mask, int Flags)
142 return Bank_SetUserFlags(User, Mask, Flags);
146 * \brief Get the balance of the passed user
148 int GetBalance(int User)
150 return Bank_GetUserBalance(User);;
154 * \brief Return the name the passed user
156 char *GetUserName(int User)
158 return Bank_GetUserName(User);
162 * \brief Get the User ID of the named user
164 int GetUserID(const char *Username)
166 return Bank_GetUserByName(Username);
169 int CreateUser(const char *Username)
173 ret = Bank_GetUserByName(Username);
174 if( ret != -1 ) return -1;
176 return Bank_AddUser(Username);
181 return giBank_NumUsers;
185 * \brief Authenticate a user
186 * \return User ID, or -1 if authentication failed
188 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
193 int ofs = strlen(Username) + strlen(Salt);
194 char input[ ofs + 40 + 1];
195 char tmp[4 + strlen(Username) + 1]; // uid=%s
200 if( strcmp(Username, "tpg") == 0 )
201 return GetUserID("tpg");
204 if( strcmp(Username, "root") == 0 ) {
205 int ret = GetUserID("root");
207 return CreateUser("root");
213 HexBin(hash, 20, PasswordString);
215 // Build string to hash
216 strcpy(input, Username);
219 // TODO: Get user's SHA-1 hash
220 sprintf(tmp, "uid=%s", Username);
221 printf("tmp = '%s'\n", tmp);
222 passhash = ReadLDAPValue(tmp, "userPassword");
226 printf("LDAP hash '%s'\n", passhash);
228 sprintf(input+ofs, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
229 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
230 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
232 // Then create the hash from the provided salt
233 // Compare that with the provided hash
237 printf("Client %i: Password hash ", Client->ID);
238 for(i=0;i<HASH_LENGTH;i++)
239 printf("%02x", hash[i]&0xFF);
249 char *ReadLDAPValue(const char *Filter, char *Value)
251 LDAPMessage *res, *res2;
252 struct berval **attrValues;
253 char *attrNames[] = {Value,NULL};
255 struct timeval timeout;
261 rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
262 attrNames, 0, NULL, NULL, &timeout, 1, &res
264 printf("ReadLDAPValue: rv = %i\n", rv);
266 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
273 res2 = ldap_first_entry(gpLDAP, res);
274 attrValues = ldap_get_values_len(gpLDAP, res2, Value);
276 ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
278 ldap_value_free_len(attrValues);
285 // TODO: Move to another file
286 void HexBin(uint8_t *Dest, int BufSize, char *Src)
289 for( i = 0; i < BufSize; i ++ )
293 if('0' <= *Src && *Src <= '9')
294 val |= (*Src-'0') << 4;
295 else if('A' <= *Src && *Src <= 'F')
296 val |= (*Src-'A'+10) << 4;
297 else if('a' <= *Src && *Src <= 'f')
298 val |= (*Src-'a'+10) << 4;
303 if('0' <= *Src && *Src <= '9')
305 else if('A' <= *Src && *Src <= 'F')
306 val |= (*Src-'A'+10);
307 else if('a' <= *Src && *Src <= 'f')
308 val |= (*Src-'a'+10);
315 for( ; i < BufSize; i++ )