]> andersk Git - openssh.git/blob - auth2.c
- Moved all the bsd-* and fake-* stuff into new libopenbsd-compat.a
[openssh.git] / auth2.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *      This product includes software developed by Markus Friedl.
15  * 4. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "includes.h"
30 RCSID("$OpenBSD: auth2.c,v 1.8 2000/05/08 17:42:24 markus Exp $");
31
32 #include <openssl/dsa.h>
33 #include <openssl/rsa.h>
34 #include <openssl/evp.h>
35
36 #include "xmalloc.h"
37 #include "rsa.h"
38 #include "ssh.h"
39 #include "pty.h"
40 #include "packet.h"
41 #include "buffer.h"
42 #include "cipher.h"
43 #include "servconf.h"
44 #include "compat.h"
45 #include "channels.h"
46 #include "bufaux.h"
47 #include "ssh2.h"
48 #include "auth.h"
49 #include "session.h"
50 #include "dispatch.h"
51 #include "auth.h"
52 #include "key.h"
53 #include "kex.h"
54
55 #include "dsa.h"
56 #include "uidswap.h"
57
58 /* import */
59 extern ServerOptions options;
60 extern unsigned char *session_id2;
61 extern int session_id2_len;
62
63 /* protocol */
64
65 void    input_service_request(int type, int plen);
66 void    input_userauth_request(int type, int plen);
67 void    protocol_error(int type, int plen);
68
69 /* auth */
70 int     ssh2_auth_none(struct passwd *pw);
71 int     ssh2_auth_password(struct passwd *pw);
72 int     ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen);
73
74 /* helper */
75 struct passwd*   auth_set_user(char *u, char *s);
76 int     user_dsa_key_allowed(struct passwd *pw, Key *key);
77
78 typedef struct Authctxt Authctxt;
79 struct Authctxt {
80         char *user;
81         char *service;
82         struct passwd pw;
83         int valid;
84 };
85 static Authctxt *authctxt = NULL;
86 static int userauth_success = 0;
87
88 /*
89  * loop until userauth_success == TRUE
90  */
91
92 void
93 do_authentication2()
94 {
95         /* turn off skey/kerberos, not supported by SSH2 */
96 #ifdef SKEY
97         options.skey_authentication = 0;
98 #endif
99 #ifdef KRB4
100         options.kerberos_authentication = 0;
101 #endif
102
103         dispatch_init(&protocol_error);
104         dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
105         dispatch_run(DISPATCH_BLOCK, &userauth_success);
106         do_authenticated2();
107 }
108
109 void
110 protocol_error(int type, int plen)
111 {
112         log("auth: protocol error: type %d plen %d", type, plen);
113         packet_start(SSH2_MSG_UNIMPLEMENTED);
114         packet_put_int(0);
115         packet_send();
116         packet_write_wait();
117 }
118
119 void
120 input_service_request(int type, int plen)
121 {
122         unsigned int len;
123         int accept = 0;
124         char *service = packet_get_string(&len);
125         packet_done();
126
127         if (strcmp(service, "ssh-userauth") == 0) {
128                 if (!userauth_success) {
129                         accept = 1;
130                         /* now we can handle user-auth requests */
131                         dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
132                 }
133         }
134         /* XXX all other service requests are denied */
135
136         if (accept) {
137                 packet_start(SSH2_MSG_SERVICE_ACCEPT);
138                 packet_put_cstring(service);
139                 packet_send();
140                 packet_write_wait();
141         } else {
142                 debug("bad service request %s", service);
143                 packet_disconnect("bad service request %s", service);
144         }
145         xfree(service);
146 }
147
148 void
149 input_userauth_request(int type, int plen)
150 {
151         static void (*authlog) (const char *fmt,...) = verbose;
152         static int attempt = 0;
153         unsigned int len, rlen;
154         int authenticated = 0;
155         char *raw, *user, *service, *method, *authmsg = NULL;
156         struct passwd *pw;
157
158         if (++attempt == AUTH_FAIL_MAX)
159                 packet_disconnect("too many failed userauth_requests");
160
161         raw = packet_get_raw(&rlen);
162         if (plen != rlen)
163                 fatal("plen != rlen");
164         user = packet_get_string(&len);
165         service = packet_get_string(&len);
166         method = packet_get_string(&len);
167         debug("userauth-request for user %s service %s method %s", user, service, method);
168
169         /* XXX we only allow the ssh-connection service */
170         pw = auth_set_user(user, service);
171         if (pw && strcmp(service, "ssh-connection")==0) {
172                 if (strcmp(method, "none") == 0) {
173                         authenticated = ssh2_auth_none(pw);
174                 } else if (strcmp(method, "password") == 0) {
175                         authenticated = ssh2_auth_password(pw);
176                 } else if (strcmp(method, "publickey") == 0) {
177                         authenticated = ssh2_auth_pubkey(pw, raw, rlen);
178                 }
179         }
180         if (authenticated && pw && pw->pw_uid == 0 && !options.permit_root_login) {
181                 authenticated = 0;
182                 log("ROOT LOGIN REFUSED FROM %.200s",
183                     get_canonical_hostname());
184         }
185
186 #ifdef USE_PAM
187                 if (authenticated && !do_pam_account(pw->pw_name, NULL))
188                         authenticated = 0;
189 #endif /* USE_PAM */
190
191         /* Raise logging level */
192         if (authenticated == 1 ||
193             attempt == AUTH_FAIL_LOG ||
194             strcmp(method, "password") == 0)
195                 authlog = log;
196
197         /* Log before sending the reply */
198         if (authenticated == 1) {
199                 authmsg = "Accepted";
200         } else if (authenticated == 0) {
201                 authmsg = "Failed";
202         } else {
203                 authmsg = "Postponed";
204         }
205         authlog("%s %s for %.200s from %.200s port %d ssh2",
206                 authmsg,
207                 method,
208                 pw && pw->pw_uid == 0 ? "ROOT" : user,
209                 get_remote_ipaddr(),
210                 get_remote_port());
211
212         /* XXX todo: check if multiple auth methods are needed */
213         if (authenticated == 1) {
214                 /* turn off userauth */
215                 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
216                 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
217                 packet_send();
218                 packet_write_wait();
219                 /* now we can break out */
220                 userauth_success = 1;
221         } else if (authenticated == 0) {
222                 packet_start(SSH2_MSG_USERAUTH_FAILURE);
223                 packet_put_cstring("publickey,password");       /* XXX dynamic */
224                 packet_put_char(0);                             /* XXX partial success, unused */
225                 packet_send();
226                 packet_write_wait();
227         }
228
229         xfree(service);
230         xfree(user);
231         xfree(method);
232 }
233
234 int
235 ssh2_auth_none(struct passwd *pw)
236 {
237         packet_done();
238 #ifdef USE_PAM
239         return auth_pam_password(pw, "");
240 #else /* USE_PAM */
241         return auth_password(pw, "");
242 #endif /* USE_PAM */
243 }
244 int
245 ssh2_auth_password(struct passwd *pw)
246 {
247         char *password;
248         int authenticated = 0;
249         int change;
250         unsigned int len;
251         change = packet_get_char();
252         if (change)
253                 log("password change not supported");
254         password = packet_get_string(&len);
255         packet_done();
256         if (options.password_authentication &&
257 #ifdef USE_PAM
258             auth_pam_password(pw, password) == 1)
259 #else /* USE_PAM */
260             auth_password(pw, password) == 1)
261 #endif /* USE_PAM */
262                 authenticated = 1;
263         memset(password, 0, len);
264         xfree(password);
265         return authenticated;
266 }
267 int
268 ssh2_auth_pubkey(struct passwd *pw, unsigned char *raw, unsigned int rlen)
269 {
270         Buffer b;
271         Key *key;
272         char *pkalg, *pkblob, *sig;
273         unsigned int alen, blen, slen;
274         int have_sig;
275         int authenticated = 0;
276
277         if (options.dsa_authentication == 0) {
278                 debug("pubkey auth disabled");
279                 return 0;
280         }
281         if (datafellows & SSH_BUG_PUBKEYAUTH) {
282                 log("bug compatibility with ssh-2.0.13 pubkey not implemented");
283                 return 0;
284         }
285         have_sig = packet_get_char();
286         pkalg = packet_get_string(&alen);
287         if (strcmp(pkalg, KEX_DSS) != 0) {
288                 xfree(pkalg);
289                 log("bad pkalg %s", pkalg);     /*XXX*/
290                 return 0;
291         }
292         pkblob = packet_get_string(&blen);
293         key = dsa_key_from_blob(pkblob, blen);
294         if (key != NULL) {
295                 if (have_sig) {
296                         sig = packet_get_string(&slen);
297                         packet_done();
298                         buffer_init(&b);
299                         buffer_append(&b, session_id2, session_id2_len);
300                         buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
301                         if (slen + 4 > rlen)
302                                 fatal("bad rlen/slen");
303                         buffer_append(&b, raw, rlen - slen - 4);
304 #ifdef DEBUG_DSS
305                         buffer_dump(&b);
306 #endif
307                         /* test for correct signature */
308                         if (user_dsa_key_allowed(pw, key) &&
309                             dsa_verify(key, sig, slen, buffer_ptr(&b), buffer_len(&b)) == 1)
310                                 authenticated = 1;
311                         buffer_clear(&b);
312                         xfree(sig);
313                 } else {
314                         packet_done();
315                         debug("test key...");
316                         /* test whether pkalg/pkblob are acceptable */
317                         /* XXX fake reply and always send PK_OK ? */
318                         /*
319                          * XXX this allows testing whether a user is allowed
320                          * to login: if you happen to have a valid pubkey this
321                          * message is sent. the message is NEVER sent at all
322                          * if a user is not allowed to login. is this an
323                          * issue? -markus
324                          */
325                         if (user_dsa_key_allowed(pw, key)) {
326                                 packet_start(SSH2_MSG_USERAUTH_PK_OK);
327                                 packet_put_string(pkalg, alen);
328                                 packet_put_string(pkblob, blen);
329                                 packet_send();
330                                 packet_write_wait();
331                                 authenticated = -1;
332                         }
333                 }
334                 key_free(key);
335         }
336         xfree(pkalg);
337         xfree(pkblob);
338         return authenticated;
339 }
340
341 /* set and get current user */
342
343 struct passwd*
344 auth_get_user(void)
345 {
346         return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
347 }
348
349 struct passwd*
350 auth_set_user(char *u, char *s)
351 {
352         struct passwd *pw, *copy;
353
354         if (authctxt == NULL) {
355                 authctxt = xmalloc(sizeof(*authctxt));
356                 authctxt->valid = 0;
357                 authctxt->user = xstrdup(u);
358                 authctxt->service = xstrdup(s);
359                 setproctitle("%s", u);
360                 pw = getpwnam(u);
361                 if (!pw || !allowed_user(pw)) {
362                         log("auth_set_user: illegal user %s", u);
363                         return NULL;
364                 }
365 #ifdef USE_PAM
366                 start_pam(pw);
367 #endif
368                 copy = &authctxt->pw;
369                 memset(copy, 0, sizeof(*copy));
370                 copy->pw_name = xstrdup(pw->pw_name);
371                 copy->pw_passwd = xstrdup(pw->pw_passwd);
372                 copy->pw_uid = pw->pw_uid;
373                 copy->pw_gid = pw->pw_gid;
374                 copy->pw_dir = xstrdup(pw->pw_dir);
375                 copy->pw_shell = xstrdup(pw->pw_shell);
376                 authctxt->valid = 1;
377         } else {
378                 if (strcmp(u, authctxt->user) != 0 ||
379                     strcmp(s, authctxt->service) != 0) {
380                         log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
381                             u, s, authctxt->user, authctxt->service);
382                         return NULL;
383                 }
384         }
385         return auth_get_user();
386 }
387
388 /* return 1 if user allows given key */
389 int
390 user_dsa_key_allowed(struct passwd *pw, Key *key)
391 {
392         char line[8192], file[1024];
393         int found_key = 0;
394         unsigned int bits = -1;
395         FILE *f;
396         unsigned long linenum = 0;
397         struct stat st;
398         Key *found;
399
400         /* Temporarily use the user's uid. */
401         temporarily_use_uid(pw->pw_uid);
402
403         /* The authorized keys. */
404         snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
405             SSH_USER_PERMITTED_KEYS2);
406
407         /* Fail quietly if file does not exist */
408         if (stat(file, &st) < 0) {
409                 /* Restore the privileged uid. */
410                 restore_uid();
411                 return 0;
412         }
413         /* Open the file containing the authorized keys. */
414         f = fopen(file, "r");
415         if (!f) {
416                 /* Restore the privileged uid. */
417                 restore_uid();
418                 return 0;
419         }
420         if (options.strict_modes) {
421                 int fail = 0;
422                 char buf[1024];
423                 /* Check open file in order to avoid open/stat races */
424                 if (fstat(fileno(f), &st) < 0 ||
425                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
426                     (st.st_mode & 022) != 0) {
427                         snprintf(buf, sizeof buf, "DSA authentication refused for %.100s: "
428                             "bad ownership or modes for '%s'.", pw->pw_name, file);
429                         fail = 1;
430                 } else {
431                         /* Check path to SSH_USER_PERMITTED_KEYS */
432                         int i;
433                         static const char *check[] = {
434                                 "", SSH_USER_DIR, NULL
435                         };
436                         for (i = 0; check[i]; i++) {
437                                 snprintf(line, sizeof line, "%.500s/%.100s",
438                                     pw->pw_dir, check[i]);
439                                 if (stat(line, &st) < 0 ||
440                                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
441                                     (st.st_mode & 022) != 0) {
442                                         snprintf(buf, sizeof buf,
443                                             "DSA authentication refused for %.100s: "
444                                             "bad ownership or modes for '%s'.",
445                                             pw->pw_name, line);
446                                         fail = 1;
447                                         break;
448                                 }
449                         }
450                 }
451                 if (fail) {
452                         log(buf);
453                         fclose(f);
454                         restore_uid();
455                         return 0;
456                 }
457         }
458         found_key = 0;
459         found = key_new(KEY_DSA);
460
461         while (fgets(line, sizeof(line), f)) {
462                 char *cp;
463                 linenum++;
464                 /* Skip leading whitespace, empty and comment lines. */
465                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
466                         ;
467                 if (!*cp || *cp == '\n' || *cp == '#')
468                         continue;
469                 bits = key_read(found, &cp);
470                 if (bits == 0)
471                         continue;
472                 if (key_equal(found, key)) {
473                         found_key = 1;
474                         debug("matching key found: file %s, line %ld",
475                             file, linenum);
476                         break;
477                 }
478         }
479         restore_uid();
480         fclose(f);
481         key_free(found);
482         return found_key;
483 }
This page took 0.098424 seconds and 5 git commands to generate.