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
24 #define INFOSTR "libshishi: info: "
25 #define WARNSTR "libshishi: warning: "
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.
42 bindtextdomain (PACKAGE
, LOCALEDIR
);
45 handle
= (Shishi
*) malloc (sizeof (*handle
));
48 fprintf (stderr
, "libshishi: error: %s\n",
49 shishi_strerror (SHISHI_MALLOC_ERROR
));
52 memset ((void *) handle
, 0, sizeof (*handle
));
54 if (_shishi_cipher_init () != SHISHI_OK
)
57 handle
->asn1
= _shishi_asn1_read ();
58 if (handle
->asn1
== NULL
)
60 shishi_warn (handle
, "%s", shishi_strerror (SHISHI_ASN1_ERROR
));
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
));
75 handle
->clientkdcetypes
[0] = SHISHI_AES256_CTS_HMAC_SHA1_96
;
77 tmp
= shishi_realm_default_guess ();
78 shishi_realm_default_set (handle
, tmp
);
81 tmp
= shishi_principal_default_guess ();
84 shishi_principal_default_set (handle
, tmp
);
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
);
103 fh
= fopen (usercfg
, "r");
110 rc
= stat (userdir
, &buf
);
111 if (rc
== -1 && errno
== ENOENT
)
113 rc
= mkdir (userdir
, S_IRUSR
|S_IWUSR
|S_IXUSR
);
115 shishi_info (handle
, "mkdir %s: %s", userdir
, strerror (errno
));
118 shishi_info (handle
, "stat %s: %s", userdir
, strerror (errno
));
120 src
= fopen (SKELCFGFILE
, "r");
123 shishi_info (handle
, "open %s: %s", SKELCFGFILE
, strerror (errno
));
127 dst
= fopen (usercfg
, "w");
131 shishi_info (handle
, "open %s: %s", usercfg
, strerror (errno
));
135 while ((c
= getc(src
)) != EOF
)
141 shishi_info (handle
, "created `%s'", usercfg
);
145 _shishi_init_read (Shishi
* handle
,
146 const char *tktsfile
,
147 const char *systemcfgfile
, const char *usercfgfile
)
151 _shishi_maybe_install_usercfg (handle
);
154 tktsfile
= shishi_tkts_default_file (handle
);
157 systemcfgfile
= shishi_cfg_default_systemfile (handle
);
160 usercfgfile
= shishi_cfg_default_userfile (handle
);
163 rc
= shishi_tkts (handle
, &handle
->tkts
);
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
)
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
)
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
)
188 if (VERBOSENOICE (handle
))
189 shishi_cfg_print (handle
, stderr
);
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
)
265 if (!handle
|| !(*handle
= shishi ()))
266 return SHISHI_HANDLE_ERROR
;
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
),
273 if (rc
!= SHISHI_OK
&& rc
!= SHISHI_FOPEN_ERROR
)
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
)
297 if (!handle
|| !(*handle
= shishi ()))
298 return SHISHI_HANDLE_ERROR
;
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
)
313 shishi_info (Shishi
* handle
, const char *fmt
, ...)
318 fprintf (stderr
, INFOSTR
);
319 vfprintf (stderr
, fmt
, ap
);
320 fprintf (stderr
, "\n");
326 shishi_warn (Shishi
* handle
, const char *fmt
, ...)
331 fprintf (stderr
, WARNSTR
);
332 vfprintf (stderr
, fmt
, ap
);
333 fprintf (stderr
, "\n");