auth2.c revision 99053
1327Sjkh/* 2379Sjkh * Copyright (c) 2000 Markus Friedl. All rights reserved. 3327Sjkh * 4327Sjkh * Redistribution and use in source and binary forms, with or without 5327Sjkh * modification, are permitted provided that the following conditions 6327Sjkh * are met: 7327Sjkh * 1. Redistributions of source code must retain the above copyright 8327Sjkh * notice, this list of conditions and the following disclaimer. 9327Sjkh * 2. Redistributions in binary form must reproduce the above copyright 10327Sjkh * notice, this list of conditions and the following disclaimer in the 11327Sjkh * documentation and/or other materials provided with the distribution. 12327Sjkh * 13327Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14327Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15327Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16327Sjkh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17327Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18327Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19327Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20327Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21327Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22327Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23327Sjkh */ 24327Sjkh 25327Sjkh#include "includes.h" 26327SjkhRCSID("$OpenBSD: auth2.c,v 1.93 2002/05/31 11:35:15 markus Exp $"); 27327SjkhRCSID("$FreeBSD: head/crypto/openssh/auth2.c 99053 2002-06-29 10:57:13Z des $"); 28327Sjkh 29411Sjkh#include "ssh2.h" 30327Sjkh#include "xmalloc.h" 31327Sjkh#include "packet.h" 32327Sjkh#include "log.h" 33411Sjkh#include "servconf.h" 34379Sjkh#include "compat.h" 35383Sjkh#include "auth.h" 36392Sjkh#include "dispatch.h" 37327Sjkh#include "pathnames.h" 38327Sjkh#include "monitor_wrap.h" 39327Sjkh 40327Sjkh/* import */ 41327Sjkhextern ServerOptions options; 42327Sjkhextern u_char *session_id2; 43327Sjkhextern int session_id2_len; 44327Sjkh 45327SjkhAuthctxt *x_authctxt = NULL; 46327Sjkh 47327Sjkh/* methods */ 48327Sjkh 49327Sjkhextern Authmethod method_none; 50327Sjkhextern Authmethod method_pubkey; 51327Sjkhextern Authmethod method_passwd; 52327Sjkhextern Authmethod method_kbdint; 53327Sjkhextern Authmethod method_hostbased; 54327Sjkh 55327SjkhAuthmethod *authmethods[] = { 56327Sjkh &method_none, 57327Sjkh &method_pubkey, 58327Sjkh &method_passwd, 59327Sjkh &method_kbdint, 60327Sjkh &method_hostbased, 61327Sjkh NULL 62327Sjkh}; 63327Sjkh 64327Sjkh/* protocol */ 65327Sjkh 66327Sjkhstatic void input_service_request(int, u_int32_t, void *); 67327Sjkhstatic void input_userauth_request(int, u_int32_t, void *); 68327Sjkh 69327Sjkh/* helper */ 70327Sjkhstatic Authmethod *authmethod_lookup(const char *); 71327Sjkhstatic char *authmethods_get(void); 72327Sjkhint user_key_allowed(struct passwd *, Key *); 73327Sjkhint hostbased_key_allowed(struct passwd *, const char *, char *, Key *); 74327Sjkh 75327Sjkh/* 76327Sjkh * loop until authctxt->success == TRUE 77327Sjkh */ 78327Sjkh 79327SjkhAuthctxt * 80327Sjkhdo_authentication2(void) 81327Sjkh{ 82327Sjkh Authctxt *authctxt = authctxt_new(); 83327Sjkh 84327Sjkh x_authctxt = authctxt; /*XXX*/ 85327Sjkh 86327Sjkh /* challenge-response is implemented via keyboard interactive */ 87327Sjkh if (options.challenge_response_authentication) 88327Sjkh options.kbd_interactive_authentication = 1; 89327Sjkh if (options.pam_authentication_via_kbd_int) 90327Sjkh options.kbd_interactive_authentication = 1; 91411Sjkh if (use_privsep) 92411Sjkh options.pam_authentication_via_kbd_int = 0; 93411Sjkh 94411Sjkh dispatch_init(&dispatch_protocol_error); 95379Sjkh dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); 96379Sjkh dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); 97379Sjkh 98379Sjkh return (authctxt); 99411Sjkh} 100411Sjkh 101411Sjkhstatic void 102411Sjkhinput_service_request(int type, u_int32_t seq, void *ctxt) 103383Sjkh{ 104383Sjkh Authctxt *authctxt = ctxt; 105383Sjkh u_int len; 106383Sjkh int accept = 0; 107392Sjkh char *service = packet_get_string(&len); 108392Sjkh packet_check_eom(); 109392Sjkh 110392Sjkh if (authctxt == NULL) 111327Sjkh fatal("input_service_request: no authctxt"); 112327Sjkh 113327Sjkh if (strcmp(service, "ssh-userauth") == 0) { 114327Sjkh if (!authctxt->success) { 115327Sjkh accept = 1; 116327Sjkh /* now we can handle user-auth requests */ 117327Sjkh dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); 118327Sjkh } 119327Sjkh } 120327Sjkh /* XXX all other service requests are denied */ 121327Sjkh 122327Sjkh if (accept) { 123327Sjkh packet_start(SSH2_MSG_SERVICE_ACCEPT); 124327Sjkh packet_put_cstring(service); 125327Sjkh packet_send(); 126327Sjkh packet_write_wait(); 127327Sjkh } else { 128327Sjkh debug("bad service request %s", service); 129327Sjkh packet_disconnect("bad service request %s", service); 130392Sjkh } 131327Sjkh xfree(service); 132327Sjkh} 133327Sjkh 134327Sjkhstatic void 135327Sjkhinput_userauth_request(int type, u_int32_t seq, void *ctxt) 136327Sjkh{ 137327Sjkh Authctxt *authctxt = ctxt; 138327Sjkh Authmethod *m = NULL; 139327Sjkh char *user, *service, *method, *style = NULL; 140327Sjkh int authenticated = 0; 141327Sjkh#ifdef HAVE_LOGIN_CAP 142327Sjkh login_cap_t *lc; 143327Sjkh const char *from_host, *from_ip; 144327Sjkh 145327Sjkh from_host = get_canonical_hostname(options.verify_reverse_mapping); 146327Sjkh from_ip = get_remote_ipaddr(); 147327Sjkh#endif 148327Sjkh 149327Sjkh if (authctxt == NULL) 150327Sjkh fatal("input_userauth_request: no authctxt"); 151327Sjkh 152327Sjkh user = packet_get_string(NULL); 153327Sjkh service = packet_get_string(NULL); 154327Sjkh method = packet_get_string(NULL); 155327Sjkh debug("userauth-request for user %s service %s method %s", user, service, method); 156327Sjkh debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 157327Sjkh 158327Sjkh if ((style = strchr(user, ':')) != NULL) 159379Sjkh *style++ = 0; 160327Sjkh 161383Sjkh if (authctxt->attempt++ == 0) { 162327Sjkh /* setup auth context */ 163327Sjkh authctxt->pw = PRIVSEP(getpwnamallow(user)); 164327Sjkh if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 165 authctxt->valid = 1; 166 debug2("input_userauth_request: setting up authctxt for %s", user); 167#ifdef USE_PAM 168 PRIVSEP(start_pam(authctxt->pw->pw_name)); 169#endif 170 } else { 171 log("input_userauth_request: illegal user %s", user); 172#ifdef USE_PAM 173 PRIVSEP(start_pam("NOUSER")); 174#endif 175 } 176 setproctitle("%s%s", authctxt->pw ? user : "unknown", 177 use_privsep ? " [net]" : ""); 178 authctxt->user = xstrdup(user); 179 authctxt->service = xstrdup(service); 180 authctxt->style = style ? xstrdup(style) : NULL; 181 if (use_privsep) 182 mm_inform_authserv(service, style); 183 } else if (strcmp(user, authctxt->user) != 0 || 184 strcmp(service, authctxt->service) != 0) { 185 packet_disconnect("Change of username or service not allowed: " 186 "(%s,%s) -> (%s,%s)", 187 authctxt->user, authctxt->service, user, service); 188 } 189 190#ifdef HAVE_LOGIN_CAP 191 if (authctxt->pw != NULL) { 192 lc = login_getpwclass(authctxt->pw); 193 if (lc == NULL) 194 lc = login_getclassbyname(NULL, authctxt->pw); 195 if (!auth_hostok(lc, from_host, from_ip)) { 196 log("Denied connection for %.200s from %.200s [%.200s].", 197 authctxt->pw->pw_name, from_host, from_ip); 198 packet_disconnect("Sorry, you are not allowed to connect."); 199 } 200 if (!auth_timeok(lc, time(NULL))) { 201 log("LOGIN %.200s REFUSED (TIME) FROM %.200s", 202 authctxt->pw->pw_name, from_host); 203 packet_disconnect("Logins not available right now."); 204 } 205 login_close(lc); 206 lc = NULL; 207 } 208#endif /* HAVE_LOGIN_CAP */ 209 210 /* reset state */ 211 auth2_challenge_stop(authctxt); 212 authctxt->postponed = 0; 213 214 /* try to authenticate user */ 215 m = authmethod_lookup(method); 216 if (m != NULL) { 217 debug2("input_userauth_request: try method %s", method); 218 authenticated = m->userauth(authctxt); 219 } 220 userauth_finish(authctxt, authenticated, method); 221 222 xfree(service); 223 xfree(user); 224 xfree(method); 225} 226 227void 228userauth_finish(Authctxt *authctxt, int authenticated, char *method) 229{ 230 char *methods; 231 232 if (!authctxt->valid && authenticated) 233 fatal("INTERNAL ERROR: authenticated invalid user %s", 234 authctxt->user); 235 236 /* Special handling for root */ 237 if (authenticated && authctxt->pw->pw_uid == 0 && 238 !auth_root_allowed(method)) 239 authenticated = 0; 240 241#ifdef USE_PAM 242 if (!use_privsep && authenticated && authctxt->user && 243 !do_pam_account(authctxt->user, NULL)) 244 authenticated = 0; 245#endif /* USE_PAM */ 246 247 /* Log before sending the reply */ 248 auth_log(authctxt, authenticated, method, " ssh2"); 249 250 if (authctxt->postponed) 251 return; 252 253 /* XXX todo: check if multiple auth methods are needed */ 254 if (authenticated == 1) { 255 /* turn off userauth */ 256 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); 257 packet_start(SSH2_MSG_USERAUTH_SUCCESS); 258 packet_send(); 259 packet_write_wait(); 260 /* now we can break out */ 261 authctxt->success = 1; 262 } else { 263 if (authctxt->failures++ > AUTH_FAIL_MAX) { 264#ifdef WITH_AIXAUTHENTICATE 265 /* XXX: privsep */ 266 loginfailed(authctxt->user, 267 get_canonical_hostname(options.verify_reverse_mapping), 268 "ssh"); 269#endif /* WITH_AIXAUTHENTICATE */ 270 packet_disconnect(AUTH_FAIL_MSG, authctxt->user); 271 } 272 methods = authmethods_get(); 273 packet_start(SSH2_MSG_USERAUTH_FAILURE); 274 packet_put_cstring(methods); 275 packet_put_char(0); /* XXX partial success, unused */ 276 packet_send(); 277 packet_write_wait(); 278 xfree(methods); 279 } 280} 281 282/* get current user */ 283 284struct passwd* 285auth_get_user(void) 286{ 287 return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL; 288} 289 290#define DELIM "," 291 292static char * 293authmethods_get(void) 294{ 295 Buffer b; 296 char *list; 297 int i; 298 299 buffer_init(&b); 300 for (i = 0; authmethods[i] != NULL; i++) { 301 if (strcmp(authmethods[i]->name, "none") == 0) 302 continue; 303 if (authmethods[i]->enabled != NULL && 304 *(authmethods[i]->enabled) != 0) { 305 if (buffer_len(&b) > 0) 306 buffer_append(&b, ",", 1); 307 buffer_append(&b, authmethods[i]->name, 308 strlen(authmethods[i]->name)); 309 } 310 } 311 buffer_append(&b, "\0", 1); 312 list = xstrdup(buffer_ptr(&b)); 313 buffer_free(&b); 314 return list; 315} 316 317static Authmethod * 318authmethod_lookup(const char *name) 319{ 320 int i; 321 322 if (name != NULL) 323 for (i = 0; authmethods[i] != NULL; i++) 324 if (authmethods[i]->enabled != NULL && 325 *(authmethods[i]->enabled) != 0 && 326 strcmp(name, authmethods[i]->name) == 0) 327 return authmethods[i]; 328 debug2("Unrecognized authentication method name: %s", 329 name ? name : "NULL"); 330 return NULL; 331} 332