3f635e54eb0fbe706d7fffd3591ba5b281c54c36
[openafs.git] / src / WINNT / aklog / aklog.c
1 /* 
2  *  Copyright (C) 1989,2004 by the Massachusetts Institute of Technology
3  * 
4  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
5  * distribute this software and its documentation for any purpose and
6  * without fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright notice and
8  * this permission notice appear in supporting documentation, and that
9  * the name of M.I.T. not be used in advertising or publicity pertaining
10  * to distribution of the software without specific, written prior
11  * permission.  M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is" without express
13  * or implied warranty.
14  */
15
16 /*
17  * Copyright (c) 2007 Secure Endpoints Inc.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  *     * Redistributions of source code must retain the above copyright
26  *       notice, this list of conditions and the following disclaimer.
27  *     * Neither the name of the Secure Endpoints Inc. nor the names of its
28  *       contributors may be used to endorse or promote products derived
29  *       from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  */
43
44 #ifndef _WIN64
45 #define HAVE_KRB4
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <errno.h>
55 #include <afs/stds.h>
56 #ifdef HAVE_KRB4
57 #include <krb.h>
58 #else
59 #define REALM_SZ 64
60 #define ANAME_SZ 64
61 #define INST_SZ  64
62 #define KSUCCESS 0
63
64 #define CREDENTIALS void
65 #endif
66 #include <krb5.h>
67 #include <afs/ptserver.h>
68 #include <afs/ptuser.h>
69
70 #ifdef WIN32
71 #include <windows.h>
72
73 #include <cm_config.h>
74 #include <auth.h>
75 #include <cellconfig.h>
76 #include <pioctl_nt.h>
77 #include <smb_iocons.h>
78
79 #define stat _stat
80 #define lstat stat
81 #define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
82 #define S_ISDIR(mode)          __S_ISTYPE((mode), _S_IFDIR)
83
84 #define DONT_HAVE_GET_AD_TKT
85 #define MAXSYMLINKS 255
86
87 #ifdef HAVE_KRB4
88 /* Win32 uses get_krb_err_txt_entry(status) instead of krb_err_txt[status],
89 * so we use a bit of indirection like the GNU CVS sources.
90 */
91 #define krb_err_text(status) get_krb_err_txt_entry(status)
92 #endif
93
94 #define DRIVECOLON ':'          /* Drive letter separator */
95 #define BDIR '\\'               /* Other character that divides directories */
96
97 static int 
98 readlink(char *path, char *buf, int buffers)
99 {
100         return -1;
101 }
102
103 char * getcwd(char*, size_t);
104
105 static long 
106 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
107 {
108         struct afsconf_cell *cc = (struct afsconf_cell *) cellconfig;
109
110         cc->hostAddr[cc->numServers] = *addrp;
111         strcpy(cc->hostName[cc->numServers], namep);
112         cc->numServers++;
113         return 0;
114 }
115
116 #else /* WIN32 */
117 #include <sys/param.h>
118 #include <netdb.h>
119 #include <arpa/inet.h>
120 #include <sys/socket.h>
121 #include <unistd.h>
122
123 #include <afs/param.h>
124 #include <afs/auth.h>
125 #include <afs/cellconfig.h>
126 #include <afs/vice.h>
127 #include <afs/venus.h>
128 #include <afs/ptserver.h>
129
130 #define krb_err_text(status) krb_err_txt[status]
131
132 /* Cheesy test for determining AFS 3.5. */
133 #ifndef AFSCONF_CLIENTNAME
134 #define AFS35
135 #endif
136
137 #ifdef AFS35
138 #include <afs/dirpath.h>
139 #else
140 #define AFSDIR_CLIENT_ETC_DIRPATH AFSCONF_CLIENTNAME
141 #endif
142
143 #endif /* WIN32 */
144
145 #include "linked_list.h"
146
147 #define AFSKEY "afs"
148 #define AFSINST ""
149
150 #define AKLOG_SUCCESS 0
151 #define AKLOG_USAGE 1
152 #define AKLOG_SOMETHINGSWRONG 2
153 #define AKLOG_AFS 3
154 #define AKLOG_KERBEROS 4
155 #define AKLOG_TOKEN 5
156 #define AKLOG_BADPATH 6
157 #define AKLOG_MISC 7
158 #define AKLOG_KFW_NOT_INSTALLED 8
159
160 #ifndef NULL
161 #define NULL 0
162 #endif
163
164 #ifndef TRUE
165 #define TRUE 1
166 #endif
167
168 #ifndef FALSE
169 #define FALSE 0
170 #endif
171
172 #ifndef MAXSYMLINKS
173 #define MAXSYMLINKS 15
174 #endif
175
176 #define DIR '/'                 /* Character that divides directories */
177 #define DIRSTRING "/"           /* String form of above */
178 #define VOLMARKER ':'           /* Character separating cellname from mntpt */
179 #define VOLMARKERSTRING ":"     /* String form of above */
180
181 typedef struct {
182     char cell[BUFSIZ];
183     char realm[REALM_SZ];
184 } cellinfo_t;
185
186
187 struct afsconf_cell ak_cellconfig; /* General information about the cell */
188
189 static char *progname = NULL;   /* Name of this program */
190 static int dflag = FALSE;       /* Give debugging information */
191 static int noprdb = FALSE;      /* Skip resolving name to id? */
192 static int force = FALSE;       /* Bash identical tokens? */
193 static linked_list authedcells; /* List of cells already logged to */
194
195 static int usev5 = TRUE;   /* use kerberos 5? */
196 static int use524 = FALSE;  /* use krb524? */
197 static krb5_ccache _krb425_ccache;
198
199 long GetLocalCell(struct afsconf_dir **pconfigdir, char *local_cell)
200 {
201     if (!(*pconfigdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)))
202     {
203         fprintf(stderr, "%s: can't get afs configuration (afsconf_Open(%s))\n",
204                  progname, AFSDIR_CLIENT_ETC_DIRPATH);
205         exit(AKLOG_AFS);
206     }
207
208     return afsconf_GetLocalCell(*pconfigdir, local_cell, MAXCELLCHARS);
209 }
210
211 long GetCellInfo(struct afsconf_dir **pconfigdir, char* cell, 
212 struct afsconf_cell **pcellconfig)
213 {
214     return afsconf_GetCellInfo(*pconfigdir, cell, NULL, *pcellconfig);
215 }
216
217 void CloseConf(struct afsconf_dir **pconfigdir)
218 {       
219     (void) afsconf_Close(*pconfigdir);
220 }
221
222 #define ALLOW_REGISTER 1
223 void ViceIDToUsername(char *username, char *realm_of_user, char *realm_of_cell,
224                       char * cell_to_use, CREDENTIALS *c,
225                       int *status, 
226                       struct ktc_principal *aclient, struct ktc_principal *aserver, struct ktc_token *atoken)
227 {
228     static char lastcell[MAXCELLCHARS+1] = { 0 };
229     static char confname[512] = { 0 };
230     char username_copy[BUFSIZ];
231     afs_int32 viceId;                   /* AFS uid of user */
232 #ifdef ALLOW_REGISTER
233     afs_int32 id;
234 #endif /* ALLOW_REGISTER */
235
236     if (confname[0] == '\0') {
237         strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
238         confname[sizeof(confname) - 2] = '\0';
239     }
240
241     if (dflag)
242         printf("About to resolve name %s to id\n", username);
243
244     strcpy(lastcell, aserver->cell);
245
246     if (!pr_Initialize (0, confname, aserver->cell)) {
247         char sname[PR_MAXNAMELEN], *at;
248
249         strncpy(sname, username, PR_MAXNAMELEN);
250         sname[PR_MAXNAMELEN-1] = '\0';
251
252         at = strchr(sname, '@');
253         if (at && !stricmp(at+1, realm_of_cell))
254             *at = '\0';
255         *status = pr_SNameToId (sname, &viceId);
256     }
257
258     if (dflag)
259     {
260         if (*status)
261             printf("Error %d\n", *status);
262         else
263             printf("Id %d\n", viceId);
264     }       
265
266     /*
267      * This code is taken from cklog -- it lets people
268      * automatically register with the ptserver in foreign cells
269      */
270
271 #ifdef ALLOW_REGISTER
272     if (*status == 0) {
273         if (viceId != ANONYMOUSID) {
274 #else /* ALLOW_REGISTER */
275             if ((*status == 0) && (viceId != ANONYMOUSID))
276 #endif /* ALLOW_REGISTER */
277             {
278 #ifdef AFS_ID_TO_NAME
279                 strncpy(username_copy, username, BUFSIZ);
280                 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
281 #endif /* AFS_ID_TO_NAME */
282             }
283 #ifdef ALLOW_REGISTER
284         } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
285             int i;
286             if (dflag) {
287                 printf("doing first-time registration of %s "
288                         "at %s\n", username, cell_to_use);
289             }
290             id = 0;
291             strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
292             aclient->name[MAXKTCNAMELEN - 1] = '\0';
293             strcpy(aclient->instance, "");
294             strncpy(aclient->cell, cell_to_use, MAXKTCREALMLEN - 1);
295             aclient->cell[MAXKTCREALMLEN - 1] = '\0';
296
297             for ( i=0; aclient->cell[i]; i++ ) {
298                 if ( islower(aclient->cell[i]) )
299                     aclient->cell[i] = toupper(aclient->cell[i]);
300             }
301
302             if ((*status = ktc_SetToken(aserver, atoken, aclient, 0))) {
303                 printf("%s: unable to set tokens for cell %s "
304                         "(status: %d).\n", progname, cell_to_use, *status);
305                 *status = AKLOG_TOKEN;
306                 return ;
307             }
308
309             /*
310              * In case you're wondering, we don't need to change the
311              * filename here because we're still connecting to the
312              * same cell -- we're just using a different authentication
313              * level
314              */
315
316             if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
317                 printf("Error %d\n", *status);
318                 return;
319             }
320
321             /* copy the name because pr_CreateUser lowercases the realm */
322             strncpy(username_copy, username, BUFSIZ);
323
324             *status = pr_CreateUser(username, &id);
325
326             /* and restore the name to the original state */
327             strncpy(username, username_copy, BUFSIZ);
328
329             if (*status) {
330                 printf("%s: unable to create remote PTS "
331                         "user %s in cell %s (status: %d).\n", progname,
332                         username, cell_to_use, *status);
333             } else {
334                 printf("created cross-cell entry for %s (Id %d) at %s\n",
335                         username, viceId, cell_to_use);
336 #ifdef AFS_ID_TO_NAME
337                 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
338 #endif /* AFS_ID_TO_NAME */
339             }
340         }
341     }
342 #endif /* ALLOW_REGISTER */
343 }
344
345 char *LastComponent(char *str)
346 {
347     char *ret = strrchr(str, DIR);
348
349 #ifdef WIN32
350     if (!ret)
351         ret = strrchr(str, BDIR);
352 #endif
353     return ret;
354 }
355
356 int FirstComponent(char *str)
357 {
358     return (int)(
359 #ifdef WIN32
360                 strchr(str, BDIR) ||
361 #endif
362                 strchr(str, DIR));
363 }
364
365 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
366 {
367 #ifdef WIN32
368     if (origpath[1] == DRIVECOLON)
369     {
370         strncpy(pathtocheck, origpath, 2);
371         strcpy(path, origpath+2);
372     }
373     else
374 #endif
375         strcpy(path, origpath);
376 }
377
378 int BeginsWithDir(char *str, int colon)
379 {
380     return (str[0] == DIR) ||
381 #ifdef WIN32
382         ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
383 #else
384     FALSE;
385 #endif
386 }
387
388
389 /* This is a pretty gross hack.  Linking against the Transarc
390 * libraries pulls in some rxkad functions which use des.  (I don't
391 * think they ever get called.)  With Transarc-supplied libraries this
392 * creates a reliance on the symbol des_pcbc_init() which is only in
393 * Transarc's DES libraries (it's an exportability symbol-hiding
394 * thing), which we don't want to use because they don't work with
395 * MIT's krb4 routines.  So export a des_pcbc_init() symbol here so we
396 * don't have to link against Transarc's des library.
397 */
398 int des_pcbc_init()
399 {
400     abort();
401     return 0;   /* avoid warning */
402 }
403
404 #ifdef HAVE_KRB4
405 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
406 {
407     int status;
408
409     status = krb_get_cred(name, inst, realm, c);
410     if (status != KSUCCESS)
411     {
412 #ifdef DONT_HAVE_GET_AD_TKT
413         KTEXT_ST ticket;
414         status = krb_mk_req(&ticket, name, inst, realm, 0);
415 #else
416         status = get_ad_tkt(name, inst, realm, 255);
417 #endif
418         if (status == KSUCCESS)
419             status = krb_get_cred(name, inst, realm, c);
420     }       
421
422     return (status);
423 }
424 #endif
425
426 static int get_v5cred(krb5_context context, 
427                       char *name, char *inst, char *realm, CREDENTIALS *c,
428                       krb5_creds **creds)
429 {
430     krb5_creds increds;
431     krb5_error_code r;
432     static krb5_principal client_principal = 0;
433
434     if (client_principal) {
435         krb5_free_principal(context, client_principal);
436         client_principal = 0;
437     }
438
439     memset((char *)&increds, 0, sizeof(increds));
440
441     if ((r = krb5_build_principal(context, &increds.server,
442                                   (int)strlen(realm), realm,
443                                   name,
444                                   (inst && strlen(inst)) ? inst : 0,
445                                   0))) {
446         return((int)r);
447     }
448
449     if (!_krb425_ccache) {
450         if ((r = krb5_cc_default(context, &_krb425_ccache)))
451             return ((int)r);
452     }
453     if (!client_principal) {
454         if ((r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal))) {
455             krb5_cc_close(context, _krb425_ccache);
456             return ((int)r);
457         }
458     }
459
460     increds.client = client_principal;
461     increds.times.endtime = 0;
462         /* Ask for DES since that is what V4 understands */
463     increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
464
465     r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
466     if (r) {
467         krb5_cc_close(context, _krb425_ccache);
468         return((int)r);
469     }
470     /* This requires krb524d to be running with the KDC */
471     if (c != NULL)
472         r = krb5_524_convert_creds(context, *creds, c);
473
474     krb5_cc_close(context, _krb425_ccache);
475
476     return((int)r);
477 }
478
479 #ifdef HAVE_KRB4
480 /* There is no header for this function.  It is supposed to be private */
481 int krb_get_admhst(char *h,char *r, int n);
482
483 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
484 {
485     char krbhst[MAX_HSTNM];
486     static char krbrlm[REALM_SZ+1];
487
488     if (!cellconfig)
489         return 0;
490
491     strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
492
493     if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
494     {
495         char *s = krbrlm;
496         char *t = cellconfig->name;
497         int c;
498
499         while (c = *t++)
500         {
501             if (islower(c))
502                 c = toupper(c);
503             *s++ = c;
504         }
505         *s++ = 0;
506     }
507     return krbrlm;
508 }
509 #endif
510
511 /* As of MIT Kerberos 1.6, krb5_get_host_realm() will return the NUL-string 
512  * if there is no domain_realm mapping for the hostname's domain.  This is 
513  * used as a trigger indicating that referrals should be used within the
514  * krb5_get_credentials() call.  However, if the KDC does not support referrals
515  * that will result in a KRB5_ERR_HOST_REALM_UNKNOWN error and we will have
516  * to manually fallback to mapping the domain of the host as a realm name.
517  * Hence, the new fallback parameter.
518  */
519 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
520 {
521     char ** krbrlms = 0;
522     static char krbrlm[REALM_SZ+1];
523     krb5_error_code status;
524
525     if (!cellconfig)
526         return 0;
527
528     if (fallback) {
529         char * p;
530         p = strchr(cellconfig->hostName[0], '.');
531         if (p++)
532             strcpy(krbrlm, p);
533         else
534             strcpy(krbrlm, cellconfig->name);
535         strupr(krbrlm);
536     } else {
537         status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
538         if (status == 0 && krbrlms && krbrlms[0]) {
539             strcpy(krbrlm, krbrlms[0]);
540         } else {
541             strcpy(krbrlm, cellconfig->name);
542             strupr(krbrlm);
543         }
544
545         if (krbrlms)
546             krb5_free_host_realm( context, krbrlms );
547     }
548     return krbrlm;
549 }       
550
551 static char *copy_cellinfo(cellinfo_t *cellinfo)
552 {
553     cellinfo_t *new_cellinfo;
554
555     if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
556         memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
557
558     return ((char *)new_cellinfo);
559 }
560
561
562 static char *copy_string(char *string)
563 {
564     char *new_string;
565
566     if (new_string = (char *)calloc(strlen(string) + 1, sizeof(char)))
567         (void) strcpy(new_string, string);
568
569     return (new_string);
570 }       
571
572
573 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
574                                                   char *local_cell)
575 {
576     int status = AKLOG_SUCCESS;
577     struct afsconf_dir *configdir = 0;
578
579     memset(local_cell, 0, sizeof(local_cell));
580     memset(cellconfig, 0, sizeof(*cellconfig));
581
582     if (GetLocalCell(&configdir, local_cell))
583     {
584         fprintf(stderr, "%s: can't determine local cell.\n", progname);
585         exit(AKLOG_AFS);
586     }
587
588     if ((cell == NULL) || (cell[0] == 0))
589         cell = local_cell;
590
591     if (GetCellInfo(&configdir, cell, &cellconfig))
592     {
593         fprintf(stderr, "%s: Can't get information about cell %s.\n",
594                 progname, cell);
595         status = AKLOG_AFS;
596     }
597
598
599     CloseConf(&configdir);
600
601     return(status);
602 }
603
604 static int get_v5_user_realm(krb5_context context,char *realm)
605 {
606     static krb5_principal client_principal = 0;
607     int i;
608
609     if (!_krb425_ccache)
610         krb5_cc_default(context, &_krb425_ccache);
611     if (!client_principal)
612         krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
613
614     i = krb5_princ_realm(context, client_principal)->length;
615     if (i < REALM_SZ-1) i = REALM_SZ-1;
616     strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
617     realm[i] = 0;
618     return(KSUCCESS);
619 }
620
621 /*
622 * Log to a cell.  If the cell has already been logged to, return without
623 * doing anything.  Otherwise, log to it and mark that it has been logged
624 * to.  */
625 static int auth_to_cell(krb5_context context, char *cell, char *realm)
626 {
627     int status = AKLOG_SUCCESS;
628     char username[BUFSIZ];        /* To hold client username structure */
629
630     char name[ANAME_SZ];          /* Name of afs key */
631     char instance[INST_SZ];       /* Instance of afs key */
632     char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
633     char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
634     char local_cell[MAXCELLCHARS+1];
635     char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
636
637     krb5_creds *v5cred = NULL;
638 #ifdef HAVE_KRB4
639     CREDENTIALS c;
640 #endif
641     struct ktc_principal aserver;
642     struct ktc_principal aclient;
643     struct ktc_token atoken, btoken;
644     int i;
645
646     /* try to avoid an expensive call to get_cellconfig */
647     if (cell && ll_string_check(&authedcells, cell))
648     {
649         if (dflag)
650             printf("Already authenticated to %s (or tried to)\n", cell);
651         return(AKLOG_SUCCESS);
652     }
653
654     memset(name, 0, sizeof(name));
655     memset(instance, 0, sizeof(instance));
656     memset(realm_of_user, 0, sizeof(realm_of_user));
657     memset(realm_of_cell, 0, sizeof(realm_of_cell));
658
659     /* NULL or empty cell returns information on local cell */
660     if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
661         return(status);
662
663     strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
664     cell_to_use[MAXCELLCHARS] = 0;
665
666     if (ll_string_check(&authedcells, cell_to_use))
667     {
668         if (dflag)
669             printf("Already authenticated to %s (or tried to)\n", cell_to_use);
670         return(AKLOG_SUCCESS);
671     }
672
673     /*
674      * Record that we have attempted to log to this cell.  We do this
675      * before we try rather than after so that we will not try
676      * and fail repeatedly for one cell.
677      */
678     (void)ll_add_string(&authedcells, cell_to_use);
679
680     if (dflag)
681         printf("Authenticating to cell %s.\n", cell_to_use);
682
683     /* We use the afs.<cellname> convention here... */
684     strcpy(name, AFSKEY);
685     strncpy(instance, cell_to_use, sizeof(instance));
686     instance[sizeof(instance)-1] = '\0';
687
688     /*
689      * Extract the session key from the ticket file and hand-frob an
690      * afs style authenticator.
691      */
692
693     if (usev5) 
694     { /* using krb5 */
695         int retry = 1;
696         int realm_fallback = 0;
697
698         if ( strchr(name,'.') != NULL ) {
699             fprintf(stderr, "%s: Can't support principal names including a dot.\n",
700                     progname);
701             return(AKLOG_MISC);
702         }
703
704       try_v5:
705         if (realm && realm[0])
706             strcpy(realm_of_cell, realm);
707         else
708             strcpy(realm_of_cell,
709                     afs_realm_of_cell5(context, &ak_cellconfig, realm_fallback));
710
711         if (dflag)
712             printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
713         status = get_v5cred(context, name, instance, realm_of_cell, 
714 #ifdef HAVE_KRB4
715                             use524 ? &c : NULL, 
716 #else
717                             NULL,
718 #endif
719                             &v5cred);
720
721         if (status == 0 && strcmp(realm_of_cell, "") == 0) {
722             krb5_error_code code;
723             krb5_ticket *ticket;
724
725             code = krb5_decode_ticket(&v5cred->ticket, &ticket);
726
727             if (code != 0) {
728                 fprintf(stderr,
729                          "%s: Couldn't decode ticket to determine realm for "
730                          "cell %s.\n",
731                          progname, cell_to_use);
732             } else {
733                 int len = krb5_princ_realm(context, ticket->server)->length;
734                 /* This really shouldn't happen. */
735                 if (len > REALM_SZ-1)
736                     len = REALM_SZ-1;
737
738                 strncpy(realm_of_cell, krb5_princ_realm(context, ticket->server)->data, len);
739                 realm_of_cell[len] = 0;
740
741                 krb5_free_ticket(context, ticket);
742             }
743         }
744
745         if (status == KRB5_ERR_HOST_REALM_UNKNOWN) {
746             realm_fallback = 1;
747             goto try_v5;
748         } else if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
749             if (!realm_of_cell[0]) {
750                 realm_fallback = 1;
751                 goto try_v5;
752             }
753             if (dflag)
754                 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
755             status = get_v5cred(context, name, "", realm_of_cell, 
756 #ifdef HAVE_KRB4
757                                 use524 ? &c : NULL, 
758 #else
759                                 NULL,
760 #endif
761                                 &v5cred);
762         }
763         if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
764             retry = 0;
765             realm_fallback = 0;
766             goto try_v5;
767         }       
768     }       
769     else 
770     {
771 #ifdef HAVE_KRB4
772         if (realm && realm[0])
773             strcpy(realm_of_cell, realm);
774         else
775             strcpy(realm_of_cell, afs_realm_of_cell(&ak_cellconfig));
776
777         /*
778          * Try to obtain AFS tickets.  Because there are two valid service
779          * names, we will try both, but trying the more specific first.
780          *
781          *      afs.<cell>@<realm>
782          *      afs@<realm>
783          */
784         if (dflag)
785             printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
786         status = get_cred(name, instance, realm_of_cell, &c);
787         if (status == KDC_PR_UNKNOWN)
788         {
789             if (dflag)
790                 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
791             status = get_cred(name, "", realm_of_cell, &c);
792         }
793 #else
794         return(AKLOG_MISC);
795 #endif
796     } 
797
798     /* TODO: get k5 error text */
799     if (status != KSUCCESS)
800     {
801         if (dflag)
802             printf("Kerberos error code returned by get_cred: %d\n", status);
803         fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
804                  progname, cell_to_use, 
805 #ifdef HAVE_KRB4
806                  (usev5)?"":krb_err_text(status)
807 #else
808                  ""
809 #endif
810                  );
811         return(AKLOG_KERBEROS);
812     }
813
814     strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
815     strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
816     strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
817
818     if (usev5 && !use524) {
819         /* This code inserts the entire K5 ticket into the token
820          * No need to perform a krb524 translation which is
821          * commented out in the code below
822          */
823         char * p;
824         int len;
825         
826         len = min(v5cred->client->data[0].length,MAXKTCNAMELEN - 1);
827         strncpy(username, v5cred->client->data[0].data, len);
828         username[len] = '\0';
829
830         if ( v5cred->client->length > 1 ) {
831             strcat(username, ".");
832             p = username + strlen(username);
833             len = min(v5cred->client->data[1].length,MAXKTCNAMELEN - strlen(username) - 1);
834             strncpy(p, v5cred->client->data[1].data, len);
835             p[len] = '\0';
836         }
837
838         memset(&atoken, '\0', sizeof(atoken));
839         atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
840         atoken.startTime = v5cred->times.starttime;
841         atoken.endTime = v5cred->times.endtime;
842         memcpy(&atoken.sessionKey, v5cred->keyblock.contents, v5cred->keyblock.length);
843         atoken.ticketLen = v5cred->ticket.length;
844         memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
845     } else {
846 #ifdef HAVE_KRB4
847         strcpy (username, c.pname);
848         if (c.pinst[0])
849         {
850             strcat(username, ".");
851             strcat(username, c.pinst);
852         }
853
854         atoken.kvno = c.kvno;
855         atoken.startTime = c.issue_date;
856         /* ticket lifetime is in five-minutes blocks. */
857         atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
858
859         memcpy(&atoken.sessionKey, c.session, 8);
860         atoken.ticketLen = c.ticket_st.length;
861         memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
862 #else
863         return(AKLOG_MISC);
864 #endif
865     }
866
867     if (!force &&
868          !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
869          atoken.kvno == btoken.kvno &&
870          atoken.ticketLen == btoken.ticketLen &&
871          !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
872          !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
873     {       
874         if (dflag)
875             printf("Identical tokens already exist; skipping.\n");
876         return 0;
877     }
878
879     if (noprdb)
880     {
881         if (dflag)
882             printf("Not resolving name %s to id (-noprdb set)\n", username);
883     }       
884     else    
885     {
886         if (usev5) {
887             if((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
888                 fprintf(stderr, "%s: Couldn't determine realm of user: %d\n",
889                          progname, status);
890                 return(AKLOG_KERBEROS);
891             }
892         } else {
893 #ifdef HAVE_KRB4
894             if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
895             {
896                 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
897                          progname, krb_err_text(status));
898                 return(AKLOG_KERBEROS);
899             }
900 #else
901             return(AKLOG_MISC);
902 #endif
903         }
904
905         /* For Khimaira we want to always append the realm to the name */
906         if (1 /* strcmp(realm_of_user, realm_of_cell) */)
907         {
908             strcat(username, "@");
909             strcat(username, realm_of_user);
910         }
911
912         ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use, 
913 #ifdef HAVE_KRB4
914                           &c, 
915 #else
916                           NULL,
917 #endif
918                           &status, &aclient, &aserver, &atoken);
919     }
920
921     if (dflag)
922         printf("Set username to %s\n", username);
923
924     /* Reset the "aclient" structure before we call ktc_SetToken.
925      * This structure was first set by the ktc_GetToken call when
926      * we were comparing whether identical tokens already existed.
927      */
928     strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
929     strcpy(aclient.instance, "");
930     
931     if (usev5 && !use524) {
932         int len = min(v5cred->client->realm.length,MAXKTCNAMELEN - 1);
933         strncpy(aclient.cell, v5cred->client->realm.data, len);
934         aclient.cell[len] = '\0';
935     } 
936 #ifdef HAVE_KRB4
937     else
938         strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
939 #endif
940
941     for ( i=0; aclient.cell[i]; i++ ) {
942         if ( islower(aclient.cell[i]) )
943             aclient.cell[i] = toupper(aclient.cell[i]);
944     }
945
946     if (dflag)
947         printf("Getting tokens.\n");
948     if (status = ktc_SetToken(&aserver, &atoken, &aclient, 0))
949     {
950         fprintf(stderr,
951                  "%s: unable to obtain tokens for cell %s (status: %d).\n",
952                  progname, cell_to_use, status);
953         status = AKLOG_TOKEN;
954     }
955
956     return(status);
957 }
958
959 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
960 {
961     char our_file[MAXPATHLEN + 1];
962     char *parent_dir;
963     char *last_component;
964     struct ViceIoctl vio;
965     char cellname[BUFSIZ];
966
967     memset(our_file, 0, sizeof(our_file));
968     strcpy(our_file, file);
969
970     if (last_component = LastComponent(our_file))
971     {
972         *last_component++ = 0;
973         parent_dir = our_file;
974     }
975     else
976     {
977         last_component = our_file;
978         parent_dir = ".";
979     }
980
981     memset(cellname, 0, sizeof(cellname));
982
983     vio.in = last_component;
984     vio.in_size = strlen(last_component)+1;
985     vio.out_size = size;
986     vio.out = mountpoint;
987
988     if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
989     {
990         if (strchr(mountpoint, VOLMARKER) == NULL)
991         {
992             vio.in = file;
993             vio.in_size = strlen(file) + 1;
994             vio.out_size = sizeof(cellname);
995             vio.out = cellname;
996
997             if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
998             {
999                 strcat(cellname, VOLMARKERSTRING);
1000                 strcat(cellname, mountpoint + 1);
1001                 memset(mountpoint + 1, 0, size - 1);
1002                 strcpy(mountpoint + 1, cellname);
1003             }
1004         }
1005         return(TRUE);
1006     }
1007     else {
1008         return(FALSE);
1009     }
1010 }       
1011
1012 /*
1013 * This routine each time it is called returns the next directory
1014 * down a pathname.  It resolves all symbolic links.  The first time
1015 * it is called, it should be called with the name of the path
1016 * to be descended.  After that, it should be called with the arguemnt
1017 * NULL.
1018 */
1019 static char *next_path(char *origpath)
1020 {
1021     static char path[MAXPATHLEN + 1];
1022     static char pathtocheck[MAXPATHLEN + 1];
1023
1024     int link = FALSE;           /* Is this a symbolic link? */
1025     char linkbuf[MAXPATHLEN + 1];
1026     char tmpbuf[MAXPATHLEN + 1];
1027
1028     static char *last_comp;     /* last component of directory name */
1029     static char *elast_comp;    /* End of last component */
1030     char *t;
1031     int len;
1032
1033     static int symlinkcount = 0;        /* We can't exceed MAXSYMLINKS */
1034
1035     /* If we are given something for origpath, we are initializing only. */
1036     if (origpath)
1037     {
1038         memset(path, 0, sizeof(path));
1039         memset(pathtocheck, 0, sizeof(pathtocheck));
1040         CopyPathColon(origpath, path, pathtocheck);
1041         last_comp = path;
1042         symlinkcount = 0;
1043         return(NULL);
1044     }
1045
1046     /* We were not given origpath; find then next path to check */
1047
1048     /* If we've gotten all the way through already, return NULL */
1049     if (last_comp == NULL)
1050         return(NULL);
1051
1052     do
1053     {
1054         while (BeginsWithDir(last_comp, FALSE))
1055             strncat(pathtocheck, last_comp++, 1);
1056         len = (elast_comp = LastComponent(last_comp))
1057             ? elast_comp - last_comp : strlen(last_comp);
1058         strncat(pathtocheck, last_comp, len);
1059         memset(linkbuf, 0, sizeof(linkbuf));
1060         if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
1061         {
1062             if (++symlinkcount > MAXSYMLINKS)
1063             {
1064                 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1065                 exit(AKLOG_BADPATH);
1066             }
1067             memset(tmpbuf, 0, sizeof(tmpbuf));
1068             if (elast_comp)
1069                 strcpy(tmpbuf, elast_comp);
1070             if (BeginsWithDir(linkbuf, FALSE))
1071             {
1072                 /*
1073                 * If this is a symbolic link to an absolute path,
1074                 * replace what we have by the absolute path.
1075                 */
1076                 memset(path, 0, strlen(path));
1077                 memcpy(path, linkbuf, sizeof(linkbuf));
1078                 strcat(path, tmpbuf);
1079                 last_comp = path;
1080                 elast_comp = NULL;
1081                 memset(pathtocheck, 0, sizeof(pathtocheck));
1082             }
1083             else
1084             {
1085                 /*
1086                 * If this is a symbolic link to a relative path,
1087                 * replace only the last component with the link name.
1088                 */
1089                 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1090                 strcat(path, tmpbuf);
1091                 elast_comp = NULL;
1092                 if (t = LastComponent(pathtocheck))
1093                 {
1094                     t++;
1095                     memset(t, 0, strlen(t));
1096                 }
1097                 else
1098                     memset(pathtocheck, 0, sizeof(pathtocheck));
1099             }
1100         }
1101         else
1102             last_comp = elast_comp;
1103     }       
1104     while(link);
1105
1106     return(pathtocheck);
1107 }
1108
1109 /*
1110 * This routine descends through a path to a directory, logging to
1111 * every cell it encounters along the way.
1112 */
1113 static int auth_to_path(krb5_context context, char *path)
1114 {
1115     int status = AKLOG_SUCCESS;
1116     int auth_to_cell_status = AKLOG_SUCCESS;
1117
1118     char *nextpath;
1119     char pathtocheck[MAXPATHLEN + 1];
1120     char mountpoint[MAXPATHLEN + 1];
1121
1122     char *cell;
1123     char *endofcell;
1124
1125     /* Initialize */
1126     if (BeginsWithDir(path, TRUE))
1127         strcpy(pathtocheck, path);
1128     else
1129     {
1130         if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
1131         {
1132             fprintf(stderr, "Unable to find current working directory:\n");
1133             fprintf(stderr, "%s\n", pathtocheck);
1134             fprintf(stderr, "Try an absolute pathname.\n");
1135             exit(AKLOG_BADPATH);
1136         }
1137         else
1138         {
1139             /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
1140             * will already have a trailing slash ('\'). Otherwise, the string will
1141             * end in the last directory name */
1142 #ifdef WIN32    
1143             if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
1144 #endif  
1145                 strcat(pathtocheck, DIRSTRING);
1146             strcat(pathtocheck, path);
1147         }
1148     }
1149     next_path(pathtocheck);
1150
1151     /* Go on to the next level down the path */
1152     while (nextpath = next_path(NULL))
1153     {
1154         strcpy(pathtocheck, nextpath);
1155         if (dflag)
1156             printf("Checking directory [%s]\n", pathtocheck);
1157         /*
1158         * If this is an afs mountpoint, determine what cell from
1159         * the mountpoint name which is of the form
1160         * #cellname:volumename or %cellname:volumename.
1161         */
1162         if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
1163         {
1164             if(dflag)
1165                 printf("Found mount point [%s]\n", mountpoint);
1166             /* skip over the '#' or '%' */
1167             cell = mountpoint + 1;
1168             if (endofcell = strchr(mountpoint, VOLMARKER))
1169             {
1170                 *endofcell = '\0';
1171                 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1172                 {
1173                     if (status == AKLOG_SUCCESS)
1174                         status = auth_to_cell_status;
1175                     else if (status != auth_to_cell_status)
1176                         status = AKLOG_SOMETHINGSWRONG;
1177                 }
1178             }
1179         }
1180         else
1181         {
1182             struct stat st;
1183
1184             if (lstat(pathtocheck, &st) < 0)
1185             {
1186                 /*
1187                 * If we've logged and still can't stat, there's
1188                 * a problem...
1189                 */
1190                 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1191                          pathtocheck, strerror(errno));
1192                 return(AKLOG_BADPATH);
1193             }
1194             else if (!S_ISDIR(st.st_mode))
1195             {
1196                 /* Allow only directories */
1197                 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1198                          strerror(ENOTDIR));
1199                 return(AKLOG_BADPATH);
1200             }
1201         }
1202     }
1203
1204     return(status);
1205 }
1206
1207 /* Print usage message and exit */
1208 static void usage(void)
1209 {
1210     fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1211              "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1212              "[[-p | -path] pathname]\n",
1213              "    [-noprdb] [-force]\n",
1214 #ifdef HAVE_KRB4
1215              "    [-5 [-m]| -4]\n"
1216 #else
1217              "    [-5]\n"
1218 #endif
1219              );
1220     fprintf(stderr, "    -d gives debugging information.\n");
1221     fprintf(stderr, "    krb_realm is the kerberos realm of a cell.\n");
1222     fprintf(stderr, "    pathname is the name of a directory to which ");
1223     fprintf(stderr, "you wish to authenticate.\n");
1224     fprintf(stderr, "    -noprdb means don't try to determine AFS ID.\n");
1225 #ifdef HAVE_KRB4
1226     fprintf(stderr, "    -5 or -4 selects whether to use Kerberos v5 or Kerberos v4.\n"
1227                     "       (default is Kerberos v5)\n");
1228     fprintf(stderr, "       -m means use krb524d to convert Kerberos v5 tickets.\n");
1229 #else
1230     fprintf(stderr, "    -5 use Kerberos v5.\n"
1231                     "       (only Kerberos v5 is available)\n");
1232 #endif
1233     fprintf(stderr, "    No commandline arguments means ");
1234     fprintf(stderr, "authenticate to the local cell.\n");
1235     fprintf(stderr, "\n");
1236     exit(AKLOG_USAGE);
1237 }
1238
1239 void
1240 validate_krb5_availability(void)
1241 {
1242 #ifndef _WIN64
1243 #define KRB5LIB "krb5_32.dll"
1244 #else
1245 #define KRB5LIB "krb5_64.dll"
1246 #endif
1247     HINSTANCE h = LoadLibrary(KRB5LIB);
1248     if (h) 
1249         FreeLibrary(h);
1250     else {
1251         fprintf(stderr, "Kerberos for Windows library %s is not available.\n", KRB5LIB);
1252         exit(AKLOG_KFW_NOT_INSTALLED);
1253     }
1254 }
1255
1256 void
1257 validate_krb4_availability(void)
1258 {
1259 #ifdef HAVE_KRB4
1260     HINSTANCE h = LoadLibrary("krbv4w32.dll");
1261     if (h) 
1262         FreeLibrary(h);
1263     else {
1264         fprintf(stderr, "Kerberos for Windows library krbv4w32.dll is not available.\n");
1265         exit(AKLOG_KFW_NOT_INSTALLED);
1266     }
1267 #else
1268     fprintf(stderr, "Kerberos v4 is not available in this build of aklog.\n");
1269     exit(AKLOG_USAGE);
1270 #endif
1271 }
1272
1273 int main(int argc, char *argv[])
1274 {
1275     int status = AKLOG_SUCCESS;
1276     int i;
1277     int somethingswrong = FALSE;
1278
1279     cellinfo_t cellinfo;
1280
1281     extern char *progname;      /* Name of this program */
1282
1283     extern int dflag;           /* Debug mode */
1284
1285     int cmode = FALSE;          /* Cellname mode */
1286     int pmode = FALSE;          /* Path name mode */
1287
1288     char realm[REALM_SZ];               /* Kerberos realm of afs server */
1289     char cell[BUFSIZ];          /* Cell to which we are authenticating */
1290     char path[MAXPATHLEN + 1];  /* Path length for path mode */
1291
1292     linked_list cells;          /* List of cells to log to */
1293     linked_list paths;          /* List of paths to log to */
1294     ll_node *cur_node;
1295
1296     krb5_context context = 0;
1297
1298     memset(&cellinfo, 0, sizeof(cellinfo));
1299
1300     memset(realm, 0, sizeof(realm));
1301     memset(cell, 0, sizeof(cell));
1302     memset(path, 0, sizeof(path));
1303
1304     ll_init(&cells);
1305     ll_init(&paths);
1306
1307     /* Store the program name here for error messages */
1308     if (progname = LastComponent(argv[0]))
1309         progname++;
1310     else
1311         progname = argv[0];
1312
1313     /* Initialize list of cells to which we have authenticated */
1314     (void)ll_init(&authedcells);
1315
1316     /* Parse commandline arguments and make list of what to do. */
1317     for (i = 1; i < argc; i++)
1318     {
1319         if (strcmp(argv[i], "-d") == 0)
1320             dflag++;
1321         else if (strcmp(argv[i], "-5") == 0)
1322             usev5++;
1323 #ifdef HAVE_KRB4
1324         else if (strcmp(argv[i], "-m") == 0)
1325             use524++;
1326         else if (strcmp(argv[i], "-4") == 0)
1327             usev5 = 0;
1328 #endif
1329         else if (strcmp(argv[i], "-noprdb") == 0)
1330             noprdb++;
1331         else if (strcmp(argv[i], "-force") == 0)
1332             force++;
1333         else if (((strcmp(argv[i], "-cell") == 0) ||
1334                    (strcmp(argv[i], "-c") == 0)) && !pmode)
1335         {       
1336             if (++i < argc)
1337             {
1338                 cmode++;
1339                 strcpy(cell, argv[i]);
1340             }
1341             else
1342                 usage();
1343         }
1344         else if (((strcmp(argv[i], "-path") == 0) ||
1345                    (strcmp(argv[i], "-p") == 0)) && !cmode)
1346         {       
1347             if (++i < argc)
1348             {
1349                 pmode++;
1350                 strcpy(path, argv[i]);
1351             }
1352             else
1353                 usage();
1354         }
1355         else if (argv[i][0] == '-')
1356             usage();
1357         else if (!pmode && !cmode)
1358         {
1359             if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1360                  (strcmp(argv[i], "..") == 0))
1361             {
1362                 pmode++;
1363                 strcpy(path, argv[i]);
1364             }
1365             else
1366             {
1367                 cmode++;
1368                 strcpy(cell, argv[i]);
1369             }
1370         }
1371         else
1372             usage();
1373
1374         if (cmode)
1375         {
1376             if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1377             {
1378                 i += 2;
1379                 if (i < argc)
1380                     strcpy(realm, argv[i]);
1381                 else
1382                     usage();
1383             }
1384             /* Add this cell to list of cells */
1385             strcpy(cellinfo.cell, cell);
1386             strcpy(cellinfo.realm, realm);
1387             if (cur_node = ll_add_node(&cells, ll_tail))
1388             {
1389                 char *new_cellinfo;
1390                 if (new_cellinfo = copy_cellinfo(&cellinfo))
1391                     ll_add_data(cur_node, new_cellinfo);
1392                 else
1393                 {
1394                     fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1395                     exit(AKLOG_MISC);
1396                 }
1397             }
1398             else
1399             {
1400                 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1401                          progname);
1402                 exit(AKLOG_MISC);
1403             }
1404             memset(&cellinfo, 0, sizeof(cellinfo));
1405             cmode = FALSE;
1406             memset(cell, 0, sizeof(cell));
1407             memset(realm, 0, sizeof(realm));
1408         }
1409         else if (pmode)
1410         {
1411             /* Add this path to list of paths */
1412             if (cur_node = ll_add_node(&paths, ll_tail))
1413             {
1414                 char *new_path;
1415                 if (new_path = copy_string(path))
1416                     ll_add_data(cur_node, new_path);
1417                 else
1418                 {
1419                     fprintf(stderr, "%s: failure copying path name.\n",
1420                              progname);
1421                     exit(AKLOG_MISC);
1422                 }
1423             }
1424             else
1425             {
1426                 fprintf(stderr, "%s: failure adding path to paths list.\n",
1427                          progname);
1428                 exit(AKLOG_MISC);
1429             }
1430             pmode = FALSE;
1431             memset(path, 0, sizeof(path));
1432         }
1433     }
1434
1435     if (usev5)
1436         validate_krb5_availability();
1437     else 
1438         validate_krb4_availability();
1439
1440     if(usev5)
1441         krb5_init_context(&context);
1442
1443     /* If nothing was given, log to the local cell. */
1444     if ((cells.nelements + paths.nelements) == 0)
1445         status = auth_to_cell(context, NULL, NULL);
1446     else
1447     {
1448         /* Log to all cells in the cells list first */
1449         for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1450         {
1451             memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1452             if (status = auth_to_cell(context, 
1453                                        cellinfo.cell, cellinfo.realm))
1454                 somethingswrong++;
1455         }       
1456
1457         /* Then, log to all paths in the paths list */
1458         for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1459         {
1460             if (status = auth_to_path(context, 
1461                                        cur_node->data))
1462                 somethingswrong++;
1463         }
1464
1465         /*
1466         * If only one thing was logged to, we'll return the status
1467         * of the single call.  Otherwise, we'll return a generic
1468         * something failed status.
1469         */
1470         if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1471             status = AKLOG_SOMETHINGSWRONG;
1472     }       
1473
1474     if(usev5)
1475         krb5_free_context(context);
1476
1477     exit(status);
1478 }