aklog-tidyup-20080401
[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 RCSID
14     ("$Header$");
15
16 #if 0
17 #include <kerberosIV/mit-copyright.h>
18 #endif
19 #include <afs/stds.h>
20 #include "aklog.h"
21 #include <krb5.h>
22
23 #ifndef MAX_HSTNM
24 #define MAX_HSTNM 100
25 #endif
26
27 #include <afs/cellconfig.h>
28
29 #include <string.h>
30 #include <ctype.h>
31
32 #define S_AD_SZ sizeof(struct sockaddr_in)
33
34 char *afs_realm_of_cell(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
35 {
36     static char krbrlm[REALM_SZ+1];
37         char **hrealms = 0;
38         krb5_error_code retval;
39
40     if (!cellconfig)
41         return 0;
42
43     if (fallback) {
44         char * p;
45         p = strchr(cellconfig->hostName[0], '.');
46         if (p++)
47             strcpy(krbrlm, p);
48         else
49             strcpy(krbrlm, cellconfig->name);
50         for (p=krbrlm; *p; p++) {
51             if (islower(*p)) 
52                 *p = toupper(*p);
53         }
54     } else {
55         if ((retval = krb5_get_host_realm(context,
56                                          cellconfig->hostName[0], &hrealms)))
57             return 0; 
58         if(!hrealms[0]) return 0;
59         strcpy(krbrlm, hrealms[0]);
60
61         if (hrealms) krb5_free_host_realm(context, hrealms);
62     }
63     return krbrlm;
64 }