summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/in.rexecd.c
blob: 3fecb1cace4c244b96f7b21647b604dcdaa33cda (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983-1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/filio.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <netdb.h>
#include <syslog.h>
#include <nss_dbdefs.h>
#include <security/pam_appl.h>
#include <deflt.h>

#ifdef SYSV
#include <shadow.h>
#endif /* SYSV */

#ifndef NCARGS
#define	NCARGS	5120
#endif /* NCARGS */

#ifdef SYSV
#define	rindex	strrchr
#define	killpg(a, b)	kill(-(a), (b))
#else
char  *sprintf();
#endif	/* SYSV */

#define	MAXFD(A, B) ((A) > (B) ? (A) : (B))
#define	_PATH_DEFAULT_LOGIN	"/etc/default/login"

static void error(char *fmt, ...);
static void doit(int f, struct sockaddr_storage *fromp);
static void getstr(char *buf, int cnt, char *err);

static int legalenvvar(char *s);

/* Function decls. for functions not in any header file.  (Grrrr.) */
extern int audit_rexecd_setup(void);
extern int audit_rexecd_success(char *, char *, char *);
extern int audit_rexecd_fail(char *, char *, char *, char *);
extern int audit_settid(int);	/* set termnal ID */

/* PAM conversation function */
static int rexec_conv(int, struct pam_message **,
		struct pam_response **, void *);

static pam_handle_t *pamh;	/* authentication handle */
static struct pam_conv conv = {
			rexec_conv,
			NULL
		};

/*
 * remote execute server:
 *	username\0
 *	password\0
 *	command\0
 *	data
 *
 * in.rexecd has been modified to run as the user invoking it. Hence there is no
 * need to limit any privileges.
 */
/*ARGSUSED*/
int
main(int argc, char **argv)
{
	struct sockaddr_storage from;
	socklen_t fromlen;

	openlog("rexec", LOG_PID | LOG_ODELAY, LOG_DAEMON);
	(void) audit_rexecd_setup();	/* BSM */
	fromlen = (socklen_t)sizeof (from);
	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
		(void) fprintf(stderr, "%s: ", argv[0]);
		perror("getpeername");
		exit(1);
	}

	if (audit_settid(0) != 0) {
		perror("settid");
		exit(1);
	}

	doit(0, &from);
	return (0);
}

static char	username[20] = "USER=";
static char	homedir[64] = "HOME=";
static char	shell[64] = "SHELL=";

static char	*envinit[] =
#ifdef SYSV
	{homedir, shell, (char *)0, username,
	(char *)0, (char *)0, (char *)0, (char *)0,
	(char *)0, (char *)0, (char *)0, (char *)0,
	(char *)0, (char *)0, (char *)0, (char *)0,
	(char *)0, (char *)0, (char *)0, (char *)0,
	(char *)0};
#define	ENVINIT_PATH	2	/* position of PATH in envinit[] */
#define	PAM_ENV_ELIM	16	/* max PAM environment variables */

/*
 *	See PSARC opinion 1992/025
 */
static char	userpath[] = "PATH=/usr/bin:";
static char	rootpath[] = "PATH=/usr/sbin:/usr/bin";
#else
	    {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", username, 0};
#endif /* SYSV */

static struct	sockaddr_storage asin;
static char pass[16];

static void
doit(int f, struct sockaddr_storage *fromp)
{
	char cmdbuf[NCARGS+1], *cp;
	char user[16];
	char hostname [MAXHOSTNAMELEN + 1];
	struct passwd *pwd, pw_data;
	char pwdbuf[NSS_BUFLEN_PASSWD];
	int s;
	ushort_t port;
	pid_t pid;
	int pv[2], cc;
	fd_set readfrom, ready;
	char buf[BUFSIZ], sig;
	int one = 1;
	int idx = 0, end_env = 0;
	char **pam_env;
	int status = PAM_AUTH_ERR;
	char abuf[INET6_ADDRSTRLEN];
	struct in_addr v4dst;
	socklen_t fromplen;
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	int pam_flags = 0;

	(void) signal(SIGINT, SIG_DFL);
	(void) signal(SIGQUIT, SIG_DFL);
	(void) signal(SIGTERM, SIG_DFL);
#ifdef DEBUG
	{
		int t = open("/dev/tty", 2);
		if (t >= 0) {
#ifdef SYSV
			(void) setsid();
#else
			(void) ioctl(t, TIOCNOTTY, (char *)0);
#endif	/* SYSV */
			(void) close(t);
		}
	}
#endif
	if (fromp->ss_family == AF_INET) {
		sin = (struct sockaddr_in *)fromp;
		fromplen = sizeof (struct sockaddr_in);
		asin.ss_family = AF_INET;  /* used for bind */
	} else if (fromp->ss_family == AF_INET6) {
		sin6 = (struct sockaddr_in6 *)fromp;
		fromplen = sizeof (struct sockaddr_in6);
		asin.ss_family = AF_INET6; /* used for bind */
	} else {
		syslog(LOG_ERR, "unknown address family %d\n",
		    fromp->ss_family);
		exit(1);
	}
	/*
	 * store common info. for audit record
	 */

	if (getnameinfo((const struct sockaddr *) fromp, fromplen, hostname,
	    sizeof (hostname), NULL, 0, 0) != 0) {
		if (fromp->ss_family == AF_INET6) {
			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
				struct in_addr ipv4_addr;

				IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr,
				    &ipv4_addr);
				inet_ntop(AF_INET, &ipv4_addr, abuf,
				    sizeof (abuf));
			} else {
				inet_ntop(AF_INET6, &sin6->sin6_addr,
				    abuf, sizeof (abuf));
			}
		} else if (fromp->ss_family == AF_INET) {
				inet_ntop(AF_INET, &sin->sin_addr,
				    abuf, sizeof (abuf));
			}
		(void) strncpy(hostname, abuf, sizeof (hostname));
	}
	(void) dup2(f, 0);
	(void) dup2(f, 1);
	(void) dup2(f, 2);
	(void) alarm(60);
	port = 0;
	for (;;) {
		char c;
		if (read(f, &c, 1) != 1)
			exit(1);
		if (c == 0)
			break;
		port = port * 10 + c - '0';
	}
	(void) alarm(0);
	if (port != 0) {
		s = socket(fromp->ss_family, SOCK_STREAM, 0);
		if (s < 0)
			exit(1);
		if (bind(s, (struct sockaddr *)&asin, fromplen) < 0)
			exit(1);
		(void) alarm(60);
		if (fromp->ss_family == AF_INET) {
			sin->sin_port = htons((ushort_t)port);
		} else if (fromp->ss_family == AF_INET6) {
			sin6->sin6_port = htons((ushort_t)port);
		}
		if (connect(s, (struct sockaddr *)fromp, fromplen) < 0)
			exit(1);
		(void) alarm(0);
	}
	getstr(user, sizeof (user), "username");
	getstr(pass, sizeof (pass), "password");
	getstr(cmdbuf, sizeof (cmdbuf), "command");

	pwd = getpwnam_r(user, &pw_data, pwdbuf, sizeof (pwdbuf));
	if (pwd == NULL) {
		(void) audit_rexecd_fail("Login incorrect", hostname, user,
		    cmdbuf);	    /* BSM */
		error("Login incorrect.\n");
		exit(1);
	}

	if (defopen(_PATH_DEFAULT_LOGIN) == 0) {
		int flags;
		char *p;
		flags = defcntl(DC_GETFLAGS, 0);
		TURNOFF(flags, DC_CASE);
		(void) defcntl(DC_SETFLAGS, flags);
		if ((p = defread("PASSREQ=")) != NULL &&
		    strcasecmp(p, "YES") == 0) {
			pam_flags |= PAM_DISALLOW_NULL_AUTHTOK;
		}
		defopen(NULL);
	}

	if (pam_start("rexec", user, &conv, &pamh) != PAM_SUCCESS) {
		exit(1);
	}
	if (pam_set_item(pamh, PAM_RHOST, hostname) != PAM_SUCCESS) {
		exit(1);
	}

	if ((status = pam_authenticate(pamh, pam_flags)) != PAM_SUCCESS) {
		switch (status) {
		case PAM_USER_UNKNOWN:
			(void) audit_rexecd_fail("Login incorrect", hostname,
			    user, cmdbuf);		/* BSM */
			error("Login incorrect.\n");
			break;
		default:
			(void) audit_rexecd_fail("Password incorrect", hostname,
			    user, cmdbuf);	/* BSM */
			error("Password incorrect.\n");
		}
		pam_end(pamh, status);
		exit(1);
	}
	if ((status = pam_acct_mgmt(pamh, pam_flags)) != PAM_SUCCESS) {
		(void) audit_rexecd_fail("Account or Password Expired",
		    hostname, user, cmdbuf);
		switch (status) {
			case PAM_NEW_AUTHTOK_REQD:
				error("Password Expired.\n");
				break;
			case PAM_PERM_DENIED:
				error("Account Expired.\n");
				break;
			case PAM_AUTHTOK_EXPIRED:
				error("Password Expired.\n");
				break;
			default:
				error("Login incorrect.\n");
				break;
		}
		pam_end(pamh, status);
		exit(1);
	}

	(void) write(2, "\0", 1);

	if (setgid((gid_t)pwd->pw_gid) < 0) {
		(void) audit_rexecd_fail("Can't setgid", hostname,
		    user, cmdbuf);	/* BSM */
		error("setgid");
		pam_end(pamh, PAM_ABORT);
		exit(1);
	}
	(void) initgroups(pwd->pw_name, pwd->pw_gid);

	if ((status = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
		(void) audit_rexecd_fail("Unable to establish credentials",
		    hostname, user, cmdbuf);	/* BSM */
		error("Unable to establish credentials.\n");
		pam_end(pamh, PAM_SUCCESS);
	}

	(void) audit_rexecd_success(hostname, user, cmdbuf);	/* BSM */

	if (setuid((uid_t)pwd->pw_uid) < 0) {
		(void) audit_rexecd_fail("Can't setuid", hostname,
		    user, cmdbuf);	/* BSM */
		error("setuid");
		pam_end(pamh, PAM_ABORT);
		exit(1);
	}


	if (port) {
		(void) pipe(pv);
		pid = fork();
		if (pid == (pid_t)-1)  {
			error("Try again.\n");
			pam_end(pamh, PAM_ABORT);
			exit(1);
		}
		if (pid) {
			/*
			 * since the daemon is running as the user no need
			 * to prune privileges.
			 */
			(void) close(0); (void) close(1); (void) close(2);
			(void) close(f); (void) close(pv[1]);
			FD_ZERO(&readfrom);
			FD_SET(s, &readfrom);
			FD_SET(pv[0], &readfrom);
			(void) ioctl(pv[0], FIONBIO, (char *)&one);
			/* should set s nbio! */
			do {
				ready = readfrom;
				if (select(MAXFD(s, pv[0])+1, &ready, NULL,
				    NULL, NULL) < 0) {
					perror("select:");
					exit(1);
				}
				if (FD_ISSET(s, &ready)) {
					if (read(s, &sig, 1) <= 0)
						FD_CLR(s, &readfrom);
					else
						(void) killpg(pid, sig);
				}
				if (FD_ISSET(pv[0], &ready)) {
					cc = read(pv[0], buf, sizeof (buf));
					if (cc <= 0) {
						(void) shutdown(s, 1+1);
						FD_CLR(pv[0], &readfrom);
					} else
						(void) write(s, buf, cc);
				}
			} while (FD_ISSET(s, &readfrom) ||
			    FD_ISSET(pv[0], &readfrom));
			exit(0);
		}
		/* setpgrp(0, getpid()); */
		(void) setsid();	/* Should be the same as above. */
		(void) close(s); (void)close(pv[0]);
		(void) dup2(pv[1], 2);
	}

	if (*pwd->pw_shell == '\0')
		pwd->pw_shell = "/bin/sh";
	if (f > 2)
		(void) close(f);
	/* Change directory only after becoming the appropriate user. */
	if (chdir(pwd->pw_dir) < 0) {
		error("No remote directory.\n");
		pam_end(pamh, PAM_ABORT);
		exit(1);
	}
#ifdef	SYSV
	if (pwd->pw_uid)
		envinit[ENVINIT_PATH] = userpath;
	else
		envinit[ENVINIT_PATH] = rootpath;
#endif	/* SYSV */
	(void) strncat(homedir, pwd->pw_dir, sizeof (homedir) - 6);
	(void) strncat(shell, pwd->pw_shell, sizeof (shell) - 7);
	(void) strncat(username, pwd->pw_name, sizeof (username) - 6);

	/*
	 * add PAM environment variables set by modules
	 * -- only allowed 16 (PAM_ENV_ELIM)
	 * -- check to see if the environment variable is legal
	 */
	for (end_env = 0; envinit[end_env] != 0; end_env++)
		;
	if ((pam_env = pam_getenvlist(pamh)) != 0) {
		while (pam_env[idx] != 0) {
			if (idx < PAM_ENV_ELIM &&
			    legalenvvar(pam_env[idx])) {
				envinit[end_env + idx] = pam_env[idx];
			}
			idx++;
		}
	}

	pam_end(pamh, PAM_SUCCESS);

	cp = rindex(pwd->pw_shell, '/');
	if (cp)
		cp++;
	else
		cp = pwd->pw_shell;
	(void) execle(pwd->pw_shell, cp, "-c", cmdbuf, (char *)0, envinit);
	perror(pwd->pw_shell);
	exit(1);
}

static void
getstr(char *buf, int cnt, char *err)
{
	char c;

	do {
		if (read(0, &c, 1) != 1)
			exit(1);
		*buf++ = c;
		if (--cnt == 0) {
			error("%s too long\n", err);
			exit(1);
		}
	} while (c != 0);
}

static void
error(char *fmt, ...)
{
	va_list ap;
	char buf[BUFSIZ];

	buf[0] = 1;
	va_start(ap, fmt);
	(void) vsprintf(buf+1, fmt, ap);
	va_end(ap);
	(void) write(2, buf, strlen(buf));
}

static char *illegal[] = {
	"SHELL=",
	"HOME=",
	"LOGNAME=",
#ifndef NO_MAIL
	"MAIL=",
#endif
	"CDPATH=",
	"IFS=",
	"PATH=",
	"USER=",
	0
};

/*
 * legalenvvar - can PAM insert this environmental variable?
 */

static int
legalenvvar(char *s)
{
	register char **p;

	for (p = illegal; *p; p++)
		if (strncmp(s, *p, strlen(*p)) == 0)
			return (0);

	if (s[0] == 'L' && s[1] == 'D' && s[2] == '_')
		return (0);

	return (1);
}

/*
 * rexec_conv -  This is the conv (conversation) function called from
 *	a PAM authentication module to print error messages
 *	or garner information from the user.
 */

/* ARGSUSED3 */
static int
rexec_conv(int num_msg, struct pam_message **msg,
    struct pam_response **response, void *appdata_ptr)
{
	struct pam_message	*m;
	struct pam_response	*r;
	int			i;

	if (num_msg <= 0)
		return (PAM_CONV_ERR);

	*response = calloc(num_msg, sizeof (struct pam_response));
	if (*response == NULL)
		return (PAM_BUF_ERR);

	m = *msg;
	r = *response;

	if (m->msg_style == PAM_PROMPT_ECHO_OFF) {
		if (pass[0] != '\0') {
			r->resp = strdup(pass);
			if (r->resp == NULL) {
				/* free responses */
				r = *response;
				for (i = 0; i < num_msg; i++, r++) {
					if (r->resp)
						free(r->resp);
				}
				free(*response);
				*response = NULL;
				return (PAM_BUF_ERR);
			}
		}
	}

	return (PAM_SUCCESS);
}