xref: /illumos-gate/usr/src/lib/krb5/plugins/kdb/db2/db2_exp.c (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright 2006 by the Massachusetts Institute of Technology.
10  * All Rights Reserved.
11  *
12  * Export of this software from the United States of America may
13  *   require a specific license from the United States Government.
14  *   It is the responsibility of any person or organization contemplating
15  *   export to obtain such a license before exporting.
16  *
17  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18  * distribute this software and its documentation for any purpose and
19  * without fee is hereby granted, provided that the above copyright
20  * notice appear in all copies and that both that copyright notice and
21  * this permission notice appear in supporting documentation, and that
22  * the name of M.I.T. not be used in advertising or publicity pertaining
23  * to distribution of the software without specific, written prior
24  * permission.  Furthermore if you modify this software you must label
25  * your software as modified software and not distribute it in such a
26  * fashion that it might be confused with the original M.I.T. software.
27  * M.I.T. makes no representations about the suitability of
28  * this software for any purpose.  It is provided "as is" without express
29  * or implied warranty.
30  */
31 
32 /**********************************************************************
33 *
34 *	C %name:		db2_exp.c %
35 *	Instance:		idc_sec_2
36 *	Description:
37 *	%created_by:	spradeep %
38 *	%date_created:	Tue Apr  5 11:44:00 2005 %
39 *
40 **********************************************************************/
41 #ifndef lint
42 static char *_csrc = "@(#) %filespec: db2_exp.c~5 %  (%full_filespec: db2_exp.c~5:csrc:idc_sec#2 %)";
43 #endif
44 
45 #include "k5-int.h"
46 
47 #if HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 
51 #include <db.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <utime.h>
55 #include <kdb/kdb5.h>
56 #include "kdb_db2.h"
57 #include "kdb_xdr.h"
58 #include "policy_db.h"
59 
60 /* Quick and dirty wrapper functions to provide for thread safety
61    within the plugin, instead of making the kdb5 library do it.  Eventually
62    these should be integrated into the real functions.
63 
64    Some of the functions wrapped here are also called directly from
65    within this library (e.g., create calls open), so simply dropping
66    locking code into the top and bottom of each referenced function
67    won't do.  (We aren't doing recursive locks, currently.)  */
68 
69 static k5_mutex_t *krb5_db2_mutex;
70 
71 #define WRAP(NAME,TYPE,ARGLIST,ARGNAMES,ERROR_RESULT)	\
72 	static TYPE wrap_##NAME ARGLIST			\
73 	{						\
74 	    TYPE result;				\
75 	    int code = k5_mutex_lock (krb5_db2_mutex);	\
76 	    if (code) { return ERROR_RESULT; }		\
77 	    result = NAME ARGNAMES;			\
78 	    k5_mutex_unlock (krb5_db2_mutex);		\
79 	    return result;				\
80 	}						\
81 	/* hack: decl to allow a following ";" */	\
82 	static TYPE wrap_##NAME ()
83 
84 /* Two special cases: void (can't assign result), and krb5_error_code
85    (return error from locking code).  */
86 
87 #define WRAP_VOID(NAME,ARGLIST,ARGNAMES)		\
88 	static void wrap_##NAME ARGLIST			\
89 	{						\
90 	    int code = k5_mutex_lock (krb5_db2_mutex);	\
91 	    if (code) { return; }			\
92 	    NAME ARGNAMES;				\
93 	    k5_mutex_unlock (krb5_db2_mutex);		\
94 	}						\
95 	/* hack: decl to allow a following ";" */	\
96 	static void wrap_##NAME ()
97 
98 #define WRAP_K(NAME,ARGLIST,ARGNAMES)			\
99 	WRAP(NAME,krb5_error_code,ARGLIST,ARGNAMES,code)
100 
101 WRAP_K (krb5_db2_open,
102 	( krb5_context kcontext,
103 	  char *conf_section,
104 	  char **db_args,
105 	  int mode ),
106 	(kcontext, conf_section, db_args, mode));
107 WRAP_K (krb5_db2_db_fini, (krb5_context ctx), (ctx));
108 WRAP_K (krb5_db2_create,
109 	( krb5_context kcontext, char *conf_section, char **db_args ),
110 	(kcontext, conf_section, db_args));
111 WRAP_K (krb5_db2_destroy,
112 	( krb5_context kcontext, char *conf_section, char **db_args ),
113 	(kcontext, conf_section, db_args));
114 WRAP_K (krb5_db2_db_get_age,
115 	(krb5_context ctx,
116 		   char *s,
117 	 time_t *t),
118 	(ctx, s, t));
119 WRAP_K (krb5_db2_db_set_option,
120 	( krb5_context kcontext,
121 	  int option,
122 	  void *value ),
123 	(kcontext, option, value));
124 
125 WRAP_K (krb5_db2_db_lock,
126 	( krb5_context 	  context,
127 	  int 	 	  in_mode),
128 	(context, in_mode));
129 WRAP_K (krb5_db2_db_unlock, (krb5_context ctx), (ctx));
130 
131 WRAP_K (krb5_db2_db_get_principal,
132 	(krb5_context ctx,
133 		   krb5_const_principal p,
134 		   krb5_db_entry *d,
135 		   int * i,
136 	 krb5_boolean *b),
137 	(ctx, p, d, i, b));
138 WRAP_K (krb5_db2_db_free_principal,
139 	(krb5_context ctx,
140 		   krb5_db_entry *d,
141 	 int i),
142 	(ctx, d, i));
143 WRAP_K (krb5_db2_db_put_principal,
144 	(krb5_context ctx,
145 	 krb5_db_entry *d,
146 	 int *i,
147 	 char **db_args),
148 	(ctx, d, i, db_args));
149 WRAP_K (krb5_db2_db_delete_principal,
150 	(krb5_context context,
151 	 krb5_const_principal searchfor,
152 	 int *nentries),
153 	(context, searchfor, nentries));
154 
155 WRAP_K (krb5_db2_db_iterate,
156 	(krb5_context ctx, char *s,
157 	 krb5_error_code (*f) (krb5_pointer,
158 			      krb5_db_entry *),
159 	 krb5_pointer p),
160 	(ctx, s, f, p));
161 
162 WRAP_K (krb5_db2_create_policy,
163 	(krb5_context context, osa_policy_ent_t entry),
164 	(context, entry));
165 WRAP_K (krb5_db2_get_policy,
166 	( krb5_context kcontext,
167 	  char *name,
168 	  osa_policy_ent_t *policy,
169 	  int *cnt),
170 	(kcontext, name, policy, cnt));
171 WRAP_K (krb5_db2_put_policy,
172 	( krb5_context kcontext, osa_policy_ent_t policy ),
173 	(kcontext, policy));
174 WRAP_K (krb5_db2_iter_policy,
175 	( krb5_context kcontext,
176 	  char *match_entry,
177 	  osa_adb_iter_policy_func func,
178 	  void *data ),
179 	(kcontext, match_entry, func, data));
180 WRAP_K (krb5_db2_delete_policy,
181 	( krb5_context kcontext, char *policy ),
182 	(kcontext, policy));
183 WRAP_VOID (krb5_db2_free_policy,
184 	   ( krb5_context kcontext, osa_policy_ent_t entry ),
185 	   (kcontext, entry));
186 
187 WRAP (krb5_db2_alloc, void *,
188       ( krb5_context kcontext,
189 	void *ptr,
190 	size_t size ),
191       (kcontext, ptr, size), NULL);
192 WRAP_VOID (krb5_db2_free,
193 	   ( krb5_context kcontext, void *ptr ),
194 	   (kcontext, ptr));
195 
196 WRAP_K (krb5_db2_set_master_key_ext,
197 	( krb5_context kcontext, char *pwd, krb5_keyblock *key),
198 	(kcontext, pwd, key));
199 WRAP_K (krb5_db2_db_get_mkey,
200 	( krb5_context context, krb5_keyblock **key),
201 	(context, key));
202 WRAP_K (krb5_db2_promote_db,
203 	( krb5_context kcontext, char *conf_section, char **db_args ),
204 	(kcontext, conf_section, db_args));
205 
206 static krb5_error_code
207 hack_init ()
208 {
209     krb5_error_code c;
210     c = krb5int_mutex_alloc (&krb5_db2_mutex);
211     if (c)
212 	return c;
213     return krb5_db2_lib_init ();
214 }
215 
216 static krb5_error_code
217 hack_cleanup (void)
218 {
219     krb5int_mutex_free (krb5_db2_mutex);
220     krb5_db2_mutex = NULL;
221     return krb5_db2_lib_cleanup();
222 }
223 
224 
225 /*
226  *      Exposed API
227  */
228 
229 kdb_vftabl kdb_function_table = {
230   /* major version number 1 */		       1,
231   /* minor version number 0 */		       0,
232   /* Solaris Kerberos: iprop support */
233   /* iprop_supported, yes for db2 */	       1,
234   /* init_library */			       hack_init,
235   /* fini_library */			       hack_cleanup,
236   /* init_module */			       wrap_krb5_db2_open,
237   /* fini_module */			       wrap_krb5_db2_db_fini,
238   /* db_create */			       wrap_krb5_db2_create,
239   /* db_destroy */			       wrap_krb5_db2_destroy,
240   /* db_get_age */                             wrap_krb5_db2_db_get_age,
241   /* db_set_option */			       wrap_krb5_db2_db_set_option,
242   /* db_lock */				       wrap_krb5_db2_db_lock,
243   /* db_unlock */			       wrap_krb5_db2_db_unlock,
244   /* db_get_principal */		       wrap_krb5_db2_db_get_principal,
245   /* Solaris Kerberos: need a nolock for iprop */
246   /* db_get_principal_nolock */		       krb5_db2_db_get_principal,
247   /* db_free_principal */		       wrap_krb5_db2_db_free_principal,
248   /* db_put_principal */		       wrap_krb5_db2_db_put_principal,
249   /* db_delete_principal */		       wrap_krb5_db2_db_delete_principal,
250   /* db_iterate */			       wrap_krb5_db2_db_iterate,
251   /* db_create_policy */                       wrap_krb5_db2_create_policy,
252   /* db_get_policy */                          wrap_krb5_db2_get_policy,
253   /* db_put_policy */                          wrap_krb5_db2_put_policy,
254   /* db_iter_policy */                         wrap_krb5_db2_iter_policy,
255   /* db_delete_policy */                       wrap_krb5_db2_delete_policy,
256   /* db_free_policy */                         wrap_krb5_db2_free_policy,
257   /* db_supported_realms */		       NULL,
258   /* db_free_supported_realms */	       NULL,
259   /* errcode_2_string */                       NULL,
260   /* release_errcode_string */		       NULL,
261   /* db_alloc */                               wrap_krb5_db2_alloc,
262   /* db_free */                                wrap_krb5_db2_free,
263   /* set_master_key */			       wrap_krb5_db2_set_master_key_ext,
264   /* get_master_key */			       wrap_krb5_db2_db_get_mkey,
265   /* blah blah blah */			       0,0,0,0,0,0,
266   /* promote_db */			       wrap_krb5_db2_promote_db,
267 };
268