| 1 | /* | 
|---|
| 2 | *  Unix SMB/CIFS implementation. | 
|---|
| 3 | *  Generate AFS tickets | 
|---|
| 4 | *  Copyright (C) Volker Lendecke 2003 | 
|---|
| 5 | * | 
|---|
| 6 | *  This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | *  it under the terms of the GNU General Public License as published by | 
|---|
| 8 | *  the Free Software Foundation; either version 3 of the License, or | 
|---|
| 9 | *  (at your option) any later version. | 
|---|
| 10 | * | 
|---|
| 11 | *  This program is distributed in the hope that it will be useful, | 
|---|
| 12 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | *  GNU General Public License for more details. | 
|---|
| 15 | * | 
|---|
| 16 | *  You should have received a copy of the GNU General Public License | 
|---|
| 17 | *  along with this program; if not, see <http://www.gnu.org/licenses/>. | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #include "includes.h" | 
|---|
| 21 |  | 
|---|
| 22 | #ifdef WITH_FAKE_KASERVER | 
|---|
| 23 |  | 
|---|
| 24 | #define NO_ASN1_TYPEDEFS 1 | 
|---|
| 25 |  | 
|---|
| 26 | #include <afs/stds.h> | 
|---|
| 27 | #include <afs/afs.h> | 
|---|
| 28 | #include <afs/auth.h> | 
|---|
| 29 | #include <afs/venus.h> | 
|---|
| 30 | #include <asm/unistd.h> | 
|---|
| 31 | #include <openssl/des.h> | 
|---|
| 32 |  | 
|---|
| 33 | struct ClearToken { | 
|---|
| 34 | uint32 AuthHandle; | 
|---|
| 35 | char HandShakeKey[8]; | 
|---|
| 36 | uint32 ViceId; | 
|---|
| 37 | uint32 BeginTimestamp; | 
|---|
| 38 | uint32 EndTimestamp; | 
|---|
| 39 | }; | 
|---|
| 40 |  | 
|---|
| 41 | static char *afs_encode_token(const char *cell, const DATA_BLOB ticket, | 
|---|
| 42 | const struct ClearToken *ct) | 
|---|
| 43 | { | 
|---|
| 44 | char *base64_ticket; | 
|---|
| 45 | char *result = NULL; | 
|---|
| 46 |  | 
|---|
| 47 | DATA_BLOB key = data_blob(ct->HandShakeKey, 8); | 
|---|
| 48 | char *base64_key; | 
|---|
| 49 | TALLOC_CTX *mem_ctx; | 
|---|
| 50 |  | 
|---|
| 51 | mem_ctx = talloc_stackframe(); | 
|---|
| 52 | if (mem_ctx == NULL) | 
|---|
| 53 | goto done; | 
|---|
| 54 |  | 
|---|
| 55 | base64_ticket = base64_encode_data_blob(mem_ctx, ticket); | 
|---|
| 56 | if (base64_ticket == NULL) | 
|---|
| 57 | goto done; | 
|---|
| 58 |  | 
|---|
| 59 | base64_key = base64_encode_data_blob(mem_ctx, key); | 
|---|
| 60 | if (base64_key == NULL) | 
|---|
| 61 | goto done; | 
|---|
| 62 |  | 
|---|
| 63 | asprintf(&result, "%s\n%u\n%s\n%u\n%u\n%u\n%s\n", cell, | 
|---|
| 64 | ct->AuthHandle, base64_key, ct->ViceId, ct->BeginTimestamp, | 
|---|
| 65 | ct->EndTimestamp, base64_ticket); | 
|---|
| 66 |  | 
|---|
| 67 | DEBUG(10, ("Got ticket string:\n%s\n", result)); | 
|---|
| 68 |  | 
|---|
| 69 | done: | 
|---|
| 70 | TALLOC_FREE(mem_ctx); | 
|---|
| 71 |  | 
|---|
| 72 | return result; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /* Create a ClearToken and an encrypted ticket. ClearToken has not yet the | 
|---|
| 76 | * ViceId set, this should be set by the caller. */ | 
|---|
| 77 |  | 
|---|
| 78 | static bool afs_createtoken(const char *username, const char *cell, | 
|---|
| 79 | DATA_BLOB *ticket, struct ClearToken *ct) | 
|---|
| 80 | { | 
|---|
| 81 | fstring clear_ticket; | 
|---|
| 82 | char *p = clear_ticket; | 
|---|
| 83 | uint32 len; | 
|---|
| 84 | uint32 now; | 
|---|
| 85 |  | 
|---|
| 86 | struct afs_key key; | 
|---|
| 87 | des_key_schedule key_schedule; | 
|---|
| 88 |  | 
|---|
| 89 | if (!secrets_init()) | 
|---|
| 90 | return False; | 
|---|
| 91 |  | 
|---|
| 92 | if (!secrets_fetch_afs_key(cell, &key)) { | 
|---|
| 93 | DEBUG(1, ("Could not fetch AFS service key\n")); | 
|---|
| 94 | return False; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | ct->AuthHandle = key.kvno; | 
|---|
| 98 |  | 
|---|
| 99 | /* Build the ticket. This is going to be encrypted, so in our | 
|---|
| 100 | way we fill in ct while we still have the unencrypted | 
|---|
| 101 | form. */ | 
|---|
| 102 |  | 
|---|
| 103 | p = clear_ticket; | 
|---|
| 104 |  | 
|---|
| 105 | /* The byte-order */ | 
|---|
| 106 | *p = 1; | 
|---|
| 107 | p += 1; | 
|---|
| 108 |  | 
|---|
| 109 | /* "Alice", the client username */ | 
|---|
| 110 | strncpy(p, username, sizeof(clear_ticket)-PTR_DIFF(p,clear_ticket)-1); | 
|---|
| 111 | p += strlen(p)+1; | 
|---|
| 112 | strncpy(p, "", sizeof(clear_ticket)-PTR_DIFF(p,clear_ticket)-1); | 
|---|
| 113 | p += strlen(p)+1; | 
|---|
| 114 | strncpy(p, cell, sizeof(clear_ticket)-PTR_DIFF(p,clear_ticket)-1); | 
|---|
| 115 | p += strlen(p)+1; | 
|---|
| 116 |  | 
|---|
| 117 | /* Alice's network layer address. At least Openafs-1.2.10 | 
|---|
| 118 | ignores this, so we fill in a dummy value here. */ | 
|---|
| 119 | SIVAL(p, 0, 0); | 
|---|
| 120 | p += 4; | 
|---|
| 121 |  | 
|---|
| 122 | /* We need to create a session key */ | 
|---|
| 123 | generate_random_buffer(p, 8); | 
|---|
| 124 |  | 
|---|
| 125 | /* Our client code needs the the key in the clear, it does not | 
|---|
| 126 | know the server-key ... */ | 
|---|
| 127 | memcpy(ct->HandShakeKey, p, 8); | 
|---|
| 128 |  | 
|---|
| 129 | p += 8; | 
|---|
| 130 |  | 
|---|
| 131 | /* This is a kerberos 4 life time. The life time is expressed | 
|---|
| 132 | * in units of 5 minute intervals up to 38400 seconds, after | 
|---|
| 133 | * that a table is used up to lifetime 0xBF. Values between | 
|---|
| 134 | * 0xC0 and 0xFF is undefined. 0xFF is defined to be the | 
|---|
| 135 | * infinite time that never expire. | 
|---|
| 136 | * | 
|---|
| 137 | * So here we cheat and use the infinite time */ | 
|---|
| 138 | *p = 255; | 
|---|
| 139 | p += 1; | 
|---|
| 140 |  | 
|---|
| 141 | /* Ticket creation time */ | 
|---|
| 142 | now = time(NULL); | 
|---|
| 143 | SIVAL(p, 0, now); | 
|---|
| 144 | ct->BeginTimestamp = now; | 
|---|
| 145 |  | 
|---|
| 146 | if(lp_afs_token_lifetime() == 0) | 
|---|
| 147 | ct->EndTimestamp = NEVERDATE; | 
|---|
| 148 | else | 
|---|
| 149 | ct->EndTimestamp = now + lp_afs_token_lifetime(); | 
|---|
| 150 |  | 
|---|
| 151 | if (((ct->EndTimestamp - ct->BeginTimestamp) & 1) == 1) { | 
|---|
| 152 | ct->BeginTimestamp += 1; /* Lifetime must be even */ | 
|---|
| 153 | } | 
|---|
| 154 | p += 4; | 
|---|
| 155 |  | 
|---|
| 156 | /* And here comes Bob's name and instance, in this case the | 
|---|
| 157 | AFS server. */ | 
|---|
| 158 | strncpy(p, "afs", sizeof(clear_ticket)-PTR_DIFF(p,clear_ticket)-1); | 
|---|
| 159 | p += strlen(p)+1; | 
|---|
| 160 | strncpy(p, "", sizeof(clear_ticket)-PTR_DIFF(p,clear_ticket)-1); | 
|---|
| 161 | p += strlen(p)+1; | 
|---|
| 162 |  | 
|---|
| 163 | /* And zero-pad to a multiple of 8 bytes */ | 
|---|
| 164 | len = PTR_DIFF(p, clear_ticket); | 
|---|
| 165 | if (len & 7) { | 
|---|
| 166 | uint32 extra_space = 8-(len & 7); | 
|---|
| 167 | memset(p, 0, extra_space); | 
|---|
| 168 | p+=extra_space; | 
|---|
| 169 | } | 
|---|
| 170 | len = PTR_DIFF(p, clear_ticket); | 
|---|
| 171 |  | 
|---|
| 172 | des_key_sched((const_des_cblock *)key.key, key_schedule); | 
|---|
| 173 | des_pcbc_encrypt(clear_ticket, clear_ticket, | 
|---|
| 174 | len, key_schedule, (C_Block *)key.key, 1); | 
|---|
| 175 |  | 
|---|
| 176 | ZERO_STRUCT(key); | 
|---|
| 177 |  | 
|---|
| 178 | *ticket = data_blob(clear_ticket, len); | 
|---|
| 179 |  | 
|---|
| 180 | return True; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | char *afs_createtoken_str(const char *username, const char *cell) | 
|---|
| 184 | { | 
|---|
| 185 | DATA_BLOB ticket; | 
|---|
| 186 | struct ClearToken ct; | 
|---|
| 187 | char *result; | 
|---|
| 188 |  | 
|---|
| 189 | if (!afs_createtoken(username, cell, &ticket, &ct)) | 
|---|
| 190 | return NULL; | 
|---|
| 191 |  | 
|---|
| 192 | result = afs_encode_token(cell, ticket, &ct); | 
|---|
| 193 |  | 
|---|
| 194 | data_blob_free(&ticket); | 
|---|
| 195 |  | 
|---|
| 196 | return result; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | /* | 
|---|
| 200 | This routine takes a radical approach completely bypassing the | 
|---|
| 201 | Kerberos idea of security and using AFS simply as an intelligent | 
|---|
| 202 | file backend. Samba has persuaded itself somehow that the user is | 
|---|
| 203 | actually correctly identified and then we create a ticket that the | 
|---|
| 204 | AFS server hopefully accepts using its KeyFile that the admin has | 
|---|
| 205 | kindly stored to our secrets.tdb. | 
|---|
| 206 |  | 
|---|
| 207 | Thanks to the book "Network Security -- PRIVATE Communication in a | 
|---|
| 208 | PUBLIC World" by Charlie Kaufman, Radia Perlman and Mike Speciner | 
|---|
| 209 | Kerberos 4 tickets are not really hard to construct. | 
|---|
| 210 |  | 
|---|
| 211 | For the comments "Alice" is the User to be auth'ed, and "Bob" is the | 
|---|
| 212 | AFS server.  */ | 
|---|
| 213 |  | 
|---|
| 214 | bool afs_login(connection_struct *conn) | 
|---|
| 215 | { | 
|---|
| 216 | extern userdom_struct current_user_info; | 
|---|
| 217 | extern struct current_user current_user; | 
|---|
| 218 | DATA_BLOB ticket; | 
|---|
| 219 | char *afs_username = NULL; | 
|---|
| 220 | char *cell = NULL; | 
|---|
| 221 | bool result; | 
|---|
| 222 | char *ticket_str = NULL; | 
|---|
| 223 | const DOM_SID *user_sid; | 
|---|
| 224 | TALLOC_CTX *ctx = talloc_tos(); | 
|---|
| 225 |  | 
|---|
| 226 | struct ClearToken ct; | 
|---|
| 227 |  | 
|---|
| 228 | afs_username = talloc_strdup(ctx, | 
|---|
| 229 | lp_afs_username_map()); | 
|---|
| 230 | if (!afs_username) { | 
|---|
| 231 | return false; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | afs_username = talloc_sub_advanced(ctx, | 
|---|
| 235 | SNUM(conn), conn->user, | 
|---|
| 236 | conn->connectpath, conn->gid, | 
|---|
| 237 | get_current_username(), | 
|---|
| 238 | current_user_info.domain, | 
|---|
| 239 | afs_username); | 
|---|
| 240 | if (!afs_username) { | 
|---|
| 241 | return false; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | user_sid = ¤t_user.nt_user_token->user_sids[0]; | 
|---|
| 245 | afs_username = talloc_string_sub(talloc_tos(), | 
|---|
| 246 | afs_username, | 
|---|
| 247 | "%s", | 
|---|
| 248 | sid_string_tos(user_sid)); | 
|---|
| 249 | if (!afs_username) { | 
|---|
| 250 | return false; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | /* The pts command always generates completely lower-case user | 
|---|
| 254 | * names. */ | 
|---|
| 255 | strlower_m(afs_username); | 
|---|
| 256 |  | 
|---|
| 257 | cell = strchr(afs_username, '@'); | 
|---|
| 258 |  | 
|---|
| 259 | if (cell == NULL) { | 
|---|
| 260 | DEBUG(1, ("AFS username doesn't contain a @, " | 
|---|
| 261 | "could not find cell\n")); | 
|---|
| 262 | return false; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | *cell = '\0'; | 
|---|
| 266 | cell += 1; | 
|---|
| 267 |  | 
|---|
| 268 | DEBUG(10, ("Trying to log into AFS for user %s@%s\n", | 
|---|
| 269 | afs_username, cell)); | 
|---|
| 270 |  | 
|---|
| 271 | if (!afs_createtoken(afs_username, cell, &ticket, &ct)) | 
|---|
| 272 | return False; | 
|---|
| 273 |  | 
|---|
| 274 | /* For which Unix-UID do we want to set the token? */ | 
|---|
| 275 | ct.ViceId = getuid(); | 
|---|
| 276 |  | 
|---|
| 277 | ticket_str = afs_encode_token(cell, ticket, &ct); | 
|---|
| 278 |  | 
|---|
| 279 | result = afs_settoken_str(ticket_str); | 
|---|
| 280 |  | 
|---|
| 281 | SAFE_FREE(ticket_str); | 
|---|
| 282 |  | 
|---|
| 283 | data_blob_free(&ticket); | 
|---|
| 284 |  | 
|---|
| 285 | return result; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | #else | 
|---|
| 289 |  | 
|---|
| 290 | bool afs_login(connection_struct *conn) | 
|---|
| 291 | { | 
|---|
| 292 | return True; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | char *afs_createtoken_str(const char *username, const char *cell) | 
|---|
| 296 | { | 
|---|
| 297 | return False; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | #endif /* WITH_FAKE_KASERVER */ | 
|---|