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