1 /* $NetBSD: common.c,v 1.1.1.1 2011/04/13 18:15:30 elric Exp $ */
4 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "kafs_locl.h"
38 #define AUTH_SUPERUSER "afs"
41 * Here only ASCII characters are relevant.
44 #define IsAsciiLower(c) ('a' <= (c) && (c) <= 'z')
46 #define ToAsciiUpper(c) ((c) - 'a' + 'A')
48 static void (*kafs_verbose
)(void *, const char *);
49 static void *kafs_verbose_ctx
;
52 _kafs_foldup(char *a
, const char *b
)
56 *a
= ToAsciiUpper(*b
);
63 kafs_set_verbose(void (*f
)(void *, const char *), void *ctx
)
67 kafs_verbose_ctx
= ctx
;
72 kafs_settoken_rxkad(const char *cell
, struct ClearToken
*ct
,
73 void *ticket
, size_t ticket_len
)
75 struct ViceIoctl parms
;
81 * length of secret token followed by secret token
83 sizeof_x
= ticket_len
;
84 memcpy(t
, &sizeof_x
, sizeof(sizeof_x
));
85 t
+= sizeof(sizeof_x
);
86 memcpy(t
, ticket
, sizeof_x
);
89 * length of clear token followed by clear token
91 sizeof_x
= sizeof(*ct
);
92 memcpy(t
, &sizeof_x
, sizeof(sizeof_x
));
93 t
+= sizeof(sizeof_x
);
94 memcpy(t
, ct
, sizeof_x
);
98 * do *not* mark as primary cell
101 memcpy(t
, &sizeof_x
, sizeof(sizeof_x
));
102 t
+= sizeof(sizeof_x
);
104 * follow with cell name
106 sizeof_x
= strlen(cell
) + 1;
107 memcpy(t
, cell
, sizeof_x
);
111 * Build argument block
114 parms
.in_size
= t
- buf
;
118 return k_pioctl(0, VIOCSETTOK
, &parms
, 0);
122 _kafs_fixup_viceid(struct ClearToken
*ct
, uid_t uid
)
124 #define ODD(x) ((x) & 1)
125 /* According to Transarc conventions ViceId is valid iff
126 * (EndTimestamp - BeginTimestamp) is odd. By decrementing EndTime
127 * the transformations:
129 * (issue_date, life) -> (StartTime, EndTime) -> (issue_date, life)
130 * preserves the original values.
132 if (uid
!= 0) /* valid ViceId */
134 if (!ODD(ct
->EndTimestamp
- ct
->BeginTimestamp
))
137 else /* not valid ViceId */
139 if (ODD(ct
->EndTimestamp
- ct
->BeginTimestamp
))
144 /* Try to get a db-server for an AFS cell from a AFSDB record */
147 dns_find_cell(const char *cell
, char *dbserver
, size_t len
)
149 struct rk_dns_reply
*r
;
151 r
= rk_dns_lookup(cell
, "afsdb");
153 struct rk_resource_record
*rr
= r
->head
;
155 if(rr
->type
== rk_ns_t_afsdb
&& rr
->u
.afsdb
->preference
== 1){
171 * Try to find the cells we should try to klog to in "file".
174 find_cells(const char *file
, char ***cells
, int *idx
)
181 f
= fopen(file
, "r");
184 while (fgets(cell
, sizeof(cell
), f
)) {
186 t
= cell
+ strlen(cell
);
187 for (; t
>= cell
; t
--)
188 if (*t
== '\n' || *t
== '\t' || *t
== ' ')
190 if (cell
[0] == '\0' || cell
[0] == '#')
192 for(i
= 0; i
< ind
; i
++)
193 if(strcmp((*cells
)[i
], cell
) == 0)
198 tmp
= realloc(*cells
, (ind
+ 1) * sizeof(**cells
));
202 (*cells
)[ind
] = strdup(cell
);
203 if ((*cells
)[ind
] == NULL
)
213 * Get tokens for all cells[]
216 afslog_cells(struct kafs_data
*data
, char **cells
, int max
, uid_t uid
,
221 for (i
= 0; i
< max
; i
++) {
222 int er
= (*data
->afslog_uid
)(data
, cells
[i
], 0, uid
, homedir
);
230 _kafs_afslog_all_local_cells(struct kafs_data
*data
,
231 uid_t uid
, const char *homedir
)
238 homedir
= getenv("HOME");
239 if (homedir
!= NULL
) {
240 char home
[MaxPathLen
];
241 snprintf(home
, sizeof(home
), "%s/.TheseCells", homedir
);
242 find_cells(home
, &cells
, &idx
);
244 find_cells(_PATH_THESECELLS
, &cells
, &idx
);
245 find_cells(_PATH_THISCELL
, &cells
, &idx
);
246 find_cells(_PATH_ARLA_THESECELLS
, &cells
, &idx
);
247 find_cells(_PATH_ARLA_THISCELL
, &cells
, &idx
);
248 find_cells(_PATH_OPENAFS_DEBIAN_THESECELLS
, &cells
, &idx
);
249 find_cells(_PATH_OPENAFS_DEBIAN_THISCELL
, &cells
, &idx
);
250 find_cells(_PATH_OPENAFS_MACOSX_THESECELLS
, &cells
, &idx
);
251 find_cells(_PATH_OPENAFS_MACOSX_THISCELL
, &cells
, &idx
);
252 find_cells(_PATH_ARLA_DEBIAN_THESECELLS
, &cells
, &idx
);
253 find_cells(_PATH_ARLA_DEBIAN_THISCELL
, &cells
, &idx
);
254 find_cells(_PATH_ARLA_OPENBSD_THESECELLS
, &cells
, &idx
);
255 find_cells(_PATH_ARLA_OPENBSD_THISCELL
, &cells
, &idx
);
257 ret
= afslog_cells(data
, cells
, idx
, uid
, homedir
);
266 file_find_cell(struct kafs_data
*data
,
267 const char *cell
, char **realm
, int exact
)
274 if ((F
= fopen(_PATH_CELLSERVDB
, "r"))
275 || (F
= fopen(_PATH_ARLA_CELLSERVDB
, "r"))
276 || (F
= fopen(_PATH_OPENAFS_DEBIAN_CELLSERVDB
, "r"))
277 || (F
= fopen(_PATH_OPENAFS_MACOSX_CELLSERVDB
, "r"))
278 || (F
= fopen(_PATH_ARLA_DEBIAN_CELLSERVDB
, "r"))) {
279 while (fgets(buf
, sizeof(buf
), F
)) {
283 continue; /* Not a cell name line, try next line */
285 strsep(&p
, " \t\n#");
288 cmp
= strcmp(buf
+ 1, cell
);
290 cmp
= strncmp(buf
+ 1, cell
, strlen(cell
));
294 * We found the cell name we're looking for.
295 * Read next line on the form ip-address '#' hostname
297 if (fgets(buf
, sizeof(buf
), F
) == NULL
)
298 break; /* Read failed, give up */
299 p
= strchr(buf
, '#');
301 break; /* No '#', give up */
303 if (buf
[strlen(buf
) - 1] == '\n')
304 buf
[strlen(buf
) - 1] = '\0';
305 *realm
= (*data
->get_realm
)(data
, p
);
306 if (*realm
&& **realm
!= '\0')
308 break; /* Won't try any more */
316 /* Find the realm associated with cell. Do this by opening CellServDB
317 file and getting the realm-of-host for the first VL-server for the
320 This does not work when the VL-server is living in one realm, but
321 the cell it is serving is living in another realm.
323 Return 0 on success, -1 otherwise.
327 _kafs_realm_of_cell(struct kafs_data
*data
,
328 const char *cell
, char **realm
)
333 ret
= file_find_cell(data
, cell
, realm
, 1);
336 if (dns_find_cell(cell
, buf
, sizeof(buf
)) == 0) {
337 *realm
= (*data
->get_realm
)(data
, buf
);
341 return file_find_cell(data
, cell
, realm
, 0);
345 _kafs_try_get_cred(struct kafs_data
*data
, const char *user
, const char *cell
,
346 const char *realm
, uid_t uid
, struct kafs_token
*kt
)
350 ret
= (*data
->get_cred
)(data
, user
, cell
, realm
, uid
, kt
);
352 const char *estr
= (*data
->get_error
)(data
, ret
);
354 asprintf(&str
, "%s tried afs%s%s@%s -> %s (%d)",
355 data
->name
, cell
? "/" : "",
356 cell
? cell
: "", realm
, estr
? estr
: "unknown", ret
);
357 (*kafs_verbose
)(kafs_verbose_ctx
, str
);
359 (*data
->free_error
)(data
, estr
);
368 _kafs_get_cred(struct kafs_data
*data
,
370 const char *realm_hint
,
373 struct kafs_token
*kt
)
379 /* We're about to find the realm that holds the key for afs in
380 * the specified cell. The problem is that null-instance
381 * afs-principals are common and that hitting the wrong realm might
382 * yield the wrong afs key. The following assumptions were made.
384 * Any realm passed to us is preferred.
386 * If there is a realm with the same name as the cell, it is most
387 * likely the correct realm to talk to.
389 * In most (maybe even all) cases the database servers of the cell
390 * will live in the realm we are looking for.
392 * Try the local realm, but if the previous cases fail, this is
393 * really a long shot.
397 /* comments on the ordering of these tests */
399 /* If the user passes a realm, she probably knows something we don't
400 * know and we should try afs@realm_hint.
404 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
405 cell
, realm_hint
, uid
, kt
);
406 if (ret
== 0) return 0;
407 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
408 NULL
, realm_hint
, uid
, kt
);
409 if (ret
== 0) return 0;
412 _kafs_foldup(CELL
, cell
);
415 * If the AFS servers have a file /usr/afs/etc/krb.conf containing
416 * REALM we still don't have to resort to cross-cell authentication.
417 * Try afs.cell@REALM.
419 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
420 cell
, realm
, uid
, kt
);
421 if (ret
== 0) return 0;
424 * If cell == realm we don't need no cross-cell authentication.
427 if (strcmp(CELL
, realm
) == 0) {
428 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
429 NULL
, realm
, uid
, kt
);
430 if (ret
== 0) return 0;
434 * We failed to get ``first class tickets'' for afs,
435 * fall back to cross-cell authentication.
439 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
440 NULL
, CELL
, uid
, kt
);
441 if (ret
== 0) return 0;
442 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
443 cell
, CELL
, uid
, kt
);
444 if (ret
== 0) return 0;
447 * Perhaps the cell doesn't correspond to any realm?
448 * Use realm of first volume location DB server.
449 * Try afs.cell@VL_REALM.
450 * Try afs@VL_REALM???
452 if (_kafs_realm_of_cell(data
, cell
, &vl_realm
) == 0
453 && strcmp(vl_realm
, realm
) != 0
454 && strcmp(vl_realm
, CELL
) != 0) {
455 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
456 cell
, vl_realm
, uid
, kt
);
458 ret
= _kafs_try_get_cred(data
, AUTH_SUPERUSER
,
459 NULL
, vl_realm
, uid
, kt
);
461 if (ret
== 0) return 0;