|
|
1.1 root 1: #ifndef lint
2: static char sccsid[] = "@(#)rogind.c 4.18 83/07/01";
3: #endif
4:
5: #include <stdio.h>
6: #include <sys/param.h>
7: #include <sys/types.h>
8: #include <sys/stat.h>
9: #include <errno.h>
10: #include <pwd.h>
11: #include <signal.h>
12: #include <sgtty.h>
13: #include <stdio.h>
14: #include <wait.h>
15: #include <sys/inet/in.h>
16: #include "config.h"
17:
18: extern errno;
19: int reapchild();
20: int alrmcatch();
21: struct passwd *getpwnam();
22: struct passwd *pwd;
23: struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" };
24: char *crypt(), *rindex(), *index(), *malloc();
25: extern char **environ;
26: char term[20] = "TERM=";
27: static char *envinit[] = {
28: term,
29: 0
30: };
31: /*
32: * remote login server:
33: * remuser\0
34: * locuser\0
35: * terminal type\0
36: * data
37: */
38: main(argc, argv)
39: int argc;
40: char **argv;
41: {
42: int f, dev;
43: unsigned long faddr;
44: int myport, fport;
45: struct in_service *sp;
46:
47: sp = in_service("login", "tcp", 0);
48: if(sp == 0){
49: fprintf(stderr, "rogind: tcp/rogin: unknown service\n");
50: exit(1);
51: }
52: myport = sp->port;
53: close(3);
54: signal(SIGPIPE, SIG_IGN);
55: signal(SIGHUP, SIG_IGN);
56:
57: f = tcp_sock();
58: if (f < 0) {
59: perror("rogind: socket");
60: exit(1);
61: }
62: signal(SIGCHLD, reapchild);
63: signal(SIGALRM, alrmcatch);
64: tcp_listen(f, myport, 0, 0);
65: for (;;) {
66: int s;
67:
68: s = tcp_accept(f, &faddr, &fport, &dev);
69: if (s < 0) {
70: if (errno == EINTR)
71: continue;
72: perror("rogind: accept");
73: continue;
74: }
75: doit(s, faddr, fport, dev);
76: close(s);
77: }
78: }
79:
80: reapchild()
81: {
82: union wait status;
83: int pid;
84:
85: signal(SIGCHLD, SIG_IGN);
86: while((pid = wait3(&status, WNOHANG, 0)) > 0)
87: rmut(pid);
88: signal(SIGCHLD, reapchild);
89: }
90:
91: char rusername[32], lusername[32];
92: char buf[BUFSIZ];
93: int child;
94: int cleanup();
95: int netf;
96: extern errno;
97:
98: doit(f, faddr, fport, dev)
99: int f;
100: unsigned long faddr;
101: {
102: extern tty_ld;
103: extern char *ttyname();
104: char c;
105: int pid, ret;
106: char *host, line[32];
107:
108: sprintf(line, "/dev/tcp%02d", dev);
109: alarm(20);
110: c = 1;
111: read(f, &c, 1);
112: alarm(0);
113: if(c != 0){
114: fprintf(stderr, "non-zero initial read from %x %d\n", faddr, fport);
115: return;
116: }
117: host = (char *)in_host(faddr);
118: if (host == 0) {
119: msg(f, "Don't know your host name\n");
120: fprintf(stderr, "didn't know his host name\n");
121: return;
122: }
123: if(fport >= 1024){
124: msg(f, "Permission denied\n");
125: fprintf(stderr, "rogind: denied him permission\n");
126: return;
127: }
128: if(access(line, 0) < 0){
129: msg(f, "No tty\n");
130: fprintf(stderr, "rogind: couldn't access line %s\n", line);
131: return;
132: }
133: pid = fork();
134: if (pid < 0){
135: msg(f, "Fork problem\n");
136: fprintf(stderr, "rogind: fork problen\n");
137: return;
138: }
139: if (pid) {
140: record(pid, line);
141: return;
142: }
143:
144: write(f, "", 1);
145:
146: signal(SIGCHLD, SIG_IGN);
147: signal(SIGHUP, SIG_DFL);
148: signal(SIGALRM, SIG_DFL);
149:
150: dup2(f, 0);
151: dup2(f, 1);
152: dup2(f, 2);
153: dup2(0, 3);
154: close(f);
155: ioctl(0, TIOCSPGRP, 0);
156: ioctl(0, TCPIOHUP, 0);
157:
158: environ = envinit;
159:
160: ret = doremotelogin(host);
161: ioctl(0, FIOPUSHLD, &tty_ld);
162: sttyek(0);
163: if(ret < 0)
164: execl(LOGIN, "login", 0);
165: else
166: execl(LOGIN, "login", "-f", pwd->pw_name, 0);
167: printf("%s not found\n", LOGIN);
168: exit(1);
169: /*NOTREACHED*/
170: }
171:
172:
173:
174: struct foo{
175: int pid;
176: char line[16];
177: };
178: #define NFOO 50
179: struct foo foo[NFOO];
180:
181: record(pid, line)
182: char *line;
183: {
184: int i;
185:
186: for(i = 0; i < NFOO; i++){
187: if(foo[i].pid == 0){
188: foo[i].pid = pid;
189: strncpy(foo[i].line, line+5, 8);
190: return;
191: }
192: }
193: }
194:
195: #include <utmp.h>
196:
197: struct utmp wtmp;
198: char wtmpf[] = "/usr/adm/wtmp";
199: char utmp[] = UTMP;
200: #define SCPYN(a, b) strncpy(a, b, sizeof(a))
201: #define SCMPN(a, b) strncmp(a, b, sizeof(a))
202:
203: rmut(pid)
204: {
205: register f;
206: int found = 0;
207: char *line;
208: int i;
209: char file[32];
210:
211: for(i = 0; i < NFOO; i++){
212: if(pid == foo[i].pid){
213: line = foo[i].line;
214: foo[i].pid = 0;
215: break;
216: }
217: }
218: if(i >= NFOO)
219: return;
220: f = open(utmp, 2);
221: if (f >= 0) {
222: while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) {
223: if (SCMPN(wtmp.ut_line, line) || wtmp.ut_name[0]==0)
224: continue;
225: lseek(f, -(long)sizeof(wtmp), 1);
226: SCPYN(wtmp.ut_name, "");
227: time(&wtmp.ut_time);
228: write(f, (char *)&wtmp, sizeof(wtmp));
229: found++;
230: }
231: close(f);
232: }
233: if (found) {
234: f = open(wtmpf, 1);
235: if (f >= 0) {
236: SCPYN(wtmp.ut_line, line+5);
237: SCPYN(wtmp.ut_name, "");
238: time(&wtmp.ut_time);
239: lseek(f, (long)0, 2);
240: write(f, (char *)&wtmp, sizeof(wtmp));
241: close(f);
242: }
243: }
244: sprintf(file, "/dev/%s", line);
245: chmod(file, 0600);
246: chown(file, 0, 0);
247: }
248:
249: msg(fd, s)
250: char *s;
251: {
252: write(fd, "\001", 1);
253: write(fd, s, strlen(s));
254: }
255:
256: alrmcatch()
257: {
258: signal(SIGALRM, alrmcatch);
259: return(-1);
260: }
261:
262: sttyek(fd)
263: {
264: struct sgttyb vec;
265:
266: gtty(fd, &vec);
267: vec.sg_erase = '#';
268: vec.sg_kill = '@';
269: vec.sg_flags |= XTABS;
270: stty(fd, &vec);
271: }
272:
273: doremotelogin(host)
274: char *host;
275: {
276: FILE *hostf;
277: int first = 1;
278: char *p;
279:
280: getstr(rusername, sizeof (rusername), "remuser");
281: getstr(lusername, sizeof (lusername), "locuser");
282: getstr(term+5, sizeof(term)-5, "Terminal type");
283: if(p = (char *)rindex(term, '/'))
284: *p = '\0';
285: if (getuid()) {
286: pwd = &nouser;
287: goto bad;
288: }
289: setpwent();
290: pwd = getpwnam(lusername);
291: endpwent();
292: if (pwd == NULL) {
293: pwd = &nouser;
294: goto bad;
295: }
296: hostf = pwd->pw_uid ? fopen(EQUIV, "r") : 0;
297: again:
298: if (hostf) {
299: char ahost[32];
300:
301: while (fgets(ahost, sizeof (ahost), hostf)) {
302: char *user;
303:
304: if ((user = index(ahost, '\n')) != 0)
305: *user++ = '\0';
306: if ((user = index(ahost, ' ')) != 0)
307: *user++ = '\0';
308: if (!strcmp(host, ahost) &&
309: !strcmp(rusername, user ? user : lusername)) {
310: fclose(hostf);
311: return (1);
312: }
313: }
314: fclose(hostf);
315: }
316: if (first == 1) {
317: char *rhosts = ".rhosts";
318: struct stat sbuf;
319:
320: first = 0;
321: if (chdir(pwd->pw_dir) < 0)
322: goto again;
323: if (lstat(rhosts, &sbuf) < 0)
324: goto again;
325: if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
326: printf("login: .rhosts is a soft link.\r\n");
327: goto bad;
328: }
329: hostf = fopen(rhosts, "r");
330: fstat(fileno(hostf), &sbuf);
331: if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
332: printf("login: Bad .rhosts ownership.\r\n");
333: fclose(hostf);
334: goto bad;
335: }
336: goto again;
337: }
338: bad:
339: return (-1);
340: }
341:
342: getstr(buf, cnt, err)
343: char *buf;
344: int cnt;
345: char *err;
346: {
347: char c;
348:
349: do {
350: if (read(0, &c, 1) != 1)
351: exit(1);
352: if (--cnt < 0) {
353: printf("%s too long\r\n", err);
354: exit(1);
355: }
356: *buf++ = c;
357: } while (c != 0);
358: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.