1 /* 2 * Copyright (c) 1997-2001 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifdef HAVE_CONFIG_H 35 #include <config.h> 36 RCSID("$Id: afslog.c,v 1.16 2001/05/16 22:10:15 assar Exp $"); 37 #endif 38 #include <ctype.h> 39 #include <krb5.h> 40 #include <kafs.h> 41 #include <roken.h> 42 #include <getarg.h> 43 #include <err.h> 44 45 static int help_flag; 46 static int version_flag; 47 #if 0 48 static int create_user; 49 #endif 50 static getarg_strings cells; 51 static char *realm; 52 static getarg_strings files; 53 static int unlog_flag; 54 static int verbose; 55 56 struct getargs args[] = { 57 { "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cells" }, 58 { "file", 'p', arg_strings, &files, "files to get tokens for", "paths" }, 59 { "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" }, 60 { "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" }, 61 #if 0 62 { "create-user", 0, arg_flag, &create_user, "create user if not found" }, 63 #endif 64 { "verbose",'v', arg_flag, &verbose }, 65 { "version", 0, arg_flag, &version_flag }, 66 { "help", 'h', arg_flag, &help_flag }, 67 }; 68 69 static int num_args = sizeof(args) / sizeof(args[0]); 70 71 static const char * 72 expand_cell_name(const char *cell) 73 { 74 FILE *f; 75 static char buf[128]; 76 char *p; 77 78 f = fopen(_PATH_CELLSERVDB, "r"); 79 if(f == NULL) 80 return cell; 81 while (fgets (buf, sizeof(buf), f) != NULL) { 82 if(buf[0] == '>'){ 83 for(p=buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++) 84 ; 85 *p = '\0'; 86 if(strstr(buf, cell)){ 87 fclose(f); 88 return buf + 1; 89 } 90 } 91 buf[0] = 0; 92 } 93 fclose(f); 94 return cell; 95 } 96 97 #if 0 98 static int 99 createuser (char *cell) 100 { 101 char cellbuf[64]; 102 char name[ANAME_SZ]; 103 char instance[INST_SZ]; 104 char realm[REALM_SZ]; 105 char cmd[1024]; 106 107 if (cell == NULL) { 108 FILE *f; 109 int len; 110 111 f = fopen (_PATH_THISCELL, "r"); 112 if (f == NULL) 113 err (1, "open(%s)", _PATH_THISCELL); 114 if (fgets (cellbuf, sizeof(cellbuf), f) == NULL) 115 err (1, "read cellname from %s", _PATH_THISCELL); 116 len = strlen(cellbuf); 117 if (cellbuf[len-1] == '\n') 118 cellbuf[len-1] = '\0'; 119 cell = cellbuf; 120 } 121 122 if(krb_get_default_principal(name, instance, realm)) 123 errx (1, "Could not even figure out who you are"); 124 125 snprintf (cmd, sizeof(cmd), 126 "pts createuser %s%s%s@%s -cell %s", 127 name, *instance ? "." : "", instance, strlwr(realm), 128 cell); 129 DEBUG("Executing %s", cmd); 130 return system(cmd); 131 } 132 #endif 133 134 static void 135 usage(int ecode) 136 { 137 arg_printusage(args, num_args, NULL, "[cell]... [path]..."); 138 exit(ecode); 139 } 140 141 static int 142 afslog_cell(krb5_context context, krb5_ccache id, 143 const char *cell, int expand) 144 { 145 const char *c = cell; 146 if(expand){ 147 c = expand_cell_name(cell); 148 if(c == NULL){ 149 krb5_warnx(context, "No cell matching \"%s\" found.", cell); 150 return -1; 151 } 152 if(verbose) 153 krb5_warnx(context, "Cell \"%s\" expanded to \"%s\"", cell, c); 154 } 155 return krb5_afslog(context, id, c, realm); 156 } 157 158 static int 159 afslog_file(krb5_context context, krb5_ccache id, 160 const char *path) 161 { 162 char cell[64]; 163 if(k_afs_cell_of_file(path, cell, sizeof(cell))){ 164 krb5_warnx(context, "No cell found for file \"%s\".", path); 165 return -1; 166 } 167 if(verbose) 168 krb5_warnx(context, "File \"%s\" lives in cell \"%s\"", path, cell); 169 return afslog_cell(context, id, cell, 0); 170 } 171 172 int 173 main(int argc, char **argv) 174 { 175 int optind = 0; 176 krb5_context context; 177 krb5_ccache id; 178 int i; 179 int num; 180 int ret = 0; 181 182 setprogname(argv[0]); 183 184 if(getarg(args, num_args, argc, argv, &optind)) 185 usage(1); 186 if(help_flag) 187 usage(0); 188 if(version_flag) { 189 print_version(NULL); 190 exit(0); 191 } 192 193 ret = krb5_init_context(&context); 194 if (ret) 195 errx (1, "krb5_init_context failed: %d", ret); 196 if(!k_hasafs()) 197 krb5_errx(context, 1, 198 "AFS doesn't seem to be present on this machine"); 199 200 if(unlog_flag){ 201 k_unlog(); 202 exit(0); 203 } 204 krb5_cc_default(context, &id); 205 num = 0; 206 for(i = 0; i < files.num_strings; i++){ 207 afslog_file(context, id, files.strings[i]); 208 num++; 209 free_getarg_strings (&files); 210 } 211 for(i = 0; i < cells.num_strings; i++){ 212 afslog_cell(context, id, cells.strings[i], 1); 213 num++; 214 free_getarg_strings (&cells); 215 } 216 for(i = optind; i < argc; i++){ 217 num++; 218 if(strcmp(argv[i], ".") == 0 || 219 strcmp(argv[i], "..") == 0 || 220 strchr(argv[i], '/') || 221 access(argv[i], F_OK) == 0) 222 afslog_file(context, id, argv[i]); 223 else 224 afslog_cell(context, id, argv[i], 1); 225 } 226 if(num == 0) { 227 krb5_afslog(context, id, NULL, NULL); 228 } 229 230 return ret; 231 } 232