Add CBC-MAC.
[shishi.git] / lib / init.c
blobbf4f990c25665fdd6b83b37610f521f7bfa33332
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 #define INFOSTR "libshishi: info: "
25 #define WARNSTR "libshishi: warning: "
28 /**
29 * shishi:
31 * Initializes the Shishi library. If this function fails, it may print
32 * diagnostic errors to stderr.
34 * Return Value: Returns Shishi library handle, or %NULL on error.
35 **/
36 Shishi *
37 shishi (void)
39 Shishi *handle;
40 char *tmp;
42 bindtextdomain (PACKAGE, LOCALEDIR);
43 textdomain (PACKAGE);
45 handle = (Shishi *) malloc (sizeof (*handle));
46 if (!handle)
48 fprintf (stderr, "libshishi: error: %s\n",
49 shishi_strerror (SHISHI_MALLOC_ERROR));
50 return NULL;
52 memset ((void *) handle, 0, sizeof (*handle));
54 if (_shishi_cipher_init () != SHISHI_OK)
55 return NULL;
57 handle->asn1 = _shishi_asn1_read ();
58 if (handle->asn1 == NULL)
60 shishi_warn (handle, "%s", shishi_strerror (SHISHI_ASN1_ERROR));
61 return NULL;
64 handle->kdctimeout = 5;
65 handle->kdcretries = 3;
67 handle->nclientkdcetypes = 1;
68 handle->clientkdcetypes = malloc (sizeof (*handle->clientkdcetypes) *
69 handle->nclientkdcetypes);
70 if (handle->clientkdcetypes == NULL)
72 shishi_warn (handle, "%s", shishi_strerror (SHISHI_MALLOC_ERROR));
73 return NULL;
75 handle->clientkdcetypes[0] = SHISHI_AES256_CTS_HMAC_SHA1_96;
77 tmp = shishi_realm_default_guess ();
78 shishi_realm_default_set (handle, tmp);
79 free (tmp);
81 tmp = shishi_principal_default_guess ();
82 if (tmp != NULL)
84 shishi_principal_default_set (handle, tmp);
85 free (tmp);
88 return handle;
92 static void
93 _shishi_maybe_install_usercfg (Shishi * handle)
95 const char *usercfg = shishi_cfg_default_userfile (handle);
96 const char *userdir = shishi_cfg_default_userdirectory (handle);
97 struct stat buf;
98 FILE *fh;
99 FILE *src, *dst;
100 int rc;
101 int c;
103 fh = fopen (usercfg, "r");
104 if (fh)
106 fclose (fh);
107 return;
110 rc = stat (userdir, &buf);
111 if (rc == -1 && errno == ENOENT)
113 rc = mkdir (userdir, S_IRUSR|S_IWUSR|S_IXUSR);
114 if (rc != 0)
115 shishi_info (handle, "mkdir %s: %s", userdir, strerror (errno));
117 else if (rc != 0)
118 shishi_info (handle, "stat %s: %s", userdir, strerror (errno));
120 src = fopen (SKELCFGFILE, "r");
121 if (!src)
123 shishi_info (handle, "open %s: %s", SKELCFGFILE, strerror (errno));
124 return;
127 dst = fopen (usercfg, "w");
128 if (!dst)
130 fclose (src);
131 shishi_info (handle, "open %s: %s", usercfg, strerror (errno));
132 return;
135 while ((c = getc(src)) != EOF )
136 putc (c, dst);
138 fclose (dst);
139 fclose (src);
141 shishi_info (handle, "created `%s'", usercfg);
144 static int
145 _shishi_init_read (Shishi * handle,
146 const char *tktsfile,
147 const char *systemcfgfile, const char *usercfgfile)
149 int rc = SHISHI_OK;
151 _shishi_maybe_install_usercfg (handle);
153 if (!tktsfile)
154 tktsfile = shishi_tkts_default_file (handle);
156 if (!systemcfgfile)
157 systemcfgfile = shishi_cfg_default_systemfile (handle);
159 if (!usercfgfile)
160 usercfgfile = shishi_cfg_default_userfile (handle);
162 if (!handle->tkts)
163 rc = shishi_tkts (handle, &handle->tkts);
164 if (rc != SHISHI_OK)
165 return rc;
167 if (*tktsfile)
168 rc = shishi_tkts_from_file (handle->tkts, tktsfile);
169 if (rc == SHISHI_FOPEN_ERROR)
170 shishi_warn (handle, "%s: %s", tktsfile, strerror (errno));
171 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
172 return rc;
174 if (*systemcfgfile)
175 rc = shishi_cfg_from_file (handle, systemcfgfile);
176 if (rc == SHISHI_FOPEN_ERROR)
177 shishi_warn (handle, "%s: %s", systemcfgfile, strerror (errno));
178 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
179 return rc;
181 if (*usercfgfile)
182 rc = shishi_cfg_from_file (handle, usercfgfile);
183 if (rc == SHISHI_FOPEN_ERROR)
184 shishi_warn (handle, "%s: %s", usercfgfile, strerror (errno));
185 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
186 return rc;
188 if (VERBOSENOICE (handle))
189 shishi_cfg_print (handle, stderr);
191 return SHISHI_OK;
195 * shishi_init:
196 * @handle: pointer to handle to be created.
198 * Create a Shishi library handle and read the system configuration
199 * file, user configuration file and user tickets from the default
200 * paths. The paths to the system configuration file is decided at
201 * compile time, and is $sysconfdir/shishi.conf. The user
202 * configuration file is $HOME/.shishi/config, and the user ticket
203 * file is $HOME/.shishi/ticket. The handle is allocated regardless
204 * of return values, except for SHISHI_HANDLE_ERROR which indicates a
205 * problem allocating the handle. (The other error conditions comes
206 * from reading the files.)
208 * Return value: Returns SHISHI_OK iff successful.
211 shishi_init (Shishi ** handle)
213 if (!handle || !(*handle = shishi ()))
214 return SHISHI_HANDLE_ERROR;
216 return _shishi_init_read (*handle, shishi_tkts_default_file (*handle),
217 shishi_cfg_default_systemfile (*handle),
218 shishi_cfg_default_userfile (*handle));
222 * shishi_init_with_paths:
223 * @handle: pointer to handle to be created.
224 * @tktsfile: Filename of ticket file, or NULL.
225 * @systemcfgfile: Filename of system configuration, or NULL.
226 * @usercfgfile: Filename of user configuration, or NULL.
228 * Like shishi_init() but use explicit paths. Like shishi_init(), the
229 * handle is allocated regardless of return values, except for
230 * SHISHI_HANDLE_ERROR which indicates a problem allocating the
231 * handle. (The other error conditions comes from reading the files.)
233 * Return value: Returns SHISHI_OK iff successful.
236 shishi_init_with_paths (Shishi ** handle,
237 const char *tktsfile,
238 const char *systemcfgfile, const char *usercfgfile)
240 if (!handle || !(*handle = shishi ()))
241 return SHISHI_HANDLE_ERROR;
243 shishi_tkts_default_file_set (*handle, tktsfile);
245 return _shishi_init_read (*handle, tktsfile, systemcfgfile, usercfgfile);
249 * shishi_init_server:
250 * @handle: pointer to handle to be created.
252 * Like shishi_init() but only read the system configuration file.
253 * Like shishi_init(), the handle is allocated regardless of return
254 * values, except for SHISHI_HANDLE_ERROR which indicates a problem
255 * allocating the handle. (The other error conditions comes from
256 * reading the configuration file.)
258 * Return value: Returns SHISHI_OK iff successful.
261 shishi_init_server (Shishi ** handle)
263 int rc;
265 if (!handle || !(*handle = shishi ()))
266 return SHISHI_HANDLE_ERROR;
268 rc =
269 shishi_cfg_from_file (*handle, shishi_cfg_default_systemfile (*handle));
270 if (rc == SHISHI_FOPEN_ERROR)
271 shishi_warn (*handle, "%s: %s", shishi_cfg_default_systemfile (*handle),
272 strerror (errno));
273 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
274 return rc;
276 return SHISHI_OK;
280 * shishi_init_server_with_paths:
281 * @handle: pointer to handle to be created.
282 * @systemcfgfile: Filename of system configuration, or NULL.
284 * Like shishi_init() but only read the system configuration file from
285 * specified location. Like shishi_init(), the handle is allocated
286 * regardless of return values, except for SHISHI_HANDLE_ERROR which
287 * indicates a problem allocating the handle. (The other error
288 * conditions comes from reading the configuration file.)
290 * Return value: Returns SHISHI_OK iff successful.
293 shishi_init_server_with_paths (Shishi ** handle, const char *systemcfgfile)
295 int rc;
297 if (!handle || !(*handle = shishi ()))
298 return SHISHI_HANDLE_ERROR;
300 if (!systemcfgfile)
301 systemcfgfile = shishi_cfg_default_systemfile (*handle);
303 rc = shishi_cfg_from_file (*handle, systemcfgfile);
304 if (rc == SHISHI_FOPEN_ERROR)
305 shishi_warn (*handle, "%s: %s", systemcfgfile, strerror (errno));
306 if (rc != SHISHI_OK && rc != SHISHI_FOPEN_ERROR)
307 return rc;
309 return SHISHI_OK;
312 void
313 shishi_info (Shishi * handle, const char *fmt, ...)
315 va_list ap;
316 va_start (ap, fmt);
318 fprintf (stderr, INFOSTR);
319 vfprintf (stderr, fmt, ap);
320 fprintf (stderr, "\n");
322 va_end (ap);
325 void
326 shishi_warn (Shishi * handle, const char *fmt, ...)
328 va_list ap;
329 va_start (ap, fmt);
331 fprintf (stderr, WARNSTR);
332 vfprintf (stderr, fmt, ap);
333 fprintf (stderr, "\n");
335 va_end (ap);