Commit | Line | Data |
---|---|---|
e5321f7b | 1 | static char *sccsid = "@(#)login.c 4.17 82/05/19"; |
88a01c09 BJ |
2 | /* |
3 | * login [ name ] | |
86eb6c9e | 4 | * login -r |
88a01c09 BJ |
5 | */ |
6 | ||
7 | #include <sys/types.h> | |
8 | #include <sgtty.h> | |
9 | #include <utmp.h> | |
10 | #include <signal.h> | |
11 | #include <pwd.h> | |
12 | #include <stdio.h> | |
13 | #include <sys/stat.h> | |
14 | #include <lastlog.h> | |
f570e1ff BJ |
15 | |
16 | #define SCPYN(a, b) strncpy(a, b, sizeof(a)) | |
88a01c09 | 17 | |
b4389814 BJ |
18 | #define NMAX sizeof(utmp.ut_name) |
19 | #define LMAX sizeof(utmp.ut_line) | |
88a01c09 | 20 | |
f570e1ff BJ |
21 | #define FALSE 0 |
22 | #define TRUE -1 | |
23 | ||
24 | char nolog[] = "/etc/nologin"; | |
25 | char qlog[] = ".hushlogin"; | |
26 | char securetty[] = "/etc/securetty"; | |
88a01c09 BJ |
27 | char maildir[30] = "/usr/spool/mail/"; |
28 | char lastlog[] = "/usr/adm/lastlog"; | |
29 | struct passwd nouser = {"", "nope"}; | |
30 | struct sgttyb ttyb; | |
31 | struct utmp utmp; | |
32 | char minusnam[16] = "-"; | |
86eb6c9e | 33 | |
88a01c09 BJ |
34 | char homedir[64] = "HOME="; |
35 | char shell[64] = "SHELL="; | |
36 | char term[64] = "TERM="; | |
f570e1ff | 37 | char user[20] = "USER="; |
86eb6c9e BJ |
38 | char *speeds[] = |
39 | { "0", "50", "75", "110", "134", "150", "200", "300", | |
40 | "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" }; | |
41 | #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0])) | |
42 | ||
43 | char *envinit[] = | |
44 | {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0}; | |
88a01c09 | 45 | |
86eb6c9e | 46 | struct passwd *pwd; |
88a01c09 | 47 | struct passwd *getpwnam(); |
86eb6c9e | 48 | char *strcat(), *rindex(), *index(); |
88a01c09 BJ |
49 | int setpwent(); |
50 | char *ttyname(); | |
51 | char *crypt(); | |
52 | char *getpass(); | |
53 | char *rindex(); | |
54 | char *stypeof(); | |
55 | extern char **environ; | |
56 | ||
841d84b0 BJ |
57 | #define CTRL(c) ('c'&037) |
58 | #define CERASE '#' | |
59 | #define CEOT CTRL(d) | |
60 | #define CKILL '@' | |
61 | #define CQUIT 034 /* FS, cntl shift L */ | |
62 | #define CINTR 0177 /* DEL */ | |
63 | #define CSTOP CTRL(s) | |
64 | #define CSTART CTRL(q) | |
65 | #define CBRK 0377 | |
66 | struct tchars tc = { | |
67 | CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK | |
68 | }; | |
69 | struct ltchars ltc = { | |
1113d588 | 70 | CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v) |
841d84b0 BJ |
71 | }; |
72 | ||
86eb6c9e | 73 | int rflag; |
b4389814 | 74 | char rusername[NMAX+1], lusername[NMAX+1]; |
86eb6c9e | 75 | char rpassword[NMAX+1]; |
e5321f7b | 76 | char name[NMAX+1]; |
b4389814 | 77 | char *rhost; |
86eb6c9e | 78 | |
88a01c09 BJ |
79 | main(argc, argv) |
80 | char **argv; | |
81 | { | |
82 | register char *namep; | |
83 | int t, f, c; | |
f570e1ff BJ |
84 | int invalid; |
85 | int quietlog; | |
86 | int i; | |
87 | FILE *nlfd; | |
88a01c09 | 88 | char *ttyn; |
b4389814 BJ |
89 | int ldisc = 0, zero = 0; |
90 | FILE *hostf; int first = 1; | |
88a01c09 BJ |
91 | |
92 | alarm(60); | |
93 | signal(SIGQUIT, SIG_IGN); | |
94 | signal(SIGINT, SIG_IGN); | |
95 | nice(-100); | |
96 | nice(20); | |
97 | nice(0); | |
4f8d3876 | 98 | if (argc > 1 && !strcmp(argv[1], "-r")) { |
86eb6c9e | 99 | rflag++; |
4f8d3876 | 100 | rhost = argv[2]; |
b4389814 | 101 | argc = 1; |
4f8d3876 BJ |
102 | getstr(rusername, sizeof (rusername), "remuser"); |
103 | getstr(lusername, sizeof (lusername), "locuser"); | |
86eb6c9e | 104 | getstr(term+5, sizeof(term)-5, "Terminal type"); |
4f8d3876 BJ |
105 | if (getuid()) |
106 | goto abnormal; | |
b4389814 BJ |
107 | setpwent(); |
108 | pwd = getpwnam(lusername); | |
4f8d3876 | 109 | endpwent(); |
b4389814 | 110 | if (pwd == NULL) { |
4f8d3876 BJ |
111 | if (strcmp(rusername, lusername)) |
112 | printf("%s: No such user\r\n", lusername); | |
113 | goto abnormal; | |
b4389814 | 114 | } |
1886582e | 115 | hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; |
b4389814 BJ |
116 | again: |
117 | if (hostf) { | |
118 | char ahost[32]; | |
119 | while (fgets(ahost, sizeof (ahost), hostf)) { | |
120 | char *user; | |
121 | if (index(ahost, '\n')) | |
122 | *index(ahost, '\n') = 0; | |
123 | user = index(ahost, ' '); | |
124 | if (user) | |
125 | *user++ = 0; | |
126 | if (!strcmp(rhost, ahost) && | |
4f8d3876 BJ |
127 | !strcmp(rusername, user ? user : lusername)) { |
128 | fclose(hostf); | |
b4389814 | 129 | goto normal; |
4f8d3876 | 130 | } |
b4389814 BJ |
131 | } |
132 | fclose(hostf); | |
133 | } | |
134 | if (first == 1) { | |
135 | first = 0; | |
136 | if (chdir(pwd->pw_dir) < 0) | |
137 | goto again; | |
138 | hostf = fopen(".rhosts", "r"); | |
139 | goto again; | |
140 | } | |
4f8d3876 | 141 | abnormal: |
b4389814 BJ |
142 | rhost = 0; |
143 | rflag = -1; | |
86eb6c9e | 144 | } |
b4389814 BJ |
145 | normal: |
146 | ioctl(0, TIOCLSET, &zero); | |
c95ed2b2 | 147 | ioctl(0, TIOCNXCL, 0); |
4f8d3876 BJ |
148 | ioctl(0, FIONBIO, &zero); |
149 | ioctl(0, FIOASYNC, &zero); | |
88a01c09 | 150 | gtty(0, &ttyb); |
86eb6c9e BJ |
151 | if (rflag) { |
152 | char *cp = index(term, '/'); | |
153 | if (cp) { | |
154 | int i; | |
155 | *cp++ = 0; | |
156 | for (i = 0; i < NSPEEDS; i++) | |
157 | if (!strcmp(speeds[i], cp)) { | |
158 | ttyb.sg_ispeed = ttyb.sg_ospeed = i; | |
159 | break; | |
160 | } | |
161 | } | |
162 | ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; | |
163 | } | |
f570e1ff BJ |
164 | ttyb.sg_erase = CERASE; |
165 | ttyb.sg_kill = CKILL; | |
88a01c09 | 166 | stty(0, &ttyb); |
841d84b0 BJ |
167 | ioctl(0, TIOCSETC, &tc); |
168 | ioctl(0, TIOCSLTC, <c); | |
88a01c09 BJ |
169 | for (t=3; t<20; t++) |
170 | close(t); | |
171 | ttyn = ttyname(0); | |
f570e1ff | 172 | if (ttyn==(char *)0) |
88a01c09 | 173 | ttyn = "/dev/tty??"; |
f570e1ff BJ |
174 | do { |
175 | ldisc = 0; | |
c95ed2b2 | 176 | ioctl(0, TIOCSETD, &ldisc); |
f570e1ff BJ |
177 | invalid = FALSE; |
178 | SCPYN(utmp.ut_name, ""); | |
179 | if (argc>1) { | |
180 | SCPYN(utmp.ut_name, argv[1]); | |
181 | argc = 0; | |
182 | } | |
4f8d3876 | 183 | if (rflag) { |
b4389814 | 184 | strcpy(utmp.ut_name, lusername); |
4f8d3876 BJ |
185 | if (rflag == -1) |
186 | rflag = 0; | |
187 | } else | |
b4389814 BJ |
188 | while (utmp.ut_name[0] == '\0') { |
189 | namep = utmp.ut_name; | |
190 | { char hostname[32]; | |
191 | gethostname(hostname, sizeof (hostname)); | |
192 | printf("%s login: ", hostname); } | |
193 | while ((c = getchar()) != '\n') { | |
194 | if (c == ' ') | |
195 | c = '_'; | |
196 | if (c == EOF) | |
197 | exit(0); | |
198 | if (namep < utmp.ut_name+NMAX) | |
199 | *namep++ = c; | |
200 | } | |
f570e1ff | 201 | } |
b4389814 BJ |
202 | if (rhost == 0) { |
203 | setpwent(); | |
204 | if ((pwd = getpwnam(utmp.ut_name)) == NULL) | |
205 | pwd = &nouser; | |
206 | endpwent(); | |
f570e1ff | 207 | } |
f570e1ff BJ |
208 | if (!strcmp(pwd->pw_shell, "/bin/csh")) { |
209 | ldisc = NTTYDISC; | |
210 | ioctl(0, TIOCSETD, &ldisc); | |
211 | } | |
b4389814 BJ |
212 | if (rhost == 0) { |
213 | if (*pwd->pw_passwd != '\0') { | |
214 | char *pp; | |
215 | nice(-4); | |
216 | if (rflag == 0) | |
217 | pp = getpass("Password:"); | |
218 | else | |
219 | pp = rpassword; | |
220 | namep = crypt(pp,pwd->pw_passwd); | |
221 | nice(4); | |
222 | if (strcmp(namep, pwd->pw_passwd)) | |
223 | invalid = TRUE; | |
224 | } | |
f570e1ff BJ |
225 | } |
226 | if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { | |
227 | /* logins are disabled except for root */ | |
228 | while ((c = getc(nlfd)) != EOF) | |
229 | putchar(c); | |
230 | fflush(stdout); | |
231 | sleep(5); | |
232 | exit(0); | |
233 | } | |
234 | if (!invalid && pwd->pw_uid == 0 && | |
235 | !rootterm(ttyn+sizeof("/dev/")-1)) { | |
4f8d3876 BJ |
236 | logerr("ROOT LOGIN REFUSED %s", |
237 | ttyn+sizeof("/dev/")-1); | |
f570e1ff BJ |
238 | invalid = TRUE; |
239 | } | |
240 | if (invalid) { | |
88a01c09 | 241 | printf("Login incorrect\n"); |
4f8d3876 BJ |
242 | if (ttyn[sizeof("/dev/tty")-1] == 'd') |
243 | logerr("BADDIALUP %s %s\n", | |
244 | ttyn+sizeof("/dev/")-1, utmp.ut_name); | |
88a01c09 | 245 | } |
f570e1ff BJ |
246 | if (*pwd->pw_shell == '\0') |
247 | pwd->pw_shell = "/bin/sh"; | |
248 | i = strlen(pwd->pw_shell); | |
249 | if (chdir(pwd->pw_dir) < 0 && !invalid ) { | |
250 | if (chdir("/") < 0) { | |
251 | printf("No directory!\n"); | |
252 | invalid = TRUE; | |
253 | } else { | |
254 | printf("No directory! Logging in with home=/\n"); | |
255 | pwd->pw_dir = "/"; | |
256 | } | |
88a01c09 | 257 | } |
86eb6c9e BJ |
258 | if (rflag && invalid) |
259 | exit(1); | |
f570e1ff | 260 | } while (invalid); |
88a01c09 | 261 | |
86eb6c9e | 262 | |
88a01c09 BJ |
263 | time(&utmp.ut_time); |
264 | t = ttyslot(); | |
265 | if (t>0 && (f = open("/etc/utmp", 1)) >= 0) { | |
266 | lseek(f, (long)(t*sizeof(utmp)), 0); | |
267 | SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); | |
268 | write(f, (char *)&utmp, sizeof(utmp)); | |
269 | close(f); | |
270 | } | |
271 | if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { | |
272 | lseek(f, 0L, 2); | |
273 | write(f, (char *)&utmp, sizeof(utmp)); | |
274 | close(f); | |
275 | } | |
4f8d3876 | 276 | quietlog = 0; |
f570e1ff | 277 | if (access(qlog, 0) == 0) |
4f8d3876 BJ |
278 | quietlog = 1; |
279 | if ((f = open(lastlog, 2)) >= 0) { | |
f570e1ff BJ |
280 | struct lastlog ll; |
281 | ||
282 | lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); | |
283 | if (read(f, (char *) &ll, sizeof ll) == sizeof ll && | |
284 | ll.ll_time != 0) { | |
4f8d3876 | 285 | if (quietlog == 0) |
f570e1ff BJ |
286 | printf("Last login: %.*s on %.*s\n" |
287 | , 24-5 | |
288 | , (char *) ctime(&ll.ll_time) | |
289 | , sizeof(ll.ll_line) | |
290 | , ll.ll_line | |
291 | ); | |
292 | } | |
293 | lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); | |
294 | time(&ll.ll_time); | |
295 | SCPYN(ll.ll_line, rindex(ttyn, '/')+1); | |
296 | write(f, (char *) &ll, sizeof ll); | |
297 | close(f); | |
298 | } | |
88a01c09 BJ |
299 | chown(ttyn, pwd->pw_uid, pwd->pw_gid); |
300 | setgid(pwd->pw_gid); | |
e5321f7b KM |
301 | strncpy(name, utmp.ut_name, NMAX); |
302 | name[NMAX] = '\0'; | |
303 | inigrp(name, pwd->pw_gid); | |
88a01c09 | 304 | setuid(pwd->pw_uid); |
88a01c09 BJ |
305 | environ = envinit; |
306 | strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); | |
307 | strncat(shell, pwd->pw_shell, sizeof(shell)-7); | |
4f8d3876 | 308 | if (term[strlen("TERM=")] == 0) |
86eb6c9e | 309 | strncat(term, stypeof(ttyn), sizeof(term)-6); |
f570e1ff | 310 | strncat(user, pwd->pw_name, sizeof(user)-6); |
88a01c09 BJ |
311 | if ((namep = rindex(pwd->pw_shell, '/')) == NULL) |
312 | namep = pwd->pw_shell; | |
313 | else | |
314 | namep++; | |
315 | strcat(minusnam, namep); | |
316 | alarm(0); | |
b4389814 | 317 | umask(022); |
4f8d3876 BJ |
318 | if (ttyn[sizeof("/dev/tty")-1] == 'd') |
319 | logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); | |
320 | if (!quietlog) { | |
f570e1ff BJ |
321 | showmotd(); |
322 | strcat(maildir, pwd->pw_name); | |
323 | if (access(maildir,4)==0) { | |
324 | struct stat statb; | |
325 | stat(maildir, &statb); | |
326 | if (statb.st_size) | |
327 | printf("You have mail.\n"); | |
328 | } | |
329 | } | |
330 | ||
88a01c09 BJ |
331 | signal(SIGQUIT, SIG_DFL); |
332 | signal(SIGINT, SIG_DFL); | |
5f87416f | 333 | signal(SIGTSTP, SIG_IGN); |
88a01c09 | 334 | execlp(pwd->pw_shell, minusnam, 0); |
f570e1ff | 335 | perror(pwd->pw_shell); |
88a01c09 BJ |
336 | printf("No shell\n"); |
337 | exit(0); | |
338 | } | |
339 | ||
340 | int stopmotd; | |
341 | catch() | |
342 | { | |
1886582e | 343 | |
88a01c09 BJ |
344 | signal(SIGINT, SIG_IGN); |
345 | stopmotd++; | |
346 | } | |
347 | ||
f570e1ff | 348 | rootterm(tty) |
1886582e | 349 | char *tty; |
f570e1ff BJ |
350 | { |
351 | register FILE *fd; | |
1886582e | 352 | char buf[100]; |
f570e1ff | 353 | |
1886582e BJ |
354 | if (rflag) |
355 | return(1); | |
f570e1ff BJ |
356 | if ((fd = fopen(securetty, "r")) == NULL) |
357 | return(1); | |
358 | while (fgets(buf, sizeof buf, fd) != NULL) { | |
359 | buf[strlen(buf)-1] = '\0'; | |
360 | if (strcmp(tty, buf) == 0) { | |
361 | fclose(fd); | |
362 | return(1); | |
363 | } | |
364 | } | |
365 | fclose(fd); | |
366 | return(0); | |
367 | } | |
368 | ||
88a01c09 BJ |
369 | showmotd() |
370 | { | |
371 | FILE *mf; | |
372 | register c; | |
373 | ||
374 | signal(SIGINT, catch); | |
f570e1ff BJ |
375 | if ((mf = fopen("/etc/motd","r")) != NULL) { |
376 | while ((c = getc(mf)) != EOF && stopmotd == 0) | |
88a01c09 BJ |
377 | putchar(c); |
378 | fclose(mf); | |
379 | } | |
380 | signal(SIGINT, SIG_IGN); | |
381 | } | |
382 | ||
f570e1ff | 383 | #undef UNKNOWN |
88a01c09 BJ |
384 | #define UNKNOWN "su" |
385 | ||
386 | char * | |
387 | stypeof(ttyid) | |
388 | char *ttyid; | |
389 | { | |
390 | static char typebuf[16]; | |
391 | char buf[50]; | |
392 | register FILE *f; | |
393 | register char *p, *t, *q; | |
394 | ||
395 | if (ttyid == NULL) | |
396 | return (UNKNOWN); | |
397 | f = fopen("/etc/ttytype", "r"); | |
398 | if (f == NULL) | |
399 | return (UNKNOWN); | |
400 | /* split off end of name */ | |
401 | for (p = q = ttyid; *p != 0; p++) | |
402 | if (*p == '/') | |
403 | q = p + 1; | |
404 | ||
405 | /* scan the file */ | |
406 | while (fgets(buf, sizeof buf, f) != NULL) | |
407 | { | |
f570e1ff | 408 | for (t=buf; *t!=' ' && *t != '\t'; t++) |
88a01c09 BJ |
409 | ; |
410 | *t++ = 0; | |
f570e1ff BJ |
411 | while (*t == ' ' || *t == '\t') |
412 | t++; | |
88a01c09 BJ |
413 | for (p=t; *p>' '; p++) |
414 | ; | |
415 | *p = 0; | |
416 | if (strcmp(q,t)==0) { | |
417 | strcpy(typebuf, buf); | |
418 | fclose(f); | |
419 | return (typebuf); | |
420 | } | |
421 | } | |
422 | fclose (f); | |
423 | return (UNKNOWN); | |
424 | } | |
86eb6c9e BJ |
425 | |
426 | getstr(buf, cnt, err) | |
427 | char *buf; | |
428 | int cnt; | |
429 | char *err; | |
430 | { | |
431 | char c; | |
432 | ||
433 | do { | |
434 | if (read(0, &c, 1) != 1) | |
435 | exit(1); | |
436 | if (--cnt < 0) { | |
437 | printf("%s too long\r\n", err); | |
438 | exit(1); | |
439 | } | |
440 | *buf++ = c; | |
441 | } while (c != 0); | |
442 | } | |
4f8d3876 BJ |
443 | |
444 | logerr(fmt, a1, a2, a3) | |
445 | char *fmt, *a1, *a2, *a3; | |
446 | { | |
447 | ||
448 | } |