Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 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. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 24 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 25 | #include "includes.h" |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 26 | RCSID("$OpenBSD: auth2.c,v 1.25 2001/01/08 22:29:05 markus Exp $"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | |
| 28 | #ifdef HAVE_OSF_SIA |
| 29 | # include <sia.h> |
| 30 | # include <siad.h> |
| 31 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 32 | |
| 33 | #include <openssl/dsa.h> |
| 34 | #include <openssl/rsa.h> |
| 35 | #include <openssl/evp.h> |
| 36 | |
| 37 | #include "xmalloc.h" |
| 38 | #include "rsa.h" |
| 39 | #include "ssh.h" |
| 40 | #include "pty.h" |
| 41 | #include "packet.h" |
| 42 | #include "buffer.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 43 | #include "servconf.h" |
| 44 | #include "compat.h" |
| 45 | #include "channels.h" |
| 46 | #include "bufaux.h" |
| 47 | #include "ssh2.h" |
| 48 | #include "auth.h" |
| 49 | #include "session.h" |
| 50 | #include "dispatch.h" |
| 51 | #include "auth.h" |
| 52 | #include "key.h" |
| 53 | #include "kex.h" |
| 54 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 55 | #include "uidswap.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 56 | #include "auth-options.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | |
| 58 | /* import */ |
| 59 | extern ServerOptions options; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 60 | extern u_char *session_id2; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 61 | extern int session_id2_len; |
| 62 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 63 | #ifdef WITH_AIXAUTHENTICATE |
| 64 | extern char *aixloginmsg; |
| 65 | #endif |
| 66 | #ifdef HAVE_OSF_SIA |
| 67 | extern int saved_argc; |
| 68 | extern char **saved_argv; |
| 69 | #endif |
| 70 | |
| 71 | static Authctxt *x_authctxt = NULL; |
| 72 | static int one = 1; |
| 73 | |
| 74 | typedef struct Authmethod Authmethod; |
| 75 | struct Authmethod { |
| 76 | char *name; |
| 77 | int (*userauth)(Authctxt *authctxt); |
| 78 | int *enabled; |
| 79 | }; |
| 80 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 81 | /* protocol */ |
| 82 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 83 | void input_service_request(int type, int plen, void *ctxt); |
| 84 | void input_userauth_request(int type, int plen, void *ctxt); |
| 85 | void protocol_error(int type, int plen, void *ctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 87 | |
| 88 | /* helper */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 89 | Authmethod *authmethod_lookup(const char *name); |
| 90 | struct passwd *pwcopy(struct passwd *pw); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 91 | int user_key_allowed(struct passwd *pw, Key *key); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 92 | char *authmethods_get(void); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 93 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 94 | /* auth */ |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 95 | void userauth_banner(void); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 96 | int userauth_none(Authctxt *authctxt); |
| 97 | int userauth_passwd(Authctxt *authctxt); |
| 98 | int userauth_pubkey(Authctxt *authctxt); |
| 99 | int userauth_kbdint(Authctxt *authctxt); |
| 100 | |
| 101 | Authmethod authmethods[] = { |
| 102 | {"none", |
| 103 | userauth_none, |
| 104 | &one}, |
| 105 | {"publickey", |
| 106 | userauth_pubkey, |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 107 | &options.pubkey_authentication}, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 108 | {"keyboard-interactive", |
| 109 | userauth_kbdint, |
| 110 | &options.kbd_interactive_authentication}, |
| 111 | {"password", |
| 112 | userauth_passwd, |
| 113 | &options.password_authentication}, |
| 114 | {NULL, NULL, NULL} |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 115 | }; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 116 | |
| 117 | /* |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 118 | * loop until authctxt->success == TRUE |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 119 | */ |
| 120 | |
| 121 | void |
| 122 | do_authentication2() |
| 123 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 124 | Authctxt *authctxt = xmalloc(sizeof(*authctxt)); |
| 125 | memset(authctxt, 'a', sizeof(*authctxt)); |
| 126 | authctxt->valid = 0; |
| 127 | authctxt->attempt = 0; |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 128 | authctxt->failures = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 129 | authctxt->success = 0; |
| 130 | x_authctxt = authctxt; /*XXX*/ |
| 131 | |
Damien Miller | 1cead2c | 2000-05-01 22:55:23 +1000 | [diff] [blame] | 132 | #ifdef KRB4 |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 133 | /* turn off kerberos, not supported by SSH2 */ |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 134 | options.kerberos_authentication = 0; |
Damien Miller | 1cead2c | 2000-05-01 22:55:23 +1000 | [diff] [blame] | 135 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 136 | dispatch_init(&protocol_error); |
| 137 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 138 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | do_authenticated2(); |
| 140 | } |
| 141 | |
| 142 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 143 | protocol_error(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 144 | { |
| 145 | log("auth: protocol error: type %d plen %d", type, plen); |
| 146 | packet_start(SSH2_MSG_UNIMPLEMENTED); |
| 147 | packet_put_int(0); |
| 148 | packet_send(); |
| 149 | packet_write_wait(); |
| 150 | } |
| 151 | |
| 152 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 153 | input_service_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 154 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 155 | Authctxt *authctxt = ctxt; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 156 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 157 | int accept = 0; |
| 158 | char *service = packet_get_string(&len); |
| 159 | packet_done(); |
| 160 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 161 | if (authctxt == NULL) |
| 162 | fatal("input_service_request: no authctxt"); |
| 163 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 164 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 165 | if (!authctxt->success) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 166 | accept = 1; |
| 167 | /* now we can handle user-auth requests */ |
| 168 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 169 | } |
| 170 | } |
| 171 | /* XXX all other service requests are denied */ |
| 172 | |
| 173 | if (accept) { |
| 174 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 175 | packet_put_cstring(service); |
| 176 | packet_send(); |
| 177 | packet_write_wait(); |
| 178 | } else { |
| 179 | debug("bad service request %s", service); |
| 180 | packet_disconnect("bad service request %s", service); |
| 181 | } |
| 182 | xfree(service); |
| 183 | } |
| 184 | |
| 185 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 186 | input_userauth_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 187 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 188 | Authctxt *authctxt = ctxt; |
| 189 | Authmethod *m = NULL; |
| 190 | char *user, *service, *method; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 191 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 192 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 193 | if (authctxt == NULL) |
| 194 | fatal("input_userauth_request: no authctxt"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 195 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 196 | user = packet_get_string(NULL); |
| 197 | service = packet_get_string(NULL); |
| 198 | method = packet_get_string(NULL); |
| 199 | debug("userauth-request for user %s service %s method %s", user, service, method); |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 200 | debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 201 | |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 202 | if (authctxt->attempt++ == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 203 | /* setup auth context */ |
| 204 | struct passwd *pw = NULL; |
| 205 | setproctitle("%s", user); |
| 206 | pw = getpwnam(user); |
| 207 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { |
| 208 | authctxt->pw = pwcopy(pw); |
| 209 | authctxt->valid = 1; |
| 210 | debug2("input_userauth_request: setting up authctxt for %s", user); |
| 211 | #ifdef USE_PAM |
| 212 | start_pam(pw); |
| 213 | #endif |
| 214 | } else { |
| 215 | log("input_userauth_request: illegal user %s", user); |
| 216 | } |
| 217 | authctxt->user = xstrdup(user); |
| 218 | authctxt->service = xstrdup(service); |
| 219 | } else if (authctxt->valid) { |
| 220 | if (strcmp(user, authctxt->user) != 0 || |
| 221 | strcmp(service, authctxt->service) != 0) { |
| 222 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", |
| 223 | user, service, authctxt->user, authctxt->service); |
| 224 | authctxt->valid = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 225 | } |
| 226 | } |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 227 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 228 | m = authmethod_lookup(method); |
| 229 | if (m != NULL) { |
| 230 | debug2("input_userauth_request: try method %s", method); |
| 231 | authenticated = m->userauth(authctxt); |
| 232 | } else { |
| 233 | debug2("input_userauth_request: unsupported method %s", method); |
| 234 | } |
| 235 | if (!authctxt->valid && authenticated == 1) { |
| 236 | log("input_userauth_request: INTERNAL ERROR: authenticated invalid user %s service %s", user, method); |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 237 | authenticated = 0; |
| 238 | } |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 239 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 240 | /* Special handling for root */ |
| 241 | if (authenticated == 1 && |
| 242 | authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 243 | authenticated = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 244 | log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname()); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | #ifdef USE_PAM |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame] | 248 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 249 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 250 | #endif /* USE_PAM */ |
| 251 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 252 | /* Log before sending the reply */ |
| 253 | userauth_log(authctxt, authenticated, method); |
| 254 | userauth_reply(authctxt, authenticated); |
| 255 | |
| 256 | xfree(service); |
| 257 | xfree(user); |
| 258 | xfree(method); |
| 259 | } |
| 260 | |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 261 | void |
| 262 | userauth_banner(void) |
| 263 | { |
| 264 | struct stat st; |
| 265 | char *banner = NULL; |
| 266 | off_t len, n; |
| 267 | int fd; |
| 268 | |
| 269 | if (options.banner == NULL || (datafellows & SSH_BUG_BANNER)) |
| 270 | return; |
| 271 | if ((fd = open(options.banner, O_RDONLY)) < 0) { |
| 272 | error("userauth_banner: open %s failed: %s", |
| 273 | options.banner, strerror(errno)); |
| 274 | return; |
| 275 | } |
| 276 | if (fstat(fd, &st) < 0) |
| 277 | goto done; |
| 278 | len = st.st_size; |
| 279 | banner = xmalloc(len + 1); |
| 280 | if ((n = read(fd, banner, len)) < 0) |
| 281 | goto done; |
| 282 | banner[n] = '\0'; |
| 283 | packet_start(SSH2_MSG_USERAUTH_BANNER); |
| 284 | packet_put_cstring(banner); |
| 285 | packet_put_cstring(""); /* language, unused */ |
| 286 | packet_send(); |
| 287 | debug("userauth_banner: sent"); |
| 288 | done: |
| 289 | if (banner) |
| 290 | xfree(banner); |
| 291 | close(fd); |
| 292 | return; |
| 293 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 294 | |
| 295 | void |
| 296 | userauth_log(Authctxt *authctxt, int authenticated, char *method) |
| 297 | { |
| 298 | void (*authlog) (const char *fmt,...) = verbose; |
| 299 | char *user = NULL, *authmsg = NULL; |
| 300 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 301 | /* Raise logging level */ |
| 302 | if (authenticated == 1 || |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 303 | !authctxt->valid || |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 304 | authctxt->failures >= AUTH_FAIL_LOG || |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 305 | strcmp(method, "password") == 0) |
| 306 | authlog = log; |
| 307 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 308 | if (authenticated == 1) { |
| 309 | authmsg = "Accepted"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 310 | } else if (authenticated == 0) { |
| 311 | authmsg = "Failed"; |
| 312 | } else { |
| 313 | authmsg = "Postponed"; |
| 314 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 315 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 316 | if (authctxt->valid) { |
| 317 | user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user; |
| 318 | } else { |
| 319 | user = "NOUSER"; |
| 320 | } |
| 321 | |
| 322 | authlog("%s %s for %.200s from %.200s port %d ssh2", |
| 323 | authmsg, |
| 324 | method, |
| 325 | user, |
| 326 | get_remote_ipaddr(), |
| 327 | get_remote_port()); |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | userauth_reply(Authctxt *authctxt, int authenticated) |
| 332 | { |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 333 | char *methods; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 334 | /* XXX todo: check if multiple auth methods are needed */ |
| 335 | if (authenticated == 1) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 336 | #ifdef WITH_AIXAUTHENTICATE |
| 337 | /* We don't have a pty yet, so just label the line as "ssh" */ |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame] | 338 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
| 339 | get_canonical_hostname(), "ssh", &aixloginmsg) < 0) |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 340 | aixloginmsg = NULL; |
| 341 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 342 | /* turn off userauth */ |
| 343 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); |
| 344 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 345 | packet_send(); |
| 346 | packet_write_wait(); |
| 347 | /* now we can break out */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 348 | authctxt->success = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 349 | } else if (authenticated == 0) { |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 350 | if (authctxt->failures++ >= AUTH_FAIL_MAX) |
| 351 | packet_disconnect("too many failed userauth_requests"); |
| 352 | methods = authmethods_get(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 353 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 354 | packet_put_cstring(methods); |
| 355 | packet_put_char(0); /* XXX partial success, unused */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 356 | packet_send(); |
| 357 | packet_write_wait(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 358 | xfree(methods); |
| 359 | } else { |
| 360 | /* do nothing, we did already send a reply */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 361 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 365 | userauth_none(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 366 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 367 | /* disable method "none", only allowed one time */ |
| 368 | Authmethod *m = authmethod_lookup("none"); |
| 369 | if (m != NULL) |
| 370 | m->enabled = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 371 | packet_done(); |
Ben Lindstrom | 48bd7c1 | 2001-01-09 00:35:42 +0000 | [diff] [blame] | 372 | userauth_banner(); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 373 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 374 | if (authctxt->valid == 0) |
| 375 | return(0); |
| 376 | |
| 377 | #ifdef HAVE_CYGWIN |
| 378 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) |
| 379 | return(0); |
| 380 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 381 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 382 | return auth_pam_password(authctxt->pw, ""); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 383 | #elif defined(HAVE_OSF_SIA) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 384 | return (sia_validate_user(NULL, saved_argc, saved_argv, |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame] | 385 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
| 386 | NULL, 0, NULL, "") == SIASUCCESS); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 387 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 388 | return auth_password(authctxt->pw, ""); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 389 | #endif /* USE_PAM */ |
| 390 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 391 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 392 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 393 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 394 | { |
| 395 | char *password; |
| 396 | int authenticated = 0; |
| 397 | int change; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 398 | u_int len; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 399 | change = packet_get_char(); |
| 400 | if (change) |
| 401 | log("password change not supported"); |
| 402 | password = packet_get_string(&len); |
| 403 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 404 | if (authctxt->valid && |
| 405 | #ifdef HAVE_CYGWIN |
| 406 | check_nt_auth(1, authctxt->pw->pw_uid) && |
| 407 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 408 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 409 | auth_pam_password(authctxt->pw, password) == 1) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 410 | #elif defined(HAVE_OSF_SIA) |
| 411 | sia_validate_user(NULL, saved_argc, saved_argv, |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame] | 412 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
| 413 | NULL, 0, NULL, password) == SIASUCCESS) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 414 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 415 | auth_password(authctxt->pw, password) == 1) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 416 | #endif /* USE_PAM */ |
| 417 | authenticated = 1; |
| 418 | memset(password, 0, len); |
| 419 | xfree(password); |
| 420 | return authenticated; |
| 421 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 422 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 423 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 424 | userauth_kbdint(Authctxt *authctxt) |
| 425 | { |
| 426 | int authenticated = 0; |
| 427 | char *lang = NULL; |
| 428 | char *devs = NULL; |
| 429 | |
| 430 | lang = packet_get_string(NULL); |
| 431 | devs = packet_get_string(NULL); |
| 432 | packet_done(); |
| 433 | |
| 434 | debug("keyboard-interactive language %s devs %s", lang, devs); |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 435 | #ifdef USE_PAM |
| 436 | if (authenticated == 0) |
| 437 | authenticated = auth2_pam(authctxt); |
| 438 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 439 | #ifdef SKEY |
| 440 | /* XXX hardcoded, we should look at devs */ |
Damien Miller | b848158 | 2000-12-03 11:51:51 +1100 | [diff] [blame] | 441 | if (authenticated == 0) |
| 442 | if (options.skey_authentication != 0) |
| 443 | authenticated = auth2_skey(authctxt); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 444 | #endif |
| 445 | xfree(lang); |
| 446 | xfree(devs); |
| 447 | #ifdef HAVE_CYGWIN |
| 448 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 449 | return(0); |
| 450 | #endif |
| 451 | return authenticated; |
| 452 | } |
| 453 | |
| 454 | int |
| 455 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 456 | { |
| 457 | Buffer b; |
| 458 | Key *key; |
| 459 | char *pkalg, *pkblob, *sig; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 460 | u_int alen, blen, slen; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 461 | int have_sig, pktype; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 462 | int authenticated = 0; |
| 463 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 464 | if (!authctxt->valid) { |
| 465 | debug2("userauth_pubkey: disabled because of invalid user"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | have_sig = packet_get_char(); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 469 | if (datafellows & SSH_BUG_PKAUTH) { |
| 470 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 471 | /* no explicit pkalg given */ |
| 472 | pkblob = packet_get_string(&blen); |
| 473 | buffer_init(&b); |
| 474 | buffer_append(&b, pkblob, blen); |
| 475 | /* so we have to extract the pkalg from the pkblob */ |
| 476 | pkalg = buffer_get_string(&b, &alen); |
| 477 | buffer_free(&b); |
| 478 | } else { |
| 479 | pkalg = packet_get_string(&alen); |
| 480 | pkblob = packet_get_string(&blen); |
| 481 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 482 | pktype = key_type_from_name(pkalg); |
| 483 | if (pktype == KEY_UNSPEC) { |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 484 | /* this is perfectly legal */ |
| 485 | log("userauth_pubkey: unsupported public key algorithm: %s", pkalg); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 486 | xfree(pkalg); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 487 | xfree(pkblob); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 488 | return 0; |
| 489 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 490 | key = key_from_blob(pkblob, blen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 491 | if (key != NULL) { |
| 492 | if (have_sig) { |
| 493 | sig = packet_get_string(&slen); |
| 494 | packet_done(); |
| 495 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 496 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 497 | buffer_append(&b, session_id2, session_id2_len); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 498 | } else { |
| 499 | buffer_put_string(&b, session_id2, session_id2_len); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 500 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 501 | /* reconstruct packet */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 502 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 503 | buffer_put_cstring(&b, authctxt->user); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 504 | buffer_put_cstring(&b, |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 505 | datafellows & SSH_BUG_PKSERVICE ? |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 506 | "ssh-userauth" : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 507 | authctxt->service); |
Ben Lindstrom | d121f61 | 2000-12-03 17:00:47 +0000 | [diff] [blame] | 508 | if (datafellows & SSH_BUG_PKAUTH) { |
| 509 | buffer_put_char(&b, have_sig); |
| 510 | } else { |
| 511 | buffer_put_cstring(&b, "publickey"); |
| 512 | buffer_put_char(&b, have_sig); |
| 513 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 514 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 515 | buffer_put_string(&b, pkblob, blen); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 516 | #ifdef DEBUG_PK |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 517 | buffer_dump(&b); |
| 518 | #endif |
| 519 | /* test for correct signature */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 520 | if (user_key_allowed(authctxt->pw, key) && |
| 521 | key_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 522 | authenticated = 1; |
| 523 | buffer_clear(&b); |
| 524 | xfree(sig); |
| 525 | } else { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 526 | debug("test whether pkalg/pkblob are acceptable"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 527 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 528 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 529 | /* XXX fake reply and always send PK_OK ? */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 530 | /* |
| 531 | * XXX this allows testing whether a user is allowed |
| 532 | * to login: if you happen to have a valid pubkey this |
| 533 | * message is sent. the message is NEVER sent at all |
| 534 | * if a user is not allowed to login. is this an |
| 535 | * issue? -markus |
| 536 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 537 | if (user_key_allowed(authctxt->pw, key)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 538 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 539 | packet_put_string(pkalg, alen); |
| 540 | packet_put_string(pkblob, blen); |
| 541 | packet_send(); |
| 542 | packet_write_wait(); |
| 543 | authenticated = -1; |
| 544 | } |
| 545 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 546 | if (authenticated != 1) |
| 547 | auth_clear_options(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 548 | key_free(key); |
| 549 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 550 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 551 | xfree(pkalg); |
| 552 | xfree(pkblob); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 553 | #ifdef HAVE_CYGWIN |
| 554 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 555 | return(0); |
| 556 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 557 | return authenticated; |
| 558 | } |
| 559 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 560 | /* get current user */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 561 | |
| 562 | struct passwd* |
| 563 | auth_get_user(void) |
| 564 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 565 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 566 | } |
| 567 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 568 | #define DELIM "," |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 569 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 570 | char * |
| 571 | authmethods_get(void) |
| 572 | { |
| 573 | Authmethod *method = NULL; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 574 | u_int size = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 575 | char *list; |
| 576 | |
| 577 | for (method = authmethods; method->name != NULL; method++) { |
| 578 | if (strcmp(method->name, "none") == 0) |
| 579 | continue; |
| 580 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 581 | if (size != 0) |
| 582 | size += strlen(DELIM); |
| 583 | size += strlen(method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 584 | } |
| 585 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 586 | size++; /* trailing '\0' */ |
| 587 | list = xmalloc(size); |
| 588 | list[0] = '\0'; |
| 589 | |
| 590 | for (method = authmethods; method->name != NULL; method++) { |
| 591 | if (strcmp(method->name, "none") == 0) |
| 592 | continue; |
| 593 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 594 | if (list[0] != '\0') |
| 595 | strlcat(list, DELIM, size); |
| 596 | strlcat(list, method->name, size); |
| 597 | } |
| 598 | } |
| 599 | return list; |
| 600 | } |
| 601 | |
| 602 | Authmethod * |
| 603 | authmethod_lookup(const char *name) |
| 604 | { |
| 605 | Authmethod *method = NULL; |
| 606 | if (name != NULL) |
| 607 | for (method = authmethods; method->name != NULL; method++) |
| 608 | if (method->enabled != NULL && |
| 609 | *(method->enabled) != 0 && |
| 610 | strcmp(name, method->name) == 0) |
| 611 | return method; |
| 612 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 613 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* return 1 if user allows given key */ |
| 617 | int |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 618 | user_key_allowed(struct passwd *pw, Key *key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 619 | { |
| 620 | char line[8192], file[1024]; |
| 621 | int found_key = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 622 | FILE *f; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 623 | u_long linenum = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 624 | struct stat st; |
| 625 | Key *found; |
| 626 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 627 | if (pw == NULL) |
| 628 | return 0; |
| 629 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 630 | /* Temporarily use the user's uid. */ |
| 631 | temporarily_use_uid(pw->pw_uid); |
| 632 | |
| 633 | /* The authorized keys. */ |
| 634 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, |
| 635 | SSH_USER_PERMITTED_KEYS2); |
| 636 | |
| 637 | /* Fail quietly if file does not exist */ |
| 638 | if (stat(file, &st) < 0) { |
| 639 | /* Restore the privileged uid. */ |
| 640 | restore_uid(); |
| 641 | return 0; |
| 642 | } |
| 643 | /* Open the file containing the authorized keys. */ |
| 644 | f = fopen(file, "r"); |
| 645 | if (!f) { |
| 646 | /* Restore the privileged uid. */ |
| 647 | restore_uid(); |
| 648 | return 0; |
| 649 | } |
| 650 | if (options.strict_modes) { |
| 651 | int fail = 0; |
| 652 | char buf[1024]; |
| 653 | /* Check open file in order to avoid open/stat races */ |
| 654 | if (fstat(fileno(f), &st) < 0 || |
| 655 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 656 | (st.st_mode & 022) != 0) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 657 | snprintf(buf, sizeof buf, |
| 658 | "%s authentication refused for %.100s: " |
| 659 | "bad ownership or modes for '%s'.", |
| 660 | key_type(key), pw->pw_name, file); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 661 | fail = 1; |
| 662 | } else { |
| 663 | /* Check path to SSH_USER_PERMITTED_KEYS */ |
| 664 | int i; |
| 665 | static const char *check[] = { |
| 666 | "", SSH_USER_DIR, NULL |
| 667 | }; |
| 668 | for (i = 0; check[i]; i++) { |
| 669 | snprintf(line, sizeof line, "%.500s/%.100s", |
| 670 | pw->pw_dir, check[i]); |
| 671 | if (stat(line, &st) < 0 || |
| 672 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 673 | (st.st_mode & 022) != 0) { |
| 674 | snprintf(buf, sizeof buf, |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 675 | "%s authentication refused for %.100s: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 676 | "bad ownership or modes for '%s'.", |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 677 | key_type(key), pw->pw_name, line); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 678 | fail = 1; |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | if (fail) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 684 | fclose(f); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 685 | log("%s",buf); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 686 | restore_uid(); |
| 687 | return 0; |
| 688 | } |
| 689 | } |
| 690 | found_key = 0; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 691 | found = key_new(key->type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 692 | |
| 693 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 694 | char *cp, *options = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 695 | linenum++; |
| 696 | /* Skip leading whitespace, empty and comment lines. */ |
| 697 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 698 | ; |
| 699 | if (!*cp || *cp == '\n' || *cp == '#') |
| 700 | continue; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 701 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 702 | if (key_read(found, &cp) == -1) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 703 | /* no key? check if there are options for this key */ |
| 704 | int quoted = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 705 | debug2("user_key_allowed: check options: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 706 | options = cp; |
| 707 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 708 | if (*cp == '\\' && cp[1] == '"') |
| 709 | cp++; /* Skip both */ |
| 710 | else if (*cp == '"') |
| 711 | quoted = !quoted; |
| 712 | } |
| 713 | /* Skip remaining whitespace. */ |
| 714 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 715 | ; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 716 | if (key_read(found, &cp) == -1) { |
| 717 | debug2("user_key_allowed: advance: '%s'", cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 718 | /* still no key? advance to next line*/ |
| 719 | continue; |
| 720 | } |
| 721 | } |
| 722 | if (key_equal(found, key) && |
| 723 | auth_parse_options(pw, options, linenum) == 1) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 724 | found_key = 1; |
| 725 | debug("matching key found: file %s, line %ld", |
| 726 | file, linenum); |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | restore_uid(); |
| 731 | fclose(f); |
| 732 | key_free(found); |
| 733 | return found_key; |
| 734 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 735 | |
| 736 | struct passwd * |
| 737 | pwcopy(struct passwd *pw) |
| 738 | { |
| 739 | struct passwd *copy = xmalloc(sizeof(*copy)); |
| 740 | memset(copy, 0, sizeof(*copy)); |
| 741 | copy->pw_name = xstrdup(pw->pw_name); |
| 742 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
| 743 | copy->pw_uid = pw->pw_uid; |
| 744 | copy->pw_gid = pw->pw_gid; |
| 745 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
| 746 | copy->pw_class = xstrdup(pw->pw_class); |
| 747 | #endif |
| 748 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 749 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 750 | return copy; |
| 751 | } |