1 /*
2  * Copyright (c) 2011, PADL Software Pty Ltd.
3  * 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  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of PADL Software nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "mech_locl.h"
34 
35 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
36 gss_acquire_cred_with_password(OM_uint32 *minor_status,
37 			       const gss_name_t desired_name,
38 			       const gss_buffer_t password,
39 			       OM_uint32 time_req,
40 			       const gss_OID_set desired_mechs,
41 			       gss_cred_usage_t cred_usage,
42 			       gss_cred_id_t *output_cred_handle,
43 			       gss_OID_set *actual_mechs,
44 			       OM_uint32 *time_rec)
45 {
46     OM_uint32 major_status, tmp_minor;
47 
48     if (desired_mechs == GSS_C_NO_OID_SET) {
49 	major_status = _gss_acquire_cred_ext(minor_status,
50 					     desired_name,
51 					     GSS_C_CRED_PASSWORD,
52 					     password,
53 					     time_req,
54 					     GSS_C_NO_OID,
55 					     cred_usage,
56 					     output_cred_handle);
57 	if (GSS_ERROR(major_status))
58 	    return major_status;
59     } else {
60 	size_t i;
61 	struct _gss_cred *new_cred;
62 
63 	new_cred = calloc(1, sizeof(*new_cred));
64 	if (new_cred == NULL) {
65 	    *minor_status = ENOMEM;
66 	    return GSS_S_FAILURE;
67 	}
68 	HEIM_SLIST_INIT(&new_cred->gc_mc);
69 
70 	for (i = 0; i < desired_mechs->count; i++) {
71 	    struct _gss_cred *tmp_cred = NULL;
72 	    struct _gss_mechanism_cred *mc;
73 
74 	    major_status = _gss_acquire_cred_ext(minor_status,
75 						 desired_name,
76 						 GSS_C_CRED_PASSWORD,
77 						 password,
78 						 time_req,
79 						 &desired_mechs->elements[i],
80 						 cred_usage,
81 						 (gss_cred_id_t *)&tmp_cred);
82 	    if (GSS_ERROR(major_status))
83 		continue;
84 
85 	    mc = HEIM_SLIST_FIRST(&tmp_cred->gc_mc);
86 	    if (mc) {
87 		HEIM_SLIST_REMOVE_HEAD(&tmp_cred->gc_mc, gmc_link);
88 		HEIM_SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
89 	    }
90 
91 	    gss_release_cred(&tmp_minor, (gss_cred_id_t *)&tmp_cred);
92 	}
93 
94 	if (!HEIM_SLIST_FIRST(&new_cred->gc_mc)) {
95 	    free(new_cred);
96 	    *minor_status = 0;
97 	    return GSS_S_NO_CRED;
98 	}
99 
100 	*output_cred_handle = (gss_cred_id_t)new_cred;
101     }
102 
103     if (actual_mechs != NULL || time_rec != NULL) {
104 	major_status = gss_inquire_cred(minor_status,
105 					*output_cred_handle,
106 					NULL,
107 					time_rec,
108 					NULL,
109 					actual_mechs);
110 	if (GSS_ERROR(major_status)) {
111 	    gss_release_cred(&tmp_minor, output_cred_handle);
112 	    return major_status;
113 	}
114     }
115 
116     *minor_status = 0;
117     return GSS_S_COMPLETE;
118 }
119