rename roken base64, fixes #107
[heimdal.git] / appl / ftp / ftp / main.c
blob5876f51dc4dd1c09d8a3f399281e86ecf0ca3c41
1 /*
2 * Copyright (c) 1985, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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.
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.
35 * FTP User Program -- Command Interface.
38 #include "ftp_locl.h"
39 #include <getarg.h>
41 RCSID("$Id$");
43 static int help_flag;
44 static int version_flag;
45 static int debug_flag;
47 struct getargs getargs[] = {
48 { NULL, 'd', arg_flag, &debug_flag,
49 "debug", NULL },
50 { NULL, 'g', arg_negative_flag, &doglob,
51 "disables globbing", NULL},
52 { NULL, 'i', arg_negative_flag, &interactive,
53 "Turn off interactive prompting", NULL},
54 { NULL, 'l', arg_negative_flag, &lineedit,
55 "Turn off line editing", NULL},
56 { NULL, 'n', arg_negative_flag, &autologin,
57 "Turn off auto-login", NULL},
58 { NULL, 'p', arg_flag, &passivemode,
59 "passive mode", NULL},
60 { NULL, 't', arg_counter, &trace,
61 "Packet tracing", NULL},
62 #ifdef KRB5
63 { "gss-bindings", 0, arg_negative_flag, &ftp_do_gss_bindings,
64 "Don't use GSS-API bindings", NULL},
65 { "gss-delegate", 0, arg_negative_flag, &ftp_do_gss_delegate,
66 "Disable delegation of GSS-API credentials", NULL},
67 #endif
68 { NULL, 'v', arg_counter, &verbose,
69 "verbosity", NULL},
70 { NULL, 'K', arg_negative_flag, &use_kerberos,
71 "Disable kerberos authentication", NULL},
72 { "encrypt", 'x', arg_flag, &doencrypt,
73 "Encrypt command and data channel if possible" },
74 { "version", 0, arg_flag, &version_flag },
75 { "help", 'h', arg_flag, &help_flag },
78 static int num_args = sizeof(getargs) / sizeof(getargs[0]);
80 static void
81 usage(int ecode)
83 arg_printusage(getargs, num_args, NULL, "[host [port]]");
84 exit(ecode);
87 int
88 main(int argc, char **argv)
90 int top;
91 struct passwd *pw = NULL;
92 char homedir[MaxPathLen];
93 struct servent *sp;
94 int optind = 0;
96 setprogname(argv[0]);
98 sp = getservbyname("ftp", "tcp");
99 if (sp == 0)
100 errx(1, "ftp/tcp: unknown service");
101 doglob = 1;
102 interactive = 1;
103 autologin = 1;
104 lineedit = 1;
105 passivemode = 0; /* passive mode not active */
106 use_kerberos = 1;
107 #ifdef KRB5
108 ftp_do_gss_bindings = 1;
109 #endif
111 if(getarg(getargs, num_args, argc, argv, &optind))
112 usage(1);
113 if(help_flag)
114 usage(0);
115 if(version_flag) {
116 print_version(NULL);
117 exit(0);
120 if (debug_flag) {
121 options |= SO_DEBUG;
122 debug++;
125 argc -= optind;
126 argv += optind;
128 fromatty = isatty(fileno(stdin));
129 if (fromatty)
130 verbose++;
131 cpend = 0; /* no pending replies */
132 proxy = 0; /* proxy not active */
133 crflag = 1; /* strip c.r. on ascii gets */
134 sendport = -1; /* not using ports */
136 * Set up the home directory in case we're globbing.
138 pw = k_getpwuid(getuid());
139 if (pw != NULL) {
140 strlcpy(homedir, pw->pw_dir, sizeof(homedir));
141 home = homedir;
143 if (argc > 0) {
144 char *xargv[5];
146 if (setjmp(toplevel))
147 exit(0);
148 signal(SIGINT, intr);
149 signal(SIGPIPE, lostpeer);
150 xargv[0] = (char*)getprogname();
151 xargv[1] = argv[0];
152 xargv[2] = argv[1];
153 xargv[3] = argv[2];
154 xargv[4] = NULL;
155 setpeer(argc+1, xargv);
157 if(setjmp(toplevel) == 0)
158 top = 1;
159 else
160 top = 0;
161 if (top) {
162 signal(SIGINT, intr);
163 signal(SIGPIPE, lostpeer);
165 for (;;) {
166 cmdscanner(top);
167 top = 1;
171 void
172 intr(int sig)
175 longjmp(toplevel, 1);
178 #ifndef SHUT_RDWR
179 #define SHUT_RDWR 2
180 #endif
182 RETSIGTYPE
183 lostpeer(int sig)
186 if (connected) {
187 if (cout != NULL) {
188 shutdown(fileno(cout), SHUT_RDWR);
189 fclose(cout);
190 cout = NULL;
192 if (data >= 0) {
193 shutdown(data, SHUT_RDWR);
194 close(data);
195 data = -1;
197 connected = 0;
199 pswitch(1);
200 if (connected) {
201 if (cout != NULL) {
202 shutdown(fileno(cout), SHUT_RDWR);
203 fclose(cout);
204 cout = NULL;
206 connected = 0;
208 proxflag = 0;
209 pswitch(0);
210 sec_end();
211 SIGRETURN(0);
215 char *
216 tail(filename)
217 char *filename;
219 char *s;
221 while (*filename) {
222 s = strrchr(filename, '/');
223 if (s == NULL)
224 break;
225 if (s[1])
226 return (s + 1);
227 *s = '\0';
229 return (filename);
233 static char *
234 simple_readline(char *prompt)
236 char buf[BUFSIZ];
237 printf ("%s", prompt);
238 fflush (stdout);
239 if(fgets(buf, sizeof(buf), stdin) == NULL)
240 return NULL;
241 if (buf[strlen(buf) - 1] == '\n')
242 buf[strlen(buf) - 1] = '\0';
243 return strdup(buf);
246 #ifndef HAVE_READLINE
248 static char *
249 readline(char *prompt)
251 return simple_readline (prompt);
254 static void
255 add_history(char *p)
259 #else
261 /* These should not really be here */
263 char *readline(char *);
264 void add_history(char *);
266 #endif
269 * Command parser.
271 void
272 cmdscanner(int top)
274 struct cmd *c;
275 int l;
277 if (!top)
278 putchar('\n');
279 for (;;) {
280 if (fromatty) {
281 char *p;
282 if (lineedit)
283 p = readline("ftp> ");
284 else
285 p = simple_readline("ftp> ");
286 if(p == NULL) {
287 printf("\n");
288 quit(0, 0);
290 strlcpy(line, p, sizeof(line));
291 if (lineedit)
292 add_history(p);
293 free(p);
294 } else{
295 if (fgets(line, sizeof line, stdin) == NULL)
296 quit(0, 0);
298 /* XXX will break on long lines */
299 l = strlen(line);
300 if (l == 0)
301 break;
302 if (line[--l] == '\n') {
303 if (l == 0)
304 break;
305 line[l] = '\0';
306 } else if (l == sizeof(line) - 2) {
307 printf("sorry, input line too long\n");
308 while ((l = getchar()) != '\n' && l != EOF)
309 /* void */;
310 break;
311 } /* else it was a line without a newline */
312 makeargv();
313 if (margc == 0) {
314 continue;
316 c = getcmd(margv[0]);
317 if (c == (struct cmd *)-1) {
318 printf("?Ambiguous command\n");
319 continue;
321 if (c == 0) {
322 printf("?Invalid command\n");
323 continue;
325 if (c->c_conn && !connected) {
326 printf("Not connected.\n");
327 continue;
329 (*c->c_handler)(margc, margv);
330 if (bell && c->c_bell)
331 putchar('\007');
332 if (c->c_handler != help)
333 break;
335 signal(SIGINT, intr);
336 signal(SIGPIPE, lostpeer);
339 struct cmd *
340 getcmd(char *name)
342 char *p, *q;
343 struct cmd *c, *found;
344 int nmatches, longest;
346 longest = 0;
347 nmatches = 0;
348 found = 0;
349 for (c = cmdtab; (p = c->c_name); c++) {
350 for (q = name; *q == *p++; q++)
351 if (*q == 0) /* exact match? */
352 return (c);
353 if (!*q) { /* the name was a prefix */
354 if (q - name > longest) {
355 longest = q - name;
356 nmatches = 1;
357 found = c;
358 } else if (q - name == longest)
359 nmatches++;
362 if (nmatches > 1)
363 return ((struct cmd *)-1);
364 return (found);
368 * Slice a string up into argc/argv.
371 int slrflag;
373 void
374 makeargv(void)
376 char **argp;
378 argp = margv;
379 stringbase = line; /* scan from first of buffer */
380 argbase = argbuf; /* store from first of buffer */
381 slrflag = 0;
382 for (margc = 0; ; margc++) {
383 /* Expand array if necessary */
384 if (margc == margvlen) {
385 int i;
387 margv = (margvlen == 0)
388 ? (char **)malloc(20 * sizeof(char *))
389 : (char **)realloc(margv,
390 (margvlen + 20)*sizeof(char *));
391 if (margv == NULL)
392 errx(1, "cannot realloc argv array");
393 for(i = margvlen; i < margvlen + 20; ++i)
394 margv[i] = NULL;
395 margvlen += 20;
396 argp = margv + margc;
399 if ((*argp++ = slurpstring()) == NULL)
400 break;
406 * Parse string into argbuf;
407 * implemented with FSM to
408 * handle quoting and strings
410 char *
411 slurpstring(void)
413 int got_one = 0;
414 char *sb = stringbase;
415 char *ap = argbase;
416 char *tmp = argbase; /* will return this if token found */
418 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
419 switch (slrflag) { /* and $ as token for macro invoke */
420 case 0:
421 slrflag++;
422 stringbase++;
423 return ((*sb == '!') ? "!" : "$");
424 /* NOTREACHED */
425 case 1:
426 slrflag++;
427 altarg = stringbase;
428 break;
429 default:
430 break;
435 switch (*sb) {
437 case '\0':
438 goto OUT;
440 case ' ':
441 case '\t':
442 sb++; goto S0;
444 default:
445 switch (slrflag) {
446 case 0:
447 slrflag++;
448 break;
449 case 1:
450 slrflag++;
451 altarg = sb;
452 break;
453 default:
454 break;
456 goto S1;
460 switch (*sb) {
462 case ' ':
463 case '\t':
464 case '\0':
465 goto OUT; /* end of token */
467 case '\\':
468 sb++; goto S2; /* slurp next character */
470 case '"':
471 sb++; goto S3; /* slurp quoted string */
473 default:
474 *ap++ = *sb++; /* add character to token */
475 got_one = 1;
476 goto S1;
480 switch (*sb) {
482 case '\0':
483 goto OUT;
485 default:
486 *ap++ = *sb++;
487 got_one = 1;
488 goto S1;
492 switch (*sb) {
494 case '\0':
495 goto OUT;
497 case '"':
498 sb++; goto S1;
500 default:
501 *ap++ = *sb++;
502 got_one = 1;
503 goto S3;
506 OUT:
507 if (got_one)
508 *ap++ = '\0';
509 argbase = ap; /* update storage pointer */
510 stringbase = sb; /* update scan pointer */
511 if (got_one) {
512 return (tmp);
514 switch (slrflag) {
515 case 0:
516 slrflag++;
517 break;
518 case 1:
519 slrflag++;
520 altarg = (char *) 0;
521 break;
522 default:
523 break;
525 return NULL;
528 #define HELPINDENT ((int) sizeof ("directory"))
531 * Help command.
532 * Call each command handler with argc == 0 and argv[0] == name.
534 void
535 help(int argc, char **argv)
537 struct cmd *c;
539 if (argc == 1) {
540 int i, j, w, k;
541 int columns, width = 0, lines;
543 printf("Commands may be abbreviated. Commands are:\n\n");
544 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
545 int len = strlen(c->c_name);
547 if (len > width)
548 width = len;
550 width = (width + 8) &~ 7;
551 columns = 80 / width;
552 if (columns == 0)
553 columns = 1;
554 lines = (NCMDS + columns - 1) / columns;
555 for (i = 0; i < lines; i++) {
556 for (j = 0; j < columns; j++) {
557 c = cmdtab + j * lines + i;
558 if ((!proxy || c->c_proxy)) {
559 printf("%s", c->c_name);
560 } else {
561 for (k=0; k < strlen(c->c_name); k++) {
562 putchar(' ');
565 if (c + lines >= &cmdtab[NCMDS]) {
566 printf("\n");
567 break;
569 w = strlen(c->c_name);
570 while (w < width) {
571 w = (w + 8) &~ 7;
572 putchar('\t');
576 return;
578 while (--argc > 0) {
579 char *arg;
580 arg = *++argv;
581 c = getcmd(arg);
582 if (c == (struct cmd *)-1)
583 printf("?Ambiguous help command %s\n", arg);
584 else if (c == (struct cmd *)0)
585 printf("?Invalid help command %s\n", arg);
586 else
587 printf("%-*s\t%s\n", HELPINDENT,
588 c->c_name, c->c_help);