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
25 init_handle (int outputtype
)
30 handle
= xcalloc (1, sizeof (*handle
));
32 shishi_set_outputtype (handle
, outputtype
);
34 rc
= _shishi_crypto_init (handle
);
38 shishi_warn (handle
, "Cannot initialize crypto library");
42 rc
= _shishi_asn1_init (handle
);
46 shishi_warn (handle
, "%s", shishi_strerror (SHISHI_ASN1_ERROR
));
50 bindtextdomain (PACKAGE
, LOCALEDIR
);
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
;
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.
80 return init_handle (SHISHI_OUTPUTTYPE_STDERR
);
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.
96 return init_handle (SHISHI_OUTPUTTYPE_SYSLOG
);
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
113 shishi_done (Shishi
* handle
)
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
);
133 shishi_asn1_done (handle
, handle
->asn1
);
139 maybe_install_usercfg (Shishi
* handle
)
141 const char *usercfg
= shishi_cfg_default_userfile (handle
);
142 const char *userdir
= shishi_cfg_default_userdirectory (handle
);
149 fh
= fopen (usercfg
, "r");
156 rc
= stat (userdir
, &buf
);
157 if (rc
== -1 && errno
== ENOENT
)
159 rc
= mkdir (userdir
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
161 shishi_info (handle
, "mkdir %s: %s", userdir
, strerror (errno
));
164 shishi_info (handle
, "stat %s: %s", userdir
, strerror (errno
));
166 src
= fopen (SKELCFGFILE
, "r");
169 shishi_info (handle
, "open %s: %s", SKELCFGFILE
, strerror (errno
));
173 dst
= fopen (usercfg
, "w");
177 shishi_info (handle
, "open %s: %s", usercfg
, strerror (errno
));
181 while ((c
= getc (src
)) != EOF
)
187 shishi_info (handle
, "created `%s'", usercfg
);
191 init_read (Shishi
* handle
,
192 const char *tktsfile
,
193 const char *systemcfgfile
,
194 const char *usercfgfile
)
198 /* XXX Is this the correct place for this? */
199 maybe_install_usercfg (handle
);
202 tktsfile
= shishi_tkts_default_file (handle
);
205 systemcfgfile
= shishi_cfg_default_systemfile (handle
);
208 usercfgfile
= shishi_cfg_default_userfile (handle
);
211 rc
= shishi_tkts (handle
, &handle
->tkts
);
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
)
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
)
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
)
236 if (VERBOSENOICE (handle
))
237 shishi_cfg_print (handle
, stderr
);
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
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
)
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
),
332 if (rc
!= SHISHI_OK
&& rc
!= SHISHI_FOPEN_ERROR
)
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
)
358 if (!handle
|| !(*handle
= shishi_server ()))
359 return SHISHI_HANDLE_ERROR
;
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
)