]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/heimdal/appl/telnet/telnet/main.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / heimdal / appl / telnet / telnet / main.c
1 /*
2  * Copyright (c) 1988, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 static char *copyright[] = {
35     "@(#) Copyright (c) 1988, 1990, 1993\n"
36     "\tThe Regents of the University of California.  All rights reserved.\n",
37     (char*)copyright
38 };
39
40 #include "telnet_locl.h"
41 RCSID("$Id: main.c 21731 2007-07-30 20:01:26Z lha $");
42
43 #if KRB5
44 #define FORWARD
45 #endif
46
47 /*
48  * Initialize variables.
49  */
50 void
51 tninit(void)
52 {
53     init_terminal();
54
55     init_network();
56
57     init_telnet();
58
59     init_sys();
60 }
61
62 static void
63 usage(int exit_code)
64 {
65   fprintf(stderr, "Usage: %s %s%s%s%s\n", prompt,
66 #ifdef  AUTHENTICATION
67           "[-8] [-E] [-K] [-L] [-G] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
68           "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] ",
69 #else
70           "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
71           "\n\t[-n tracefile]",
72 #endif
73           "[-r] ",
74 #ifdef  ENCRYPTION
75           "[-x] [host-name [port]]"
76 #else
77           "[host-name [port]]"
78 #endif
79     );
80   exit(exit_code);
81 }
82
83 /*
84  * main.  Parse arguments, invoke the protocol or command parser.
85  */
86
87
88 #ifdef  FORWARD
89 int forward_option = 0; /* forward flags set from command line */
90 #endif  /* FORWARD */
91 void
92 set_forward_options(void)
93 {
94 #ifdef FORWARD
95         switch(forward_option) {
96         case 'f':
97                 kerberos5_set_forward(1);
98                 kerberos5_set_forwardable(0);
99                 break;
100         case 'F':
101                 kerberos5_set_forward(1);
102                 kerberos5_set_forwardable(1);
103                 break;
104         case 'G':
105                 kerberos5_set_forward(0);
106                 kerberos5_set_forwardable(0);
107                 break;
108         default:
109                 break;
110         }
111 #endif
112 }
113
114 #ifdef KRB5
115 #define Authenticator asn1_Authenticator
116 #include <krb5.h>
117 static void
118 krb5_init(void)
119 {
120     krb5_context context;
121     krb5_error_code ret;
122     krb5_boolean ret_val;
123
124     ret = krb5_init_context(&context);
125     if (ret)
126         return;
127
128 #if defined(AUTHENTICATION) && defined(FORWARD)
129     krb5_appdefault_boolean(context, NULL,
130                             NULL, "forward",
131                             0, &ret_val);
132     if (ret_val)
133             kerberos5_set_forward(1);
134     krb5_appdefault_boolean(context, NULL,
135                             NULL, "forwardable",
136                             0, &ret_val);
137     if (ret_val)
138             kerberos5_set_forwardable(1);
139 #endif
140 #ifdef  ENCRYPTION
141     krb5_appdefault_boolean(context, NULL, 
142                             NULL, "encrypt",
143                             0, &ret_val);
144     if (ret_val) {
145           encrypt_auto(1);
146           decrypt_auto(1); 
147           wantencryption = 1;
148           EncryptVerbose(1);
149         }
150 #endif
151
152     krb5_free_context(context);
153 }
154 #endif
155
156 #if defined(AUTHENTICATION) && defined(KRB4)
157 extern char *dest_realm, dst_realm_buf[];
158 extern int dst_realm_sz;
159 #endif
160
161 int
162 main(int argc, char **argv)
163 {
164         int ch;
165         char *user;
166
167         setprogname(argv[0]);
168
169 #ifdef KRB5
170         krb5_init();
171 #endif
172         
173         tninit();               /* Clear out things */
174
175         TerminalSaveState();
176
177         if ((prompt = strrchr(argv[0], '/')))
178                 ++prompt;
179         else
180                 prompt = argv[0];
181
182         user = NULL;
183
184         rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
185
186         /* 
187          * if AUTHENTICATION and ENCRYPTION is set autologin will be
188          * se to true after the getopt switch; unless the -K option is
189          * passed 
190          */
191         autologin = -1;
192
193         if (argc == 2 && strcmp(argv[1], "--version") == 0) {
194             print_version(NULL);
195             exit(0);
196         }
197         if (argc == 2 && strcmp(argv[1], "--help") == 0)
198             usage(0);
199
200
201         while((ch = getopt(argc, argv,
202                            "78DEKLS:X:abcde:fFk:l:n:rxG")) != -1) {
203                 switch(ch) {
204                 case '8':
205                         eight = 3;      /* binary output and input */
206                         break;
207                 case '7':
208                         eight = 0;
209                         break;
210                 case 'b':
211                     binary = 3;
212                     break;
213                 case 'D': {
214                     /* sometimes we don't want a mangled display */
215                     char *p;
216                     if((p = getenv("DISPLAY")))
217                         env_define((unsigned char*)"DISPLAY", (unsigned char*)p);
218                     break;
219                 }
220                 case 'E':
221                         rlogin = escape = _POSIX_VDISABLE;
222                         break;
223                 case 'K':
224 #ifdef  AUTHENTICATION
225                         autologin = 0;
226 #endif
227                         break;
228                 case 'L':
229                         eight |= 2;     /* binary output only */
230                         break;
231                 case 'S':
232                     {
233 #ifdef  HAVE_PARSETOS
234                         extern int tos;
235
236                         if ((tos = parsetos(optarg, "tcp")) < 0)
237                                 fprintf(stderr, "%s%s%s%s\n",
238                                         prompt, ": Bad TOS argument '",
239                                         optarg,
240                                         "; will try to use default TOS");
241 #else
242                         fprintf(stderr,
243                            "%s: Warning: -S ignored, no parsetos() support.\n",
244                                                                 prompt);
245 #endif
246                     }
247                         break;
248                 case 'X':
249 #ifdef  AUTHENTICATION
250                         auth_disable_name(optarg);
251 #endif
252                         break;
253                 case 'a':
254                         autologin = 1;
255                         break;
256                 case 'c':
257                         skiprc = 1;
258                         break;
259                 case 'd':
260                         debug = 1;
261                         break;
262                 case 'e':
263                         set_escape_char(optarg);
264                         break;
265                 case 'f':
266                 case 'F':
267                 case 'G':
268 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
269                         if (forward_option) {
270                             fprintf(stderr,
271                                     "%s: Only one of -f, -F and -G allowed.\n",
272                                     prompt);
273                             usage(1);
274                         }
275                         forward_option = ch;
276 #else
277                         fprintf(stderr,
278                          "%s: Warning: -%c ignored, no Kerberos V5 support.\n",
279                                 prompt, ch);
280 #endif
281                         break;
282                 case 'k':
283 #if defined(AUTHENTICATION) && defined(KRB4)
284                     {
285                         dest_realm = dst_realm_buf;
286                         strlcpy(dest_realm, optarg, dst_realm_sz);
287                     }
288 #else
289                         fprintf(stderr,
290                            "%s: Warning: -k ignored, no Kerberos V4 support.\n",
291                                                                 prompt);
292 #endif
293                         break;
294                 case 'l':
295                   if(autologin == 0){
296                     fprintf(stderr, "%s: Warning: -K ignored\n", prompt);
297                     autologin = -1;
298                   }
299                         user = optarg;
300                         break;
301                 case 'n':
302                                 SetNetTrace(optarg);
303                         break;
304                 case 'r':
305                         rlogin = '~';
306                         break;
307                 case 'x':
308 #ifdef  ENCRYPTION
309                         encrypt_auto(1);
310                         decrypt_auto(1);
311                         wantencryption = 1;
312                         EncryptVerbose(1);
313 #else
314                         fprintf(stderr,
315                             "%s: Warning: -x ignored, no ENCRYPT support.\n",
316                                                                 prompt);
317 #endif
318                         break;
319
320                 case '?':
321                 default:
322                         usage(1);
323                         /* NOTREACHED */
324                 }
325         }
326
327         if (autologin == -1) {          /* esc@magic.fi; force  */
328 #if defined(AUTHENTICATION)
329                 autologin = 1;
330 #endif
331 #if defined(ENCRYPTION)
332                 encrypt_auto(1);
333                 decrypt_auto(1);
334                 wantencryption = -1;
335 #endif
336         }
337
338         if (autologin == -1)
339                 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
340
341         argc -= optind;
342         argv += optind;
343
344         if (argc) {
345                 char *args[7], **argp = args;
346
347                 if (argc > 2)
348                         usage(1);
349                 *argp++ = prompt;
350                 if (user) {
351                         *argp++ = "-l";
352                         *argp++ = user;
353                 }
354                 *argp++ = argv[0];              /* host */
355                 if (argc > 1)
356                         *argp++ = argv[1];      /* port */
357                 *argp = 0;
358
359                 if (setjmp(toplevel) != 0)
360                         Exit(0);
361                 if (tn(argp - args, args) == 1)
362                         return (0);
363                 else
364                         return (1);
365         }
366         setjmp(toplevel);
367         for (;;) {
368                         command(1, 0, 0);
369         }
370 }