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