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