c5add3317285a48df7d7d8a8ec79c9d0ad617007
[openafs.git] / src / aklog / aklog_main.c
1 /* 
2  * $Id$
3  *
4  * Copyright 1990,1991 by the Massachusetts Institute of Technology
5  * For distribution and copying rights, see the file "mit-copyright.h"
6  */
7
8 #if !defined(lint) && !defined(SABER)
9 static char *rcsid =
10         "$Id$";
11 #endif /* lint || SABER */
12
13 #include <afsconfig.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <sys/types.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #ifdef HAVE_MEMORY_H
25 #include <memory.h>
26 #endif
27 #include <errno.h>
28
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32 #ifndef WINDOWS
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <netdb.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <pwd.h>
40 #endif /* WINDOWS */
41
42 /* on AIX AFS has an unresolved reference to osi_audit. We will define
43  * it here as extern. It also trys to call the ntohl and htonl routines
44  * as routines rather then macros. We need a real routine here. 
45  * We do this before the ntohl and htonl macros are defined in net/in.h
46  */
47 int osi_audit()
48     { return(0);}
49
50 #if 0
51 #ifdef _AIX
52 u_long htonl(u_long x)
53     { return(x);}
54
55 u_long ntohl(u_long x)
56     { return(x);}
57 #endif
58
59 #include <netinet/in.h>
60 /* #include <krb.h> */
61 #endif /* 0 */
62
63 #include <krb5.h>
64
65 #ifdef WINDOWS
66
67 #include <afs/stds.h>
68 #include <afs/auth.h>
69 #include <rx/rxkad.h>
70 #include <afs/dirpath.h>
71
72 #else /* !WINDOWS */
73 #include <afs/stds.h>
74 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
75 #include <afs/com_err.h>
76 #endif
77
78 #include <afs/param.h>
79 #ifdef AFS_SUN5_ENV
80 #include <sys/ioccom.h>
81 #endif
82 #include <afs/auth.h>
83 #include <afs/cellconfig.h>
84 #include <afs/vice.h>
85 #include <afs/venus.h>
86 #include <afs/ptserver.h>
87 #include <afs/ptuser.h>
88 #include <afs/dirpath.h>
89 #endif /* WINDOWS */
90
91 #include "aklog.h"
92 #include "linked_list.h"
93
94 #define AFSKEY "afs"
95 #define AFSINST ""
96
97 #ifndef AFS_TRY_FULL_PRINC
98 #define AFS_TRY_FULL_PRINC 1
99 #endif /* AFS_TRY_FULL_PRINC */
100
101 #define AKLOG_SUCCESS 0
102 #define AKLOG_USAGE 1
103 #define AKLOG_SOMETHINGSWRONG 2
104 #define AKLOG_AFS 3
105 #define AKLOG_KERBEROS 4
106 #define AKLOG_TOKEN 5
107 #define AKLOG_BADPATH 6
108 #define AKLOG_MISC 7
109
110 #ifndef NULL
111 #define NULL 0
112 #endif
113
114 #ifndef TRUE
115 #define TRUE 1
116 #endif
117
118 #ifndef FALSE
119 #define FALSE 0
120 #endif
121
122 #ifndef MAXSYMLINKS
123 /* RedHat 4.x doesn't seem to define this */
124 #define MAXSYMLINKS     5
125 #endif
126
127 #define DIR '/'                 /* Character that divides directories */
128 #define DIRSTRING "/"           /* String form of above */
129 #define VOLMARKER ':'           /* Character separating cellname from mntpt */
130 #define VOLMARKERSTRING ":"     /* String form of above */
131
132 typedef struct {
133     char cell[BUFSIZ];
134     char realm[REALM_SZ];
135 } cellinfo_t;
136
137 struct afsconf_cell ak_cellconfig; /* General information about the cell */
138 static char linkedcell[MAXCELLCHARS+1];
139 static char linkedcell2[MAXCELLCHARS+1];
140 static krb5_ccache  _krb425_ccache = NULL;
141
142 #ifdef WINDOWS
143
144 /* libafsconf.dll */
145 extern long cm_GetRootCellName();
146 extern long cm_SearchCellFile();
147
148 static long cm_SearchCellFile_CallBack();
149
150 #else /* !WINDOWS */
151
152 /*
153  * Why doesn't AFS provide these prototypes?
154  */
155
156 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
157
158 /*
159  * Other prototypes
160  */
161
162 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *);
163 static int isdir(char *, unsigned char *);
164 static krb5_error_code get_credv5(krb5_context context, char *, char *,
165                                   char *, krb5_creds **);
166 static int get_user_realm(krb5_context, char *);
167
168 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
169
170 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
171 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
172 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
173 #define realm_data(c, p) krb5_princ_realm(c, p)->data
174 #define realm_len(c, p) krb5_princ_realm(c, p)->length
175
176 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
177
178 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
179 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
180 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
181 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
182 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
183
184 #else
185 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
186 #endif
187
188 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
189
190 #define get_cred_keydata(c) c->keyblock.contents
191 #define get_cred_keylen(c) c->keyblock.length
192 #define get_creds_enctype(c) c->keyblock.enctype
193
194 #elif defined(HAVE_KRB5_CREDS_SESSION)
195
196 #define get_cred_keydata(c) c->session.keyvalue.data
197 #define get_cred_keylen(c) c->session.keyvalue.length
198 #define get_creds_enctype(c) c->session.keytype
199
200 #else
201 #error "Must have either keyblock or session member of krb5_creds"
202 #endif
203
204 #if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
205 #define krb5_524_convert_creds krb524_convert_creds_kdc
206 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
207 #error "You must have one of krb5_524_convert_creds or krb524_convert_creds_kdc available"
208 #endif
209
210 #endif /* WINDOWS */
211
212 /*
213  * Provide a replacement for strerror if we don't have it
214  */
215
216 #ifndef HAVE_STRERROR
217 extern char *sys_errlist[];
218 #define strerror(x) sys_errlist[x]
219 #endif /* HAVE_STRERROR */
220
221 static char *progname = NULL;   /* Name of this program */
222 static int dflag = FALSE;       /* Give debugging information */
223 static int noauth = FALSE;      /* If true, don't try to get tokens */
224 static int zsubs = FALSE;       /* Are we keeping track of zephyr subs? */
225 static int hosts = FALSE;       /* Are we keeping track of hosts? */
226 static int noprdb = FALSE;      /* Skip resolving name to id? */
227 static int linked = FALSE;  /* try for both AFS nodes */
228 static int afssetpag = FALSE; /* setpag for AFS */
229 static int force = FALSE;       /* Bash identical tokens? */
230 static int do524 = FALSE;       /* Should we do 524 instead of rxkad2b? */
231 static linked_list zsublist;    /* List of zephyr subscriptions */
232 static linked_list hostlist;    /* List of host addresses */
233 static linked_list authedcells; /* List of cells already logged to */
234
235 /* ANL - CMU lifetime convert routine */
236 /* for K5.4.1 don't use this for now. Need to see if it is needed */
237 /* maybe needed in the krb524d module as well */
238 /* extern unsigned long krb_life_to_time(); */
239
240 static char *copy_cellinfo(cellinfo_t *cellinfo)
241 {
242     cellinfo_t *new_cellinfo;
243
244     if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
245         memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
246     
247     return ((char *)new_cellinfo);
248 }
249
250
251 static char *copy_string(char *string)    
252 {
253     char *new_string;
254
255     if ((new_string = (char *)calloc(strlen(string) + 1, sizeof(char))))
256         (void) strcpy(new_string, string);
257
258     return (new_string);
259 }
260
261
262 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
263 {
264     int status = AKLOG_SUCCESS;
265     struct afsconf_dir *configdir;
266
267     memset(local_cell, 0, sizeof(local_cell));
268     memset((char *)cellconfig, 0, sizeof(*cellconfig));
269
270 #ifndef WINDOWS
271
272     if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
273         fprintf(stderr, 
274                 "%s: can't get afs configuration (afsconf_Open(%s))\n",
275                 progname, AFSDIR_CLIENT_ETC_DIRPATH);
276         exit(AKLOG_AFS);
277     }
278
279     if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
280         fprintf(stderr, "%s: can't determine local cell.\n", progname);
281         exit(AKLOG_AFS);
282     }
283
284     if ((cell == NULL) || (cell[0] == 0))
285         cell = local_cell;
286
287         linkedcell[0] = '\0';
288     if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
289         fprintf(stderr, "%s: Can't get information about cell %s.\n",
290                 progname, cell);
291         status = AKLOG_AFS;
292     }
293         if (cellconfig->linkedCell) 
294                 strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
295
296     (void) afsconf_Close(configdir);
297
298 #else /* WINDOWS */
299     /*
300      * We'll try to mimic the GetCellInfo call here and fill in as much
301      * of the afsconf_cell structure as we can.
302      */
303     if (cm_GetRootCellName(local_cell)) {
304         fprintf(stderr, "%s: can't get local cellname\n", progname);
305         exit(AKLOG_AFS);
306     }
307
308     if ((cell == NULL) || (cell[0] == 0))
309         cell = local_cell;
310
311     strcpy(cellconfig->name, cell);
312
313     /* No way of figuring this out as far as I can tell */
314     linkedcell[0] = '\0';
315
316     /* Initialize server info */
317     cellconfig->numServers = 0;
318     cellconfig->hostName[0][0] = "\0";
319
320     /*
321      * Get servers of cell. cm_SearchCellFile_CallBack() gets call with
322      * each server.
323      */
324     status = (int) cm_SearchCellFile(cell, NULL, &cm_SearchCellFile_CallBack,
325                                      cellconfig /* rock */);
326
327     switch(status) {
328     case 0:
329         break;
330
331     case -1:
332         fprintf(stderr, "%s: GetWindowsDirectory() failed.\n", progname);
333         break;
334
335     case -2:
336         fprintf(stderr, "%s: Couldn't open afsdcells.ini for reading\n",
337                 progname);
338         break;
339
340     case -3:
341         fprintf(stderr, "%s: Couldn't find any servers for cell %s\n",
342                 progname, cell);
343         break;
344
345     case -4:
346         fprintf(stderr, "%s: Badly formatted line in afsdcells.ini (does not begin with a \">\" or contain \"#\"\n",
347                 progname);
348         break;
349
350     default:
351         fprintf(stderr, "%s cm_SearchCellFile returned unknown error %d\n",
352                 status);
353     }
354
355     if (status) {
356         exit(AKLOG_AFS);
357     }
358
359     status = AKLOG_SUCCESS;
360
361     
362 #endif /* WINDOWS */
363
364     return(status);
365 }
366
367
368 #ifdef WINDOWS
369 /*
370  * Callback function for cm_SearchCellFile() in get_cellconfig() above.
371  * This function gets called once for each server that is found for the cell.
372  */
373 static long
374 cm_SearchCellFile_CallBack(void *rock /* cellconfig */,
375                            struct sockaddr_in *addr, /* Not used */
376                            char *server)
377 {
378     struct afsconf_cell *cellconfig = rock;
379
380
381     /*
382      * Save server name and increment count of servers
383      */
384     strcpy(cellconfig->hostName[cellconfig->numServers++], server);
385     
386     return (long) 0;
387 }
388
389     
390 #endif /* WINDOWS */
391
392
393 /* 
394  * Log to a cell.  If the cell has already been logged to, return without
395  * doing anything.  Otherwise, log to it and mark that it has been logged
396  * to.
397  */
398 static int auth_to_cell(krb5_context context, char *cell, char *realm)
399 {
400     int status = AKLOG_SUCCESS;
401     char username[BUFSIZ];      /* To hold client username structure */
402     afs_int32 viceId;           /* AFS uid of user */
403
404     char name[ANAME_SZ];        /* Name of afs key */
405     char primary_instance[INST_SZ];     /* Instance of afs key */
406     char secondary_instance[INST_SZ];   /* Backup instance to try */
407     int try_secondary = 0;              /* Flag to indicate if we try second */
408     char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
409     char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
410     char local_cell[MAXCELLCHARS+1];
411     char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
412     static char lastcell[MAXCELLCHARS+1] = { 0 };
413 #ifndef WINDOWS
414     static char confname[512] = { 0 };
415 #endif
416     krb5_creds *v5cred = NULL;
417     struct ktc_principal aserver;
418     struct ktc_principal aclient;
419     struct ktc_token atoken, btoken;
420
421 #ifdef ALLOW_REGISTER
422     afs_int32 id;
423 #endif /* ALLOW_REGISTER */
424
425     memset(name, 0, sizeof(name));
426     memset(primary_instance, 0, sizeof(primary_instance));
427     memset(secondary_instance, 0, sizeof(secondary_instance));
428     memset(realm_of_user, 0, sizeof(realm_of_user));
429     memset(realm_of_cell, 0, sizeof(realm_of_cell));
430
431 #ifndef WINDOWS
432     if (confname[0] == '\0') {
433         strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
434         confname[sizeof(confname) - 2] = '\0';
435     }
436 #endif /* WINDOWS */
437
438     /* NULL or empty cell returns information on local cell */
439     if ((status = get_cellconfig(cell, &ak_cellconfig,
440                          local_cell, linkedcell)))
441         return(status);
442
443     strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
444     cell_to_use[MAXCELLCHARS] = 0;
445
446     if (ll_string(&authedcells, ll_s_check, cell_to_use)) {
447         if (dflag) {
448             printf("Already authenticated to %s (or tried to)\n", 
449                    cell_to_use);
450         }
451         return(AKLOG_SUCCESS);
452     }
453
454     /* 
455      * Record that we have attempted to log to this cell.  We do this
456      * before we try rather than after so that we will not try
457      * and fail repeatedly for one cell.
458      */
459     (void)ll_string(&authedcells, ll_s_add, cell_to_use);
460
461     /* 
462      * Record this cell in the list of zephyr subscriptions.  We may
463      * want zephyr subscriptions even if authentication fails.
464      * If this is done after we attempt to get tokens, aklog -zsubs
465      * can return something different depending on whether or not we
466      * are in -noauth mode.
467      */
468     if (ll_string(&zsublist, ll_s_add, cell_to_use) == LL_FAILURE) {
469         fprintf(stderr, 
470                 "%s: failure adding cell %s to zephyr subscriptions list.\n",
471                 progname, cell_to_use);
472         exit(AKLOG_MISC);
473     }
474     if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
475         fprintf(stderr, 
476                 "%s: failure adding cell %s to zephyr subscriptions list.\n",
477                 progname, local_cell);
478         exit(AKLOG_MISC);
479     }
480
481     if (!noauth) {
482         if (dflag) {
483             printf("Authenticating to cell %s (server %s).\n",
484                    cell_to_use, ak_cellconfig.hostName[0]);
485         }
486
487         /*
488          * Find out which realm we're supposed to authenticate to.  If one
489          * is not included, use the kerberos realm found in the credentials
490          * cache.
491          */
492
493         if (realm && realm[0]) {
494             strcpy(realm_of_cell, realm);
495             if (dflag) {
496                 printf("We were told to authenticate to realm %s.\n", realm);
497             }
498         }
499         else {
500             char *realm = afs_realm_of_cell(context, &ak_cellconfig);
501
502             if (!realm) {
503                 fprintf(stderr, 
504                         "%s: Couldn't figure out realm for cell %s.\n",
505                         progname, cell_to_use);
506                 exit(AKLOG_MISC);
507             }
508
509             strcpy(realm_of_cell, realm);
510
511             if (dflag) {
512                 printf("We've deduced that we need to authenticate to"
513                        " realm %s.\n", realm_of_cell);
514             }
515         }
516
517         /* We use the afs.<cellname> convention here... 
518          *
519          * Doug Engert's original code had principals of the form:
520          *
521          * "afsx/cell@realm"
522          *
523          * in the KDC, so the name wouldn't conflict with DFS.  Since we're
524          * not using DFS, I changed it just to look for the following
525          * principals:
526          *
527          * afs/<cell>@<realm>
528          * afs@<realm>
529          *
530          * Because people are transitioning from afs@realm to afs/cell,
531          * we configure things so that if the first one isn't found, we
532          * try the second one.  You can select which one you prefer with
533          * a configure option.
534          */
535
536         strcpy(name, AFSKEY);
537
538         if (AFS_TRY_FULL_PRINC || strcasecmp(cell_to_use, realm_of_cell) != 0) {
539             strncpy(primary_instance, cell_to_use, sizeof(primary_instance));
540             primary_instance[sizeof(primary_instance)-1] = '\0';
541             if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
542                 try_secondary = 1;
543                 secondary_instance[0] = '\0';
544             }
545         } else {
546             primary_instance[0] = '\0';
547             try_secondary = 1;
548             strncpy(secondary_instance, cell_to_use,
549                     sizeof(secondary_instance));
550             secondary_instance[sizeof(secondary_instance)-1] = '\0';
551         }
552
553         /* 
554          * Extract the session key from the ticket file and hand-frob an
555          * afs style authenticator.
556          */
557
558         /*
559          * Try to obtain AFS tickets.  Because there are two valid service
560          * names, we will try both, but trying the more specific first.
561          *
562          *      afs/<cell>@<realm> i.e. allow for single name with "."
563          *      afs@<realm>
564          */
565
566         if (dflag) {
567             printf("Getting tickets: %s/%s@%s\n", name,
568                    primary_instance, realm_of_cell);
569         }
570
571         status = get_credv5(context, name, primary_instance, realm_of_cell,
572                             &v5cred);
573
574         if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
575             if (try_secondary) {
576                 if (dflag) {
577                     printf("Principal not found, trying alternate "
578                            "service name: %s/%s@%s\n", name,
579                             secondary_instance, realm_of_cell);
580                 }
581                 status = get_credv5(context, name, secondary_instance,
582                                     realm_of_cell, &v5cred);
583             }
584         }
585
586         if (status) {
587             if (dflag) {
588                 printf("Kerberos error code returned by get_cred: %d\n",
589                         status);
590             }
591             fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
592                     progname, cell_to_use);
593                 com_err(progname, status, "while getting AFS tickets");
594             return(AKLOG_KERBEROS);
595         }
596
597         strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
598         strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
599         strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
600
601         /*
602          * The default is to use rxkad2b, which means we put in a full
603          * V5 ticket.  If the user specifies -524, we talk to the
604          * 524 ticket converter.
605          */
606
607         if (! do524) {
608             char *p;
609             int len;
610
611             if (dflag)
612                 printf("Using Kerberos V5 ticket natively\n");
613
614             len = min(get_princ_len(context, v5cred->client, 0),
615                       second_comp(context, v5cred->client) ?
616                                         MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
617             strncpy(username, get_princ_str(context, v5cred->client, 0), len);
618             username[len] = '\0';
619
620             if (second_comp(context, v5cred->client)) {
621                 strcat(username, ".");
622                 p = username + strlen(username);
623                 len = min(get_princ_len(context, v5cred->client, 1),
624                           MAXKTCNAMELEN - strlen(username) - 1);
625                 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
626                 p[len] = '\0';
627             }
628
629             memset(&atoken, 0, sizeof(atoken));
630             atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
631             atoken.startTime = v5cred->times.starttime;;
632             atoken.endTime = v5cred->times.endtime;
633             memcpy(&atoken.sessionKey, get_cred_keydata(v5cred),
634                    get_cred_keylen(v5cred));
635             atoken.ticketLen = v5cred->ticket.length;
636             memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
637         } else {
638             CREDENTIALS cred;
639
640             if (dflag)
641                 printf("Using Kerberos 524 translator service\n");
642
643             status = krb5_524_convert_creds(context, v5cred, &cred);
644
645             if (status) {
646                 com_err(progname, status, "while converting tickets "
647                         "to Kerberos V4 format");
648                 return(AKLOG_KERBEROS);
649             }
650
651             strcpy (username, cred.pname);
652             if (cred.pinst[0]) {
653                 strcat (username, ".");
654                 strcat (username, cred.pinst);
655             }
656
657             atoken.kvno = cred.kvno;
658             atoken.startTime = cred.issue_date;
659             /*
660              * It seems silly to go through a bunch of contortions to
661              * extract the expiration time, when the v5 credentials already
662              * has the exact time!  Let's use that instead.
663              *
664              * Note that this isn't a security hole, as the expiration time
665              * is also contained in the encrypted token
666              */
667             atoken.endTime = v5cred->times.endtime;
668             memcpy(&atoken.sessionKey, cred.session, 8);
669             atoken.ticketLen = cred.ticket_st.length;
670             memcpy(atoken.ticket, cred.ticket_st.dat, atoken.ticketLen);
671         }
672         
673         if (!force &&
674             !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
675             atoken.kvno == btoken.kvno &&
676             atoken.ticketLen == btoken.ticketLen &&
677             !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
678             !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen)) {
679
680             if (dflag) {
681                 printf("Identical tokens already exist; skipping.\n");
682             }
683             return 0;
684         }
685
686 #ifdef FORCE_NOPRDB
687         noprdb = 1;
688 #endif
689
690 #ifndef WINDOWS
691         if (noprdb) {
692 #endif
693             if (dflag) {
694                 printf("Not resolving name %s to id (-noprdb set)\n",
695                         username);
696             }
697 #ifndef WINDOWS
698         }
699         else {
700             if ((status = get_user_realm(context, realm_of_user))) {
701                 fprintf(stderr, "%s: Couldn't determine realm of user:)",
702                         progname);
703                 com_err(progname, status, " while getting realm");
704                 return(AKLOG_KERBEROS);
705             }
706             if (strcmp(realm_of_user, realm_of_cell)) {
707                 strcat(username, "@");
708                 strcat(username, realm_of_user);
709             }
710
711             if (dflag) {
712                 printf("About to resolve name %s to id in cell %s.\n", 
713                         username, aserver.cell);
714             }
715
716             strcpy(lastcell, aserver.cell);
717
718             if (!pr_Initialize (0, confname, aserver.cell))
719                     status = pr_SNameToId (username, &viceId);
720             
721             if (dflag) {
722                 if (status) 
723                     printf("Error %d\n", status);
724                 else
725                     printf("Id %d\n", (int) viceId);
726             }
727             
728                 /*
729                  * This is a crock, but it is Transarc's crock, so
730                  * we have to play along in order to get the
731                  * functionality.  The way the afs id is stored is
732                  * as a string in the username field of the token.
733                  * Contrary to what you may think by looking at
734                  * the code for tokens, this hack (AFS ID %d) will
735                  * not work if you change %d to something else.
736                  */
737
738                 /*
739                  * This code is taken from cklog -- it lets people
740                  * automatically register with the ptserver in foreign cells
741                  */
742
743 #ifdef ALLOW_REGISTER
744         if (status == 0) {
745             if (viceId != ANONYMOUSID) {
746 #else /* ALLOW_REGISTER */
747             if ((status == 0) && (viceId != ANONYMOUSID))
748 #endif /* ALLOW_REGISTER */
749                 sprintf (username, "AFS ID %d", (int) viceId);
750 #ifdef ALLOW_REGISTER
751             } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
752                 if (dflag) {
753                     printf("doing first-time registration of %s "
754                             "at %s\n", username, cell_to_use);
755                 }
756                 id = 0;
757                 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
758                 strcpy(aclient.instance, "");
759                 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
760                 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
761                     fprintf(stderr, "%s: unable to obtain tokens for cell %s "
762                             "(status: %d).\n", progname, cell_to_use, status);
763                     status = AKLOG_TOKEN;
764                 }
765
766                 /*
767                  * In case you're wondering, we don't need to change the
768                  * filename here because we're still connecting to the
769                  * same cell -- we're just using a different authentication
770                  * level
771                  */
772
773                 if ((status = pr_Initialize(1L, confname, aserver.cell))) {
774                     printf("Error %d\n", status);
775                 }
776
777                 if ((status = pr_CreateUser(username, &id))) {
778                     fprintf(stderr, "%s: %s so unable to create remote PTS "
779                             "user %s in cell %s (status: %d).\n", progname,
780                             error_message(status), username, cell_to_use,
781                             status);
782                 } else {
783                     printf("created cross-cell entry for %s at %s\n",
784                            username, cell_to_use);
785                     sprintf(username, "AFS ID %d", (int) id);
786                 }
787             }
788         }
789 #endif /* ALLOW_REGISTER */
790
791         }
792 #endif /* !WINDOWS */
793
794         if (dflag) {
795             fprintf(stdout, "Set username to %s\n", username);
796         }
797
798         /* Reset the "aclient" structure before we call ktc_SetToken.
799          * This structure was first set by the ktc_GetToken call when
800          * we were comparing whether identical tokens already existed.
801          */
802         strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
803         strcpy(aclient.instance, "");
804         strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
805
806         if (dflag) {
807             printf("Setting tokens. %s / %s @ %s \n",
808                     aclient.name, aclient.instance, aclient.cell );
809         }
810         /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before 
811          * this routine, it will not add the token. It is not clear what 
812          * is going on here! So we will do the following operation
813          */
814         write(2,"",0); /* dummy write */
815 #ifndef WINDOWS
816         if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
817             fprintf(stderr, 
818                     "%s: unable to obtain tokens for cell %s (status: %d).\n",
819                     progname, cell_to_use, status);
820             status = AKLOG_TOKEN;
821         }
822 #else /* WINDOWS */
823         /* Note switched 2nd and 3rd args */
824         if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
825             switch(status) {
826             case KTC_INVAL:
827                 fprintf(stderr, "%s: Bad ticket length", progname);
828                 break;
829             case KTC_PIOCTLFAIL:
830                 fprintf(stderr, "%s: Unknown error contacting AFS service",
831                         progname);
832                 break;
833             case KTC_NOCELL:
834                 fprintf(stderr, "%s: Cell name (%s) not recognized by AFS service",
835                         progname, realm_of_cell);
836                 break;
837             case KTC_NOCM:
838                 fprintf(stderr, "%s: AFS service is unavailable", progname);
839                 break;
840             default:
841                 fprintf(stderr, "%s: Undocumented error (%d) contacting AFS service", progname, status);
842                 break;  
843             }
844             status = AKLOG_TOKEN;           
845         }
846 #endif /* !WINDOWS */
847     }
848     else
849         if (dflag) {
850             printf("Noauth mode; not authenticating.\n");
851         }
852         
853     return(status);
854 }
855
856 #ifndef WINDOWS /* struct ViceIoctl missing */
857
858 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
859 {
860 #ifdef AFS_SUN_ENV
861         char V ='V'; /* AFS has problem on Sun with pioctl */
862 #endif
863     char our_file[MAXPATHLEN + 1];
864     char *parent_dir;
865     char *last_component;
866     struct ViceIoctl vio;
867     char cellname[BUFSIZ];
868
869     memset(our_file, 0, sizeof(our_file));
870     strcpy(our_file, file);
871
872     if ((last_component = strrchr(our_file, DIR))) {
873         *last_component++ = 0;
874         parent_dir = our_file;
875     }
876     else {
877         last_component = our_file;
878         parent_dir = ".";
879     }    
880     
881     memset(cellname, 0, sizeof(cellname));
882
883     vio.in = last_component;
884     vio.in_size = strlen(last_component)+1;
885     vio.out_size = size;
886     vio.out = mountpoint;
887
888     if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
889         if (strchr(mountpoint, VOLMARKER) == NULL) {
890             vio.in = file;
891             vio.in_size = strlen(file) + 1;
892             vio.out_size = sizeof(cellname);
893             vio.out = cellname;
894             
895             if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
896                 strcat(cellname, VOLMARKERSTRING);
897                 strcat(cellname, mountpoint + 1);
898                 memset(mountpoint + 1, 0, size - 1);
899                 strcpy(mountpoint + 1, cellname);
900             }
901         }
902         return(TRUE);
903     }
904     else
905         return(FALSE);
906 }
907
908 /* 
909  * This routine each time it is called returns the next directory 
910  * down a pathname.  It resolves all symbolic links.  The first time
911  * it is called, it should be called with the name of the path
912  * to be descended.  After that, it should be called with the arguemnt
913  * NULL.
914  */
915 static char *next_path(char *origpath)
916 {
917     static char path[MAXPATHLEN + 1];
918     static char pathtocheck[MAXPATHLEN + 1];
919
920     int link = FALSE;           /* Is this a symbolic link? */
921     char linkbuf[MAXPATHLEN + 1];
922     char tmpbuf[MAXPATHLEN + 1];
923
924     static char *last_comp;     /* last component of directory name */
925     static char *elast_comp;    /* End of last component */
926     char *t;
927     int len;
928     
929     static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
930     
931     /* If we are given something for origpath, we are initializing only. */
932     if (origpath) {
933         memset(path, 0, sizeof(path));
934         memset(pathtocheck, 0, sizeof(pathtocheck));
935         strcpy(path, origpath);
936         last_comp = path;
937         symlinkcount = 0;
938         return(NULL);
939     }
940
941     /* We were not given origpath; find then next path to check */
942     
943     /* If we've gotten all the way through already, return NULL */
944     if (last_comp == NULL)
945         return(NULL);
946
947     do {
948         while (*last_comp == DIR)
949             strncat(pathtocheck, last_comp++, 1);
950         len = (elast_comp = strchr(last_comp, DIR)) 
951             ? elast_comp - last_comp : strlen(last_comp);
952         strncat(pathtocheck, last_comp, len);
953         memset(linkbuf, 0, sizeof(linkbuf));
954         if (link = (readlink(pathtocheck, linkbuf, 
955                                     sizeof(linkbuf)) > 0)) {
956             if (++symlinkcount > MAXSYMLINKS) {
957                 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
958                 exit(AKLOG_BADPATH);
959             }
960             memset(tmpbuf, 0, sizeof(tmpbuf));
961             if (elast_comp)
962                 strcpy(tmpbuf, elast_comp);
963             if (linkbuf[0] == DIR) {
964                 /* 
965                  * If this is a symbolic link to an absolute path, 
966                  * replace what we have by the absolute path.
967                  */
968                 memset(path, 0, strlen(path));
969                 memcpy(path, linkbuf, sizeof(linkbuf));
970                 strcat(path, tmpbuf);
971                 last_comp = path;
972                 elast_comp = NULL;
973                 memset(pathtocheck, 0, sizeof(pathtocheck));
974             }
975             else {
976                 /* 
977                  * If this is a symbolic link to a relative path, 
978                  * replace only the last component with the link name.
979                  */
980                 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
981                 strcat(path, tmpbuf);
982                 elast_comp = NULL;
983                 if ((t = strrchr(pathtocheck, DIR))) {
984                     t++;
985                     memset(t, 0, strlen(t));
986                 }
987                 else
988                     memset(pathtocheck, 0, sizeof(pathtocheck));
989             }
990         }
991         else
992             last_comp = elast_comp;
993     }
994     while(link);
995
996     return(pathtocheck);
997 }
998
999 #endif /* WINDOWS */
1000
1001 #if 0
1002 /*****************************************/
1003 int dee_gettokens()
1004 {
1005 #ifdef AFS_SUN_ENV
1006         char V = 'V'; /* AFS has problem on SunOS */
1007 #endif
1008    struct ViceIoctl vio;
1009    char outbuf[BUFSIZ];
1010    long ind;
1011    int fd;
1012
1013    memset(outbuf, 0, sizeof(outbuf));
1014
1015    vio.out_size = sizeof(outbuf);
1016    vio.in_size = sizeof(ind);
1017    vio.out = outbuf;
1018    vio.in = &ind;
1019
1020    ind = 0;
1021    fd = open("dee.tok",O_WRONLY);
1022    while(!pioctl(0,VIOCGETTOK,&vio,0)) {
1023         write(fd,&outbuf,sizeof(outbuf)); 
1024        ind++;
1025    }
1026    close(fd);
1027 }
1028 /*****************************************/
1029 #endif
1030
1031 #ifndef WINDOWS /* struct ViceIoctl missing */
1032
1033 static void add_hosts(char *file)
1034 {
1035 #ifdef AFS_SUN_ENV
1036         char V = 'V'; /* AFS has problem on SunOS */
1037 #endif
1038     struct ViceIoctl vio;
1039     char outbuf[BUFSIZ];
1040     long *phosts;
1041     int i;
1042     struct hostent *hp;
1043     struct in_addr in;
1044     
1045     memset(outbuf, 0, sizeof(outbuf));
1046
1047     vio.out_size = sizeof(outbuf);
1048     vio.in_size = 0;
1049     vio.out = outbuf;
1050
1051     if (dflag) {
1052         printf("Getting list of hosts for %s\n", file);
1053     }
1054     /* Don't worry about errors. */
1055     if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1056         phosts = (long *) outbuf;
1057
1058         /*
1059          * Lists hosts that we care about.  If ALLHOSTS is defined,
1060          * then all hosts that you ever may possible go through are
1061          * included in this list.  If not, then only hosts that are
1062          * the only ones appear.  That is, if a volume you must use
1063          * is replaced on only one server, that server is included.
1064          * If it is replicated on many servers, then none are included.
1065          * This is not perfect, but the result is that people don't
1066          * get subscribed to a lot of instances of FILSRV that they
1067          * probably won't need which reduces the instances of 
1068          * people getting messages that don't apply to them.
1069          */
1070 #ifndef ALLHOSTS
1071         if (phosts[1] != '\0')
1072             return;
1073 #endif
1074         for (i = 0; phosts[i]; i++) {
1075             if (hosts) {
1076                 in.s_addr = phosts[i];
1077                 if (dflag) {
1078                     printf("Got host %s\n", inet_ntoa(in));
1079                 }
1080                 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1081             }
1082             if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1083                 if (dflag) {
1084                     printf("Got host %s\n", hp->h_name);
1085                 }
1086                 ll_string(&zsublist, ll_s_add, hp->h_name);
1087             }
1088         }
1089     }
1090 }
1091
1092 #endif /* WINDOWS */
1093
1094 #ifndef WINDOWS /* next_path(), get_afs_mountpoint() */
1095
1096 /*
1097  * This routine descends through a path to a directory, logging to 
1098  * every cell it encounters along the way.
1099  */
1100 static int auth_to_path(krb5_context context, char *path)
1101 {
1102     int status = AKLOG_SUCCESS;
1103     int auth_to_cell_status = AKLOG_SUCCESS;
1104
1105     char *nextpath;
1106     char pathtocheck[MAXPATHLEN + 1];
1107     char mountpoint[MAXPATHLEN + 1];
1108
1109     char *cell;
1110     char *endofcell;
1111
1112     u_char isdirectory;
1113
1114     /* Initialize */
1115     if (path[0] == DIR)
1116         strcpy(pathtocheck, path);
1117     else {
1118         if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1119             fprintf(stderr, "Unable to find current working directory:\n");
1120             fprintf(stderr, "%s\n", pathtocheck);
1121             fprintf(stderr, "Try an absolute pathname.\n");
1122             exit(AKLOG_BADPATH);
1123         }
1124         else {
1125             strcat(pathtocheck, DIRSTRING);
1126             strcat(pathtocheck, path);
1127         }
1128     }
1129     next_path(pathtocheck);
1130
1131     /* Go on to the next level down the path */
1132     while ((nextpath = next_path(NULL))) {
1133         strcpy(pathtocheck, nextpath);
1134         if (dflag) {
1135             printf("Checking directory %s\n", pathtocheck);
1136         }
1137         /* 
1138          * If this is an afs mountpoint, determine what cell from 
1139          * the mountpoint name which is of the form 
1140          * #cellname:volumename or %cellname:volumename.
1141          */
1142         if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1143             /* skip over the '#' or '%' */
1144             cell = mountpoint + 1;
1145             /* Add this (cell:volumename) to the list of zsubs */
1146             if (zsubs)
1147                 ll_string(&zsublist, ll_s_add, cell);
1148             if (zsubs || hosts)
1149                 add_hosts(pathtocheck);
1150             if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1151                 *endofcell = '\0';
1152                 if ((auth_to_cell_status = auth_to_cell(context, cell, NULL))) {
1153                     if (status == AKLOG_SUCCESS)
1154                         status = auth_to_cell_status;
1155                     else if (status != auth_to_cell_status)
1156                         status = AKLOG_SOMETHINGSWRONG;
1157                 }
1158             }
1159         }
1160         else {
1161             if (isdir(pathtocheck, &isdirectory) < 0) {
1162                 /*
1163                  * If we've logged and still can't stat, there's
1164                  * a problem... 
1165                  */
1166                 fprintf(stderr, "%s: stat(%s): %s\n", progname, 
1167                         pathtocheck, strerror(errno));
1168                 return(AKLOG_BADPATH);
1169             }
1170             else if (! isdirectory) {
1171                 /* Allow only directories */
1172                 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1173                         strerror(ENOTDIR));
1174                 return(AKLOG_BADPATH);
1175             }
1176         }
1177     }
1178     
1179
1180     return(status);
1181 }
1182
1183 #endif /* WINDOWS */
1184
1185
1186 /* Print usage message and exit */
1187 static void usage(void)
1188 {
1189     fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1190             "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1191             "[[-p | -path] pathname]\n",
1192             "    [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1193             "    [-linked] [-524]\n");
1194     fprintf(stderr, "    -d gives debugging information.\n");
1195     fprintf(stderr, "    krb_realm is the kerberos realm of a cell.\n");
1196     fprintf(stderr, "    pathname is the name of a directory to which ");
1197     fprintf(stderr, "you wish to authenticate.\n");
1198     fprintf(stderr, "    -zsubs gives zephyr subscription information.\n");
1199     fprintf(stderr, "    -hosts gives host address information.\n");
1200     fprintf(stderr, "    -noauth does not attempt to get tokens.\n");
1201     fprintf(stderr, "    -noprdb means don't try to determine AFS ID.\n");
1202     fprintf(stderr, "    -force means replace identical tickets. \n");
1203     fprintf(stderr, "    -linked means if AFS node is linked, try both. \n");
1204     fprintf(stderr, "    -setpag set the AFS process authentication group.\n");
1205     fprintf(stderr, "    -524 means use the 524 converter instead of V5 directly\n");
1206     fprintf(stderr, "    No commandline arguments means ");
1207     fprintf(stderr, "authenticate to the local cell.\n");
1208     fprintf(stderr, "\n");
1209     exit(AKLOG_USAGE);
1210 }
1211
1212 void aklog(int argc, char *argv[])
1213 {
1214         krb5_context context;
1215     int status = AKLOG_SUCCESS;
1216     int i;
1217     int somethingswrong = FALSE;
1218
1219     cellinfo_t cellinfo;
1220
1221     extern char *progname;      /* Name of this program */
1222
1223     extern int dflag;           /* Debug mode */
1224
1225     int cmode = FALSE;          /* Cellname mode */
1226     int pmode = FALSE;          /* Path name mode */
1227
1228     char realm[REALM_SZ];       /* Kerberos realm of afs server */
1229     char cell[BUFSIZ];          /* Cell to which we are authenticating */
1230     char path[MAXPATHLEN + 1];          /* Path length for path mode */
1231
1232     linked_list cells;          /* List of cells to log to */
1233     linked_list paths;          /* List of paths to log to */
1234     ll_node *cur_node;
1235
1236     memset(&cellinfo, 0, sizeof(cellinfo));
1237
1238     memset(realm, 0, sizeof(realm));
1239     memset(cell, 0, sizeof(cell));
1240     memset(path, 0, sizeof(path));
1241
1242     ll_init(&cells);
1243     ll_init(&paths);
1244
1245     ll_init(&zsublist);
1246     ll_init(&hostlist);
1247
1248     /* Store the program name here for error messages */
1249     if ((progname = strrchr(argv[0], DIR)))
1250         progname++;
1251     else
1252         progname = argv[0];
1253
1254     krb5_init_context(&context);
1255 #ifndef WINDOWS
1256         initialize_ktc_error_table ();
1257 #endif
1258
1259     /* Initialize list of cells to which we have authenticated */
1260     (void)ll_init(&authedcells);
1261
1262     /* Parse commandline arguments and make list of what to do. */
1263     for (i = 1; i < argc; i++) {
1264         if (strcmp(argv[i], "-d") == 0)
1265             dflag++;
1266         else if (strcmp(argv[i], "-noauth") == 0) 
1267             noauth++;
1268         else if (strcmp(argv[i], "-zsubs") == 0)
1269             zsubs++;
1270         else if (strcmp(argv[i], "-hosts") == 0)
1271             hosts++;
1272         else if (strcmp(argv[i], "-noprdb") == 0)
1273             noprdb++;
1274         else if (strcmp(argv[i], "-linked") == 0)
1275                 linked++;
1276         else if (strcmp(argv[i], "-force") == 0)
1277             force++;
1278         else if (strcmp(argv[i], "-524") == 0)
1279             do524++;
1280     else if (strcmp(argv[i], "-setpag") == 0)
1281             afssetpag++;
1282         else if (((strcmp(argv[i], "-cell") == 0) ||
1283                   (strcmp(argv[i], "-c") == 0)) && !pmode)
1284             if (++i < argc) {
1285                 cmode++;
1286                 strcpy(cell, argv[i]);
1287             }
1288             else
1289                 usage();
1290         else if (((strcmp(argv[i], "-path") == 0) ||
1291                   (strcmp(argv[i], "-p") == 0)) && !cmode)
1292 #ifndef WINDOWS
1293             if (++i < argc) {
1294                 pmode++;
1295                 strcpy(path, argv[i]);
1296             }
1297             else
1298                 usage();
1299 #else /* WINDOWS */
1300         {
1301             fprintf(stderr, "%s: path mode not supported.\n", progname);
1302             exit(AKLOG_MISC);
1303         }
1304 #endif /* WINDOWS */
1305             
1306         else if (argv[i][0] == '-')
1307             usage();
1308         else if (!pmode && !cmode) {
1309             if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1310                 (strcmp(argv[i], "..") == 0)) {
1311 #ifndef WINDOWS
1312                 pmode++;
1313                 strcpy(path, argv[i]);
1314 #else /* WINDOWS */
1315                 fprintf(stderr, "%s: path mode not supported.\n", progname);
1316                 exit(AKLOG_MISC);
1317 #endif /* WINDOWS */
1318             }
1319             else { 
1320                 cmode++;
1321                 strcpy(cell, argv[i]);
1322             }
1323         }
1324         else
1325             usage();
1326
1327         if (cmode) {
1328             if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1329                 i+=2;
1330                 if (i < argc)
1331                     strcpy(realm, argv[i]);
1332                 else
1333                     usage();
1334             }
1335             /* Add this cell to list of cells */
1336             strcpy(cellinfo.cell, cell);
1337             strcpy(cellinfo.realm, realm);
1338             if ((cur_node = ll_add_node(&cells, ll_tail))) {
1339                 char *new_cellinfo;
1340                 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1341                     ll_add_data(cur_node, new_cellinfo);
1342                 else {
1343                     fprintf(stderr, 
1344                             "%s: failure copying cellinfo.\n", progname);
1345                     exit(AKLOG_MISC);
1346                 }
1347             }
1348             else {
1349                 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1350                         progname);
1351                 exit(AKLOG_MISC);
1352             }
1353             memset(&cellinfo, 0, sizeof(cellinfo));
1354             cmode = FALSE;
1355             memset(cell, 0, sizeof(cell));
1356             memset(realm, 0, sizeof(realm));
1357         }
1358 #ifndef WINDOWS
1359         else if (pmode) {
1360             /* Add this path to list of paths */
1361             if ((cur_node = ll_add_node(&paths, ll_tail))) {
1362                 char *new_path; 
1363                 if ((new_path = copy_string(path)))
1364                     ll_add_data(cur_node, new_path);
1365                 else {
1366                     fprintf(stderr, "%s: failure copying path name.\n",
1367                             progname);
1368                     exit(AKLOG_MISC);
1369                 }
1370             }
1371             else {
1372                 fprintf(stderr, "%s: failure adding path to paths list.\n",
1373                         progname);
1374                 exit(AKLOG_MISC);
1375             }
1376             pmode = FALSE;
1377             memset(path, 0, sizeof(path));
1378         }
1379 #endif /* WINDOWS */
1380     }
1381
1382     /*
1383      * The code that _used_ to be here called setpag().  When you think
1384      * about this, doing this makes no sense!  setpag() allocates a PAG
1385      * only for the current process, so the token installed would have
1386      * not be usable in the parent!  Since ktc_SetToken() now takes a
1387      * 4th argument to control whether or not we're going to allocate
1388      * a PAG (and since when you do it _that_ way, it modifies the cred
1389      * structure of your parent)), why don't we use that instead?
1390      */
1391
1392 #if 0
1393     if (afssetpag) {
1394            status = setpag();
1395            if (dflag) { 
1396              int i,j;
1397                  int gidsetlen = 50;
1398          int gidset[50];
1399
1400                  printf("setpag %d\n",status);
1401              j = getgroups(gidsetlen,gidset);
1402          printf("Groups(%d):",j);
1403          for (i = 0; i<j; i++) {
1404            printf("%d",gidset[i]);
1405            if((i+1)<j) printf(",");
1406          }
1407          printf("\n");
1408            }
1409         }
1410 #endif
1411     /* If nothing was given, log to the local cell. */
1412     if ((cells.nelements + paths.nelements) == 0) {
1413                 struct passwd *pwd;
1414
1415                 status = auth_to_cell(context, NULL, NULL);
1416         
1417                 /* If this cell is linked to a DCE cell, and user 
1418                  * requested -linked, get tokens for both 
1419                  * This is very usefull when the AFS cell is linked to a DFS 
1420                  * cell and this system does not also have DFS. 
1421                  */
1422
1423                 if (!status && linked && linkedcell[0]) {
1424                                 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1425                             if (dflag) {
1426                                 printf("Linked cell: %s\n", linkedcell);
1427                             }
1428                                 status = auth_to_cell(context, linkedcell2, NULL);
1429                 }
1430
1431 #ifndef WINDOWS
1432                 /*
1433                  * Local hack - if the person has a file in their home
1434                  * directory called ".xlog", read that for a list of
1435                  * extra cells to authenticate to
1436                  */
1437
1438                 if ((pwd = getpwuid(getuid())) != NULL) {
1439                     struct stat sbuf;
1440                     FILE *f;
1441                     char fcell[100], xlog_path[512];
1442
1443                     strcpy(xlog_path, pwd->pw_dir);
1444                     strcat(xlog_path, "/.xlog");
1445
1446                     if ((stat(xlog_path, &sbuf) == 0) &&
1447                         ((f = fopen(xlog_path, "r")) != NULL)) {
1448
1449                         if (dflag) {
1450                             printf("Reading %s for cells to "
1451                                     "authenticate to.\n", xlog_path);
1452                         }
1453
1454                         while (fgets(fcell, 100, f) != NULL) {
1455                             int auth_status;
1456
1457                             fcell[strlen(fcell) - 1] = '\0';
1458
1459                             if (dflag) {
1460                                 printf("Found cell %s in %s.\n",
1461                                         fcell, xlog_path);
1462                             }
1463
1464                             auth_status = auth_to_cell(context, fcell, NULL);
1465                             if (status == AKLOG_SUCCESS)
1466                                 status = auth_status;
1467                             else
1468                                 status = AKLOG_SOMETHINGSWRONG;
1469                         }
1470                     }
1471                 }
1472 #endif /* WINDOWS */
1473         }
1474     else {
1475         /* Log to all cells in the cells list first */
1476         for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1477             memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1478             if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm)))
1479                 somethingswrong++;
1480                 else {
1481                         if (linked && linkedcell[0]) {
1482                                 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1483                 if (dflag) {
1484                     printf("Linked cell: %s\n",
1485                         linkedcell);
1486                 }
1487                                 if ((status = auth_to_cell(context,linkedcell2,
1488                                                          cellinfo.realm)))
1489                                 somethingswrong++;
1490                         }
1491                 }
1492         }
1493         
1494 #ifndef WINDOWS
1495         /* Then, log to all paths in the paths list */
1496         for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1497             if ((status = auth_to_path(context, cur_node->data)))
1498                 somethingswrong++;
1499         }
1500 #endif /* WINDOWS */
1501         
1502         /* 
1503          * If only one thing was logged to, we'll return the status 
1504          * of the single call.  Otherwise, we'll return a generic
1505          * something failed status.
1506          */
1507         if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1508             status = AKLOG_SOMETHINGSWRONG;
1509     }
1510
1511     /* If we are keeping track of zephyr subscriptions, print them. */
1512     if (zsubs) 
1513         for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1514             printf("zsub: %s\n", cur_node->data);
1515         }
1516
1517     /* If we are keeping track of host information, print it. */
1518     if (hosts)
1519         for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1520             printf("host: %s\n", cur_node->data);
1521         }
1522
1523     exit(status);
1524 }
1525
1526 #ifndef HAVE_ADD_TO_ERROR_TABLE
1527
1528 #define error_table error_table_compat
1529 #include <afs/error_table.h>
1530 #undef error_table
1531
1532 #ifndef HAVE_ADD_ERROR_TABLE
1533 void add_error_table (const struct error_table *);
1534 #endif /* !HAVE_ADD_ERROR_TABLE */
1535
1536 void
1537 add_to_error_table(struct et_list *new_table)
1538 {
1539         add_error_table((struct error_table *) new_table->table);
1540 }
1541 #endif /* HAVE_ADD_TO_ERROR_TABLE */
1542
1543 static int isdir(char *path, unsigned char *val)
1544 {
1545     struct stat statbuf;
1546
1547     if (lstat(path, &statbuf) < 0)
1548         return (-1);
1549     else {
1550         if ((statbuf.st_mode & S_IFMT) == S_IFDIR) 
1551             *val = TRUE;
1552         else
1553             *val = FALSE;
1554         return (0);
1555     }  
1556 }
1557
1558 static krb5_error_code get_credv5(krb5_context context, 
1559                         char *name, char *inst, char *realm,
1560                         krb5_creds **creds)
1561 {
1562     krb5_creds increds;
1563     krb5_error_code r;
1564     static krb5_principal client_principal = 0;
1565
1566     memset((char *)&increds, 0, sizeof(increds));
1567 /* ANL - instance may be ptr to a null string. Pass null then */
1568     if ((r = krb5_build_principal(context, &increds.server,
1569                      strlen(realm), realm,
1570                      name,
1571            (inst && strlen(inst)) ? inst : (void *) NULL,
1572                      (void *) NULL))) {
1573         return r;
1574     }
1575
1576     if (!_krb425_ccache) {
1577         r = krb5_cc_default(context, &_krb425_ccache);
1578         if (r)
1579             return r;
1580     }
1581     if (!client_principal) {
1582         r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1583         if (r)
1584             return r;
1585     }
1586
1587     increds.client = client_principal;
1588     increds.times.endtime = 0;
1589         /* Ask for DES since that is what V4 understands */
1590     get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
1591
1592     r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
1593
1594     return r;
1595 }
1596
1597
1598 static int get_user_realm(krb5_context context, char *realm)
1599 {
1600     static krb5_principal client_principal = 0;
1601     int i;
1602
1603     if (!_krb425_ccache)
1604         krb5_cc_default(context, &_krb425_ccache);
1605     if (!client_principal)
1606         krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1607
1608     i = realm_len(context, client_principal);
1609     if (i > REALM_SZ-1) i = REALM_SZ-1;
1610     strncpy(realm,realm_data(context, client_principal), i);
1611     realm[i] = 0;
1612
1613     return(0);
1614 }