login.c revision 1.22 1 1.22 lukem /* $NetBSD: login.c,v 1.22 1997/06/29 02:38:25 lukem Exp $ */
2 1.12 jtc
3 1.1 cgd /*-
4 1.12 jtc * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
5 1.10 brezak * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.10 brezak static char copyright[] =
38 1.12 jtc "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
39 1.10 brezak The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.12 jtc #if 0
44 1.12 jtc static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
45 1.12 jtc #endif
46 1.22 lukem static char rcsid[] = "$NetBSD: login.c,v 1.22 1997/06/29 02:38:25 lukem Exp $";
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * login [ name ]
51 1.1 cgd * login -h hostname (for telnetd, etc.)
52 1.1 cgd * login -f name (for pre-authenticated login: datakit, xterm, etc.)
53 1.1 cgd */
54 1.1 cgd
55 1.1 cgd #include <sys/param.h>
56 1.1 cgd #include <sys/stat.h>
57 1.1 cgd #include <sys/time.h>
58 1.1 cgd #include <sys/resource.h>
59 1.1 cgd #include <sys/file.h>
60 1.1 cgd
61 1.12 jtc #include <err.h>
62 1.5 cgd #include <errno.h>
63 1.1 cgd #include <grp.h>
64 1.1 cgd #include <pwd.h>
65 1.12 jtc #include <setjmp.h>
66 1.12 jtc #include <signal.h>
67 1.19 veego #include <stdio.h>
68 1.18 mikel #include <skey.h>
69 1.5 cgd #include <stdlib.h>
70 1.1 cgd #include <string.h>
71 1.12 jtc #include <syslog.h>
72 1.12 jtc #include <ttyent.h>
73 1.12 jtc #include <tzfile.h>
74 1.12 jtc #include <unistd.h>
75 1.12 jtc #include <utmp.h>
76 1.13 jtc #include <util.h>
77 1.12 jtc
78 1.1 cgd #include "pathnames.h"
79 1.1 cgd
80 1.5 cgd void badlogin __P((char *));
81 1.5 cgd void checknologin __P((void));
82 1.5 cgd void dolastlog __P((int));
83 1.5 cgd void getloginname __P((void));
84 1.5 cgd void motd __P((void));
85 1.5 cgd int rootterm __P((char *));
86 1.5 cgd void sigint __P((int));
87 1.5 cgd void sleepexit __P((int));
88 1.5 cgd char *stypeof __P((char *));
89 1.5 cgd void timedout __P((int));
90 1.9 deraadt int pwcheck __P((char *, char *, char *, char *));
91 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
92 1.5 cgd int klogin __P((struct passwd *, char *, char *, char *));
93 1.12 jtc void kdestroy __P((void));
94 1.12 jtc void dofork __P((void));
95 1.5 cgd #endif
96 1.5 cgd
97 1.12 jtc extern void login __P((struct utmp *));
98 1.12 jtc
99 1.1 cgd #define TTYGRPNAME "tty" /* name of group to own ttys */
100 1.1 cgd
101 1.1 cgd /*
102 1.1 cgd * This bounds the time given to login. Not a define so it can
103 1.1 cgd * be patched on machines where it's too small.
104 1.1 cgd */
105 1.12 jtc u_int timeout = 300;
106 1.5 cgd
107 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
108 1.1 cgd int notickets = 1;
109 1.1 cgd char *instance;
110 1.12 jtc char *krbtkfile_env;
111 1.1 cgd int authok;
112 1.1 cgd #endif
113 1.1 cgd
114 1.1 cgd struct passwd *pwd;
115 1.1 cgd int failures;
116 1.1 cgd char term[64], *envinit[1], *hostname, *username, *tty;
117 1.20 lukem #ifdef SKEY
118 1.20 lukem int used_skey = 0;
119 1.20 lukem int require_skey = 0;
120 1.20 lukem char skeypw[] = "s/key";
121 1.20 lukem #endif
122 1.1 cgd
123 1.5 cgd int
124 1.1 cgd main(argc, argv)
125 1.1 cgd int argc;
126 1.5 cgd char *argv[];
127 1.1 cgd {
128 1.5 cgd extern char **environ;
129 1.5 cgd struct group *gr;
130 1.5 cgd struct stat st;
131 1.5 cgd struct timeval tp;
132 1.5 cgd struct utmp utmp;
133 1.12 jtc int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
134 1.12 jtc uid_t uid;
135 1.20 lukem char *domain, *p, *salt, *ttyn, *pwprompt;
136 1.1 cgd char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
137 1.1 cgd char localhost[MAXHOSTNAMELEN];
138 1.1 cgd
139 1.20 lukem tbuf[0] = '\0';
140 1.20 lukem rval = 0;
141 1.20 lukem pwprompt = NULL;
142 1.20 lukem
143 1.1 cgd (void)signal(SIGALRM, timedout);
144 1.12 jtc (void)alarm(timeout);
145 1.1 cgd (void)signal(SIGQUIT, SIG_IGN);
146 1.1 cgd (void)signal(SIGINT, SIG_IGN);
147 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, 0);
148 1.1 cgd
149 1.1 cgd openlog("login", LOG_ODELAY, LOG_AUTH);
150 1.1 cgd
151 1.1 cgd /*
152 1.1 cgd * -p is used by getty to tell login not to destroy the environment
153 1.5 cgd * -f is used to skip a second login authentication
154 1.1 cgd * -h is used by other servers to pass the name of the remote
155 1.1 cgd * host to login so that it may be placed in utmp and wtmp
156 1.20 lukem * -s is used to force use of S/Key or equivalent.
157 1.1 cgd */
158 1.1 cgd domain = NULL;
159 1.1 cgd if (gethostname(localhost, sizeof(localhost)) < 0)
160 1.1 cgd syslog(LOG_ERR, "couldn't get local hostname: %m");
161 1.1 cgd else
162 1.12 jtc domain = strchr(localhost, '.');
163 1.1 cgd
164 1.1 cgd fflag = hflag = pflag = 0;
165 1.1 cgd uid = getuid();
166 1.20 lukem while ((ch = getopt(argc, argv, "fh:ps")) != EOF)
167 1.1 cgd switch (ch) {
168 1.1 cgd case 'f':
169 1.1 cgd fflag = 1;
170 1.1 cgd break;
171 1.1 cgd case 'h':
172 1.12 jtc if (uid)
173 1.12 jtc errx(1, "-h option: %s", strerror(EPERM));
174 1.1 cgd hflag = 1;
175 1.12 jtc if (domain && (p = strchr(optarg, '.')) &&
176 1.1 cgd strcasecmp(p, domain) == 0)
177 1.1 cgd *p = 0;
178 1.1 cgd hostname = optarg;
179 1.1 cgd break;
180 1.1 cgd case 'p':
181 1.1 cgd pflag = 1;
182 1.1 cgd break;
183 1.20 lukem case 's':
184 1.20 lukem #ifdef SKEY
185 1.20 lukem /*
186 1.20 lukem * If -s given twice, use S/Key or no login
187 1.20 lukem * otherwise just insist on S/Key if user has one.
188 1.20 lukem */
189 1.20 lukem require_skey++;
190 1.20 lukem #endif
191 1.20 lukem break;
192 1.1 cgd case '?':
193 1.1 cgd default:
194 1.1 cgd if (!uid)
195 1.1 cgd syslog(LOG_ERR, "invalid flag %c", ch);
196 1.1 cgd (void)fprintf(stderr,
197 1.1 cgd "usage: login [-fp] [-h hostname] [username]\n");
198 1.1 cgd exit(1);
199 1.1 cgd }
200 1.1 cgd argc -= optind;
201 1.1 cgd argv += optind;
202 1.5 cgd
203 1.1 cgd if (*argv) {
204 1.1 cgd username = *argv;
205 1.1 cgd ask = 0;
206 1.1 cgd } else
207 1.1 cgd ask = 1;
208 1.1 cgd
209 1.1 cgd for (cnt = getdtablesize(); cnt > 2; cnt--)
210 1.5 cgd (void)close(cnt);
211 1.1 cgd
212 1.5 cgd ttyn = ttyname(STDIN_FILENO);
213 1.1 cgd if (ttyn == NULL || *ttyn == '\0') {
214 1.5 cgd (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
215 1.1 cgd ttyn = tname;
216 1.1 cgd }
217 1.18 mikel if ((tty = strrchr(ttyn, '/')))
218 1.1 cgd ++tty;
219 1.1 cgd else
220 1.1 cgd tty = ttyn;
221 1.1 cgd
222 1.1 cgd for (cnt = 0;; ask = 1) {
223 1.20 lukem #ifdef SKEY
224 1.20 lukem used_skey = 0;
225 1.20 lukem #endif
226 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
227 1.10 brezak kdestroy();
228 1.10 brezak #endif
229 1.1 cgd if (ask) {
230 1.1 cgd fflag = 0;
231 1.1 cgd getloginname();
232 1.1 cgd }
233 1.5 cgd rootlogin = 0;
234 1.1 cgd #ifdef KERBEROS
235 1.12 jtc if ((instance = strchr(username, '.')) != NULL) {
236 1.1 cgd if (strncmp(instance, ".root", 5) == 0)
237 1.5 cgd rootlogin = 1;
238 1.1 cgd *instance++ = '\0';
239 1.5 cgd } else
240 1.1 cgd instance = "";
241 1.1 cgd #endif
242 1.10 brezak #ifdef KERBEROS5
243 1.12 jtc if ((instance = strchr(username, '/')) != NULL) {
244 1.10 brezak if (strncmp(instance, "/root", 5) == 0)
245 1.10 brezak rootlogin = 1;
246 1.10 brezak *instance++ = '\0';
247 1.10 brezak } else
248 1.10 brezak instance = "";
249 1.10 brezak #endif
250 1.16 sommerfe if (strlen(username) > MAXLOGNAME)
251 1.16 sommerfe username[MAXLOGNAME] = '\0';
252 1.1 cgd
253 1.1 cgd /*
254 1.1 cgd * Note if trying multiple user names; log failures for
255 1.1 cgd * previous user name, but don't bother logging one failure
256 1.1 cgd * for nonexistent name (mistyped username).
257 1.1 cgd */
258 1.1 cgd if (failures && strcmp(tbuf, username)) {
259 1.1 cgd if (failures > (pwd ? 0 : 1))
260 1.1 cgd badlogin(tbuf);
261 1.1 cgd failures = 0;
262 1.1 cgd }
263 1.17 mrg (void)strncpy(tbuf, username, sizeof(tbuf) - 1);
264 1.1 cgd
265 1.18 mikel if ((pwd = getpwnam(username)))
266 1.1 cgd salt = pwd->pw_passwd;
267 1.1 cgd else
268 1.1 cgd salt = "xx";
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * if we have a valid account name, and it doesn't have a
272 1.1 cgd * password, or the -f option was specified and the caller
273 1.1 cgd * is root or the caller isn't changing their uid, don't
274 1.1 cgd * authenticate.
275 1.1 cgd */
276 1.7 mycroft if (pwd) {
277 1.7 mycroft if (pwd->pw_uid == 0)
278 1.7 mycroft rootlogin = 1;
279 1.7 mycroft
280 1.8 mycroft if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
281 1.7 mycroft /* already authenticated */
282 1.7 mycroft break;
283 1.7 mycroft } else if (pwd->pw_passwd[0] == '\0') {
284 1.7 mycroft /* pretend password okay */
285 1.7 mycroft rval = 0;
286 1.7 mycroft goto ttycheck;
287 1.7 mycroft }
288 1.7 mycroft }
289 1.7 mycroft
290 1.1 cgd fflag = 0;
291 1.1 cgd
292 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -4);
293 1.1 cgd
294 1.20 lukem #ifdef SKEY
295 1.20 lukem p = NULL;
296 1.20 lukem pwprompt = "Password:";
297 1.20 lukem
298 1.20 lukem if (require_skey > 1) /* -s -s */
299 1.20 lukem p = skeypw;
300 1.20 lukem else if (skey_haskey(username) == 0) {
301 1.20 lukem if (require_skey > 0) /* -s */
302 1.20 lukem p = skeypw;
303 1.20 lukem else {
304 1.20 lukem extern char *skey_keyinfo();
305 1.20 lukem static char skprompt[80];
306 1.20 lukem char *skinfo = skey_keyinfo(username);
307 1.20 lukem
308 1.20 lukem (void)snprintf(skprompt, sizeof(skprompt)-1,
309 1.20 lukem "Password [%s]:", skinfo);
310 1.20 lukem pwprompt = skprompt;
311 1.20 lukem }
312 1.20 lukem }
313 1.20 lukem if (p == NULL)
314 1.20 lukem #endif
315 1.20 lukem p = getpass(pwprompt);
316 1.1 cgd
317 1.1 cgd if (pwd) {
318 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
319 1.1 cgd rval = klogin(pwd, instance, localhost, p);
320 1.12 jtc if (rval != 0 && rootlogin && pwd->pw_uid != 0)
321 1.12 jtc rootlogin = 0;
322 1.1 cgd if (rval == 0)
323 1.1 cgd authok = 1;
324 1.10 brezak else if (rval == 1) {
325 1.10 brezak if (pwd->pw_uid != 0)
326 1.10 brezak rootlogin = 0;
327 1.9 deraadt rval = pwcheck(username, p, salt, pwd->pw_passwd);
328 1.10 brezak }
329 1.1 cgd #else
330 1.9 deraadt rval = pwcheck(username, p, salt, pwd->pw_passwd);
331 1.1 cgd #endif
332 1.1 cgd }
333 1.20 lukem #ifdef SKEY
334 1.20 lukem if (used_skey == 0 && p != skeypw)
335 1.20 lukem #endif
336 1.20 lukem memset(p, 0, strlen(p));
337 1.1 cgd
338 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, 0);
339 1.1 cgd
340 1.7 mycroft ttycheck:
341 1.1 cgd /*
342 1.1 cgd * If trying to log in as root without Kerberos,
343 1.1 cgd * but with insecure terminal, refuse the login attempt.
344 1.1 cgd */
345 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
346 1.1 cgd if (authok == 0)
347 1.1 cgd #endif
348 1.7 mycroft if (pwd && !rval && rootlogin && !rootterm(tty)) {
349 1.1 cgd (void)fprintf(stderr,
350 1.1 cgd "%s login refused on this terminal.\n",
351 1.1 cgd pwd->pw_name);
352 1.1 cgd if (hostname)
353 1.1 cgd syslog(LOG_NOTICE,
354 1.1 cgd "LOGIN %s REFUSED FROM %s ON TTY %s",
355 1.1 cgd pwd->pw_name, hostname, tty);
356 1.1 cgd else
357 1.1 cgd syslog(LOG_NOTICE,
358 1.1 cgd "LOGIN %s REFUSED ON TTY %s",
359 1.1 cgd pwd->pw_name, tty);
360 1.1 cgd continue;
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd if (pwd && !rval)
364 1.1 cgd break;
365 1.1 cgd
366 1.1 cgd (void)printf("Login incorrect\n");
367 1.1 cgd failures++;
368 1.1 cgd /* we allow 10 tries, but after 3 we start backing off */
369 1.1 cgd if (++cnt > 3) {
370 1.1 cgd if (cnt >= 10) {
371 1.1 cgd badlogin(username);
372 1.1 cgd sleepexit(1);
373 1.1 cgd }
374 1.1 cgd sleep((u_int)((cnt - 3) * 5));
375 1.1 cgd }
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /* committed to login -- turn off timeout */
379 1.1 cgd (void)alarm((u_int)0);
380 1.1 cgd
381 1.1 cgd endpwent();
382 1.1 cgd
383 1.1 cgd /* if user not super-user, check for disabled logins */
384 1.1 cgd if (!rootlogin)
385 1.1 cgd checknologin();
386 1.1 cgd
387 1.1 cgd if (chdir(pwd->pw_dir) < 0) {
388 1.1 cgd (void)printf("No home directory %s!\n", pwd->pw_dir);
389 1.1 cgd if (chdir("/"))
390 1.1 cgd exit(0);
391 1.1 cgd pwd->pw_dir = "/";
392 1.1 cgd (void)printf("Logging in with home = \"/\".\n");
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
396 1.1 cgd
397 1.1 cgd if (pwd->pw_change || pwd->pw_expire)
398 1.1 cgd (void)gettimeofday(&tp, (struct timezone *)NULL);
399 1.1 cgd if (pwd->pw_change)
400 1.1 cgd if (tp.tv_sec >= pwd->pw_change) {
401 1.1 cgd (void)printf("Sorry -- your password has expired.\n");
402 1.1 cgd sleepexit(1);
403 1.1 cgd } else if (pwd->pw_change - tp.tv_sec <
404 1.21 lukem _PASSWORD_WARNDAYS * SECSPERDAY && !quietlog)
405 1.1 cgd (void)printf("Warning: your password expires on %s",
406 1.5 cgd ctime(&pwd->pw_change));
407 1.1 cgd if (pwd->pw_expire)
408 1.1 cgd if (tp.tv_sec >= pwd->pw_expire) {
409 1.1 cgd (void)printf("Sorry -- your account has expired.\n");
410 1.1 cgd sleepexit(1);
411 1.1 cgd } else if (pwd->pw_expire - tp.tv_sec <
412 1.21 lukem _PASSWORD_WARNDAYS * SECSPERDAY && !quietlog)
413 1.1 cgd (void)printf("Warning: your account expires on %s",
414 1.1 cgd ctime(&pwd->pw_expire));
415 1.1 cgd
416 1.5 cgd /* Nothing else left to fail -- really log in. */
417 1.12 jtc memset((void *)&utmp, 0, sizeof(utmp));
418 1.5 cgd (void)time(&utmp.ut_time);
419 1.5 cgd (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
420 1.5 cgd if (hostname)
421 1.5 cgd (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
422 1.5 cgd (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
423 1.5 cgd login(&utmp);
424 1.1 cgd
425 1.1 cgd dolastlog(quietlog);
426 1.1 cgd
427 1.1 cgd (void)chown(ttyn, pwd->pw_uid,
428 1.1 cgd (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
429 1.15 gwr
430 1.15 gwr if (ttyaction(ttyn, "login", pwd->pw_name))
431 1.15 gwr (void)printf("Warning: ttyaction failed.\n");
432 1.15 gwr
433 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
434 1.10 brezak /* Fork so that we can call kdestroy */
435 1.10 brezak if (krbtkfile_env)
436 1.10 brezak dofork();
437 1.10 brezak #endif
438 1.1 cgd (void)setgid(pwd->pw_gid);
439 1.1 cgd
440 1.1 cgd initgroups(username, pwd->pw_gid);
441 1.1 cgd
442 1.1 cgd if (*pwd->pw_shell == '\0')
443 1.1 cgd pwd->pw_shell = _PATH_BSHELL;
444 1.1 cgd
445 1.5 cgd /* Destroy environment unless user has requested its preservation. */
446 1.1 cgd if (!pflag)
447 1.1 cgd environ = envinit;
448 1.1 cgd (void)setenv("HOME", pwd->pw_dir, 1);
449 1.1 cgd (void)setenv("SHELL", pwd->pw_shell, 1);
450 1.1 cgd if (term[0] == '\0')
451 1.5 cgd (void)strncpy(term, stypeof(tty), sizeof(term));
452 1.1 cgd (void)setenv("TERM", term, 0);
453 1.1 cgd (void)setenv("LOGNAME", pwd->pw_name, 1);
454 1.1 cgd (void)setenv("USER", pwd->pw_name, 1);
455 1.1 cgd (void)setenv("PATH", _PATH_DEFPATH, 0);
456 1.1 cgd #ifdef KERBEROS
457 1.1 cgd if (krbtkfile_env)
458 1.1 cgd (void)setenv("KRBTKFILE", krbtkfile_env, 1);
459 1.1 cgd #endif
460 1.10 brezak #ifdef KERBEROS5
461 1.10 brezak if (krbtkfile_env)
462 1.10 brezak (void)setenv("KRB5CCNAME", krbtkfile_env, 1);
463 1.10 brezak #endif
464 1.1 cgd
465 1.12 jtc if (tty[sizeof("tty")-1] == 'd')
466 1.12 jtc syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
467 1.12 jtc
468 1.5 cgd /* If fflag is on, assume caller/authenticator has logged root login. */
469 1.1 cgd if (rootlogin && fflag == 0)
470 1.1 cgd if (hostname)
471 1.1 cgd syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
472 1.1 cgd username, tty, hostname);
473 1.1 cgd else
474 1.1 cgd syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
475 1.1 cgd
476 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
477 1.1 cgd if (!quietlog && notickets == 1)
478 1.1 cgd (void)printf("Warning: no Kerberos tickets issued.\n");
479 1.1 cgd #endif
480 1.1 cgd
481 1.1 cgd if (!quietlog) {
482 1.10 brezak (void)printf("%s\n\t%s %s\n\n",
483 1.12 jtc "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994",
484 1.10 brezak "The Regents of the University of California. ",
485 1.10 brezak "All rights reserved.");
486 1.1 cgd motd();
487 1.5 cgd (void)snprintf(tbuf,
488 1.5 cgd sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
489 1.1 cgd if (stat(tbuf, &st) == 0 && st.st_size != 0)
490 1.1 cgd (void)printf("You have %smail.\n",
491 1.1 cgd (st.st_mtime > st.st_atime) ? "new " : "");
492 1.1 cgd }
493 1.1 cgd
494 1.1 cgd (void)signal(SIGALRM, SIG_DFL);
495 1.1 cgd (void)signal(SIGQUIT, SIG_DFL);
496 1.1 cgd (void)signal(SIGINT, SIG_DFL);
497 1.1 cgd (void)signal(SIGTSTP, SIG_IGN);
498 1.1 cgd
499 1.1 cgd tbuf[0] = '-';
500 1.17 mrg (void)strncpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
501 1.17 mrg p + 1 : pwd->pw_shell, sizeof(tbuf) - 2);
502 1.1 cgd
503 1.1 cgd if (setlogin(pwd->pw_name) < 0)
504 1.1 cgd syslog(LOG_ERR, "setlogin() failure: %m");
505 1.1 cgd
506 1.5 cgd /* Discard permissions last so can't get killed and drop core. */
507 1.1 cgd if (rootlogin)
508 1.1 cgd (void) setuid(0);
509 1.1 cgd else
510 1.1 cgd (void) setuid(pwd->pw_uid);
511 1.1 cgd
512 1.1 cgd execlp(pwd->pw_shell, tbuf, 0);
513 1.12 jtc err(1, "%s", pwd->pw_shell);
514 1.9 deraadt }
515 1.9 deraadt
516 1.9 deraadt int
517 1.9 deraadt pwcheck(user, p, salt, passwd)
518 1.9 deraadt char *user, *p, *salt, *passwd;
519 1.9 deraadt {
520 1.20 lukem int sts = strcmp(crypt(p, salt), passwd);
521 1.20 lukem
522 1.9 deraadt #ifdef SKEY
523 1.20 lukem if (sts) { /* wasn't passwd */
524 1.20 lukem if (skey_haskey(user)) {
525 1.22 lukem if (strcasecmp(p, skeypw) == 0)
526 1.22 lukem return 1;
527 1.20 lukem } else {
528 1.20 lukem if (strcasecmp(p, skeypw) == 0) {
529 1.20 lukem sts = skey_authenticate(user);
530 1.20 lukem } else {
531 1.20 lukem if ((sts = skey_passcheck(user, p)) != -1) {
532 1.20 lukem sts = 0; /* ok */
533 1.20 lukem }
534 1.20 lukem }
535 1.20 lukem }
536 1.20 lukem if (sts == 0)
537 1.20 lukem used_skey = 1;
538 1.20 lukem }
539 1.9 deraadt #endif
540 1.20 lukem return sts;
541 1.1 cgd }
542 1.1 cgd
543 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
544 1.16 sommerfe #define NBUFSIZ (MAXLOGNAME + 1 + 5) /* .root suffix */
545 1.1 cgd #else
546 1.16 sommerfe #define NBUFSIZ (MAXLOGNAME + 1)
547 1.10 brezak #endif
548 1.10 brezak
549 1.10 brezak #if defined(KERBEROS) || defined(KERBEROS5)
550 1.10 brezak /*
551 1.10 brezak * This routine handles cleanup stuff, and the like.
552 1.10 brezak * It exists only in the child process.
553 1.10 brezak */
554 1.10 brezak #include <sys/wait.h>
555 1.10 brezak void
556 1.10 brezak dofork()
557 1.10 brezak {
558 1.10 brezak int child;
559 1.10 brezak
560 1.10 brezak if (!(child = fork()))
561 1.10 brezak return; /* Child process */
562 1.10 brezak
563 1.10 brezak /* Setup stuff? This would be things we could do in parallel with login */
564 1.10 brezak (void) chdir("/"); /* Let's not keep the fs busy... */
565 1.10 brezak
566 1.10 brezak /* If we're the parent, watch the child until it dies */
567 1.10 brezak while (wait(0) != child)
568 1.10 brezak ;
569 1.10 brezak
570 1.10 brezak /* Cleanup stuff */
571 1.10 brezak /* Run kdestroy to destroy tickets */
572 1.10 brezak kdestroy();
573 1.10 brezak
574 1.10 brezak /* Leave */
575 1.10 brezak exit(0);
576 1.10 brezak }
577 1.1 cgd #endif
578 1.1 cgd
579 1.5 cgd void
580 1.1 cgd getloginname()
581 1.1 cgd {
582 1.12 jtc int ch;
583 1.12 jtc char *p;
584 1.1 cgd static char nbuf[NBUFSIZ];
585 1.1 cgd
586 1.1 cgd for (;;) {
587 1.1 cgd (void)printf("login: ");
588 1.1 cgd for (p = nbuf; (ch = getchar()) != '\n'; ) {
589 1.1 cgd if (ch == EOF) {
590 1.1 cgd badlogin(username);
591 1.1 cgd exit(0);
592 1.1 cgd }
593 1.1 cgd if (p < nbuf + (NBUFSIZ - 1))
594 1.1 cgd *p++ = ch;
595 1.1 cgd }
596 1.1 cgd if (p > nbuf)
597 1.1 cgd if (nbuf[0] == '-')
598 1.1 cgd (void)fprintf(stderr,
599 1.1 cgd "login names may not start with '-'.\n");
600 1.1 cgd else {
601 1.1 cgd *p = '\0';
602 1.1 cgd username = nbuf;
603 1.1 cgd break;
604 1.1 cgd }
605 1.1 cgd }
606 1.1 cgd }
607 1.1 cgd
608 1.5 cgd int
609 1.1 cgd rootterm(ttyn)
610 1.1 cgd char *ttyn;
611 1.1 cgd {
612 1.1 cgd struct ttyent *t;
613 1.1 cgd
614 1.12 jtc return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
615 1.1 cgd }
616 1.1 cgd
617 1.1 cgd jmp_buf motdinterrupt;
618 1.1 cgd
619 1.5 cgd void
620 1.1 cgd motd()
621 1.1 cgd {
622 1.12 jtc int fd, nchars;
623 1.1 cgd sig_t oldint;
624 1.1 cgd char tbuf[8192];
625 1.1 cgd
626 1.1 cgd if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
627 1.1 cgd return;
628 1.1 cgd oldint = signal(SIGINT, sigint);
629 1.1 cgd if (setjmp(motdinterrupt) == 0)
630 1.1 cgd while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
631 1.1 cgd (void)write(fileno(stdout), tbuf, nchars);
632 1.1 cgd (void)signal(SIGINT, oldint);
633 1.1 cgd (void)close(fd);
634 1.1 cgd }
635 1.1 cgd
636 1.5 cgd /* ARGSUSED */
637 1.1 cgd void
638 1.5 cgd sigint(signo)
639 1.5 cgd int signo;
640 1.1 cgd {
641 1.1 cgd longjmp(motdinterrupt, 1);
642 1.1 cgd }
643 1.1 cgd
644 1.5 cgd /* ARGSUSED */
645 1.5 cgd void
646 1.5 cgd timedout(signo)
647 1.5 cgd int signo;
648 1.5 cgd {
649 1.5 cgd (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
650 1.5 cgd exit(0);
651 1.5 cgd }
652 1.5 cgd
653 1.5 cgd void
654 1.1 cgd checknologin()
655 1.1 cgd {
656 1.12 jtc int fd, nchars;
657 1.1 cgd char tbuf[8192];
658 1.1 cgd
659 1.1 cgd if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
660 1.1 cgd while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
661 1.1 cgd (void)write(fileno(stdout), tbuf, nchars);
662 1.1 cgd sleepexit(0);
663 1.1 cgd }
664 1.1 cgd }
665 1.1 cgd
666 1.5 cgd void
667 1.1 cgd dolastlog(quiet)
668 1.1 cgd int quiet;
669 1.1 cgd {
670 1.1 cgd struct lastlog ll;
671 1.1 cgd int fd;
672 1.1 cgd
673 1.1 cgd if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
674 1.1 cgd (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
675 1.1 cgd if (!quiet) {
676 1.1 cgd if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
677 1.1 cgd ll.ll_time != 0) {
678 1.1 cgd (void)printf("Last login: %.*s ",
679 1.1 cgd 24-5, (char *)ctime(&ll.ll_time));
680 1.1 cgd if (*ll.ll_host != '\0')
681 1.1 cgd (void)printf("from %.*s\n",
682 1.5 cgd (int)sizeof(ll.ll_host),
683 1.5 cgd ll.ll_host);
684 1.1 cgd else
685 1.1 cgd (void)printf("on %.*s\n",
686 1.5 cgd (int)sizeof(ll.ll_line),
687 1.5 cgd ll.ll_line);
688 1.1 cgd }
689 1.1 cgd (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
690 1.1 cgd }
691 1.12 jtc memset((void *)&ll, 0, sizeof(ll));
692 1.1 cgd (void)time(&ll.ll_time);
693 1.5 cgd (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
694 1.1 cgd if (hostname)
695 1.5 cgd (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
696 1.1 cgd (void)write(fd, (char *)&ll, sizeof(ll));
697 1.1 cgd (void)close(fd);
698 1.1 cgd }
699 1.1 cgd }
700 1.1 cgd
701 1.5 cgd void
702 1.1 cgd badlogin(name)
703 1.1 cgd char *name;
704 1.1 cgd {
705 1.1 cgd if (failures == 0)
706 1.1 cgd return;
707 1.1 cgd if (hostname) {
708 1.1 cgd syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
709 1.1 cgd failures, failures > 1 ? "S" : "", hostname);
710 1.1 cgd syslog(LOG_AUTHPRIV|LOG_NOTICE,
711 1.1 cgd "%d LOGIN FAILURE%s FROM %s, %s",
712 1.1 cgd failures, failures > 1 ? "S" : "", hostname, name);
713 1.1 cgd } else {
714 1.1 cgd syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
715 1.1 cgd failures, failures > 1 ? "S" : "", tty);
716 1.1 cgd syslog(LOG_AUTHPRIV|LOG_NOTICE,
717 1.1 cgd "%d LOGIN FAILURE%s ON %s, %s",
718 1.1 cgd failures, failures > 1 ? "S" : "", tty, name);
719 1.1 cgd }
720 1.1 cgd }
721 1.1 cgd
722 1.1 cgd #undef UNKNOWN
723 1.1 cgd #define UNKNOWN "su"
724 1.1 cgd
725 1.1 cgd char *
726 1.1 cgd stypeof(ttyid)
727 1.1 cgd char *ttyid;
728 1.1 cgd {
729 1.1 cgd struct ttyent *t;
730 1.1 cgd
731 1.12 jtc return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
732 1.1 cgd }
733 1.1 cgd
734 1.5 cgd void
735 1.1 cgd sleepexit(eval)
736 1.1 cgd int eval;
737 1.1 cgd {
738 1.12 jtc (void)sleep(5);
739 1.1 cgd exit(eval);
740 1.1 cgd }
741