login.c revision 1.95 1 1.95 christos /* $NetBSD: login.c,v 1.95 2007/05/09 01:56:25 christos 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.73 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.24 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.24 lukem __COPYRIGHT(
35 1.12 jtc "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\
36 1.24 lukem The Regents of the University of California. All rights reserved.\n");
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.12 jtc #if 0
41 1.12 jtc static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94";
42 1.12 jtc #endif
43 1.95 christos __RCSID("$NetBSD: login.c,v 1.95 2007/05/09 01:56:25 christos Exp $");
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * login [ name ]
48 1.1 cgd * login -h hostname (for telnetd, etc.)
49 1.1 cgd * login -f name (for pre-authenticated login: datakit, xterm, etc.)
50 1.1 cgd */
51 1.1 cgd
52 1.1 cgd #include <sys/param.h>
53 1.1 cgd #include <sys/stat.h>
54 1.1 cgd #include <sys/time.h>
55 1.1 cgd #include <sys/resource.h>
56 1.1 cgd #include <sys/file.h>
57 1.38 mrg #include <sys/wait.h>
58 1.67 christos #include <sys/socket.h>
59 1.1 cgd
60 1.12 jtc #include <err.h>
61 1.5 cgd #include <errno.h>
62 1.1 cgd #include <grp.h>
63 1.1 cgd #include <pwd.h>
64 1.12 jtc #include <setjmp.h>
65 1.12 jtc #include <signal.h>
66 1.19 veego #include <stdio.h>
67 1.5 cgd #include <stdlib.h>
68 1.1 cgd #include <string.h>
69 1.12 jtc #include <syslog.h>
70 1.36 kleink #include <time.h>
71 1.12 jtc #include <ttyent.h>
72 1.12 jtc #include <tzfile.h>
73 1.12 jtc #include <unistd.h>
74 1.95 christos #include <sysexits.h>
75 1.67 christos #ifdef SUPPORT_UTMP
76 1.12 jtc #include <utmp.h>
77 1.66 christos #endif
78 1.67 christos #ifdef SUPPORT_UTMPX
79 1.66 christos #include <utmpx.h>
80 1.66 christos #endif
81 1.13 jtc #include <util.h>
82 1.29 mycroft #ifdef SKEY
83 1.29 mycroft #include <skey.h>
84 1.29 mycroft #endif
85 1.25 mycroft #ifdef KERBEROS5
86 1.45 christos #include <krb5/krb5.h>
87 1.54 aidan #include <com_err.h>
88 1.25 mycroft #endif
89 1.48 mjl #ifdef LOGIN_CAP
90 1.48 mjl #include <login_cap.h>
91 1.48 mjl #endif
92 1.79 christos #include <vis.h>
93 1.25 mycroft
94 1.1 cgd #include "pathnames.h"
95 1.1 cgd
96 1.57 aidan #ifdef KERBEROS5
97 1.57 aidan int login_krb5_get_tickets = 1;
98 1.57 aidan int login_krb5_forwardable_tgt = 0;
99 1.57 aidan int login_krb5_retain_ccache = 0;
100 1.57 aidan #endif
101 1.57 aidan
102 1.81 xtraeme void badlogin(char *);
103 1.81 xtraeme void checknologin(char *);
104 1.67 christos #ifdef SUPPORT_UTMP
105 1.81 xtraeme static void doutmp(void);
106 1.81 xtraeme static void dolastlog(int);
107 1.66 christos #endif
108 1.67 christos #ifdef SUPPORT_UTMPX
109 1.81 xtraeme static void doutmpx(void);
110 1.81 xtraeme static void dolastlogx(int);
111 1.66 christos #endif
112 1.81 xtraeme static void update_db(int);
113 1.81 xtraeme void getloginname(void);
114 1.81 xtraeme void motd(char *);
115 1.81 xtraeme int rootterm(char *);
116 1.81 xtraeme void sigint(int);
117 1.81 xtraeme void sleepexit(int);
118 1.81 xtraeme const char *stypeof(const char *);
119 1.81 xtraeme void timedout(int);
120 1.44 aidan #ifdef KERBEROS5
121 1.81 xtraeme int k5login(struct passwd *, char *, char *, char *);
122 1.81 xtraeme void k5destroy(void);
123 1.81 xtraeme int k5_read_creds(char*);
124 1.81 xtraeme int k5_write_creds(void);
125 1.57 aidan #endif
126 1.89 wiz #if defined(KERBEROS5)
127 1.81 xtraeme void dofork(void);
128 1.44 aidan #endif
129 1.81 xtraeme void decode_ss(const char *);
130 1.81 xtraeme void usage(void);
131 1.12 jtc
132 1.1 cgd #define TTYGRPNAME "tty" /* name of group to own ttys */
133 1.1 cgd
134 1.50 mjl #define DEFAULT_BACKOFF 3
135 1.50 mjl #define DEFAULT_RETRIES 10
136 1.48 mjl
137 1.1 cgd /*
138 1.1 cgd * This bounds the time given to login. Not a define so it can
139 1.1 cgd * be patched on machines where it's too small.
140 1.1 cgd */
141 1.12 jtc u_int timeout = 300;
142 1.5 cgd
143 1.89 wiz #if defined(KERBEROS5)
144 1.1 cgd int notickets = 1;
145 1.1 cgd char *instance;
146 1.57 aidan int has_ccache = 0;
147 1.25 mycroft extern krb5_context kcontext;
148 1.44 aidan extern int have_forward;
149 1.57 aidan extern char *krb5tkfile_env;
150 1.60 thorpej extern int krb5_configured;
151 1.60 thorpej #endif
152 1.60 thorpej
153 1.89 wiz #if defined(KERBEROS5)
154 1.60 thorpej #define KERBEROS_CONFIGURED krb5_configured
155 1.25 mycroft #endif
156 1.1 cgd
157 1.1 cgd struct passwd *pwd;
158 1.79 christos int failures, have_ss;
159 1.70 itojun char term[64], *envinit[1], *hostname, *username, *tty, *nested;
160 1.66 christos struct timeval now;
161 1.66 christos struct sockaddr_storage ss;
162 1.1 cgd
163 1.77 lukem extern const char copyrightstr[];
164 1.34 thorpej
165 1.5 cgd int
166 1.80 xtraeme main(int argc, char *argv[])
167 1.1 cgd {
168 1.5 cgd extern char **environ;
169 1.5 cgd struct group *gr;
170 1.5 cgd struct stat st;
171 1.29 mycroft int ask, ch, cnt, fflag, hflag, pflag, sflag, quietlog, rootlogin, rval;
172 1.44 aidan int Fflag;
173 1.33 hubertf uid_t uid, saved_uid;
174 1.35 hubertf gid_t saved_gid, saved_gids[NGROUPS_MAX];
175 1.35 hubertf int nsaved_gids;
176 1.39 mycroft char *domain, *p, *ttyn, *pwprompt;
177 1.1 cgd char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
178 1.37 mrg char localhost[MAXHOSTNAMELEN + 1];
179 1.29 mycroft int need_chpass, require_chpass;
180 1.48 mjl int login_retries = DEFAULT_RETRIES,
181 1.48 mjl login_backoff = DEFAULT_BACKOFF;
182 1.48 mjl time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
183 1.25 mycroft #ifdef KERBEROS5
184 1.25 mycroft krb5_error_code kerror;
185 1.25 mycroft #endif
186 1.89 wiz #if defined(KERBEROS5)
187 1.57 aidan int got_tickets = 0;
188 1.57 aidan #endif
189 1.48 mjl #ifdef LOGIN_CAP
190 1.49 mjl char *shell = NULL;
191 1.48 mjl login_cap_t *lc = NULL;
192 1.48 mjl #endif
193 1.1 cgd
194 1.20 lukem tbuf[0] = '\0';
195 1.20 lukem rval = 0;
196 1.20 lukem pwprompt = NULL;
197 1.70 itojun nested = NULL;
198 1.29 mycroft need_chpass = require_chpass = 0;
199 1.20 lukem
200 1.1 cgd (void)signal(SIGALRM, timedout);
201 1.12 jtc (void)alarm(timeout);
202 1.1 cgd (void)signal(SIGQUIT, SIG_IGN);
203 1.1 cgd (void)signal(SIGINT, SIG_IGN);
204 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, 0);
205 1.1 cgd
206 1.63 lukem openlog("login", 0, LOG_AUTH);
207 1.1 cgd
208 1.1 cgd /*
209 1.1 cgd * -p is used by getty to tell login not to destroy the environment
210 1.5 cgd * -f is used to skip a second login authentication
211 1.66 christos * -h is used by other servers to pass the name of the remote host to
212 1.66 christos * login so that it may be placed in utmp/utmpx and wtmp/wtmpx
213 1.87 jnemeth * -a in addition to -h, a server may supply -a to pass the actual
214 1.79 christos * server address.
215 1.20 lukem * -s is used to force use of S/Key or equivalent.
216 1.1 cgd */
217 1.1 cgd domain = NULL;
218 1.1 cgd if (gethostname(localhost, sizeof(localhost)) < 0)
219 1.1 cgd syslog(LOG_ERR, "couldn't get local hostname: %m");
220 1.1 cgd else
221 1.12 jtc domain = strchr(localhost, '.');
222 1.37 mrg localhost[sizeof(localhost) - 1] = '\0';
223 1.1 cgd
224 1.44 aidan Fflag = fflag = hflag = pflag = sflag = 0;
225 1.79 christos have_ss = 0;
226 1.44 aidan #ifdef KERBEROS5
227 1.44 aidan have_forward = 0;
228 1.44 aidan #endif
229 1.1 cgd uid = getuid();
230 1.79 christos while ((ch = getopt(argc, argv, "a:Ffh:ps")) != -1)
231 1.1 cgd switch (ch) {
232 1.79 christos case 'a':
233 1.79 christos if (uid)
234 1.95 christos errx(EXIT_FAILURE, "-a option: %s", strerror(EPERM));
235 1.79 christos decode_ss(optarg);
236 1.82 christos #ifdef notdef
237 1.82 christos (void)sockaddr_snprintf(optarg,
238 1.82 christos sizeof(struct sockaddr_storage), "%a", (void *)&ss);
239 1.82 christos #endif
240 1.79 christos break;
241 1.44 aidan case 'F':
242 1.44 aidan Fflag = 1;
243 1.44 aidan /* FALLTHROUGH */
244 1.1 cgd case 'f':
245 1.1 cgd fflag = 1;
246 1.1 cgd break;
247 1.1 cgd case 'h':
248 1.12 jtc if (uid)
249 1.95 christos errx(EXIT_FAILURE, "-h option: %s", strerror(EPERM));
250 1.1 cgd hflag = 1;
251 1.79 christos #ifdef notdef
252 1.23 mikel if (domain && (p = strchr(optarg, '.')) != NULL &&
253 1.1 cgd strcasecmp(p, domain) == 0)
254 1.79 christos *p = '\0';
255 1.79 christos #endif
256 1.1 cgd hostname = optarg;
257 1.1 cgd break;
258 1.1 cgd case 'p':
259 1.1 cgd pflag = 1;
260 1.1 cgd break;
261 1.20 lukem case 's':
262 1.29 mycroft sflag = 1;
263 1.20 lukem break;
264 1.29 mycroft default:
265 1.1 cgd case '?':
266 1.79 christos usage();
267 1.79 christos break;
268 1.1 cgd }
269 1.82 christos
270 1.82 christos setproctitle(NULL);
271 1.1 cgd argc -= optind;
272 1.1 cgd argv += optind;
273 1.5 cgd
274 1.1 cgd if (*argv) {
275 1.1 cgd username = *argv;
276 1.1 cgd ask = 0;
277 1.1 cgd } else
278 1.1 cgd ask = 1;
279 1.1 cgd
280 1.82 christos #ifdef F_CLOSEM
281 1.82 christos (void)fcntl(3, F_CLOSEM, 0);
282 1.82 christos #else
283 1.1 cgd for (cnt = getdtablesize(); cnt > 2; cnt--)
284 1.5 cgd (void)close(cnt);
285 1.82 christos #endif
286 1.1 cgd
287 1.5 cgd ttyn = ttyname(STDIN_FILENO);
288 1.1 cgd if (ttyn == NULL || *ttyn == '\0') {
289 1.5 cgd (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
290 1.1 cgd ttyn = tname;
291 1.1 cgd }
292 1.82 christos if ((tty = strstr(ttyn, "/pts/")) != NULL)
293 1.82 christos ++tty;
294 1.82 christos else if ((tty = strrchr(ttyn, '/')) != NULL)
295 1.1 cgd ++tty;
296 1.1 cgd else
297 1.1 cgd tty = ttyn;
298 1.25 mycroft
299 1.70 itojun if (issetugid()) {
300 1.70 itojun nested = strdup(user_from_uid(getuid(), 0));
301 1.70 itojun if (nested == NULL) {
302 1.93 isaki syslog(LOG_ERR, "strdup: %m");
303 1.95 christos sleepexit(EXIT_FAILURE);
304 1.70 itojun }
305 1.70 itojun }
306 1.70 itojun
307 1.48 mjl #ifdef LOGIN_CAP
308 1.48 mjl /* Get "login-retries" and "login-backoff" from default class */
309 1.55 enami if ((lc = login_getclass(NULL)) != NULL) {
310 1.55 enami login_retries = (int)login_getcapnum(lc, "login-retries",
311 1.55 enami DEFAULT_RETRIES, DEFAULT_RETRIES);
312 1.55 enami login_backoff = (int)login_getcapnum(lc, "login-backoff",
313 1.55 enami DEFAULT_BACKOFF, DEFAULT_BACKOFF);
314 1.48 mjl login_close(lc);
315 1.48 mjl lc = NULL;
316 1.55 enami }
317 1.48 mjl #endif
318 1.48 mjl
319 1.25 mycroft #ifdef KERBEROS5
320 1.25 mycroft kerror = krb5_init_context(&kcontext);
321 1.25 mycroft if (kerror) {
322 1.61 thorpej /*
323 1.61 thorpej * If Kerberos is not configured, that is, we are
324 1.61 thorpej * not using Kerberos, do not log the error message.
325 1.61 thorpej * However, if Kerberos is configured, and the
326 1.61 thorpej * context init fails for some other reason, we need
327 1.61 thorpej * to issue a no tickets warning to the user when the
328 1.61 thorpej * login succeeds.
329 1.61 thorpej */
330 1.61 thorpej if (kerror != ENXIO) { /* XXX NetBSD-local Heimdal hack */
331 1.61 thorpej syslog(LOG_NOTICE,
332 1.61 thorpej "%s when initializing Kerberos context",
333 1.61 thorpej error_message(kerror));
334 1.61 thorpej krb5_configured = 1;
335 1.61 thorpej }
336 1.57 aidan login_krb5_get_tickets = 0;
337 1.58 aidan }
338 1.64 cgd #endif /* KERBEROS5 */
339 1.1 cgd
340 1.1 cgd for (cnt = 0;; ask = 1) {
341 1.57 aidan #if defined(KERBEROS5)
342 1.57 aidan if (login_krb5_get_tickets)
343 1.57 aidan k5destroy();
344 1.57 aidan #endif
345 1.1 cgd if (ask) {
346 1.1 cgd fflag = 0;
347 1.1 cgd getloginname();
348 1.1 cgd }
349 1.5 cgd rootlogin = 0;
350 1.10 brezak #ifdef KERBEROS5
351 1.29 mycroft if ((instance = strchr(username, '/')) != NULL)
352 1.10 brezak *instance++ = '\0';
353 1.29 mycroft else
354 1.10 brezak instance = "";
355 1.10 brezak #endif
356 1.16 sommerfe if (strlen(username) > MAXLOGNAME)
357 1.16 sommerfe username[MAXLOGNAME] = '\0';
358 1.1 cgd
359 1.1 cgd /*
360 1.1 cgd * Note if trying multiple user names; log failures for
361 1.1 cgd * previous user name, but don't bother logging one failure
362 1.1 cgd * for nonexistent name (mistyped username).
363 1.1 cgd */
364 1.1 cgd if (failures && strcmp(tbuf, username)) {
365 1.1 cgd if (failures > (pwd ? 0 : 1))
366 1.1 cgd badlogin(tbuf);
367 1.1 cgd failures = 0;
368 1.1 cgd }
369 1.71 itojun (void)strlcpy(tbuf, username, sizeof(tbuf));
370 1.1 cgd
371 1.90 hubertf pwd = getpwnam(username);
372 1.1 cgd
373 1.48 mjl #ifdef LOGIN_CAP
374 1.48 mjl /*
375 1.48 mjl * Establish the class now, before we might goto
376 1.48 mjl * within the next block. pwd can be NULL since it
377 1.52 mjl * falls back to the "default" class if it is.
378 1.52 mjl */
379 1.52 mjl lc = login_getclass(pwd ? pwd->pw_class : NULL);
380 1.48 mjl #endif
381 1.1 cgd /*
382 1.1 cgd * if we have a valid account name, and it doesn't have a
383 1.1 cgd * password, or the -f option was specified and the caller
384 1.1 cgd * is root or the caller isn't changing their uid, don't
385 1.1 cgd * authenticate.
386 1.1 cgd */
387 1.7 mycroft if (pwd) {
388 1.7 mycroft if (pwd->pw_uid == 0)
389 1.7 mycroft rootlogin = 1;
390 1.7 mycroft
391 1.8 mycroft if (fflag && (uid == 0 || uid == pwd->pw_uid)) {
392 1.7 mycroft /* already authenticated */
393 1.44 aidan #ifdef KERBEROS5
394 1.57 aidan if (login_krb5_get_tickets && Fflag)
395 1.44 aidan k5_read_creds(username);
396 1.44 aidan #endif
397 1.7 mycroft break;
398 1.7 mycroft } else if (pwd->pw_passwd[0] == '\0') {
399 1.7 mycroft /* pretend password okay */
400 1.7 mycroft rval = 0;
401 1.7 mycroft goto ttycheck;
402 1.7 mycroft }
403 1.7 mycroft }
404 1.7 mycroft
405 1.1 cgd fflag = 0;
406 1.1 cgd
407 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -4);
408 1.1 cgd
409 1.27 mycroft #ifdef SKEY
410 1.29 mycroft if (skey_haskey(username) == 0) {
411 1.29 mycroft static char skprompt[80];
412 1.59 thorpej const char *skinfo = skey_keyinfo(username);
413 1.20 lukem
414 1.75 itojun (void)snprintf(skprompt, sizeof(skprompt),
415 1.83 tron "Password [ %s ]:",
416 1.29 mycroft skinfo ? skinfo : "error getting challenge");
417 1.29 mycroft pwprompt = skprompt;
418 1.29 mycroft } else
419 1.27 mycroft #endif
420 1.29 mycroft pwprompt = "Password:";
421 1.27 mycroft
422 1.29 mycroft p = getpass(pwprompt);
423 1.1 cgd
424 1.29 mycroft if (pwd == NULL) {
425 1.29 mycroft rval = 1;
426 1.29 mycroft goto skip;
427 1.29 mycroft }
428 1.29 mycroft #ifdef KERBEROS5
429 1.57 aidan if (login_krb5_get_tickets &&
430 1.57 aidan k5login(pwd, instance, localhost, p) == 0) {
431 1.29 mycroft rval = 0;
432 1.57 aidan got_tickets = 1;
433 1.57 aidan }
434 1.57 aidan #endif
435 1.89 wiz #if defined(KERBEROS5)
436 1.57 aidan if (got_tickets)
437 1.29 mycroft goto skip;
438 1.29 mycroft #endif
439 1.20 lukem #ifdef SKEY
440 1.29 mycroft if (skey_haskey(username) == 0 &&
441 1.29 mycroft skey_passcheck(username, p) != -1) {
442 1.29 mycroft rval = 0;
443 1.29 mycroft goto skip;
444 1.29 mycroft }
445 1.20 lukem #endif
446 1.29 mycroft if (!sflag && *pwd->pw_passwd != '\0' &&
447 1.29 mycroft !strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd)) {
448 1.29 mycroft rval = 0;
449 1.29 mycroft require_chpass = 1;
450 1.29 mycroft goto skip;
451 1.29 mycroft }
452 1.29 mycroft rval = 1;
453 1.29 mycroft
454 1.29 mycroft skip:
455 1.29 mycroft memset(p, 0, strlen(p));
456 1.1 cgd
457 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, 0);
458 1.1 cgd
459 1.7 mycroft ttycheck:
460 1.1 cgd /*
461 1.1 cgd * If trying to log in as root without Kerberos,
462 1.1 cgd * but with insecure terminal, refuse the login attempt.
463 1.1 cgd */
464 1.7 mycroft if (pwd && !rval && rootlogin && !rootterm(tty)) {
465 1.87 jnemeth (void)printf("Login incorrect or refused on this "
466 1.87 jnemeth "terminal.\n");
467 1.1 cgd if (hostname)
468 1.1 cgd syslog(LOG_NOTICE,
469 1.1 cgd "LOGIN %s REFUSED FROM %s ON TTY %s",
470 1.1 cgd pwd->pw_name, hostname, tty);
471 1.1 cgd else
472 1.1 cgd syslog(LOG_NOTICE,
473 1.1 cgd "LOGIN %s REFUSED ON TTY %s",
474 1.1 cgd pwd->pw_name, tty);
475 1.1 cgd continue;
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd if (pwd && !rval)
479 1.1 cgd break;
480 1.1 cgd
481 1.87 jnemeth (void)printf("Login incorrect or refused on this "
482 1.87 jnemeth "terminal.\n");
483 1.1 cgd failures++;
484 1.48 mjl cnt++;
485 1.88 jnemeth /*
486 1.88 jnemeth * We allow login_retries tries, but after login_backoff
487 1.88 jnemeth * we start backing off. These default to 10 and 3
488 1.88 jnemeth * respectively.
489 1.88 jnemeth */
490 1.50 mjl if (cnt > login_backoff) {
491 1.48 mjl if (cnt >= login_retries) {
492 1.1 cgd badlogin(username);
493 1.95 christos sleepexit(EXIT_FAILURE);
494 1.1 cgd }
495 1.88 jnemeth sleep((u_int)((cnt - login_backoff) * 5));
496 1.1 cgd }
497 1.1 cgd }
498 1.1 cgd
499 1.1 cgd /* committed to login -- turn off timeout */
500 1.1 cgd (void)alarm((u_int)0);
501 1.1 cgd
502 1.1 cgd endpwent();
503 1.1 cgd
504 1.1 cgd /* if user not super-user, check for disabled logins */
505 1.48 mjl #ifdef LOGIN_CAP
506 1.93 isaki if (!login_getcapbool(lc, "ignorenologin", rootlogin))
507 1.52 mjl checknologin(login_getcapstr(lc, "nologin", NULL, NULL));
508 1.48 mjl #else
509 1.93 isaki if (!rootlogin)
510 1.93 isaki checknologin(NULL);
511 1.48 mjl #endif
512 1.1 cgd
513 1.48 mjl #ifdef LOGIN_CAP
514 1.93 isaki quietlog = login_getcapbool(lc, "hushlogin", 0);
515 1.48 mjl #else
516 1.93 isaki quietlog = 0;
517 1.48 mjl #endif
518 1.33 hubertf /* Temporarily give up special privileges so we can change */
519 1.33 hubertf /* into NFS-mounted homes that are exported for non-root */
520 1.33 hubertf /* access and have mode 7x0 */
521 1.33 hubertf saved_uid = geteuid();
522 1.35 hubertf saved_gid = getegid();
523 1.35 hubertf nsaved_gids = getgroups(NGROUPS_MAX, saved_gids);
524 1.35 hubertf
525 1.35 hubertf (void)setegid(pwd->pw_gid);
526 1.35 hubertf initgroups(username, pwd->pw_gid);
527 1.35 hubertf (void)seteuid(pwd->pw_uid);
528 1.33 hubertf
529 1.1 cgd if (chdir(pwd->pw_dir) < 0) {
530 1.48 mjl #ifdef LOGIN_CAP
531 1.93 isaki if (login_getcapbool(lc, "requirehome", 0)) {
532 1.55 enami (void)printf("Home directory %s required\n",
533 1.55 enami pwd->pw_dir);
534 1.95 christos sleepexit(EXIT_FAILURE);
535 1.48 mjl }
536 1.48 mjl #endif
537 1.1 cgd (void)printf("No home directory %s!\n", pwd->pw_dir);
538 1.95 christos if (chdir("/") == -1)
539 1.95 christos exit(EXIT_FAILURE);
540 1.1 cgd pwd->pw_dir = "/";
541 1.1 cgd (void)printf("Logging in with home = \"/\".\n");
542 1.1 cgd }
543 1.1 cgd
544 1.55 enami if (!quietlog)
545 1.48 mjl quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
546 1.33 hubertf
547 1.33 hubertf /* regain special privileges */
548 1.33 hubertf (void)seteuid(saved_uid);
549 1.35 hubertf setgroups(nsaved_gids, saved_gids);
550 1.35 hubertf (void)setegid(saved_gid);
551 1.1 cgd
552 1.48 mjl #ifdef LOGIN_CAP
553 1.93 isaki pw_warntime = login_getcaptime(lc, "password-warn",
554 1.93 isaki _PASSWORD_WARNDAYS * SECSPERDAY,
555 1.93 isaki _PASSWORD_WARNDAYS * SECSPERDAY);
556 1.48 mjl #endif
557 1.48 mjl
558 1.66 christos (void)gettimeofday(&now, (struct timezone *)NULL);
559 1.40 ross if (pwd->pw_expire) {
560 1.66 christos if (now.tv_sec >= pwd->pw_expire) {
561 1.1 cgd (void)printf("Sorry -- your account has expired.\n");
562 1.95 christos sleepexit(EXIT_FAILURE);
563 1.66 christos } else if (pwd->pw_expire - now.tv_sec < pw_warntime &&
564 1.55 enami !quietlog)
565 1.1 cgd (void)printf("Warning: your account expires on %s",
566 1.1 cgd ctime(&pwd->pw_expire));
567 1.40 ross }
568 1.40 ross if (pwd->pw_change) {
569 1.30 mycroft if (pwd->pw_change == _PASSWORD_CHGNOW)
570 1.30 mycroft need_chpass = 1;
571 1.66 christos else if (now.tv_sec >= pwd->pw_change) {
572 1.31 mycroft (void)printf("Sorry -- your password has expired.\n");
573 1.95 christos sleepexit(EXIT_FAILURE);
574 1.66 christos } else if (pwd->pw_change - now.tv_sec < pw_warntime &&
575 1.55 enami !quietlog)
576 1.30 mycroft (void)printf("Warning: your password expires on %s",
577 1.30 mycroft ctime(&pwd->pw_change));
578 1.1 cgd
579 1.40 ross }
580 1.5 cgd /* Nothing else left to fail -- really log in. */
581 1.66 christos update_db(quietlog);
582 1.1 cgd
583 1.1 cgd (void)chown(ttyn, pwd->pw_uid,
584 1.1 cgd (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
585 1.15 gwr
586 1.15 gwr if (ttyaction(ttyn, "login", pwd->pw_name))
587 1.15 gwr (void)printf("Warning: ttyaction failed.\n");
588 1.15 gwr
589 1.89 wiz #if defined(KERBEROS5)
590 1.10 brezak /* Fork so that we can call kdestroy */
591 1.89 wiz if (! login_krb5_retain_ccache && has_ccache)
592 1.29 mycroft dofork();
593 1.10 brezak #endif
594 1.53 mjl
595 1.53 mjl /* Destroy environment unless user has requested its preservation. */
596 1.53 mjl if (!pflag)
597 1.53 mjl environ = envinit;
598 1.53 mjl
599 1.48 mjl #ifdef LOGIN_CAP
600 1.70 itojun if (nested == NULL && setusercontext(lc, pwd, pwd->pw_uid,
601 1.70 itojun LOGIN_SETLOGIN) != 0) {
602 1.70 itojun syslog(LOG_ERR, "setusercontext failed");
603 1.95 christos exit(EXIT_FAILURE);
604 1.70 itojun }
605 1.70 itojun if (setusercontext(lc, pwd, pwd->pw_uid,
606 1.70 itojun (LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETLOGIN))) != 0) {
607 1.52 mjl syslog(LOG_ERR, "setusercontext failed");
608 1.95 christos exit(EXIT_FAILURE);
609 1.48 mjl }
610 1.52 mjl #else
611 1.1 cgd (void)setgid(pwd->pw_gid);
612 1.1 cgd
613 1.1 cgd initgroups(username, pwd->pw_gid);
614 1.48 mjl
615 1.70 itojun if (nested == NULL && setlogin(pwd->pw_name) < 0)
616 1.48 mjl syslog(LOG_ERR, "setlogin() failure: %m");
617 1.48 mjl
618 1.48 mjl /* Discard permissions last so can't get killed and drop core. */
619 1.48 mjl if (rootlogin)
620 1.48 mjl (void)setuid(0);
621 1.48 mjl else
622 1.48 mjl (void)setuid(pwd->pw_uid);
623 1.52 mjl #endif
624 1.1 cgd
625 1.1 cgd if (*pwd->pw_shell == '\0')
626 1.1 cgd pwd->pw_shell = _PATH_BSHELL;
627 1.48 mjl #ifdef LOGIN_CAP
628 1.55 enami if ((shell = login_getcapstr(lc, "shell", NULL, NULL)) != NULL) {
629 1.55 enami if ((shell = strdup(shell)) == NULL) {
630 1.93 isaki syslog(LOG_ERR, "Cannot alloc mem");
631 1.95 christos sleepexit(EXIT_FAILURE);
632 1.48 mjl }
633 1.48 mjl pwd->pw_shell = shell;
634 1.48 mjl }
635 1.48 mjl #endif
636 1.48 mjl
637 1.1 cgd (void)setenv("HOME", pwd->pw_dir, 1);
638 1.1 cgd (void)setenv("SHELL", pwd->pw_shell, 1);
639 1.48 mjl if (term[0] == '\0') {
640 1.55 enami char *tt = (char *)stypeof(tty);
641 1.48 mjl #ifdef LOGIN_CAP
642 1.55 enami if (tt == NULL)
643 1.48 mjl tt = login_getcapstr(lc, "term", NULL, NULL);
644 1.48 mjl #endif
645 1.48 mjl /* unknown term -> "su" */
646 1.71 itojun (void)strlcpy(term, tt != NULL ? tt : "su", sizeof(term));
647 1.55 enami }
648 1.1 cgd (void)setenv("TERM", term, 0);
649 1.1 cgd (void)setenv("LOGNAME", pwd->pw_name, 1);
650 1.1 cgd (void)setenv("USER", pwd->pw_name, 1);
651 1.51 mjl
652 1.48 mjl #ifdef LOGIN_CAP
653 1.52 mjl setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH);
654 1.48 mjl #else
655 1.1 cgd (void)setenv("PATH", _PATH_DEFPATH, 0);
656 1.48 mjl #endif
657 1.51 mjl
658 1.10 brezak #ifdef KERBEROS5
659 1.57 aidan if (krb5tkfile_env)
660 1.57 aidan (void)setenv("KRB5CCNAME", krb5tkfile_env, 1);
661 1.10 brezak #endif
662 1.1 cgd
663 1.12 jtc if (tty[sizeof("tty")-1] == 'd')
664 1.12 jtc syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
665 1.12 jtc
666 1.5 cgd /* If fflag is on, assume caller/authenticator has logged root login. */
667 1.40 ross if (rootlogin && fflag == 0) {
668 1.1 cgd if (hostname)
669 1.1 cgd syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
670 1.1 cgd username, tty, hostname);
671 1.1 cgd else
672 1.55 enami syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s",
673 1.55 enami username, tty);
674 1.40 ross }
675 1.1 cgd
676 1.89 wiz #if defined(KERBEROS5)
677 1.60 thorpej if (KERBEROS_CONFIGURED && !quietlog && notickets == 1)
678 1.1 cgd (void)printf("Warning: no Kerberos tickets issued.\n");
679 1.1 cgd #endif
680 1.1 cgd
681 1.1 cgd if (!quietlog) {
682 1.51 mjl char *fname;
683 1.48 mjl #ifdef LOGIN_CAP
684 1.48 mjl fname = login_getcapstr(lc, "copyright", NULL, NULL);
685 1.55 enami if (fname != NULL && access(fname, F_OK) == 0)
686 1.48 mjl motd(fname);
687 1.48 mjl else
688 1.55 enami #endif
689 1.69 itojun (void)printf("%s", copyrightstr);
690 1.48 mjl
691 1.48 mjl #ifdef LOGIN_CAP
692 1.93 isaki fname = login_getcapstr(lc, "welcome", NULL, NULL);
693 1.93 isaki if (fname == NULL || access(fname, F_OK) != 0)
694 1.48 mjl #endif
695 1.93 isaki fname = _PATH_MOTDFILE;
696 1.93 isaki motd(fname);
697 1.48 mjl
698 1.5 cgd (void)snprintf(tbuf,
699 1.5 cgd sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
700 1.1 cgd if (stat(tbuf, &st) == 0 && st.st_size != 0)
701 1.1 cgd (void)printf("You have %smail.\n",
702 1.1 cgd (st.st_mtime > st.st_atime) ? "new " : "");
703 1.1 cgd }
704 1.1 cgd
705 1.48 mjl #ifdef LOGIN_CAP
706 1.48 mjl login_close(lc);
707 1.48 mjl #endif
708 1.48 mjl
709 1.1 cgd (void)signal(SIGALRM, SIG_DFL);
710 1.1 cgd (void)signal(SIGQUIT, SIG_DFL);
711 1.1 cgd (void)signal(SIGINT, SIG_DFL);
712 1.1 cgd (void)signal(SIGTSTP, SIG_IGN);
713 1.1 cgd
714 1.1 cgd tbuf[0] = '-';
715 1.71 itojun (void)strlcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
716 1.71 itojun p + 1 : pwd->pw_shell, sizeof(tbuf) - 1);
717 1.1 cgd
718 1.24 lukem /* Wait to change password until we're unprivileged */
719 1.24 lukem if (need_chpass) {
720 1.29 mycroft if (!require_chpass)
721 1.24 lukem (void)printf(
722 1.24 lukem "Warning: your password has expired. Please change it as soon as possible.\n");
723 1.29 mycroft else {
724 1.38 mrg int status;
725 1.38 mrg
726 1.29 mycroft (void)printf(
727 1.24 lukem "Your password has expired. Please choose a new one.\n");
728 1.38 mrg switch (fork()) {
729 1.38 mrg case -1:
730 1.38 mrg warn("fork");
731 1.95 christos sleepexit(EXIT_FAILURE);
732 1.38 mrg case 0:
733 1.92 mrg execl(_PATH_BINPASSWD, "passwd", NULL);
734 1.95 christos _exit(EXIT_FAILURE);
735 1.38 mrg default:
736 1.38 mrg if (wait(&status) == -1 ||
737 1.38 mrg WEXITSTATUS(status))
738 1.95 christos sleepexit(EXIT_FAILURE);
739 1.38 mrg }
740 1.29 mycroft }
741 1.24 lukem }
742 1.1 cgd
743 1.44 aidan #ifdef KERBEROS5
744 1.57 aidan if (login_krb5_get_tickets)
745 1.57 aidan k5_write_creds();
746 1.44 aidan #endif
747 1.92 mrg execlp(pwd->pw_shell, tbuf, NULL);
748 1.95 christos err(EXIT_FAILURE, "%s", pwd->pw_shell);
749 1.1 cgd }
750 1.57 aidan
751 1.89 wiz #if defined(KERBEROS5)
752 1.16 sommerfe #define NBUFSIZ (MAXLOGNAME + 1 + 5) /* .root suffix */
753 1.1 cgd #else
754 1.16 sommerfe #define NBUFSIZ (MAXLOGNAME + 1)
755 1.10 brezak #endif
756 1.10 brezak
757 1.89 wiz #if defined(KERBEROS5)
758 1.10 brezak /*
759 1.10 brezak * This routine handles cleanup stuff, and the like.
760 1.10 brezak * It exists only in the child process.
761 1.10 brezak */
762 1.10 brezak void
763 1.80 xtraeme dofork(void)
764 1.10 brezak {
765 1.95 christos pid_t child, wchild;
766 1.38 mrg
767 1.95 christos switch (child = fork()) {
768 1.95 christos case 0:
769 1.38 mrg return; /* Child process */
770 1.95 christos case -1:
771 1.95 christos err(EXIT_FAILURE, "Can't fork");
772 1.95 christos /*NOTREACHED*/
773 1.95 christos default:
774 1.95 christos break;
775 1.95 christos }
776 1.38 mrg
777 1.55 enami /*
778 1.55 enami * Setup stuff? This would be things we could do in parallel
779 1.55 enami * with login
780 1.55 enami */
781 1.95 christos if (chdir("/") == -1) /* Let's not keep the fs busy... */
782 1.95 christos err(EXIT_FAILURE, "Can't chdir to `/'");
783 1.10 brezak
784 1.38 mrg /* If we're the parent, watch the child until it dies */
785 1.95 christos while ((wchild = wait(NULL)) != child)
786 1.95 christos if (wchild == -1)
787 1.95 christos err(EXIT_FAILURE, "Can't wait");
788 1.10 brezak
789 1.38 mrg /* Cleanup stuff */
790 1.38 mrg /* Run kdestroy to destroy tickets */
791 1.57 aidan if (login_krb5_get_tickets)
792 1.57 aidan k5destroy();
793 1.10 brezak
794 1.38 mrg /* Leave */
795 1.95 christos exit(EXIT_SUCCESS);
796 1.10 brezak }
797 1.1 cgd #endif
798 1.1 cgd
799 1.5 cgd void
800 1.80 xtraeme getloginname(void)
801 1.1 cgd {
802 1.12 jtc int ch;
803 1.12 jtc char *p;
804 1.1 cgd static char nbuf[NBUFSIZ];
805 1.1 cgd
806 1.1 cgd for (;;) {
807 1.1 cgd (void)printf("login: ");
808 1.1 cgd for (p = nbuf; (ch = getchar()) != '\n'; ) {
809 1.1 cgd if (ch == EOF) {
810 1.1 cgd badlogin(username);
811 1.95 christos exit(EXIT_FAILURE);
812 1.1 cgd }
813 1.1 cgd if (p < nbuf + (NBUFSIZ - 1))
814 1.1 cgd *p++ = ch;
815 1.1 cgd }
816 1.40 ross if (p > nbuf) {
817 1.1 cgd if (nbuf[0] == '-')
818 1.1 cgd (void)fprintf(stderr,
819 1.1 cgd "login names may not start with '-'.\n");
820 1.1 cgd else {
821 1.1 cgd *p = '\0';
822 1.1 cgd username = nbuf;
823 1.1 cgd break;
824 1.1 cgd }
825 1.40 ross }
826 1.1 cgd }
827 1.1 cgd }
828 1.1 cgd
829 1.5 cgd int
830 1.80 xtraeme rootterm(char *ttyn)
831 1.1 cgd {
832 1.1 cgd struct ttyent *t;
833 1.1 cgd
834 1.12 jtc return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
835 1.1 cgd }
836 1.1 cgd
837 1.1 cgd jmp_buf motdinterrupt;
838 1.1 cgd
839 1.5 cgd void
840 1.80 xtraeme motd(char *fname)
841 1.1 cgd {
842 1.12 jtc int fd, nchars;
843 1.1 cgd sig_t oldint;
844 1.1 cgd char tbuf[8192];
845 1.1 cgd
846 1.48 mjl if ((fd = open(fname ? fname : _PATH_MOTDFILE, O_RDONLY, 0)) < 0)
847 1.1 cgd return;
848 1.1 cgd oldint = signal(SIGINT, sigint);
849 1.1 cgd if (setjmp(motdinterrupt) == 0)
850 1.1 cgd while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
851 1.1 cgd (void)write(fileno(stdout), tbuf, nchars);
852 1.1 cgd (void)signal(SIGINT, oldint);
853 1.1 cgd (void)close(fd);
854 1.1 cgd }
855 1.1 cgd
856 1.5 cgd /* ARGSUSED */
857 1.1 cgd void
858 1.80 xtraeme sigint(int signo)
859 1.1 cgd {
860 1.38 mrg
861 1.1 cgd longjmp(motdinterrupt, 1);
862 1.1 cgd }
863 1.1 cgd
864 1.5 cgd /* ARGSUSED */
865 1.5 cgd void
866 1.80 xtraeme timedout(int signo)
867 1.5 cgd {
868 1.38 mrg
869 1.5 cgd (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
870 1.95 christos exit(EXIT_FAILURE);
871 1.5 cgd }
872 1.5 cgd
873 1.5 cgd void
874 1.80 xtraeme checknologin(char *fname)
875 1.1 cgd {
876 1.12 jtc int fd, nchars;
877 1.1 cgd char tbuf[8192];
878 1.1 cgd
879 1.48 mjl if ((fd = open(fname ? fname : _PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
880 1.1 cgd while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
881 1.1 cgd (void)write(fileno(stdout), tbuf, nchars);
882 1.95 christos sleepexit(EXIT_SUCCESS);
883 1.1 cgd }
884 1.1 cgd }
885 1.1 cgd
886 1.66 christos static void
887 1.66 christos update_db(int quietlog)
888 1.66 christos {
889 1.70 itojun if (nested != NULL) {
890 1.70 itojun if (hostname != NULL)
891 1.70 itojun syslog(LOG_NOTICE, "%s to %s on tty %s from %s",
892 1.70 itojun nested, pwd->pw_name, tty, hostname);
893 1.70 itojun else
894 1.70 itojun syslog(LOG_NOTICE, "%s to %s on tty %s", nested,
895 1.70 itojun pwd->pw_name, tty);
896 1.70 itojun
897 1.70 itojun return;
898 1.70 itojun }
899 1.79 christos if (hostname != NULL && have_ss == 0) {
900 1.66 christos socklen_t len = sizeof(ss);
901 1.86 christos have_ss = getpeername(STDIN_FILENO, (struct sockaddr *)&ss,
902 1.86 christos &len) != -1;
903 1.66 christos }
904 1.66 christos (void)gettimeofday(&now, NULL);
905 1.67 christos #ifdef SUPPORT_UTMPX
906 1.66 christos doutmpx();
907 1.66 christos dolastlogx(quietlog);
908 1.66 christos quietlog = 1;
909 1.66 christos #endif
910 1.67 christos #ifdef SUPPORT_UTMP
911 1.66 christos doutmp();
912 1.66 christos dolastlog(quietlog);
913 1.66 christos #endif
914 1.66 christos }
915 1.66 christos
916 1.67 christos #ifdef SUPPORT_UTMPX
917 1.66 christos static void
918 1.80 xtraeme doutmpx(void)
919 1.66 christos {
920 1.66 christos struct utmpx utmpx;
921 1.66 christos char *t;
922 1.66 christos
923 1.66 christos memset((void *)&utmpx, 0, sizeof(utmpx));
924 1.66 christos utmpx.ut_tv = now;
925 1.66 christos (void)strncpy(utmpx.ut_name, username, sizeof(utmpx.ut_name));
926 1.66 christos if (hostname) {
927 1.66 christos (void)strncpy(utmpx.ut_host, hostname, sizeof(utmpx.ut_host));
928 1.66 christos utmpx.ut_ss = ss;
929 1.66 christos }
930 1.66 christos (void)strncpy(utmpx.ut_line, tty, sizeof(utmpx.ut_line));
931 1.66 christos utmpx.ut_type = USER_PROCESS;
932 1.66 christos utmpx.ut_pid = getpid();
933 1.66 christos t = tty + strlen(tty);
934 1.66 christos if (t - tty >= sizeof(utmpx.ut_id)) {
935 1.66 christos (void)strncpy(utmpx.ut_id, t - sizeof(utmpx.ut_id),
936 1.66 christos sizeof(utmpx.ut_id));
937 1.66 christos } else {
938 1.66 christos (void)strncpy(utmpx.ut_id, tty, sizeof(utmpx.ut_id));
939 1.66 christos }
940 1.66 christos if (pututxline(&utmpx) == NULL)
941 1.84 jmmv syslog(LOG_NOTICE, "Cannot update utmpx: %m");
942 1.66 christos endutxent();
943 1.66 christos if (updwtmpx(_PATH_WTMPX, &utmpx) != 0)
944 1.84 jmmv syslog(LOG_NOTICE, "Cannot update wtmpx: %m");
945 1.66 christos }
946 1.66 christos
947 1.66 christos static void
948 1.80 xtraeme dolastlogx(int quiet)
949 1.66 christos {
950 1.66 christos struct lastlogx ll;
951 1.85 christos if (!quiet && getlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != NULL) {
952 1.66 christos time_t t = (time_t)ll.ll_tv.tv_sec;
953 1.66 christos (void)printf("Last login: %.24s ", ctime(&t));
954 1.66 christos if (*ll.ll_host != '\0')
955 1.66 christos (void)printf("from %.*s ",
956 1.66 christos (int)sizeof(ll.ll_host),
957 1.66 christos ll.ll_host);
958 1.66 christos (void)printf("on %.*s\n",
959 1.66 christos (int)sizeof(ll.ll_line),
960 1.66 christos ll.ll_line);
961 1.66 christos }
962 1.66 christos ll.ll_tv = now;
963 1.66 christos (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
964 1.86 christos if (hostname)
965 1.66 christos (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
966 1.86 christos else
967 1.86 christos (void)memset(ll.ll_host, '\0', sizeof(ll.ll_host));
968 1.86 christos if (have_ss)
969 1.66 christos ll.ll_ss = ss;
970 1.86 christos else
971 1.86 christos (void)memset(&ll.ll_ss, 0, sizeof(ll.ll_ss));
972 1.66 christos if (updlastlogx(_PATH_LASTLOGX, pwd->pw_uid, &ll) != 0)
973 1.84 jmmv syslog(LOG_NOTICE, "Cannot update lastlogx: %m");
974 1.66 christos }
975 1.66 christos #endif
976 1.66 christos
977 1.67 christos #ifdef SUPPORT_UTMP
978 1.66 christos static void
979 1.80 xtraeme doutmp(void)
980 1.66 christos {
981 1.66 christos struct utmp utmp;
982 1.66 christos
983 1.66 christos (void)memset((void *)&utmp, 0, sizeof(utmp));
984 1.66 christos utmp.ut_time = now.tv_sec;
985 1.66 christos (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
986 1.66 christos if (hostname)
987 1.66 christos (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
988 1.66 christos (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
989 1.66 christos login(&utmp);
990 1.66 christos }
991 1.66 christos
992 1.66 christos static void
993 1.80 xtraeme dolastlog(int quiet)
994 1.1 cgd {
995 1.1 cgd struct lastlog ll;
996 1.1 cgd int fd;
997 1.1 cgd
998 1.1 cgd if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
999 1.26 kleink (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)), SEEK_SET);
1000 1.1 cgd if (!quiet) {
1001 1.1 cgd if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
1002 1.1 cgd ll.ll_time != 0) {
1003 1.66 christos (void)printf("Last login: %.24s ",
1004 1.66 christos ctime(&ll.ll_time));
1005 1.1 cgd if (*ll.ll_host != '\0')
1006 1.66 christos (void)printf("from %.*s ",
1007 1.5 cgd (int)sizeof(ll.ll_host),
1008 1.5 cgd ll.ll_host);
1009 1.66 christos (void)printf("on %.*s\n",
1010 1.66 christos (int)sizeof(ll.ll_line), ll.ll_line);
1011 1.1 cgd }
1012 1.26 kleink (void)lseek(fd, (off_t)(pwd->pw_uid * sizeof(ll)),
1013 1.26 kleink SEEK_SET);
1014 1.1 cgd }
1015 1.12 jtc memset((void *)&ll, 0, sizeof(ll));
1016 1.66 christos ll.ll_time = now.tv_sec;
1017 1.5 cgd (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
1018 1.1 cgd if (hostname)
1019 1.5 cgd (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
1020 1.1 cgd (void)write(fd, (char *)&ll, sizeof(ll));
1021 1.1 cgd (void)close(fd);
1022 1.1 cgd }
1023 1.1 cgd }
1024 1.66 christos #endif
1025 1.1 cgd
1026 1.5 cgd void
1027 1.80 xtraeme badlogin(char *name)
1028 1.1 cgd {
1029 1.55 enami
1030 1.1 cgd if (failures == 0)
1031 1.1 cgd return;
1032 1.1 cgd if (hostname) {
1033 1.1 cgd syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
1034 1.1 cgd failures, failures > 1 ? "S" : "", hostname);
1035 1.1 cgd syslog(LOG_AUTHPRIV|LOG_NOTICE,
1036 1.1 cgd "%d LOGIN FAILURE%s FROM %s, %s",
1037 1.1 cgd failures, failures > 1 ? "S" : "", hostname, name);
1038 1.1 cgd } else {
1039 1.1 cgd syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
1040 1.1 cgd failures, failures > 1 ? "S" : "", tty);
1041 1.1 cgd syslog(LOG_AUTHPRIV|LOG_NOTICE,
1042 1.1 cgd "%d LOGIN FAILURE%s ON %s, %s",
1043 1.1 cgd failures, failures > 1 ? "S" : "", tty, name);
1044 1.1 cgd }
1045 1.1 cgd }
1046 1.1 cgd
1047 1.39 mycroft const char *
1048 1.80 xtraeme stypeof(const char *ttyid)
1049 1.1 cgd {
1050 1.1 cgd struct ttyent *t;
1051 1.1 cgd
1052 1.48 mjl return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : NULL);
1053 1.1 cgd }
1054 1.1 cgd
1055 1.5 cgd void
1056 1.80 xtraeme sleepexit(int eval)
1057 1.1 cgd {
1058 1.38 mrg
1059 1.12 jtc (void)sleep(5);
1060 1.1 cgd exit(eval);
1061 1.1 cgd }
1062 1.79 christos
1063 1.79 christos void
1064 1.80 xtraeme decode_ss(const char *arg)
1065 1.79 christos {
1066 1.79 christos struct sockaddr_storage *ssp;
1067 1.79 christos size_t len = strlen(arg);
1068 1.79 christos
1069 1.79 christos if (len > sizeof(*ssp) * 4 + 1 || len < sizeof(*ssp))
1070 1.95 christos errx(EXIT_FAILURE, "Bad argument");
1071 1.79 christos
1072 1.79 christos if ((ssp = malloc(len)) == NULL)
1073 1.95 christos err(EXIT_FAILURE, NULL);
1074 1.79 christos
1075 1.79 christos if (strunvis((char *)ssp, arg) != sizeof(*ssp))
1076 1.95 christos errx(EXIT_FAILURE, "Decoding error");
1077 1.79 christos
1078 1.79 christos (void)memcpy(&ss, ssp, sizeof(ss));
1079 1.79 christos have_ss = 1;
1080 1.79 christos }
1081 1.79 christos
1082 1.79 christos void
1083 1.80 xtraeme usage(void)
1084 1.79 christos {
1085 1.79 christos (void)fprintf(stderr,
1086 1.79 christos "Usage: %s [-Ffps] [-a address] [-h hostname] [username]\n",
1087 1.79 christos getprogname());
1088 1.95 christos exit(EXIT_FAILURE);
1089 1.79 christos }
1090