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