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