Release bumped to "gts4".
[tac_plus.git] / default_v0_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 #include <stdlib.h>
24 #include <string.h>
25
26 #include "default_v0_fn.h"
27 #include "expire.h"
28 #include "utils.h"
29 #include "report.h"
30 #include "pwlib.h"
31 #include "choose_authen.h"              /* for "struct authen_data" */
32 #include "do_author.h"                  /* for "struct identity" */
33 #include "packet.h"
34 #include "main.h"
35 #include "cfgfile.h"
36
37
38 /* internal state variables */
39 #define STATE_AUTHEN_START   0  /* no requests issued */
40 #define STATE_AUTHEN_GETUSER 1  /* username has been requested */
41 #define STATE_AUTHEN_GETPASS 2  /* password has been requested */
42
43 struct private_data {
44     char password[MAX_PASSWD_LEN + 1];
45     int state;
46 };
47
48 /*
49  * Default tacacs login authentication function. Wants a username
50  * and a password, and tries to verify them.
51  *
52  * Choose_authen will ensure that we already have a username before this
53  * gets called.
54  *
55  * We will query for a password and keep it in the method_data.
56  *
57  * Any strings returned via pointers in authen_data must come from the
58  * heap. They will get freed by the caller.
59  *
60  * Return 0 if data->status is valid, otherwise 1
61  */
62
63 int default_v0_fn TAC_ARGS((struct authen_data *data));
64
65 int
66 default_v0_fn(data)
67 struct authen_data *data;
68 {
69     char *name, *passwd;
70     struct private_data *p;
71     char *prompt;
72
73     p = (struct private_data *) data->method_data;
74
75     /* An abort has been received. Clean up and return */
76     if (data->flags & TAC_PLUS_CONTINUE_FLAG_ABORT) {
77         if (data->method_data)
78             free(data->method_data);
79         data->method_data = NULL;
80         return (1);
81     }
82     /* Initialise method_data if first time through */
83     if (!p) {
84         p = (struct private_data *) tac_malloc(sizeof(struct private_data));
85         bzero(p, sizeof(struct private_data));
86         data->method_data = p;
87         p->state = STATE_AUTHEN_START;
88     }
89
90     /* Unless we're enabling, we need a username */
91     if (data->service != TAC_PLUS_AUTHEN_SVC_ENABLE &&
92         !(char) data->NAS_id->username[0]) {
93         switch (p->state) {
94
95         case STATE_AUTHEN_GETUSER:
96             /* we have previously asked for a username but none came back.
97              * This is a gross error */
98             data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
99             report(LOG_ERR, "%s: No username supplied after GETUSER",
100                    session.peer);
101             return (0);
102
103         case STATE_AUTHEN_START:
104             /* No username. Try requesting one */
105             data->status = TAC_PLUS_AUTHEN_STATUS_GETUSER;
106             if (data->service == TAC_PLUS_AUTHEN_SVC_LOGIN) {
107                 prompt = "\nUser Access Verification\n\nUsername: ";
108             } else {
109                 prompt = "Username: ";
110             }
111             data->server_msg = tac_strdup(prompt);
112             p->state = STATE_AUTHEN_GETUSER;
113             return (0);
114
115         default:
116             /* something awful has happened. Give up and die */
117             report(LOG_ERR, "%s: default_fn bad state %d",
118                    session.peer, p->state);
119             return (1);
120         }
121     }
122
123     /* we now have a username if we needed one */
124     name = data->NAS_id->username;
125
126     /* Do we have a password? */
127     passwd = p->password;
128
129     if (!passwd[0]) {
130
131         /* no password yet. Either we need to ask for one and expect to get
132          * called again, or we asked but nothing came back, which is fatal */
133
134         switch (p->state) {
135         case STATE_AUTHEN_GETPASS:
136             /* We already asked for a password. This should be the reply */
137             strncpy(passwd, data->client_msg, MAX_PASSWD_LEN);
138             passwd[MAX_PASSWD_LEN + 1] = '\0';
139             break;
140
141         default:
142             data->flags = TAC_PLUS_AUTHEN_FLAG_NOECHO;
143             data->server_msg = tac_strdup("Password: ");
144             data->status = TAC_PLUS_AUTHEN_STATUS_GETPASS;
145             p->state = STATE_AUTHEN_GETPASS;
146             return (0);
147         }
148     }
149
150     /* We have a username and password. Try validating */
151
152     if (STREQ(name, DEFAULT_USERNAME)) {
153         /* Never authenticate this user. It's for authorization only */
154         data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
155         if (debug) {
156             report(LOG_DEBUG,
157                    "authentication query for '%s' %s from %s rejected",
158                    name && name[0] ? name : "unknown",
159                    session.port, session.peer);
160         }
161         return(0);
162     }
163
164     /* Assume the worst */
165     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
166
167     switch (data->service) {
168     case TAC_PLUS_AUTHEN_SVC_NASI:
169     case TAC_PLUS_AUTHEN_SVC_LOGIN:
170     case TAC_PLUS_AUTHEN_SVC_PPP:
171         verify(name, passwd, data, TAC_PLUS_RECURSE);
172         if (debug)
173             report(LOG_INFO, "login query for '%s' %s from %s %s",
174                    name && name[0] ? name : "unknown",
175                    data->NAS_id->NAS_port && data->NAS_id->NAS_port[0] ?
176                        data->NAS_id->NAS_port : "unknown",
177                    session.peer,
178                    (data->status == TAC_PLUS_AUTHEN_STATUS_PASS) ?
179                    "accepted" : "rejected");
180         break;
181
182     default:
183         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
184         report(LOG_ERR, "%s: Bogus service value %d from packet",
185                session.peer, data->service);
186         break;
187     }
188
189     if (data->method_data)
190         free(data->method_data);
191     data->method_data = NULL;
192
193     switch (data->status) {
194     case TAC_PLUS_AUTHEN_STATUS_ERROR:
195     case TAC_PLUS_AUTHEN_STATUS_FAIL:
196     case TAC_PLUS_AUTHEN_STATUS_PASS:
197         return (0);
198     default:
199         report(LOG_ERR, "%s: default_v0_fn can't set status %d",
200                session.peer, data->status);
201         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
202         return (1);
203     }
204 }