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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#define BSD_COMP
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sac.h> /* for SC_WILDC */
#include <utmpx.h>
#include <rpc/rpc.h>
#include <sys/file.h>
#include <sys/filio.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
* # include <sys/label.h>
* # include <sys/audit.h>
*
*
*
* # include <pwdadj.h>
*/
#include <sys/ttold.h>
#include <stropts.h>
#include <sys/stream.h>
#include "rex.h"
#include <security/pam_appl.h>
pam_handle_t *pamh;
#define NTTYDISC 2 /* New ttydiscipline: stolen from ttold.h */
/*
* unix_login - hairy junk to simulate logins for Unix
*/
int Master, Slave; /* sides of the pty */
int Slave_is_closed_on_master_side;
static char *slavename;
extern char *ptsname();
int InputSocket, /* Network sockets */
OutputSocket;
int Helper1, /* pids of the helpers */
Helper2;
char UserName[256]; /* saves the user name for loging */
char HostName[256]; /* saves the host name for loging */
static int TtySlot; /* slot number in Utmpx */
/*
* pseudo-xprts used to add pty fds to svc_pollfd[]. This allows the
* polling for all i/o in one poll().
*/
SVCXPRT uxprt[2];
#define INPUTSOCKET 0 /* InputSocket xprt */
#define MASTER 1 /* Master xprt */
extern int child; /* pid of the executed process */
extern int ChildDied; /* flag */
extern int HasHelper; /* flag */
extern void setproctitle(char *user, char *host);
extern int Debug;
extern void audit_rexd_fail(char *, char *, char *, uid_t, gid_t,
char *, char **);
#define bzero(s, n) memset((s), 0, (n))
#define bcopy(a, b, c) memcpy((b), (a), (c))
static void LogoutUser(void);
/*
* Check for user being able to run on this machine.
* returns 0 if OK, TRUE if problem, error message in "error"
* copies name of shell and home directory if user is valid.
*/
int
ValidUser(host, uid, gid, error, shell, dir, rst)
char *host; /* passed in */
uid_t uid;
gid_t gid;
char *error; /* filled in on return */
char *shell; /* filled in on return */
char *dir; /* filled in on return */
struct rex_start *rst; /* passed in */
{
struct passwd *pw, *getpwuid();
int v;
pw = getpwuid(uid);
if (pw == NULL || pw->pw_name == NULL)
{
errprintf(error, "rexd: User id %d not valid\n", uid);
audit_rexd_fail("user id is not valid",
host,
NULL,
uid,
gid,
NULL,
rst->rst_cmd); /* BSM */
return (1);
}
strncpy(UserName, pw->pw_name, sizeof (UserName) - 1);
strncpy(HostName, host, sizeof (HostName) - 1);
strcpy(shell, pw->pw_shell);
strcpy(dir, pw->pw_dir);
setproctitle(pw->pw_name, host);
if (pam_start("rexd", pw->pw_name, NULL, &pamh) != PAM_SUCCESS ||
pam_set_item(pamh, PAM_RHOST, host) != PAM_SUCCESS) {
audit_rexd_fail("user id is not valid",
host,
pw->pw_name,
uid,
gid,
shell,
rst->rst_cmd); /* BSM */
errprintf(error, "rexd: User id %d not valid\n", uid);
if (pamh) {
pam_end(pamh, PAM_ABORT);
pamh = NULL;
}
return (1);
}
if ((v = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
switch (v) {
case PAM_NEW_AUTHTOK_REQD:
errprintf(error,
"rexd: User id %d Password Expired\n", uid);
break;
case PAM_PERM_DENIED:
errprintf(error,
"rexd: User id %d Account Expired\n", uid);
break;
case PAM_AUTHTOK_EXPIRED:
errprintf(error,
"rexd: User id %d Password Expired\n", uid);
break;
default:
errprintf(error,
"rexd: User id %d not valid\n", uid);
break;
}
pam_end(pamh, PAM_ABORT);
pamh = NULL;
audit_rexd_fail("user account expired",
host,
pw->pw_name,
uid,
gid,
shell,
rst->rst_cmd); /* BSM */
return (1);
}
return (0);
}
/*
* Add an audit record with argv that was pre-set, plus the given string
*/
/*
* Allocate a pseudo-terminal
* sets the global variables Master and Slave.
* returns 1 on error, 0 if OK
*/
int
AllocatePty(socket0, socket1)
int socket0, socket1;
{
int on = 1;
sigset(SIGHUP, SIG_IGN);
sigset(SIGTTOU, SIG_IGN);
sigset(SIGTTIN, SIG_IGN);
if ((Master = open("/dev/ptmx", O_RDWR)) == -1) {
if (Debug)
printf("open-ptmx-failure\n");
perror("AloocatePtyMaster fails");
return (1); /* error could not open /dev/ptmx */
}
if (Debug)
printf("open-ptmx success Master =%d\n", Master);
if (Debug)
printf("Before grantpt...Master=%d\n", Master);
if (grantpt(Master) == -1) {
perror("could not grant slave pty");
exit(1);
}
if (unlockpt(Master) == -1) {
perror("could not unlock slave pty");
exit(1);
}
if ((slavename = ptsname(Master)) == NULL) {
perror("could not enable slave pty");
exit(1);
}
if ((Slave = open(slavename, O_RDWR)) == -1) {
perror("could not open slave pty");
exit(1);
}
if (ioctl(Slave, I_PUSH, "ptem") == -1) {
perror("ioctl I_PUSH ptem");
exit(1);
}
if (ioctl(Slave, I_PUSH, "ldterm") == -1) {
perror("ioctl I_PUSH ldterm");
exit(1);
}
if (ioctl(Slave, I_PUSH, "ttcompat") == -1) {
perror("ioctl I_PUSH ttcompat");
exit(1);
}
Slave_is_closed_on_master_side = FALSE;
setsid(); /* get rid of controlling terminal */
/* LoginUser(); */
InputSocket = socket0;
OutputSocket = socket1;
ioctl(Master, FIONBIO, &on);
uxprt[INPUTSOCKET].xp_fd = InputSocket;
xprt_register(&uxprt[INPUTSOCKET]);
uxprt[MASTER].xp_fd = Master;
xprt_register(&uxprt[MASTER]);
return (0);
}
void
OpenPtySlave()
{
close(Slave);
Slave = open(slavename, O_RDWR);
if (Slave < 0) {
perror(slavename);
exit(1);
}
}
/*
* Special processing for interactive operation.
* Given pointers to three standard file descriptors,
* which get set to point to the pty.
*/
void
DoHelper(pfd0, pfd1, pfd2)
int *pfd0, *pfd1, *pfd2;
{
int pgrp;
sigset(SIGINT, SIG_IGN);
close(Master);
close(InputSocket);
close(OutputSocket);
*pfd0 = Slave;
*pfd1 = Slave;
*pfd2 = Slave;
}
/*
* destroy the helpers when the executing process dies
*/
void
KillHelper(int grp)
{
if (Debug)
printf("Enter KillHelper\n");
close(Master);
xprt_unregister(&uxprt[MASTER]);
close(InputSocket);
xprt_unregister(&uxprt[INPUTSOCKET]);
close(OutputSocket);
LogoutUser();
if (grp)
kill((-grp), SIGKILL);
}
/*
* edit the Unix traditional data files that tell who is logged
* into "the system"
*/
unsigned char utid[] = {'o', 'n', SC_WILDC, SC_WILDC};
void
LoginUser(void)
{
char *user;
char *rhost;
/* the next 4 variables are needed for utmpx mgmt */
int tmplen;
struct utmpx *u = NULL;
struct utmpx set_utmp;
char *ttyntail;
/* We're pretty drastic here, exiting if an error is detected */
if (pam_set_item(pamh, PAM_TTY, slavename) != PAM_SUCCESS ||
pam_get_item(pamh, PAM_USER, (void **) &user) != PAM_SUCCESS ||
pam_get_item(pamh, PAM_RHOST, (void **) &rhost) != PAM_SUCCESS ||
pam_open_session(pamh, 0) != PAM_SUCCESS) {
/*
* XXX should print something but for now we exit
*/
exit(1);
}
(void) memset((void *)&set_utmp, 0, sizeof (set_utmp));
(void) time(&set_utmp.ut_tv.tv_sec);
set_utmp.ut_pid = getpid();
if (rhost != NULL && rhost[0] != '\0') {
(void) strcpy(set_utmp.ut_host, rhost);
tmplen = strlen(rhost) + 1;
if (tmplen < sizeof (set_utmp.ut_host))
set_utmp.ut_syslen = tmplen;
else
set_utmp.ut_syslen = sizeof (set_utmp.ut_host);
} else {
(void) memset(set_utmp.ut_host, 0, sizeof (set_utmp.ut_host));
set_utmp.ut_syslen = 0;
}
(void) strcpy(set_utmp.ut_user, user);
/*
* Copy in the name of the tty minus the "/dev/" if a /dev/ is
* in the path name.
*/
ttyntail = slavename;
if (strstr(ttyntail, "/dev/") != 0)
ttyntail = ttyntail + strlen("/dev/");
(void) strcpy(set_utmp.ut_line, ttyntail);
set_utmp.ut_type = USER_PROCESS;
if (utid != NULL)
(void) memcpy(set_utmp.ut_id, utid, sizeof (set_utmp.ut_id));
/*
* Go through each entry one by one, looking only at INIT,
* LOGIN or USER Processes. Use the entry found if flags == 0
* and the line name matches, or if the process ID matches if
* the UPDATE_ENTRY flag is set. The UPDATE_ENTRY flag is mainly
* for login which normally only wants to update an entry if
* the pid fields matches.
*/
if (u == (struct utmpx *)NULL) {
(void) makeutx(&set_utmp);
} else
updwtmpx(WTMPX_FILE, &set_utmp);
}
/*
* edit the Unix traditional data files that tell who is logged
* into "the system".
*/
static void
LogoutUser(void)
{
struct utmpx *up;
struct utmpx ut;
int pid;
char user[sizeof (ut.ut_user) + 1];
char ttyn[sizeof (ut.ut_line) + 1];
char rhost[sizeof (ut.ut_host) + 1];
sighold(SIGCHLD); /* no disruption during cleanup */
if (pamh) {
pam_end(pamh, PAM_SUCCESS);
pamh = NULL;
}
/* BEGIN RESET UTMP */
pid = child;
setutxent();
while (up = getutxent()) {
if (up->ut_pid == pid) {
if (up->ut_type == DEAD_PROCESS) {
/*
* Cleaned up elsewhere.
*/
break;
}
strncpy(user, up->ut_user, sizeof (up->ut_user));
user[sizeof (up->ut_user)] = '\0';
strncpy(ttyn, up->ut_line, sizeof (up->ut_line));
ttyn[sizeof (up->ut_line)] = '\0';
strncpy(rhost, up->ut_host, sizeof (up->ut_host));
rhost[sizeof (up->ut_host)] = '\0';
if ((pam_start("rexd", user, NULL, &pamh))
== PAM_SUCCESS) {
(void) pam_set_item(pamh, PAM_TTY, ttyn);
(void) pam_set_item(pamh, PAM_RHOST, rhost);
(void) pam_close_session(pamh, 0);
(void) pam_end(pamh, PAM_SUCCESS);
pamh = NULL;
}
up->ut_type = DEAD_PROCESS;
up->ut_exit.e_termination = WTERMSIG(0);
up->ut_exit.e_exit = WEXITSTATUS(0);
(void) time(&up->ut_tv.tv_sec);
if (modutx(up) == NULL) {
/*
* Since modutx failed we'll
* write out the new entry
* ourselves.
*/
(void) pututxline(up);
updwtmpx("wtmpx", up);
}
break;
}
}
endutxent();
/* END RESET UTMP */
sigrelse(SIGCHLD);
}
/*
* set the pty modes to the given values
*/
void
SetPtyMode(mode)
struct rex_ttymode *mode;
{
struct sgttyb svr4_sgttyb_var;
int ldisc = NTTYDISC;
if (Debug)
printf("Enter SetPtyMode\n");
if (Debug)
printf("SetPtyMode:opened slave\n");
ioctl(Slave, TIOCSETD, &ldisc);
if (Debug)
printf("SetPtyMode:Slave TIOCSETD done\n");
/*
* Copy from over-the-net(bsd) to SVR4 format
*/
svr4_sgttyb_var.sg_ispeed = mode->basic.sg_ispeed;
svr4_sgttyb_var.sg_ospeed = mode->basic.sg_ospeed;
svr4_sgttyb_var.sg_erase = mode->basic.sg_erase;
svr4_sgttyb_var.sg_kill = mode->basic.sg_kill;
svr4_sgttyb_var.sg_flags = (int)mode->basic.sg_flags;
/*
* Clear any possible sign extension caused by (int)
* typecast
*/
svr4_sgttyb_var.sg_flags &= 0xFFFF;
ioctl(Slave, TIOCSETN, &svr4_sgttyb_var);
if (Debug)
printf("SetPtyMode:Slave TIOCSETN done\n");
ioctl(Slave, TIOCSETC, &mode->more);
if (Debug)
printf("SetPtyMode:Slave TIOCSETC done\n");
ioctl(Slave, TIOCSLTC, &mode->yetmore);
if (Debug)
printf("SetPtyMode:Slave TIOCSLTC done\n");
ioctl(Slave, TIOCLSET, &mode->andmore);
if (Debug)
printf("SetPtyMode:Slave TIOCSET done\n");
/* Opened in AllocPty for parent, still open in child */
if (Slave_is_closed_on_master_side == FALSE) {
close(Slave);
Slave_is_closed_on_master_side = TRUE;
}
}
/*
* set the pty window size to the given value
*/
void
SetPtySize(struct rex_ttysize *sizep)
{
struct winsize newsize;
/* if size has changed, this ioctl changes it */
/* *and* sends SIGWINCH to process group */
newsize.ws_row = (unsigned short) sizep->ts_lines;
newsize.ws_col = (unsigned short) sizep->ts_cols;
(void) ioctl(Master, TIOCSWINSZ, &newsize);
if (Slave_is_closed_on_master_side == FALSE) {
close(Slave);
Slave_is_closed_on_master_side = TRUE;
}
}
/*
* send the given signal to the group controlling the terminal
*/
void
SendSignal(int sig)
{
pid_t pgrp;
pgrp = getpgid(child);
if (pgrp != (pid_t)-1)
(void) kill((-pgrp), sig);
}
/*
* called when the main select loop detects that we might want to
* read something.
*/
void
HelperRead(pollfd_t *fdp, int nfds, int *pollretval)
{
char buf[128];
int retval;
extern int errno;
int mask;
int master = -1;
int inputsocket = -1;
/*
* fdp pollset may be compressed. Search for Master and
* InputSocket fds.
*/
int i;
for (i = 0; i < nfds; i++) {
if (fdp[i].fd == Master && fdp[i].revents != 0)
master = i;
if (fdp[i].fd == InputSocket && fdp[i].revents != 0)
inputsocket = i;
}
/* mask = sigsetmask (sigmask (SIGCHLD)); */
mask = sighold(SIGCHLD);
retval = 0;
if (master != -1) {
if (!(fdp[master].revents & (POLLERR | POLLHUP | POLLNVAL))) {
retval = read(Master, buf, sizeof (buf));
if (retval > 0) {
(void) write(OutputSocket, buf, retval);
} else {
if (errno != EINTR && errno != EIO &&
errno != EWOULDBLOCK)
perror("pty read");
/* 1 => further sends disallowed */
shutdown(OutputSocket, 1);
xprt_unregister(&uxprt[MASTER]);
}
}
/* clear this event for svc_getreq_poll */
fdp[master].revents = 0;
*pollretval = *pollretval - 1;
if (retval <= 0 && ChildDied) {
KillHelper(child);
HasHelper = 0;
if (inputsocket != -1) {
fdp[inputsocket].revents = 0;
*pollretval = *pollretval - 1;
}
goto done;
}
}
if (inputsocket != -1) {
if (!(fdp[inputsocket].revents & (POLLERR | POLLHUP |
POLLNVAL))) {
retval = read(InputSocket, buf, sizeof (buf));
if (retval > 0) {
(void) write(Master, buf, retval);
} else {
if (errno != EINTR && errno != EWOULDBLOCK)
perror("socket read");
xprt_unregister(&uxprt[INPUTSOCKET]);
}
}
/* clear this event for svc_getreq_poll */
fdp[inputsocket].revents = 0;
*pollretval = *pollretval - 1;
}
done:
/* sigsetmask (mask); */
sigrelse(SIGCHLD);
}
|