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