1 /* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt 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 27 #include <sys/types.h> 28 29 #include <pwd.h> 30 #include <string.h> 31 #include <stdarg.h> 32 33 #include "xmalloc.h" 34 #include "ssh2.h" 35 #include "packet.h" 36 #include "log.h" 37 #include "buffer.h" 38 #include "servconf.h" 39 #include "compat.h" 40 #include "key.h" 41 #include "hostfile.h" 42 #include "auth.h" 43 #include "dispatch.h" 44 #include "pathnames.h" 45 #ifdef GSSAPI 46 #include "ssh-gss.h" 47 #endif 48 #include "monitor_wrap.h" 49 50 /* import */ 51 extern ServerOptions options; 52 extern u_char *session_id2; 53 extern u_int session_id2_len; 54 55 /* methods */ 56 57 extern Authmethod method_none; 58 extern Authmethod method_pubkey; 59 extern Authmethod method_passwd; 60 extern Authmethod method_kbdint; 61 extern Authmethod method_hostbased; 62 #ifdef GSSAPI 63 extern Authmethod method_gssapi; 64 #endif 65 66 Authmethod *authmethods[] = { 67 &method_none, 68 &method_pubkey, 69 #ifdef GSSAPI 70 &method_gssapi, 71 #endif 72 &method_passwd, 73 &method_kbdint, 74 &method_hostbased, 75 NULL 76 }; 77 78 /* protocol */ 79 80 static void input_service_request(int, u_int32_t, void *); 81 static void input_userauth_request(int, u_int32_t, void *); 82 83 /* helper */ 84 static Authmethod *authmethod_lookup(const char *); 85 static char *authmethods_get(void); 86 int user_key_allowed(struct passwd *, Key *); 87 88 /* 89 * loop until authctxt->success == TRUE 90 */ 91 92 void 93 do_authentication2(Authctxt *authctxt) 94 { 95 /* challenge-response is implemented via keyboard interactive */ 96 if (options.challenge_response_authentication) 97 options.kbd_interactive_authentication = 1; 98 99 dispatch_init(&dispatch_protocol_error); 100 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request); 101 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt); 102 } 103 104 /*ARGSUSED*/ 105 static void 106 input_service_request(int type, u_int32_t seq, void *ctxt) 107 { 108 Authctxt *authctxt = ctxt; 109 u_int len; 110 int acceptit = 0; 111 char *service = packet_get_string(&len); 112 packet_check_eom(); 113 114 if (authctxt == NULL) 115 fatal("input_service_request: no authctxt"); 116 117 if (strcmp(service, "ssh-userauth") == 0) { 118 if (!authctxt->success) { 119 acceptit = 1; 120 /* now we can handle user-auth requests */ 121 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request); 122 } 123 } 124 /* XXX all other service requests are denied */ 125 126 if (acceptit) { 127 packet_start(SSH2_MSG_SERVICE_ACCEPT); 128 packet_put_cstring(service); 129 packet_send(); 130 packet_write_wait(); 131 } else { 132 debug("bad service request %s", service); 133 packet_disconnect("bad service request %s", service); 134 } 135 xfree(service); 136 } 137 138 /*ARGSUSED*/ 139 static void 140 input_userauth_request(int type, u_int32_t seq, void *ctxt) 141 { 142 Authctxt *authctxt = ctxt; 143 Authmethod *m = NULL; 144 char *user, *service, *method, *style = NULL; 145 int authenticated = 0; 146 147 if (authctxt == NULL) 148 fatal("input_userauth_request: no authctxt"); 149 150 user = packet_get_string(NULL); 151 service = packet_get_string(NULL); 152 method = packet_get_string(NULL); 153 debug("userauth-request for user %s service %s method %s", user, service, method); 154 debug("attempt %d failures %d", authctxt->attempt, authctxt->failures); 155 156 if ((style = strchr(user, ':')) != NULL) 157 *style++ = 0; 158 159 if (authctxt->attempt++ == 0) { 160 /* setup auth context */ 161 authctxt->pw = PRIVSEP(getpwnamallow(user)); 162 if (authctxt->pw && strcmp(service, "ssh-connection")==0) { 163 authctxt->valid = 1; 164 debug2("input_userauth_request: setting up authctxt for %s", user); 165 } else { 166 logit("input_userauth_request: invalid user %s", user); 167 authctxt->pw = fakepw(); 168 } 169 setproctitle("%s%s", authctxt->valid ? user : "unknown", 170 use_privsep ? " [net]" : ""); 171 authctxt->user = xstrdup(user); 172 authctxt->service = xstrdup(service); 173 authctxt->style = style ? xstrdup(style) : NULL; 174 if (use_privsep) 175 mm_inform_authserv(service, style); 176 } else if (strcmp(user, authctxt->user) != 0 || 177 strcmp(service, authctxt->service) != 0) { 178 packet_disconnect("Change of username or service not allowed: " 179 "(%s,%s) -> (%s,%s)", 180 authctxt->user, authctxt->service, user, service); 181 } 182 /* reset state */ 183 auth2_challenge_stop(authctxt); 184 185 #ifdef GSSAPI 186 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL); 187 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL); 188 #endif 189 190 authctxt->postponed = 0; 191 192 /* try to authenticate user */ 193 m = authmethod_lookup(method); 194 if (m != NULL) { 195 debug2("input_userauth_request: try method %s", method); 196 authenticated = m->userauth(authctxt); 197 } 198 userauth_finish(authctxt, authenticated, method); 199 200 xfree(service); 201 xfree(user); 202 xfree(method); 203 } 204 205 void 206 userauth_finish(Authctxt *authctxt, int authenticated, char *method) 207 { 208 char *methods; 209 210 if (!authctxt->valid && authenticated) 211 fatal("INTERNAL ERROR: authenticated invalid user %s", 212 authctxt->user); 213 214 /* Special handling for root */ 215 if (authenticated && authctxt->pw->pw_uid == 0 && 216 !auth_root_allowed(method)) 217 authenticated = 0; 218 219 /* Log before sending the reply */ 220 auth_log(authctxt, authenticated, method, " ssh2"); 221 222 if (authctxt->postponed) 223 return; 224 225 /* XXX todo: check if multiple auth methods are needed */ 226 if (authenticated == 1) { 227 /* turn off userauth */ 228 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore); 229 packet_start(SSH2_MSG_USERAUTH_SUCCESS); 230 packet_send(); 231 packet_write_wait(); 232 /* now we can break out */ 233 authctxt->success = 1; 234 } else { 235 if (authctxt->failures++ > options.max_authtries) 236 packet_disconnect(AUTH_FAIL_MSG, authctxt->user); 237 methods = authmethods_get(); 238 packet_start(SSH2_MSG_USERAUTH_FAILURE); 239 packet_put_cstring(methods); 240 packet_put_char(0); /* XXX partial success, unused */ 241 packet_send(); 242 packet_write_wait(); 243 xfree(methods); 244 } 245 } 246 247 #define DELIM "," 248 249 static char * 250 authmethods_get(void) 251 { 252 Buffer b; 253 char *list; 254 int i; 255 256 buffer_init(&b); 257 for (i = 0; authmethods[i] != NULL; i++) { 258 if (strcmp(authmethods[i]->name, "none") == 0) 259 continue; 260 if (authmethods[i]->enabled != NULL && 261 *(authmethods[i]->enabled) != 0) { 262 if (buffer_len(&b) > 0) 263 buffer_append(&b, ",", 1); 264 buffer_append(&b, authmethods[i]->name, 265 strlen(authmethods[i]->name)); 266 } 267 } 268 buffer_append(&b, "\0", 1); 269 list = xstrdup(buffer_ptr(&b)); 270 buffer_free(&b); 271 return list; 272 } 273 274 static Authmethod * 275 authmethod_lookup(const char *name) 276 { 277 int i; 278 279 if (name != NULL) 280 for (i = 0; authmethods[i] != NULL; i++) 281 if (authmethods[i]->enabled != NULL && 282 *(authmethods[i]->enabled) != 0 && 283 strcmp(name, authmethods[i]->name) == 0) 284 return authmethods[i]; 285 debug2("Unrecognized authentication method name: %s", 286 name ? name : "NULL"); 287 return NULL; 288 } 289