1/*
2 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6
7/*
8 * lib/krb5/os/ktdefname.c
9 *
10 * Copyright 1990 by the Massachusetts Institute of Technology.
11 * All Rights Reserved.
12 *
13 * Export of this software from the United States of America may
14 *   require a specific license from the United States Government.
15 *   It is the responsibility of any person or organization contemplating
16 *   export to obtain such a license before exporting.
17 *
18 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19 * distribute this software and its documentation for any purpose and
20 * without fee is hereby granted, provided that the above copyright
21 * notice appear in all copies and that both that copyright notice and
22 * this permission notice appear in supporting documentation, and that
23 * the name of M.I.T. not be used in advertising or publicity pertaining
24 * to distribution of the software without specific, written prior
25 * permission.  Furthermore if you modify this software you must label
26 * your software as modified software and not distribute it in such a
27 * fashion that it might be confused with the original M.I.T. software.
28 * M.I.T. makes no representations about the suitability of
29 * this software for any purpose.  It is provided "as is" without express
30 * or implied warranty.
31 *
32 *
33 * Return default keytab file name.
34 */
35
36#define NEED_WINDOWS
37
38#include "k5-int.h"
39
40extern char *krb5_defkeyname;
41
42/* this is a an exceedinly gross thing. */
43char *krb5_overridekeyname = NULL;
44
45krb5_error_code KRB5_CALLCONV
46krb5_kt_default_name(krb5_context context, char *name, int namesize)
47{
48    char *cp = 0;
49    char *retval;
50
51    if (krb5_overridekeyname) {
52	if ((size_t) namesize < (strlen(krb5_overridekeyname)+1))
53	    return KRB5_CONFIG_NOTENUFSPACE;
54	/* Solaris Kerberos */
55	strncpy(name, krb5_overridekeyname, namesize);
56    } else if ((context->profile_secure == FALSE) &&
57	(cp = getenv("KRB5_KTNAME"))) {
58	if ((size_t) namesize < (strlen(cp)+1))
59	    return KRB5_CONFIG_NOTENUFSPACE;
60	/* Solaris Kerberos */
61	strncpy(name, cp, namesize);
62    } else if ((profile_get_string(context->profile,
63				   "libdefaults",
64				   "default_keytab_name", NULL,
65				   NULL, &retval) == 0) &&
66	       retval) {
67	if ((size_t) namesize < (strlen(retval)+1))
68	    return KRB5_CONFIG_NOTENUFSPACE;
69	/* Solaris Kerberos */
70	strncpy(name, retval, namesize);
71	profile_release_string(retval);
72    } else {
73#if defined(_WIN32)
74	{
75	    char    defname[160];
76	    int     len;
77
78	    len= GetWindowsDirectory( defname, sizeof(defname)-2 );
79	    defname[len]= '\0';
80	    if ( (len + strlen(krb5_defkeyname) + 1) > namesize )
81		return KRB5_CONFIG_NOTENUFSPACE;
82	    sprintf(name, krb5_defkeyname, defname);
83	}
84#else
85	if ((size_t) namesize < (strlen(krb5_defkeyname)+1))
86	    return KRB5_CONFIG_NOTENUFSPACE;
87	/* Solaris Kerberos */
88	strncpy(name, krb5_defkeyname, namesize);
89#endif
90    }
91    return 0;
92}
93
94