diff options
Diffstat (limited to 'crypto/heimdal/appl/afsutil/afslog.c')
-rw-r--r-- | crypto/heimdal/appl/afsutil/afslog.c | 200 |
1 files changed, 155 insertions, 45 deletions
diff --git a/crypto/heimdal/appl/afsutil/afslog.c b/crypto/heimdal/appl/afsutil/afslog.c index 5451b22..fd104df 100644 --- a/crypto/heimdal/appl/afsutil/afslog.c +++ b/crypto/heimdal/appl/afsutil/afslog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997-2001 Kungliga Tekniska Högskolan + * Copyright (c) 1997-2003 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * @@ -33,10 +33,15 @@ #ifdef HAVE_CONFIG_H #include <config.h> -RCSID("$Id: afslog.c,v 1.16 2001/05/16 22:10:15 assar Exp $"); +RCSID("$Id: afslog.c,v 1.21.2.1 2003/04/23 18:04:26 lha Exp $"); #endif #include <ctype.h> +#ifdef KRB5 #include <krb5.h> +#endif +#ifdef KRB4 +#include <krb.h> +#endif #include <kafs.h> #include <roken.h> #include <getarg.h> @@ -52,12 +57,24 @@ static char *realm; static getarg_strings files; static int unlog_flag; static int verbose; +#ifdef KRB4 +static int use_krb4 = 1; +#endif +#ifdef KRB5 +static int use_krb5 = 1; +#endif struct getargs args[] = { - { "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cells" }, - { "file", 'p', arg_strings, &files, "files to get tokens for", "paths" }, + { "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cell" }, + { "file", 'p', arg_strings, &files, "files to get tokens for", "path" }, { "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" }, { "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" }, +#ifdef KRB4 + { "v4", 0, arg_negative_flag, &use_krb4, "use Kerberos 4" }, +#endif +#ifdef KRB5 + { "v5", 0, arg_negative_flag, &use_krb5, "use Kerberos 5" }, +#endif #if 0 { "create-user", 0, arg_flag, &create_user, "create user if not found" }, #endif @@ -68,29 +85,49 @@ struct getargs args[] = { static int num_args = sizeof(args) / sizeof(args[0]); +#ifdef KRB5 +krb5_context context; +krb5_ccache id; +#endif + static const char * -expand_cell_name(const char *cell) +expand_one_file(FILE *f, const char *cell) { - FILE *f; - static char buf[128]; + static char buf[1024]; char *p; - f = fopen(_PATH_CELLSERVDB, "r"); - if(f == NULL) - return cell; while (fgets (buf, sizeof(buf), f) != NULL) { - if(buf[0] == '>'){ - for(p=buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++) + if(buf[0] == '>') { + for(p = buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++) ; *p = '\0'; - if(strstr(buf, cell)){ - fclose(f); + if(strncmp(buf + 1, cell, strlen(cell)) == 0) return buf + 1; - } } - buf[0] = 0; + buf[0] = '\0'; + } + return NULL; +} + +static const char * +expand_cell_name(const char *cell) +{ + FILE *f; + const char *c; + const char **fn, *files[] = { _PATH_CELLSERVDB, + _PATH_ARLA_CELLSERVDB, + _PATH_OPENAFS_DEBIAN_CELLSERVDB, + _PATH_ARLA_DEBIAN_CELLSERVDB, + NULL }; + for(fn = files; *fn; fn++) { + f = fopen(*fn, "r"); + if(f == NULL) + continue; + c = expand_one_file(f, cell); + fclose(f); + if(c) + return c; } - fclose(f); return cell; } @@ -134,50 +171,109 @@ createuser (char *cell) static void usage(int ecode) { - arg_printusage(args, num_args, NULL, "[cell]... [path]..."); + arg_printusage(args, num_args, NULL, "[cell|path]..."); exit(ecode); } +struct cell_list { + char *cell; + struct cell_list *next; +} *cell_list; + static int -afslog_cell(krb5_context context, krb5_ccache id, - const char *cell, int expand) +afslog_cell(const char *cell, int expand) { + struct cell_list *p, **q; const char *c = cell; if(expand){ c = expand_cell_name(cell); if(c == NULL){ - krb5_warnx(context, "No cell matching \"%s\" found.", cell); + warnx("No cell matching \"%s\" found.", cell); return -1; } - if(verbose) - krb5_warnx(context, "Cell \"%s\" expanded to \"%s\"", cell, c); + if(verbose && strcmp(c, cell) != 0) + warnx("Cell \"%s\" expanded to \"%s\"", cell, c); } - return krb5_afslog(context, id, c, realm); + /* add to list of cells to get tokens for, and also remove + duplicates; the actual afslog takes place later */ + for(p = cell_list, q = &cell_list; p; q = &p->next, p = p->next) + if(strcmp(p->cell, c) == 0) + return 0; + p = malloc(sizeof(*p)); + if(p == NULL) + return -1; + p->cell = strdup(c); + if(p->cell == NULL) { + free(p); + return -1; + } + p->next = NULL; + *q = p; + return 0; } static int -afslog_file(krb5_context context, krb5_ccache id, - const char *path) +afslog_file(const char *path) { char cell[64]; if(k_afs_cell_of_file(path, cell, sizeof(cell))){ - krb5_warnx(context, "No cell found for file \"%s\".", path); + warnx("No cell found for file \"%s\".", path); return -1; } if(verbose) - krb5_warnx(context, "File \"%s\" lives in cell \"%s\"", path, cell); - return afslog_cell(context, id, cell, 0); + warnx("File \"%s\" lives in cell \"%s\"", path, cell); + return afslog_cell(cell, 0); +} + +static int +do_afslog(const char *cell) +{ + int k5ret, k4ret; + + k5ret = k4ret = 0; + +#ifdef KRB5 + if(context != NULL && id != NULL && use_krb5) { + k5ret = krb5_afslog(context, id, cell, NULL); + if(k5ret == 0) + return 0; + } +#endif +#if KRB4 + if (use_krb4) { + k4ret = krb_afslog(cell, NULL); + if(k4ret == 0) + return 0; + } +#endif +#ifdef KRB5 + if (k5ret) + warnx("krb5_afslog(%s): %s", cell, krb5_get_err_text(context, k5ret)); +#endif +#ifdef KRB4 + if (k4ret) + warnx("krb_afslog(%s): %s", cell, krb_get_err_text(k4ret)); +#endif + if (k5ret || k4ret) + return 1; + return 0; +} + +static void +log_func(void *ctx, const char *str) +{ + fprintf(stderr, "%s\n", str); } int main(int argc, char **argv) { int optind = 0; - krb5_context context; - krb5_ccache id; int i; int num; int ret = 0; + int failed = 0; + struct cell_list *p; setprogname(argv[0]); @@ -190,42 +286,56 @@ main(int argc, char **argv) exit(0); } - ret = krb5_init_context(&context); - if (ret) - errx (1, "krb5_init_context failed: %d", ret); if(!k_hasafs()) - krb5_errx(context, 1, - "AFS doesn't seem to be present on this machine"); + errx(1, "AFS does not seem to be present on this machine"); if(unlog_flag){ k_unlog(); exit(0); } - krb5_cc_default(context, &id); +#ifdef KRB5 + ret = krb5_init_context(&context); + if (ret) + context = NULL; + else + if(krb5_cc_default(context, &id) != 0) + id = NULL; +#endif + + if (verbose) + kafs_set_verbose(log_func, NULL); + num = 0; for(i = 0; i < files.num_strings; i++){ - afslog_file(context, id, files.strings[i]); + afslog_file(files.strings[i]); num++; - free_getarg_strings (&files); } + free_getarg_strings (&files); for(i = 0; i < cells.num_strings; i++){ - afslog_cell(context, id, cells.strings[i], 1); + afslog_cell(cells.strings[i], 1); num++; - free_getarg_strings (&cells); } + free_getarg_strings (&cells); for(i = optind; i < argc; i++){ num++; if(strcmp(argv[i], ".") == 0 || strcmp(argv[i], "..") == 0 || strchr(argv[i], '/') || access(argv[i], F_OK) == 0) - afslog_file(context, id, argv[i]); + afslog_file(argv[i]); else - afslog_cell(context, id, argv[i], 1); + afslog_cell(argv[i], 1); } if(num == 0) { - krb5_afslog(context, id, NULL, NULL); + if(do_afslog(NULL)) + failed++; + } else + for(p = cell_list; p; p = p->next) { + if(verbose) + warnx("Getting tokens for cell \"%s\"", p->cell); + if(do_afslog(p->cell)) + failed++; } - return ret; + return failed; } |