Release bumped to "gts4".
[tac_plus.git] / enable.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 "enable.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 static void enable TAC_ARGS((char *passwd, struct authen_data *data));
49
50 static void
51 enable(passwd, data)
52 char *passwd;
53 struct authen_data *data;
54 {
55     int level = data->NAS_id->priv_lvl;
56
57     /* sanity check */
58     if (level < TAC_PLUS_PRIV_LVL_MIN || level > TAC_PLUS_PRIV_LVL_MAX) {
59         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
60         data->server_msg = tac_strdup("Invalid privilege level in packet");
61         report(LOG_ERR, "%s level=%d %s", session.peer, level, data->server_msg);
62         return;
63     }
64     /* 0 <= level <= 14: look for $enab<n>$ and verify */
65     if (level < TAC_PLUS_PRIV_LVL_MAX) {
66         char buf[11];
67
68         sprintf(buf, "$enab%d$", level);
69         if (!verify(buf, passwd, data, TAC_PLUS_NORECURSE))
70             data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
71         return;
72     }
73
74     /* 2). level=15. Try $enab15$ or $enable$ (for backwards
75        compatibility) and verify */
76
77     if (verify("$enable$", passwd, data, TAC_PLUS_NORECURSE) ||
78         verify("$enab15$", passwd, data, TAC_PLUS_NORECURSE)) {
79         return;
80     }
81
82     /* return fail */
83     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
84
85     return;
86 }
87
88
89 /*
90  * Tacacs enable authentication function. Wants an enable
91  * password, and tries to verify it.
92  *
93  * Choose_authen will ensure that we already have a username before this
94  * gets called.
95  *
96  * We will query for a password and keep it in the method_data.
97  *
98  * Any strings returned via pointers in authen_data must come from the
99  * heap. They will get freed by the caller.
100  *
101  * Return 0 if data->status is valid, otherwise 1
102  */
103
104 int enable_fn TAC_ARGS((struct authen_data *data));
105
106 int
107 enable_fn(data)
108 struct authen_data *data;
109 {
110     char *passwd;
111     struct private_data *p;
112     int pwlen;
113
114     p = (struct private_data *) data->method_data;
115
116     /* An abort has been received. Clean up and return */
117     if (data->flags & TAC_PLUS_CONTINUE_FLAG_ABORT) {
118         if (data->method_data)
119             free(data->method_data);
120         data->method_data = NULL;
121         return (1);
122     }
123     /* Initialise method_data if first time through */
124     if (!p) {
125         p = (struct private_data *) tac_malloc(sizeof(struct private_data));
126         bzero(p, sizeof(struct private_data));
127         data->method_data = p;
128         p->state = STATE_AUTHEN_START;
129     }
130
131     /* As we're enabling, we don't need a username, but do we have a
132        password? */
133
134     passwd = p->password;
135
136     if (!passwd[0]) {
137
138         /* No password. Either we need to ask for one and expect to get
139          * called again, or we asked but nothing came back, which is fatal */
140
141         switch (p->state) {
142         case STATE_AUTHEN_GETPASS:
143             /* We already asked for a password. This should be the
144                reply */
145             if (data->client_msg) {
146                 pwlen = MIN((int)strlen(data->client_msg), MAX_PASSWD_LEN);
147             } else {
148                 pwlen = 0;
149             }
150             strncpy(passwd, data->client_msg, pwlen);
151             passwd[pwlen] = '\0';
152             break;
153
154         default:
155             /* Request a password */
156             data->flags = TAC_PLUS_AUTHEN_FLAG_NOECHO;
157             data->server_msg = tac_strdup("Password: ");
158             data->status = TAC_PLUS_AUTHEN_STATUS_GETPASS;
159             p->state = STATE_AUTHEN_GETPASS;
160             return (0);
161         }
162     }
163
164     /* We have a password. Try validating */
165
166     /* Assume the worst */
167     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
168
169     switch (data->service) {
170     case TAC_PLUS_AUTHEN_SVC_ENABLE:
171         enable(passwd, data);
172         if (debug) {
173             char *name = data->NAS_id->username;
174
175             report(LOG_INFO, "enable query for '%s' %s from %s %s",
176                    name && name[0] ? name : "unknown",
177                    data->NAS_id->NAS_port && data->NAS_id->NAS_port[0] ?
178                        data->NAS_id->NAS_port : "unknown",
179                    session.peer,
180                    (data->status == TAC_PLUS_AUTHEN_STATUS_PASS) ?
181                    "accepted" : "rejected");
182         }
183         break;
184     default:
185         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
186         report(LOG_ERR, "%s: Bogus service value %d from packet",
187                session.peer, data->service);
188         break;
189     }
190
191     if (data->method_data)
192         free(data->method_data);
193     data->method_data = NULL;
194
195     switch (data->status) {
196     case TAC_PLUS_AUTHEN_STATUS_ERROR:
197     case TAC_PLUS_AUTHEN_STATUS_FAIL:
198     case TAC_PLUS_AUTHEN_STATUS_PASS:
199         return (0);
200     default:
201         report(LOG_ERR, "%s: authenticate_fn can't set status %d",
202                session.peer, data->status);
203         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
204         return (1);
205     }
206 }