Check for NULL return from getpwnam_shadow
[login_krb5.git] / login_passwd / login.c
blob7a3478f5d20e146e6602a79db423d684c79737c1
1 /* $OpenBSD: login.c,v 1.18 2018/06/13 15:02:09 reyk Exp $ */
3 /*-
4 * Copyright (c) 1995 Berkeley Software Design, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Berkeley Software Design,
17 * Inc.
18 * 4. The name of Berkeley Software Design, Inc. may not be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
22 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * BSDI $From: login_passwd.c,v 1.11 1997/08/08 18:58:24 prb Exp $
37 #include "common.h"
39 FILE *back = NULL;
41 int
42 main(int argc, char **argv)
44 int opt, mode = 0, ret, lastchance = 0;
45 char *username, *password = NULL;
46 char pbuf[1024];
47 char response[1024];
48 int arg_login = 0, arg_notickets = 0;
49 char invokinguser[LOGIN_NAME_MAX];
50 char *wheel = NULL, *class = NULL;
51 #ifdef PASSWD
52 struct passwd *pwd, *pwd_save;
53 #endif
55 invokinguser[0] = '\0';
57 setpriority(PRIO_PROCESS, 0, 0);
59 openlog(NULL, LOG_ODELAY, LOG_AUTH);
61 while ((opt = getopt(argc, argv, "ds:v:")) != -1) {
62 switch (opt) {
63 case 'd':
64 back = stdout;
65 break;
66 case 's': /* service */
67 if (strcmp(optarg, "login") == 0)
68 mode = MODE_LOGIN;
69 else if (strcmp(optarg, "challenge") == 0)
70 mode = MODE_CHALLENGE;
71 else if (strcmp(optarg, "response") == 0)
72 mode = MODE_RESPONSE;
73 else {
74 syslog(LOG_ERR, "%s: invalid service", optarg);
75 exit(1);
77 break;
78 case 'v':
79 if (strncmp(optarg, "wheel=", 6) == 0)
80 wheel = optarg + 6;
81 else if (strncmp(optarg, "lastchance=", 11) == 0)
82 lastchance = (strcmp(optarg + 11, "yes") == 0);
83 else if (strcmp(optarg, "login=yes") == 0)
84 arg_login = 1;
85 else if (strcmp(optarg, "notickets=yes") == 0)
86 arg_notickets = 1;
87 else if (strncmp(optarg, "invokinguser=", 13) == 0)
88 snprintf(invokinguser, sizeof(invokinguser),
89 "%s", &optarg[13]);
90 /* Silently ignore unsupported variables */
91 break;
92 default:
93 syslog(LOG_ERR, "usage error1");
94 exit(1);
98 switch (argc - optind) {
99 case 2:
100 class = argv[optind + 1];
101 /*FALLTHROUGH*/
102 case 1:
103 username = argv[optind];
104 break;
105 default:
106 syslog(LOG_ERR, "usage error2");
107 exit(1);
110 #ifdef PASSWD
111 /* get the password hash before pledge(2) or it will return '*' */
112 pwd = getpwnam_shadow(username);
113 if (pwd == NULL || (pwd_save = pw_dup(pwd)) == NULL)
114 exit(1);
115 #endif
117 if ((arg_login) && (!arg_notickets)) {
118 if (pledge("stdio rpath tty id getpw dns inet flock cpath wpath chown", NULL) == -1) {
119 syslog(LOG_ERR, "pledge: %m");
120 exit(1);
122 } else {
123 if (pledge("stdio rpath tty id getpw dns inet flock", NULL) == -1) {
124 syslog(LOG_ERR, "pledge: %m");
125 exit(1);
129 if (back == NULL && (back = fdopen(3, "r+")) == NULL) {
130 syslog(LOG_ERR, "reopening back channel: %m");
131 exit(1);
135 * Read password, either as from the terminal or if the
136 * response mode is active from the caller program.
138 * XXX This is completely ungrokkable, and should be rewritten.
140 switch (mode) {
141 case MODE_RESPONSE: {
142 int count;
143 mode = 0;
144 count = -1;
145 while (++count < sizeof(response) &&
146 read(3, &response[count], 1) == 1) {
147 if (response[count] == '\0' && ++mode == 2)
148 break;
149 if (response[count] == '\0' && mode == 1) {
150 password = response + count + 1;
153 if (mode < 2) {
154 syslog(LOG_ERR, "protocol error on back channel");
155 exit(1);
157 break;
160 case MODE_LOGIN:
161 password = readpassphrase("Password:", pbuf, sizeof(pbuf), RPP_ECHO_OFF);
162 break;
163 case MODE_CHALLENGE:
164 fprintf(back, BI_SILENT "\n");
165 exit(0);
166 break;
167 default:
168 syslog(LOG_ERR, "%d: unknown mode", mode);
169 exit(1);
170 break;
173 ret = AUTH_FAILED;
174 ret = krb5_login(username, invokinguser, password, arg_login,
175 !arg_notickets, class);
176 #ifdef PASSWD
177 if (ret != AUTH_OK)
178 ret = pwd_login(username, password, wheel, lastchance, class, pwd_save);
179 free (pwd_save);
180 #endif
182 if (password != NULL)
183 explicit_bzero(password, strlen(password));
184 if (ret != AUTH_OK)
185 fprintf(back, BI_REJECT "\n");
187 closelog();
189 exit(0);