Path: blob/main/crypto/heimdal/appl/afsutil/afslog.c
39507 views
/*1* Copyright (c) 1997-2003 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35RCSID("$Id$");36#endif37#include <ctype.h>38#ifdef KRB539#include <krb5.h>40#endif41#include <kafs.h>42#include <roken.h>43#include <getarg.h>44#include <err.h>4546static int help_flag;47static int version_flag;48static getarg_strings cells;49static char *realm;50static getarg_strings files;51static int unlog_flag;52static int verbose;53#ifdef KRB554static char *client_string;55static char *cache_string;56static int use_krb5 = 1;57#endif5859struct getargs args[] = {60{ "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cell" },61{ "file", 'p', arg_strings, &files, "files to get tokens for", "path" },62{ "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" },63{ "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" },64#ifdef KRB565{ "principal",'P',arg_string,&client_string,"principal to use","principal"},66{ "cache", 0, arg_string, &cache_string, "ccache to use", "cache"},67{ "v5", 0, arg_negative_flag, &use_krb5, "don't use Kerberos 5" },68#endif69{ "verbose",'v', arg_flag, &verbose },70{ "version", 0, arg_flag, &version_flag },71{ "help", 'h', arg_flag, &help_flag },72};7374static int num_args = sizeof(args) / sizeof(args[0]);7576#ifdef KRB577krb5_context context;78krb5_ccache id;79#endif8081static const char *82expand_one_file(FILE *f, const char *cell)83{84static char buf[1024];85char *p;8687while (fgets (buf, sizeof(buf), f) != NULL) {88if(buf[0] == '>') {89for(p = buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)90;91*p = '\0';92if(strncmp(buf + 1, cell, strlen(cell)) == 0)93return buf + 1;94}95buf[0] = '\0';96}97return NULL;98}99100static const char *101expand_cell_name(const char *cell)102{103FILE *f;104const char *c;105const char **fn, *files[] = { _PATH_CELLSERVDB,106_PATH_ARLA_CELLSERVDB,107_PATH_OPENAFS_DEBIAN_CELLSERVDB,108_PATH_ARLA_DEBIAN_CELLSERVDB,109NULL };110for(fn = files; *fn; fn++) {111f = fopen(*fn, "r");112if(f == NULL)113continue;114c = expand_one_file(f, cell);115fclose(f);116if(c)117return c;118}119return cell;120}121122static void123usage(int ecode)124{125arg_printusage(args, num_args, NULL, "[cell|path]...");126exit(ecode);127}128129struct cell_list {130char *cell;131struct cell_list *next;132} *cell_list;133134static int135afslog_cell(const char *cell, int expand)136{137struct cell_list *p, **q;138const char *c = cell;139if(expand){140c = expand_cell_name(cell);141if(c == NULL){142warnx("No cell matching \"%s\" found.", cell);143return -1;144}145if(verbose && strcmp(c, cell) != 0)146warnx("Cell \"%s\" expanded to \"%s\"", cell, c);147}148/* add to list of cells to get tokens for, and also remove149duplicates; the actual afslog takes place later */150for(p = cell_list, q = &cell_list; p; q = &p->next, p = p->next)151if(strcmp(p->cell, c) == 0)152return 0;153p = malloc(sizeof(*p));154if(p == NULL)155return -1;156p->cell = strdup(c);157if(p->cell == NULL) {158free(p);159return -1;160}161p->next = NULL;162*q = p;163return 0;164}165166static int167afslog_file(const char *path)168{169char cell[64];170if(k_afs_cell_of_file(path, cell, sizeof(cell))){171warnx("No cell found for file \"%s\".", path);172return -1;173}174if(verbose)175warnx("File \"%s\" lives in cell \"%s\"", path, cell);176return afslog_cell(cell, 0);177}178179static int180do_afslog(const char *cell)181{182int k5ret;183184k5ret = 0;185186#ifdef KRB5187if(context != NULL && id != NULL && use_krb5) {188k5ret = krb5_afslog(context, id, cell, realm);189if(k5ret == 0)190return 0;191}192#endif193if (cell == NULL)194cell = "<default cell>";195#ifdef KRB5196if (k5ret)197krb5_warn(context, k5ret, "krb5_afslog(%s)", cell);198#endif199if (k5ret)200return 1;201return 0;202}203204static void205log_func(void *ctx, const char *str)206{207fprintf(stderr, "%s\n", str);208}209210int211main(int argc, char **argv)212{213int optind = 0;214int i;215int num;216int ret = 0;217int failed = 0;218struct cell_list *p;219220setprogname(argv[0]);221222if(getarg(args, num_args, argc, argv, &optind))223usage(1);224if(help_flag)225usage(0);226if(version_flag) {227print_version(NULL);228exit(0);229}230231if(!k_hasafs())232errx(1, "AFS does not seem to be present on this machine");233234if(unlog_flag){235k_unlog();236exit(0);237}238#ifdef KRB5239ret = krb5_init_context(&context);240if (ret) {241context = NULL;242} else {243if (client_string) {244krb5_principal client;245246ret = krb5_parse_name(context, client_string, &client);247if (ret == 0)248ret = krb5_cc_cache_match(context, client, &id);249if (ret)250id = NULL;251}252if (id == NULL && cache_string) {253if(krb5_cc_resolve(context, cache_string, &id) != 0) {254krb5_warnx(context, "failed to open kerberos 5 cache '%s'",255cache_string);256id = NULL;257}258}259if (id == NULL)260if(krb5_cc_default(context, &id) != 0)261id = NULL;262}263#endif264265if (verbose)266kafs_set_verbose(log_func, NULL);267268num = 0;269for(i = 0; i < files.num_strings; i++){270afslog_file(files.strings[i]);271num++;272}273free_getarg_strings (&files);274for(i = 0; i < cells.num_strings; i++){275afslog_cell(cells.strings[i], 1);276num++;277}278free_getarg_strings (&cells);279for(i = optind; i < argc; i++){280num++;281if(strcmp(argv[i], ".") == 0 ||282strcmp(argv[i], "..") == 0 ||283strchr(argv[i], '/') ||284access(argv[i], F_OK) == 0)285afslog_file(argv[i]);286else287afslog_cell(argv[i], 1);288}289if(num == 0) {290if(do_afslog(NULL))291failed++;292} else293for(p = cell_list; p; p = p->next) {294if(verbose)295warnx("Getting tokens for cell \"%s\"", p->cell);296if(do_afslog(p->cell))297failed++;298}299300return failed;301}302303304