xref: /titanic_51/usr/src/cmd/cmd-inet/usr.bin/telnet/authenc.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Miscellaneous routines needed by the telnet client for authentication
10  * and / or encryption.
11  */
12 
13 /*
14  * Copyright (c) 1991, 1993
15  *	The Regents of the University of California.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *	This product includes software developed by the University of
28  *	California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  */
45 
46 #ifndef lint
47 static char sccsid[] = "@(#)authenc.c	8.1 (Berkeley) 6/6/93";
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 #include <arpa/telnet.h>
52 
53 #include "general.h"
54 #include "ring.h"
55 #include "externs.h"
56 #include "defines.h"
57 #include "types.h"
58 
59 char *RemoteHostName = NULL;
60 char *UserNameRequested = NULL;
61 
62 #define	MAXNETDATA	16
63 
64 /*
65  * Get ready to do authentication and encryption by calling their
66  * init routines, and clearing the user name variable
67  */
68 /* ARGSUSED */
69 void
70 auth_encrypt_init(char *local, char *remote, char *name)
71 {
72 	RemoteHostName = remote;
73 
74 	auth_init(name);
75 
76 	encrypt_init(name);
77 
78 	if (UserNameRequested) {
79 		free(UserNameRequested);
80 		UserNameRequested = NULL;
81 	}
82 }
83 
84 /*
85  * Set the user name variable.  This is the user name used from now
86  * on for authentication and encryption
87  */
88 void
89 auth_encrypt_user(char *name)
90 {
91 	if (UserNameRequested)
92 		free(UserNameRequested);
93 	UserNameRequested = name ? strdup(name) : NULL;
94 }
95 
96 int
97 net_write(unsigned char *str, int len)
98 {
99 	if (NETROOM() > len) {
100 		ring_supply_data(&netoring, str, len);
101 		if (str[0] == IAC && str[1] == SE)
102 			printsub('>', &str[2], len - 2);
103 		return (len);
104 	}
105 	return (0);
106 }
107 
108 void
109 net_encrypt(void)
110 {
111 	if (encrypt_output)
112 		ring_encrypt(&netoring, encrypt_output);
113 	else
114 		ring_clearto(&netoring);
115 }
116 
117 /*
118  * Spin to wait for authentication to complete
119  * This allows for a timeout
120  */
121 void
122 telnet_spin(void)
123 {
124 	extern boolean_t scheduler_lockout_tty;
125 
126 	scheduler_lockout_tty = B_TRUE;
127 	(void) Scheduler(0);
128 	scheduler_lockout_tty = B_FALSE;
129 }
130 
131 
132 /*
133  * Used to print out unsigned chars as decimals for debugging options
134  */
135 void
136 printd(unsigned char *data, int cnt)
137 {
138 	cnt = (cnt < MAXNETDATA) ? cnt:MAXNETDATA;
139 	while (cnt-- > 0)
140 		(void) printf(" %02x", *data++);
141 }
142