2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* ticket caching code */
12 #include <afs/param.h>
13 #include <afsconfig.h>
19 #include <afs/pthread_glock.h>
20 #include <sys/types.h>
26 #include <afs/smb_iocons.h>
27 #include <afs/pioctl_nt.h>
28 #include "../WINNT/afsd/afsrpc.h"
31 #include <afs/afsutil.h>
34 /* Forward declarations for local token cache. */
35 static int SetLocalToken(struct ktc_principal *aserver,
36 struct ktc_token *atoken,
37 struct ktc_principal *aclient,
39 static int GetLocalToken(struct ktc_principal *aserver,
40 struct ktc_token *atoken,
42 struct ktc_principal *aclient);
43 static int ForgetLocalTokens();
44 static int ForgetOneLocalToken(struct ktc_principal *aserver);
47 static char AFSConfigKeyName[] =
48 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
51 * Support for RPC's to send and receive session keys
53 * Session keys must not be sent and received in the clear. We have no
54 * way to piggyback encryption on SMB, so we use a separate RPC, using
55 * packet privacy (when available). In SetToken, the RPC is done first;
56 * in GetToken, the pioctl is done first.
61 void __RPC_FAR * __RPC_USER midl_user_allocate (size_t cBytes)
63 return ((void __RPC_FAR *) malloc(cBytes));
66 void __RPC_USER midl_user_free(void __RPC_FAR * p)
72 * Determine the server name to be used in the RPC binding. If it is
73 * the same as the client (i.e. standalone, non-gateway), NULL can be
74 * used, so it is not necessary to call gethostbyname().
76 void getservername(char **snp, unsigned int snSize)
81 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
82 0, KEY_QUERY_VALUE, &parmKey);
83 if (code != ERROR_SUCCESS)
85 code = RegQueryValueEx(parmKey, "Gateway", NULL, NULL,
87 RegCloseKey (parmKey);
88 if (code == ERROR_SUCCESS)
91 /* No gateway name in registry; use ourself */
95 RPC_STATUS send_key(afs_uuid_t uuid, char sessionKey[8])
98 char *stringBinding = NULL;
99 ULONG authnLevel, authnSvc;
100 char serverName[256];
101 char *serverNamep = serverName;
103 BOOL encryptionOff = FALSE;
106 /* Encryption on by default */
107 if (GetEnvironmentVariable("AFS_RPC_ENCRYPT",
108 encrypt, sizeof(encrypt)))
109 if (!strcmpi(encrypt, "OFF"))
110 encryptionOff = TRUE;
112 /* Protocol sequence is named pipe by default */
113 if (!GetEnvironmentVariable("AFS_RPC_PROTSEQ",
114 protseq, sizeof(protseq)))
115 strcpy(protseq, "ncacn_np");
118 getservername(&serverNamep, sizeof(serverName));
120 status = RpcStringBindingCompose("", /* obj uuid */
124 "", /* protocol options */
126 if (status != RPC_S_OK)
129 status = RpcBindingFromStringBinding(stringBinding, &hAfsHandle);
130 if (status != RPC_S_OK)
134 * On Windows 95/98, we must resolve the binding before calling
135 * SetAuthInfo. On Windows NT, we don't have to resolve yet,
136 * but it does no harm.
138 status = RpcEpResolveBinding(hAfsHandle, afsrpc_v1_0_c_ifspec);
139 if (status != RPC_S_OK)
143 authnLevel = RPC_C_AUTHN_LEVEL_NONE;
144 authnSvc = RPC_C_AUTHN_WINNT;
146 authnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
147 authnSvc = RPC_C_AUTHN_WINNT;
150 status = RpcBindingSetAuthInfo(hAfsHandle, NULL, authnLevel, authnSvc,
151 NULL, RPC_C_AUTHZ_NONE);
152 if (status != RPC_S_OK)
156 status = AFSRPC_SetToken(uuid, sessionKey);
159 status = RpcExceptionCode();
165 RpcStringFree(&stringBinding);
167 if (hAfsHandle != NULL)
168 RpcBindingFree(&hAfsHandle);
173 RPC_STATUS receive_key(afs_uuid_t uuid, char sessionKey[8])
176 char *stringBinding = NULL;
177 ULONG authnLevel, authnSvc;
178 char serverName[256];
179 char *serverNamep = serverName;
181 BOOL encryptionOff = FALSE;
184 /* Encryption on by default */
185 if (GetEnvironmentVariable("AFS_RPC_ENCRYPT",
186 encrypt, sizeof(encrypt)))
187 if (!strcmpi(encrypt, "OFF"))
188 encryptionOff = TRUE;
190 /* Protocol sequence is named pipe by default */
191 if (!GetEnvironmentVariable("AFS_RPC_PROTSEQ",
192 protseq, sizeof(protseq)))
193 strcpy(protseq, "ncacn_np");
196 getservername(&serverNamep, sizeof(serverName));
198 status = RpcStringBindingCompose("", /* obj uuid */
202 "", /* protocol options */
204 if (status != RPC_S_OK)
207 status = RpcBindingFromStringBinding(stringBinding, &hAfsHandle);
208 if (status != RPC_S_OK)
212 * On Windows 95/98, we must resolve the binding before calling
213 * SetAuthInfo. On Windows NT, we don't have to resolve yet,
214 * but it does no harm.
216 status = RpcEpResolveBinding(hAfsHandle, afsrpc_v1_0_c_ifspec);
217 if (status != RPC_S_OK)
221 authnLevel = RPC_C_AUTHN_LEVEL_NONE;
222 authnSvc = RPC_C_AUTHN_WINNT;
224 authnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
225 authnSvc = RPC_C_AUTHN_WINNT;
228 status = RpcBindingSetAuthInfo(hAfsHandle, NULL, authnLevel, authnSvc,
229 NULL, RPC_C_AUTHZ_NONE);
230 if (status != RPC_S_OK)
234 status = AFSRPC_GetToken(uuid, sessionKey);
237 status = RpcExceptionCode();
243 RpcStringFree(&stringBinding);
245 if (hAfsHandle != NULL)
246 RpcBindingFree(&hAfsHandle);
252 struct ktc_principal *server,
253 struct ktc_token *token,
254 struct ktc_principal *client,
257 struct ViceIoctl iob;
260 struct ClearToken ct;
266 if (token->ticketLen < MINKTCTICKETLEN
267 || token->ticketLen > MAXKTCTICKETLEN)
270 if (strcmp(server->name, "afs")) {
271 return SetLocalToken(server, token, client, flags);
277 memcpy(tp, &token->ticketLen, sizeof(token->ticketLen));
278 tp += sizeof(&token->ticketLen);
281 memcpy(tp, token->ticket, token->ticketLen);
282 tp += token->ticketLen;
285 ct.AuthHandle = token->kvno;
287 * Instead of sending the session key in the clear, we zero it,
288 * and send it later, via RPC, encrypted.
290 #ifndef AFS_WIN95_ENV
292 memcpy(ct.HandShakeKey, &token->sessionKey, sizeof(token->sessionKey));
294 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
296 memcpy(ct.HandShakeKey, &token->sessionKey, sizeof(token->sessionKey));
298 ct.BeginTimestamp = token->startTime;
299 ct.EndTimestamp = token->endTime;
300 if (ct.BeginTimestamp == 0) ct.BeginTimestamp = 1;
302 /* We don't know from Vice ID's yet */
303 ct.ViceId = 37; /* XXX */
304 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
305 ct.BeginTimestamp++; /* force lifetime to be even */
307 /* size of clear token */
308 temp = sizeof(struct ClearToken);
309 memcpy(tp, &temp, sizeof(temp));
312 /* clear token itself */
313 memcpy(tp, &ct, sizeof(ct));
316 /* flags; on NT there is no setpag flag, but there is an
317 * integrated logon flag */
318 temp = ((flags & AFS_SETTOK_LOGON) ? PIOCTL_LOGON : 0);
319 memcpy(tp, &temp, sizeof(temp));
323 temp = strlen(server->cell);
324 if (temp >= MAXKTCREALMLEN)
326 strcpy(tp, server->cell);
330 temp = strlen(client->name);
331 if (temp >= MAXKTCNAMELEN)
333 strcpy(tp, client->name);
337 status = UuidCreate((UUID *)&uuid);
338 memcpy(tp, &uuid, sizeof(uuid));
341 #ifndef AFS_WIN95_ENV
342 /* RPC to send session key */
343 status = send_key(uuid, token->sessionKey.data);
344 if (status != RPC_S_OK) {
346 strcpy(rpcErr, "RPC failure in AFS gateway");
348 DceErrorInqText(status, rpcErr);
349 if (status == RPC_S_SERVER_UNAVAILABLE
350 || status == EPT_S_NOT_REGISTERED)
355 #endif /* AFS_WIN95_ENV */
357 /* set up for pioctl */
359 iob.in_size = tp - tbuffer;
361 iob.out_size = sizeof(tbuffer);
363 code = pioctl(0, VIOCSETTOK, &iob, 0);
368 else if (errno == ENODEV)
370 else if (errno == EINVAL)
373 return KTC_PIOCTLFAIL;
376 return KTC_PIOCTLFAIL;
383 struct ktc_principal *server,
384 struct ktc_token *token,
386 struct ktc_principal *client)
388 struct ViceIoctl iob;
393 struct ClearToken ct;
403 /* check to see if the user is requesting tokens for a principal
404 * other than afs. If so, check the local token cache.
406 if (strcmp(server->name, "afs")) {
407 return GetLocalToken(server, token, tokenLen, client);
411 strcpy(tp, server->cell);
412 tp += strlen(server->cell) + 1;
415 status = UuidCreate((UUID *)&uuid);
416 memcpy(tp, &uuid, sizeof(uuid));
420 iob.in_size = tp - tbuffer;
422 iob.out_size = sizeof(tbuffer);
424 code = pioctl(0, VIOCNEWGETTOK, &iob, 0);
429 else if (errno == ENODEV)
431 else if (errno == EINVAL)
433 else if (errno == EDOM)
436 return KTC_PIOCTLFAIL;
439 return KTC_PIOCTLFAIL;
442 #ifndef AFS_WIN95_ENV /* get rid of RPC for win95 build */
443 /* RPC to receive session key */
444 status = receive_key(uuid, token->sessionKey.data);
445 if (status != RPC_S_OK) {
447 strcpy(rpcErr, "RPC failure in AFS gateway");
449 DceErrorInqText(status, rpcErr);
450 if (status == RPC_S_SERVER_UNAVAILABLE
451 || status == EPT_S_NOT_REGISTERED)
456 #endif /* AFS_WIN95_ENV */
461 memcpy(&ticketLen, cp, sizeof(ticketLen));
462 cp += sizeof(ticketLen);
464 /* remember where ticket is and skip over it */
468 /* size of clear token */
469 memcpy(&temp, cp, sizeof(temp));
471 if (temp != sizeof(ct))
475 memcpy(&ct, cp, temp);
478 /* skip over primary flag */
481 /* remember cell name and skip over it */
483 cellNameSize = strlen(cp);
484 cp += cellNameSize + 1;
486 /* user name is here */
488 /* check that ticket will fit */
489 maxLen = tokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
490 if (maxLen < ticketLen)
493 /* set return values */
494 memcpy(token->ticket, ticketP, ticketLen);
495 token->startTime = ct.BeginTimestamp;
496 token->endTime = ct.EndTimestamp;
497 if (ct.AuthHandle == -1) ct.AuthHandle = 999;
498 token->kvno = ct.AuthHandle;
499 #ifndef AFS_WIN95_ENV
501 * Session key has already been set via RPC
504 memcpy(&token->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
505 #endif /* AFS_WIN95_ENV */
506 token->ticketLen = ticketLen;
508 strcpy(client->name, cp);
509 client->instance[0] = '\0';
510 strcpy(client->cell, cellName);
519 struct ktc_principal *server)
521 struct ViceIoctl iob;
524 int newIter, ticketLen, temp;
530 memcpy(tp, &cellNum, sizeof(cellNum));
531 tp += sizeof(cellNum);
535 iob.in_size = tp - tbuffer;
537 iob.out_size = sizeof(tbuffer);
539 code = pioctl(0, VIOCGETTOK, &iob, 0);
544 else if (errno == ENODEV)
546 else if (errno == EINVAL)
548 else if (errno == EDOM)
551 return KTC_PIOCTLFAIL;
554 return KTC_PIOCTLFAIL;
560 memcpy(&newIter, cp, sizeof(newIter));
561 cp += sizeof(newIter);
564 memcpy(&ticketLen, cp, sizeof(ticketLen));
565 cp += sizeof(ticketLen);
567 /* skip over ticket */
570 /* clear token size */
571 memcpy(&temp, cp, sizeof(temp));
573 if (temp != sizeof(struct ClearToken))
576 /* skip over clear token */
577 cp += sizeof(struct ClearToken);
579 /* skip over primary flag */
582 /* cell name is here */
584 /* set return values */
585 strcpy(server->cell, cp);
586 server->instance[0] = '\0';
587 strcpy(server->name, "afs");
594 struct ktc_principal *server)
596 struct ViceIoctl iob;
601 if (strcmp(server->name, "afs")) {
602 return ForgetOneLocalToken(server);
608 strcpy(tp, server->cell);
609 tp += strlen(tp) + 1;
613 iob.in_size = tp - tbuffer;
615 iob.out_size = sizeof(tbuffer);
617 code = pioctl(0, VIOCDELTOK, &iob, 0);
622 else if (errno == EDOM)
624 else if (errno == ENODEV)
627 return KTC_PIOCTLFAIL;
630 return KTC_PIOCTLFAIL;
635 int ktc_ForgetAllTokens()
637 struct ViceIoctl iob;
641 (void) ForgetLocalTokens();
647 iob.out_size = sizeof(tbuffer);
649 code = pioctl(0, VIOCDELALLTOK, &iob, 0);
655 return KTC_PIOCTLFAIL;
658 return KTC_PIOCTLFAIL;
669 #define MAXLOCALTOKENS 4
673 struct ktc_principal server;
674 struct ktc_principal client;
675 struct ktc_token token;
676 } local_tokens[MAXLOCALTOKENS] = {0};
678 static int SetLocalToken(struct ktc_principal *aserver,
679 struct ktc_token *atoken,
680 struct ktc_principal *aclient,
687 for (i = 0; i < MAXLOCALTOKENS; i++)
688 if (local_tokens[i].valid) {
689 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0) &&
690 (strcmp(local_tokens[i].server.instance,
691 aserver->instance) == 0) &&
692 (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
693 found = i; /* replace existing entry */
696 } else if (found == -1)
697 found = i; /* remember empty slot but keep looking for a match */
702 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
703 memcpy(&local_tokens[found].server, aserver, sizeof(struct ktc_principal));
704 memcpy(&local_tokens[found].client, aclient, sizeof(struct ktc_principal));
705 local_tokens[found].valid = 1;
711 static int GetLocalToken(struct ktc_principal *aserver,
712 struct ktc_token *atoken,
714 struct ktc_principal *aclient)
719 for (i = 0; i < MAXLOCALTOKENS; i++)
720 if (local_tokens[i].valid &&
721 (strcmp(local_tokens[i].server.name, aserver->name) == 0) &&
722 (strcmp(local_tokens[i].server.instance,aserver->instance) == 0) &&
723 (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
724 memcpy(atoken, &local_tokens[i].token,
725 min(atokenLen, sizeof(struct ktc_token)));
726 memcpy(aclient, &local_tokens[i].client,
727 sizeof(struct ktc_principal));
736 static int ForgetLocalTokens()
741 for (i = 0; i < MAXLOCALTOKENS; i++) {
742 local_tokens[i].valid = 0;
743 memset(&local_tokens[i].token.sessionKey, 0,
744 sizeof(struct ktc_encryptionKey));
751 static int ForgetOneLocalToken(struct ktc_principal *aserver)
756 for (i = 0; i < MAXLOCALTOKENS; i++) {
757 if (local_tokens[i].valid &&
758 (strcmp(local_tokens[i].server.name, aserver->name) == 0) &&
759 (strcmp(local_tokens[i].server.instance,aserver->instance) == 0) &&
760 (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
761 local_tokens[i].valid = 0;
762 memset(&local_tokens[i].token.sessionKey, 0,
763 sizeof(struct ktc_encryptionKey));