00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00055
00056
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
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
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 if (emsg)
00098 *emsg = "";
00099
00100
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
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
00115 if (life < 0) {
00116
00117 life = DFLTTOKENLIFETIME;
00118 } else if (life == 0) {
00119
00120 life = 300;
00121 }
00122
00123
00124 afs_int32 rc = 0;
00125 if ((rc = ka_Init(0))) {
00126
00127 if (emsg)
00128 *emsg = GetAFSErrorString(rc);
00129 return (void *)0;
00130 }
00131
00132
00133 struct ktc_encryptionKey key;
00134 if (pwlen > 0) {
00135
00136 memcpy(key.data, pwd, pwlen);
00137 } else {
00138
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
00146 ka_StringToKey(pw, 0, &key);
00147 delete[] pw;
00148 }
00149
00150
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
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
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
00177 if (emsg)
00178 *emsg = GetAFSErrorString(rc);
00179 ubik_ClientDestroy(conn);
00180 return (void *)0;
00181 }
00182
00183
00184 if ((rc = ka_GetAuthToken((char *)usr, "", "", &key, life, &pwexpires))) {
00185
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
00194 if (emsg)
00195 *emsg = GetAFSErrorString(rc);
00196 ubik_ClientDestroy(conn);
00197 return (void *)0;
00198 }
00199
00200
00201 ubik_ClientDestroy(conn);
00202
00203
00204 return (void *)tkn;
00205 }
00206
00207
00208 int VerifyAFSToken(void *token)
00209 {
00210
00211
00212
00213
00214
00215 if (!token)
00216 return 0;
00217
00218
00219 AFStoken_t *tkn = (AFStoken_t *) token;
00220
00221
00222 return ((int) tkn->endTime - time(0));
00223
00224 }
00225
00226
00227 void DeleteAFSToken(void *token)
00228 {
00229
00230
00231 if (token)
00232 delete (AFStoken_t *)token;
00233 }
00234
00235
00236 char *AFSLocalCell()
00237 {
00238
00239
00240
00241 return ka_LocalCell();
00242 }
00243
00244 }