xref: /freebsd/crypto/heimdal/appl/ftp/ftpd/gss_userok.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1b528cefcSMark Murray /*
25e9cd1aeSAssar Westerlund  * Copyright (c) 1998 - 2001 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "ftpd_locl.h"
35b528cefcSMark Murray #include <gssapi.h>
36b528cefcSMark Murray #include <krb5.h>
37b528cefcSMark Murray 
385e9cd1aeSAssar Westerlund RCSID("$Id: gss_userok.c,v 1.7 2001/01/30 00:36:58 assar Exp $");
39b528cefcSMark Murray 
40b528cefcSMark Murray /* XXX a bit too much of krb5 dependency here...
41b528cefcSMark Murray    What is the correct way to do this?
42b528cefcSMark Murray    */
43b528cefcSMark Murray 
44b528cefcSMark Murray extern krb5_context gssapi_krb5_context;
45b528cefcSMark Murray 
46b528cefcSMark Murray /* XXX sync with gssapi.c */
47b528cefcSMark Murray struct gss_data {
48b528cefcSMark Murray     gss_ctx_id_t context_hdl;
49b528cefcSMark Murray     char *client_name;
505e9cd1aeSAssar Westerlund     gss_cred_id_t delegated_cred_handle;
51b528cefcSMark Murray };
52b528cefcSMark Murray 
53b528cefcSMark Murray int gss_userok(void*, char*); /* to keep gcc happy */
54b528cefcSMark Murray 
55b528cefcSMark Murray int
56b528cefcSMark Murray gss_userok(void *app_data, char *username)
57b528cefcSMark Murray {
58b528cefcSMark Murray     struct gss_data *data = app_data;
59b528cefcSMark Murray     if(gssapi_krb5_context) {
60b528cefcSMark Murray 	krb5_principal client;
61b528cefcSMark Murray 	krb5_error_code ret;
625e9cd1aeSAssar Westerlund 
63b528cefcSMark Murray 	ret = krb5_parse_name(gssapi_krb5_context, data->client_name, &client);
64b528cefcSMark Murray 	if(ret)
65b528cefcSMark Murray 	    return 1;
66b528cefcSMark Murray 	ret = krb5_kuserok(gssapi_krb5_context, client, username);
675e9cd1aeSAssar Westerlund         if (!ret) {
68b528cefcSMark Murray            krb5_free_principal(gssapi_krb5_context, client);
695e9cd1aeSAssar Westerlund            return 1;
705e9cd1aeSAssar Westerlund         }
715e9cd1aeSAssar Westerlund 
725e9cd1aeSAssar Westerlund         ret = 0;
735e9cd1aeSAssar Westerlund 
745e9cd1aeSAssar Westerlund         /* more of krb-depend stuff :-( */
755e9cd1aeSAssar Westerlund 	/* gss_add_cred() ? */
765e9cd1aeSAssar Westerlund         if (data->delegated_cred_handle &&
775e9cd1aeSAssar Westerlund             data->delegated_cred_handle->ccache ) {
785e9cd1aeSAssar Westerlund 
795e9cd1aeSAssar Westerlund            krb5_ccache ccache = NULL;
805e9cd1aeSAssar Westerlund            char* ticketfile;
815e9cd1aeSAssar Westerlund            struct passwd *pw;
825e9cd1aeSAssar Westerlund 	   OM_uint32 minor_status;
835e9cd1aeSAssar Westerlund 
845e9cd1aeSAssar Westerlund            pw = getpwnam(username);
855e9cd1aeSAssar Westerlund 
865e9cd1aeSAssar Westerlund 	   if (pw == NULL) {
875e9cd1aeSAssar Westerlund 	       ret = 1;
885e9cd1aeSAssar Westerlund 	       goto fail;
895e9cd1aeSAssar Westerlund 	   }
905e9cd1aeSAssar Westerlund 
915e9cd1aeSAssar Westerlund            asprintf (&ticketfile, "%s%u", KRB5_DEFAULT_CCROOT, pw->pw_uid);
925e9cd1aeSAssar Westerlund 
935e9cd1aeSAssar Westerlund            ret = krb5_cc_resolve(gssapi_krb5_context, ticketfile, &ccache);
945e9cd1aeSAssar Westerlund            if (ret)
955e9cd1aeSAssar Westerlund               goto fail;
965e9cd1aeSAssar Westerlund 
975e9cd1aeSAssar Westerlund            ret = gss_krb5_copy_ccache(&minor_status,
985e9cd1aeSAssar Westerlund 				      data->delegated_cred_handle,
995e9cd1aeSAssar Westerlund 				      ccache);
1005e9cd1aeSAssar Westerlund            if (ret)
1015e9cd1aeSAssar Westerlund               goto fail;
1025e9cd1aeSAssar Westerlund 
1035e9cd1aeSAssar Westerlund            chown (ticketfile+5, pw->pw_uid, pw->pw_gid);
1045e9cd1aeSAssar Westerlund 
1055e9cd1aeSAssar Westerlund #ifdef KRB4
1065e9cd1aeSAssar Westerlund            if (k_hasafs()) {
1075e9cd1aeSAssar Westerlund               krb5_afslog(gssapi_krb5_context, ccache, 0, 0);
1085e9cd1aeSAssar Westerlund            }
1095e9cd1aeSAssar Westerlund #endif
1105e9cd1aeSAssar Westerlund            esetenv ("KRB5CCNAME", ticketfile, 1);
1115e9cd1aeSAssar Westerlund 
1125e9cd1aeSAssar Westerlund fail:
1135e9cd1aeSAssar Westerlund            if (ccache)
1145e9cd1aeSAssar Westerlund               krb5_cc_close(gssapi_krb5_context, ccache);
1155e9cd1aeSAssar Westerlund            krb5_cc_destroy(gssapi_krb5_context,
1165e9cd1aeSAssar Westerlund                            data->delegated_cred_handle->ccache);
1175e9cd1aeSAssar Westerlund            data->delegated_cred_handle->ccache = NULL;
1185e9cd1aeSAssar Westerlund            free(ticketfile);
1195e9cd1aeSAssar Westerlund         }
1205e9cd1aeSAssar Westerlund 
1215e9cd1aeSAssar Westerlund 	krb5_free_principal(gssapi_krb5_context, client);
1225e9cd1aeSAssar Westerlund         return ret;
123b528cefcSMark Murray     }
124b528cefcSMark Murray     return 1;
125b528cefcSMark Murray }
126