edited dilatometer
[matches/MCTX3420.git] / server / login.c
1 /**
2  * @file login.c
3  * @brief Implementation of Login related functionality
4  */
5
6
7 #include "common.h"
8 #include "options.h"
9 #include <ctype.h>
10 #include <unistd.h>
11
12 // LDAP stuff
13 #define LDAP_DEPRECATED 1 // Required to use ldap_simple_bind_s
14 #include <ldap.h>
15
16 // MySQL stuff
17 #undef _GNU_SOURCE // HACK to silence compiler warning on redefinition in my_global.h
18 #include <my_global.h>
19 #include <mysql.h>
20
21
22
23
24
25 /**
26  * Attempt to login by searching a MySQL database
27  * @param user - Username
28  * @param pass - Password
29  * @param db_host - Host running the DataBase
30  * @param db_user - User to search the database as
31  * @param db_pass - Password for the database user
32  * @param db_name - Name of the database to use
33  * @param db_table - Table to search in
34  * @returns Privelage level of the user or USER_UNAUTH for failure to authenticate
35  */
36 UserType Login_MySQL(const char * user, const char * pass, 
37         const char * db_host, const char * db_user, const char * db_pass, const char * db_name, const char * db_table)
38 {
39         MYSQL * con = mysql_init(NULL);
40         if (con == NULL)
41         {
42                 Log(LOGERR, "mysql_init failed - %s", mysql_error(con));
43                 return USER_UNAUTH;
44         }
45
46         if (mysql_real_connect(con, db_host, db_user, db_pass, NULL, 0, NULL, 0) == NULL)
47         {
48                 Log(LOGERR, "mysql_real_connect failed - %s", mysql_error(con));
49                 mysql_close(con);
50                 return USER_UNAUTH;
51         }
52
53         char buffer[BUFSIZ];
54
55         // Select the database
56         sprintf(buffer, "USE %s;", db_name);
57         if (mysql_query(con, buffer))
58         {
59                 Log(LOGERR, "mysql_query failed - %s", mysql_error(con));
60                 mysql_close(con);
61                 return USER_UNAUTH;
62         }
63
64         // Search for the user
65         sprintf(buffer, "SELECT password FROM %s WHERE user_name = \"%s\";", db_table, user);
66         if (mysql_query(con, buffer))
67         {
68                 Log(LOGERR, "mysql_query failed - %s", mysql_error(con));
69                 mysql_close(con);
70                 return USER_UNAUTH;
71         }
72         
73         // Process the result
74         MYSQL_RES * result = mysql_store_result(con);
75         if (result == NULL)
76         {
77                 Log(LOGERR, "mysql_store_result failed - %s", mysql_error(con));
78                 mysql_close(con);
79                 return USER_UNAUTH;
80         }
81
82         int num_fields = mysql_num_fields(result);
83         if (num_fields != 1)
84         {
85                 Log(LOGERR, "The database may be corrupt; %d fields found, expected %d", num_fields, 1);
86                 mysql_close(con);
87                 return USER_UNAUTH;
88         }
89
90         UserType user_type = USER_UNAUTH;
91         MYSQL_ROW row;
92
93         // Get first row
94         if ((row = mysql_fetch_row(result)))
95         {
96                 if (strcmp(crypt(pass, row[0]), row[0]) == 0)
97                 {
98                         user_type = USER_NORMAL;
99                 }
100
101                 // There should only be one row. Through a hissy fit if we see any more.
102                 if ((row = mysql_fetch_row(result)))
103                 {
104                         Log(LOGERR, "Too many rows found.");
105                         user_type = USER_UNAUTH;
106                 }
107         }
108         else
109         {
110                 Log(LOGERR, "No user matching %s", user);
111         }
112
113         //TODO: Handle administrator users somehow better than this
114         // UserCake stores the permission level in a seperate table to the username/password, which is annoying
115         if (user_type != USER_UNAUTH && strcmp(user, "admin") == 0)
116         {
117                 user_type = USER_ADMIN;
118         }
119         mysql_free_result(result);
120         mysql_close(con);
121         return user_type;
122 }
123
124 /**
125  * Attempt to login using a file formatted like /etc/shadow
126  * This is here... because all better options have been exhausted
127  * @param user - The username
128  * @param pass - The password
129  * @param shadow - The file to use
130  * @returns Privelage level of the user or USER_UNAUTH for failure to authenticate
131  */
132 UserType Login_Shadow(const char * user, const char * pass, const char * shadow)
133 {
134         if (strlen(user) + strlen(pass) >= BUFSIZ-1)
135         {
136                 Log(LOGERR, "User/Password too long!\n");
137                 return USER_UNAUTH;
138         }
139
140         FILE * f = fopen(shadow, "r");
141         if (f == NULL)
142         {
143                 Log(LOGERR,"Can't open %s - %s\n", shadow, strerror(errno));
144                 return USER_UNAUTH;
145         }
146
147         char buffer[BUFSIZ];
148         int passwd_index = -1;
149
150         while (fgets(buffer, BUFSIZ, f) != NULL) // NOTE: Restrict username+password strings to BUFSIZ... what could possibly go wrong?
151         {
152
153                 //Log(LOGDEBUG,"Scanning %d: %s", strlen(buffer), buffer);
154         
155                 for (int i = 0; i < BUFSIZ-1 && buffer[i] != '\0'; ++i)
156                 {
157                         if (buffer[i] == ':')
158                         {
159                                 buffer[i] = '\0';
160                                 passwd_index = i+1;
161                                 break;
162                         }
163                 }
164
165                 if (strcmp(user,buffer) == 0)
166                 {
167                         //Log(LOGDEBUG,"User matches! %s\n", buffer);
168                         break;
169                 } 
170                 passwd_index = -1;
171         }
172
173         if (passwd_index <= 0)
174         {
175                 //Log(LOGDEBUG,"No user found matching %s\n", user);
176                 return USER_UNAUTH;
177         }
178
179         int gid_index = -1;
180         for (int i = passwd_index; i < BUFSIZ-1 && buffer[i] != '\0'; ++i)
181         {
182                 if (buffer[i] == ':')
183                 {
184                         gid_index = i+1;
185                         buffer[i] = '\0';
186                 }
187                 if (buffer[i] == '\n')
188                         buffer[i] = '\0';
189         }
190         char * end = buffer+gid_index;
191         UserType user_type = USER_NORMAL;
192         if (gid_index > passwd_index && gid_index < BUFSIZ-1)
193         {
194                 int gid = strtol(buffer+gid_index, &end,10);
195                 Log(LOGDEBUG, "Usertype %d %s", gid, buffer+gid_index);
196                 if (*end == '\0' && gid == 0)
197                 {
198                         Log(LOGDEBUG, "Admin");
199                         user_type = USER_ADMIN;
200                 }
201         }
202         
203         // Determine the salt
204         char salt[BUFSIZ];
205         int s = 0; int count = 0;
206         for (int i = passwd_index; i < BUFSIZ-1; ++i)
207         {
208                 salt[s++] = buffer[i];
209                 if (salt[s] == '$' && ++count >= 3)
210                         break;
211         }
212
213 //      Log(LOGDEBUG,"Salted Entry: %s\n", buffer+passwd_index);
214 //      Log(LOGDEBUG,"Salted Attempt: %s\n", crypt(pass, salt));
215         
216         if (strcmp(crypt(pass, salt), buffer+passwd_index) == 0)
217         {
218                 return user_type;
219         }
220         return USER_UNAUTH;
221 }
222
223 /**
224  * Attempt to bind to a LDAP uri
225  * @param uri - The uri
226  * @param dn - The DN
227  * @param pass - The password
228  * @returns An error code according to libldap; LDAP_SUCCESS if everything worked
229  */
230 int Login_LDAP_Bind(const char * uri, const char * dn, const char * pass)
231 {
232         Log(LOGDEBUG, "Bind to %s with dn %s and pass %s", uri, dn, pass);
233
234         // Initialise LDAP; prepares to connect to the server
235         LDAP * ld = NULL;
236         int err = ldap_initialize(&ld, uri);
237         if (err != LDAP_SUCCESS || ld == NULL)
238         {
239                 Log(LOGERR,"ldap_initialize failed - %s (ld = %p)", ldap_err2string(err), ld);
240                 return err;
241         }
242
243         // Set the LDAP version...
244         int version = LDAP_VERSION3;
245         err = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version); // specify the version
246         if (err != LDAP_SUCCESS)
247         {
248                 Log(LOGERR,"ldap_set_option failed - %s", ldap_err2string(err));
249                 return err;
250         }
251
252         // Attempt to bind using the supplied credentials.
253         // NOTE: ldap_simple_bind_s is "deprecated" in <ldap.h>, but not listed as such in the man pages :S
254         err = ldap_simple_bind_s(ld, dn, pass);
255         if (err != LDAP_SUCCESS)
256         {
257                 Log(LOGERR, "ldap_simple_bind_s failed - %s", ldap_err2string(err));
258         }
259         else
260         {
261                 Log(LOGDEBUG, "Successfully bound to %s with dn %s", uri, dn);
262         }
263
264         int err2 = ldap_unbind_s(ld);
265         if (err2 != LDAP_SUCCESS)
266         {
267                 Log(LOGERR, "ldap_unbind_s failed - %s", ldap_err2string(err2));
268                 err = err2;
269         }
270         return err;
271 }
272
273 /**
274  * Logout
275  * @param context - The context. The key will be cleared.
276  * @param params - Parameter string, UNUSED
277  */
278 void Logout_Handler(FCGIContext * context, char * params)
279 {
280         FCGI_ReleaseControl(context);
281         FCGI_SendControlCookie(context, false); //Unset the cookie
282         FCGI_AcceptJSON(context, "Logged out");
283 }
284
285
286 /**
287  * Handle a Login Request
288  * @param context - The context
289  * @param params - Parameter string, should contain username and password.
290  *                                 NOTE: Care should be taken when using params, as it is
291  *                                 completely unescaped. Do not log or use it without
292  *                 suitable escaping.
293  */
294 void Login_Handler(FCGIContext * context, char * params)
295 {
296         char * user; // The username supplied through CGI
297         char * pass; // The password supplied through CGI
298
299         FCGIValue values[] = {
300                 {"user", &user, FCGI_REQUIRED(FCGI_STRING_T)},
301                 {"pass", &pass, FCGI_REQUIRED(FCGI_STRING_T)},
302         };
303
304         //enum to avoid the use of magic numbers
305         typedef enum {
306                 USER,
307                 PASS,
308                 LOGOUT
309         } LoginParams;
310
311         // Fill values appropriately
312         if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
313         {
314                 // Error occured; FCGI_RejectJSON already called
315                 return;
316         }
317
318         // Trim leading whitespace
319         int i = 0;
320         for (i = 0; isspace(user[0]) && user[0] != '\0'; ++i, ++user);
321
322         // Truncate string at first non alphanumeric character
323         for (i = 0; isalnum(user[i]) && user[i] != '\0'; ++i);
324         user[i] = '\0';
325
326         
327         UserType user_type = USER_UNAUTH;
328         
329         switch (g_options.auth_method)
330         {
331
332                 case AUTH_LDAP:
333                 {
334                         if (*pass == '\0')
335                         {
336                                 FCGI_RejectJSON(context, "No password supplied.");
337                                 return;
338                         }
339
340                         //TODO: Generate the DN in some sane way
341                         char dn[BUFSIZ];
342
343
344                 
345                         // On a simple LDAP server:
346                         //int len = sprintf(dn, "uid=%s,%s", user, g_options.auth_options);
347         
348                         // At UWA (hooray)
349                         char * user_group = "Students";
350                         if (user[0] == '0')
351                                 user_group = "Staff";
352                         int len = sprintf(dn, "cn=%s,ou=%s,%s", user, user_group, g_options.auth_options);
353                 
354
355                         if (len >= BUFSIZ)
356                         {
357                                 FCGI_RejectJSON(context, "DN too long! Recompile with increased BUFSIZ");
358                                 return;
359                         }
360                 
361                         if (Login_LDAP_Bind(g_options.auth_uri, dn, pass) == LDAP_SUCCESS)
362                         {
363                                 if (user[0] == '0')
364                                         user_type = USER_ADMIN;
365                                 else
366                                         user_type = USER_NORMAL;
367                         }
368                         break;
369                 }
370                 case AUTH_SHADOW:
371                 {
372                         user_type = Login_Shadow(user, pass, g_options.auth_uri);
373                         break;
374                 }
375                 case AUTH_MYSQL:
376                 {
377                         //WARNING: C string manipulation code approaching!
378                         // Non reentrent; uses strsep and modifies g_options.auth_options
379                         // If problems happen, try strdup first ...
380                         static char * db_opts[] = {"root", "", "users", "uc_users"};
381                         static bool db_init_opts = false;
382                         if (!db_init_opts)
383                         {
384                                 db_init_opts = true;
385                                 db_opts[0] = (char*)g_options.auth_options;
386                                 for (int i = 1; i < sizeof(db_opts)/sizeof(char*); ++i)
387                                 {
388                                         char * def = db_opts[i];
389                                         db_opts[i] = db_opts[i-1];
390
391                                         strsep(db_opts+i, ",");
392                                         if (db_opts[i] == NULL)
393                                         {
394                                                 db_opts[i] = def;
395                                                 break;
396                                         }
397                                 }                       
398                                 //Log(LOGDEBUG, "MySQL: user %s pass %s name %s table %s", db_opts[0], db_opts[1], db_opts[2], db_opts[3]);     
399                         }
400
401                         user_type = Login_MySQL(user, pass, g_options.auth_uri, db_opts[0],db_opts[1], db_opts[2], db_opts[3]);
402                         break;
403                 }
404                 default:
405                 {
406                         Log(LOGWARN, "No authentication!");
407                         user_type = USER_ADMIN;
408                         break;
409                 }
410         }
411                 
412         // error check  
413         
414         if (user_type == USER_UNAUTH)
415         {
416                 Log(LOGDEBUG, "Authentication failure for %s", user);
417                 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, "Authentication failure.");
418         }
419         else
420         {
421                 // Try and gain control over the system
422                 if (FCGI_LockControl(context, user, user_type))
423                 {
424                         FCGI_EscapeText(context->user_name); //Don't break javascript pls
425                         // Give the user a cookie
426                         FCGI_SendControlCookie(context, true); //Send the control key
427                         FCGI_AcceptJSON(context, "Logged in");
428                         Log(LOGDEBUG, "Successful authentication for %s", user);
429                 }
430                 else
431                 {
432                         Log(LOGDEBUG, "%s successfully authenticated but system was in use by %s", user, context->user_name);
433                         FCGI_RejectJSON(context, "Someone else is already logged in (and you are not an admin)");
434                 }
435         }
436 }

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