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