multiple-local-realms-20051208
[openafs.git] / src / util / get_krbrlm.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute of Technology.
3  * For copying and distribution information, please see the file <mit-copyright.h>.
4  */
5
6 #include <afsconfig.h>
7 #include <afs/param.h>
8
9 RCSID
10     ("$Header$");
11
12 #include <stdio.h>
13 #include "afsutil.h"
14
15 /*
16  * Specialized version of the kerberos krb_get_lrealm function.
17  * krb_get_lrealm takes a pointer to a string, and a number, n.  It fills
18  * in the string, r, with the name of the nth realm specified on the
19  * first line of the kerberos config file (KRB_CONF, defined in "krb.h").
20  * It returns 0 (KSUCCESS) on success, and KFAILURE on failure. 
21  *
22  * On the kerberos version if the config file does not exist, and if n=1, a 
23  * successful return will occur with r = KRB_REALM (also defined in "krb.h").
24  *
25  */
26 #define KSUCCESS        0
27 #define KFAILURE        (-1)
28
29 static char *
30 parse_str(char *buffer, char *result, int size)
31 {
32     int n=0;
33
34     if (!buffer)
35         goto cleanup;
36
37     while (*buffer && isspace(*buffer))
38         buffer++;
39     while (*buffer && !isspace(*buffer)) {
40         if (n < size - 1) {
41             *result++=*buffer++;
42             n++;
43         } else {
44             buffer++;
45         }
46     }
47
48   cleanup:
49     *result='\0';
50     return buffer;
51 }
52
53
54 int
55 afs_krb_get_lrealm(char *r, int n)
56 {
57     char linebuf[2048];
58     char tr[AFS_REALM_SZ] = "";
59     char *p;
60     FILE *cnffile/*, *fopen()*/;
61     int i;
62     int rv = KFAILURE;
63
64     *r = '\0';
65
66     if ((cnffile = fopen(AFSDIR_SERVER_KCONF_FILEPATH, "r")) == NULL) {
67         return (KFAILURE);
68     }
69     if (fgets(linebuf, sizeof(linebuf)-1, cnffile) == NULL) {
70         goto cleanup;
71     }
72     linebuf[sizeof(linebuf)-1] = '\0';
73     for (i=0, p=linebuf; i<=n && *p; i++) {
74         p = parse_str(p, tr, AFS_REALM_SZ);
75     }
76
77     if (*tr) {
78         strcpy(r,tr);
79         rv = KSUCCESS;
80     }
81
82   cleanup:
83     (void)fclose(cnffile);
84     return rv;
85 }
86
87 int
88 afs_krb_exclusion(char * name)
89 {
90     char linebuf[2048];
91     char excl_name[256] = "";
92     FILE *cnffile/*, *fopen()*/;
93     int exclude = 0;
94
95     if ((cnffile = fopen(AFSDIR_SERVER_KRB_EXCL_FILEPATH, "r")) == NULL)
96         return exclude;
97
98     for (;;) {
99         if (fgets(linebuf, sizeof(linebuf)-1, cnffile) == NULL) {
100             goto cleanup;
101         }
102         linebuf[sizeof(linebuf)-1] = '\0';
103         parse_str(linebuf, excl_name, sizeof(excl_name));
104
105         if (!strcmp(name,excl_name)) {
106             exclude = 1;
107             break;
108         }
109     }
110
111   cleanup:
112     (void)fclose(cnffile);
113     return exclude;
114 }
115
116 int 
117 afs_is_foreign_ticket_name(char *tcell, char *tname, char *tinst, char *localrealm)
118 {
119     int foreign = 0;
120
121     if (localrealm && strcasecmp(localrealm, tcell))
122         foreign = 1;
123
124 #if     defined(AFS_ATHENA_STDENV) || defined(AFS_KERBREALM_ENV)
125     if (!foreign) {
126         static char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
127         static int  num_lrealms = -1;
128         int lrealm_match, i;
129         char uname[256];
130
131         if (num_lrealms == -1) {
132             for (i=0; i<AFS_NUM_LREALMS; i++) {
133                 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
134                     break;
135             }
136
137             if (i=0 && localrealm) {
138                 strncpy(local_realms[0], localrealm, AFS_REALM_SZ);
139                 num_lrealms = 1;
140             } else {
141                 num_lrealms = i;
142             }
143         }
144
145         /* See if the ticket cell matches one of the local realms */
146         lrealm_match = 0;
147         for ( i=0;i<num_lrealms;i++ ) {
148             if (!strcasecmp(local_realms[i], tcell)) {
149                 lrealm_match = 1;
150                 break;
151             }
152         }
153
154         /* If yes, then make sure that the name is not present in 
155          * an exclusion list */
156         if (lrealm_match) {
157             if (tinst[0])
158                 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
159             else
160                 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
161
162             if (afs_krb_exclusion(uname))
163                 lrealm_match = 0;
164         }
165
166         foreign = !lrealm_match;
167     }
168 #endif
169     return foreign;
170 }
171
172
173