-
Notifications
You must be signed in to change notification settings - Fork 154
/
auth_krb.c
432 lines (368 loc) · 11.8 KB
/
auth_krb.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* auth_krb.c -- Kerberos authorization
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <stdlib.h>
#include <sysexits.h>
#include "auth.h"
#include "xmalloc.h"
#include "util.h"
#ifdef HAVE_KRB
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <krb.h>
#include <ndbm.h>
#ifndef KRB_MAPNAME
#define KRB_MAPNAME (SYSCONF_DIR "/krb.equiv")
#endif
struct auth_state {
char userid[MAX_K_NAME_SZ+1];
char aname[ANAME_SZ];
char inst[INST_SZ];
char realm[REALM_SZ];
};
static struct auth_state auth_anonymous = {
"anonymous", "anonymous", "", ""
};
static int parse_krbequiv_line (const char *src,
char *principal, char *localuser);
static char *auth_map_krbid (const char *real_aname, const char *real_inst,
const char *real_realm);
/*
* Determine if the user is a member of 'identifier'
* Returns one of:
* 0 User does not match identifier
* 1 identifier matches everybody
* 2 User is in the group that is identifier
* 3 User is identifer
*/
static int mymemberof(const struct auth_state *auth_state, const char *identifier)
{
char aname[ANAME_SZ];
char inst[INST_SZ];
char realm[REALM_SZ];
if (!auth_state) auth_state = &auth_anonymous;
if (strcmp(identifier, "anyone") == 0) return 1;
if (strcmp(identifier, auth_state->userid) == 0) return 3;
/* "anonymous" is not a member of any group */
if (strcmp(auth_state->userid, "anonymous") == 0) return 0;
aname[0] = inst[0] = realm[0] = '\0';
if (kname_parse(aname, inst, realm, (char *) identifier) != 0) {
return 0;
}
if (strcmp(aname, auth_state->aname) != 0 && strcmp(aname, "*") != 0) {
return 0;
}
if (strcmp(inst, auth_state->inst) != 0 && strcmp(inst, "*") != 0) {
return 0;
}
if (strcmp(realm, auth_state->realm) != 0 && strcmp(realm, "*") != 0) {
return 0;
}
return 2;
}
/*
* Parse a line 'src' from an /etc/krb.equiv file.
* Sets the buffer pointed to by 'principal' to be the kerberos
* identity and sets the buffer pointed to by 'localuser' to
* be the local user. Both buffers must be of size one larger than
* MAX_K_NAME_SZ. Returns 1 on success, 0 on failure.
*/
static int
parse_krbequiv_line(const char *src, char *principal, char *localuser)
{
int i;
while (Uisspace(*src)) src++;
if (!*src) return 0;
for (i = 0; *src && !Uisspace(*src); i++) {
if (i >= MAX_K_NAME_SZ) return 0;
*principal++ = *src++;
}
*principal = 0;
if (!Uisspace(*src)) return 0; /* Need at least one separator */
while (Uisspace(*src)) src++;
if (!*src) return 0;
for (i = 0; *src && !Uisspace(*src); i++) {
if (i >= MAX_K_NAME_SZ) return 0;
*localuser++ = *src++;
}
*localuser = 0;
return 1;
}
/*
* Map a remote kerberos principal to a local username. If a mapping
* is found, a pointer to the local username is returned. Otherwise,
* a NULL pointer is returned.
* Eventually, this may be more sophisticated than a simple file scan.
*/
static char *auth_map_krbid(real_aname, real_inst, real_realm)
const char *real_aname;
const char *real_inst;
const char *real_realm;
{
static char localuser[MAX_K_NAME_SZ + 1];
char principal[MAX_K_NAME_SZ + 1];
char aname[ANAME_SZ];
char inst[INST_SZ];
char realm[REALM_SZ];
char lrealm[REALM_SZ];
char krbhst[256];
char *p;
char buf[1024];
FILE *mapfile;
if (!(mapfile = fopen(KRB_MAPNAME, "r"))) {
/* If the file can't be opened, don't do mappings */
return 0;
}
for (;;) {
if (!fgets(buf, sizeof(buf), mapfile)) break;
if (parse_krbequiv_line(buf, principal, localuser) == 0 ||
kname_parse(aname, inst, realm, principal) != 0) {
/* Ignore badly formed lines */
continue;
}
if (!strcmp(aname, real_aname) && !strcmp(inst, real_inst) &&
!strcmp(realm, real_realm)) {
fclose(mapfile);
aname[0] = inst[0] = realm[0] = '\0';
if (kname_parse(aname, inst, realm, localuser) != 0) {
return 0;
}
/* Upcase realm name */
for (p = realm; *p; p++) {
if (Uislower(*p)) *p = toupper(*p);
}
if (*realm) {
if (krb_get_lrealm(lrealm,1) == 0 &&
strcmp(lrealm, realm) == 0) {
*realm = 0;
}
else if (krb_get_krbhst(krbhst, realm, 1)) {
return 0; /* Unknown realm */
}
}
strcpy(localuser, aname);
if (*inst) {
strcat(localuser, ".");
strcat(localuser, inst);
}
if (*realm) {
strcat(localuser, "@");
strcat(localuser, realm);
}
return localuser;
}
}
fclose(mapfile);
return 0;
}
/*
* Convert 'identifier' into canonical form.
* Returns a pointer to a static buffer containing the canonical form
* or NULL if 'identifier' is invalid.
*/
static const char *mycanonifyid(const char *identifier, size_t len)
{
static char retbuf[MAX_K_NAME_SZ+1];
char aname[ANAME_SZ];
char inst[INST_SZ];
char realm[REALM_SZ];
char lrealm[REALM_SZ];
char krbhst[256];
char *canon_buf;
char *p;
if(!len) len = strlen(identifier);
canon_buf = malloc(len + 1);
if(!canon_buf) return 0;
memcpy(canon_buf, identifier, len);
canon_buf[len] = '\0';
aname[0] = inst[0] = realm[0] = '\0';
if (kname_parse(aname, inst, realm, canon_buf) != 0) {
free(canon_buf);
return 0;
}
free(canon_buf);
/* Upcase realm name */
for (p = realm; *p; p++) {
if (Uislower(*p)) *p = toupper(*p);
}
if (*realm) {
if (krb_get_lrealm(lrealm,1) == 0 &&
strcmp(lrealm, realm) == 0) {
*realm = 0;
}
else if (krb_get_krbhst(krbhst, realm, 1)) {
return 0; /* Unknown realm */
}
}
/* Check for krb.equiv remappings. */
if ((p = auth_map_krbid(aname, inst, realm)) ) {
strcpy(retbuf, p);
return retbuf;
}
strcpy(retbuf, aname);
if (*inst) {
strcat(retbuf, ".");
strcat(retbuf, inst);
}
if (*realm) {
strcat(retbuf, "@");
strcat(retbuf, realm);
}
return retbuf;
}
/*
* Set the current user to 'identifier'. 'cacheid', if non-null,
* points to a 16-byte binary key to cache identifier's information
* with.
*/
static struct auth_state *mynewstate(const char *identifier)
{
struct auth_state *newstate;
identifier = auth_canonifyid(identifier, 0);
if (!identifier) return 0;
newstate = (struct auth_state *)xmalloc(sizeof(struct auth_state));
strcpy(newstate->userid, identifier);
newstate->aname[0] = newstate->inst[0] = newstate->realm[0] = '\0';
kname_parse(newstate->aname, newstate->inst, newstate->realm, (char *) identifier);
return newstate;
}
static void myfreestate(struct auth_state *auth_state)
{
free((char *)auth_state);
}
static char *make_krb_wildcard(const char *aname, const char *inst, const char *realm)
{
return strconcat(
(aname ? aname : "*"),
".",
(inst ? inst : "*"),
"@",
(realm ? realm : "*"),
NULL
);
}
/* KRB4 groups are just principals with wildcarded components.
* XXX This hasn't even been so much as compile-tested for lack of
* a kerberos test environment! If you use this, please provide
* feedback.
*/
static strarray_t *mygroups(const struct auth_state *auth_state)
{
strarray_t *sa = strarray_new();
char *tmp = NULL;
/* *.*@* */
tmp = make_krb_wildcard(NULL, NULL, NULL);
strarray_appendm(sa, tmp);
/* *.*@realm */
if (auth_state->realm) {
tmp = make_krb_wildcard(NULL, NULL, auth_state->realm);
strarray_appendm(sa, tmp);
}
/* *.inst@* */
if (auth_state->inst) {
tmp = make_krb_wildcard(NULL, auth_state->inst, NULL);
strarray_appendm(sa, tmp);
if (auth_state->realm) {
tmp = make_krb_wildcard(NULL, auth_state->inst, auth_state->realm);
strarray_appendm(sa, tmp);
}
}
/* aname.*@* */
if (auth_state->aname) {
tmp = make_krb_wildcard(auth_state->aname, NULL, NULL);
strarray_appendm(sa, tmp);
if (auth_state->realm) {
tmp = make_krb_wildcard(auth_state->aname, NULL, auth_state->realm);
strarray_appendm(sa, tmp);
}
if (auth_state->inst) {
tmp = make_krb_wildcard(auth_state->aname, auth_state->inst, NULL);
strarray_appendm(sa, tmp);
}
/* n.b. non-wildcard "aname.inst@realm" is NOT a group! */
}
return sa;
}
#else /* HAVE_KRB */
static int mymemberof(
const struct auth_state *auth_state __attribute__((unused)),
const char *identifier __attribute__((unused)))
{
fatal("Authentication mechanism (krb) not compiled in", EX_CONFIG);
return 0;
}
static const char *mycanonifyid(
const char *identifier __attribute__((unused)),
size_t len __attribute__((unused)))
{
fatal("Authentication mechanism (krb) not compiled in", EX_CONFIG);
return NULL;
}
static struct auth_state *mynewstate(
const char *identifier __attribute__((unused)))
{
fatal("Authentication mechanism (krb) not compiled in", EX_CONFIG);
return NULL;
}
static void myfreestate(
struct auth_state *auth_state __attribute__((unused)))
{
fatal("Authentication mechanism (krb) not compiled in", EX_CONFIG);
}
static strarray_t *mygroups(
const struct auth_state *auth_state __attribute__((unused)))
{
fatal("Authentication mechanism (krb) not compiled in", EX_CONFIG);
}
#endif
HIDDEN struct auth_mech auth_krb =
{
"krb", /* name */
&mycanonifyid,
&mymemberof,
&mynewstate,
&myfreestate,
&mygroups,
};