xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/vfy_increds.c (revision 5d01ff851af9111addb3ce2812568425bdf01c29)
17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
47c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #include <k5-int.h>
8505d05c7Sgtb #include "int-proto.h"
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_libdefault_boolean();
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate static krb5_error_code
13505d05c7Sgtb krb5_cc_copy_creds_except(krb5_context context, krb5_ccache incc, krb5_ccache outcc, krb5_principal princ)
147c478bd9Sstevel@tonic-gate {
157c478bd9Sstevel@tonic-gate    krb5_error_code code;
167c478bd9Sstevel@tonic-gate    krb5_flags flags;
177c478bd9Sstevel@tonic-gate    krb5_cc_cursor cur;
187c478bd9Sstevel@tonic-gate    krb5_creds creds;
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate    flags = 0;				/* turns off OPENCLOSE mode */
217c478bd9Sstevel@tonic-gate    if ((code = krb5_cc_set_flags(context, incc, flags)) != NULL)
227c478bd9Sstevel@tonic-gate       return(code);
237c478bd9Sstevel@tonic-gate    if ((code = krb5_cc_set_flags(context, outcc, flags)) != NULL)
247c478bd9Sstevel@tonic-gate       return(code);
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate    if ((code = krb5_cc_start_seq_get(context, incc, &cur)) != NULL)
277c478bd9Sstevel@tonic-gate       goto cleanup;
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate    while ((code = krb5_cc_next_cred(context, incc, &cur, &creds)) == NULL) {
307c478bd9Sstevel@tonic-gate       if (krb5_principal_compare(context, princ, creds.server))
317c478bd9Sstevel@tonic-gate 	 continue;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate       code = krb5_cc_store_cred(context, outcc, &creds);
347c478bd9Sstevel@tonic-gate       krb5_free_cred_contents(context, &creds);
357c478bd9Sstevel@tonic-gate       if (code)
367c478bd9Sstevel@tonic-gate 	 goto cleanup;
377c478bd9Sstevel@tonic-gate    }
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate    if (code != KRB5_CC_END)
407c478bd9Sstevel@tonic-gate       goto cleanup;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate    code = 0;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate cleanup:
457c478bd9Sstevel@tonic-gate    flags = KRB5_TC_OPENCLOSE;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate    if (code)
487c478bd9Sstevel@tonic-gate       (void) krb5_cc_set_flags(context, incc, flags);
497c478bd9Sstevel@tonic-gate    else
507c478bd9Sstevel@tonic-gate       code = krb5_cc_set_flags(context, incc, flags);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate    if (code)
537c478bd9Sstevel@tonic-gate       (void) krb5_cc_set_flags(context, outcc, flags);
547c478bd9Sstevel@tonic-gate    else
557c478bd9Sstevel@tonic-gate       code = krb5_cc_set_flags(context, outcc, flags);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate    return(code);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
60505d05c7Sgtb krb5_error_code KRB5_CALLCONV
617c478bd9Sstevel@tonic-gate krb5_verify_init_creds(krb5_context context,
627c478bd9Sstevel@tonic-gate 		       krb5_creds *creds,
637c478bd9Sstevel@tonic-gate 		       krb5_principal server_arg,
647c478bd9Sstevel@tonic-gate 		       krb5_keytab keytab_arg,
657c478bd9Sstevel@tonic-gate 		       krb5_ccache *ccache_arg,
667c478bd9Sstevel@tonic-gate 		       krb5_verify_init_creds_opt *options)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate    krb5_error_code ret;
697c478bd9Sstevel@tonic-gate    krb5_principal server;
707c478bd9Sstevel@tonic-gate    krb5_keytab keytab;
717c478bd9Sstevel@tonic-gate    krb5_ccache ccache;
727c478bd9Sstevel@tonic-gate    krb5_keytab_entry kte;
737c478bd9Sstevel@tonic-gate    krb5_creds in_creds, *out_creds;
747c478bd9Sstevel@tonic-gate    krb5_auth_context authcon;
757c478bd9Sstevel@tonic-gate    krb5_data ap_req;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate    /* KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate    server = NULL;
807c478bd9Sstevel@tonic-gate    keytab = NULL;
817c478bd9Sstevel@tonic-gate    ccache = NULL;
827c478bd9Sstevel@tonic-gate    out_creds = NULL;
837c478bd9Sstevel@tonic-gate    authcon = NULL;
847c478bd9Sstevel@tonic-gate    ap_req.data = NULL;
857c478bd9Sstevel@tonic-gate 
86*5d01ff85Swillf    if (server_arg)
877c478bd9Sstevel@tonic-gate       server = server_arg;
88*5d01ff85Swillf    else if (ret = krb5_sname_to_principal(context, NULL, NULL,
89*5d01ff85Swillf 					KRB5_NT_SRV_HST, &server))
907c478bd9Sstevel@tonic-gate       goto cleanup;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate    /* first, check if the server is in the keytab.  If not, there's
937c478bd9Sstevel@tonic-gate       no reason to continue.  rd_req does all this, but there's
947c478bd9Sstevel@tonic-gate       no way to know that a given error is caused by a missing
957c478bd9Sstevel@tonic-gate       keytab or key, and not by some other problem. */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate    if (keytab_arg) {
987c478bd9Sstevel@tonic-gate       keytab = keytab_arg;
997c478bd9Sstevel@tonic-gate    } else {
100*5d01ff85Swillf        /* Solaris Kerberos: ignore errors here, deal with below */
101*5d01ff85Swillf       ret = krb5_kt_default(context, &keytab);
1027c478bd9Sstevel@tonic-gate    }
1037c478bd9Sstevel@tonic-gate 
104*5d01ff85Swillf    /* Warning: be very, very careful when modifying the logic here */
105*5d01ff85Swillf    if (keytab == NULL ||
106*5d01ff85Swillf        (ret = krb5_kt_get_entry(context, keytab, server, 0, 0, &kte))) {
1077c478bd9Sstevel@tonic-gate        /* this means there is no keying material.  This is ok, as long as
1087c478bd9Sstevel@tonic-gate 	  it is not prohibited by the configuration */
109*5d01ff85Swillf 
110*5d01ff85Swillf        int nofail = 1;  /* Solaris Kerberos: default return error if keytab problems */
111*5d01ff85Swillf 
1127c478bd9Sstevel@tonic-gate        if (options &&
1137c478bd9Sstevel@tonic-gate 	   (options->flags & KRB5_VERIFY_INIT_CREDS_OPT_AP_REQ_NOFAIL)) {
114*5d01ff85Swillf 	   /* first, if options are set then use the option value to set nofail */
115*5d01ff85Swillf 	    nofail = options->ap_req_nofail;
116*5d01ff85Swillf        } else {
117*5d01ff85Swillf 	   /*
118*5d01ff85Swillf 	    * Check verify_ap_req_nofail if set in config file.  Note this logic
119*5d01ff85Swillf 	    * assumes that krb5_libdefault_boolean will not set nofail to a
120*5d01ff85Swillf 	    * default value if verify_ap_req_nofail is not explictly set in
121*5d01ff85Swillf 	    * config file.  Don't care about the return code.
122*5d01ff85Swillf 	    */
123*5d01ff85Swillf 	   (void) krb5_libdefault_boolean(context, &creds->client->realm,
124*5d01ff85Swillf 					  "verify_ap_req_nofail",
125*5d01ff85Swillf 					  &nofail);
1267c478bd9Sstevel@tonic-gate        }
127*5d01ff85Swillf        /* Solaris Kerberos: exit without an error ONLY if nofail is false */
128*5d01ff85Swillf        if (!nofail)
129*5d01ff85Swillf 	   ret = 0;
130*5d01ff85Swillf 
131*5d01ff85Swillf        goto cleanup;
1327c478bd9Sstevel@tonic-gate    }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate    krb5_kt_free_entry(context, &kte);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate    /* If the creds are for the server principal, we're set, just do
1377c478bd9Sstevel@tonic-gate       a mk_req.	 Otherwise, do a get_credentials first. */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate    if (krb5_principal_compare(context, server, creds->server)) {
1407c478bd9Sstevel@tonic-gate       /* make an ap_req */
141505d05c7Sgtb       if ((ret = krb5_mk_req_extended(context, &authcon, 0, NULL, creds,
142505d05c7Sgtb 				     &ap_req)))
1437c478bd9Sstevel@tonic-gate 	 goto cleanup;
1447c478bd9Sstevel@tonic-gate    } else {
1457c478bd9Sstevel@tonic-gate       /* this is unclean, but it's the easiest way without ripping the
1467c478bd9Sstevel@tonic-gate 	 library into very small pieces.  store the client's initial cred
1477c478bd9Sstevel@tonic-gate 	 in a memory ccache, then call the library.  Later, we'll copy
1487c478bd9Sstevel@tonic-gate 	 everything except the initial cred into the ccache we return to
1497c478bd9Sstevel@tonic-gate 	 the user.  A clean implementation would involve library
1507c478bd9Sstevel@tonic-gate 	 internals with a coherent idea of "in" and "out". */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate       /* insert the initial cred into the ccache */
1537c478bd9Sstevel@tonic-gate 
154505d05c7Sgtb       if ((ret = krb5_cc_resolve(context, "MEMORY:rd_req", &ccache)))
1557c478bd9Sstevel@tonic-gate 	 goto cleanup;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate       if ((ret = krb5_cc_initialize(context, ccache, creds->client)) != NULL)
1587c478bd9Sstevel@tonic-gate 	 goto cleanup;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate       if ((ret = krb5_cc_store_cred(context, ccache, creds)) != NULL)
1617c478bd9Sstevel@tonic-gate 	 goto cleanup;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate       /* set up for get_creds */
1647c478bd9Sstevel@tonic-gate       memset(&in_creds, 0, sizeof(in_creds));
1657c478bd9Sstevel@tonic-gate       in_creds.client = creds->client;
1667c478bd9Sstevel@tonic-gate       in_creds.server = server;
167505d05c7Sgtb       if ((ret = krb5_timeofday(context, &in_creds.times.endtime)))
1687c478bd9Sstevel@tonic-gate 	 goto cleanup;
1697c478bd9Sstevel@tonic-gate       in_creds.times.endtime += 5*60;
1707c478bd9Sstevel@tonic-gate 
171505d05c7Sgtb       if ((ret = krb5_get_credentials(context, 0, ccache, &in_creds,
172505d05c7Sgtb 				     &out_creds)))
1737c478bd9Sstevel@tonic-gate 	 goto cleanup;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate       /* make an ap_req */
176505d05c7Sgtb       if ((ret = krb5_mk_req_extended(context, &authcon, 0, NULL, out_creds,
177505d05c7Sgtb 				     &ap_req)))
1787c478bd9Sstevel@tonic-gate 	 goto cleanup;
1797c478bd9Sstevel@tonic-gate    }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate    /* wipe the auth context for mk_req */
1827c478bd9Sstevel@tonic-gate    if (authcon) {
1837c478bd9Sstevel@tonic-gate       krb5_auth_con_free(context, authcon);
1847c478bd9Sstevel@tonic-gate       authcon = NULL;
1857c478bd9Sstevel@tonic-gate    }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate    /* verify the ap_req */
1887c478bd9Sstevel@tonic-gate 
189505d05c7Sgtb    if ((ret = krb5_rd_req(context, &authcon, &ap_req, server, keytab,
190505d05c7Sgtb 			 NULL, NULL)))
1917c478bd9Sstevel@tonic-gate       goto cleanup;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate    /* if we get this far, then the verification succeeded.  We can
1947c478bd9Sstevel@tonic-gate       still fail if the library stuff here fails, but that's it */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate    if (ccache_arg && ccache) {
1977c478bd9Sstevel@tonic-gate        if (*ccache_arg == NULL) {
1987c478bd9Sstevel@tonic-gate 	   krb5_ccache retcc;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	   retcc = NULL;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	   if (((ret = krb5_cc_resolve(context, "MEMORY:rd_req2", &retcc)) != NULL) ||
2037c478bd9Sstevel@tonic-gate 	       ((ret = krb5_cc_initialize(context, retcc, creds->client)) != NULL) ||
2047c478bd9Sstevel@tonic-gate 	       ((ret = krb5_cc_copy_creds_except(context, ccache, retcc,
2057c478bd9Sstevel@tonic-gate 						creds->server)) != NULL)) {
2067c478bd9Sstevel@tonic-gate 	       if (retcc)
2077c478bd9Sstevel@tonic-gate 		   (void) krb5_cc_destroy(context, retcc);
2087c478bd9Sstevel@tonic-gate 	   } else {
2097c478bd9Sstevel@tonic-gate 	       *ccache_arg = retcc;
2107c478bd9Sstevel@tonic-gate 	   }
2117c478bd9Sstevel@tonic-gate        } else {
2127c478bd9Sstevel@tonic-gate 	   ret = krb5_cc_copy_creds_except(context, ccache, *ccache_arg,
2137c478bd9Sstevel@tonic-gate 					   server);
2147c478bd9Sstevel@tonic-gate        }
2157c478bd9Sstevel@tonic-gate    }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate    /* if any of the above paths returned an errors, then ret is set
2187c478bd9Sstevel@tonic-gate       accordingly.  either that, or it's zero, which is fine, too */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate cleanup:
2217c478bd9Sstevel@tonic-gate    if (!server_arg && server)
2227c478bd9Sstevel@tonic-gate       krb5_free_principal(context, server);
2237c478bd9Sstevel@tonic-gate    if (!keytab_arg && keytab)
2247c478bd9Sstevel@tonic-gate       (void) krb5_kt_close(context, keytab);
2257c478bd9Sstevel@tonic-gate    if (ccache)
2267c478bd9Sstevel@tonic-gate       (void) krb5_cc_destroy(context, ccache);
2277c478bd9Sstevel@tonic-gate    if (out_creds)
2287c478bd9Sstevel@tonic-gate       krb5_free_creds(context, out_creds);
2297c478bd9Sstevel@tonic-gate    if (authcon)
2307c478bd9Sstevel@tonic-gate       krb5_auth_con_free(context, authcon);
2317c478bd9Sstevel@tonic-gate    if (ap_req.data)
2327c478bd9Sstevel@tonic-gate       krb5_xfree(ap_req.data);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate    return(ret);
2357c478bd9Sstevel@tonic-gate }
236