be47e688a6674d8b112c198df239885e075893d3
[openafs.git] / src / aklog / krb_util.c
1 /*
2  * This file replaces some of the routines in the Kerberos utilities.
3  * It is based on the Kerberos library modules:
4  *      send_to_kdc.c
5  *
6  * Copyright 1987, 1988, 1992 by the Massachusetts Institute of Technology.
7  *
8  * For copying and distribution information, please see the file
9  * <mit-copyright.h>.
10  */
11
12 #include <afsconfig.h>
13 #include <afs/stds.h>
14
15 #include <roken.h>
16
17 #include "aklog.h"
18 #include <krb5.h>
19
20 #ifndef MAX_HSTNM
21 #define MAX_HSTNM 100
22 #endif
23
24 #include <afs/cellconfig.h>
25
26 #include <ctype.h>
27
28 #define S_AD_SZ sizeof(struct sockaddr_in)
29
30 char *afs_realm_of_cell(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
31 {
32     static char krbrlm[REALM_SZ+1];
33         char **hrealms = 0;
34         krb5_error_code retval;
35
36     if (!cellconfig)
37         return 0;
38
39     if (fallback) {
40         char * p;
41         p = strchr(cellconfig->hostName[0], '.');
42         if (p++)
43             strcpy(krbrlm, p);
44         else
45             strcpy(krbrlm, cellconfig->name);
46         for (p=krbrlm; *p; p++) {
47             if (islower(*p))
48                 *p = toupper(*p);
49         }
50     } else {
51         if ((retval = krb5_get_host_realm(context,
52                                          cellconfig->hostName[0], &hrealms)))
53             return 0;
54         if(!hrealms[0]) return 0;
55         strcpy(krbrlm, hrealms[0]);
56
57         if (hrealms) krb5_free_host_realm(context, hrealms);
58     }
59     return krbrlm;
60 }