Work on Authentication System(s)
[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 for horrible hack purposes
20  * @param user - The username
21  * @param pass - The password
22  * @returns True if the login was successful, false otherwise
23  */
24 bool 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 false;
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 false;
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; ++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 false;
69         }
70
71         for (int i = passwd_index; i < BUFSIZ-1; ++i)
72         {
73                 if (buffer[i] == ':' || buffer[i] == '\n')
74                 {
75                         buffer[i] = '\0';
76                         
77                 }
78         }
79         
80         // Determine the salt
81         char salt[BUFSIZ];
82         int s = 0; int count = 0;
83         for (int i = passwd_index; i < BUFSIZ-1; ++i)
84         {
85                 salt[s++] = buffer[i];
86                 if (salt[s] == '$' && ++count >= 3)
87                         break;
88         }
89
90         Log(LOGDEBUG,"Salted Entry: %s\n", buffer+passwd_index);
91         Log(LOGDEBUG,"Salted Attempt: %s\n", crypt(pass, salt));
92         
93         return (strcmp(crypt(pass, salt), buffer+passwd_index) == 0);
94 }
95
96 /**
97  * Attempt to bind to a LDAP uri
98  * @param uri - The uri
99  * @param dn - The DN
100  * @param pass - The password
101  * @returns An error code according to libldap; LDAP_SUCCESS if everything worked
102  */
103 int Login_LDAP_Bind(const char * uri, const char * dn, const char * pass)
104 {
105         Log(LOGDEBUG, "Bind to %s with dn %s and pass %s", uri, dn, pass);
106
107         // Initialise LDAP; prepares to connect to the server
108         LDAP * ld = NULL;
109         int err = ldap_initialize(&ld, uri);
110         if (err != LDAP_SUCCESS || ld == NULL)
111         {
112                 Log(LOGERR,"ldap_initialize failed - %s (ld = %p)", ldap_err2string(err), ld);
113                 return err;
114         }
115
116         // Set the LDAP version...
117         int version = LDAP_VERSION3;
118         err = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version); // specify the version
119         if (err != LDAP_SUCCESS)
120         {
121                 Log(LOGERR,"ldap_set_option failed - %s", ldap_err2string(err));
122                 return err;
123         }
124
125         // Attempt to bind using the supplied credentials.
126         // NOTE: ldap_simple_bind_s is "deprecated" in <ldap.h>, but not listed as such in the man pages :S
127         err = ldap_simple_bind_s(ld, dn, pass);
128         if (err != LDAP_SUCCESS)
129         {
130                 Log(LOGERR, "ldap_simple_bind_s failed - %s", ldap_err2string(err));
131         }
132         else
133         {
134                 Log(LOGDEBUG, "Successfully bound to %s with dn %s", uri, dn);
135         }
136
137         int err2 = ldap_unbind_s(ld);
138         if (err2 != LDAP_SUCCESS)
139         {
140                 Log(LOGERR, "ldap_unbind_s failed - %s", ldap_err2string(err2));
141                 err = err2;
142         }
143         return err;
144 }
145
146 /**
147  * Logout
148  * @param context - The context. The key will be cleared.
149  * @param params - Parameter string, UNUSED
150  */
151 void Logout_Handler(FCGIContext * context, char * params)
152 {               
153         FCGI_ReleaseControl(context);
154 }
155
156
157 /**
158  * Handle a Login Request
159  * @param context - The context
160  * @param params - Parameter string, should contain username and password
161  */
162 void Login_Handler(FCGIContext * context, char * params)
163 {
164
165         if (context->control_key[0] != '\0')
166         {
167                 FCGI_RejectJSON(context, "Already logged in.");
168                 return;
169         }
170
171         char * user = ""; // The username supplied through CGI
172         char * pass = ""; // The password supplied through CGI
173                                                 //TODO: Make sure these are passed through HTTPS, *not* HTTP .... otherwise people can eavesdrop on the passwords
174
175         FCGIValue values[] = {
176                 {"user", &user, FCGI_REQUIRED(FCGI_STRING_T)},
177                 {"pass", &pass, FCGI_REQUIRED(FCGI_STRING_T)},
178         };
179
180         //enum to avoid the use of magic numbers
181         typedef enum {
182                 USER,
183                 PASS,
184                 LOGOUT
185         } LoginParams;
186
187         // Fill values appropriately
188         if (!FCGI_ParseRequest(context, params, values, sizeof(values)/sizeof(FCGIValue)))
189         {
190                 // Error occured; FCGI_RejectJSON already called
191                 return;
192         }
193
194
195         // Trim leading whitespace (the BUFSIZ check is to make sure incorrectly terminated strings don't cause an infinite loop)
196         int i = 0;
197         for (i = 0; i < BUFSIZ && isspace(user[0]) && user[0] != '\0'; ++i,++user);
198
199         // Truncate string at first non alphanumeric character
200         for (i = 0; i < BUFSIZ && isalnum(user[i]) && user[i] != '\0'; ++i);
201         user[i] = '\0';
202
203
204
205         
206         bool authenticated = true;
207         
208         switch (g_options.auth_method)
209         {
210
211                 case AUTH_LDAP:
212                 {
213                         if (strlen(pass) <= 0)
214                         {
215                                 FCGI_RejectJSON(context, "No password supplied.");
216                                 return;
217                         }
218
219                         //TODO: Generate the DN in some sane way
220                         char dn[BUFSIZ];
221                 
222                         // On a simple LDAP server:
223                         int len = sprintf(dn, "uid=%s,%s", user, g_options.ldap_base_dn);
224         
225                         // At UWA (hooray)
226                         //char * user_type = (user[0] != '0') : "Students" ? "Staff";
227                         //int len = sprintf(dn, "cn=%s,ou=%s", user, user_type, g_options.ldap_dn_base);
228                 
229
230                         if (len >= BUFSIZ)
231                         {
232                                 FCGI_RejectJSON(context, "DN too long! Recompile with increased BUFSIZ");
233                         }
234                 
235                         authenticated = (Login_LDAP_Bind(g_options.auth_uri, dn, pass) == LDAP_SUCCESS);
236                         break;
237                 }
238                 case AUTH_SHADOW:
239                 {
240                         authenticated = Login_Shadow(user, pass, g_options.auth_uri);
241                         break;
242                 }
243                 default:
244                 {
245                         Log(LOGWARN, "No authentication!");
246                         break;
247                 }
248         }
249                 
250         // error check  
251         
252         if (!authenticated)
253         {
254                 FCGI_RejectJSON(context, "Authentication failure.");
255                 return;
256         }
257
258         FCGI_LockControl(context, false);
259         
260         // Give the user a cookie
261         FCGI_PrintRaw("Content-type: text\r\n");
262         FCGI_PrintRaw("Set-Cookie: %s\r\n\r\n", context->control_key);
263         
264 }

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