xref: /minix3/crypto/external/bsd/heimdal/dist/lib/kadm5/init_s.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: init_s.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadm5_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc 
41ebfedea0SLionel Sambuc static kadm5_ret_t
kadm5_s_init_with_context(krb5_context context,const char * client_name,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)42ebfedea0SLionel Sambuc kadm5_s_init_with_context(krb5_context context,
43ebfedea0SLionel Sambuc 			  const char *client_name,
44ebfedea0SLionel Sambuc 			  const char *service_name,
45ebfedea0SLionel Sambuc 			  kadm5_config_params *realm_params,
46ebfedea0SLionel Sambuc 			  unsigned long struct_version,
47ebfedea0SLionel Sambuc 			  unsigned long api_version,
48ebfedea0SLionel Sambuc 			  void **server_handle)
49ebfedea0SLionel Sambuc {
50ebfedea0SLionel Sambuc     kadm5_ret_t ret;
51ebfedea0SLionel Sambuc     kadm5_server_context *ctx;
52ebfedea0SLionel Sambuc     ret = _kadm5_s_init_context(&ctx, realm_params, context);
53ebfedea0SLionel Sambuc     if(ret)
54ebfedea0SLionel Sambuc 	return ret;
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc     assert(ctx->config.dbname != NULL);
57ebfedea0SLionel Sambuc     assert(ctx->config.stash_file != NULL);
58ebfedea0SLionel Sambuc     assert(ctx->config.acl_file != NULL);
59ebfedea0SLionel Sambuc     assert(ctx->log_context.log_file != NULL);
60ebfedea0SLionel Sambuc #ifndef NO_UNIX_SOCKETS
61ebfedea0SLionel Sambuc     assert(ctx->log_context.socket_name.sun_path[0] != '\0');
62ebfedea0SLionel Sambuc #else
63ebfedea0SLionel Sambuc     assert(ctx->log_context.socket_info != NULL);
64ebfedea0SLionel Sambuc #endif
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc     ret = hdb_create(ctx->context, &ctx->db, ctx->config.dbname);
67ebfedea0SLionel Sambuc     if(ret)
68ebfedea0SLionel Sambuc 	return ret;
69ebfedea0SLionel Sambuc     ret = hdb_set_master_keyfile (ctx->context,
70ebfedea0SLionel Sambuc 				  ctx->db, ctx->config.stash_file);
71ebfedea0SLionel Sambuc     if(ret)
72ebfedea0SLionel Sambuc 	return ret;
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc     ctx->log_context.log_fd   = -1;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc #ifndef NO_UNIX_SOCKETS
77ebfedea0SLionel Sambuc     ctx->log_context.socket_fd = socket (AF_UNIX, SOCK_DGRAM, 0);
78ebfedea0SLionel Sambuc #else
79ebfedea0SLionel Sambuc     ctx->log_context.socket_fd = socket (ctx->log_context.socket_info->ai_family,
80ebfedea0SLionel Sambuc 					 ctx->log_context.socket_info->ai_socktype,
81ebfedea0SLionel Sambuc 					 ctx->log_context.socket_info->ai_protocol);
82ebfedea0SLionel Sambuc #endif
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc     ret = krb5_parse_name(ctx->context, client_name, &ctx->caller);
85ebfedea0SLionel Sambuc     if(ret)
86ebfedea0SLionel Sambuc 	return ret;
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc     ret = _kadm5_acl_init(ctx);
89ebfedea0SLionel Sambuc     if(ret)
90ebfedea0SLionel Sambuc 	return ret;
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc     *server_handle = ctx;
93ebfedea0SLionel Sambuc     return 0;
94ebfedea0SLionel Sambuc }
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_password_ctx(krb5_context context,const char * client_name,const char * password,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)97ebfedea0SLionel Sambuc kadm5_s_init_with_password_ctx(krb5_context context,
98ebfedea0SLionel Sambuc 			       const char *client_name,
99ebfedea0SLionel Sambuc 			       const char *password,
100ebfedea0SLionel Sambuc 			       const char *service_name,
101ebfedea0SLionel Sambuc 			       kadm5_config_params *realm_params,
102ebfedea0SLionel Sambuc 			       unsigned long struct_version,
103ebfedea0SLionel Sambuc 			       unsigned long api_version,
104ebfedea0SLionel Sambuc 			       void **server_handle)
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc     return kadm5_s_init_with_context(context,
107ebfedea0SLionel Sambuc 				     client_name,
108ebfedea0SLionel Sambuc 				     service_name,
109ebfedea0SLionel Sambuc 				     realm_params,
110ebfedea0SLionel Sambuc 				     struct_version,
111ebfedea0SLionel Sambuc 				     api_version,
112ebfedea0SLionel Sambuc 				     server_handle);
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc 
115ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_password(const char * client_name,const char * password,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)116ebfedea0SLionel Sambuc kadm5_s_init_with_password(const char *client_name,
117ebfedea0SLionel Sambuc 			   const char *password,
118ebfedea0SLionel Sambuc 			   const char *service_name,
119ebfedea0SLionel Sambuc 			   kadm5_config_params *realm_params,
120ebfedea0SLionel Sambuc 			   unsigned long struct_version,
121ebfedea0SLionel Sambuc 			   unsigned long api_version,
122ebfedea0SLionel Sambuc 			   void **server_handle)
123ebfedea0SLionel Sambuc {
124ebfedea0SLionel Sambuc     krb5_context context;
125ebfedea0SLionel Sambuc     kadm5_ret_t ret;
126ebfedea0SLionel Sambuc     kadm5_server_context *ctx;
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
129ebfedea0SLionel Sambuc     if (ret)
130ebfedea0SLionel Sambuc 	return ret;
131ebfedea0SLionel Sambuc     ret = kadm5_s_init_with_password_ctx(context,
132ebfedea0SLionel Sambuc 					 client_name,
133ebfedea0SLionel Sambuc 					 password,
134ebfedea0SLionel Sambuc 					 service_name,
135ebfedea0SLionel Sambuc 					 realm_params,
136ebfedea0SLionel Sambuc 					 struct_version,
137ebfedea0SLionel Sambuc 					 api_version,
138ebfedea0SLionel Sambuc 					 server_handle);
139ebfedea0SLionel Sambuc     if(ret){
140ebfedea0SLionel Sambuc 	krb5_free_context(context);
141ebfedea0SLionel Sambuc 	return ret;
142ebfedea0SLionel Sambuc     }
143ebfedea0SLionel Sambuc     ctx = *server_handle;
144ebfedea0SLionel Sambuc     ctx->my_context = 1;
145ebfedea0SLionel Sambuc     return 0;
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_skey_ctx(krb5_context context,const char * client_name,const char * keytab,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)149ebfedea0SLionel Sambuc kadm5_s_init_with_skey_ctx(krb5_context context,
150ebfedea0SLionel Sambuc 			   const char *client_name,
151ebfedea0SLionel Sambuc 			   const char *keytab,
152ebfedea0SLionel Sambuc 			   const char *service_name,
153ebfedea0SLionel Sambuc 			   kadm5_config_params *realm_params,
154ebfedea0SLionel Sambuc 			   unsigned long struct_version,
155ebfedea0SLionel Sambuc 			   unsigned long api_version,
156ebfedea0SLionel Sambuc 			   void **server_handle)
157ebfedea0SLionel Sambuc {
158ebfedea0SLionel Sambuc     return kadm5_s_init_with_context(context,
159ebfedea0SLionel Sambuc 				     client_name,
160ebfedea0SLionel Sambuc 				     service_name,
161ebfedea0SLionel Sambuc 				     realm_params,
162ebfedea0SLionel Sambuc 				     struct_version,
163ebfedea0SLionel Sambuc 				     api_version,
164ebfedea0SLionel Sambuc 				     server_handle);
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_skey(const char * client_name,const char * keytab,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)168ebfedea0SLionel Sambuc kadm5_s_init_with_skey(const char *client_name,
169ebfedea0SLionel Sambuc 		       const char *keytab,
170ebfedea0SLionel Sambuc 		       const char *service_name,
171ebfedea0SLionel Sambuc 		       kadm5_config_params *realm_params,
172ebfedea0SLionel Sambuc 		       unsigned long struct_version,
173ebfedea0SLionel Sambuc 		       unsigned long api_version,
174ebfedea0SLionel Sambuc 		       void **server_handle)
175ebfedea0SLionel Sambuc {
176ebfedea0SLionel Sambuc     krb5_context context;
177ebfedea0SLionel Sambuc     kadm5_ret_t ret;
178ebfedea0SLionel Sambuc     kadm5_server_context *ctx;
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
181ebfedea0SLionel Sambuc     if (ret)
182ebfedea0SLionel Sambuc 	return ret;
183ebfedea0SLionel Sambuc     ret = kadm5_s_init_with_skey_ctx(context,
184ebfedea0SLionel Sambuc 				     client_name,
185ebfedea0SLionel Sambuc 				     keytab,
186ebfedea0SLionel Sambuc 				     service_name,
187ebfedea0SLionel Sambuc 				     realm_params,
188ebfedea0SLionel Sambuc 				     struct_version,
189ebfedea0SLionel Sambuc 				     api_version,
190ebfedea0SLionel Sambuc 				     server_handle);
191ebfedea0SLionel Sambuc     if(ret){
192ebfedea0SLionel Sambuc 	krb5_free_context(context);
193ebfedea0SLionel Sambuc 	return ret;
194ebfedea0SLionel Sambuc     }
195ebfedea0SLionel Sambuc     ctx = *server_handle;
196ebfedea0SLionel Sambuc     ctx->my_context = 1;
197ebfedea0SLionel Sambuc     return 0;
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc 
200ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_creds_ctx(krb5_context context,const char * client_name,krb5_ccache ccache,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)201ebfedea0SLionel Sambuc kadm5_s_init_with_creds_ctx(krb5_context context,
202ebfedea0SLionel Sambuc 			    const char *client_name,
203ebfedea0SLionel Sambuc 			    krb5_ccache ccache,
204ebfedea0SLionel Sambuc 			    const char *service_name,
205ebfedea0SLionel Sambuc 			    kadm5_config_params *realm_params,
206ebfedea0SLionel Sambuc 			    unsigned long struct_version,
207ebfedea0SLionel Sambuc 			    unsigned long api_version,
208ebfedea0SLionel Sambuc 			    void **server_handle)
209ebfedea0SLionel Sambuc {
210ebfedea0SLionel Sambuc     return kadm5_s_init_with_context(context,
211ebfedea0SLionel Sambuc 				     client_name,
212ebfedea0SLionel Sambuc 				     service_name,
213ebfedea0SLionel Sambuc 				     realm_params,
214ebfedea0SLionel Sambuc 				     struct_version,
215ebfedea0SLionel Sambuc 				     api_version,
216ebfedea0SLionel Sambuc 				     server_handle);
217ebfedea0SLionel Sambuc }
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc kadm5_ret_t
kadm5_s_init_with_creds(const char * client_name,krb5_ccache ccache,const char * service_name,kadm5_config_params * realm_params,unsigned long struct_version,unsigned long api_version,void ** server_handle)220ebfedea0SLionel Sambuc kadm5_s_init_with_creds(const char *client_name,
221ebfedea0SLionel Sambuc 			krb5_ccache ccache,
222ebfedea0SLionel Sambuc 			const char *service_name,
223ebfedea0SLionel Sambuc 			kadm5_config_params *realm_params,
224ebfedea0SLionel Sambuc 			unsigned long struct_version,
225ebfedea0SLionel Sambuc 			unsigned long api_version,
226ebfedea0SLionel Sambuc 			void **server_handle)
227ebfedea0SLionel Sambuc {
228ebfedea0SLionel Sambuc     krb5_context context;
229ebfedea0SLionel Sambuc     kadm5_ret_t ret;
230ebfedea0SLionel Sambuc     kadm5_server_context *ctx;
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
233ebfedea0SLionel Sambuc     if (ret)
234ebfedea0SLionel Sambuc 	return ret;
235ebfedea0SLionel Sambuc     ret = kadm5_s_init_with_creds_ctx(context,
236ebfedea0SLionel Sambuc 				      client_name,
237ebfedea0SLionel Sambuc 				      ccache,
238ebfedea0SLionel Sambuc 				      service_name,
239ebfedea0SLionel Sambuc 				      realm_params,
240ebfedea0SLionel Sambuc 				      struct_version,
241ebfedea0SLionel Sambuc 				      api_version,
242ebfedea0SLionel Sambuc 				      server_handle);
243ebfedea0SLionel Sambuc     if(ret){
244ebfedea0SLionel Sambuc 	krb5_free_context(context);
245ebfedea0SLionel Sambuc 	return ret;
246ebfedea0SLionel Sambuc     }
247ebfedea0SLionel Sambuc     ctx = *server_handle;
248ebfedea0SLionel Sambuc     ctx->my_context = 1;
249ebfedea0SLionel Sambuc     return 0;
250ebfedea0SLionel Sambuc }
251