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