f2bc4e62f2b8f15afe4ee66c3b749e4f6f30cc4d
[citadel.git] / citadel / serv_pas2.c
1 /*
2  * cmd_pas2 - MD5 APOP style auth keyed off of the hash of the password
3  *            plus a nonce displayed at the login banner.
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9
10 #if TIME_WITH_SYS_TIME
11 # include <sys/time.h>
12 # include <time.h>
13 #else
14 # if HAVE_SYS_TIME_H
15 #  include <sys/time.h>
16 # else
17 #  include <time.h>
18 # endif
19 #endif
20
21 #include <ctype.h>
22 #include <string.h>
23 #include <errno.h>
24 #include "sysdep_decls.h"
25 #include "citadel.h"
26 #include "server.h"
27 #include "citserver.h"
28 #include "support.h"
29 #include "serv_extensions.h"
30 #include "user_ops.h"
31 #include "md5.h"
32 #include "tools.h"
33
34
35 void cmd_pas2(char *argbuf)
36 {
37         char pw[256];
38         char hexstring[MD5_HEXSTRING_SIZE];
39         
40
41         if (!strcmp(CC->curr_user, NLI))
42         {
43                 cprintf("%d You must enter a user with the USER command first.\n", ERROR + USERNAME_REQUIRED);
44                 return;
45         }
46         
47         if (CC->logged_in)
48         {
49                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
50                 return;
51         }
52         
53         extract_token(pw, argbuf, 0, '|', sizeof pw);
54         
55         if (getuser(&CC->user, CC->curr_user))
56         {
57                 cprintf("%d Unable to find user record for %s.\n", ERROR + NO_SUCH_USER, CC->curr_user);
58                 return;
59         }
60         
61         strproc(pw);
62         strproc(CC->user.password);
63         
64         if (strlen(pw) != (MD5_HEXSTRING_SIZE-1))
65         {
66                 cprintf("%d Auth string of length %ld is the wrong length (should be %d).\n", ERROR + ILLEGAL_VALUE, (long)strlen(pw), MD5_HEXSTRING_SIZE-1);
67                 return;
68         }
69         
70         make_apop_string(CC->user.password, CC->cs_nonce, hexstring, sizeof hexstring);
71         
72         if (!strcmp(hexstring, pw))
73         {
74                 do_login();
75                 return;
76         }
77         else
78         {
79                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
80                 return;
81         }
82 }
83
84
85
86
87
88 char *serv_pas2_init(void)
89 {
90         CtdlRegisterProtoHook(cmd_pas2, "PAS2", "APOP-based login");
91
92         /* return our Subversion id for the Log */
93         return "$Id$";
94 }