Path: blob/main/crypto/heimdal/appl/afsutil/pagsh.c
39481 views
/*1* Copyright (c) 1995 - 2005 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>35#endif3637RCSID("$Id$");3839#include <stdio.h>40#include <stdlib.h>41#include <string.h>42#include <unistd.h>43#ifdef HAVE_SYS_TYPES_H44#include <sys/types.h>45#endif46#include <time.h>47#ifdef HAVE_FCNTL_H48#include <fcntl.h>49#endif50#ifdef HAVE_PWD_H51#include <pwd.h>52#endif5354#ifdef KRB555#include <krb5.h>56#endif57#include <kafs.h>5859#include <err.h>60#include <roken.h>61#include <getarg.h>6263#ifndef TKT_ROOT64#define TKT_ROOT "/tmp/tkt"65#endif6667static int help_flag;68static int version_flag;69static int c_flag;70#ifdef KRB571static char *typename_arg;72#endif7374struct getargs getargs[] = {75{ NULL, 'c', arg_flag, &c_flag },76#ifdef KRB577{ "cache-type", 0, arg_string, &typename_arg },78#endif79{ "version", 0, arg_flag, &version_flag },80{ "help", 'h', arg_flag, &help_flag },81};8283static int num_args = sizeof(getargs) / sizeof(getargs[0]);8485static void86usage(int ecode)87{88arg_printusage(getargs, num_args, NULL, "command [args...]");89exit(ecode);90}9192/*93* Run command with a new ticket file / credentials cache / token94*/9596int97main(int argc, char **argv)98{99int f;100char tf[1024];101char *p;102103char *path;104char **args;105unsigned int i;106int optind = 0;107108setprogname(argv[0]);109if(getarg(getargs, num_args, argc, argv, &optind))110usage(1);111if(help_flag)112usage(0);113if(version_flag) {114print_version(NULL);115exit(0);116}117118argc -= optind;119argv += optind;120121#ifdef KRB5122{123krb5_error_code ret;124krb5_context context;125krb5_ccache id;126const char *name;127128ret = krb5_init_context(&context);129if (ret) /* XXX should this really call exit ? */130errx(1, "no kerberos 5 support");131132ret = krb5_cc_new_unique(context, typename_arg, NULL, &id);133if (ret)134krb5_err(context, 1, ret, "Failed generating credential cache");135136name = krb5_cc_get_name(context, id);137if (name == NULL)138krb5_errx(context, 1, "Generated credential cache have no name");139140snprintf(tf, sizeof(tf), "%s:%s", krb5_cc_get_type(context, id), name);141142ret = krb5_cc_close(context, id);143if (ret)144krb5_err(context, 1, ret, "Failed closing credential cache");145146krb5_free_context(context);147148esetenv("KRB5CCNAME", tf, 1);149}150#endif151152snprintf (tf, sizeof(tf), "%s_XXXXXX", TKT_ROOT);153f = mkstemp (tf);154if (f < 0)155err(1, "mkstemp failed");156close (f);157unlink (tf);158esetenv("KRBTKFILE", tf, 1);159160i = 0;161162args = (char **) malloc((argc + 10)*sizeof(char *));163if (args == NULL)164errx (1, "Out of memory allocating %lu bytes",165(unsigned long)((argc + 10)*sizeof(char *)));166167if(*argv == NULL) {168path = getenv("SHELL");169if(path == NULL){170struct passwd *pw = k_getpwuid(geteuid());171if (pw == NULL)172errx(1, "no such user: %d", (int)geteuid());173path = strdup(pw->pw_shell);174}175} else {176path = strdup(*argv++);177}178if (path == NULL)179errx (1, "Out of memory copying path");180181p=strrchr(path, '/');182if(p)183args[i] = strdup(p+1);184else185args[i] = strdup(path);186187if (args[i++] == NULL)188errx (1, "Out of memory copying arguments");189190while(*argv)191args[i++] = *argv++;192193args[i++] = NULL;194195if(k_hasafs())196k_setpag();197198unsetenv("PAGPID");199execvp(path, args);200if (errno == ENOENT || c_flag) {201char **sh_args = malloc ((i + 2) * sizeof(char *));202unsigned int j;203204if (sh_args == NULL)205errx (1, "Out of memory copying sh arguments");206for (j = 1; j < i; ++j)207sh_args[j + 2] = args[j];208sh_args[0] = "sh";209sh_args[1] = "-c";210sh_args[2] = path;211execv ("/bin/sh", sh_args);212}213err (1, "execvp");214}215216217