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