summaryrefslogtreecommitdiff
path: root/distrib/utils/ssh/ssh.c
blob: 3bdd00da282fb174ec51000f64b3d5f0e2557360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*	$NetBSD: ssh.c,v 1.1.1.1 1995/10/08 23:08:46 gwr Exp $	*/

/*
 * Copyright (c) 1995 Gordon W. Ross
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 4. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Gordon W. Ross
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Small Shell - Nothing fancy.  Just runs programs.
 * The RAMDISK root uses this to save space.
 */

#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/param.h>
#include <sys/wait.h>

/* XXX - SunOS hacks... */
#ifndef	WCOREDUMP
#define	WCOREDUMP(x) ((x) & 0200)
#endif

#ifndef	__P
#ifdef	__STDC__
#define __P(x) x
#else	/* STDC */
#define	__P(x) ()
#endif	/* STDC */
#endif	/* __P */

extern char *optarg;
extern int optind, opterr;

#define MAXLINE 256
#define MAXARGS 32

#define	MAXPATH 256
char cur_path[MAXPATH] = "PATH=/bin:/usr/bin";

char rc_name[] = ".sshrc";
char *prompt = "ssh: ";

int eflag;	/* exit on cmd failure */
int iflag;	/* interactive mode (catch interrupts) */
int sflag;	/* read from stdin (ignore file arg) */
int xflag;	/* execution trace */

/* Command file: name, line number, arg count, arg vector */
char *cf_name;
int cf_line;
int cf_argc;
char **cf_argv;

int def_omode = 0666;
int run_bg_pid;

jmp_buf next_cmd;

void catchsig __P((int sig));
void child_newfd __P((int setfd, char *file, int otype));
int find_in_path __P((char *cmd, char *filebuf));
void print_termsig __P((FILE *fp, int cstat));
int runfile __P((FILE *fp));


main(argc, argv)
	int argc;
	char **argv;
{
	struct sigaction sa;
	FILE *cfp;		/* command file ptr */
	int c, sig;
	int error = 0;

	while ((c = getopt(argc, argv, "eisx")) != -1) {
		switch (c) {
		case 'e':
			eflag++;
			break;
		case 'i':
			eflag++;
			break;
		case 's':
			sflag++;
			break;
		case 'x':
			xflag++;
			break;
		case '?':
			error++;
			break;
		}
	}
	if (error) {
		fprintf(stderr, "usage:  ssh [-eisx] [cmd_file [...]]\n");
		exit(1);
	}
	cf_argc = argc - optind;
	cf_argv = &argv[optind];

	/* If this is a login shell, run the rc file. */
	if (argv[0] && argv[0][0] == '-') {
		cf_line = 0;
		cf_name = rc_name;
		if ((cfp = fopen(cf_name, "r")) != NULL) {
			error = runfile(cfp);
			fclose(cfp);
		}
	}

	/* If no file names, read commands from stdin. */
	if (cf_argc == 0)
		sflag++;
	/* If stdin is a tty, be interactive. */
	if (sflag && isatty(fileno(stdin)))
		iflag++;

	/* Maybe run a command file... */
	if (!sflag && cf_argc) {
		cf_line = 0;
		cf_name = cf_argv[0];
		cfp = fopen(cf_name, "r");
		if (cfp == NULL) {
			perror(cf_name);
			exit(1);
		}
		error = runfile(cfp);
		fclose(cfp);
		exit(error);
	}

	/* Read commands from stdin. */
	cf_line = 0;
	cf_name = "(stdin)";
	if (iflag) {
		eflag = 0;	/* don't kill shell on error. */
		sig = setjmp(next_cmd);
		if (sig == 0) {
			/* Initialization... */
			sa.sa_handler = catchsig;
			sa.sa_flags = 0;
			sigemptyset(&sa.sa_mask);
			sigaction(SIGINT,  &sa, NULL);
			sigaction(SIGQUIT, &sa, NULL);
			sigaction(SIGTERM, &sa, NULL);
		} else {
			/* Got here via longjmp. */
			fprintf(stderr, " signal %d\n", sig);
			sigemptyset(&sa.sa_mask);
			sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
		}
	}
	error = runfile(stdin);
	exit (error);
}

void
catchsig(sig)
	int sig;
{
	longjmp(next_cmd, sig);
}

/*
 * Run command from the passed stdio file pointer.
 * Returns exit status.
 */
int
runfile(cfp)
	FILE *cfp;
{
	char ibuf[MAXLINE];
	char *argv[MAXARGS];
	char *p;
	int i, argc, exitcode, cpid, cstat;

	/* The command loop. */
	exitcode = 0;
	for (;;) {
		if (iflag) {
			fprintf(stderr, prompt);
			fflush(stderr);
		}

		if ((fgets(ibuf, sizeof(ibuf), cfp)) == NULL)
			break;
		cf_line++;

		argc = 0;
		p = ibuf;

		while (argc < MAXARGS-1) {
			/* skip blanks or tabs */
			while ((*p == ' ') || (*p == '\t')) {
			next_token:
				*p++ = '\0';
			}
			/* end of line? */
			if ((*p == '\n') || (*p == '#')) {
				*p = '\0';
				break;	/* to eol */
			}
			if (*p == '\0')
				break;
			/* save start of token */
			argv[argc++] = p;
			/* find end of token */
			while (*p) {
				if ((*p == '\n') || (*p == '#')) {
					*p = '\0';
					goto eol;
				}
				if ((*p == ' ') || (*p == '\t'))
					goto next_token;
				p++;
			}
		}
	eol:

		if (argc > 0) {
			if (xflag) {
				fprintf(stderr, "x");
				for (i = 0; i < argc; i++) {
					fprintf(stderr, " %s", argv[i]);
				}
				fprintf(stderr, "\n");
			}
			argv[argc] = NULL;
			exitcode = cmd_eval(argc, argv);
		}

		/* Collect children. */
		while ((cpid = waitpid(0, &cstat, WNOHANG)) > 0) {
			if (iflag) {
				fprintf(stderr, "[%d] ", cpid);
				if (WTERMSIG(cstat)) {
					print_termsig(stderr, cstat);
				} else {
					fprintf(stderr, "Exited, status %d\n",
							WEXITSTATUS(cstat));
				}
			}
		}

		if (exitcode && eflag)
			break;
	}
	/* return status of last command */
	return (exitcode);
}


/****************************************************************
 *  Table of buildin commands
 *  for cmd_eval() to search...
 ****************************************************************/

struct cmd {
	char *name;
	int (*func)();
	char *help;
};
struct cmd cmd_table[];

/*
 * Evaluate a command named as argv[0]
 * with arguments argv[1],argv[2]...
 * Returns exit status.
 */
int
cmd_eval(argc, argv)
	int argc;
	char **argv;
{
	struct cmd *cp;

	/*
	 * Do linear search for a builtin command.
	 * Performance does not matter here.
	 */
	for (cp = cmd_table; cp->name; cp++) {
		if (!strcmp(cp->name, argv[0])) {
			/* Pass only args to builtin. */
			--argc; argv++;
			return (cp->func(argc, argv));
		}
	}

	/*
	 * If no matching builtin, let "run ..."
	 * have a chance to try an external.
	 */
	return (cmd_run(argc, argv));
}

/*****************************************************************
 *  Here are the actual commands.  For these,
 *  the command name has been skipped, so
 *  argv[0] is the first arg (if any args).
 *  All return an exit status.
 ****************************************************************/

char help_cd[] = "cd [dir]";

int
cmd_cd(argc, argv)
	int argc;
	char **argv;
{
	char *dir;
	int err;

	if (argc > 0)
		dir = argv[0];
	else {
		dir = getenv("HOME");
		if (dir == NULL)
			dir = "/";
	}
	if (chdir(dir)) {
		perror(dir);
		return (1);
	}
	return(0);
}

char help_exit[] = "exit [n]";

int
cmd_exit(argc, argv)
	int argc;
	char **argv;
{
	int val = 0;

	if (argc > 0)
		val = atoi(argv[0]);
	exit(val);
}

char help_help[] = "help [command]";

int
cmd_help(argc, argv)
	int argc;
	char **argv;
{
	struct cmd *cp;

	if (argc > 0) {
		for (cp = cmd_table; cp->name; cp++) {
			if (!strcmp(cp->name, argv[0])) {
				printf("usage:  %s\n", cp->help);
				return (0);
			}
		}
		printf("%s: no such command\n", argv[0]);
	}

	printf("Builtin commands: ");
	for (cp = cmd_table; cp->name; cp++) {
		printf(" %s", cp->name);
	}
	printf("\nFor specific usage:  help [command]\n");
	return (0);
}

char help_path[] = "path [dir1:dir2:...]";

int
cmd_path(argc, argv)
	int argc;
	char **argv;
{
	int i;

	if (argc <= 0) {
		printf("%s\n", cur_path);
		return(0);
	}

	strncpy(cur_path+5, argv[0], MAXPATH-6);
	putenv(cur_path);

	return (0);
}

/*****************************************************************
 *  The "run" command is the big one.
 *  Does fork/exec/wait, redirection...
 *  Returns exit status of child
 *  (or zero for a background job)
 ****************************************************************/

char help_run[] = "\
run [-bg] [-i ifile] [-o ofile] [-e efile] program [args...]\n\
or simply:  program [args...]";

int
cmd_run(argc, argv)
	int argc;
	char **argv;
{
	struct sigaction sa;
	int pid, err, cstat, fd;
	char file[MAXPATHLEN];
	int background;
	char *opt, *ifile, *ofile, *efile;
	extern char **environ;

	/*
	 * Parse options:
	 * -b  : background
	 * -i  : input file
	 * -o  : output file
	 * -e  : error file
	 */
	background = 0;
	ifile = ofile = efile = NULL;
	while ((argc > 0) && (argv[0][0] == '-')) {
		opt = argv[0];
		--argc; argv++;
		switch (opt[1]) {
		case 'b':
			background++;
			break;
		case 'i':
			ifile = argv[0];
			goto shift;
		case 'o':
			ofile = argv[0];
			goto shift;
		case 'e':
			efile = argv[0];
			goto shift;
		default:
			fprintf(stderr, "run %s: bad option\n", opt);
			return (1);
		shift:
			--argc; argv++;
		}
	}

	if (argc <= 0) {
		fprintf(stderr, "%s:%d run: missing command\n",
				cf_name, cf_line);
		return (1);
	}

	/* Commands containing '/' get no path search. */
	if (strchr(argv[0], '/')) {
		strncpy(file, argv[0], sizeof(file)-1);
		if (access(file, X_OK)) {
			perror(file);
			return (1);
		}
	} else {
		if (find_in_path(argv[0], file)) {
			fprintf(stderr, "%s: command not found\n", argv[0]);
			return (1);
		}
	}

	pid = fork();
	if (pid == 0) {
		/* child runs this */
		/* handle redirection options... */
		if (ifile)
			child_newfd(0, ifile, O_RDONLY);
		if (ofile)
			child_newfd(1, ofile, O_WRONLY|O_CREAT);
		if (efile)
			child_newfd(2, efile, O_WRONLY|O_CREAT);
		if (background) {
			/* Ignore SIGINT, SIGQUIT */
			sa.sa_handler = SIG_IGN;
			sa.sa_flags = 0;
			sigemptyset(&sa.sa_mask);
			sigaction(SIGINT,  &sa, NULL);
			sigaction(SIGQUIT, &sa, NULL);
		}
		err = execve(file, argv, environ);
		perror(argv[0]);
		return (1);
	}
	/* parent */
	/* Handle background option... */
	if (background) {
		fprintf(stderr, "[%d]\n", pid);
		run_bg_pid = pid;
		return (0);
	}
	if (waitpid(pid, &cstat, 0) < 0) {
		perror("waitpid");
		return (1);
	}
	if (WTERMSIG(cstat)) {
		print_termsig(stderr, cstat);
	}
	return (WEXITSTATUS(cstat));
}

/*****************************************************************
 *  table of builtin commands
 ****************************************************************/
struct cmd cmd_table[] = {
	{ "cd",   cmd_cd,   help_cd },
	{ "exit", cmd_exit, help_exit },
	{ "help", cmd_help, help_help },
	{ "path", cmd_path, help_path },
	{ "run",  cmd_run,  help_run },
	{ 0 },
};

/*****************************************************************
 *  helper functions for the "run" command
 ****************************************************************/

int
find_in_path(cmd, filebuf)
	char *cmd;
	char *filebuf;
{
	char *dirp, *endp, *bufp;	/* dir, end */

	dirp = cur_path + 5;
	while (*dirp) {
		endp = dirp;
		bufp = filebuf;
		while (*endp && (*endp != ':'))
			*bufp++ = *endp++;
		*bufp++ = '/';
		strcpy(bufp, cmd);
		if (access(filebuf, X_OK) == 0)
			return (0);
		if (*endp == ':')
			endp++;
		dirp = endp;	/* next dir */
	}
	return (-1);
}

/*
 * Set the file descriptor SETFD to FILE,
 * which was opened with OTYPE and MODE.
 */
void
child_newfd(setfd, file, otype)
	int setfd;	/* what to set (i.e. 0,1,2) */
	char *file;
	int otype;	/* O_RDONLY, etc. */
{
	int newfd;

	close(setfd);
	if ((newfd = open(file, otype, def_omode)) < 0) {
		perror(file);
		exit(1);
	}
	if (newfd != setfd) {
		dup2(newfd, setfd);
		close(newfd);
	}
}

void
print_termsig(fp, cstat)
	FILE *fp;
	int cstat;
{
	fprintf(fp, "Terminated, signal %d",
			WTERMSIG(cstat));
	if (WCOREDUMP(cstat))
		fprintf(fp, " (core dumped)");
	fprintf(fp, "\n");
}