Release bumped to "gts4".
[tac_plus.git] / skey_fn.c
1 /*
2    Copyright (c) 1995-1998 by Cisco systems, Inc.
3
4    Permission to use, copy, modify, and distribute this software for
5    any purpose and without fee is hereby granted, provided that this
6    copyright and permission notice appear on all copies of the
7    software and supporting documentation, the name of Cisco Systems,
8    Inc. not be used in advertising or publicity pertaining to
9    distribution of the program without specific prior permission, and
10    notice be given in supporting documentation that modification,
11    copying and distribution is by permission of Cisco Systems, Inc.
12
13    Cisco Systems, Inc. makes no representations about the suitability
14    of this software for any purpose.  THIS SOFTWARE IS PROVIDED ``AS
15    IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16    WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17    FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20
21 #include "tac_plus.h"
22
23 #ifdef SKEY
24
25 #include <skey.h>
26
27 #include "skey_fn.h"
28 #include "expire.h"
29
30
31 /* internal state variables */
32 #define STATE_AUTHEN_START   0  /* no requests issued */
33 #define STATE_AUTHEN_GETUSER 1  /* username has been requested */
34 #define STATE_AUTHEN_GETPASS 2  /* password has been requested */
35
36 struct private_data {
37     struct skey skey;
38     char password[MAX_PASSWD_LEN + 1];
39     int state;
40 };
41
42 /* Use s/key to verify a supplied password using state set up earlier
43 when the username was supplied */
44
45 static int skey_verify TAC_ARGS((char *passwd, struct authen_data *data)); 
46
47 static int
48 skey_verify(passwd, data)
49 char *passwd;
50 struct authen_data *data;
51 {
52     struct private_data *p = data->method_data;
53     struct skey *skeyp = &p->skey;
54
55     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
56
57     if (skeyverify(skeyp, passwd) == 0) {
58         /* S/Key authentication succeeded */
59         data->status = TAC_PLUS_AUTHEN_STATUS_PASS;
60         if (skeyp->n < 5) {
61             data->server_msg = tac_strdup("Password will expire soon");
62             return (1);
63         }
64     }
65     return (0);
66 }
67
68 /*
69  * Skey tacacs login authentication function. Wants a username
70  * and a password, and tries to verify them via skey.
71  *
72  * Choose_authen will ensure that we already have a username before this
73  * gets called.
74  *
75  * We will query for a password and keep it in the method_data.
76  *
77  * Any strings returned via pointers in authen_data must come from the
78  * heap. They will get freed by the caller.
79  *
80  * Return 0 if data->status is valid, otherwise 1
81  */
82
83 int skey_fn TAC_ARGS((struct authen_data *data));
84
85 int
86 skey_fn(data)
87 struct authen_data *data;
88 {
89     char *name, *passwd;
90     struct private_data *p;
91     char *prompt;
92     int pwlen;
93
94     p = (struct private_data *) data->method_data;
95
96     /* An abort has been received. Clean up and return */
97     if (data->flags & TAC_PLUS_CONTINUE_FLAG_ABORT) {
98         if (data->method_data)
99             free(data->method_data);
100         data->method_data = NULL;
101         return (1);
102     }
103     /* Initialise method_data if first time through */
104     if (!p) {
105         p = (struct private_data *) tac_malloc(sizeof(struct private_data));
106         bzero(p, sizeof(struct private_data));
107         data->method_data = p;
108         p->state = STATE_AUTHEN_START;
109     }
110
111     /* Unless we're enabling, we need a username */
112     if (data->service != TAC_PLUS_AUTHEN_SVC_ENABLE &&
113         !(char) data->NAS_id->username[0]) {
114         switch (p->state) {
115
116         case STATE_AUTHEN_GETUSER:
117             /* we have previously asked for a username but none came back.
118              * This is a gross error */
119             data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
120             report(LOG_ERR, "%s: No username supplied after GETUSER",
121                    session.peer);
122             return (0);
123
124         case STATE_AUTHEN_START:
125             /* No username. Try requesting one */
126             data->status = TAC_PLUS_AUTHEN_STATUS_GETUSER;
127             if (data->service == TAC_PLUS_AUTHEN_SVC_LOGIN) {
128                 prompt = "\nUser Access Verification\n\nUsername: ";
129             } else {
130                 prompt = "Username: ";
131             }
132             data->server_msg = tac_strdup(prompt);
133             p->state = STATE_AUTHEN_GETUSER;
134             return (0);
135
136         default:
137             /* something awful has happened. Give up and die */
138             report(LOG_ERR, "%s: skey_fn bad state %d",
139                    session.peer, p->state);
140             return (1);
141         }
142     }
143
144     /* we now have a username if we needed one */
145     name = data->NAS_id->username;
146
147     /* Do we have a password? */
148     passwd = p->password;
149
150     if (!passwd[0]) {
151         char skeyprompt[80];
152
153         /* no password yet. Either we need to ask for one and expect to get
154          * called again, or we asked but nothing came back, which is fatal */
155
156         switch (p->state) {
157         case STATE_AUTHEN_GETPASS:
158             /* We already asked for a password. This should be the reply */
159             if (data->client_msg) {
160                 pwlen = MIN(strlen(data->client_msg), MAX_PASSWD_LEN);
161             } else {
162                 pwlen = 0;
163             }
164             strncpy(passwd, data->client_msg, pwlen);
165             passwd[pwlen] = '\0';
166             break;
167
168         default:
169             /* Request a password */
170             passwd = cfg_get_login_secret(name, TAC_PLUS_RECURSE);
171             if (!passwd && !STREQ(passwd, "skey")) {
172                 report(LOG_ERR, "Cannot find skey password declaration for %s",
173                        name);
174                 data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
175                 return(1);
176             }
177
178             if (skeychallenge(&p->skey, name, skeyprompt) == 0) {
179                 char buf[256];
180                 sprintf(buf, "%s\nPassword: ", skeyprompt);
181                 data->server_msg = tac_strdup(buf);
182                 data->status = TAC_PLUS_AUTHEN_STATUS_GETPASS;
183                 p->state = STATE_AUTHEN_GETPASS;
184                 return (0);
185             }
186
187             data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
188             report(LOG_ERR, "Cannot generate skey prompt for %s", name);
189             return(1);
190         }
191     }
192
193     /* We have a username and password. Try validating */
194
195     /* Assume the worst */
196     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
197
198     switch (data->service) {
199     case TAC_PLUS_AUTHEN_SVC_LOGIN:
200         skey_verify(passwd, data);
201         if (debug)
202             report(LOG_INFO, "login query for '%s' %s from %s %s",
203                    name && name[0] ? name : "unknown",
204                    data->NAS_id->NAS_port && data->NAS_id->NAS_port[0] ?
205                        data->NAS_id->NAS_port : "unknown",
206                    session.peer,
207                    (data->status == TAC_PLUS_AUTHEN_STATUS_PASS) ?
208                    "accepted" : "rejected");
209         break;
210
211     default:
212         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
213         report(LOG_ERR, "%s: Bogus service value %d from packet",
214                session.peer, data->service);
215         break;
216     }
217
218     if (data->method_data)
219         free(data->method_data);
220     data->method_data = NULL;
221
222     switch (data->status) {
223     case TAC_PLUS_AUTHEN_STATUS_ERROR:
224     case TAC_PLUS_AUTHEN_STATUS_FAIL:
225     case TAC_PLUS_AUTHEN_STATUS_PASS:
226         return (0);
227     default:
228         report(LOG_ERR, "%s: skey_fn couldn't set recognizable status %d",
229                session.peer, data->status);
230         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
231         return (1);
232     }
233 }
234
235 #else /* SKEY */
236
237 TAC_SOURCEFILE_EMPTY
238
239 #endif /* SKEY */