aklog: Fix improper use of readlink
[openafs.git] / src / aklog / aklog.c
index 81be7eb..22df70d 100644 (file)
  */
 
 #include <afsconfig.h>
+#include <afs/param.h>
+#include <afs/stds.h>
 
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <sys/types.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#endif
-#include <errno.h>
-
-#include <sys/stat.h>
-#include <fcntl.h>
+#include <roken.h>
 
-#include <sys/param.h>
-#include <sys/errno.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <pwd.h>
+#include <ctype.h>
 
-#include <afs/stds.h>
 #include <afs/ktc.h>
 #include <afs/token.h>
 
 #include <afs/com_err.h>
 #endif
 
-#include <afs/param.h>
 #ifdef AFS_SUN5_ENV
 #include <sys/ioccom.h>
 #endif
 
-/* Prevent inclusion of des.h to avoid conflicts with des types */
-#define NO_DES_H_INCLUDE
-
 #include <afs/auth.h>
 #include <afs/cellconfig.h>
 #include <afs/vice.h>
 #define AKLOG_BADPATH 6
 #define AKLOG_MISC 7
 
-#ifndef NULL
-#define NULL 0
-#endif
-
 #ifndef TRUE
 #define TRUE 1
 #endif
@@ -245,10 +216,11 @@ Done:
 #error "Must have either keyblock or session member of krb5_creds"
 #endif
 
-#if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
-#define krb5_524_convert_creds krb524_convert_creds_kdc
-#elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
+/* MITKerberosShim logs but returns success */
+#if !defined(HAVE_KRB5_524_CONV_PRINCIPAL) || defined(AFS_DARWIN110_ENV) || (!defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC))
 #define HAVE_NO_KRB5_524
+#elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
+#define krb5_524_convert_creds krb524_convert_creds_kdc
 #endif
 
 #if USING_HEIMDAL
@@ -338,8 +310,11 @@ redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
        if (strncmp(str, "unknown", strlen("unknown")) == 0) {
 #ifdef HAVE_KRB5_SVC_GET_MSG
            krb5_svc_get_msg(code,&str);
-#elif defined(HAVE_ERROR_MESSAGE)
-           str = error_message(code);
+#elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
+           krb5_context context;
+           krb5_init_context(&context);
+           str = krb5_get_error_message(context, code);
+           krb5_free_context(context);
 #else
            ; /* IRIX apparently has neither: use the string we have */
 #endif
@@ -372,7 +347,7 @@ copy_cellinfo(cellinfo_t *cellinfo)
 {
     cellinfo_t *new_cellinfo;
 
-    if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
+    if ((new_cellinfo = malloc(sizeof(cellinfo_t))))
        memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
 
     return ((char *)new_cellinfo);
@@ -380,7 +355,8 @@ copy_cellinfo(cellinfo_t *cellinfo)
 
 
 static int
-get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
+get_cellconfig(const char *config, char *cell,
+              struct afsconf_cell *cellconfig, char **local_cell)
 {
     int status = AKLOG_SUCCESS;
     struct afsconf_dir *configdir;
@@ -394,10 +370,10 @@ get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
        exit(AKLOG_AFS);
     }
 
-    if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
+    if (!(configdir = afsconf_Open(config))) {
        fprintf(stderr,
                "%s: can't get afs configuration (afsconf_Open(%s))\n",
-               progname, AFSDIR_CLIENT_ETC_DIRPATH);
+               progname, config);
        exit(AKLOG_AFS);
     }
 
@@ -622,6 +598,11 @@ rxkad_get_ticket(krb5_context context, char *realm,
        fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
                progname, cell->name);
        afs_com_err(progname, status, "while getting AFS tickets");
+#ifdef KRB5_CC_NOT_KTYPE
+       if (status == KRB5_CC_NOT_KTYPE) {
+           fprintf(stderr, "allow_weak_crypto may be required in the Kerberos configuration\n");
+       }
+#endif
        status = AKLOG_KERBEROS;
        goto out;
     }
@@ -678,13 +659,12 @@ static int
 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
                         struct ktc_tokenUnion **tokenPtr, char **userPtr) {
     char username[BUFSIZ];
-    struct ktc_tokenUnion *token;
-    struct token_rxkad *rxkadToken;
+    struct ktc_token token;
+    int status;
 #ifdef HAVE_NO_KRB5_524
     char *p;
     int len;
 #else
-    int status;
     char k4name[ANAME_SZ];
     char k4inst[INST_SZ];
     char k4realm[REALM_SZ];
@@ -727,30 +707,21 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
     }
 #endif
 
-    token = malloc(sizeof(struct ktc_tokenUnion));
-    if (token == NULL)
-       return ENOMEM;
-
-    memset(token, 0, sizeof(struct ktc_tokenUnion));
-
-    token->at_type = AFSTOKEN_UNION_KAD;
-    rxkadToken = &token->ktc_tokenUnion_u.at_kad;
+    memset(&token, 0, sizeof(struct ktc_token));
 
-    rxkadToken->rk_kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
-    rxkadToken->rk_begintime = v5cred->times.starttime;;
-    rxkadToken->rk_endtime = v5cred->times.endtime;
-    memcpy(&rxkadToken->rk_key, get_cred_keydata(v5cred),
+    token.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
+    token.startTime = v5cred->times.starttime;;
+    token.endTime = v5cred->times.endtime;
+    memcpy(&token.sessionKey, get_cred_keydata(v5cred),
           get_cred_keylen(v5cred));
-    rxkadToken->rk_ticket.rk_ticket_len = v5cred->ticket.length;
-    rxkadToken->rk_ticket.rk_ticket_val = malloc(v5cred->ticket.length);
-    if (rxkadToken->rk_ticket.rk_ticket_val == NULL) {
-       free(token);
-       return ENOMEM;
+    token.ticketLen = v5cred->ticket.length;
+    memcpy(token.ticket, v5cred->ticket.data, token.ticketLen);
+
+    status = token_importRxkadViceId(tokenPtr, &token, 0);
+    if (status) {
+       return status;
     }
-    memcpy(rxkadToken->rk_ticket.rk_ticket_val, v5cred->ticket.data,
-          rxkadToken->rk_ticket.rk_ticket_len);
 
-    *tokenPtr = token;
     *userPtr = strdup(username);
 
     return 0;
@@ -792,8 +763,7 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
                          struct ktc_tokenUnion **tokenPtr, char **userPtr) {
     CREDENTIALS cred;
     char username[BUFSIZ];
-    struct ktc_tokenUnion *token;
-    struct token_rxkad *rxkadToken;
+    struct ktc_token token;
     int status;
 
     *tokenPtr = NULL;
@@ -815,16 +785,10 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
        strcat (username, cred.pinst);
     }
 
-    token = malloc(sizeof(struct ktc_tokenUnion));
-    if (token == NULL)
-       return ENOMEM;
-    memset(token, 0, sizeof(struct ktc_tokenUnion));
-
-    token->at_type = AFSTOKEN_UNION_KAD;
+    memset(&token, 0, sizeof(struct ktc_token));
 
-    rxkadToken = &token->ktc_tokenUnion_u.at_kad;
-    rxkadToken->rk_kvno = cred.kvno;
-    rxkadToken->rk_begintime = cred.issue_date;
+    token.kvno = cred.kvno;
+    token.startTime = cred.issue_date;
     /*
      * It seems silly to go through a bunch of contortions to
      * extract the expiration time, when the v5 credentials already
@@ -833,18 +797,16 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
      * Note that this isn't a security hole, as the expiration time
      * is also contained in the encrypted token
      */
-    rxkadToken->rk_endtime = v5cred->times.endtime;
-    memcpy(&rxkadToken->rk_key, cred.session, 8);
-    rxkadToken->rk_ticket.rk_ticket_len = cred.ticket_st.length;
-    rxkadToken->rk_ticket.rk_ticket_val = malloc(cred.ticket_st.length);
-    if (rxkadToken->rk_ticket.rk_ticket_val == NULL) {
-       free(token);
-       return ENOMEM;
+    token.endTime = v5cred->times.endtime;
+    memcpy(&token.sessionKey, cred.session, 8);
+    token.ticketLen = cred.ticket_st.length;
+    memcpy(token.ticket, cred.ticket_st.dat, token.ticketLen);
+
+    status = token_importRxkadViceId(tokenPtr, &token, 0);
+    if (status) {
+       return status;
     }
-    memcpy(rxkadToken->rk_ticket.rk_ticket_val, cred.ticket_st.dat,
-          rxkadToken->rk_ticket.rk_ticket_len);
 
-    *tokenPtr = token;
     *userPtr = strdup(username);
 
     return 0;
@@ -881,7 +843,6 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
     char *realmUsed = NULL;
     char *username = NULL;
     int status;
-    size_t len;
 
     *token = NULL;
     *authuser = NULL;
@@ -906,9 +867,7 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
        username = NULL;
        *foreign = 0;
     } else {
-       len = strlen(username)+strlen(realmUsed)+2;
-       *authuser = malloc(len);
-       afs_snprintf(*authuser, len, "%s@%s", username, realmUsed);
+       asprintf(authuser, "%s@%s", username, realmUsed);
        *foreign = 1;
     }
 
@@ -927,7 +886,8 @@ out:
  * to.
  */
 static int
-auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
+auth_to_cell(krb5_context context, const char *config, 
+            char *cell, char *realm, char **linkedcell)
 {
     int status = AKLOG_SUCCESS;
     int isForeign = 0;
@@ -941,7 +901,7 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
     struct afsconf_cell cellconf;
 
     /* NULL or empty cell returns information on local cell */
-    if ((status = get_cellconfig(cell, &cellconf, &local_cell)))
+    if ((status = get_cellconfig(config, cell, &cellconf, &local_cell)))
        return(status);
 
     if (linkedcell != NULL) {
@@ -1090,8 +1050,13 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
 #endif /* ALLOW_REGISTER */
 
            if ((status == 0) && (viceId != ANONYMOUSID)) {
-               rxkadToken->ktc_tokenUnion_u.at_kad.rk_viceid = viceId;
-               token_replaceToken(token, rxkadToken);
+               status = token_setRxkadViceId(rxkadToken, viceId);
+               if (status) {
+                   fprintf(stderr, "Error %d setting rxkad ViceId\n", status);
+                   status = AKLOG_SUCCESS;
+               } else {
+                   token_replaceToken(token, rxkadToken);
+               }
            }
        }
 
@@ -1118,8 +1083,7 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
 
 out:
     if (rxkadToken) {
-       free(rxkadToken->ktc_tokenUnion_u.at_kad.rk_ticket.rk_ticket_val);
-       free(rxkadToken);
+       token_freeToken(&rxkadToken);
     }
 
     if (local_cell)
@@ -1228,12 +1192,16 @@ next_path(char *origpath)
            ? elast_comp - last_comp : strlen(last_comp);
        strncat(pathtocheck, last_comp, len);
        memset(linkbuf, 0, sizeof(linkbuf));
-       if ((link = (readlink(pathtocheck, linkbuf,
-                                   sizeof(linkbuf)) > 0))) {
+       link = readlink(pathtocheck, linkbuf, sizeof(linkbuf)-1);
+
+       if (link > 0) {
+           linkbuf[link] = '\0'; /* NUL terminate string */
+
            if (++symlinkcount > MAXSYMLINKS) {
                fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
                exit(AKLOG_BADPATH);
            }
+
            memset(tmpbuf, 0, sizeof(tmpbuf));
            if (elast_comp)
                strcpy(tmpbuf, elast_comp);
@@ -1333,7 +1301,7 @@ add_hosts(char *file)
  * every cell it encounters along the way.
  */
 static int
-auth_to_path(krb5_context context, char *path)
+auth_to_path(krb5_context context, const char *config, char *path)
 {
     int status = AKLOG_SUCCESS;
     int auth_status = AKLOG_SUCCESS;
@@ -1383,7 +1351,8 @@ auth_to_path(krb5_context context, char *path)
                add_hosts(pathtocheck);
            if ((endofcell = strchr(mountpoint, VOLMARKER))) {
                *endofcell = '\0';
-               if ((auth_status = auth_to_cell(context, cell, NULL, NULL))) {
+               auth_status = auth_to_cell(context, config, cell, NULL, NULL);
+               if (auth_status) {
                    if (status == AKLOG_SUCCESS)
                        status = auth_status;
                    else if (status != auth_status)
@@ -1471,6 +1440,7 @@ main(int argc, char *argv[])
     linked_list paths;         /* List of paths to log to */
     ll_node *cur_node;
     char *linkedcell;
+    const char *config = AFSDIR_CLIENT_ETC_DIRPATH;
 
     memset(&cellinfo, 0, sizeof(cellinfo));
 
@@ -1490,7 +1460,30 @@ main(int argc, char *argv[])
     else
        progname = argv[0];
 
-    krb5_init_context(&context);
+#if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
+    {
+       char *filepath = NULL, *newpath = NULL;
+#ifndef AFS_DARWIN_ENV
+       char *defaultpath = "/etc/krb5.conf";
+#else
+       char *defaultpath = "~/Library/Preferences/edu.mit.Kerberos:/Library/Preferences/edu.mit.Kerberos";
+#endif
+       filepath = getenv("KRB5_CONFIG");
+       asprintf(&newpath, "%s:%s/krb5-weak.conf",
+                filepath ? filepath : defaultpath,
+                AFSDIR_CLIENT_ETC_DIRPATH);
+       setenv("KRB5_CONFIG", newpath, 1);
+#endif
+       krb5_init_context(&context);
+
+#if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
+       free(newpath);
+       if (filepath)
+           setenv("KRB5_CONFIG", filepath, 1);
+       else
+           unsetenv("KRB5_CONFIG");
+    }
+#endif
     initialize_KTC_error_table ();
     initialize_U_error_table();
     initialize_RXK_error_table();
@@ -1564,7 +1557,12 @@ main(int argc, char *argv[])
            }
            else
                usage();
-
+       else if (strcmp(argv[i], "-config") == 0)
+           if (++i < argc) {
+               config = argv[i];
+           }
+           else
+               usage();
        else if (argv[i][0] == '-')
            usage();
        else if (!pmode && !cmode) {
@@ -1638,7 +1636,7 @@ main(int argc, char *argv[])
     if ((cells.nelements + paths.nelements) == 0) {
        struct passwd *pwd;
 
-       status = auth_to_cell(context, NULL, NULL, &linkedcell);
+       status = auth_to_cell(context, config, NULL, NULL, &linkedcell);
 
        /* If this cell is linked to a DCE cell, and user requested -linked,
         * get tokens for both. This is very useful when the AFS cell is
@@ -1647,7 +1645,7 @@ main(int argc, char *argv[])
 
        if (!status && linked && linkedcell != NULL) {
            afs_dprintf("Linked cell: %s\n", linkedcell);
-           status = auth_to_cell(context, linkedcell, NULL, NULL);
+           status = auth_to_cell(context, config, linkedcell, NULL, NULL);
        }
        if (linkedcell) {
            free(linkedcell);
@@ -1681,7 +1679,7 @@ main(int argc, char *argv[])
 
                    afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
 
-                   auth_status = auth_to_cell(context, fcell, NULL, NULL);
+                   auth_status = auth_to_cell(context, config, fcell, NULL, NULL);
                    if (status == AKLOG_SUCCESS)
                        status = auth_status;
                    else
@@ -1694,14 +1692,16 @@ main(int argc, char *argv[])
        /* Log to all cells in the cells list first */
        for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
            memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
-           if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm,
-                                      &linkedcell)))
+           status = auth_to_cell(context, config, cellinfo.cell,
+                                 cellinfo.realm, &linkedcell);
+           if (status) {
                somethingswrong++;
-           else {
+           } else {
                if (linked && linkedcell != NULL) {
                    afs_dprintf("Linked cell: %s\n", linkedcell);
-                   if ((status = auth_to_cell(context, linkedcell,
-                                              cellinfo.realm, NULL)))
+                   status = auth_to_cell(context, config, linkedcell,
+                                         cellinfo.realm, NULL);
+                   if (status)
                        somethingswrong++;
                }
                if (linkedcell != NULL) {
@@ -1713,7 +1713,8 @@ main(int argc, char *argv[])
 
        /* Then, log to all paths in the paths list */
        for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
-           if ((status = auth_to_path(context, cur_node->data)))
+           status = auth_to_path(context, config, cur_node->data);
+           if (status)
                somethingswrong++;
        }
 
@@ -1783,7 +1784,7 @@ get_credv5_akimpersonate(krb5_context context,
     EncTicketPart enc_tkt_reply[1];
     krb5_address address[30];
     krb5_addresses faddr[1];
-    int temp_vno[1];
+    unsigned int temp_vno[1];
     time_t temp_time[2];
 #else
     krb5_ticket ticket_reply[1];
@@ -1987,7 +1988,7 @@ get_credv5_akimpersonate(krb5_context context,
         }
     } /* crypto block */
     ticket_reply->enc_part.etype = deref_entry_enctype(entry);
-    ticket_reply->enc_part.kvno = temp_vno;
+    ticket_reply->enc_part.kvno = (void *)temp_vno;
     *ticket_reply->enc_part.kvno = entry->vno;
     ticket_reply->tkt_vno = 5;
 #else
@@ -2094,8 +2095,8 @@ get_credv5(krb5_context context, char *name, char *inst, char *realm,
     if ((r = krb5_build_principal(context, &increds.server,
                                  strlen(realm), realm,
                                  name,
-                                 (inst && strlen(inst)) ? inst : (void *) NULL,
-                                 (void *) NULL))) {
+                                 (inst && strlen(inst)) ? inst : NULL,
+                                 NULL))) {
         return r;
     }
 
@@ -2148,8 +2149,11 @@ get_user_realm(krb5_context context, char **realm)
 
     *realm = NULL;
 
-    if (!_krb425_ccache)
-        krb5_cc_default(context, &_krb425_ccache);
+    if (!_krb425_ccache) {
+       r = krb5_cc_default(context, &_krb425_ccache);
+       if (r)
+           return r;
+    }
     if (!client_principal) {
        if (client) {
            r = krb5_parse_name(context, client,  &client_principal);