Removed references to LDAP when USE_LDAP is undefined
[tpg/opendispense2.git] / src / cokebank_basic / main.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * cokebank.c - Coke-Bank management
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file COPYING
8  * for full details.
9  */
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <openssl/sha.h>
15 #include "common.h"
16 #if USE_LDAP
17 # include <ldap.h>
18 #endif
19
20 // === HACKS ===
21 #define HACK_TPG_NOAUTH 1
22 #define HACK_ROOT_NOAUTH        1
23
24 // === PROTOTYPES ===
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);
30  int    GetMaxID(void);
31  int    GetUserAuth(const char *Salt, const char *Username, const char *PasswordString);
32 #if USE_LDAP
33 char    *ReadLDAPValue(const char *Filter, char *Value);
34 #endif
35 void    HexBin(uint8_t *Dest, int BufSize, char *Src);
36
37 // === GLOBALS ===
38 FILE    *gBank_LogFile;
39 #if USE_LDAP
40 char    *gsLDAPServer = "mussel";
41  int    giLDAPPort = 389;
42 LDAP    *gpLDAP;
43 #endif
44
45 // === CODE ===
46 /**
47  * \brief Load the cokebank database
48  */
49 void Init_Cokebank(const char *Argument)
50 {
51         #if USE_LDAP
52          int    rv;
53         #endif
54         
55         // Open Cokebank
56         gBank_File = fopen(Argument, "rb+");
57         if( !gBank_File ) {
58                 gBank_File = fopen(Argument, "wb+");
59         }
60         if( !gBank_File ) {
61                 perror("Opening coke bank");
62         }
63
64         // Open log file
65         // TODO: Do I need this?
66         gBank_LogFile = fopen("cokebank.log", "a");
67         if( !gBank_LogFile )    gBank_LogFile = stdout;
68
69         // Read in cokebank
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);
75         
76         #if USE_LDAP
77         // Connect to LDAP
78         rv = ldap_create(&gpLDAP);
79         if(rv) {
80                 fprintf(stderr, "ldap_create: %s\n", ldap_err2string(rv));
81                 exit(1);
82         }
83         rv = ldap_initialize(&gpLDAP, "ldap://mussel:389");
84         if(rv) {
85                 fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rv));
86                 exit(1);
87         }
88         { int ver = LDAP_VERSION3; ldap_set_option(gpLDAP, LDAP_OPT_PROTOCOL_VERSION, &ver); }
89         # if 0
90         rv = ldap_start_tls_s(gpLDAP, NULL, NULL);
91         if(rv) {
92                 fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
93                 exit(1);
94         }
95         # endif
96         {
97                 struct berval   cred;
98                 struct berval   *servcred;
99                 cred.bv_val = "secret";
100                 cred.bv_len = 6;
101                 rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au",
102                         "", &cred, NULL, NULL, NULL);
103                 if(rv) {
104                         fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv));
105                         exit(1);
106                 }
107         }
108         #endif
109 }
110
111 /**
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
118  */
119 int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason)
120 {
121          int    srcBal = Bank_GetUserBalance(SourceUser);
122          int    dstBal = Bank_GetUserBalance(DestUser);
123         
124         if( srcBal - Ammount < Bank_GetMinAllowedBalance(SourceUser) )
125                 return 1;
126         if( dstBal + Ammount < Bank_GetMinAllowedBalance(DestUser) )
127                 return 1;
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);
132         return 0;
133 }
134
135 int GetFlags(int User)
136 {
137         return Bank_GetUserFlags(User);
138 }
139
140 int SetFlags(int User, int Mask, int Flags)
141 {
142         return Bank_SetUserFlags(User, Mask, Flags);
143 }
144
145 /**
146  * \brief Get the balance of the passed user
147  */
148 int GetBalance(int User)
149 {
150         return Bank_GetUserBalance(User);;
151 }
152
153 /**
154  * \brief Return the name the passed user
155  */
156 char *GetUserName(int User)
157 {
158         return Bank_GetUserName(User);
159 }
160
161 /**
162  * \brief Get the User ID of the named user
163  */
164 int GetUserID(const char *Username)
165 {
166         return Bank_GetUserByName(Username);
167 }
168
169 int CreateUser(const char *Username)
170 {
171          int    ret;
172         
173         ret = Bank_GetUserByName(Username);
174         if( ret != -1 ) return -1;
175         
176         return Bank_AddUser(Username);
177 }
178
179 int GetMaxID(void)
180 {
181         return giBank_NumUsers;
182 }
183
184 /**
185  * \brief Authenticate a user
186  * \return User ID, or -1 if authentication failed
187  */
188 int GetUserAuth(const char *Salt, const char *Username, const char *PasswordString)
189 {
190         #if USE_LDAP
191         uint8_t hash[20];
192         uint8_t h[20];
193          int    ofs = strlen(Username) + strlen(Salt);
194         char    input[ ofs + 40 + 1];
195         char    tmp[4 + strlen(Username) + 1];  // uid=%s
196         char    *passhash;
197         #endif
198         
199         #if HACK_TPG_NOAUTH
200         if( strcmp(Username, "tpg") == 0 )
201                 return GetUserID("tpg");
202         #endif
203         #if HACK_ROOT_NOAUTH
204         if( strcmp(Username, "root") == 0 ) {
205                 int ret = GetUserID("root");
206                 if( ret == -1 )
207                         return CreateUser("root");
208                 return ret;
209         }
210         #endif
211         
212         #if USE_LDAP
213         HexBin(hash, 20, PasswordString);
214         
215         // Build string to hash
216         strcpy(input, Username);
217         strcpy(input, Salt);
218         
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");
223         if( !passhash ) {
224                 return -1;
225         }
226         printf("LDAP hash '%s'\n", passhash);
227         
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]
231                 );
232         // Then create the hash from the provided salt
233         // Compare that with the provided hash
234
235         if( giDebugLevel ) {
236                  int    i;
237                 printf("Client %i: Password hash ", Client->ID);
238                 for(i=0;i<HASH_LENGTH;i++)
239                         printf("%02x", hash[i]&0xFF);
240                 printf("\n");
241         }
242         
243         #endif
244         
245         return -1;
246 }
247
248 #if USE_LDAP
249 char *ReadLDAPValue(const char *Filter, char *Value)
250 {
251         LDAPMessage     *res, *res2;
252         struct berval **attrValues;
253         char    *attrNames[] = {Value,NULL};
254         char    *ret;
255         struct timeval  timeout;
256          int    rv;
257         
258         timeout.tv_sec = 5;
259         timeout.tv_usec = 0;
260         
261         rv = ldap_search_ext_s(gpLDAP, "", LDAP_SCOPE_BASE, Filter,
262                 attrNames, 0, NULL, NULL, &timeout, 1, &res
263                 );
264         printf("ReadLDAPValue: rv = %i\n", rv);
265         if(rv) {
266                 fprintf(stderr, "LDAP Error reading '%s' with filter '%s'\n%s\n",
267                         Value, Filter,
268                         ldap_err2string(rv)
269                         );
270                 return NULL;
271         }
272         
273         res2 = ldap_first_entry(gpLDAP, res);
274         attrValues = ldap_get_values_len(gpLDAP, res2, Value);
275         
276         ret = strndup(attrValues[0]->bv_val, attrValues[0]->bv_len);
277         
278         ldap_value_free_len(attrValues);
279         
280         
281         return ret;
282 }
283 #endif
284
285 // TODO: Move to another file
286 void HexBin(uint8_t *Dest, int BufSize, char *Src)
287 {
288          int    i;
289         for( i = 0; i < BufSize; i ++ )
290         {
291                 uint8_t val = 0;
292                 
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;
299                 else
300                         break;
301                 Src ++;
302                 
303                 if('0' <= *Src && *Src <= '9')
304                         val |= (*Src-'0');
305                 else if('A' <= *Src && *Src <= 'F')
306                         val |= (*Src-'A'+10);
307                 else if('a' <= *Src && *Src <= 'f')
308                         val |= (*Src-'a'+10);
309                 else
310                         break;
311                 Src ++;
312                 
313                 Dest[i] = val;
314         }
315         for( ; i < BufSize; i++ )
316                 Dest[i] = 0;
317 }
318

UCC git Repository :: git.ucc.asn.au