Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / usr.bin / 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  * @(#)main.c   8.3 (Berkeley) 5/30/95
34  * $FreeBSD: src/crypto/telnet/telnet/main.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $
35  */
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "ring.h"
44 #include "externs.h"
45 #include "defines.h"
46
47 #ifdef  AUTHENTICATION
48 #include <libtelnet/auth.h>
49 #endif
50 #ifdef  ENCRYPTION
51 #include <libtelnet/encrypt.h>
52 #endif
53
54 /* These values need to be the same as defined in libtelnet/kerberos5.c */
55 /* Either define them in both places, or put in some common header file. */
56 #define OPTS_FORWARD_CREDS      0x00000002
57 #define OPTS_FORWARDABLE_CREDS  0x00000001
58
59 #if 0
60 #define FORWARD
61 #endif
62
63 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
64 char *ipsec_policy_in = NULL;
65 char *ipsec_policy_out = NULL;
66 #endif
67
68 int family = AF_UNSPEC;
69
70 /*
71  * Initialize variables.
72  */
73 void
74 tninit(void)
75 {
76     init_terminal();
77
78     init_network();
79
80     init_telnet();
81
82     init_sys();
83 }
84
85 static void
86 usage(void)
87 {
88         fprintf(stderr, "Usage: %s %s%s%s%s\n",
89             prompt,
90 #ifdef  AUTHENTICATION
91             "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-c] [-d]",
92             "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
93 #else
94             "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-c] [-d]",
95             "\n\t[-e char] [-l user] [-n tracefile] ",
96 #endif
97             "[-r] [-s src_addr] [-u] ",
98 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
99             "[-P policy] "
100 #endif
101 #ifdef  ENCRYPTION
102             "[-y] [host-name [port]]"
103 #else   /* ENCRYPTION */
104             "[host-name [port]]"
105 #endif  /* ENCRYPTION */
106         );
107         exit(1);
108 }
109
110 /*
111  * main.  Parse arguments, invoke the protocol or command parser.
112  */
113
114 int
115 main(int argc, char *argv[])
116 {
117         int ch;
118         char *user;
119         char *src_addr = NULL;
120 #ifdef  FORWARD
121         extern int forward_flags;
122 #endif  /* FORWARD */
123
124         tninit();               /* Clear out things */
125
126         TerminalSaveState();
127
128         if ((prompt = strrchr(argv[0], '/')))
129                 ++prompt;
130         else
131                 prompt = argv[0];
132
133         user = NULL;
134
135         rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
136 #ifdef AUTHENTICATION
137         autologin = 0;
138 #else
139         autologin = -1;
140 #endif
141
142 #ifdef  ENCRYPTION
143         encrypt_auto(1);
144         decrypt_auto(1);
145 #endif
146
147 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
148 #define IPSECOPT        "P:"
149 #else
150 #define IPSECOPT
151 #endif
152         while ((ch = getopt(argc, argv,
153                             "468EKLNS:X:acde:fFk:l:n:rs:t:uxy" IPSECOPT)) != -1)
154 #undef IPSECOPT
155         {
156                 switch(ch) {
157                 case '4':
158                         family = AF_INET;
159                         break;
160 #ifdef INET6
161                 case '6':
162                         family = AF_INET6;
163                         break;
164 #endif
165                 case '8':
166                         eight = 3;      /* binary output and input */
167                         break;
168                 case 'E':
169                         rlogin = escape = _POSIX_VDISABLE;
170                         break;
171                 case 'K':
172 #ifdef  AUTHENTICATION
173                         /* It's the default now, so ignore */
174 #endif
175                         break;
176                 case 'L':
177                         eight |= 2;     /* binary output only */
178                         break;
179                 case 'N':
180                         doaddrlookup = 0;
181                         break;
182                 case 'S':
183                     {
184 #ifdef  HAS_GETTOS
185                         extern int tos;
186
187                         if ((tos = parsetos(optarg, "tcp")) < 0)
188                                 fprintf(stderr, "%s%s%s%s\n",
189                                         prompt, ": Bad TOS argument '",
190                                         optarg,
191                                         "; will try to use default TOS");
192 #else
193                         fprintf(stderr,
194                            "%s: Warning: -S ignored, no parsetos() support.\n",
195                                                                 prompt);
196 #endif
197                     }
198                         break;
199                 case 'X':
200 #ifdef  AUTHENTICATION
201                         auth_disable_name(optarg);
202 #endif
203                         break;
204                 case 'a':
205                         autologin = 1;
206                         break;
207                 case 'c':
208                         skiprc = 1;
209                         break;
210                 case 'd':
211                         debug = 1;
212                         break;
213                 case 'e':
214                         set_escape_char(optarg);
215                         break;
216                 case 'f':
217 #ifdef  AUTHENTICATION
218 #if defined(KRB5) && defined(FORWARD)
219                         if (forward_flags & OPTS_FORWARD_CREDS) {
220                             fprintf(stderr,
221                                     "%s: Only one of -f and -F allowed.\n",
222                                     prompt);
223                             usage();
224                         }
225                         forward_flags |= OPTS_FORWARD_CREDS;
226 #else
227                         fprintf(stderr,
228                          "%s: Warning: -f ignored, no Kerberos V5 support.\n",
229                                 prompt);
230 #endif
231 #else
232                         fprintf(stderr,
233                          "%s: Warning: -f ignored, no Kerberos V5 support.\n",
234                                 prompt);
235 #endif
236                         break;
237                 case 'F':
238 #ifdef  AUTHENTICATION
239 #if defined(KRB5) && defined(FORWARD)
240                         if (forward_flags & OPTS_FORWARD_CREDS) {
241                             fprintf(stderr,
242                                     "%s: Only one of -f and -F allowed.\n",
243                                     prompt);
244                             usage();
245                         }
246                         forward_flags |= OPTS_FORWARD_CREDS;
247                         forward_flags |= OPTS_FORWARDABLE_CREDS;
248 #else
249                         fprintf(stderr,
250                          "%s: Warning: -F ignored, no Kerberos V5 support.\n",
251                                 prompt);
252 #endif
253 #else
254                         fprintf(stderr,
255                          "%s: Warning: -F ignored, no Kerberos V5 support.\n",
256                                 prompt);
257 #endif
258                         break;
259                 case 'k':
260 #ifdef  AUTHENTICATION
261 #if defined(KRB4)
262                     {
263                         extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
264                         dest_realm = dst_realm_buf;
265                         (void)strncpy(dest_realm, optarg, dst_realm_sz);
266                     }
267 #else
268                         fprintf(stderr,
269                            "%s: Warning: -k ignored, no Kerberos V4 support.\n",
270                                                                 prompt);
271 #endif
272 #else
273                         fprintf(stderr,
274                            "%s: Warning: -k ignored, no Kerberos V4 support.\n",
275                                                                 prompt);
276 #endif
277                         break;
278                 case 'l':
279 #ifdef  AUTHENTICATION
280                         /* This is the default now, so ignore it */
281 #else
282                         autologin = 1;
283 #endif
284                         user = optarg;
285                         break;
286                 case 'n':
287                                 SetNetTrace(optarg);
288                         break;
289                 case 'r':
290                         rlogin = '~';
291                         break;
292                 case 's':
293                         src_addr = optarg;
294                         break;
295                 case 'u':
296                         family = AF_UNIX;
297                         break;
298                 case 'x':
299 #ifndef ENCRYPTION
300                         fprintf(stderr,
301                             "%s: Warning: -x ignored, no ENCRYPT support.\n",
302                                                                 prompt);
303 #endif  /* ENCRYPTION */
304                         break;
305                 case 'y':
306 #ifdef  ENCRYPTION
307                         encrypt_auto(0);
308                         decrypt_auto(0);
309 #else
310                         fprintf(stderr,
311                             "%s: Warning: -y ignored, no ENCRYPT support.\n",
312                                                                 prompt);
313 #endif  /* ENCRYPTION */
314                         break;
315 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
316                 case 'P':
317                         if (!strncmp("in", optarg, 2))
318                                 ipsec_policy_in = strdup(optarg);
319                         else if (!strncmp("out", optarg, 3))
320                                 ipsec_policy_out = strdup(optarg);
321                         else
322                                 usage();
323                         break;
324 #endif
325                 case '?':
326                 default:
327                         usage();
328                         /* NOTREACHED */
329                 }
330         }
331         if (autologin == -1)
332                 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
333
334         argc -= optind;
335         argv += optind;
336
337         if (argc) {
338                 char *args[9], **argp = args;
339
340                 if (argc > 2)
341                         usage();
342                 *argp++ = prompt;
343                 if (user) {
344                         *argp++ = strdup("-l");
345                         *argp++ = user;
346                 }
347                 if (src_addr) {
348                         *argp++ = strdup("-s");
349                         *argp++ = src_addr;
350                 }
351                 *argp++ = argv[0];              /* host */
352                 if (argc > 1)
353                         *argp++ = argv[1];      /* port */
354                 *argp = NULL;
355
356                 if (setjmp(toplevel) != 0)
357                         Exit(0);
358                 if (tn(argp - args, args) == 1)
359                         return (0);
360                 else
361                         return (1);
362         }
363         (void)setjmp(toplevel);
364         for (;;) {
365                         command(1, 0, 0);
366         }
367         return 0;
368 }