date and time created 83/02/11 15:44:29 by rrh
[unix-history] / usr / src / usr.bin / login / login.c.1
CommitLineData
3479a16a 1static char *sccsid = "@(#)login.c.1 4.20 82/12/21";
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) {
108 char ahost[32];
109 while (fgets(ahost, sizeof (ahost), hostf)) {
110 char *user;
111 if (index(ahost, '\n'))
112 *index(ahost, '\n') = 0;
113 user = index(ahost, ' ');
114 if (user)
115 *user++ = 0;
116 if (!strcmp(rhost, ahost) &&
4f8d3876
BJ
117 !strcmp(rusername, user ? user : lusername)) {
118 fclose(hostf);
b4389814 119 goto normal;
4f8d3876 120 }
b4389814
BJ
121 }
122 fclose(hostf);
123 }
124 if (first == 1) {
125 first = 0;
126 if (chdir(pwd->pw_dir) < 0)
127 goto again;
128 hostf = fopen(".rhosts", "r");
129 goto again;
130 }
4f8d3876 131abnormal:
b4389814
BJ
132 rhost = 0;
133 rflag = -1;
86eb6c9e 134 }
b4389814 135normal:
3479a16a 136 ioctl(0, TIOCLSET, &zero); /* XXX */
c95ed2b2 137 ioctl(0, TIOCNXCL, 0);
4f8d3876
BJ
138 ioctl(0, FIONBIO, &zero);
139 ioctl(0, FIOASYNC, &zero);
3479a16a 140 ioctl(0, TIOCGETP, &ttyb); /* XXX */
86eb6c9e
BJ
141 if (rflag) {
142 char *cp = index(term, '/');
143 if (cp) {
144 int i;
145 *cp++ = 0;
146 for (i = 0; i < NSPEEDS; i++)
147 if (!strcmp(speeds[i], cp)) {
148 ttyb.sg_ispeed = ttyb.sg_ospeed = i;
149 break;
150 }
151 }
152 ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS;
153 }
3479a16a
SL
154 ioctl(0, TIOCSETP, &ttyb); /* XXX */
155 ioctl(0, TIOCCSET, &tc);
88a01c09
BJ
156 for (t=3; t<20; t++)
157 close(t);
158 ttyn = ttyname(0);
f570e1ff 159 if (ttyn==(char *)0)
88a01c09 160 ttyn = "/dev/tty??";
f570e1ff
BJ
161 do {
162 ldisc = 0;
c95ed2b2 163 ioctl(0, TIOCSETD, &ldisc);
f570e1ff
BJ
164 invalid = FALSE;
165 SCPYN(utmp.ut_name, "");
166 if (argc>1) {
167 SCPYN(utmp.ut_name, argv[1]);
168 argc = 0;
169 }
4f8d3876 170 if (rflag) {
3479a16a 171 SCPYN(utmp.ut_name, lusername);
4f8d3876
BJ
172 if (rflag == -1)
173 rflag = 0;
174 } else
b4389814
BJ
175 while (utmp.ut_name[0] == '\0') {
176 namep = utmp.ut_name;
177 { char hostname[32];
178 gethostname(hostname, sizeof (hostname));
179 printf("%s login: ", hostname); }
180 while ((c = getchar()) != '\n') {
181 if (c == ' ')
182 c = '_';
183 if (c == EOF)
184 exit(0);
185 if (namep < utmp.ut_name+NMAX)
186 *namep++ = c;
187 }
f570e1ff 188 }
b4389814
BJ
189 if (rhost == 0) {
190 setpwent();
191 if ((pwd = getpwnam(utmp.ut_name)) == NULL)
192 pwd = &nouser;
193 endpwent();
f570e1ff 194 }
f570e1ff
BJ
195 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
196 ldisc = NTTYDISC;
197 ioctl(0, TIOCSETD, &ldisc);
198 }
b4389814
BJ
199 if (rhost == 0) {
200 if (*pwd->pw_passwd != '\0') {
201 char *pp;
202 nice(-4);
203 if (rflag == 0)
204 pp = getpass("Password:");
205 else
206 pp = rpassword;
207 namep = crypt(pp,pwd->pw_passwd);
208 nice(4);
209 if (strcmp(namep, pwd->pw_passwd))
210 invalid = TRUE;
211 }
f570e1ff
BJ
212 }
213 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
214 /* logins are disabled except for root */
215 while ((c = getc(nlfd)) != EOF)
216 putchar(c);
217 fflush(stdout);
218 sleep(5);
219 exit(0);
220 }
221 if (!invalid && pwd->pw_uid == 0 &&
222 !rootterm(ttyn+sizeof("/dev/")-1)) {
4f8d3876
BJ
223 logerr("ROOT LOGIN REFUSED %s",
224 ttyn+sizeof("/dev/")-1);
f570e1ff
BJ
225 invalid = TRUE;
226 }
227 if (invalid) {
88a01c09 228 printf("Login incorrect\n");
4f8d3876
BJ
229 if (ttyn[sizeof("/dev/tty")-1] == 'd')
230 logerr("BADDIALUP %s %s\n",
231 ttyn+sizeof("/dev/")-1, utmp.ut_name);
88a01c09 232 }
f570e1ff
BJ
233 if (*pwd->pw_shell == '\0')
234 pwd->pw_shell = "/bin/sh";
235 i = strlen(pwd->pw_shell);
236 if (chdir(pwd->pw_dir) < 0 && !invalid ) {
237 if (chdir("/") < 0) {
238 printf("No directory!\n");
239 invalid = TRUE;
240 } else {
241 printf("No directory! Logging in with home=/\n");
242 pwd->pw_dir = "/";
243 }
88a01c09 244 }
86eb6c9e
BJ
245 if (rflag && invalid)
246 exit(1);
f570e1ff 247 } while (invalid);
88a01c09 248
86eb6c9e 249
88a01c09
BJ
250 time(&utmp.ut_time);
251 t = ttyslot();
252 if (t>0 && (f = open("/etc/utmp", 1)) >= 0) {
253 lseek(f, (long)(t*sizeof(utmp)), 0);
254 SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
255 write(f, (char *)&utmp, sizeof(utmp));
256 close(f);
257 }
258 if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
259 lseek(f, 0L, 2);
260 write(f, (char *)&utmp, sizeof(utmp));
261 close(f);
262 }
4f8d3876 263 quietlog = 0;
f570e1ff 264 if (access(qlog, 0) == 0)
4f8d3876
BJ
265 quietlog = 1;
266 if ((f = open(lastlog, 2)) >= 0) {
f570e1ff
BJ
267 struct lastlog ll;
268
269 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
270 if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
271 ll.ll_time != 0) {
4f8d3876 272 if (quietlog == 0)
f570e1ff
BJ
273 printf("Last login: %.*s on %.*s\n"
274 , 24-5
275 , (char *) ctime(&ll.ll_time)
276 , sizeof(ll.ll_line)
277 , ll.ll_line
278 );
279 }
280 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
281 time(&ll.ll_time);
282 SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
283 write(f, (char *) &ll, sizeof ll);
284 close(f);
285 }
88a01c09 286 chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3479a16a 287 chmod(ttyn, 0622);
88a01c09 288 setgid(pwd->pw_gid);
e5321f7b
KM
289 strncpy(name, utmp.ut_name, NMAX);
290 name[NMAX] = '\0';
b1198826 291 initgroups(name, pwd->pw_gid);
88a01c09 292 setuid(pwd->pw_uid);
88a01c09
BJ
293 environ = envinit;
294 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
295 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
4f8d3876 296 if (term[strlen("TERM=")] == 0)
86eb6c9e 297 strncat(term, stypeof(ttyn), sizeof(term)-6);
f570e1ff 298 strncat(user, pwd->pw_name, sizeof(user)-6);
88a01c09
BJ
299 if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
300 namep = pwd->pw_shell;
301 else
302 namep++;
303 strcat(minusnam, namep);
304 alarm(0);
b4389814 305 umask(022);
4f8d3876
BJ
306 if (ttyn[sizeof("/dev/tty")-1] == 'd')
307 logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
308 if (!quietlog) {
f570e1ff
BJ
309 showmotd();
310 strcat(maildir, pwd->pw_name);
311 if (access(maildir,4)==0) {
312 struct stat statb;
313 stat(maildir, &statb);
314 if (statb.st_size)
315 printf("You have mail.\n");
316 }
317 }
318
88a01c09
BJ
319 signal(SIGQUIT, SIG_DFL);
320 signal(SIGINT, SIG_DFL);
5f87416f 321 signal(SIGTSTP, SIG_IGN);
88a01c09 322 execlp(pwd->pw_shell, minusnam, 0);
f570e1ff 323 perror(pwd->pw_shell);
88a01c09
BJ
324 printf("No shell\n");
325 exit(0);
326}
327
328int stopmotd;
329catch()
330{
1886582e 331
88a01c09
BJ
332 signal(SIGINT, SIG_IGN);
333 stopmotd++;
334}
335
f570e1ff 336rootterm(tty)
1886582e 337 char *tty;
f570e1ff
BJ
338{
339 register FILE *fd;
1886582e 340 char buf[100];
f570e1ff 341
1886582e
BJ
342 if (rflag)
343 return(1);
f570e1ff
BJ
344 if ((fd = fopen(securetty, "r")) == NULL)
345 return(1);
346 while (fgets(buf, sizeof buf, fd) != NULL) {
347 buf[strlen(buf)-1] = '\0';
348 if (strcmp(tty, buf) == 0) {
349 fclose(fd);
350 return(1);
351 }
352 }
353 fclose(fd);
354 return(0);
355}
356
88a01c09
BJ
357showmotd()
358{
359 FILE *mf;
360 register c;
361
362 signal(SIGINT, catch);
f570e1ff
BJ
363 if ((mf = fopen("/etc/motd","r")) != NULL) {
364 while ((c = getc(mf)) != EOF && stopmotd == 0)
88a01c09
BJ
365 putchar(c);
366 fclose(mf);
367 }
368 signal(SIGINT, SIG_IGN);
369}
370
f570e1ff 371#undef UNKNOWN
88a01c09
BJ
372#define UNKNOWN "su"
373
374char *
375stypeof(ttyid)
376char *ttyid;
377{
378 static char typebuf[16];
379 char buf[50];
380 register FILE *f;
381 register char *p, *t, *q;
382
383 if (ttyid == NULL)
384 return (UNKNOWN);
385 f = fopen("/etc/ttytype", "r");
386 if (f == NULL)
387 return (UNKNOWN);
388 /* split off end of name */
389 for (p = q = ttyid; *p != 0; p++)
390 if (*p == '/')
391 q = p + 1;
392
393 /* scan the file */
394 while (fgets(buf, sizeof buf, f) != NULL)
395 {
f570e1ff 396 for (t=buf; *t!=' ' && *t != '\t'; t++)
88a01c09
BJ
397 ;
398 *t++ = 0;
f570e1ff
BJ
399 while (*t == ' ' || *t == '\t')
400 t++;
88a01c09
BJ
401 for (p=t; *p>' '; p++)
402 ;
403 *p = 0;
404 if (strcmp(q,t)==0) {
405 strcpy(typebuf, buf);
406 fclose(f);
407 return (typebuf);
408 }
409 }
410 fclose (f);
411 return (UNKNOWN);
412}
86eb6c9e
BJ
413
414getstr(buf, cnt, err)
415 char *buf;
416 int cnt;
417 char *err;
418{
419 char c;
420
421 do {
422 if (read(0, &c, 1) != 1)
423 exit(1);
424 if (--cnt < 0) {
425 printf("%s too long\r\n", err);
426 exit(1);
427 }
428 *buf++ = c;
429 } while (c != 0);
430}
4f8d3876
BJ
431
432logerr(fmt, a1, a2, a3)
433 char *fmt, *a1, *a2, *a3;
434{
435
436}