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" |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 26 | RCSID("$OpenBSD: auth2.c,v 1.20 2000/10/14 12:16:56 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 | |
| 55 | #include "dsa.h" |
| 56 | #include "uidswap.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 57 | #include "auth-options.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | |
| 59 | /* import */ |
| 60 | extern ServerOptions options; |
| 61 | extern unsigned char *session_id2; |
| 62 | extern int session_id2_len; |
| 63 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 64 | #ifdef WITH_AIXAUTHENTICATE |
| 65 | extern char *aixloginmsg; |
| 66 | #endif |
| 67 | #ifdef HAVE_OSF_SIA |
| 68 | extern int saved_argc; |
| 69 | extern char **saved_argv; |
| 70 | #endif |
| 71 | |
| 72 | static Authctxt *x_authctxt = NULL; |
| 73 | static int one = 1; |
| 74 | |
| 75 | typedef struct Authmethod Authmethod; |
| 76 | struct Authmethod { |
| 77 | char *name; |
| 78 | int (*userauth)(Authctxt *authctxt); |
| 79 | int *enabled; |
| 80 | }; |
| 81 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 82 | /* protocol */ |
| 83 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 84 | void input_service_request(int type, int plen, void *ctxt); |
| 85 | void input_userauth_request(int type, int plen, void *ctxt); |
| 86 | void protocol_error(int type, int plen, void *ctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 87 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 88 | |
| 89 | /* helper */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 90 | Authmethod *authmethod_lookup(const char *name); |
| 91 | struct passwd *pwcopy(struct passwd *pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 92 | int user_dsa_key_allowed(struct passwd *pw, Key *key); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 93 | char *authmethods_get(void); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 94 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 95 | /* auth */ |
| 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, |
| 107 | &options.dsa_authentication}, |
| 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; |
| 128 | authctxt->success = 0; |
| 129 | x_authctxt = authctxt; /*XXX*/ |
| 130 | |
Damien Miller | 1cead2c | 2000-05-01 22:55:23 +1000 | [diff] [blame] | 131 | #ifdef KRB4 |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 132 | /* turn off kerberos, not supported by SSH2 */ |
Damien Miller | 35dabd0 | 2000-05-01 21:10:33 +1000 | [diff] [blame] | 133 | options.kerberos_authentication = 0; |
Damien Miller | 1cead2c | 2000-05-01 22:55:23 +1000 | [diff] [blame] | 134 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 135 | dispatch_init(&protocol_error); |
| 136 | dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 137 | dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 138 | do_authenticated2(); |
| 139 | } |
| 140 | |
| 141 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 142 | protocol_error(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 143 | { |
| 144 | log("auth: protocol error: type %d plen %d", type, plen); |
| 145 | packet_start(SSH2_MSG_UNIMPLEMENTED); |
| 146 | packet_put_int(0); |
| 147 | packet_send(); |
| 148 | packet_write_wait(); |
| 149 | } |
| 150 | |
| 151 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 152 | input_service_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 153 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 154 | Authctxt *authctxt = ctxt; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 155 | unsigned int len; |
| 156 | int accept = 0; |
| 157 | char *service = packet_get_string(&len); |
| 158 | packet_done(); |
| 159 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 160 | if (authctxt == NULL) |
| 161 | fatal("input_service_request: no authctxt"); |
| 162 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 163 | if (strcmp(service, "ssh-userauth") == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 164 | if (!authctxt->success) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 165 | accept = 1; |
| 166 | /* now we can handle user-auth requests */ |
| 167 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); |
| 168 | } |
| 169 | } |
| 170 | /* XXX all other service requests are denied */ |
| 171 | |
| 172 | if (accept) { |
| 173 | packet_start(SSH2_MSG_SERVICE_ACCEPT); |
| 174 | packet_put_cstring(service); |
| 175 | packet_send(); |
| 176 | packet_write_wait(); |
| 177 | } else { |
| 178 | debug("bad service request %s", service); |
| 179 | packet_disconnect("bad service request %s", service); |
| 180 | } |
| 181 | xfree(service); |
| 182 | } |
| 183 | |
| 184 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 185 | input_userauth_request(int type, int plen, void *ctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 186 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 187 | Authctxt *authctxt = ctxt; |
| 188 | Authmethod *m = NULL; |
| 189 | char *user, *service, *method; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 190 | int authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 191 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 192 | if (authctxt == NULL) |
| 193 | fatal("input_userauth_request: no authctxt"); |
| 194 | if (authctxt->attempt++ >= AUTH_FAIL_MAX) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 195 | #ifdef WITH_AIXAUTHENTICATE |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame^] | 196 | loginfailed(authctxt->user?authctxt->user:"NOUSER", |
| 197 | get_canonical_hostname(), "ssh"); |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 198 | #endif /* WITH_AIXAUTHENTICATE */ |
| 199 | packet_disconnect("too many failed userauth_requests"); |
| 200 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 201 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 202 | user = packet_get_string(NULL); |
| 203 | service = packet_get_string(NULL); |
| 204 | method = packet_get_string(NULL); |
| 205 | debug("userauth-request for user %s service %s method %s", user, service, method); |
| 206 | debug("attempt #%d", authctxt->attempt); |
| 207 | |
| 208 | if (authctxt->attempt == 1) { |
| 209 | /* setup auth context */ |
| 210 | struct passwd *pw = NULL; |
| 211 | setproctitle("%s", user); |
| 212 | pw = getpwnam(user); |
| 213 | if (pw && allowed_user(pw) && strcmp(service, "ssh-connection")==0) { |
| 214 | authctxt->pw = pwcopy(pw); |
| 215 | authctxt->valid = 1; |
| 216 | debug2("input_userauth_request: setting up authctxt for %s", user); |
| 217 | #ifdef USE_PAM |
| 218 | start_pam(pw); |
| 219 | #endif |
| 220 | } else { |
| 221 | log("input_userauth_request: illegal user %s", user); |
| 222 | } |
| 223 | authctxt->user = xstrdup(user); |
| 224 | authctxt->service = xstrdup(service); |
| 225 | } else if (authctxt->valid) { |
| 226 | if (strcmp(user, authctxt->user) != 0 || |
| 227 | strcmp(service, authctxt->service) != 0) { |
| 228 | log("input_userauth_request: missmatch: (%s,%s)!=(%s,%s)", |
| 229 | user, service, authctxt->user, authctxt->service); |
| 230 | authctxt->valid = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 231 | } |
| 232 | } |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 233 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 234 | m = authmethod_lookup(method); |
| 235 | if (m != NULL) { |
| 236 | debug2("input_userauth_request: try method %s", method); |
| 237 | authenticated = m->userauth(authctxt); |
| 238 | } else { |
| 239 | debug2("input_userauth_request: unsupported method %s", method); |
| 240 | } |
| 241 | if (!authctxt->valid && authenticated == 1) { |
| 242 | 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] | 243 | authenticated = 0; |
| 244 | } |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 245 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 246 | /* Special handling for root */ |
| 247 | if (authenticated == 1 && |
| 248 | authctxt->valid && authctxt->pw->pw_uid == 0 && !options.permit_root_login) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 249 | authenticated = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 250 | log("ROOT LOGIN REFUSED FROM %.200s", get_canonical_hostname()); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #ifdef USE_PAM |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame^] | 254 | if (authenticated && authctxt->user && !do_pam_account(authctxt->user, NULL)) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 255 | authenticated = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 256 | #endif /* USE_PAM */ |
| 257 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 258 | /* Log before sending the reply */ |
| 259 | userauth_log(authctxt, authenticated, method); |
| 260 | userauth_reply(authctxt, authenticated); |
| 261 | |
| 262 | xfree(service); |
| 263 | xfree(user); |
| 264 | xfree(method); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | void |
| 269 | userauth_log(Authctxt *authctxt, int authenticated, char *method) |
| 270 | { |
| 271 | void (*authlog) (const char *fmt,...) = verbose; |
| 272 | char *user = NULL, *authmsg = NULL; |
| 273 | |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 274 | /* Raise logging level */ |
| 275 | if (authenticated == 1 || |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 276 | !authctxt->valid || |
| 277 | authctxt->attempt >= AUTH_FAIL_LOG || |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 278 | strcmp(method, "password") == 0) |
| 279 | authlog = log; |
| 280 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 281 | if (authenticated == 1) { |
| 282 | authmsg = "Accepted"; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 283 | } else if (authenticated == 0) { |
| 284 | authmsg = "Failed"; |
| 285 | } else { |
| 286 | authmsg = "Postponed"; |
| 287 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 288 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 289 | if (authctxt->valid) { |
| 290 | user = authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user; |
| 291 | } else { |
| 292 | user = "NOUSER"; |
| 293 | } |
| 294 | |
| 295 | authlog("%s %s for %.200s from %.200s port %d ssh2", |
| 296 | authmsg, |
| 297 | method, |
| 298 | user, |
| 299 | get_remote_ipaddr(), |
| 300 | get_remote_port()); |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | userauth_reply(Authctxt *authctxt, int authenticated) |
| 305 | { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 306 | /* XXX todo: check if multiple auth methods are needed */ |
| 307 | if (authenticated == 1) { |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 308 | #ifdef WITH_AIXAUTHENTICATE |
| 309 | /* 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^] | 310 | if (loginsuccess(authctxt->user?authctxt->user:"NOUSER", |
| 311 | get_canonical_hostname(), "ssh", &aixloginmsg) < 0) |
Damien Miller | d2c208a | 2000-05-17 22:00:02 +1000 | [diff] [blame] | 312 | aixloginmsg = NULL; |
| 313 | #endif /* WITH_AIXAUTHENTICATE */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 314 | /* turn off userauth */ |
| 315 | dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error); |
| 316 | packet_start(SSH2_MSG_USERAUTH_SUCCESS); |
| 317 | packet_send(); |
| 318 | packet_write_wait(); |
| 319 | /* now we can break out */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 320 | authctxt->success = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 321 | } else if (authenticated == 0) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 322 | char *methods = authmethods_get(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 323 | packet_start(SSH2_MSG_USERAUTH_FAILURE); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 324 | packet_put_cstring(methods); |
| 325 | packet_put_char(0); /* XXX partial success, unused */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 326 | packet_send(); |
| 327 | packet_write_wait(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 328 | xfree(methods); |
| 329 | } else { |
| 330 | /* do nothing, we did already send a reply */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 331 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 335 | userauth_none(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 336 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 337 | /* disable method "none", only allowed one time */ |
| 338 | Authmethod *m = authmethod_lookup("none"); |
| 339 | if (m != NULL) |
| 340 | m->enabled = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 341 | packet_done(); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 342 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 343 | if (authctxt->valid == 0) |
| 344 | return(0); |
| 345 | |
| 346 | #ifdef HAVE_CYGWIN |
| 347 | if (check_nt_auth(1, authctxt->pw->pw_uid) == 0) |
| 348 | return(0); |
| 349 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 350 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 351 | return auth_pam_password(authctxt->pw, ""); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 352 | #elif defined(HAVE_OSF_SIA) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 353 | return (sia_validate_user(NULL, saved_argc, saved_argv, |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame^] | 354 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
| 355 | NULL, 0, NULL, "") == SIASUCCESS); |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 356 | #else /* !HAVE_OSF_SIA && !USE_PAM */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 357 | return auth_password(authctxt->pw, ""); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 358 | #endif /* USE_PAM */ |
| 359 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 360 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 361 | int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 362 | userauth_passwd(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 363 | { |
| 364 | char *password; |
| 365 | int authenticated = 0; |
| 366 | int change; |
| 367 | unsigned int len; |
| 368 | change = packet_get_char(); |
| 369 | if (change) |
| 370 | log("password change not supported"); |
| 371 | password = packet_get_string(&len); |
| 372 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 373 | if (authctxt->valid && |
| 374 | #ifdef HAVE_CYGWIN |
| 375 | check_nt_auth(1, authctxt->pw->pw_uid) && |
| 376 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 377 | #ifdef USE_PAM |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 378 | auth_pam_password(authctxt->pw, password) == 1) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 379 | #elif defined(HAVE_OSF_SIA) |
| 380 | sia_validate_user(NULL, saved_argc, saved_argv, |
Damien Miller | d425d4d | 2000-10-28 21:05:57 +1100 | [diff] [blame^] | 381 | get_canonical_hostname(), authctxt->user?authctxt->user:"NOUSER", |
| 382 | NULL, 0, NULL, password) == SIASUCCESS) |
Damien Miller | b8c656e | 2000-06-28 15:22:41 +1000 | [diff] [blame] | 383 | #else /* !USE_PAM && !HAVE_OSF_SIA */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 384 | auth_password(authctxt->pw, password) == 1) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 385 | #endif /* USE_PAM */ |
| 386 | authenticated = 1; |
| 387 | memset(password, 0, len); |
| 388 | xfree(password); |
| 389 | return authenticated; |
| 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_kbdint(Authctxt *authctxt) |
| 394 | { |
| 395 | int authenticated = 0; |
| 396 | char *lang = NULL; |
| 397 | char *devs = NULL; |
| 398 | |
| 399 | lang = packet_get_string(NULL); |
| 400 | devs = packet_get_string(NULL); |
| 401 | packet_done(); |
| 402 | |
| 403 | debug("keyboard-interactive language %s devs %s", lang, devs); |
| 404 | #ifdef SKEY |
| 405 | /* XXX hardcoded, we should look at devs */ |
| 406 | if (options.skey_authentication != 0) |
| 407 | authenticated = auth2_skey(authctxt); |
| 408 | #endif |
| 409 | xfree(lang); |
| 410 | xfree(devs); |
| 411 | #ifdef HAVE_CYGWIN |
| 412 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 413 | return(0); |
| 414 | #endif |
| 415 | return authenticated; |
| 416 | } |
| 417 | |
| 418 | int |
| 419 | userauth_pubkey(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 420 | { |
| 421 | Buffer b; |
| 422 | Key *key; |
| 423 | char *pkalg, *pkblob, *sig; |
| 424 | unsigned int alen, blen, slen; |
| 425 | int have_sig; |
| 426 | int authenticated = 0; |
| 427 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 428 | if (!authctxt->valid) { |
| 429 | debug2("userauth_pubkey: disabled because of invalid user"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | have_sig = packet_get_char(); |
| 433 | pkalg = packet_get_string(&alen); |
| 434 | if (strcmp(pkalg, KEX_DSS) != 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 435 | log("bad pkalg %s", pkalg); /*XXX*/ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 436 | xfree(pkalg); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | pkblob = packet_get_string(&blen); |
| 440 | key = dsa_key_from_blob(pkblob, blen); |
| 441 | if (key != NULL) { |
| 442 | if (have_sig) { |
| 443 | sig = packet_get_string(&slen); |
| 444 | packet_done(); |
| 445 | buffer_init(&b); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 446 | if (datafellows & SSH_OLD_SESSIONID) { |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 447 | buffer_append(&b, session_id2, session_id2_len); |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 448 | } else { |
| 449 | buffer_put_string(&b, session_id2, session_id2_len); |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 450 | } |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 451 | /* reconstruct packet */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 452 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 453 | buffer_put_cstring(&b, authctxt->user); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 454 | buffer_put_cstring(&b, |
| 455 | datafellows & SSH_BUG_PUBKEYAUTH ? |
| 456 | "ssh-userauth" : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 457 | authctxt->service); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 458 | buffer_put_cstring(&b, "publickey"); |
| 459 | buffer_put_char(&b, have_sig); |
| 460 | buffer_put_cstring(&b, KEX_DSS); |
| 461 | buffer_put_string(&b, pkblob, blen); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 462 | #ifdef DEBUG_DSS |
| 463 | buffer_dump(&b); |
| 464 | #endif |
| 465 | /* test for correct signature */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 466 | if (user_dsa_key_allowed(authctxt->pw, key) && |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 467 | dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1) |
| 468 | authenticated = 1; |
| 469 | buffer_clear(&b); |
| 470 | xfree(sig); |
| 471 | } else { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 472 | debug("test whether pkalg/pkblob are acceptable"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 473 | packet_done(); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 474 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 475 | /* XXX fake reply and always send PK_OK ? */ |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 476 | /* |
| 477 | * XXX this allows testing whether a user is allowed |
| 478 | * to login: if you happen to have a valid pubkey this |
| 479 | * message is sent. the message is NEVER sent at all |
| 480 | * if a user is not allowed to login. is this an |
| 481 | * issue? -markus |
| 482 | */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 483 | if (user_dsa_key_allowed(authctxt->pw, key)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 484 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 485 | packet_put_string(pkalg, alen); |
| 486 | packet_put_string(pkblob, blen); |
| 487 | packet_send(); |
| 488 | packet_write_wait(); |
| 489 | authenticated = -1; |
| 490 | } |
| 491 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 492 | if (authenticated != 1) |
| 493 | auth_clear_options(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 494 | key_free(key); |
| 495 | } |
| 496 | xfree(pkalg); |
| 497 | xfree(pkblob); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 498 | #ifdef HAVE_CYGWIN |
| 499 | if (check_nt_auth(0, authctxt->pw->pw_uid) == 0) |
| 500 | return(0); |
| 501 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 502 | return authenticated; |
| 503 | } |
| 504 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 505 | /* get current user */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 506 | |
| 507 | struct passwd* |
| 508 | auth_get_user(void) |
| 509 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 510 | return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 511 | } |
| 512 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 513 | #define DELIM "," |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 514 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 515 | char * |
| 516 | authmethods_get(void) |
| 517 | { |
| 518 | Authmethod *method = NULL; |
| 519 | unsigned int size = 0; |
| 520 | char *list; |
| 521 | |
| 522 | for (method = authmethods; method->name != NULL; method++) { |
| 523 | if (strcmp(method->name, "none") == 0) |
| 524 | continue; |
| 525 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 526 | if (size != 0) |
| 527 | size += strlen(DELIM); |
| 528 | size += strlen(method->name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 529 | } |
| 530 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 531 | size++; /* trailing '\0' */ |
| 532 | list = xmalloc(size); |
| 533 | list[0] = '\0'; |
| 534 | |
| 535 | for (method = authmethods; method->name != NULL; method++) { |
| 536 | if (strcmp(method->name, "none") == 0) |
| 537 | continue; |
| 538 | if (method->enabled != NULL && *(method->enabled) != 0) { |
| 539 | if (list[0] != '\0') |
| 540 | strlcat(list, DELIM, size); |
| 541 | strlcat(list, method->name, size); |
| 542 | } |
| 543 | } |
| 544 | return list; |
| 545 | } |
| 546 | |
| 547 | Authmethod * |
| 548 | authmethod_lookup(const char *name) |
| 549 | { |
| 550 | Authmethod *method = NULL; |
| 551 | if (name != NULL) |
| 552 | for (method = authmethods; method->name != NULL; method++) |
| 553 | if (method->enabled != NULL && |
| 554 | *(method->enabled) != 0 && |
| 555 | strcmp(name, method->name) == 0) |
| 556 | return method; |
| 557 | debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); |
| 558 | return NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* return 1 if user allows given key */ |
| 562 | int |
| 563 | user_dsa_key_allowed(struct passwd *pw, Key *key) |
| 564 | { |
| 565 | char line[8192], file[1024]; |
| 566 | int found_key = 0; |
| 567 | unsigned int bits = -1; |
| 568 | FILE *f; |
| 569 | unsigned long linenum = 0; |
| 570 | struct stat st; |
| 571 | Key *found; |
| 572 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 573 | if (pw == NULL) |
| 574 | return 0; |
| 575 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 576 | /* Temporarily use the user's uid. */ |
| 577 | temporarily_use_uid(pw->pw_uid); |
| 578 | |
| 579 | /* The authorized keys. */ |
| 580 | snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, |
| 581 | SSH_USER_PERMITTED_KEYS2); |
| 582 | |
| 583 | /* Fail quietly if file does not exist */ |
| 584 | if (stat(file, &st) < 0) { |
| 585 | /* Restore the privileged uid. */ |
| 586 | restore_uid(); |
| 587 | return 0; |
| 588 | } |
| 589 | /* Open the file containing the authorized keys. */ |
| 590 | f = fopen(file, "r"); |
| 591 | if (!f) { |
| 592 | /* Restore the privileged uid. */ |
| 593 | restore_uid(); |
| 594 | return 0; |
| 595 | } |
| 596 | if (options.strict_modes) { |
| 597 | int fail = 0; |
| 598 | char buf[1024]; |
| 599 | /* Check open file in order to avoid open/stat races */ |
| 600 | if (fstat(fileno(f), &st) < 0 || |
| 601 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 602 | (st.st_mode & 022) != 0) { |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 603 | snprintf(buf, sizeof buf, |
| 604 | "%s authentication refused for %.100s: " |
| 605 | "bad ownership or modes for '%s'.", |
| 606 | key_type(key), pw->pw_name, file); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 607 | fail = 1; |
| 608 | } else { |
| 609 | /* Check path to SSH_USER_PERMITTED_KEYS */ |
| 610 | int i; |
| 611 | static const char *check[] = { |
| 612 | "", SSH_USER_DIR, NULL |
| 613 | }; |
| 614 | for (i = 0; check[i]; i++) { |
| 615 | snprintf(line, sizeof line, "%.500s/%.100s", |
| 616 | pw->pw_dir, check[i]); |
| 617 | if (stat(line, &st) < 0 || |
| 618 | (st.st_uid != 0 && st.st_uid != pw->pw_uid) || |
| 619 | (st.st_mode & 022) != 0) { |
| 620 | snprintf(buf, sizeof buf, |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 621 | "%s authentication refused for %.100s: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 622 | "bad ownership or modes for '%s'.", |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 623 | key_type(key), pw->pw_name, line); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 624 | fail = 1; |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | if (fail) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 630 | fclose(f); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 631 | log("%s",buf); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 632 | restore_uid(); |
| 633 | return 0; |
| 634 | } |
| 635 | } |
| 636 | found_key = 0; |
Damien Miller | d344494 | 2000-09-30 14:20:03 +1100 | [diff] [blame] | 637 | found = key_new(key->type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 638 | |
| 639 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 640 | char *cp, *options = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 641 | linenum++; |
| 642 | /* Skip leading whitespace, empty and comment lines. */ |
| 643 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 644 | ; |
| 645 | if (!*cp || *cp == '\n' || *cp == '#') |
| 646 | continue; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 647 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 648 | bits = key_read(found, &cp); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 649 | if (bits == 0) { |
| 650 | /* no key? check if there are options for this key */ |
| 651 | int quoted = 0; |
| 652 | options = cp; |
| 653 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 654 | if (*cp == '\\' && cp[1] == '"') |
| 655 | cp++; /* Skip both */ |
| 656 | else if (*cp == '"') |
| 657 | quoted = !quoted; |
| 658 | } |
| 659 | /* Skip remaining whitespace. */ |
| 660 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 661 | ; |
| 662 | bits = key_read(found, &cp); |
| 663 | if (bits == 0) { |
| 664 | /* still no key? advance to next line*/ |
| 665 | continue; |
| 666 | } |
| 667 | } |
| 668 | if (key_equal(found, key) && |
| 669 | auth_parse_options(pw, options, linenum) == 1) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 670 | found_key = 1; |
| 671 | debug("matching key found: file %s, line %ld", |
| 672 | file, linenum); |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | restore_uid(); |
| 677 | fclose(f); |
| 678 | key_free(found); |
| 679 | return found_key; |
| 680 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 681 | |
| 682 | struct passwd * |
| 683 | pwcopy(struct passwd *pw) |
| 684 | { |
| 685 | struct passwd *copy = xmalloc(sizeof(*copy)); |
| 686 | memset(copy, 0, sizeof(*copy)); |
| 687 | copy->pw_name = xstrdup(pw->pw_name); |
| 688 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
| 689 | copy->pw_uid = pw->pw_uid; |
| 690 | copy->pw_gid = pw->pw_gid; |
| 691 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
| 692 | copy->pw_class = xstrdup(pw->pw_class); |
| 693 | #endif |
| 694 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 695 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 696 | return copy; |
| 697 | } |