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