Avoid <krb5/...> includes
[krb5.git] / src / lib / krb5 / os / def_realm.c
1 /*
2  * lib/krb5/os/def_realm.c
3  *
4  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * krb5_get_default_realm() function.
25  */
26
27 #include "k5-int.h"
28 #include <stdio.h>
29
30 /*
31  * Retrieves the default realm to be used if no user-specified realm is
32  *  available.  [e.g. to interpret a user-typed principal name with the
33  *  realm omitted for convenience]
34  * 
35  *  returns system errors, NOT_ENOUGH_SPACE
36 */
37
38 /*
39  * Implementation:  the default realm is stored in a configuration file,
40  * named by krb5_config_file;  the first token in this file is taken as
41  * the default local realm name.
42  */
43
44 extern char *krb5_config_file;          /* extern so can be set at
45                                            load/runtime */
46
47 /*
48  * In case the program wants to override this.
49  */
50 extern char *krb5_override_default_realm;
51
52 char *krb5_override_default_realm = 0;
53
54 krb5_error_code
55 krb5_get_default_realm(context, lrealm)
56     krb5_context context;
57     char **lrealm;
58 {
59     FILE *config_file;
60     char realmbuf[BUFSIZ];
61     static char *saved_realm = 0;
62     char *realm;
63     char *cp;
64
65     if (krb5_override_default_realm)
66             realm = krb5_override_default_realm;
67     else if (saved_realm)
68             realm = saved_realm;
69     else {
70             if (!(config_file = fopen(krb5_config_file, "r")))
71                     /* can't open */
72                     return KRB5_CONFIG_CANTOPEN;
73
74             if (fgets(realmbuf, sizeof(realmbuf), config_file) == NULL) {
75                     fclose(config_file);
76                     return(KRB5_CONFIG_BADFORMAT);
77             }
78             fclose(config_file);
79             
80             realmbuf[BUFSIZ-1] = '0';
81             cp = strchr(realmbuf, '\n');
82             if (cp)
83                     *cp = '\0';
84             cp = strchr(realmbuf, ' ');
85             if (cp)
86                     *cp = '\0';
87
88             saved_realm = malloc(strlen (realmbuf) + 1);
89             if (!saved_realm)
90                     return ENOMEM;
91
92             strcpy(saved_realm, realmbuf);
93
94             realm = saved_realm;
95     }
96     
97     if (!(*lrealm = cp = malloc((unsigned int) strlen(realm) + 1)))
98             return ENOMEM;
99     strcpy(cp, realm);
100     return(0);
101 }