summaryrefslogtreecommitdiff
path: root/lib/ZExpnRlm.c
blob: 6f904b650a7c2584cb8b31effe9198ee899a3b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <internal.h>
#include <sys/param.h>
#include <ctype.h>

char *
ZExpandRealm(char *realm)
{
        char *cp1, *cp2;
        static char expand[REALM_SZ];
#ifdef  HAVE_KRB5
	krb5_error_code result;
	char **list_realms;
	result = krb5_get_host_realm(Z_krb5_ctx, realm, &list_realms);
	if (result) {
		/* Error, just return upper-cased realm */
		cp2 = realm;
       		cp1 = expand;
        	while (*cp2) {
                	*cp1++ = toupper(*cp2++);
        	}
        	*cp1 = '\0';
		return expand;
	}
	strncpy(expand, list_realms[0], sizeof(expand));
	expand[sizeof(expand)-1] = '\0';
	result = krb5_free_host_realm(Z_krb5_ctx, list_realms);
	return expand;
#else
#ifndef HAVE_KRB4
        struct hostent *he;

        he = gethostbyname(realm);

        if (!he || !he->h_name)
                /* just use the raw realm */
                cp2 = realm;
        else
                cp2 = he->h_name;

        cp1 = expand;
        while (*cp2) {
                *cp1++ = toupper(*cp2++);
        }
        *cp1 = '\0';

        return(expand);
#else
	int retval;
	FILE *rlm_file;
	char krb_host[NS_MAXDNAME + 1];
        static char krb_realm[REALM_SZ+1];
	char linebuf[BUFSIZ];
	char scratch[64];

/* upcase what we got */
	cp2 = realm;
        cp1 = expand;
        while (*cp2) {
                *cp1++ = toupper(*cp2++);
        }
        *cp1 = '\0';

	if ((rlm_file = fopen("/etc/krb.conf", "r")) == (FILE *) 0) {
                return(expand);
        }
	
	if (fgets(linebuf, BUFSIZ, rlm_file) == NULL) {
	  /* error reading */
	  (void) fclose(rlm_file);
	  return(expand);
	}

	if (sscanf(linebuf, "%s", krb_realm) < 1) {
	  /* error reading */
	  (void) fclose(rlm_file);
	  return(expand);
	}

	if (!strncmp(krb_realm, expand, strlen(expand))) {
	  (void) fclose(rlm_file);
	  return(krb_realm);
	}

	while (1) {
	  /* run through the file, looking for admin host */
	  if (fgets(linebuf, BUFSIZ, rlm_file) == NULL) {
            (void) fclose(rlm_file);
            return(expand);
	  }

	  if (sscanf(linebuf, "%s %s admin %s", krb_realm, krb_host, scratch)
	      < 2)
            continue;
	  if (!strncmp(krb_realm, expand, strlen(expand))) {
	    (void) fclose(rlm_file);
	    return(krb_realm);
	  }
	}
#endif /* HAVE_KRB4 */
#endif
}