convert to 4.1c sys calls and directory layout
[unix-history] / usr / src / usr.bin / login / login.c
CommitLineData
b1198826 1static char *sccsid = "@(#)login.c 4.19 82/11/14";
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";
29struct passwd nouser = {"", "nope"};
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
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
66struct tchars tc = {
67 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
68};
69struct ltchars ltc = {
1113d588 70 CTRL(z), CTRL(y), CTRL(r), CTRL(o), CTRL(w), CTRL(v)
841d84b0
BJ
71};
72
86eb6c9e 73int rflag;
b4389814 74char rusername[NMAX+1], lusername[NMAX+1];
86eb6c9e 75char rpassword[NMAX+1];
e5321f7b 76char name[NMAX+1];
b4389814 77char *rhost;
86eb6c9e 78
88a01c09
BJ
79main(argc, argv)
80char **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 141abnormal:
b4389814
BJ
142 rhost = 0;
143 rflag = -1;
86eb6c9e 144 }
b4389814
BJ
145normal:
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, &ltc);
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 299 chown(ttyn, pwd->pw_uid, pwd->pw_gid);
12d1bcdd 300 chmod(ttyn, 622);
88a01c09 301 setgid(pwd->pw_gid);
e5321f7b
KM
302 strncpy(name, utmp.ut_name, NMAX);
303 name[NMAX] = '\0';
b1198826 304 initgroups(name, pwd->pw_gid);
88a01c09 305 setuid(pwd->pw_uid);
88a01c09
BJ
306 environ = envinit;
307 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
308 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
4f8d3876 309 if (term[strlen("TERM=")] == 0)
86eb6c9e 310 strncat(term, stypeof(ttyn), sizeof(term)-6);
f570e1ff 311 strncat(user, pwd->pw_name, sizeof(user)-6);
88a01c09
BJ
312 if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
313 namep = pwd->pw_shell;
314 else
315 namep++;
316 strcat(minusnam, namep);
317 alarm(0);
b4389814 318 umask(022);
4f8d3876
BJ
319 if (ttyn[sizeof("/dev/tty")-1] == 'd')
320 logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
321 if (!quietlog) {
f570e1ff
BJ
322 showmotd();
323 strcat(maildir, pwd->pw_name);
324 if (access(maildir,4)==0) {
325 struct stat statb;
326 stat(maildir, &statb);
327 if (statb.st_size)
328 printf("You have mail.\n");
329 }
330 }
331
88a01c09
BJ
332 signal(SIGQUIT, SIG_DFL);
333 signal(SIGINT, SIG_DFL);
5f87416f 334 signal(SIGTSTP, SIG_IGN);
88a01c09 335 execlp(pwd->pw_shell, minusnam, 0);
f570e1ff 336 perror(pwd->pw_shell);
88a01c09
BJ
337 printf("No shell\n");
338 exit(0);
339}
340
341int stopmotd;
342catch()
343{
1886582e 344
88a01c09
BJ
345 signal(SIGINT, SIG_IGN);
346 stopmotd++;
347}
348
f570e1ff 349rootterm(tty)
1886582e 350 char *tty;
f570e1ff
BJ
351{
352 register FILE *fd;
1886582e 353 char buf[100];
f570e1ff 354
1886582e
BJ
355 if (rflag)
356 return(1);
f570e1ff
BJ
357 if ((fd = fopen(securetty, "r")) == NULL)
358 return(1);
359 while (fgets(buf, sizeof buf, fd) != NULL) {
360 buf[strlen(buf)-1] = '\0';
361 if (strcmp(tty, buf) == 0) {
362 fclose(fd);
363 return(1);
364 }
365 }
366 fclose(fd);
367 return(0);
368}
369
88a01c09
BJ
370showmotd()
371{
372 FILE *mf;
373 register c;
374
375 signal(SIGINT, catch);
f570e1ff
BJ
376 if ((mf = fopen("/etc/motd","r")) != NULL) {
377 while ((c = getc(mf)) != EOF && stopmotd == 0)
88a01c09
BJ
378 putchar(c);
379 fclose(mf);
380 }
381 signal(SIGINT, SIG_IGN);
382}
383
f570e1ff 384#undef UNKNOWN
88a01c09
BJ
385#define UNKNOWN "su"
386
387char *
388stypeof(ttyid)
389char *ttyid;
390{
391 static char typebuf[16];
392 char buf[50];
393 register FILE *f;
394 register char *p, *t, *q;
395
396 if (ttyid == NULL)
397 return (UNKNOWN);
398 f = fopen("/etc/ttytype", "r");
399 if (f == NULL)
400 return (UNKNOWN);
401 /* split off end of name */
402 for (p = q = ttyid; *p != 0; p++)
403 if (*p == '/')
404 q = p + 1;
405
406 /* scan the file */
407 while (fgets(buf, sizeof buf, f) != NULL)
408 {
f570e1ff 409 for (t=buf; *t!=' ' && *t != '\t'; t++)
88a01c09
BJ
410 ;
411 *t++ = 0;
f570e1ff
BJ
412 while (*t == ' ' || *t == '\t')
413 t++;
88a01c09
BJ
414 for (p=t; *p>' '; p++)
415 ;
416 *p = 0;
417 if (strcmp(q,t)==0) {
418 strcpy(typebuf, buf);
419 fclose(f);
420 return (typebuf);
421 }
422 }
423 fclose (f);
424 return (UNKNOWN);
425}
86eb6c9e
BJ
426
427getstr(buf, cnt, err)
428 char *buf;
429 int cnt;
430 char *err;
431{
432 char c;
433
434 do {
435 if (read(0, &c, 1) != 1)
436 exit(1);
437 if (--cnt < 0) {
438 printf("%s too long\r\n", err);
439 exit(1);
440 }
441 *buf++ = c;
442 } while (c != 0);
443}
4f8d3876
BJ
444
445logerr(fmt, a1, a2, a3)
446 char *fmt, *a1, *a2, *a3;
447{
448
449}