1/*
| 1/*
|
2 * Copyright (c) 1997-2001 Kungliga Tekniska H�gskolan
| 2 * Copyright (c) 1997-2003 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>
| 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>
|
36RCSID("$Id: afslog.c,v 1.16 2001/05/16 22:10:15 assar Exp $");
| 36RCSID("$Id: afslog.c,v 1.21.2.1 2003/04/23 18:04:26 lha Exp $");
|
37#endif 38#include <ctype.h>
| 37#endif 38#include <ctype.h>
|
| 39#ifdef KRB5
|
39#include <krb5.h>
| 40#include <krb5.h>
|
| 41#endif 42#ifdef KRB4 43#include <krb.h> 44#endif
|
40#include <kafs.h> 41#include <roken.h> 42#include <getarg.h> 43#include <err.h> 44 45static int help_flag; 46static int version_flag; 47#if 0 48static int create_user; 49#endif 50static getarg_strings cells; 51static char *realm; 52static getarg_strings files; 53static int unlog_flag; 54static int verbose;
| 45#include <kafs.h> 46#include <roken.h> 47#include <getarg.h> 48#include <err.h> 49 50static int help_flag; 51static int version_flag; 52#if 0 53static int create_user; 54#endif 55static getarg_strings cells; 56static char *realm; 57static getarg_strings files; 58static int unlog_flag; 59static int verbose;
|
| 60#ifdef KRB4 61static int use_krb4 = 1; 62#endif 63#ifdef KRB5 64static int use_krb5 = 1; 65#endif
|
55 56struct getargs args[] = {
| 66 67struct 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" },
| 68 { "cell", 'c', arg_strings, &cells, "cells to get tokens for", "cell" }, 69 { "file", 'p', arg_strings, &files, "files to get tokens for", "path" },
|
59 { "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" }, 60 { "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" },
| 70 { "realm", 'k', arg_string, &realm, "realm for afs cell", "realm" }, 71 { "unlog", 'u', arg_flag, &unlog_flag, "remove tokens" },
|
| 72#ifdef KRB4 73 { "v4", 0, arg_negative_flag, &use_krb4, "use Kerberos 4" }, 74#endif 75#ifdef KRB5 76 { "v5", 0, arg_negative_flag, &use_krb5, "use Kerberos 5" }, 77#endif
|
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 69static int num_args = sizeof(args) / sizeof(args[0]); 70
| 78#if 0 79 { "create-user", 0, arg_flag, &create_user, "create user if not found" }, 80#endif 81 { "verbose",'v', arg_flag, &verbose }, 82 { "version", 0, arg_flag, &version_flag }, 83 { "help", 'h', arg_flag, &help_flag }, 84}; 85 86static int num_args = sizeof(args) / sizeof(args[0]); 87
|
| 88#ifdef KRB5 89krb5_context context; 90krb5_ccache id; 91#endif 92
|
71static const char *
| 93static const char *
|
72expand_cell_name(const char *cell)
| 94expand_one_file(FILE *f, const char *cell)
|
73{
| 95{
|
74 FILE *f; 75 static char buf[128];
| 96 static char buf[1024];
|
76 char *p; 77
| 97 char *p; 98
|
78 f = fopen(_PATH_CELLSERVDB, "r"); 79 if(f == NULL) 80 return cell;
| |
81 while (fgets (buf, sizeof(buf), f) != NULL) {
| 99 while (fgets (buf, sizeof(buf), f) != NULL) {
|
82 if(buf[0] == '>'){ 83 for(p=buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)
| 100 if(buf[0] == '>') { 101 for(p = buf; *p && !isspace((unsigned char)*p) && *p != '#'; p++)
|
84 ; 85 *p = '\0';
| 102 ; 103 *p = '\0';
|
86 if(strstr(buf, cell)){ 87 fclose(f);
| 104 if(strncmp(buf + 1, cell, strlen(cell)) == 0)
|
88 return buf + 1;
| 105 return buf + 1;
|
89 }
| |
90 }
| 106 }
|
91 buf[0] = 0;
| 107 buf[0] = '\0';
|
92 }
| 108 }
|
93 fclose(f);
| 109 return NULL; 110} 111 112static const char * 113expand_cell_name(const char *cell) 114{ 115 FILE *f; 116 const char *c; 117 const char **fn, *files[] = { _PATH_CELLSERVDB, 118 _PATH_ARLA_CELLSERVDB, 119 _PATH_OPENAFS_DEBIAN_CELLSERVDB, 120 _PATH_ARLA_DEBIAN_CELLSERVDB, 121 NULL }; 122 for(fn = files; *fn; fn++) { 123 f = fopen(*fn, "r"); 124 if(f == NULL) 125 continue; 126 c = expand_one_file(f, cell); 127 fclose(f); 128 if(c) 129 return c; 130 }
|
94 return cell; 95} 96 97#if 0 98static int 99createuser (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 134static void 135usage(int ecode) 136{
| 131 return cell; 132} 133 134#if 0 135static int 136createuser (char *cell) 137{ 138 char cellbuf[64]; 139 char name[ANAME_SZ]; 140 char instance[INST_SZ]; 141 char realm[REALM_SZ]; 142 char cmd[1024]; 143 144 if (cell == NULL) { 145 FILE *f; 146 int len; 147 148 f = fopen (_PATH_THISCELL, "r"); 149 if (f == NULL) 150 err (1, "open(%s)", _PATH_THISCELL); 151 if (fgets (cellbuf, sizeof(cellbuf), f) == NULL) 152 err (1, "read cellname from %s", _PATH_THISCELL); 153 len = strlen(cellbuf); 154 if (cellbuf[len-1] == '\n') 155 cellbuf[len-1] = '\0'; 156 cell = cellbuf; 157 } 158 159 if(krb_get_default_principal(name, instance, realm)) 160 errx (1, "Could not even figure out who you are"); 161 162 snprintf (cmd, sizeof(cmd), 163 "pts createuser %s%s%s@%s -cell %s", 164 name, *instance ? "." : "", instance, strlwr(realm), 165 cell); 166 DEBUG("Executing %s", cmd); 167 return system(cmd); 168} 169#endif 170 171static void 172usage(int ecode) 173{
|
137 arg_printusage(args, num_args, NULL, "[cell]... [path]...");
| 174 arg_printusage(args, num_args, NULL, "[cell|path]...");
|
138 exit(ecode); 139} 140
| 175 exit(ecode); 176} 177
|
| 178struct cell_list { 179 char *cell; 180 struct cell_list *next; 181} *cell_list; 182
|
141static int
| 183static int
|
142afslog_cell(krb5_context context, krb5_ccache id, 143 const char *cell, int expand)
| 184afslog_cell(const char *cell, int expand)
|
144{
| 185{
|
| 186 struct cell_list *p, **q;
|
145 const char *c = cell; 146 if(expand){ 147 c = expand_cell_name(cell); 148 if(c == NULL){
| 187 const char *c = cell; 188 if(expand){ 189 c = expand_cell_name(cell); 190 if(c == NULL){
|
149 krb5_warnx(context, "No cell matching \"%s\" found.", cell);
| 191 warnx("No cell matching \"%s\" found.", cell);
|
150 return -1; 151 }
| 192 return -1; 193 }
|
152 if(verbose) 153 krb5_warnx(context, "Cell \"%s\" expanded to \"%s\"", cell, c);
| 194 if(verbose && strcmp(c, cell) != 0) 195 warnx("Cell \"%s\" expanded to \"%s\"", cell, c);
|
154 }
| 196 }
|
155 return krb5_afslog(context, id, c, realm);
| 197 /* add to list of cells to get tokens for, and also remove 198 duplicates; the actual afslog takes place later */ 199 for(p = cell_list, q = &cell_list; p; q = &p->next, p = p->next) 200 if(strcmp(p->cell, c) == 0) 201 return 0; 202 p = malloc(sizeof(*p)); 203 if(p == NULL) 204 return -1; 205 p->cell = strdup(c); 206 if(p->cell == NULL) { 207 free(p); 208 return -1; 209 } 210 p->next = NULL; 211 *q = p; 212 return 0;
|
156} 157 158static int
| 213} 214 215static int
|
159afslog_file(krb5_context context, krb5_ccache id, 160 const char *path)
| 216afslog_file(const char *path)
|
161{ 162 char cell[64]; 163 if(k_afs_cell_of_file(path, cell, sizeof(cell))){
| 217{ 218 char cell[64]; 219 if(k_afs_cell_of_file(path, cell, sizeof(cell))){
|
164 krb5_warnx(context, "No cell found for file \"%s\".", path);
| 220 warnx("No cell found for file \"%s\".", path);
|
165 return -1; 166 } 167 if(verbose)
| 221 return -1; 222 } 223 if(verbose)
|
168 krb5_warnx(context, "File \"%s\" lives in cell \"%s\"", path, cell); 169 return afslog_cell(context, id, cell, 0);
| 224 warnx("File \"%s\" lives in cell \"%s\"", path, cell); 225 return afslog_cell(cell, 0);
|
170} 171
| 226} 227
|
| 228static int 229do_afslog(const char *cell) 230{ 231 int k5ret, k4ret; 232 233 k5ret = k4ret = 0; 234 235#ifdef KRB5 236 if(context != NULL && id != NULL && use_krb5) { 237 k5ret = krb5_afslog(context, id, cell, NULL); 238 if(k5ret == 0) 239 return 0; 240 } 241#endif 242#if KRB4 243 if (use_krb4) { 244 k4ret = krb_afslog(cell, NULL); 245 if(k4ret == 0) 246 return 0; 247 } 248#endif 249#ifdef KRB5 250 if (k5ret) 251 warnx("krb5_afslog(%s): %s", cell, krb5_get_err_text(context, k5ret)); 252#endif 253#ifdef KRB4 254 if (k4ret) 255 warnx("krb_afslog(%s): %s", cell, krb_get_err_text(k4ret)); 256#endif 257 if (k5ret || k4ret) 258 return 1; 259 return 0; 260} 261 262static void 263log_func(void *ctx, const char *str) 264{ 265 fprintf(stderr, "%s\n", str); 266} 267
|
172int 173main(int argc, char **argv) 174{ 175 int optind = 0;
| 268int 269main(int argc, char **argv) 270{ 271 int optind = 0;
|
176 krb5_context context; 177 krb5_ccache id;
| |
178 int i; 179 int num; 180 int ret = 0;
| 272 int i; 273 int num; 274 int ret = 0;
|
| 275 int failed = 0; 276 struct cell_list *p;
|
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
| 277 278 setprogname(argv[0]); 279 280 if(getarg(args, num_args, argc, argv, &optind)) 281 usage(1); 282 if(help_flag) 283 usage(0); 284 if(version_flag) { 285 print_version(NULL); 286 exit(0); 287 } 288
|
193 ret = krb5_init_context(&context); 194 if (ret) 195 errx (1, "krb5_init_context failed: %d", ret);
| |
196 if(!k_hasafs())
| 289 if(!k_hasafs())
|
197 krb5_errx(context, 1, 198 "AFS doesn't seem to be present on this machine");
| 290 errx(1, "AFS does not seem to be present on this machine");
|
199 200 if(unlog_flag){ 201 k_unlog(); 202 exit(0); 203 }
| 291 292 if(unlog_flag){ 293 k_unlog(); 294 exit(0); 295 }
|
204 krb5_cc_default(context, &id);
| 296#ifdef KRB5 297 ret = krb5_init_context(&context); 298 if (ret) 299 context = NULL; 300 else 301 if(krb5_cc_default(context, &id) != 0) 302 id = NULL; 303#endif 304 305 if (verbose) 306 kafs_set_verbose(log_func, NULL); 307
|
205 num = 0; 206 for(i = 0; i < files.num_strings; i++){
| 308 num = 0; 309 for(i = 0; i < files.num_strings; i++){
|
207 afslog_file(context, id, files.strings[i]);
| 310 afslog_file(files.strings[i]);
|
208 num++;
| 311 num++;
|
209 free_getarg_strings (&files);
| |
210 }
| 312 }
|
| 313 free_getarg_strings (&files);
|
211 for(i = 0; i < cells.num_strings; i++){
| 314 for(i = 0; i < cells.num_strings; i++){
|
212 afslog_cell(context, id, cells.strings[i], 1);
| 315 afslog_cell(cells.strings[i], 1);
|
213 num++;
| 316 num++;
|
214 free_getarg_strings (&cells);
| |
215 }
| 317 }
|
| 318 free_getarg_strings (&cells);
|
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)
| 319 for(i = optind; i < argc; i++){ 320 num++; 321 if(strcmp(argv[i], ".") == 0 || 322 strcmp(argv[i], "..") == 0 || 323 strchr(argv[i], '/') || 324 access(argv[i], F_OK) == 0)
|
222 afslog_file(context, id, argv[i]);
| 325 afslog_file(argv[i]);
|
223 else
| 326 else
|
224 afslog_cell(context, id, argv[i], 1);
| 327 afslog_cell(argv[i], 1);
|
225 } 226 if(num == 0) {
| 328 } 329 if(num == 0) {
|
227 krb5_afslog(context, id, NULL, NULL);
| 330 if(do_afslog(NULL)) 331 failed++; 332 } else 333 for(p = cell_list; p; p = p->next) { 334 if(verbose) 335 warnx("Getting tokens for cell \"%s\"", p->cell); 336 if(do_afslog(p->cell)) 337 failed++;
|
228 } 229
| 338 } 339
|
230 return ret;
| 340 return failed;
|
231}
| 341}
|