auth.c revision 1.22 1 1.22 christos /* $NetBSD: auth.c,v 1.22 2018/12/14 23:42:39 christos Exp $ */
2 1.7 christos
3 1.1 cgd /*-
4 1.3 cgd * Copyright (c) 1991, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.16 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.6 lukem #if 0
35 1.6 lukem static char sccsid[] = "@(#)auth.c 8.3 (Berkeley) 5/30/95"
36 1.6 lukem #else
37 1.22 christos __RCSID("$NetBSD: auth.c,v 1.22 2018/12/14 23:42:39 christos Exp $");
38 1.6 lukem #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Copyright (C) 1990 by the Massachusetts Institute of Technology
43 1.1 cgd *
44 1.1 cgd * Export of this software from the United States of America is assumed
45 1.1 cgd * to require a specific license from the United States Government.
46 1.1 cgd * It is the responsibility of any person or organization contemplating
47 1.1 cgd * export to obtain such a license before exporting.
48 1.1 cgd *
49 1.1 cgd * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
50 1.1 cgd * distribute this software and its documentation for any purpose and
51 1.1 cgd * without fee is hereby granted, provided that the above copyright
52 1.1 cgd * notice appear in all copies and that both that copyright notice and
53 1.1 cgd * this permission notice appear in supporting documentation, and that
54 1.1 cgd * the name of M.I.T. not be used in advertising or publicity pertaining
55 1.1 cgd * to distribution of the software without specific, written prior
56 1.1 cgd * permission. M.I.T. makes no representations about the suitability of
57 1.1 cgd * this software for any purpose. It is provided "as is" without express
58 1.1 cgd * or implied warranty.
59 1.1 cgd */
60 1.1 cgd
61 1.1 cgd
62 1.13 itojun #ifdef AUTHENTICATION
63 1.1 cgd #include <stdio.h>
64 1.1 cgd #include <sys/types.h>
65 1.1 cgd #include <signal.h>
66 1.1 cgd #define AUTH_NAMES
67 1.1 cgd #include <arpa/telnet.h>
68 1.1 cgd #include <stdlib.h>
69 1.11 thorpej #include <unistd.h>
70 1.1 cgd #ifdef NO_STRING_H
71 1.1 cgd #include <strings.h>
72 1.1 cgd #else
73 1.1 cgd #include <string.h>
74 1.1 cgd #endif
75 1.1 cgd
76 1.1 cgd #include "encrypt.h"
77 1.1 cgd #include "auth.h"
78 1.1 cgd #include "misc-proto.h"
79 1.1 cgd #include "auth-proto.h"
80 1.1 cgd
81 1.1 cgd #define typemask(x) (1<<((x)-1))
82 1.1 cgd
83 1.3 cgd #ifdef RSA_ENCPWD
84 1.3 cgd extern rsaencpwd_init();
85 1.3 cgd extern rsaencpwd_send();
86 1.3 cgd extern rsaencpwd_is();
87 1.3 cgd extern rsaencpwd_reply();
88 1.3 cgd extern rsaencpwd_status();
89 1.3 cgd extern rsaencpwd_printsub();
90 1.3 cgd #endif
91 1.3 cgd
92 1.1 cgd int auth_debug_mode = 0;
93 1.9 mycroft static const char *Name = "Noname";
94 1.1 cgd static int Server = 0;
95 1.1 cgd static Authenticator *authenticated = 0;
96 1.1 cgd static int authenticating = 0;
97 1.1 cgd static int validuser = 0;
98 1.1 cgd static unsigned char _auth_send_data[256];
99 1.1 cgd static unsigned char *auth_send_data;
100 1.1 cgd static int auth_send_cnt = 0;
101 1.1 cgd
102 1.17 perry static void auth_intr(int);
103 1.11 thorpej
104 1.1 cgd /*
105 1.1 cgd * Authentication types supported. Plese note that these are stored
106 1.1 cgd * in priority order, i.e. try the first one first.
107 1.1 cgd */
108 1.1 cgd Authenticator authenticators[] = {
109 1.3 cgd #ifdef SPX
110 1.3 cgd { AUTHTYPE_SPX, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
111 1.3 cgd spx_init,
112 1.3 cgd spx_send,
113 1.3 cgd spx_is,
114 1.3 cgd spx_reply,
115 1.3 cgd spx_status,
116 1.3 cgd spx_printsub },
117 1.3 cgd { AUTHTYPE_SPX, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
118 1.3 cgd spx_init,
119 1.3 cgd spx_send,
120 1.3 cgd spx_is,
121 1.3 cgd spx_reply,
122 1.3 cgd spx_status,
123 1.3 cgd spx_printsub },
124 1.3 cgd #endif
125 1.1 cgd #ifdef KRB5
126 1.11 thorpej # ifdef ENCRYPTION
127 1.11 thorpej { AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
128 1.11 thorpej kerberos5_init,
129 1.11 thorpej kerberos5_send,
130 1.11 thorpej kerberos5_is,
131 1.11 thorpej kerberos5_reply,
132 1.11 thorpej kerberos5_status,
133 1.11 thorpej kerberos5_printsub },
134 1.11 thorpej # endif /* ENCRYPTION */
135 1.1 cgd { AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
136 1.1 cgd kerberos5_init,
137 1.1 cgd kerberos5_send,
138 1.1 cgd kerberos5_is,
139 1.1 cgd kerberos5_reply,
140 1.1 cgd kerberos5_status,
141 1.1 cgd kerberos5_printsub },
142 1.1 cgd #endif
143 1.3 cgd #ifdef RSA_ENCPWD
144 1.3 cgd { AUTHTYPE_RSA_ENCPWD, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
145 1.3 cgd rsaencpwd_init,
146 1.3 cgd rsaencpwd_send,
147 1.3 cgd rsaencpwd_is,
148 1.3 cgd rsaencpwd_reply,
149 1.3 cgd rsaencpwd_status,
150 1.3 cgd rsaencpwd_printsub },
151 1.3 cgd #endif
152 1.18 christos #ifdef SRA
153 1.18 christos { AUTHTYPE_SRA, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
154 1.18 christos sra_init,
155 1.18 christos sra_send,
156 1.18 christos sra_is,
157 1.18 christos sra_reply,
158 1.18 christos sra_status,
159 1.18 christos sra_printsub },
160 1.18 christos
161 1.18 christos #endif
162 1.18 christos { 0, 0, 0, 0, 0, 0, 0, 0 },
163 1.1 cgd };
164 1.1 cgd
165 1.20 christos static Authenticator NoAuth = { .type = 0 };
166 1.1 cgd
167 1.1 cgd static int i_support = 0;
168 1.1 cgd static int i_wont_support = 0;
169 1.1 cgd
170 1.21 matt Authenticator *
171 1.21 matt findauthenticator(int type, int way)
172 1.1 cgd {
173 1.1 cgd Authenticator *ap = authenticators;
174 1.1 cgd
175 1.1 cgd while (ap->type && (ap->type != type || ap->way != way))
176 1.1 cgd ++ap;
177 1.1 cgd return(ap->type ? ap : 0);
178 1.1 cgd }
179 1.1 cgd
180 1.21 matt void
181 1.21 matt auth_init(const char *name, int server)
182 1.1 cgd {
183 1.1 cgd Authenticator *ap = authenticators;
184 1.1 cgd
185 1.1 cgd Server = server;
186 1.1 cgd Name = name;
187 1.1 cgd
188 1.1 cgd i_support = 0;
189 1.1 cgd authenticated = 0;
190 1.1 cgd authenticating = 0;
191 1.1 cgd while (ap->type) {
192 1.1 cgd if (!ap->init || (*ap->init)(ap, server)) {
193 1.1 cgd i_support |= typemask(ap->type);
194 1.1 cgd if (auth_debug_mode)
195 1.1 cgd printf(">>>%s: I support auth type %d %d\r\n",
196 1.1 cgd Name,
197 1.1 cgd ap->type, ap->way);
198 1.1 cgd }
199 1.5 jtk else if (auth_debug_mode)
200 1.5 jtk printf(">>>%s: Init failed: auth type %d %d\r\n",
201 1.5 jtk Name, ap->type, ap->way);
202 1.1 cgd ++ap;
203 1.1 cgd }
204 1.1 cgd }
205 1.1 cgd
206 1.21 matt void
207 1.21 matt auth_disable_name(char *name)
208 1.1 cgd {
209 1.1 cgd int x;
210 1.1 cgd for (x = 0; x < AUTHTYPE_CNT; ++x) {
211 1.18 christos if (AUTHTYPE_NAME(x) && !strcasecmp(name, AUTHTYPE_NAME(x))) {
212 1.1 cgd i_wont_support |= typemask(x);
213 1.1 cgd break;
214 1.1 cgd }
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd
218 1.21 matt int
219 1.22 christos getauthmask(const char *type, int *maskp)
220 1.1 cgd {
221 1.1 cgd register int x;
222 1.1 cgd
223 1.18 christos if (AUTHTYPE_NAME(0) && !strcasecmp(type, AUTHTYPE_NAME(0))) {
224 1.1 cgd *maskp = -1;
225 1.1 cgd return(1);
226 1.1 cgd }
227 1.1 cgd
228 1.1 cgd for (x = 1; x < AUTHTYPE_CNT; ++x) {
229 1.18 christos if (AUTHTYPE_NAME(x) && !strcasecmp(type, AUTHTYPE_NAME(x))) {
230 1.1 cgd *maskp = typemask(x);
231 1.1 cgd return(1);
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd return(0);
235 1.1 cgd }
236 1.1 cgd
237 1.21 matt int
238 1.22 christos auth_enable(const char *type)
239 1.1 cgd {
240 1.1 cgd return(auth_onoff(type, 1));
241 1.1 cgd }
242 1.1 cgd
243 1.21 matt int
244 1.22 christos auth_disable(const char *type)
245 1.1 cgd {
246 1.1 cgd return(auth_onoff(type, 0));
247 1.1 cgd }
248 1.1 cgd
249 1.21 matt int
250 1.22 christos auth_onoff(const char *type, int on)
251 1.1 cgd {
252 1.5 jtk int i, mask = -1;
253 1.1 cgd Authenticator *ap;
254 1.1 cgd
255 1.1 cgd if (!strcasecmp(type, "?") || !strcasecmp(type, "help")) {
256 1.5 jtk printf("auth %s 'type'\n", on ? "enable" : "disable");
257 1.1 cgd printf("Where 'type' is one of:\n");
258 1.1 cgd printf("\t%s\n", AUTHTYPE_NAME(0));
259 1.5 jtk mask = 0;
260 1.5 jtk for (ap = authenticators; ap->type; ap++) {
261 1.5 jtk if ((mask & (i = typemask(ap->type))) != 0)
262 1.5 jtk continue;
263 1.5 jtk mask |= i;
264 1.1 cgd printf("\t%s\n", AUTHTYPE_NAME(ap->type));
265 1.5 jtk }
266 1.1 cgd return(0);
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd if (!getauthmask(type, &mask)) {
270 1.1 cgd printf("%s: invalid authentication type\n", type);
271 1.1 cgd return(0);
272 1.1 cgd }
273 1.1 cgd if (on)
274 1.1 cgd i_wont_support &= ~mask;
275 1.1 cgd else
276 1.1 cgd i_wont_support |= mask;
277 1.1 cgd return(1);
278 1.1 cgd }
279 1.1 cgd
280 1.21 matt int
281 1.21 matt auth_togdebug(int on)
282 1.1 cgd {
283 1.1 cgd if (on < 0)
284 1.1 cgd auth_debug_mode ^= 1;
285 1.1 cgd else
286 1.1 cgd auth_debug_mode = on;
287 1.1 cgd printf("auth debugging %s\n", auth_debug_mode ? "enabled" : "disabled");
288 1.1 cgd return(1);
289 1.1 cgd }
290 1.1 cgd
291 1.21 matt int
292 1.22 christos auth_status(const char *s)
293 1.1 cgd {
294 1.1 cgd Authenticator *ap;
295 1.5 jtk int i, mask;
296 1.1 cgd
297 1.1 cgd if (i_wont_support == -1)
298 1.1 cgd printf("Authentication disabled\n");
299 1.1 cgd else
300 1.1 cgd printf("Authentication enabled\n");
301 1.1 cgd
302 1.5 jtk mask = 0;
303 1.5 jtk for (ap = authenticators; ap->type; ap++) {
304 1.5 jtk if ((mask & (i = typemask(ap->type))) != 0)
305 1.5 jtk continue;
306 1.5 jtk mask |= i;
307 1.1 cgd printf("%s: %s\n", AUTHTYPE_NAME(ap->type),
308 1.1 cgd (i_wont_support & typemask(ap->type)) ?
309 1.1 cgd "disabled" : "enabled");
310 1.5 jtk }
311 1.1 cgd return(1);
312 1.1 cgd }
313 1.1 cgd
314 1.1 cgd /*
315 1.1 cgd * This routine is called by the server to start authentication
316 1.1 cgd * negotiation.
317 1.1 cgd */
318 1.21 matt void
319 1.21 matt auth_request(void)
320 1.1 cgd {
321 1.1 cgd static unsigned char str_request[64] = { IAC, SB,
322 1.1 cgd TELOPT_AUTHENTICATION,
323 1.1 cgd TELQUAL_SEND, };
324 1.1 cgd Authenticator *ap = authenticators;
325 1.1 cgd unsigned char *e = str_request + 4;
326 1.1 cgd
327 1.1 cgd if (!authenticating) {
328 1.1 cgd authenticating = 1;
329 1.1 cgd while (ap->type) {
330 1.1 cgd if (i_support & ~i_wont_support & typemask(ap->type)) {
331 1.1 cgd if (auth_debug_mode) {
332 1.1 cgd printf(">>>%s: Sending type %d %d\r\n",
333 1.1 cgd Name, ap->type, ap->way);
334 1.1 cgd }
335 1.1 cgd *e++ = ap->type;
336 1.1 cgd *e++ = ap->way;
337 1.1 cgd }
338 1.1 cgd ++ap;
339 1.1 cgd }
340 1.1 cgd *e++ = IAC;
341 1.1 cgd *e++ = SE;
342 1.10 thorpej telnet_net_write(str_request, e - str_request);
343 1.1 cgd printsub('>', &str_request[2], e - str_request - 2);
344 1.1 cgd }
345 1.1 cgd }
346 1.1 cgd
347 1.1 cgd /*
348 1.1 cgd * This is called when an AUTH SEND is received.
349 1.1 cgd * It should never arrive on the server side (as only the server can
350 1.1 cgd * send an AUTH SEND).
351 1.1 cgd * You should probably respond to it if you can...
352 1.1 cgd *
353 1.1 cgd * If you want to respond to the types out of order (i.e. even
354 1.1 cgd * if he sends LOGIN KERBEROS and you support both, you respond
355 1.1 cgd * with KERBEROS instead of LOGIN (which is against what the
356 1.1 cgd * protocol says)) you will have to hack this code...
357 1.1 cgd */
358 1.21 matt void
359 1.21 matt auth_send(unsigned char *data, int cnt)
360 1.1 cgd {
361 1.1 cgd Authenticator *ap;
362 1.1 cgd static unsigned char str_none[] = { IAC, SB, TELOPT_AUTHENTICATION,
363 1.1 cgd TELQUAL_IS, AUTHTYPE_NULL, 0,
364 1.1 cgd IAC, SE };
365 1.1 cgd if (Server) {
366 1.1 cgd if (auth_debug_mode) {
367 1.1 cgd printf(">>>%s: auth_send called!\r\n", Name);
368 1.1 cgd }
369 1.1 cgd return;
370 1.1 cgd }
371 1.1 cgd
372 1.1 cgd if (auth_debug_mode) {
373 1.1 cgd printf(">>>%s: auth_send got:", Name);
374 1.1 cgd printd(data, cnt); printf("\r\n");
375 1.1 cgd }
376 1.1 cgd
377 1.1 cgd /*
378 1.1 cgd * Save the data, if it is new, so that we can continue looking
379 1.1 cgd * at it if the authorization we try doesn't work
380 1.1 cgd */
381 1.1 cgd if (data < _auth_send_data ||
382 1.1 cgd data > _auth_send_data + sizeof(_auth_send_data)) {
383 1.20 christos auth_send_cnt = (size_t)cnt > sizeof(_auth_send_data)
384 1.1 cgd ? sizeof(_auth_send_data)
385 1.20 christos : (size_t)cnt;
386 1.20 christos memmove(_auth_send_data, data, auth_send_cnt);
387 1.1 cgd auth_send_data = _auth_send_data;
388 1.1 cgd } else {
389 1.1 cgd /*
390 1.1 cgd * This is probably a no-op, but we just make sure
391 1.1 cgd */
392 1.1 cgd auth_send_data = data;
393 1.1 cgd auth_send_cnt = cnt;
394 1.1 cgd }
395 1.1 cgd while ((auth_send_cnt -= 2) >= 0) {
396 1.1 cgd if (auth_debug_mode)
397 1.1 cgd printf(">>>%s: He supports %d\r\n",
398 1.1 cgd Name, *auth_send_data);
399 1.1 cgd if ((i_support & ~i_wont_support) & typemask(*auth_send_data)) {
400 1.1 cgd ap = findauthenticator(auth_send_data[0],
401 1.1 cgd auth_send_data[1]);
402 1.3 cgd if (ap && ap->send) {
403 1.1 cgd if (auth_debug_mode)
404 1.1 cgd printf(">>>%s: Trying %d %d\r\n",
405 1.1 cgd Name, auth_send_data[0],
406 1.1 cgd auth_send_data[1]);
407 1.1 cgd if ((*ap->send)(ap)) {
408 1.1 cgd /*
409 1.1 cgd * Okay, we found one we like
410 1.1 cgd * and did it.
411 1.1 cgd * we can go home now.
412 1.1 cgd */
413 1.1 cgd if (auth_debug_mode)
414 1.1 cgd printf(">>>%s: Using type %d\r\n",
415 1.1 cgd Name, *auth_send_data);
416 1.1 cgd auth_send_data += 2;
417 1.1 cgd return;
418 1.1 cgd }
419 1.1 cgd }
420 1.1 cgd /* else
421 1.1 cgd * just continue on and look for the
422 1.1 cgd * next one if we didn't do anything.
423 1.1 cgd */
424 1.1 cgd }
425 1.1 cgd auth_send_data += 2;
426 1.1 cgd }
427 1.10 thorpej telnet_net_write(str_none, sizeof(str_none));
428 1.1 cgd printsub('>', &str_none[2], sizeof(str_none) - 2);
429 1.1 cgd if (auth_debug_mode)
430 1.1 cgd printf(">>>%s: Sent failure message\r\n", Name);
431 1.1 cgd auth_finished(0, AUTH_REJECT);
432 1.3 cgd #ifdef KANNAN
433 1.3 cgd /*
434 1.3 cgd * We requested strong authentication, however no mechanisms worked.
435 1.3 cgd * Therefore, exit on client end.
436 1.3 cgd */
437 1.5 jtk printf("Unable to securely authenticate user ... exit\n");
438 1.3 cgd exit(0);
439 1.3 cgd #endif /* KANNAN */
440 1.1 cgd }
441 1.1 cgd
442 1.21 matt void
443 1.21 matt auth_send_retry(void)
444 1.1 cgd {
445 1.1 cgd /*
446 1.1 cgd * if auth_send_cnt <= 0 then auth_send will end up rejecting
447 1.1 cgd * the authentication and informing the other side of this.
448 1.1 cgd */
449 1.1 cgd auth_send(auth_send_data, auth_send_cnt);
450 1.1 cgd }
451 1.1 cgd
452 1.21 matt void
453 1.21 matt auth_is(unsigned char *data, int cnt)
454 1.1 cgd {
455 1.1 cgd Authenticator *ap;
456 1.1 cgd
457 1.1 cgd if (cnt < 2)
458 1.1 cgd return;
459 1.1 cgd
460 1.1 cgd if (data[0] == AUTHTYPE_NULL) {
461 1.1 cgd auth_finished(0, AUTH_REJECT);
462 1.1 cgd return;
463 1.1 cgd }
464 1.1 cgd
465 1.11 thorpej if ((ap = findauthenticator(data[0], data[1])) != NULL) {
466 1.1 cgd if (ap->is)
467 1.1 cgd (*ap->is)(ap, data+2, cnt-2);
468 1.1 cgd } else if (auth_debug_mode)
469 1.1 cgd printf(">>>%s: Invalid authentication in IS: %d\r\n",
470 1.1 cgd Name, *data);
471 1.1 cgd }
472 1.1 cgd
473 1.21 matt void
474 1.21 matt auth_reply(unsigned char *data, int cnt)
475 1.1 cgd {
476 1.1 cgd Authenticator *ap;
477 1.1 cgd
478 1.1 cgd if (cnt < 2)
479 1.1 cgd return;
480 1.1 cgd
481 1.11 thorpej if ((ap = findauthenticator(data[0], data[1])) != NULL) {
482 1.1 cgd if (ap->reply)
483 1.1 cgd (*ap->reply)(ap, data+2, cnt-2);
484 1.1 cgd } else if (auth_debug_mode)
485 1.1 cgd printf(">>>%s: Invalid authentication in SEND: %d\r\n",
486 1.1 cgd Name, *data);
487 1.1 cgd }
488 1.1 cgd
489 1.21 matt void
490 1.21 matt auth_name(unsigned char *data, int cnt)
491 1.1 cgd {
492 1.1 cgd unsigned char savename[256];
493 1.1 cgd
494 1.1 cgd if (cnt < 1) {
495 1.1 cgd if (auth_debug_mode)
496 1.1 cgd printf(">>>%s: Empty name in NAME\r\n", Name);
497 1.1 cgd return;
498 1.1 cgd }
499 1.20 christos if ((size_t)cnt > sizeof(savename) - 1) {
500 1.1 cgd if (auth_debug_mode)
501 1.8 thorpej printf(">>>%s: Name in NAME (%d) exceeds %ld length\r\n",
502 1.8 thorpej Name, cnt, (long)sizeof(savename)-1);
503 1.1 cgd return;
504 1.1 cgd }
505 1.5 jtk memmove((void *)savename, (void *)data, cnt);
506 1.1 cgd savename[cnt] = '\0'; /* Null terminate */
507 1.1 cgd if (auth_debug_mode)
508 1.1 cgd printf(">>>%s: Got NAME [%s]\r\n", Name, savename);
509 1.1 cgd auth_encrypt_user(savename);
510 1.1 cgd }
511 1.1 cgd
512 1.21 matt int
513 1.21 matt auth_sendname(unsigned char *cp, int len)
514 1.1 cgd {
515 1.1 cgd static unsigned char str_request[256+6]
516 1.1 cgd = { IAC, SB, TELOPT_AUTHENTICATION, TELQUAL_NAME, };
517 1.1 cgd register unsigned char *e = str_request + 4;
518 1.1 cgd register unsigned char *ee = &str_request[sizeof(str_request)-2];
519 1.1 cgd
520 1.1 cgd while (--len >= 0) {
521 1.1 cgd if ((*e++ = *cp++) == IAC)
522 1.1 cgd *e++ = IAC;
523 1.1 cgd if (e >= ee)
524 1.1 cgd return(0);
525 1.1 cgd }
526 1.1 cgd *e++ = IAC;
527 1.1 cgd *e++ = SE;
528 1.10 thorpej telnet_net_write(str_request, e - str_request);
529 1.1 cgd printsub('>', &str_request[2], e - &str_request[2]);
530 1.1 cgd return(1);
531 1.1 cgd }
532 1.1 cgd
533 1.21 matt void
534 1.21 matt auth_finished(Authenticator *ap, int result)
535 1.1 cgd {
536 1.1 cgd if (!(authenticated = ap))
537 1.1 cgd authenticated = &NoAuth;
538 1.1 cgd validuser = result;
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd /* ARGSUSED */
542 1.21 matt static void
543 1.21 matt auth_intr(int sig)
544 1.1 cgd {
545 1.1 cgd auth_finished(0, AUTH_REJECT);
546 1.1 cgd }
547 1.1 cgd
548 1.21 matt int
549 1.21 matt auth_wait(char *name, size_t l)
550 1.1 cgd {
551 1.1 cgd if (auth_debug_mode)
552 1.1 cgd printf(">>>%s: in auth_wait.\r\n", Name);
553 1.1 cgd
554 1.1 cgd if (Server && !authenticating)
555 1.1 cgd return(0);
556 1.1 cgd
557 1.1 cgd (void) signal(SIGALRM, auth_intr);
558 1.1 cgd alarm(30);
559 1.1 cgd while (!authenticated)
560 1.1 cgd if (telnet_spin())
561 1.1 cgd break;
562 1.1 cgd alarm(0);
563 1.1 cgd (void) signal(SIGALRM, SIG_DFL);
564 1.1 cgd
565 1.1 cgd /*
566 1.1 cgd * Now check to see if the user is valid or not
567 1.1 cgd */
568 1.1 cgd if (!authenticated || authenticated == &NoAuth)
569 1.1 cgd return(AUTH_REJECT);
570 1.1 cgd
571 1.1 cgd if (validuser == AUTH_VALID)
572 1.1 cgd validuser = AUTH_USER;
573 1.1 cgd
574 1.1 cgd if (authenticated->status)
575 1.1 cgd validuser = (*authenticated->status)(authenticated,
576 1.15 itojun name, l, validuser);
577 1.1 cgd return(validuser);
578 1.1 cgd }
579 1.1 cgd
580 1.21 matt void
581 1.21 matt auth_debug(int mode)
582 1.1 cgd {
583 1.1 cgd auth_debug_mode = mode;
584 1.1 cgd }
585 1.1 cgd
586 1.21 matt void
587 1.21 matt auth_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
588 1.1 cgd {
589 1.1 cgd Authenticator *ap;
590 1.1 cgd
591 1.1 cgd if ((ap = findauthenticator(data[1], data[2])) && ap->printsub)
592 1.1 cgd (*ap->printsub)(data, cnt, buf, buflen);
593 1.1 cgd else
594 1.1 cgd auth_gen_printsub(data, cnt, buf, buflen);
595 1.1 cgd }
596 1.1 cgd
597 1.21 matt void
598 1.21 matt auth_gen_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
599 1.1 cgd {
600 1.1 cgd register unsigned char *cp;
601 1.1 cgd unsigned char tbuf[16];
602 1.1 cgd
603 1.1 cgd cnt -= 3;
604 1.1 cgd data += 3;
605 1.1 cgd buf[buflen-1] = '\0';
606 1.1 cgd buf[buflen-2] = '*';
607 1.1 cgd buflen -= 2;
608 1.1 cgd for (; cnt > 0; cnt--, data++) {
609 1.14 itojun snprintf((char *)tbuf, sizeof(tbuf), " %d", *data);
610 1.1 cgd for (cp = tbuf; *cp && buflen > 0; --buflen)
611 1.1 cgd *buf++ = *cp++;
612 1.1 cgd if (buflen <= 0)
613 1.1 cgd return;
614 1.1 cgd }
615 1.1 cgd *buf = '\0';
616 1.1 cgd }
617 1.1 cgd #endif
618