17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * lib/krb5/os/get_krbhst.c
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
67c478bd9Sstevel@tonic-gate  * All Rights Reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
97c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
107c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
117c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
147c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
157c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
167c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
177c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
187c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
197c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
207c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
217c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
227c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
237c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
247c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
257c478bd9Sstevel@tonic-gate  * or implied warranty.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * krb5_get_krbhst() function.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <k5-int.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  Figures out the Kerberos server names for the given realm, filling in a
377c478bd9Sstevel@tonic-gate  pointer to an argv[] style list of names, terminated with a null pointer.
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate  If the realm is unknown, the filled-in pointer is set to NULL.
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate  The pointer array and strings pointed to are all in allocated storage,
427c478bd9Sstevel@tonic-gate  and should be freed by the caller when finished.
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate  returns system errors
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Implementation:  the server names for given realms are stored in a
497c478bd9Sstevel@tonic-gate  * configuration file,
507c478bd9Sstevel@tonic-gate  * named by krb5_config_file;  the first token (on the first line) in
517c478bd9Sstevel@tonic-gate  * this file is taken as the default local realm name.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Each succeeding line has a realm name as the first token, and a server name
547c478bd9Sstevel@tonic-gate  * as a second token.  Additional tokens may be present on the line, but
557c478bd9Sstevel@tonic-gate  * are ignored by this function.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * All lines which begin with the desired realm name will have the
587c478bd9Sstevel@tonic-gate  * hostname added to the list returned.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate krb5_error_code
62*505d05c7Sgtb krb5_get_krbhst(krb5_context context, const krb5_data *realm, char ***hostlist)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate     char	**values, **cpp, *cp;
657c478bd9Sstevel@tonic-gate     const char	*realm_kdc_names[4];
667c478bd9Sstevel@tonic-gate     krb5_error_code	retval;
677c478bd9Sstevel@tonic-gate     int	i, count;
687c478bd9Sstevel@tonic-gate     char **rethosts;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate     rethosts = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     realm_kdc_names[0] = "realms";
737c478bd9Sstevel@tonic-gate     realm_kdc_names[1] = realm->data;
747c478bd9Sstevel@tonic-gate     realm_kdc_names[2] = "kdc";
757c478bd9Sstevel@tonic-gate     realm_kdc_names[3] = 0;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate     if (context->profile == 0)
787c478bd9Sstevel@tonic-gate 	return KRB5_CONFIG_CANTOPEN;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate     retval = profile_get_values(context->profile, realm_kdc_names, &values);
817c478bd9Sstevel@tonic-gate     if (retval == PROF_NO_SECTION)
827c478bd9Sstevel@tonic-gate 	return KRB5_REALM_UNKNOWN;
837c478bd9Sstevel@tonic-gate     if (retval == PROF_NO_RELATION)
847c478bd9Sstevel@tonic-gate 	return KRB5_CONFIG_BADFORMAT;
857c478bd9Sstevel@tonic-gate     if (retval)
867c478bd9Sstevel@tonic-gate 	return retval;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate     /*
897c478bd9Sstevel@tonic-gate      * Do cleanup over the list.  We allow for some extra field to be
907c478bd9Sstevel@tonic-gate      * added to the kdc line later (maybe the port number)
917c478bd9Sstevel@tonic-gate      */
927c478bd9Sstevel@tonic-gate     for (cpp = values; *cpp; cpp++) {
937c478bd9Sstevel@tonic-gate 	cp = strchr(*cpp, ' ');
947c478bd9Sstevel@tonic-gate 	if (cp)
957c478bd9Sstevel@tonic-gate 	    *cp = 0;
967c478bd9Sstevel@tonic-gate 	cp = strchr(*cpp, '\t');
977c478bd9Sstevel@tonic-gate 	if (cp)
987c478bd9Sstevel@tonic-gate 	    *cp = 0;
997c478bd9Sstevel@tonic-gate 	cp = strchr(*cpp, ':');
1007c478bd9Sstevel@tonic-gate 	if (cp)
1017c478bd9Sstevel@tonic-gate 	    *cp = 0;
1027c478bd9Sstevel@tonic-gate     }
1037c478bd9Sstevel@tonic-gate     count = cpp - values;
1047c478bd9Sstevel@tonic-gate     rethosts = malloc(sizeof(char *) * (count + 1));
1057c478bd9Sstevel@tonic-gate     if (!rethosts) {
1067c478bd9Sstevel@tonic-gate         retval = ENOMEM;
1077c478bd9Sstevel@tonic-gate         goto cleanup;
1087c478bd9Sstevel@tonic-gate     }
1097c478bd9Sstevel@tonic-gate     for (i = 0; i < count; i++) {
110*505d05c7Sgtb 	unsigned int len = strlen (values[i]) + 1;
1117c478bd9Sstevel@tonic-gate         rethosts[i] = malloc(len);
1127c478bd9Sstevel@tonic-gate         if (!rethosts[i]) {
1137c478bd9Sstevel@tonic-gate             retval = ENOMEM;
1147c478bd9Sstevel@tonic-gate             goto cleanup;
1157c478bd9Sstevel@tonic-gate         }
1167c478bd9Sstevel@tonic-gate 	memcpy (rethosts[i], values[i], len);
1177c478bd9Sstevel@tonic-gate     }
1187c478bd9Sstevel@tonic-gate     rethosts[count] = 0;
1197c478bd9Sstevel@tonic-gate  cleanup:
1207c478bd9Sstevel@tonic-gate     if (retval && rethosts) {
1217c478bd9Sstevel@tonic-gate         for (cpp = rethosts; *cpp; cpp++)
1227c478bd9Sstevel@tonic-gate             free(*cpp);
1237c478bd9Sstevel@tonic-gate         free(rethosts);
1247c478bd9Sstevel@tonic-gate 	rethosts = 0;
1257c478bd9Sstevel@tonic-gate     }
1267c478bd9Sstevel@tonic-gate     profile_free_list(values);
1277c478bd9Sstevel@tonic-gate     *hostlist = rethosts;
1287c478bd9Sstevel@tonic-gate     return retval;
1297c478bd9Sstevel@tonic-gate }
130