Modify image capture routine
[matches/MCTX3420.git] / server / login.c
1 /**
2  * @file login.c
3  * @brief Implementation of Login related functionality
4  */
5
6
7
8
9 #include "login.h"
10 #include "options.h"
11 #include <ctype.h>
12 #include <unistd.h>
13
14 #define LDAP_DEPRECATED 1 // Required to use ldap_simple_bind_s
15 #include <ldap.h>
16
17 /**
18  * Attempt to login using a file formatted like /etc/shadow
19  * This is here... because all better options have been exhausted
20  * @param user - The username
21  * @param pass - The password
22  * @returns Privelage level of the user or USER_UNAUTH for failure to authenticate
23  */
24 UserType Login_Shadow(const char * user, const char * pass, const char * shadow)
25 {
26         if (strlen(user) + strlen(pass) >= BUFSIZ-1)
27         {
28                 Log(LOGERR, "User/Password too long!\n");
29                 return USER_UNAUTH;
30         }
31
32         FILE * f = fopen(shadow, "r");
33         if (f == NULL)
34         {
35                 Log(LOGERR,"Can't open %s - %s\n", shadow, strerror(errno));
36                 return USER_UNAUTH;
37         }
38
39         char buffer[BUFSIZ];
40         int passwd_index = -1;
41
42         while (fgets(buffer, BUFSIZ, f) != NULL) // NOTE: Restrict username+password strings to BUFSIZ... what could possibly go wrong?
43         {
44
45                 //Log(LOGDEBUG,"Scanning %d: %s", strlen(buffer), buffer);
46         
47                 for (int i = 0; i < BUFSIZ-1 && buffer[i] != '\0'; ++i)
48                 {
49                         if (buffer[i] == ':')
50                         {
51                                 buffer[i] = '\0';
52                                 passwd_index = i+1;
53                                 break;
54                         }
55                 }
56
57                 if (strcmp(user,buffer) == 0)
58                 {
59                         //Log(LOGDEBUG,"User matches! %s\n", buffer);
60                         break;
61                 } 
62                 passwd_index = -1;
63         }
64
65         if (passwd_index <= 0)
66         {
67                 //Log(LOGDEBUG,"No user found matching %s\n", user);
68                 return USER_UNAUTH;
69         }
70
71         int gid_index = -1;
72         for (int i = passwd_index; i < BUFSIZ-1 && buffer[i] != '\0'; ++i)
73         {
74                 if (buffer[i] == ':')
75                 {
76                         gid_index = i+1;
77                         buffer[i] = '\0';
78                 }
79                 if (buffer[i] == '\n')
80                         buffer[i] = '\0';
81         }
82         char * end = buffer+gid_index;
83         UserType user_type = USER_NORMAL;
84         if (gid_index > passwd_index && gid_index < BUFSIZ-1)
85         {
86                 int gid = strtol(buffer+gid_index, &end,10);
87                 Log(LOGDEBUG, "Usertype %d %s", gid, buffer+gid_index);
88                 if (*end == '\0' && gid == 0)
89                 {
90                         Log(LOGDEBUG, "Admin");
91                         user_type = USER_ADMIN;
92                 }
93         }
94         
95         // Determine the salt
96         char salt[BUFSIZ];
97         int s = 0; int count = 0;
98         for (int i = passwd_index; i < BUFSIZ-1; ++i)
99         {
100                 salt[s++] = buffer[i];
101                 if (salt[s] == '$' && ++count >= 3)
102                         break;
103         }
104
105 //      Log(LOGDEBUG,"Salted Entry: %s\n", buffer+passwd_index);
106 //      Log(LOGDEBUG,"Salted Attempt: %s\n", crypt(pass, salt));
107         
108         if (strcmp(crypt(pass, salt), buffer+passwd_index) == 0)
109         {
110                 return user_type;
111         }
112         return USER_UNAUTH;
113 }
114
115 /**
116  * Attempt to bind to a LDAP uri
117  * @param uri - The uri
118  * @param dn - The DN
119  * @param pass - The password
120  * @returns An error code according to libldap; LDAP_SUCCESS if everything worked
121  */
122 int Login_LDAP_Bind(const char * uri, const char * dn, const char * pass)
123 {
124         Log(LOGDEBUG, "Bind to %s with dn %s and pass %s", uri, dn, pass);
125
126         // Initialise LDAP; prepares to connect to the server
127         LDAP * ld = NULL;
128         int err = ldap_initialize(&ld, uri);
129         if (err != LDAP_SUCCESS || ld == NULL)
130         {
131                 Log(LOGERR,"ldap_initialize failed - %s (ld = %p)", ldap_err2string(err), ld);
132                 return err;
133         }
134
135         // Set the LDAP version...
136         int version = LDAP_VERSION3;
137         err = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version); // specify the version
138         if (err != LDAP_SUCCESS)
139         {
140                 Log(LOGERR,"ldap_set_option failed - %s", ldap_err2string(err));
141                 return err;
142         }
143
144         // Attempt to bind using the supplied credentials.
145         // NOTE: ldap_simple_bind_s is "deprecated" in <ldap.h>, but not listed as such in the man pages :S
146         err = ldap_simple_bind_s(ld, dn, pass);
147         if (err != LDAP_SUCCESS)
148         {
149                 Log(LOGERR, "ldap_simple_bind_s failed - %s", ldap_err2string(err));
150         }
151         else
152         {
153                 Log(LOGDEBUG, "Successfully bound to %s with dn %s", uri, dn);
154         }
155
156         int err2 = ldap_unbind_s(ld);
157         if (err2 != LDAP_SUCCESS)
158         {
159                 Log(LOGERR, "ldap_unbind_s failed - %s", ldap_err2string(err2));
160                 err = err2;
161         }
162         return err;
163 }
164
165 /**
166  * Logout
167  * @param context - The context. The key will be cleared.
168  * @param params - Parameter string, UNUSED
169  */
170 void Logout_Handler(FCGIContext * context, char * params)
171 {
172         FCGI_ReleaseControl(context);
173         FCGI_AcceptJSON(context, "Logged out", "0");
174 }
175
176
177 /**
178  * Handle a Login Request
179  * @param context - The context
180  * @param params - Parameter string, should contain username and password
181  */
182 void Login_Handler(FCGIContext * context, char * params)
183 {
184         char * user; // The username supplied through CGI
185         char * pass; // The password supplied through CGI
186
187         FCGIValue values[] = {
188                 {"user", &user, FCGI_REQUIRED(FCGI_STRING_T)},
189                 {"pass", &pass, FCGI_REQUIRED(FCGI_STRING_T)},
190         };
191
192         //enum to avoid the use of magic numbers
193         typedef enum {
194                 USER,
195                 PASS,
196                 LOGOUT
197         } LoginParams;
198
199         // Fill values appropriately
200         if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
201         {
202                 // Error occured; FCGI_RejectJSON already called
203                 return;
204         }
205
206         // Trim leading whitespace
207         int i = 0;
208         for (i = 0; isspace(user[0]) && user[0] != '\0'; ++i, ++user);
209
210         // Truncate string at first non alphanumeric character
211         for (i = 0; isalnum(user[i]) && user[i] != '\0'; ++i);
212         user[i] = '\0';
213
214         
215         UserType user_type = USER_UNAUTH;
216         
217         switch (g_options.auth_method)
218         {
219
220                 case AUTH_LDAP:
221                 {
222                         if (strlen(pass) <= 0)
223                         {
224                                 FCGI_RejectJSON(context, "No password supplied.");
225                                 return;
226                         }
227
228                         //TODO: Generate the DN in some sane way
229                         char dn[BUFSIZ];
230                 
231                         // On a simple LDAP server:
232                         //int len = sprintf(dn, "uid=%s,%s", user, g_options.ldap_base_dn);
233         
234                         // At UWA (hooray)
235                         char * user_group = "Students";
236                         if (user[0] == '0')
237                                 user_group = "Staff";
238                         int len = sprintf(dn, "cn=%s,ou=%s,%s", user, user_group, g_options.ldap_base_dn);
239                 
240
241                         if (len >= BUFSIZ)
242                         {
243                                 FCGI_RejectJSON(context, "DN too long! Recompile with increased BUFSIZ");
244                                 return;
245                         }
246                 
247                         if (Login_LDAP_Bind(g_options.auth_uri, dn, pass) == LDAP_SUCCESS)
248                         {
249                                 if (user[0] == '0')
250                                         user_type = USER_ADMIN;
251                                 else
252                                         user_type = USER_NORMAL;
253                         }
254                         break;
255                 }
256                 case AUTH_SHADOW:
257                 {
258                         user_type = Login_Shadow(user, pass, g_options.auth_uri);
259                         break;
260                 }
261                 default:
262                 {
263                         Log(LOGWARN, "No authentication!");
264                         user_type = USER_ADMIN;
265                         break;
266                 }
267         }
268                 
269         // error check  
270         
271         if (user_type == USER_UNAUTH)
272         {
273                 Log(LOGDEBUG, "Authentication failure for %s", user);
274                 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, "Authentication failure.");
275         }
276         else
277         {
278                 // Try and gain control over the system
279                 if (FCGI_LockControl(context, user, user_type))
280                 {
281                         FCGI_EscapeText(context->user_name); //Don't break javascript pls
282                         // Give the user a cookie
283                         FCGI_AcceptJSON(context, "Logged in", context->control_key);
284                         Log(LOGDEBUG, "Successful authentication for %s", user);
285                 }
286                 else
287                 {
288                         Log(LOGDEBUG, "%s successfully authenticated but system was in use by %s", user, context->user_name);
289                         FCGI_RejectJSON(context, "Someone else is already logged in (and you are not an admin)");
290                 }
291         }
292 }

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