xref: /titanic_52/usr/src/lib/gss_mechs/mech_krb5/krb5/os/get_krbhst.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
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(krb5_context context, const krb5_data *realm, char ***hostlist)
63 {
64     char	**values, **cpp, *cp;
65     const char	*realm_kdc_names[4];
66     krb5_error_code	retval;
67     int	i, count;
68     char **rethosts;
69 
70     rethosts = 0;
71 
72     realm_kdc_names[0] = "realms";
73     realm_kdc_names[1] = realm->data;
74     realm_kdc_names[2] = "kdc";
75     realm_kdc_names[3] = 0;
76 
77     if (context->profile == 0)
78 	return KRB5_CONFIG_CANTOPEN;
79 
80     retval = profile_get_values(context->profile, realm_kdc_names, &values);
81     if (retval == PROF_NO_SECTION)
82 	return KRB5_REALM_UNKNOWN;
83     if (retval == PROF_NO_RELATION)
84 	return KRB5_CONFIG_BADFORMAT;
85     if (retval)
86 	return retval;
87 
88     /*
89      * Do cleanup over the list.  We allow for some extra field to be
90      * added to the kdc line later (maybe the port number)
91      */
92     for (cpp = values; *cpp; cpp++) {
93 	cp = strchr(*cpp, ' ');
94 	if (cp)
95 	    *cp = 0;
96 	cp = strchr(*cpp, '\t');
97 	if (cp)
98 	    *cp = 0;
99 	cp = strchr(*cpp, ':');
100 	if (cp)
101 	    *cp = 0;
102     }
103     count = cpp - values;
104     rethosts = malloc(sizeof(char *) * (count + 1));
105     if (!rethosts) {
106         retval = ENOMEM;
107         goto cleanup;
108     }
109     for (i = 0; i < count; i++) {
110 	unsigned int len = strlen (values[i]) + 1;
111         rethosts[i] = malloc(len);
112         if (!rethosts[i]) {
113             retval = ENOMEM;
114             goto cleanup;
115         }
116 	memcpy (rethosts[i], values[i], len);
117     }
118     rethosts[count] = 0;
119  cleanup:
120     if (retval && rethosts) {
121         for (cpp = rethosts; *cpp; cpp++)
122             free(*cpp);
123         free(rethosts);
124 	rethosts = 0;
125     }
126     profile_free_list(values);
127     *hostlist = rethosts;
128     return retval;
129 }
130