1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * lib/krb5/os/get_krbhst.c
4  *
5  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
8  * Export of this software from the United States of America may
9  *   require a specific license from the United States Government.
10  *   It is the responsibility of any person or organization contemplating
11  *   export to obtain such a license before exporting.
12  *
13  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14  * distribute this software and its documentation for any purpose and
15  * without fee is hereby granted, provided that the above copyright
16  * notice appear in all copies and that both that copyright notice and
17  * this permission notice appear in supporting documentation, and that
18  * the name of M.I.T. not be used in advertising or publicity pertaining
19  * to distribution of the software without specific, written prior
20  * permission.  Furthermore if you modify this software you must label
21  * your software as modified software and not distribute it in such a
22  * fashion that it might be confused with the original M.I.T. software.
23  * M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  *
27  *
28  * krb5_get_krbhst() function.
29  */
30 
31 #include <k5-int.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 
35 /*
36  Figures out the Kerberos server names for the given realm, filling in a
37  pointer to an argv[] style list of names, terminated with a null pointer.
38 
39  If the realm is unknown, the filled-in pointer is set to NULL.
40 
41  The pointer array and strings pointed to are all in allocated storage,
42  and should be freed by the caller when finished.
43 
44  returns system errors
45 */
46 
47 /*
48  * Implementation:  the server names for given realms are stored in a
49  * configuration file,
50  * named by krb5_config_file;  the first token (on the first line) in
51  * this file is taken as the default local realm name.
52  *
53  * Each succeeding line has a realm name as the first token, and a server name
54  * as a second token.  Additional tokens may be present on the line, but
55  * are ignored by this function.
56  *
57  * All lines which begin with the desired realm name will have the
58  * hostname added to the list returned.
59  */
60 
61 krb5_error_code
62 krb5_get_krbhst(context, realm, hostlist)
63     krb5_context context;
64     const krb5_data *realm;
65     char ***hostlist;
66 {
67     char	**values, **cpp, *cp;
68     const char	*realm_kdc_names[4];
69     krb5_error_code	retval;
70     int	i, count;
71     char **rethosts;
72 
73     rethosts = 0;
74 
75     realm_kdc_names[0] = "realms";
76     realm_kdc_names[1] = realm->data;
77     realm_kdc_names[2] = "kdc";
78     realm_kdc_names[3] = 0;
79 
80     if (context->profile == 0)
81 	return KRB5_CONFIG_CANTOPEN;
82 
83     retval = profile_get_values(context->profile, realm_kdc_names, &values);
84     if (retval == PROF_NO_SECTION)
85 	return KRB5_REALM_UNKNOWN;
86     if (retval == PROF_NO_RELATION)
87 	return KRB5_CONFIG_BADFORMAT;
88     if (retval)
89 	return retval;
90 
91     /*
92      * Do cleanup over the list.  We allow for some extra field to be
93      * added to the kdc line later (maybe the port number)
94      */
95     for (cpp = values; *cpp; cpp++) {
96 	cp = strchr(*cpp, ' ');
97 	if (cp)
98 	    *cp = 0;
99 	cp = strchr(*cpp, '\t');
100 	if (cp)
101 	    *cp = 0;
102 	cp = strchr(*cpp, ':');
103 	if (cp)
104 	    *cp = 0;
105     }
106     count = cpp - values;
107     rethosts = malloc(sizeof(char *) * (count + 1));
108     if (!rethosts) {
109         retval = ENOMEM;
110         goto cleanup;
111     }
112     for (i = 0; i < count; i++) {
113 	int len = strlen (values[i]) + 1;
114         rethosts[i] = malloc(len);
115         if (!rethosts[i]) {
116             retval = ENOMEM;
117             goto cleanup;
118         }
119 	memcpy (rethosts[i], values[i], len);
120     }
121     rethosts[count] = 0;
122  cleanup:
123     if (retval && rethosts) {
124         for (cpp = rethosts; *cpp; cpp++)
125             free(*cpp);
126         free(rethosts);
127 	rethosts = 0;
128     }
129     profile_free_list(values);
130     *hostlist = rethosts;
131     return retval;
132 }
133