1 /* $OpenBSD: auth2.c,v 1.124 2011/12/07 05:44:38 djm Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "includes.h" 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <sys/uio.h> 31 32 #include <fcntl.h> 33 #include <pwd.h> 34 #include <stdarg.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include "atomicio.h" 39 #include "xmalloc.h" 40 #include "ssh2.h" 41 #include "packet.h" 42 #include "log.h" 43 #include "buffer.h" 44 #include "servconf.h" 45 #include "compat.h" 46 #include "key.h" 47 #include "hostfile.h" 48 #include "auth.h" 49 #include "dispatch.h" 50 #include "pathnames.h" 51 #include "buffer.h" 52 #include "canohost.h" 53 54 #ifdef GSSAPI 55 #include "ssh-gss.h" 56 #endif 57 #include "monitor_wrap.h" 58 59 /* import */ 60 extern ServerOptions options; 61 extern u_char *session_id2; 62 extern u_int session_id2_len; 63 extern Buffer loginmsg; 64 65 /* methods */ 66 67 extern Authmethod method_none; 68 extern Authmethod method_pubkey; 69 extern Authmethod method_passwd; 70 extern Authmethod method_kbdint; 71 extern Authmethod method_hostbased; 72 #ifdef GSSAPI 73 extern Authmethod method_gssapi; 74 #endif 75 #ifdef JPAKE 76 extern Authmethod method_jpake; 77 #endif 78 79 static int log_flag = 0; 80 81 82 Authmethod *authmethods[] = { 83 &method_none, 84 &method_pubkey, 85 #ifdef GSSAPI 86 &method_gssapi, 87 #endif 88 #ifdef JPAKE 89 &method_jpake, 90 #endif 91 &method_passwd, 92 &method_kbdint, 93 &method_hostbased, 94 NULL 95 }; 96 97 /* protocol */ 98 99 static void input_service_request(int, u_int32_t, void *); 100 static void input_userauth_request(int, u_int32_t, void *); 101 102 /* helper */ 103 static Authmethod *authmethod_lookup(const char *); 104 static char *authmethods_get(void); 105 106 char * 107 auth2_read_banner(void) 108 { 109 struct stat st; 110 char *banner = NULL; 111 size_t len, n; 112 int fd; 113 114 if ((fd = open(options.banner, O_RDONLY)) == -1) 115 return (NULL); 116 if (fstat(fd, &st) == -1) { 117 close(fd); 118 return (NULL); 119 } 120 if (st.st_size <= 0 || st.st_size > 1*1024*1024) { 121 close(fd); 122 return (NULL); 123 } 124 125 len = (size_t)st.st_size; /* truncate */ 126 banner = xmalloc(len + 1); 127 n = atomicio(read, fd, banner, len); 128 close(fd); 129 130 if (n != len) { 131 xfree(banner); 132 return (NULL); 133 } 134 banner[n] = '\0'; 135 136 return (banner); 137 } 138 139 void 140 userauth_send_banner(const char *msg) 141 { 142 if (datafellows & SSH_BUG_BANNER) 143 return; 144 145 packet_start(SSH2_MSG_USERAUTH_BANNER); 146 packet_put_cstring(msg); 147 packet_put_cstring(""); /* language, unused */ 148 packet_send(); 149 debug("%s: sent", __func__); 150 } 151 152 static void 153 userauth_banner(void) 154 { 155 char *banner = NULL; 156 157 if (options.banner == NULL || 158 strcasecmp(options.banner, "none") == 0 || 159 (datafellows & SSH_BUG_BANNER) != 0) 160 return; 161 162 if ((banner = PRIVSEP(auth2_read_banner())) == NULL) 163 goto done; 164 userauth_send_banner(banner); 165 166 done: 167 if (banner) 168 xfree(banner); 169 } 170 171 /* 172 * loop until authctxt->success == TRUE 173 */ 174 void 175 do_authentication2(Authctxt *authctxt) 176 { 177 dispatch_init(&dispatch_protocol_error); 178 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); 179 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); 180 } 181 182 /*ARGSUSED*/ 183 static void 184 input_service_request(int type, u_int32_t seq, void *ctxt) 185 { 186 Authctxt *authctxt = ctxt; 187 u_int len; 188 int acceptit = 0; 189 char *service = packet_get_cstring(&len); 190 packet_check_eom(); 191 192 if (authctxt == NULL) 193 fatal("input_service_request: no authctxt"); 194 195 if (strcmp(service, "ssh-userauth") == 0) { 196 if (!authctxt->success) { 197 acceptit = 1; 198 /* now we can handle user-auth requests */ 199 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); 200 } 201 } 202 /* XXX all other service requests are denied */ 203 204 if (acceptit) { 205 packet_start(SSH2_MSG_SERVICE_ACCEPT); 206 packet_put_cstring(service); 207 packet_send(); 208 packet_write_wait(); 209 } else { 210 debug("bad service request %s", service); 211 packet_disconnect("bad service request %s", service); 212 } 213 xfree(service); 214 } 215 216 /*ARGSUSED*/ 217 static void 218 input_userauth_request(int type, u_int32_t seq, void *ctxt) 219 { 220 Authctxt *authctxt = ctxt; 221 Authmethod *m = NULL; 222 char *user, *service, *method, *style = NULL; 223 int authenticated = 0; 224 #ifdef HAVE_LOGIN_CAP 225 login_cap_t *lc; 226 const char *from_host, *from_ip; 227 228 from_host = get_canonical_hostname(options.use_dns); 229 from_ip = get_remote_ipaddr(); 230 #endif 231 232 if (authctxt == NULL) 233 fatal("input_userauth_request: no authctxt"); 234 235 user = packet_get_cstring(NULL); 236 service = packet_get_cstring(NULL); 237 method = packet_get_cstring(NULL); 238 debug("userauth-request for user %s service %s method %s", user, service, method); 239 if (!log_flag) { 240 logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s", 241 get_remote_ipaddr(), get_remote_port(), user); 242 log_flag = 1; 243 } 244 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 245 246 if ((style = strchr(user, ':')) != NULL) 247 *style++ = 0; 248 249 if (authctxt->attempt++ == 0) { 250 /* setup auth context */ 251 authctxt->pw = PRIVSEP(getpwnamallow(user)); 252 authctxt->user = xstrdup(user); 253 if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 254 authctxt->valid = 1; 255 debug2("input_userauth_request: setting up authctxt for %s", user); 256 } else { 257 logit("input_userauth_request: invalid user %s", user); 258 authctxt->pw = fakepw(); 259 #ifdef SSH_AUDIT_EVENTS 260 PRIVSEP(audit_event(SSH_INVALID_USER)); 261 #endif 262 } 263 #ifdef USE_PAM 264 if (options.use_pam) 265 PRIVSEP(start_pam(authctxt)); 266 #endif 267 setproctitle("%s%s", authctxt->valid ? user : "unknown", 268 use_privsep ? " [net]" : ""); 269 authctxt->service = xstrdup(service); 270 authctxt->style = style ? xstrdup(style) : NULL; 271 if (use_privsep) 272 mm_inform_authserv(service, style); 273 userauth_banner(); 274 } else if (strcmp(user, authctxt->user) != 0 || 275 strcmp(service, authctxt->service) != 0) { 276 packet_disconnect("Change of username or service not allowed: " 277 "(%s,%s) -> (%s,%s)", 278 authctxt->user, authctxt->service, user, service); 279 } 280 281 #ifdef HAVE_LOGIN_CAP 282 if (authctxt->pw != NULL) { 283 lc = login_getpwclass(authctxt->pw); 284 if (lc == NULL) 285 lc = login_getclassbyname(NULL, authctxt->pw); 286 if (!auth_hostok(lc, from_host, from_ip)) { 287 logit("Denied connection for %.200s from %.200s [%.200s].", 288 authctxt->pw->pw_name, from_host, from_ip); 289 packet_disconnect("Sorry, you are not allowed to connect."); 290 } 291 if (!auth_timeok(lc, time(NULL))) { 292 logit("LOGIN %.200s REFUSED (TIME) FROM %.200s", 293 authctxt->pw->pw_name, from_host); 294 packet_disconnect("Logins not available right now."); 295 } 296 login_close(lc); 297 lc = NULL; 298 } 299 #endif /* HAVE_LOGIN_CAP */ 300 301 /* reset state */ 302 auth2_challenge_stop(authctxt); 303 #ifdef JPAKE 304 auth2_jpake_stop(authctxt); 305 #endif 306 307 #ifdef GSSAPI 308 /* XXX move to auth2_gssapi_stop() */ 309 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 310 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 311 #endif 312 313 authctxt->postponed = 0; 314 authctxt->server_caused_failure = 0; 315 316 /* try to authenticate user */ 317 m = authmethod_lookup(method); 318 if (m != NULL && authctxt->failures < options.max_authtries) { 319 debug2("input_userauth_request: try method %s", method); 320 authenticated = m->userauth(authctxt); 321 } 322 userauth_finish(authctxt, authenticated, method); 323 324 xfree(service); 325 xfree(user); 326 xfree(method); 327 } 328 329 void 330 userauth_finish(Authctxt *authctxt, int authenticated, char *method) 331 { 332 char *methods; 333 334 if (!authctxt->valid && authenticated) 335 fatal("INTERNAL ERROR: authenticated invalid user %s", 336 authctxt->user); 337 338 /* Special handling for root */ 339 if (authenticated && authctxt->pw->pw_uid == 0 && 340 !auth_root_allowed(method)) { 341 authenticated = 0; 342 #ifdef SSH_AUDIT_EVENTS 343 PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); 344 #endif 345 } 346 347 #ifdef USE_PAM 348 if (options.use_pam && authenticated) { 349 if (!PRIVSEP(do_pam_account())) { 350 /* if PAM returned a message, send it to the user */ 351 if (buffer_len(&loginmsg) > 0) { 352 buffer_append(&loginmsg, "\0", 1); 353 userauth_send_banner(buffer_ptr(&loginmsg)); 354 packet_write_wait(); 355 } 356 fatal("Access denied for user %s by PAM account " 357 "configuration", authctxt->user); 358 } 359 } 360 #endif 361 362 #ifdef _UNICOS 363 if (authenticated && cray_access_denied(authctxt->user)) { 364 authenticated = 0; 365 fatal("Access denied for user %s.",authctxt->user); 366 } 367 #endif /* _UNICOS */ 368 369 /* Log before sending the reply */ 370 auth_log(authctxt, authenticated, method, " ssh2"); 371 372 if (authctxt->postponed) 373 return; 374 375 /* XXX todo: check if multiple auth methods are needed */ 376 if (authenticated == 1) { 377 /* turn off userauth */ 378 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); 379 packet_start(SSH2_MSG_USERAUTH_SUCCESS); 380 packet_send(); 381 packet_write_wait(); 382 /* now we can break out */ 383 authctxt->success = 1; 384 } else { 385 386 /* Allow initial try of "none" auth without failure penalty */ 387 if (!authctxt->server_caused_failure && 388 (authctxt->attempt > 1 || strcmp(method, "none") != 0)) 389 authctxt->failures++; 390 if (authctxt->failures >= options.max_authtries) { 391 #ifdef SSH_AUDIT_EVENTS 392 PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); 393 #endif 394 packet_disconnect(AUTH_FAIL_MSG, authctxt->user); 395 } 396 methods = authmethods_get(); 397 packet_start(SSH2_MSG_USERAUTH_FAILURE); 398 packet_put_cstring(methods); 399 packet_put_char(0); /* XXX partial success, unused */ 400 packet_send(); 401 packet_write_wait(); 402 xfree(methods); 403 } 404 } 405 406 static char * 407 authmethods_get(void) 408 { 409 Buffer b; 410 char *list; 411 int i; 412 413 buffer_init(&b); 414 for (i = 0; authmethods[i] != NULL; i++) { 415 if (strcmp(authmethods[i]->name, "none") == 0) 416 continue; 417 if (authmethods[i]->enabled != NULL && 418 *(authmethods[i]->enabled) != 0) { 419 if (buffer_len(&b) > 0) 420 buffer_append(&b, ",", 1); 421 buffer_append(&b, authmethods[i]->name, 422 strlen(authmethods[i]->name)); 423 } 424 } 425 buffer_append(&b, "\0", 1); 426 list = xstrdup(buffer_ptr(&b)); 427 buffer_free(&b); 428 return list; 429 } 430 431 static Authmethod * 432 authmethod_lookup(const char *name) 433 { 434 int i; 435 436 if (name != NULL) 437 for (i = 0; authmethods[i] != NULL; i++) 438 if (authmethods[i]->enabled != NULL && 439 *(authmethods[i]->enabled) != 0 && 440 strcmp(name, authmethods[i]->name) == 0) 441 return authmethods[i]; 442 debug2("Unrecognized authentication method name: %s", 443 name ? name : "NULL"); 444 return NULL; 445 } 446 447