1 /*
2  * lib/krb5/os/get_krbhst.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.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  *
27  * krb5_get_krbhst() function.
28  */
29 
30 #include <k5-int.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 
34 /*
35  Figures out the Kerberos server names for the given realm, filling in a
36  pointer to an argv[] style list of names, terminated with a null pointer.
37 
38  If the realm is unknown, the filled-in pointer is set to NULL.
39 
40  The pointer array and strings pointed to are all in allocated storage,
41  and should be freed by the caller when finished.
42 
43  returns system errors
44 */
45 
46 /*
47  * Implementation:  the server names for given realms are stored in a
48  * configuration file,
49  * named by krb5_config_file;  the first token (on the first line) in
50  * this file is taken as the default local realm name.
51  *
52  * Each succeeding line has a realm name as the first token, and a server name
53  * as a second token.  Additional tokens may be present on the line, but
54  * are ignored by this function.
55  *
56  * All lines which begin with the desired realm name will have the
57  * hostname added to the list returned.
58  */
59 
60 krb5_error_code
krb5_get_krbhst(krb5_context context,const krb5_data * realm,char *** hostlist)61 krb5_get_krbhst(krb5_context context, const krb5_data *realm, char ***hostlist)
62 {
63     char	**values, **cpp, *cp;
64     const char	*realm_kdc_names[4];
65     krb5_error_code	retval;
66     int	i, count;
67     char **rethosts;
68 
69     rethosts = 0;
70 
71     realm_kdc_names[0] = "realms";
72     realm_kdc_names[1] = realm->data;
73     realm_kdc_names[2] = "kdc";
74     realm_kdc_names[3] = 0;
75 
76     if (context->profile == 0)
77 	return KRB5_CONFIG_CANTOPEN;
78 
79     retval = profile_get_values(context->profile, realm_kdc_names, &values);
80     if (retval == PROF_NO_SECTION)
81 	return KRB5_REALM_UNKNOWN;
82     if (retval == PROF_NO_RELATION)
83 	return KRB5_CONFIG_BADFORMAT;
84     if (retval)
85 	return retval;
86 
87     /*
88      * Do cleanup over the list.  We allow for some extra field to be
89      * added to the kdc line later (maybe the port number)
90      */
91     for (cpp = values; *cpp; cpp++) {
92 	cp = strchr(*cpp, ' ');
93 	if (cp)
94 	    *cp = 0;
95 	cp = strchr(*cpp, '\t');
96 	if (cp)
97 	    *cp = 0;
98 	cp = strchr(*cpp, ':');
99 	if (cp)
100 	    *cp = 0;
101     }
102     count = cpp - values;
103     rethosts = malloc(sizeof(char *) * (count + 1));
104     if (!rethosts) {
105         retval = ENOMEM;
106         goto cleanup;
107     }
108     for (i = 0; i < count; i++) {
109 	unsigned int len = strlen (values[i]) + 1;
110         rethosts[i] = malloc(len);
111         if (!rethosts[i]) {
112             retval = ENOMEM;
113             goto cleanup;
114         }
115 	memcpy (rethosts[i], values[i], len);
116     }
117     rethosts[count] = 0;
118  cleanup:
119     if (retval && rethosts) {
120         for (cpp = rethosts; *cpp; cpp++)
121             free(*cpp);
122         free(rethosts);
123 	rethosts = 0;
124     }
125     profile_free_list(values);
126     *hostlist = rethosts;
127     return retval;
128 }
129