Fix _shishi_*_init().
[shishi.git] / lib / init.c
blob4f1adecb9c0529d25db5fdf988b6c0c57d4a0842
1 /* init.c initialization functions
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 static Shishi *
25 init_handle (int outputtype)
27 Shishi *handle;
28 int rc;
30 handle = xcalloc (1, sizeof (*handle));
32 shishi_set_outputtype (handle, outputtype);
34 rc = _shishi_crypto_init (handle);
35 if (rc != SHISHI_OK)
37 free (handle);
38 shishi_warn (handle, "Cannot initialize crypto library");
39 return NULL;
42 rc = _shishi_asn1_init (handle);
43 if (rc != SHISHI_OK)
45 free (handle);
46 shishi_warn (handle, "%s", shishi_strerror (SHISHI_ASN1_ERROR));
47 return NULL;
50 bindtextdomain (PACKAGE, LOCALEDIR);
51 textdomain (PACKAGE);
53 handle->kdctimeout = 5;
54 handle->kdcretries = 3;
56 handle->ticketlife = TICKETLIFE;
57 handle->renewlife = RENEWLIFE;
59 handle->nclientkdcetypes = 1;
60 handle->clientkdcetypes = xmalloc (sizeof (*handle->clientkdcetypes) *
61 handle->nclientkdcetypes);
62 handle->clientkdcetypes[0] = SHISHI_AES256_CTS_HMAC_SHA1_96;
64 return handle;
67 /**
68 * shishi:
70 * Initializes the Shishi library, and set up, using
71 * shishi_set_outputtype(), the library so that future warnings and
72 * informational messages are printed to stderr. If this function
73 * fails, it may print diagnostic errors to stderr.
75 * Return value: Returns Shishi library handle, or %NULL on error.
76 **/
77 Shishi *
78 shishi (void)
80 return init_handle (SHISHI_OUTPUTTYPE_STDERR);
83 /**
84 * shishi_server:
86 * Initializes the Shishi library, and set up, using
87 * shishi_set_outputtype(), the library so that future warnings and
88 * informational messages are printed to the syslog. If this function
89 * fails, it may print diagnostic errors to the syslog.
91 * Return value: Returns Shishi library handle, or %NULL on error.
92 **/
93 Shishi *
94 shishi_server (void)
96 return init_handle (SHISHI_OUTPUTTYPE_SYSLOG);
99 /**
100 * shishi_done:
101 * @handle: shishi handle as allocated by shishi_init().
103 * Deallocates the shishi library handle. The handle must not be used
104 * in any calls to shishi functions after this.
106 * If there is a default tkts, it is written to the default tkts file
107 * (call shishi_tkts_default_file_set() to change the default tkts
108 * file). If you do not wish to write the default tkts file, close the
109 * default tkts with shishi_tkts_done(handle, NULL) before calling
110 * this function.
112 void
113 shishi_done (Shishi * handle)
115 if (handle->tkts)
117 shishi_tkts_to_file (handle->tkts, shishi_tkts_default_file (handle));
118 shishi_tkts_done (&handle->tkts);
121 if (handle->default_realm)
122 free (handle->default_realm);
123 if (handle->usercfgfile)
124 free (handle->usercfgfile);
125 if (handle->tktsdefaultfile)
126 free (handle->tktsdefaultfile);
127 if (handle->hostkeysdefaultfile)
128 free (handle->hostkeysdefaultfile);
129 if (handle->clientkdcetypes)
130 free (handle->clientkdcetypes);
132 if (handle->asn1)
133 shishi_asn1_done (handle, handle->asn1);
135 free (handle);
138 static void
139 maybe_install_usercfg (Shishi * handle)
141 const char *usercfg = shishi_cfg_default_userfile (handle);
142 const char *userdir = shishi_cfg_default_userdirectory (handle);
143 struct stat buf;
144 FILE *fh;
145 FILE *src, *dst;
146 int rc;
147 int c;
149 fh = fopen (usercfg, "r");
150 if (fh)
152 fclose (fh);
153 return;
156 rc = stat (userdir, &buf);
157 if (rc == -1 && errno == ENOENT)
159 rc = mkdir (userdir, S_IRUSR | S_IWUSR | S_IXUSR);
160 if (rc != 0)
161 shishi_info (handle, "mkdir %s: %s", userdir, strerror (errno));
163 else if (rc != 0)
164 shishi_info (handle, "stat %s: %s", userdir, strerror (errno));
166 src = fopen (SKELCFGFILE, "r");
167 if (!src)
169 shishi_info (handle, "open %s: %s", SKELCFGFILE, strerror (errno));
170 return;
173 dst = fopen (usercfg, "w");
174 if (!dst)
176 fclose (src);
177 shishi_info (handle, "open %s: %s", usercfg, strerror (errno));
178 return;
181 while ((c = getc (src)) != EOF)
182 putc (c, dst);
184 fclose (dst);
185 fclose (src);
187 shishi_info (handle, "created `%s'", usercfg);
190 static int
191 init_read (Shishi * handle,
192 const char *tktsfile,
193 const char *systemcfgfile,
194 const char *usercfgfile)
196 int rc = SHISHI_OK;
198 /* XXX Is this the correct place for this? */
199 maybe_install_usercfg (handle);
201 if (!tktsfile)
202 tktsfile = shishi_tkts_default_file (handle);
204 if (!systemcfgfile)
205 systemcfgfile = shishi_cfg_default_systemfile (handle);
207 if (!usercfgfile)
208 usercfgfile = shishi_cfg_default_userfile (handle);
210 if (!handle->tkts)
211 rc = shishi_tkts (handle, &handle->tkts);
212 if (rc != SHISHI_OK)
213 return rc;
215 if (*tktsfile)
216 rc = shishi_tkts_from_file (handle->tkts, tktsfile);
217 if (rc == SHISHI_FOPEN_ERROR)
218 shishi_warn (handle, "%s: %s", tktsfile, strerror (errno));
219 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
220 return rc;
222 if (*systemcfgfile)
223 rc = shishi_cfg_from_file (handle, systemcfgfile);
224 if (rc == SHISHI_FOPEN_ERROR)
225 shishi_warn (handle, "%s: %s", systemcfgfile, strerror (errno));
226 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
227 return rc;
229 if (*usercfgfile)
230 rc = shishi_cfg_from_file (handle, usercfgfile);
231 if (rc == SHISHI_FOPEN_ERROR)
232 shishi_warn (handle, "%s: %s", usercfgfile, strerror (errno));
233 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
234 return rc;
236 if (VERBOSENOICE (handle))
237 shishi_cfg_print (handle, stderr);
239 return SHISHI_OK;
243 * shishi_init:
244 * @handle: pointer to handle to be created.
246 * Create a Shishi library handle, using shishi(), and read the system
247 * configuration file, user configuration file and user tickets from
248 * their default locations. The paths to the system configuration
249 * file is decided at compile time, and is $sysconfdir/shishi.conf.
250 * The user configuration file is $HOME/.shishi/config, and the user
251 * ticket file is $HOME/.shishi/ticket.
253 * The handle is allocated regardless of return values, except for
254 * SHISHI_HANDLE_ERROR which indicates a problem allocating the
255 * handle. (The other error conditions comes from reading the files.)
257 * Return value: Returns SHISHI_OK iff successful.
260 shishi_init (Shishi ** handle)
262 if (!handle || !(*handle = shishi ()))
263 return SHISHI_HANDLE_ERROR;
265 return init_read (*handle, shishi_tkts_default_file (*handle),
266 shishi_cfg_default_systemfile (*handle),
267 shishi_cfg_default_userfile (*handle));
271 * shishi_init_with_paths:
272 * @handle: pointer to handle to be created.
273 * @tktsfile: Filename of ticket file, or NULL.
274 * @systemcfgfile: Filename of system configuration, or NULL.
275 * @usercfgfile: Filename of user configuration, or NULL.
277 * Create a Shishi library handle, using shishi(), and read the system
278 * configuration file, user configuration file, and user tickets from
279 * the specified locations. If any of @usercfgfile or @systemcfgfile
280 * is NULL, the file is read from its default location, which for the
281 * system configuration file is decided at compile time, and is
282 * $sysconfdir/shishi.conf, and for the user configuration file is
283 * $HOME/.shishi/config. If the ticket file is NULL, a ticket file is
284 * not read at all.
286 * The handle is allocated regardless of return values, except for
287 * SHISHI_HANDLE_ERROR which indicates a problem allocating the
288 * handle. (The other error conditions comes from reading the files.)
290 * Return value: Returns SHISHI_OK iff successful.
293 shishi_init_with_paths (Shishi ** handle,
294 const char *tktsfile,
295 const char *systemcfgfile, const char *usercfgfile)
297 if (!handle || !(*handle = shishi ()))
298 return SHISHI_HANDLE_ERROR;
300 shishi_tkts_default_file_set (*handle, tktsfile);
302 return init_read (*handle, tktsfile, systemcfgfile, usercfgfile);
306 * shishi_init_server:
307 * @handle: pointer to handle to be created.
309 * Create a Shishi library handle, using shishi_server(), and read the
310 * system configuration file. The paths to the system configuration
311 * file is decided at compile time, and is $sysconfdir/shishi.conf.
313 * The handle is allocated regardless of return values, except for
314 * SHISHI_HANDLE_ERROR which indicates a problem allocating the
315 * handle. (The other error conditions comes from reading the file.)
317 * Return value: Returns SHISHI_OK iff successful.
320 shishi_init_server (Shishi ** handle)
322 int rc;
324 if (!handle || !(*handle = shishi_server ()))
325 return SHISHI_HANDLE_ERROR;
327 rc = shishi_cfg_from_file (*handle,
328 shishi_cfg_default_systemfile (*handle));
329 if (rc == SHISHI_FOPEN_ERROR)
330 shishi_warn (*handle, "%s: %s", shishi_cfg_default_systemfile (*handle),
331 strerror (errno));
332 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
333 return rc;
335 return SHISHI_OK;
339 * shishi_init_server_with_paths:
340 * @handle: pointer to handle to be created.
341 * @systemcfgfile: Filename of system configuration, or NULL.
343 * Create a Shishi library handle, using shishi_server(), and read the
344 * system configuration file from specified location. The paths to
345 * the system configuration file is decided at compile time, and is
346 * $sysconfdir/shishi.conf. The handle is allocated regardless of
347 * return values, except for SHISHI_HANDLE_ERROR which indicates a
348 * problem allocating the handle. (The other error conditions comes
349 * from reading the file.)
351 * Return value: Returns SHISHI_OK iff successful.
354 shishi_init_server_with_paths (Shishi ** handle, const char *systemcfgfile)
356 int rc;
358 if (!handle || !(*handle = shishi_server ()))
359 return SHISHI_HANDLE_ERROR;
361 if (!systemcfgfile)
362 systemcfgfile = shishi_cfg_default_systemfile (*handle);
364 rc = shishi_cfg_from_file (*handle, systemcfgfile);
365 if (rc == SHISHI_FOPEN_ERROR)
366 shishi_warn (*handle, "%s: %s", systemcfgfile, strerror (errno));
367 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
368 return rc;
370 return SHISHI_OK;