]> git.saurik.com Git - apple/system_cmds.git/blame - login.tproj/login.c
system_cmds-175.2.tar.gz
[apple/system_cmds.git] / login.tproj / login.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*-
25 * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#ifndef lint
58static char copyright[] =
59"@(#) Copyright (c) Apple Computer, Inc. 1997\n\n";
60#endif /* not lint */
61
62/*
63 * login [ name ]
64 * login -h hostname (for telnetd, etc.)
65 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
66 */
67
68#include <sys/param.h>
69#include <sys/stat.h>
70#include <sys/time.h>
71#include <sys/resource.h>
72#include <sys/file.h>
73
74#include <err.h>
75#include <errno.h>
76#include <grp.h>
77#include <pwd.h>
78#include <setjmp.h>
79#include <signal.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <syslog.h>
84#include <ttyent.h>
85#include <tzfile.h>
86#include <unistd.h>
87#include <utmp.h>
88
89#include "pathnames.h"
90
91void badlogin __P((char *));
92void checknologin __P((void));
93void dolastlog __P((int));
94void getloginname __P((void));
95void motd __P((void));
96int rootterm __P((char *));
97void sigint __P((int));
98void sleepexit __P((int));
99char *stypeof __P((char *));
100void timedout __P((int));
101#ifdef KERBEROS
102int klogin __P((struct passwd *, char *, char *, char *));
103#endif
104
105extern void login __P((struct utmp *));
106
107#define TTYGRPNAME "tty" /* name of group to own ttys */
108
109/*
110 * This bounds the time given to login. Not a define so it can
111 * be patched on machines where it's too small.
112 */
113u_int timeout = 300;
114
115#ifdef KERBEROS
116int notickets = 1;
117char *instance;
118char *krbtkfile_env;
119int authok;
120#endif
121
122struct passwd *pwd;
123int failures;
124char term[64], *envinit[1], *hostname, *username, *tty;
125
126int
127main(argc, argv)
128 int argc;
129 char *argv[];
130{
131 extern char **environ;
132 struct group *gr;
133 struct stat st;
134 struct timeval tp;
135 struct utmp utmp;
136 int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval;
137 uid_t uid;
138 char *domain, *p, *salt, *ttyn;
139 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
140 char localhost[MAXHOSTNAMELEN];
141
142 (void)signal(SIGALRM, timedout);
143 (void)alarm(timeout);
144 (void)signal(SIGQUIT, SIG_IGN);
145 (void)signal(SIGINT, SIG_IGN);
146 (void)setpriority(PRIO_PROCESS, 0, 0);
147
148 openlog("login", LOG_ODELAY, LOG_AUTH);
149
150 /*
151 * -p is used by getty to tell login not to destroy the environment
152 * -f is used to skip a second login authentication
153 * -h is used by other servers to pass the name of the remote
154 * host to login so that it may be placed in utmp and wtmp
155 */
156 domain = NULL;
157 if (gethostname(localhost, sizeof(localhost)) < 0)
158 syslog(LOG_ERR, "couldn't get local hostname: %m");
159 else
160 domain = strchr(localhost, '.');
161
162 fflag = hflag = pflag = 0;
163 uid = getuid();
164 while ((ch = getopt(argc, argv, "fh:p")) != EOF)
165 switch (ch) {
166 case 'f':
167 fflag = 1;
168 break;
169 case 'h':
170 if (uid)
171 errx(1, "-h option: %s", strerror(EPERM));
172 hflag = 1;
173 if (domain && (p = strchr(optarg, '.')) &&
174 strcasecmp(p, domain) == 0)
175 *p = 0;
176 hostname = optarg;
177 break;
178 case 'p':
179 pflag = 1;
180 break;
181 case '?':
182 default:
183 if (!uid)
184 syslog(LOG_ERR, "invalid flag %c", ch);
185 (void)fprintf(stderr,
186 "usage: login [-fp] [-h hostname] [username]\n");
187 exit(1);
188 }
189 argc -= optind;
190 argv += optind;
191
192 if (*argv) {
193 username = *argv;
194 ask = 0;
195 } else
196 ask = 1;
197
198 for (cnt = getdtablesize(); cnt > 2; cnt--)
199 (void)close(cnt);
200
201 ttyn = ttyname(STDIN_FILENO);
202 if (ttyn == NULL || *ttyn == '\0') {
203 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY);
204 ttyn = tname;
205 }
206 if (tty = strrchr(ttyn, '/'))
207 ++tty;
208 else
209 tty = ttyn;
210
211 for (cnt = 0;; ask = 1) {
212 if (ask) {
213 fflag = 0;
214 getloginname();
215 }
216 rootlogin = 0;
217#ifdef KERBEROS
218 if ((instance = strchr(username, '.')) != NULL) {
219 if (strncmp(instance, ".root", 5) == 0)
220 rootlogin = 1;
221 *instance++ = '\0';
222 } else
223 instance = "";
224#endif
225 if (strlen(username) > UT_NAMESIZE)
226 username[UT_NAMESIZE] = '\0';
227
228 /*
229 * Note if trying multiple user names; log failures for
230 * previous user name, but don't bother logging one failure
231 * for nonexistent name (mistyped username).
232 */
233 if (failures && strcmp(tbuf, username)) {
234 if (failures > (pwd ? 0 : 1))
235 badlogin(tbuf);
236 failures = 0;
237 }
238 (void)strcpy(tbuf, username);
239
240 if (pwd = getpwnam(username))
241 salt = pwd->pw_passwd;
242 else
243 salt = "xx";
244
245 /*
246 * if we have a valid account name, and it doesn't have a
247 * password, or the -f option was specified and the caller
248 * is root or the caller isn't changing their uid, don't
249 * authenticate.
250 */
251 if (pwd && (*pwd->pw_passwd == '\0' ||
252 fflag && (uid == 0 || uid == pwd->pw_uid)))
253 break;
254 fflag = 0;
255 if (pwd && pwd->pw_uid == 0)
256 rootlogin = 1;
257
258 (void)setpriority(PRIO_PROCESS, 0, -4);
259
260 p = getpass("Password:");
261
262 if (pwd) {
263#ifdef KERBEROS
264 rval = klogin(pwd, instance, localhost, p);
265 if (rval != 0 && rootlogin && pwd->pw_uid != 0)
266 rootlogin = 0;
267 if (rval == 0)
268 authok = 1;
269 else if (rval == 1)
270 rval = strcmp(crypt(p, salt), pwd->pw_passwd);
271#else
272 rval = strcmp(crypt(p, salt), pwd->pw_passwd);
273#endif
274 }
275 memset(p, 0, strlen(p));
276
277 (void)setpriority(PRIO_PROCESS, 0, 0);
278
279 /*
280 * If trying to log in as root without Kerberos,
281 * but with insecure terminal, refuse the login attempt.
282 */
283#ifdef KERBEROS
284 if (authok == 0)
285#endif
286 if (pwd && rootlogin && !rootterm(tty)) {
287 (void)fprintf(stderr,
288 "%s login refused on this terminal.\n",
289 pwd->pw_name);
290 if (hostname)
291 syslog(LOG_NOTICE,
292 "LOGIN %s REFUSED FROM %s ON TTY %s",
293 pwd->pw_name, hostname, tty);
294 else
295 syslog(LOG_NOTICE,
296 "LOGIN %s REFUSED ON TTY %s",
297 pwd->pw_name, tty);
298 continue;
299 }
300
301 if (pwd && !rval)
302 break;
303
304 (void)printf("Login incorrect\n");
305 failures++;
306 /* we allow 10 tries, but after 3 we start backing off */
307 if (++cnt > 3) {
308 if (cnt >= 10) {
309 badlogin(username);
310 sleepexit(1);
311 }
312 sleep((u_int)((cnt - 3) * 5));
313 }
314 }
315
316 /* committed to login -- turn off timeout */
317 (void)alarm((u_int)0);
318
319 endpwent();
320
321 /* if user not super-user, check for disabled logins */
322 if (!rootlogin)
323 checknologin();
324
325 if (chdir(pwd->pw_dir) < 0) {
326 (void)printf("No home directory %s!\n", pwd->pw_dir);
327 if (chdir("/"))
328 exit(0);
329 pwd->pw_dir = "/";
330 (void)printf("Logging in with home = \"/\".\n");
331 }
332
333 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0;
334
335 if (pwd->pw_change || pwd->pw_expire)
336 (void)gettimeofday(&tp, (struct timezone *)NULL);
337 if (pwd->pw_change)
338 if (tp.tv_sec >= pwd->pw_change) {
339 (void)printf("Sorry -- your password has expired.\n");
340 sleepexit(1);
341 } else if (pwd->pw_change - tp.tv_sec <
342 2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
343 (void)printf("Warning: your password expires on %s",
344 ctime(&pwd->pw_change));
345 if (pwd->pw_expire)
346 if (tp.tv_sec >= pwd->pw_expire) {
347 (void)printf("Sorry -- your account has expired.\n");
348 sleepexit(1);
349 } else if (pwd->pw_expire - tp.tv_sec <
350 2 * DAYSPERWEEK * SECSPERDAY && !quietlog)
351 (void)printf("Warning: your account expires on %s",
352 ctime(&pwd->pw_expire));
353
354 /* Nothing else left to fail -- really log in. */
355 memset((void *)&utmp, 0, sizeof(utmp));
356 (void)time(&utmp.ut_time);
357 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
358 if (hostname)
359 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
360 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
361 login(&utmp);
362
363 dolastlog(quietlog);
364
365 (void)chown(ttyn, pwd->pw_uid,
366 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
367 (void)setgid(pwd->pw_gid);
368
369 initgroups(username, pwd->pw_gid);
370
371 if (*pwd->pw_shell == '\0')
372 pwd->pw_shell = _PATH_BSHELL;
373
374 /* Destroy environment unless user has requested its preservation. */
375 if (!pflag)
376 environ = envinit;
377 (void)setenv("HOME", pwd->pw_dir, 1);
378 (void)setenv("SHELL", pwd->pw_shell, 1);
379 if (term[0] == '\0')
380 (void)strncpy(term, stypeof(tty), sizeof(term));
381 (void)setenv("TERM", term, 0);
382 (void)setenv("LOGNAME", pwd->pw_name, 1);
383 (void)setenv("USER", pwd->pw_name, 1);
384 (void)setenv("PATH", _PATH_DEFPATH, 0);
385#ifdef KERBEROS
386 if (krbtkfile_env)
387 (void)setenv("KRBTKFILE", krbtkfile_env, 1);
388#endif
389
390 if (tty[sizeof("tty")-1] == 'd')
391 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
392
393 /* If fflag is on, assume caller/authenticator has logged root login. */
394 if (rootlogin && fflag == 0)
395 if (hostname)
396 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s",
397 username, tty, hostname);
398 else
399 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty);
400
401#ifdef KERBEROS
402 if (!quietlog && notickets == 1)
403 (void)printf("Warning: no Kerberos tickets issued.\n");
404#endif
405
406 if (!quietlog) {
407 motd();
408 (void)snprintf(tbuf,
409 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name);
410 if (stat(tbuf, &st) == 0 && st.st_size != 0)
411 (void)printf("You have %smail.\n",
412 (st.st_mtime > st.st_atime) ? "new " : "");
413 }
414
415 (void)signal(SIGALRM, SIG_DFL);
416 (void)signal(SIGQUIT, SIG_DFL);
417 (void)signal(SIGINT, SIG_DFL);
418 (void)signal(SIGTSTP, SIG_IGN);
419
420 tbuf[0] = '-';
421 (void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ?
422 p + 1 : pwd->pw_shell);
423
424 if (setlogin(pwd->pw_name) < 0)
425 syslog(LOG_ERR, "setlogin() failure: %m");
426
427 /* Discard permissions last so can't get killed and drop core. */
428 if (rootlogin)
429 (void) setuid(0);
430 else
431 (void) setuid(pwd->pw_uid);
432
433 execlp(pwd->pw_shell, tbuf, 0);
434 err(1, "%s", pwd->pw_shell);
435}
436
437#ifdef KERBEROS
438#define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */
439#else
440#define NBUFSIZ (UT_NAMESIZE + 1)
441#endif
442
443void
444getloginname()
445{
446 int ch;
447 char *p;
448 static char nbuf[NBUFSIZ];
449
450 for (;;) {
451 (void)printf("login: ");
452 for (p = nbuf; (ch = getchar()) != '\n'; ) {
453 if (ch == EOF) {
454 badlogin(username);
455 exit(0);
456 }
457 if (p < nbuf + (NBUFSIZ - 1))
458 *p++ = ch;
459 }
460 if (p > nbuf)
461 if (nbuf[0] == '-')
462 (void)fprintf(stderr,
463 "login names may not start with '-'.\n");
464 else {
465 *p = '\0';
466 username = nbuf;
467 break;
468 }
469 }
470}
471
472int
473rootterm(ttyn)
474 char *ttyn;
475{
476 struct ttyent *t;
477
478 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
479}
480
481jmp_buf motdinterrupt;
482
483void
484motd()
485{
486 int fd, nchars;
487 sig_t oldint;
488 char tbuf[8192];
489
490 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
491 return;
492 oldint = signal(SIGINT, sigint);
493 if (setjmp(motdinterrupt) == 0)
494 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
495 (void)write(fileno(stdout), tbuf, nchars);
496 (void)signal(SIGINT, oldint);
497 (void)close(fd);
498}
499
500/* ARGSUSED */
501void
502sigint(signo)
503 int signo;
504{
505
506 longjmp(motdinterrupt, 1);
507}
508
509/* ARGSUSED */
510void
511timedout(signo)
512 int signo;
513{
514
515 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
516 exit(0);
517}
518
519void
520checknologin()
521{
522 int fd, nchars;
523 char tbuf[8192];
524
525 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
526 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
527 (void)write(fileno(stdout), tbuf, nchars);
528 sleepexit(0);
529 }
530}
531
532void
533dolastlog(quiet)
534 int quiet;
535{
536 struct lastlog ll;
537 int fd;
538
539 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
540 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
541 if (!quiet) {
542 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
543 ll.ll_time != 0) {
544 (void)printf("Last login: %.*s ",
545 24-5, (char *)ctime(&ll.ll_time));
546 if (*ll.ll_host != '\0')
547 (void)printf("from %.*s\n",
548 (int)sizeof(ll.ll_host),
549 ll.ll_host);
550 else
551 (void)printf("on %.*s\n",
552 (int)sizeof(ll.ll_line),
553 ll.ll_line);
554 }
555 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
556 }
557 memset((void *)&ll, 0, sizeof(ll));
558 (void)time(&ll.ll_time);
559 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
560 if (hostname)
561 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
562 (void)write(fd, (char *)&ll, sizeof(ll));
563 (void)close(fd);
564 }
565}
566
567void
568badlogin(name)
569 char *name;
570{
571
572 if (failures == 0)
573 return;
574 if (hostname) {
575 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s",
576 failures, failures > 1 ? "S" : "", hostname);
577 syslog(LOG_AUTHPRIV|LOG_NOTICE,
578 "%d LOGIN FAILURE%s FROM %s, %s",
579 failures, failures > 1 ? "S" : "", hostname, name);
580 } else {
581 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s",
582 failures, failures > 1 ? "S" : "", tty);
583 syslog(LOG_AUTHPRIV|LOG_NOTICE,
584 "%d LOGIN FAILURE%s ON %s, %s",
585 failures, failures > 1 ? "S" : "", tty, name);
586 }
587}
588
589#undef UNKNOWN
590#define UNKNOWN "su"
591
592char *
593stypeof(ttyid)
594 char *ttyid;
595{
596 struct ttyent *t;
597
598 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
599}
600
601void
602sleepexit(eval)
603 int eval;
604{
605
606 (void)sleep(5);
607 exit(eval);
608}