Repository navigation
Expand file tree
/
Copy pathrshd.c
More file actions
445 lines (419 loc) · 8.56 KB
/
Copy pathrshd.c
File metadata and controls
445 lines (419 loc) · 8.56 KB
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
/*
in.rshd.c
*/
/*
main channel:
back channel\0
remuser\0
locuser\0
command\0
data
back channel:
signal\0
*/
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <netdb.h>
#include <net/gen/socket.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/hton.h>
#include <net/netlib.h>
#define DEBUG 0
#if DEBUG
#define where() fprintf(stderr, "%s, %d: ", __FILE__, __LINE__)
#endif
char cmdbuf[_POSIX_ARG_MAX+1], locuser[16], remuser[16];
extern char **environ;
char username[20]="USER=";
char homedir[64]="HOME=";
char shell[64]="SHELL=";
char tz[1024]="TZ=";
char *envinit[]= {homedir, shell, username, tz, "PATH=:/bin:/usr/bin", 0};
char *prog_name;
char buffer[PIPE_BUF];
#if __STDC__
#define PROTO(func, args) func args
#else
#define PROTO(func, args) func ()
#endif
PROTO (int main, (int argc, char *argv[]));
PROTO (void getstr, (char*buf, int cnt, char *err));
PROTO (void close_on_exec, (int fd));
int main(argc, argv)
int argc;
char *argv[];
{
int result, result1;
nwio_tcpconf_t tcpconf, err_tcpconf;
nwio_tcpcl_t tcpconnopt;
nwio_tcpatt_t tcpattachopt;
tcpport_t tcpport;
tcpport_t err_port;
int err_fd, pds[2];
pid_t pid, pid1, new_pg;
#if USEATTACH
int err2_fd;
#endif
struct passwd *pwent;
char *cp, *buff_ptr, *TZ;
char sig;
prog_name= argv[0];
if (argc != 1)
{
fprintf(stderr, "%s: wrong number of arguments (%d)\n",
prog_name, argc);
exit(1);
}
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (0, NWIOGTCPCONF, &tcpconf);
if (result<0)
{
fprintf(stderr, "%s: ioctl(NWIOGTCPCONF)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
tcpport= ntohs(tcpconf.nwtc_remport);
if (tcpport >= TCPPORT_RESERVED || tcpport < TCPPORT_RESERVED/2)
{
printf("\1%s: unprotected port (%d)\n", prog_name, tcpport);
exit(1);
}
alarm(60);
err_port= 0;
for (;;)
{
char c;
result= read(0, &c, 1);
if (result <0)
{
fprintf(stderr, "%s: read= %d : %s\n", prog_name,
errno, strerror(errno));
}
if (result<1)
exit(1);
if (c == 0)
break;
err_port= err_port*10 + c - '0';
}
alarm(0);
if (err_port != 0)
{
int n, pid, lport;
pid= getpid();
lport= 1;
do {
lport= (lport << 1) | (pid & 1);
pid >>= 1;
} while (lport < TCPPORT_RESERVED/2);
n= TCPPORT_RESERVED/2;
do
{
if (--lport < TCPPORT_RESERVED/2)
lport= TCPPORT_RESERVED-1;
err_fd= open ("/dev/tcp", O_RDWR);
if (err_fd<0)
{
fprintf(stderr, "%s: open= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
close_on_exec(err_fd);
err_tcpconf.nwtc_flags= NWTC_LP_SET | NWTC_SET_RA |
NWTC_SET_RP | NWTC_EXCL;
err_tcpconf.nwtc_locport= htons(lport);
err_tcpconf.nwtc_remport= htons(err_port);
err_tcpconf.nwtc_remaddr= tcpconf.nwtc_remaddr;
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (err_fd, NWIOSTCPCONF, &err_tcpconf);
if (result == 0) break;
if (errno != EADDRINUSE)
{
fprintf(stderr,
"%s: ioctl(NWIOSTCPCONF)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
close(err_fd);
} while (--n > 0);
if (n == 0)
{
printf("\1can't get stderr port\n");
exit(1);
}
err_tcpconf.nwtc_flags= NWTC_SHARED;
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (err_fd, NWIOSTCPCONF, &err_tcpconf);
if (result<0)
{
fprintf(stderr,
"%s: ioctl(NWIOSTCPCONF)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
tcpconnopt.nwtcl_flags= 0;
n= 20;
for (;;)
{
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (err_fd, NWIOTCPCONN, &tcpconnopt);
if (result == 0) break;
if (errno != EAGAIN && errno != ECONNREFUSED)
{
fprintf(stderr,
"%s: ioctl(NWIOTCPCONN)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
if (--n == 0) break;
sleep(1);
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
}
#if USEATTACH
err2_fd= open ("/dev/tcp", O_RDWR);
close_on_exec(err2_fd);
if (err2_fd<0)
{
fprintf(stderr, "%s: open= %d : %s\n", errno,
prog_name, strerror(errno));
exit(1);
}
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (err2_fd, NWIOSTCPCONF, &err_tcpconf);
if (result<0)
{
fprintf(stderr, "%s: ioctl(NWIOSTCPCONF)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
tcpattachopt.nwta_flags= 0;
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
result= ioctl (err2_fd, NWIOTCPATTACH, &tcpattachopt);
if (result<0)
{
fprintf(stderr, "%s: ioctl(NWIOTCPATTACH)= %d : %s\n",
prog_name, errno, strerror(errno));
exit(1);
}
#if DEBUG
{ where(); fprintf(stderr, "\n"); }
#endif
#endif
}
getstr(remuser, sizeof(remuser), "remuser");
getstr(locuser, sizeof(locuser), "locuser");
getstr(cmdbuf, sizeof(cmdbuf), "cmdbuf");
setpwent();
pwent= getpwnam(locuser);
if (!pwent)
{
printf("\1Login incorrect.\n");
exit(1);
}
endpwent();
if (chdir(pwent->pw_dir) < 0)
{
chdir("/");
}
#if DEBUG
{ where(); fprintf(stderr, "calling iruserok(%s, %d, %s, %s)\n",
inet_ntoa(tcpconf.nwtc_remaddr), 0, remuser, locuser); }
#endif
if (iruserok(tcpconf.nwtc_remaddr, 0, remuser, locuser) < 0)
{
printf("\1Permission denied.\n");
exit(1);
}
if (err_port)
{
/* Let's go to
C36C
a different process group. */
new_pg= setsid();
pid= fork();
if (pid<0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "%s: fork()= %d : %s\n",
prog_name, errno, strerror(errno));
}
printf("\1Try again.\n");
exit(1);
}
if (pid)
{
close(0); /* stdin */
close(1); /* stdout */
#if USEATTACH
close(err_fd); /* stderr for shell */
#endif
dup2(2,0);
dup2(2,1);
for (;;)
{
#if !USEATTACH
if (read(err_fd, &sig, 1) <= 0)
#else
if (read(err2_fd, &sig, 1) <= 0)
#endif
{
#if 0
printf("read failed: %d\n", errno);
#endif
exit(0);
}
pid= 0;
#if 0
printf("killing %d with %d\n", -new_pg, sig);
#endif
kill(-new_pg, sig);
}
}
#if USEATTACH
close(err2_fd); /* signal channel for parent */
#endif
result= pipe(pds);
if (result<0)
{
printf("\1Can't make pipe\n");
kill(getppid(), SIGTERM);
exit(1);
}
pid1= fork();
if (pid1<0)
{
if (errno != EAGAIN)
{
fprintf(stderr, "%s: fork()= %d : %s\n",
prog_name, errno, strerror(errno));
}
printf("\1Try again.\n");
kill(-new_pg, SIGTERM);
exit(1);
}
if (pid1)
{
close(pds[1]); /* write side of pipe */
for (;;)
{
result= read(pds[0], buffer, sizeof(buffer));
if (result<=0)
{
kill(pid, SIGTERM);
exit(0);
}
buff_ptr= buffer;
while (result>0)
{
result1= write (err_fd, buff_ptr,
result);
if (result1 <= 0)
{
fprintf(stderr,
"%s: write()= %d : %s\n",
prog_name, errno,
strerror(errno));
kill(-new_pg, SIGTERM);
exit(1);
}
result -= result1;
}
}
}
close(err_fd); /* file descriptor for error channel */
close (pds[0]); /* read side of pipe */
dup2(pds[1], 2);
close (pds[1]); /* write side of pipe */
}
if (*pwent->pw_shell == '\0')
pwent->pw_shell= "/bin/sh";
#if __minix_vmd
initgroups(pwent->pw_name, pwent->pw_gid);
#endif
setgid(pwent->pw_gid);
setuid(pwent->pw_uid);
TZ=getenv("TZ");
environ= envinit;
strncat(homedir, pwent->pw_dir, sizeof(homedir)-6);
strncat(shell, pwent->pw_shell, sizeof(shell)-7);
strncat(username, pwent->pw_name, sizeof(username)-6);
if (TZ)
strncat(tz, TZ, sizeof(tz)-4);
else
envinit[3]= NULL;
cp= strrchr(pwent->pw_shell, '/');
if (cp)
cp++;
else
cp= pwent->pw_shell;
if (!err_port)
dup2(1, 2);
write(1, "\0", 1);
execl(pwent->pw_shell, cp, "-c", cmdbuf, 0);
close(2);
open("/dev/tty", O_RDWR);
fprintf(stderr, "%s: execl(%s, %s, .., %s)= %d : %s\n", prog_name,
pwent->pw_shell, cp, cmdbuf, errno, strerror(errno));
kill(getppid(), SIGTERM);
exit(1);
}
void getstr(buf, cnt, err)
char *buf;
int cnt;
char *err;
{
char c;
do
{
if (read(0, &c, 1) != 1)
exit(1);
*buf++ = c;
if (--cnt == 0)
{
printf("\1%s too long", err);
exit(1);
}
} while (c != 0);
}
void close_on_exec(fd)
int fd;
{
(void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
}