Initial import.
[login_krb5.git] / login_passwd / login.c
blob29a9fc983190eb6f1503662583f2ca06e847c14a
1 /* $OpenBSD: login.c,v 1.10 2012/06/01 01:43:19 dlg 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 response[1024];
47 int arg_login = 0, arg_notickets = 0;
48 char invokinguser[LOGIN_NAME_MAX];
49 char *wheel = NULL, *class = NULL;
51 invokinguser[0] = '\0';
53 setpriority(PRIO_PROCESS, 0, 0);
55 openlog(NULL, LOG_ODELAY, LOG_AUTH);
57 while ((opt = getopt(argc, argv, "ds:v:")) != -1) {
58 switch (opt) {
59 case 'd':
60 back = stdout;
61 break;
62 case 's': /* service */
63 if (strcmp(optarg, "login") == 0)
64 mode = MODE_LOGIN;
65 else if (strcmp(optarg, "challenge") == 0)
66 mode = MODE_CHALLENGE;
67 else if (strcmp(optarg, "response") == 0)
68 mode = MODE_RESPONSE;
69 else {
70 syslog(LOG_ERR, "%s: invalid service", optarg);
71 exit(1);
73 break;
74 case 'v':
75 if (strncmp(optarg, "wheel=", 6) == 0)
76 wheel = optarg + 6;
77 else if (strncmp(optarg, "lastchance=", 11) == 0)
78 lastchance = (strcmp(optarg + 11, "yes") == 0);
79 else if (strcmp(optarg, "login=yes") == 0)
80 arg_login = 1;
81 else if (strcmp(optarg, "notickets=yes") == 0)
82 arg_notickets = 1;
83 else if (strncmp(optarg, "invokinguser=", 13) == 0)
84 snprintf(invokinguser, sizeof(invokinguser),
85 "%s", &optarg[13]);
86 /* Silently ignore unsupported variables */
87 break;
88 default:
89 syslog(LOG_ERR, "usage error1");
90 exit(1);
94 switch (argc - optind) {
95 case 2:
96 class = argv[optind + 1];
97 /*FALLTHROUGH*/
98 case 1:
99 username = argv[optind];
100 break;
101 default:
102 syslog(LOG_ERR, "usage error2");
103 exit(1);
106 if (back == NULL && (back = fdopen(3, "r+")) == NULL) {
107 syslog(LOG_ERR, "reopening back channel: %m");
108 exit(1);
112 * Read password, either as from the terminal or if the
113 * response mode is active from the caller program.
115 * XXX This is completely ungrokkable, and should be rewritten.
117 switch (mode) {
118 case MODE_RESPONSE: {
119 int count;
120 mode = 0;
121 count = -1;
122 while (++count < sizeof(response) &&
123 read(3, &response[count], (size_t)1) == (ssize_t)1) {
124 if (response[count] == '\0' && ++mode == 2)
125 break;
126 if (response[count] == '\0' && mode == 1) {
127 password = response + count + 1;
130 if (mode < 2) {
131 syslog(LOG_ERR, "protocol error on back channel");
132 exit(1);
134 break;
137 case MODE_LOGIN:
138 password = getpass("Password:");
139 break;
140 case MODE_CHALLENGE:
141 fprintf(back, BI_AUTH "\n");
142 exit(0);
143 break;
144 default:
145 syslog(LOG_ERR, "%d: unknown mode", mode);
146 exit(1);
147 break;
150 ret = AUTH_FAILED;
151 #ifdef KRB5
152 ret = krb5_login(username, invokinguser, password, arg_login,
153 !arg_notickets, class);
154 #endif
155 #ifdef PASSWD
156 if (ret != AUTH_OK)
157 ret = pwd_login(username, password, wheel, lastchance, class);
158 #endif
160 if (password != NULL)
161 memset(password, 0, strlen(password));
162 if (ret != AUTH_OK)
163 fprintf(back, BI_REJECT "\n");
165 closelog();
167 exit(0);