AFSAuth.cxx

Go to the documentation of this file.
00001 // @(#)root/auth:$Id: AFSAuth.cxx 21664 2008-01-13 12:38:42Z brun $
00002 // Author: G. Ganis, Nov 2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // AFSAuth                                                              //
00015 //                                                                      //
00016 // Utility functions to acquire and handle AFS tokens.                  //
00017 // These functions are available as separate plugin, libAFSAuth.so,     //
00018 // depending aonly on the AFS libraries.                                //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include <string.h>
00023 
00024 #include "AFSAuth.h"
00025 
00026 extern "C" {
00027 #include <afs/stds.h>
00028 #include <afs/kautils.h>
00029 #include <afs/com_err.h>
00030 afs_int32 ka_Authenticate(char *name, char *instance, char *cell,
00031                           struct ubik_client *conn, int service,
00032                           struct ktc_encryptionKey *key, Date start,
00033                           Date end, struct ktc_token *token,
00034                           afs_int32 * pwexpires);
00035 afs_int32 ka_AuthServerConn(char *cell, int service,
00036                             struct ktc_token *token,
00037                             struct ubik_client **conn);
00038 afs_int32 ka_GetAuthToken(char *name, char *instance, char *cell,
00039                           struct ktc_encryptionKey *key,
00040                           afs_int32 lifetime, afs_int32 *pwexpires);
00041 afs_int32 ka_GetAFSTicket(char *name, char *instance, char *realm,
00042                           Date lifetime, afs_int32 flags);
00043 char *ka_LocalCell();
00044 void      ka_StringToKey(char *str, char *cell,
00045                          struct ktc_encryptionKey *key);
00046 int ktc_GetToken(struct ktc_principal *server, struct ktc_token *token,
00047                  int tokenLen, struct ktc_principal *client);
00048 
00049 typedef struct ktc_token AFStoken_t;
00050 
00051 //________________________________________________________________________
00052 char *GetAFSErrorString(afs_int32 rc)
00053 {
00054    // Decode the error code returning a pointer to a human
00055    // readable string. The two additional messages are taken from the OpenAFS
00056    // source (src/kauth/user.c).
00057 
00058    const char *emsg = 0;
00059    if (rc) {
00060       switch (rc) {
00061          case KABADREQUEST:
00062             emsg = "password was incorrect";
00063             break;
00064          case KAUBIKCALL:
00065             emsg = "Authentication Server was unavailable";
00066             break;
00067          default:
00068 #ifdef R__AFSOLDCOMERR
00069             emsg = error_message(rc);
00070 #else
00071             emsg = afs_error_message(rc);
00072 #endif
00073       }
00074    } else {
00075       emsg = "";
00076    }
00077 
00078    // Done
00079    return (char *)emsg;
00080 }
00081 
00082 
00083 //________________________________________________________________________
00084 void *GetAFSToken(const char *usr, const char *pwd, int pwlen,
00085                   int life, char **emsg)
00086 {
00087    // Get AFS token for the local cell for 'usr'. The meaning of the
00088    // information passed at 'pwd' depends on 'pwlen'. For 'pwlen <= 0'
00089    // 'pwd' is interpreted as the plain password (null terminated string).
00090    // For 'pwlen > 0', the 'pwlen' bytes at 'pwd' contain the password in
00091    // for of encryption key (struct ktc_encryptionKey).
00092    // On success a token is returned as opaque information.
00093    // On error / failure, 0 is returned; if emsg != 0, *emsg points to an
00094    // error message.
00095 
00096    // reset the error message, if defined
00097    if (emsg)
00098       *emsg = "";
00099 
00100    // Check user name
00101    if (!usr || strlen(usr) <= 0) {
00102       if (emsg)
00103          *emsg = "Input user name undefined - check your inputs!";
00104       return (void *)0;
00105    }
00106 
00107    // Check password buffer
00108    if (!pwd || (pwlen <= 0 && strlen(pwd) <= 0)) {
00109       if (emsg)
00110          *emsg = "Password buffer undefined - check your inputs!";
00111       return (void *)0;
00112    }
00113 
00114    // Check lifetime
00115    if (life < 0) {
00116       // Use default
00117       life = DFLTTOKENLIFETIME;
00118    } else if (life == 0) {
00119       // Shortest possible (5 min; smaller values are ignored)
00120       life = 300;
00121    }
00122 
00123    // Init error tables and connect to the correct CellServDB file
00124    afs_int32 rc = 0;
00125    if ((rc = ka_Init(0))) {
00126       // Failure
00127       if (emsg)
00128          *emsg = GetAFSErrorString(rc);
00129       return (void *)0;
00130    }
00131 
00132    // Fill the encryption key
00133    struct ktc_encryptionKey key;
00134    if (pwlen > 0) {
00135       // Just copy the input buffer
00136       memcpy(key.data, pwd, pwlen);
00137    } else {
00138       // Get rid of '\n', if any
00139       int len = strlen(pwd);
00140       if (pwd[len-1] == '\n')
00141          len--;
00142       char *pw = new char[len + 1];
00143       memcpy(pw, pwd, len);
00144       pw[len] = 0;
00145       // Create the key from the password
00146       ka_StringToKey(pw, 0, &key);
00147       delete[] pw;
00148    }
00149 
00150    // Get the cell
00151    char *cell = 0;
00152    char cellname[MAXKTCREALMLEN];
00153    if (ka_ExpandCell(cell, cellname, 0) != 0) {
00154       if (emsg)
00155          *emsg = "Could not expand cell name";
00156       return (void *)0;
00157    }
00158    cell = cellname;
00159 
00160    // Get an unauthenticated connection to desired cell
00161    struct ubik_client *conn = 0;
00162    if (ka_AuthServerConn(cell, KA_AUTHENTICATION_SERVICE, 0, &conn) != 0) {
00163       if (emsg)
00164          *emsg = "Could not get a connection to server";
00165       return (void *)0;
00166    }
00167 
00168    // Authenticate now
00169    AFStoken_t *tkn = new AFStoken_t;
00170    int pwexpires;
00171    int now = time(0);
00172    rc = 0;
00173    if ((rc = ka_Authenticate((char *)usr, (char *)"", cell, conn,
00174                              KA_TICKET_GRANTING_SERVICE,
00175                              &key, now, now + life, tkn, &pwexpires))) {
00176       // Failure
00177       if (emsg)
00178          *emsg = GetAFSErrorString(rc);
00179       ubik_ClientDestroy(conn);
00180       return (void *)0;
00181    }
00182 
00183    // Now get a ticket to access user's AFS private area
00184    if ((rc = ka_GetAuthToken((char *)usr, "", "", &key, life, &pwexpires))) {
00185       // Failure
00186       if (emsg)
00187          *emsg = GetAFSErrorString(rc);
00188       ubik_ClientDestroy(conn);
00189       return (void *)0;
00190    }
00191    if ((rc = ka_GetAFSTicket((char *)usr, "", "", life,
00192                              KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG))) {
00193       // Failure
00194       if (emsg)
00195          *emsg = GetAFSErrorString(rc);
00196       ubik_ClientDestroy(conn);
00197       return (void *)0;
00198    }
00199 
00200    // Release the connection to the server
00201    ubik_ClientDestroy(conn);
00202 
00203    // Success
00204    return (void *)tkn;
00205 }
00206 
00207 //________________________________________________________________________
00208 int VerifyAFSToken(void *token)
00209 {
00210    // Verify validity an AFS token. The opaque input information is the one
00211    // returned by a successful call to GetAFSToken.
00212    // The remaining lifetime is returned, i.e. <=0 if expired.
00213 
00214    // Check input
00215    if (!token)
00216       return 0;
00217 
00218    // Unveil it
00219    AFStoken_t *tkn = (AFStoken_t *) token;
00220 
00221    // Compare expiration time with now
00222    return ((int) tkn->endTime - time(0));
00223 
00224 }
00225 
00226 //________________________________________________________________________
00227 void DeleteAFSToken(void *token)
00228 {
00229    // Delete an AFS token returned by a successful call to GetAFSToken.
00230 
00231    if (token)
00232       delete (AFStoken_t *)token;
00233 }
00234 
00235 //________________________________________________________________________
00236 char *AFSLocalCell()
00237 {
00238    // Returns a pointer to a string with the local cell. The string must
00239    // not be freed or deleted.
00240 
00241    return ka_LocalCell();
00242 }
00243 
00244 }

Generated on Tue Jul 5 14:45:30 2011 for ROOT_528-00b_version by  doxygen 1.5.1