Import of tac_plus.v8.tar.gz: 173206 bytes, md5:
[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 #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 static void
34 enable(passwd, data)
35 char *passwd;
36 struct authen_data *data;
37 {
38     int level = data->NAS_id->priv_lvl;
39
40     /* sanity check */
41     if (level < TAC_PLUS_PRIV_LVL_MIN || level > TAC_PLUS_PRIV_LVL_MAX) {
42         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
43         data->server_msg = tac_strdup("Invalid privilege level in packet");
44         report(LOG_ERR, "%s level=%d %s", session.peer, level, data->server_msg);
45         return;
46     }
47     /* 0 <= level <= 14: look for $enab<n>$ and verify */
48     if (level < TAC_PLUS_PRIV_LVL_MAX) {
49         char buf[11];
50
51         sprintf(buf, "$enab%d$", level);
52         if (!verify(buf, passwd, data, TAC_PLUS_NORECURSE))
53             data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
54         return;
55     }
56
57     /* 2). level=15. Try $enab15$ or $enable$ (for backwards
58        compatibility) and verify */
59
60     if (verify("$enable$", passwd, data, TAC_PLUS_NORECURSE) ||
61         verify("$enab15$", passwd, data, TAC_PLUS_NORECURSE)) {
62         return;
63     }
64
65     /* return fail */
66     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
67
68     return;
69 }
70
71
72 /*
73  * Tacacs enable authentication function. Wants an enable
74  * password, and tries to verify it.
75  *
76  * Choose_authen will ensure that we already have a username before this
77  * gets called.
78  *
79  * We will query for a password and keep it in the method_data.
80  *
81  * Any strings returned via pointers in authen_data must come from the
82  * heap. They will get freed by the caller.
83  *
84  * Return 0 if data->status is valid, otherwise 1
85  */
86
87 int
88 enable_fn(data)
89 struct authen_data *data;
90 {
91     char *passwd;
92     struct private_data *p;
93     int pwlen;
94
95     p = (struct private_data *) data->method_data;
96
97     /* An abort has been received. Clean up and return */
98     if (data->flags & TAC_PLUS_CONTINUE_FLAG_ABORT) {
99         if (data->method_data)
100             free(data->method_data);
101         data->method_data = NULL;
102         return (1);
103     }
104     /* Initialise method_data if first time through */
105     if (!p) {
106         p = (struct private_data *) tac_malloc(sizeof(struct private_data));
107         bzero(p, sizeof(struct private_data));
108         data->method_data = p;
109         p->state = STATE_AUTHEN_START;
110     }
111
112     /* As we're enabling, we don't need a username, but do we have a
113        password? */
114
115     passwd = p->password;
116
117     if (!passwd[0]) {
118
119         /* No password. Either we need to ask for one and expect to get
120          * called again, or we asked but nothing came back, which is fatal */
121
122         switch (p->state) {
123         case STATE_AUTHEN_GETPASS:
124             /* We already asked for a password. This should be the
125                reply */
126             if (data->client_msg) {
127                 pwlen = MIN((int)strlen(data->client_msg), MAX_PASSWD_LEN);
128             } else {
129                 pwlen = 0;
130             }
131             strncpy(passwd, data->client_msg, pwlen);
132             passwd[pwlen] = '\0';
133             break;
134
135         default:
136             /* Request a password */
137             data->flags = TAC_PLUS_AUTHEN_FLAG_NOECHO;
138             data->server_msg = tac_strdup("Password: ");
139             data->status = TAC_PLUS_AUTHEN_STATUS_GETPASS;
140             p->state = STATE_AUTHEN_GETPASS;
141             return (0);
142         }
143     }
144
145     /* We have a password. Try validating */
146
147     /* Assume the worst */
148     data->status = TAC_PLUS_AUTHEN_STATUS_FAIL;
149
150     switch (data->service) {
151     case TAC_PLUS_AUTHEN_SVC_ENABLE:
152         enable(passwd, data);
153         if (debug) {
154             char *name = data->NAS_id->username;
155
156             report(LOG_INFO, "enable 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         }
164         break;
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: authenticate_fn can't set status %d",
183                session.peer, data->status);
184         data->status = TAC_PLUS_AUTHEN_STATUS_ERROR;
185         return (1);
186     }
187 }