xref: /freebsd/contrib/pam-krb5/portable/krb5-profile.c (revision bf6873c5786e333d679a7838d28812febf479a8a)
1*bf6873c5SCy Schubert /*
2*bf6873c5SCy Schubert  * Kerberos compatibility functions for AIX's NAS libraries.
3*bf6873c5SCy Schubert  *
4*bf6873c5SCy Schubert  * AIX for some reason doesn't provide the krb5_appdefault_* functions, but
5*bf6873c5SCy Schubert  * does provide the underlying profile library functions (as a separate
6*bf6873c5SCy Schubert  * libk5profile with a separate k5profile.h header file).
7*bf6873c5SCy Schubert  *
8*bf6873c5SCy Schubert  * This file is therefore (apart from the includes, opening and closing
9*bf6873c5SCy Schubert  * comments, and the spots marked with an rra-c-util comment) a verbatim copy
10*bf6873c5SCy Schubert  * of src/lib/krb5/krb/appdefault.c from MIT Kerberos 1.4.4.
11*bf6873c5SCy Schubert  *
12*bf6873c5SCy Schubert  * The canonical version of this file is maintained in the rra-c-util package,
13*bf6873c5SCy Schubert  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
14*bf6873c5SCy Schubert  *
15*bf6873c5SCy Schubert  * Copyright 1985-2005 by the Massachusetts Institute of Technology.
16*bf6873c5SCy Schubert  * For license information, see the end of this file.
17*bf6873c5SCy Schubert  */
18*bf6873c5SCy Schubert 
19*bf6873c5SCy Schubert #include <config.h>
20*bf6873c5SCy Schubert 
21*bf6873c5SCy Schubert #include <krb5.h>
22*bf6873c5SCy Schubert #ifdef HAVE_K5PROFILE_H
23*bf6873c5SCy Schubert # include <k5profile.h>
24*bf6873c5SCy Schubert #endif
25*bf6873c5SCy Schubert #ifdef HAVE_PROFILE_H
26*bf6873c5SCy Schubert # include <profile.h>
27*bf6873c5SCy Schubert #endif
28*bf6873c5SCy Schubert #include <stdio.h>
29*bf6873c5SCy Schubert #include <string.h>
30*bf6873c5SCy Schubert 
31*bf6873c5SCy Schubert  /*xxx Duplicating this is annoying; try to work on a better way.*/
32*bf6873c5SCy Schubert static const char *const conf_yes[] = {
33*bf6873c5SCy Schubert 	"y", "yes", "true", "t", "1", "on",
34*bf6873c5SCy Schubert 	0,
35*bf6873c5SCy Schubert };
36*bf6873c5SCy Schubert 
37*bf6873c5SCy Schubert static const char *const conf_no[] = {
38*bf6873c5SCy Schubert 	"n", "no", "false", "nil", "0", "off",
39*bf6873c5SCy Schubert 	0,
40*bf6873c5SCy Schubert };
41*bf6873c5SCy Schubert 
conf_boolean(char * s)42*bf6873c5SCy Schubert static int conf_boolean(char *s)
43*bf6873c5SCy Schubert {
44*bf6873c5SCy Schubert 	const char * const *p;
45*bf6873c5SCy Schubert 	for(p=conf_yes; *p; p++) {
46*bf6873c5SCy Schubert 		if (!strcasecmp(*p,s))
47*bf6873c5SCy Schubert 			return 1;
48*bf6873c5SCy Schubert 	}
49*bf6873c5SCy Schubert 	for(p=conf_no; *p; p++) {
50*bf6873c5SCy Schubert 		if (!strcasecmp(*p,s))
51*bf6873c5SCy Schubert 		return 0;
52*bf6873c5SCy Schubert 	}
53*bf6873c5SCy Schubert 	/* Default to "no" */
54*bf6873c5SCy Schubert 	return 0;
55*bf6873c5SCy Schubert }
56*bf6873c5SCy Schubert 
appdefault_get(krb5_context context,const char * appname,const krb5_data * realm,const char * option,char ** ret_value)57*bf6873c5SCy Schubert static krb5_error_code appdefault_get(krb5_context context, const char *appname, const krb5_data *realm, const char *option, char **ret_value)
58*bf6873c5SCy Schubert {
59*bf6873c5SCy Schubert         profile_t profile;
60*bf6873c5SCy Schubert         const char *names[5];
61*bf6873c5SCy Schubert 	char **nameval = NULL;
62*bf6873c5SCy Schubert 	krb5_error_code retval;
63*bf6873c5SCy Schubert 	const char * realmstr =  realm?realm->data:NULL;
64*bf6873c5SCy Schubert 
65*bf6873c5SCy Schubert         /*
66*bf6873c5SCy Schubert          * rra-c-util: The magic values are internal, so a magic check for the
67*bf6873c5SCy Schubert          * context struct was removed here.  Call krb5_get_profile if it's
68*bf6873c5SCy Schubert          * available since the krb5_context struct may be opaque.
69*bf6873c5SCy Schubert          */
70*bf6873c5SCy Schubert 	    if (!context)
71*bf6873c5SCy Schubert 	    return KV5M_CONTEXT;
72*bf6873c5SCy Schubert 
73*bf6873c5SCy Schubert #ifdef HAVE_KRB5_GET_PROFILE
74*bf6873c5SCy Schubert             krb5_get_profile(context, &profile);
75*bf6873c5SCy Schubert #else
76*bf6873c5SCy Schubert 	    profile = context->profile;
77*bf6873c5SCy Schubert #endif
78*bf6873c5SCy Schubert 
79*bf6873c5SCy Schubert 	/*
80*bf6873c5SCy Schubert 	 * Try number one:
81*bf6873c5SCy Schubert 	 *
82*bf6873c5SCy Schubert 	 * [appdefaults]
83*bf6873c5SCy Schubert 	 *	app = {
84*bf6873c5SCy Schubert 	 *		SOME.REALM = {
85*bf6873c5SCy Schubert 	 *			option = <boolean>
86*bf6873c5SCy Schubert 	 *		}
87*bf6873c5SCy Schubert 	 *	}
88*bf6873c5SCy Schubert 	 */
89*bf6873c5SCy Schubert 
90*bf6873c5SCy Schubert 	names[0] = "appdefaults";
91*bf6873c5SCy Schubert 	names[1] = appname;
92*bf6873c5SCy Schubert 
93*bf6873c5SCy Schubert 	if (realmstr) {
94*bf6873c5SCy Schubert 		names[2] = realmstr;
95*bf6873c5SCy Schubert 		names[3] = option;
96*bf6873c5SCy Schubert 		names[4] = 0;
97*bf6873c5SCy Schubert 		retval = profile_get_values(profile, names, &nameval);
98*bf6873c5SCy Schubert 		if (retval == 0 && nameval && nameval[0]) {
99*bf6873c5SCy Schubert 			*ret_value = strdup(nameval[0]);
100*bf6873c5SCy Schubert 			goto goodbye;
101*bf6873c5SCy Schubert 		}
102*bf6873c5SCy Schubert 	}
103*bf6873c5SCy Schubert 
104*bf6873c5SCy Schubert 	/*
105*bf6873c5SCy Schubert 	 * Try number two:
106*bf6873c5SCy Schubert 	 *
107*bf6873c5SCy Schubert 	 * [appdefaults]
108*bf6873c5SCy Schubert 	 *	app = {
109*bf6873c5SCy Schubert 	 *		option = <boolean>
110*bf6873c5SCy Schubert 	 *      }
111*bf6873c5SCy Schubert 	 */
112*bf6873c5SCy Schubert 
113*bf6873c5SCy Schubert 	names[2] = option;
114*bf6873c5SCy Schubert 	names[3] = 0;
115*bf6873c5SCy Schubert 	retval = profile_get_values(profile, names, &nameval);
116*bf6873c5SCy Schubert 	if (retval == 0 && nameval && nameval[0]) {
117*bf6873c5SCy Schubert 		*ret_value = strdup(nameval[0]);
118*bf6873c5SCy Schubert 		goto goodbye;
119*bf6873c5SCy Schubert 	}
120*bf6873c5SCy Schubert 
121*bf6873c5SCy Schubert 	/*
122*bf6873c5SCy Schubert 	 * Try number three:
123*bf6873c5SCy Schubert 	 *
124*bf6873c5SCy Schubert 	 * [appdefaults]
125*bf6873c5SCy Schubert 	 *	realm = {
126*bf6873c5SCy Schubert 	 *		option = <boolean>
127*bf6873c5SCy Schubert 	 */
128*bf6873c5SCy Schubert 
129*bf6873c5SCy Schubert 	if (realmstr) {
130*bf6873c5SCy Schubert 		names[1] = realmstr;
131*bf6873c5SCy Schubert 		names[2] = option;
132*bf6873c5SCy Schubert 		names[3] = 0;
133*bf6873c5SCy Schubert 		retval = profile_get_values(profile, names, &nameval);
134*bf6873c5SCy Schubert 		if (retval == 0 && nameval && nameval[0]) {
135*bf6873c5SCy Schubert 			*ret_value = strdup(nameval[0]);
136*bf6873c5SCy Schubert 			goto goodbye;
137*bf6873c5SCy Schubert 		}
138*bf6873c5SCy Schubert 	}
139*bf6873c5SCy Schubert 
140*bf6873c5SCy Schubert 	/*
141*bf6873c5SCy Schubert 	 * Try number four:
142*bf6873c5SCy Schubert 	 *
143*bf6873c5SCy Schubert 	 * [appdefaults]
144*bf6873c5SCy Schubert 	 *	option = <boolean>
145*bf6873c5SCy Schubert 	 */
146*bf6873c5SCy Schubert 
147*bf6873c5SCy Schubert 	names[1] = option;
148*bf6873c5SCy Schubert 	names[2] = 0;
149*bf6873c5SCy Schubert 	retval = profile_get_values(profile, names, &nameval);
150*bf6873c5SCy Schubert 	if (retval == 0 && nameval && nameval[0]) {
151*bf6873c5SCy Schubert 		*ret_value = strdup(nameval[0]);
152*bf6873c5SCy Schubert 	} else {
153*bf6873c5SCy Schubert 		return retval;
154*bf6873c5SCy Schubert 	}
155*bf6873c5SCy Schubert 
156*bf6873c5SCy Schubert goodbye:
157*bf6873c5SCy Schubert 	if (nameval) {
158*bf6873c5SCy Schubert 		char **cpp;
159*bf6873c5SCy Schubert 		for (cpp = nameval; *cpp; cpp++)
160*bf6873c5SCy Schubert 			free(*cpp);
161*bf6873c5SCy Schubert 		free(nameval);
162*bf6873c5SCy Schubert 	}
163*bf6873c5SCy Schubert 	return 0;
164*bf6873c5SCy Schubert }
165*bf6873c5SCy Schubert 
166*bf6873c5SCy Schubert void KRB5_CALLCONV
krb5_appdefault_boolean(krb5_context context,const char * appname,const krb5_data * realm,const char * option,int default_value,int * ret_value)167*bf6873c5SCy Schubert krb5_appdefault_boolean(krb5_context context, const char *appname, const krb5_data *realm, const char *option, int default_value, int *ret_value)
168*bf6873c5SCy Schubert {
169*bf6873c5SCy Schubert 	char *string = NULL;
170*bf6873c5SCy Schubert 	krb5_error_code retval;
171*bf6873c5SCy Schubert 
172*bf6873c5SCy Schubert 	retval = appdefault_get(context, appname, realm, option, &string);
173*bf6873c5SCy Schubert 
174*bf6873c5SCy Schubert 	if (! retval && string) {
175*bf6873c5SCy Schubert 		*ret_value = conf_boolean(string);
176*bf6873c5SCy Schubert 		free(string);
177*bf6873c5SCy Schubert 	} else
178*bf6873c5SCy Schubert 		*ret_value = default_value;
179*bf6873c5SCy Schubert }
180*bf6873c5SCy Schubert 
181*bf6873c5SCy Schubert void KRB5_CALLCONV
krb5_appdefault_string(krb5_context context,const char * appname,const krb5_data * realm,const char * option,const char * default_value,char ** ret_value)182*bf6873c5SCy Schubert krb5_appdefault_string(krb5_context context, const char *appname, const krb5_data *realm, const char *option, const char *default_value, char **ret_value)
183*bf6873c5SCy Schubert {
184*bf6873c5SCy Schubert 	krb5_error_code retval;
185*bf6873c5SCy Schubert 	char *string;
186*bf6873c5SCy Schubert 
187*bf6873c5SCy Schubert 	retval = appdefault_get(context, appname, realm, option, &string);
188*bf6873c5SCy Schubert 
189*bf6873c5SCy Schubert 	if (! retval && string) {
190*bf6873c5SCy Schubert 		*ret_value = string;
191*bf6873c5SCy Schubert 	} else {
192*bf6873c5SCy Schubert 		*ret_value = strdup(default_value);
193*bf6873c5SCy Schubert 	}
194*bf6873c5SCy Schubert }
195*bf6873c5SCy Schubert 
196*bf6873c5SCy Schubert /*
197*bf6873c5SCy Schubert  * Copyright (C) 1985-2005 by the Massachusetts Institute of Technology.
198*bf6873c5SCy Schubert  * All rights reserved.
199*bf6873c5SCy Schubert  *
200*bf6873c5SCy Schubert  * Export of this software from the United States of America may require
201*bf6873c5SCy Schubert  * a specific license from the United States Government.  It is the
202*bf6873c5SCy Schubert  * responsibility of any person or organization contemplating export to
203*bf6873c5SCy Schubert  * obtain such a license before exporting.
204*bf6873c5SCy Schubert  *
205*bf6873c5SCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
206*bf6873c5SCy Schubert  * distribute this software and its documentation for any purpose and
207*bf6873c5SCy Schubert  * without fee is hereby granted, provided that the above copyright
208*bf6873c5SCy Schubert  * notice appear in all copies and that both that copyright notice and
209*bf6873c5SCy Schubert  * this permission notice appear in supporting documentation, and that
210*bf6873c5SCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
211*bf6873c5SCy Schubert  * to distribution of the software without specific, written prior
212*bf6873c5SCy Schubert  * permission.  Furthermore if you modify this software you must label
213*bf6873c5SCy Schubert  * your software as modified software and not distribute it in such a
214*bf6873c5SCy Schubert  * fashion that it might be confused with the original MIT software.
215*bf6873c5SCy Schubert  * M.I.T. makes no representations about the suitability of this software
216*bf6873c5SCy Schubert  * for any purpose.  It is provided "as is" without express or implied
217*bf6873c5SCy Schubert  * warranty.
218*bf6873c5SCy Schubert  *
219*bf6873c5SCy Schubert  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
220*bf6873c5SCy Schubert  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
221*bf6873c5SCy Schubert  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
222*bf6873c5SCy Schubert  *
223*bf6873c5SCy Schubert  * Individual source code files are copyright MIT, Cygnus Support,
224*bf6873c5SCy Schubert  * OpenVision, Oracle, Sun Soft, FundsXpress, and others.
225*bf6873c5SCy Schubert  *
226*bf6873c5SCy Schubert  * Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira,
227*bf6873c5SCy Schubert  * and Zephyr are trademarks of the Massachusetts Institute of Technology
228*bf6873c5SCy Schubert  * (MIT).  No commercial use of these trademarks may be made without
229*bf6873c5SCy Schubert  * prior written permission of MIT.
230*bf6873c5SCy Schubert  *
231*bf6873c5SCy Schubert  * "Commercial use" means use of a name in a product or other for-profit
232*bf6873c5SCy Schubert  * manner.  It does NOT prevent a commercial firm from referring to the
233*bf6873c5SCy Schubert  * MIT trademarks in order to convey information (although in doing so,
234*bf6873c5SCy Schubert  * recognition of their trademark status should be given).
235*bf6873c5SCy Schubert  *
236*bf6873c5SCy Schubert  * There is no SPDX-License-Identifier registered for this license.
237*bf6873c5SCy Schubert  */
238