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