* More license declarations
[citadel.git] / citadel / modules / pas2 / 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  * Copyright (c) 1994-2009 by the citadel.org team
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # if HAVE_SYS_TIME_H
31 #  include <sys/time.h>
32 # else
33 #  include <time.h>
34 # endif
35 #endif
36
37 #include <ctype.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <libcitadel.h>
41 #include "citadel.h"
42 #include "server.h"
43 #include "citserver.h"
44 #include "support.h"
45 #include "user_ops.h"
46 #include "md5.h"
47
48
49 #include "ctdl_module.h"
50
51
52 void cmd_pas2(char *argbuf)
53 {
54         char pw[256];
55         char hexstring[MD5_HEXSTRING_SIZE];
56         
57
58         if (!strcmp(CC->curr_user, NLI))
59         {
60                 cprintf("%d You must enter a user with the USER command first.\n", ERROR + USERNAME_REQUIRED);
61                 return;
62         }
63         
64         if (CC->logged_in)
65         {
66                 cprintf("%d Already logged in.\n", ERROR + ALREADY_LOGGED_IN);
67                 return;
68         }
69         
70         extract_token(pw, argbuf, 0, '|', sizeof pw);
71         
72         if (getuser(&CC->user, CC->curr_user))
73         {
74                 cprintf("%d Unable to find user record for %s.\n", ERROR + NO_SUCH_USER, CC->curr_user);
75                 return;
76         }
77         
78         strproc(pw);
79         strproc(CC->user.password);
80         
81         if (strlen(pw) != (MD5_HEXSTRING_SIZE-1))
82         {
83                 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);
84                 return;
85         }
86         
87         make_apop_string(CC->user.password, CC->cs_nonce, hexstring, sizeof hexstring);
88         
89         if (!strcmp(hexstring, pw))
90         {
91                 do_login();
92                 return;
93         }
94         else
95         {
96                 cprintf("%d Wrong password.\n", ERROR + PASSWORD_REQUIRED);
97                 return;
98         }
99 }
100
101
102
103
104
105 CTDL_MODULE_INIT(pas2)
106 {
107         if (!threading)
108         {
109                 CtdlRegisterProtoHook(cmd_pas2, "PAS2", "APOP-based login");
110         }
111         
112         /* return our Subversion id for the Log */
113         return "$Id$";
114 }