2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 RCSID("$OpenBSD: auth2.c,v 1.7 2000/05/06 17:45:36 markus Exp $");
32 #include <openssl/dsa.h>
33 #include <openssl/rsa.h>
34 #include <openssl/evp.h>
59 extern ServerOptions options;
60 extern unsigned char *session_id2;
61 extern int session_id2_len;
65 void input_service_request(int type, int plen);
66 void input_userauth_request(int type, int plen);
67 void protocol_error(int type, int plen);
70 int ssh2_auth_none(struct passwd *pw);
71 int ssh2_auth_password(struct passwd *pw);
72 int ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen);
75 struct passwd* auth_set_user(char *u, char *s);
76 int user_dsa_key_allowed(struct passwd *pw, Key *key);
78 typedef struct Authctxt Authctxt;
85 static Authctxt *authctxt = NULL;
86 static int userauth_success = 0;
89 * loop until userauth_success == TRUE
95 /* turn off skey/kerberos, not supported by SSH2 */
97 options.skey_authentication = 0;
100 options.kerberos_authentication = 0;
103 dispatch_init(&protocol_error);
104 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
105 dispatch_run(DISPATCH_BLOCK, &userauth_success);
110 protocol_error(int type, int plen)
112 log("auth: protocol error: type %d plen %d", type, plen);
113 packet_start(SSH2_MSG_UNIMPLEMENTED);
120 input_service_request(int type, int plen)
124 char *service = packet_get_string(&len);
127 if (strcmp(service, "ssh-userauth") == 0) {
128 if (!userauth_success) {
130 /* now we can handle user-auth requests */
131 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
134 /* XXX all other service requests are denied */
137 packet_start(SSH2_MSG_SERVICE_ACCEPT);
138 packet_put_cstring(service);
142 debug("bad service request %s", service);
143 packet_disconnect("bad service request %s", service);
149 input_userauth_request(int type, int plen)
151 static void (*authlog) (const char *fmt,...) = verbose;
152 static int attempt = 0;
153 unsigned int len, rlen;
154 int authenticated = 0;
155 char *raw, *user, *service, *method, *authmsg = NULL;
158 if (++attempt == AUTH_FAIL_MAX)
159 packet_disconnect("too many failed userauth_requests");
161 raw = packet_get_raw(&rlen);
163 fatal("plen != rlen");
164 user = packet_get_string(&len);
165 service = packet_get_string(&len);
166 method = packet_get_string(&len);
167 debug("userauth-request for user %s service %s method %s", user, service, method);
169 /* XXX we only allow the ssh-connection service */
170 pw = auth_set_user(user, service);
171 if (pw && strcmp(service, "ssh-connection")==0) {
172 if (strcmp(method, "none") == 0) {
173 authenticated = ssh2_auth_none(pw);
174 } else if (strcmp(method, "password") == 0) {
175 authenticated = ssh2_auth_password(pw);
176 } else if (strcmp(method, "publickey") == 0) {
177 authenticated = ssh2_auth_pubkey(pw, raw, rlen);
180 if (authenticated && pw && pw->pw_uid == 0 && !options.permit_root_login) {
182 log("ROOT LOGIN REFUSED FROM %.200s",
183 get_canonical_hostname());
187 if (authenticated && !do_pam_account(pw->pw_name, NULL))
191 /* Raise logging level */
192 if (authenticated == 1 ||
193 attempt == AUTH_FAIL_LOG ||
194 strcmp(method, "password") == 0)
197 /* Log before sending the reply */
198 if (authenticated == 1) {
199 authmsg = "Accepted";
200 } else if (authenticated == 0) {
203 authmsg = "Postponed";
205 authlog("%s %s for %.200s from %.200s port %d ssh2",
208 pw && pw->pw_uid == 0 ? "ROOT" : user,
212 /* XXX todo: check if multiple auth methods are needed */
213 if (authenticated == 1) {
214 /* turn off userauth */
215 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
216 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
219 /* now we can break out */
220 userauth_success = 1;
221 } else if (authenticated == 0) {
222 packet_start(SSH2_MSG_USERAUTH_FAILURE);
223 packet_put_cstring("publickey,password"); /* XXX dynamic */
224 packet_put_char(0); /* XXX partial success, unused */
235 ssh2_auth_none(struct passwd *pw)
239 return auth_pam_password(pw, "");
241 return auth_password(pw, "");
245 ssh2_auth_password(struct passwd *pw)
248 int authenticated = 0;
251 change = packet_get_char();
253 log("password change not supported");
254 password = packet_get_string(&len);
256 if (options.password_authentication &&
258 auth_pam_password(pw, password) == 1)
260 auth_password(pw, password) == 1)
263 memset(password, 0, len);
265 return authenticated;
268 ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen)
272 char *pkalg, *pkblob, *sig;
273 unsigned int alen, blen, slen;
275 int authenticated = 0;
277 if (options.dsa_authentication == 0) {
278 debug("pubkey auth disabled");
281 have_sig = packet_get_char();
282 pkalg = packet_get_string(&alen);
283 if (strcmp(pkalg, KEX_DSS) != 0) {
285 log("bad pkalg %s", pkalg); /*XXX*/
288 pkblob = packet_get_string(&blen);
289 key = dsa_key_from_blob(pkblob, blen);
292 sig = packet_get_string(&slen);
295 buffer_append(&b, session_id2, session_id2_len);
296 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
298 fatal("bad rlen/slen");
299 buffer_append(&b, raw, rlen - slen - 4);
303 /* test for correct signature */
304 if (user_dsa_key_allowed(pw, key) &&
305 dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
311 debug("test key...");
312 /* test whether pkalg/pkblob are acceptable */
313 /* XXX fake reply and always send PK_OK ? */
315 * XXX this allows testing whether a user is allowed
316 * to login: if you happen to have a valid pubkey this
317 * message is sent. the message is NEVER sent at all
318 * if a user is not allowed to login. is this an
321 if (user_dsa_key_allowed(pw, key)) {
322 packet_start(SSH2_MSG_USERAUTH_PK_OK);
323 packet_put_string(pkalg, alen);
324 packet_put_string(pkblob, blen);
334 return authenticated;
337 /* set and get current user */
342 return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
346 auth_set_user(char *u, char *s)
348 struct passwd *pw, *copy;
350 if (authctxt == NULL) {
351 authctxt = xmalloc(sizeof(*authctxt));
353 authctxt->user = xstrdup(u);
354 authctxt->service = xstrdup(s);
355 setproctitle("%s", u);
357 if (!pw || !allowed_user(pw)) {
358 log("auth_set_user: illegal user %s", u);
364 copy = &authctxt->pw;
365 memset(copy, 0, sizeof(*copy));
366 copy->pw_name = xstrdup(pw->pw_name);
367 copy->pw_passwd = xstrdup(pw->pw_passwd);
368 copy->pw_uid = pw->pw_uid;
369 copy->pw_gid = pw->pw_gid;
370 copy->pw_dir = xstrdup(pw->pw_dir);
371 copy->pw_shell = xstrdup(pw->pw_shell);
374 if (strcmp(u, authctxt->user) != 0 ||
375 strcmp(s, authctxt->service) != 0) {
376 log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
377 u, s, authctxt->user, authctxt->service);
381 return auth_get_user();
384 /* return 1 if user allows given key */
386 user_dsa_key_allowed(struct passwd *pw, Key *key)
388 char line[8192], file[1024];
390 unsigned int bits = -1;
392 unsigned long linenum = 0;
396 /* Temporarily use the user's uid. */
397 temporarily_use_uid(pw->pw_uid);
399 /* The authorized keys. */
400 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
401 SSH_USER_PERMITTED_KEYS2);
403 /* Fail quietly if file does not exist */
404 if (stat(file, &st) < 0) {
405 /* Restore the privileged uid. */
409 /* Open the file containing the authorized keys. */
410 f = fopen(file, "r");
412 /* Restore the privileged uid. */
416 if (options.strict_modes) {
419 /* Check open file in order to avoid open/stat races */
420 if (fstat(fileno(f), &st) < 0 ||
421 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
422 (st.st_mode & 022) != 0) {
423 snprintf(buf, sizeof buf, "DSA authentication refused for %.100s: "
424 "bad ownership or modes for '%s'.", pw->pw_name, file);
427 /* Check path to SSH_USER_PERMITTED_KEYS */
429 static const char *check[] = {
430 "", SSH_USER_DIR, NULL
432 for (i = 0; check[i]; i++) {
433 snprintf(line, sizeof line, "%.500s/%.100s",
434 pw->pw_dir, check[i]);
435 if (stat(line, &st) < 0 ||
436 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
437 (st.st_mode & 022) != 0) {
438 snprintf(buf, sizeof buf,
439 "DSA authentication refused for %.100s: "
440 "bad ownership or modes for '%s'.",
455 found = key_new(KEY_DSA);
457 while (fgets(line, sizeof(line), f)) {
460 /* Skip leading whitespace, empty and comment lines. */
461 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
463 if (!*cp || *cp == '\n' || *cp == '#')
465 bits = key_read(found, &cp);
468 if (key_equal(found, key)) {
470 debug("matching key found: file %s, line %ld",