]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZExpnRlm.c
fix last use of MAXPATHLEN, make the hurd people happy
[1ts-debian.git] / zephyr / lib / ZExpnRlm.c
1 #include <internal.h>
2 #include <sys/param.h>
3 #include <ctype.h>
4
5 char *
6 ZExpandRealm(realm)
7 char *realm;
8 {
9         char *cp1, *cp2;
10         static char expand[REALM_SZ];
11 #ifdef  HAVE_KRB5
12         krb5_error_code result;
13         char **list_realms;
14         result = krb5_get_host_realm(Z_krb5_ctx, realm, &list_realms);
15         if (result) {
16                 /* Error, just return upper-cased realm */
17                 cp2 = realm;
18                 cp1 = expand;
19                 while (*cp2) {
20                         *cp1++ = toupper(*cp2++);
21                 }
22                 *cp1 = '\0';
23                 return expand;
24         }
25         strncpy(expand, list_realms[0], sizeof(expand));
26         expand[sizeof(expand)-1] = '\0';
27         result = krb5_free_host_realm(Z_krb5_ctx, list_realms);
28         return expand;
29 #else
30 #ifndef HAVE_KRB4
31         struct hostent *he;
32
33         he = gethostbyname(realm);
34
35         if (!he || !he->h_name)
36                 /* just use the raw realm */
37                 cp2 = realm;
38         else
39                 cp2 = he->h_name;
40
41         cp1 = expand;
42         while (*cp2) {
43                 *cp1++ = toupper(*cp2++);
44         }
45         *cp1 = '\0';
46
47         return(expand);
48 #else
49         int retval;
50         FILE *rlm_file;
51         char krb_host[NS_MAXDNAME + 1];
52         static char krb_realm[REALM_SZ+1];
53         char linebuf[BUFSIZ];
54         char scratch[64];
55
56 /* upcase what we got */
57         cp2 = realm;
58         cp1 = expand;
59         while (*cp2) {
60                 *cp1++ = toupper(*cp2++);
61         }
62         *cp1 = '\0';
63
64         if ((rlm_file = fopen("/etc/krb.conf", "r")) == (FILE *) 0) {
65                 return(expand);
66         }
67         
68         if (fgets(linebuf, BUFSIZ, rlm_file) == NULL) {
69           /* error reading */
70           (void) fclose(rlm_file);
71           return(expand);
72         }
73
74         if (sscanf(linebuf, "%s", krb_realm) < 1) {
75           /* error reading */
76           (void) fclose(rlm_file);
77           return(expand);
78         }
79
80         if (!strncmp(krb_realm, expand, strlen(expand))) {
81           (void) fclose(rlm_file);
82           return(krb_realm);
83         }
84
85         while (1) {
86           /* run through the file, looking for admin host */
87           if (fgets(linebuf, BUFSIZ, rlm_file) == NULL) {
88             (void) fclose(rlm_file);
89             return(expand);
90           }
91
92           if (sscanf(linebuf, "%s %s admin %s", krb_realm, krb_host, scratch)
93               < 2)
94             continue;
95           if (!strncmp(krb_realm, expand, strlen(expand))) {
96             (void) fclose(rlm_file);
97             return(krb_realm);
98           }
99         }
100 #endif /* HAVE_KRB4 */
101 #endif
102 }