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 <afsconfig.h>
13 #include <afs/param.h>
20 #include <afs/pthread_glock.h>
21 #include <sys/types.h>
27 #include <afs/smb_iocons.h>
28 #include <afs/pioctl_nt.h>
29 #include "afs/afsrpc.h"
32 #include <afs/afsutil.h>
34 /* TBUFFERSIZE must be at least 512 larger than KTCMAXTICKETSIZE */
35 #define TBUFFERSIZE 12512
37 /* Forward declarations for local token cache. */
38 static int SetLocalToken(struct ktc_principal *aserver,
39 struct ktc_token *atoken,
40 struct ktc_principal *aclient, afs_int32 flags);
41 static int GetLocalToken(struct ktc_principal *aserver,
42 struct ktc_token *atoken, int atokenLen,
43 struct ktc_principal *aclient);
44 static int ForgetLocalTokens();
45 static int ForgetOneLocalToken(struct ktc_principal *aserver);
48 static char AFSConfigKeyName[] =
49 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
51 static char AFSGlobalKTCMutexName[] = "Global\\AFS_KTC_Mutex";
52 static char AFSKTCMutexName[] = "AFS_KTC_Mutex";
55 * Support for RPC's to send and receive session keys
57 * Session keys must not be sent and received in the clear. We have no
58 * way to piggyback encryption on SMB, so we use a separate RPC, using
59 * packet privacy (when available). In SetToken, the RPC is done first;
60 * in GetToken, the pioctl is done first.
65 void __RPC_FAR *__RPC_USER
66 midl_user_allocate(size_t cBytes)
68 return ((void __RPC_FAR *)malloc(cBytes));
72 midl_user_free(void __RPC_FAR * p)
78 * Determine the server name to be used in the RPC binding. If it is
79 * the same as the client (i.e. standalone, non-gateway), NULL can be
80 * used, so it is not necessary to call gethostbyname().
83 getservername(char **snp, unsigned int snSize)
89 RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName, 0, KEY_QUERY_VALUE,
91 if (code != ERROR_SUCCESS)
93 code = RegQueryValueEx(parmKey, "Gateway", NULL, NULL, *snp, &snSize);
95 if (code == ERROR_SUCCESS)
98 /* No gateway name in registry; use ourself */
103 send_key(afs_uuid_t uuid, char sessionKey[8])
106 char *stringBinding = NULL;
107 ULONG authnLevel, authnSvc;
108 char serverName[256];
109 char *serverNamep = serverName;
111 BOOL encryptionOff = FALSE;
114 /* Encryption on by default */
115 if (GetEnvironmentVariable("AFS_RPC_ENCRYPT", encrypt, sizeof(encrypt)))
116 if (!strcmpi(encrypt, "OFF"))
117 encryptionOff = TRUE;
119 /* Protocol sequence is local by default */
120 if (!GetEnvironmentVariable("AFS_RPC_PROTSEQ", protseq, sizeof(protseq)))
121 strcpy(protseq, "ncalrpc");
124 getservername(&serverNamep, sizeof(serverName));
126 status = RpcStringBindingCompose("", /* obj uuid */
127 protseq, serverNamep, "", /* endpoint */
128 "", /* protocol options */
130 if (status != RPC_S_OK)
133 status = RpcBindingFromStringBinding(stringBinding, &hAfsHandle);
134 if (status != RPC_S_OK)
138 * On Windows 95/98, we must resolve the binding before calling
139 * SetAuthInfo. On Windows NT, we don't have to resolve yet,
140 * but it does no harm.
142 status = RpcEpResolveBinding(hAfsHandle, afsrpc_v1_0_c_ifspec);
143 if (status != RPC_S_OK)
147 authnLevel = RPC_C_AUTHN_LEVEL_NONE;
148 authnSvc = RPC_C_AUTHN_WINNT;
150 authnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
151 authnSvc = RPC_C_AUTHN_WINNT;
155 RpcBindingSetAuthInfo(hAfsHandle, NULL, authnLevel, authnSvc, NULL,
157 if (status != RPC_S_OK)
161 status = AFSRPC_SetToken(uuid, sessionKey);
164 status = RpcExceptionCode();
166 RpcEndExcept cleanup:if (stringBinding)
167 RpcStringFree(&stringBinding);
169 if (hAfsHandle != NULL)
170 RpcBindingFree(&hAfsHandle);
176 receive_key(afs_uuid_t uuid, char sessionKey[8])
179 char *stringBinding = NULL;
180 ULONG authnLevel, authnSvc;
181 char serverName[256];
182 char *serverNamep = serverName;
184 BOOL encryptionOff = FALSE;
187 /* Encryption on by default */
188 if (GetEnvironmentVariable("AFS_RPC_ENCRYPT", encrypt, sizeof(encrypt)))
189 if (!strcmpi(encrypt, "OFF"))
190 encryptionOff = TRUE;
192 /* Protocol sequence is local by default */
193 if (!GetEnvironmentVariable("AFS_RPC_PROTSEQ", protseq, sizeof(protseq)))
194 strcpy(protseq, "ncalrpc");
197 getservername(&serverNamep, sizeof(serverName));
199 status = RpcStringBindingCompose("", /* obj uuid */
200 protseq, serverNamep, "", /* endpoint */
201 "", /* protocol options */
203 if (status != RPC_S_OK)
206 status = RpcBindingFromStringBinding(stringBinding, &hAfsHandle);
207 if (status != RPC_S_OK)
211 * On Windows 95/98, we must resolve the binding before calling
212 * SetAuthInfo. On Windows NT, we don't have to resolve yet,
213 * but it does no harm.
215 status = RpcEpResolveBinding(hAfsHandle, afsrpc_v1_0_c_ifspec);
216 if (status != RPC_S_OK)
220 authnLevel = RPC_C_AUTHN_LEVEL_NONE;
221 authnSvc = RPC_C_AUTHN_WINNT;
223 authnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
224 authnSvc = RPC_C_AUTHN_WINNT;
228 RpcBindingSetAuthInfo(hAfsHandle, NULL, authnLevel, authnSvc, NULL,
230 if (status != RPC_S_OK)
234 status = AFSRPC_GetToken(uuid, sessionKey);
237 status = RpcExceptionCode();
239 RpcEndExcept cleanup:if (stringBinding)
240 RpcStringFree(&stringBinding);
242 if (hAfsHandle != NULL)
243 RpcBindingFree(&hAfsHandle);
249 ktc_SetToken(struct ktc_principal *server, struct ktc_token *token,
250 struct ktc_principal *client, int flags)
252 struct ViceIoctl iob;
253 char tbuffer[TBUFFERSIZE];
255 struct ClearToken ct;
260 HANDLE ktcMutex = NULL;
262 if (token->ticketLen < MINKTCTICKETLEN
263 || token->ticketLen > MAXKTCTICKETLEN)
266 if (strcmp(server->name, "afs")) {
267 return SetLocalToken(server, token, client, flags);
273 memcpy(tp, &token->ticketLen, sizeof(token->ticketLen));
274 tp += sizeof(token->ticketLen);
277 memcpy(tp, token->ticket, token->ticketLen);
278 tp += token->ticketLen;
281 ct.AuthHandle = token->kvno;
283 * Instead of sending the session key in the clear, we zero it,
284 * and send it later, via RPC, encrypted.
286 #ifndef AFS_WIN95_ENV
288 * memcpy(ct.HandShakeKey, &token->sessionKey, sizeof(token->sessionKey));
290 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
292 memcpy(ct.HandShakeKey, &token->sessionKey, sizeof(token->sessionKey));
294 ct.BeginTimestamp = token->startTime;
295 ct.EndTimestamp = token->endTime;
296 if (ct.BeginTimestamp == 0)
297 ct.BeginTimestamp = 1;
299 /* We don't know from Vice ID's yet */
300 ct.ViceId = 37; /* XXX */
301 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
302 ct.BeginTimestamp++; /* force lifetime to be even */
304 /* size of clear token */
305 temp = sizeof(struct ClearToken);
306 memcpy(tp, &temp, sizeof(temp));
309 /* clear token itself */
310 memcpy(tp, &ct, sizeof(ct));
313 /* flags; on NT there is no setpag flag, but there is an
314 * integrated logon flag */
315 temp = ((flags & AFS_SETTOK_LOGON) ? PIOCTL_LOGON : 0);
316 memcpy(tp, &temp, sizeof(temp));
320 temp = (int)strlen(server->cell);
321 if (temp >= MAXKTCREALMLEN)
323 strcpy(tp, server->cell);
327 temp = (int)strlen(client->name);
328 if (temp >= MAXKTCNAMELEN)
330 strcpy(tp, client->name);
333 /* we need the SMB user name to associate the tokens with in the
334 * integrated logon case. */
335 if (flags & AFS_SETTOK_LOGON) {
336 if (client->smbname == NULL)
339 temp = (int)strlen(client->smbname);
340 if (temp == 0 || temp >= MAXKTCNAMELEN)
342 strcpy(tp, client->smbname);
347 status = UuidCreate((UUID *) & uuid);
348 memcpy(tp, &uuid, sizeof(uuid));
352 #ifndef AFS_WIN95_ENV
353 ktcMutex = CreateMutex(NULL, TRUE, AFSGlobalKTCMutexName);
354 if (ktcMutex == NULL)
355 return KTC_PIOCTLFAIL;
356 if (GetLastError() == ERROR_ALREADY_EXISTS) {
357 if (WaitForSingleObject(ktcMutex, INFINITE) != WAIT_OBJECT_0) {
358 CloseHandle(ktcMutex);
359 return KTC_PIOCTLFAIL;
363 /* RPC to send session key */
364 status = send_key(uuid, token->sessionKey.data);
365 if (status != RPC_S_OK) {
367 strcpy(rpcErr, "RPC failure in AFS gateway");
369 DceErrorInqText(status, rpcErr);
370 if (status == RPC_S_SERVER_UNAVAILABLE ||
371 status == EPT_S_NOT_REGISTERED) {
372 ReleaseMutex(ktcMutex);
373 CloseHandle(ktcMutex);
376 ReleaseMutex(ktcMutex);
377 CloseHandle(ktcMutex);
381 #endif /* AFS_WIN95_ENV */
383 /* set up for pioctl */
385 iob.in_size = (long)(tp - tbuffer);
387 iob.out_size = sizeof(tbuffer);
389 code = pioctl(0, VIOCSETTOK, &iob, 0);
391 #ifndef AFS_WIN95_ENV
392 ReleaseMutex(ktcMutex);
393 CloseHandle(ktcMutex);
394 #endif /* AFS_WIN95_ENV */
400 else if (errno == ENODEV)
402 else if (errno == EINVAL)
405 return KTC_PIOCTLFAIL;
407 return KTC_PIOCTLFAIL;
414 ktc_GetToken(struct ktc_principal *server, struct ktc_token *token,
415 int tokenLen, struct ktc_principal *client)
417 struct ViceIoctl iob;
418 char tbuffer[TBUFFERSIZE];
422 struct ClearToken ct;
429 HANDLE ktcMutex = NULL;
433 /* check to see if the user is requesting tokens for a principal
434 * other than afs. If so, check the local token cache.
436 if (strcmp(server->name, "afs")) {
437 return GetLocalToken(server, token, tokenLen, client);
441 strcpy(tp, server->cell);
442 tp += strlen(server->cell) + 1;
445 status = UuidCreate((UUID *) & uuid);
446 memcpy(tp, &uuid, sizeof(uuid));
450 iob.in_size = (long)(tp - tbuffer);
452 iob.out_size = sizeof(tbuffer);
454 #ifndef AFS_WIN95_ENV
455 ktcMutex = CreateMutex(NULL, TRUE, AFSGlobalKTCMutexName);
456 if (ktcMutex == NULL)
457 return KTC_PIOCTLFAIL;
458 if (GetLastError() == ERROR_ALREADY_EXISTS) {
459 if (WaitForSingleObject(ktcMutex, INFINITE) != WAIT_OBJECT_0) {
460 CloseHandle(ktcMutex);
461 return KTC_PIOCTLFAIL;
464 #endif /* AFS_WIN95_ENV */
466 code = pioctl(0, VIOCNEWGETTOK, &iob, 0);
468 #ifndef AFS_WIN95_ENV
469 ReleaseMutex(ktcMutex);
470 CloseHandle(ktcMutex);
471 #endif /* AFS_WIN95_ENV */
475 else if (errno == ENODEV)
477 else if (errno == EINVAL)
479 else if (errno == EDOM)
482 return KTC_PIOCTLFAIL;
484 return KTC_PIOCTLFAIL;
486 #ifndef AFS_WIN95_ENV
487 /* get rid of RPC for win95 build */
488 /* RPC to receive session key */
489 status = receive_key(uuid, token->sessionKey.data);
491 ReleaseMutex(ktcMutex);
492 CloseHandle(ktcMutex);
494 if (status != RPC_S_OK) {
496 strcpy(rpcErr, "RPC failure in AFS gateway");
498 DceErrorInqText(status, rpcErr);
499 if (status == RPC_S_SERVER_UNAVAILABLE
500 || status == EPT_S_NOT_REGISTERED)
505 #endif /* AFS_WIN95_ENV */
510 memcpy(&ticketLen, cp, sizeof(ticketLen));
511 cp += sizeof(ticketLen);
513 /* remember where ticket is and skip over it */
517 /* size of clear token */
518 memcpy(&temp, cp, sizeof(temp));
520 if (temp != sizeof(ct))
524 memcpy(&ct, cp, temp);
527 /* skip over primary flag */
530 /* remember cell name and skip over it */
532 cellNameSize = (int)strlen(cp);
533 cp += cellNameSize + 1;
535 /* user name is here */
537 /* check that ticket will fit
538 * this compares the size of the ktc_token allocated by the app
539 * which might be smaller than the current definition of MAXKTCTICKETLEN
541 maxLen = tokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
542 if (maxLen < ticketLen)
545 /* set return values */
546 memcpy(token->ticket, ticketP, ticketLen);
547 token->startTime = ct.BeginTimestamp;
548 token->endTime = ct.EndTimestamp;
549 if (ct.AuthHandle == -1)
551 token->kvno = ct.AuthHandle;
552 #ifndef AFS_WIN95_ENV
554 * Session key has already been set via RPC
557 memcpy(&token->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
558 #endif /* AFS_WIN95_ENV */
559 token->ticketLen = ticketLen;
561 strcpy(client->name, cp);
562 client->instance[0] = '\0';
563 strcpy(client->cell, cellName);
570 ktc_ListTokens(int cellNum, int *cellNumP, struct ktc_principal *server)
572 struct ViceIoctl iob;
573 char tbuffer[TBUFFERSIZE];
575 int newIter, ticketLen, temp;
577 HANDLE ktcMutex = NULL;
579 #ifndef AFS_WIN95_ENV
580 ktcMutex = CreateMutex(NULL, TRUE, AFSGlobalKTCMutexName);
581 if (ktcMutex == NULL)
582 return KTC_PIOCTLFAIL;
583 if (GetLastError() == ERROR_ALREADY_EXISTS) {
584 if (WaitForSingleObject(ktcMutex, INFINITE) != WAIT_OBJECT_0) {
585 CloseHandle(ktcMutex);
586 return KTC_PIOCTLFAIL;
589 #endif /* AFS_WIN95_ENV */
594 memcpy(tp, &cellNum, sizeof(cellNum));
595 tp += sizeof(cellNum);
599 iob.in_size = (long)(tp - tbuffer);
601 iob.out_size = sizeof(tbuffer);
603 code = pioctl(0, VIOCGETTOK, &iob, 0);
605 #ifndef AFS_WIN95_ENV
606 ReleaseMutex(ktcMutex);
607 CloseHandle(ktcMutex);
608 #endif /* AFS_WIN95_ENV */
614 else if (errno == ENODEV)
616 else if (errno == EINVAL)
618 else if (errno == EDOM)
621 return KTC_PIOCTLFAIL;
623 return KTC_PIOCTLFAIL;
629 memcpy(&newIter, cp, sizeof(newIter));
630 cp += sizeof(newIter);
633 memcpy(&ticketLen, cp, sizeof(ticketLen));
634 cp += sizeof(ticketLen);
636 /* skip over ticket */
639 /* clear token size */
640 memcpy(&temp, cp, sizeof(temp));
642 if (temp != sizeof(struct ClearToken))
645 /* skip over clear token */
646 cp += sizeof(struct ClearToken);
648 /* skip over primary flag */
651 /* cell name is here */
653 /* set return values */
654 strcpy(server->cell, cp);
655 server->instance[0] = '\0';
656 strcpy(server->name, "afs");
663 ktc_ForgetToken(struct ktc_principal *server)
665 struct ViceIoctl iob;
666 char tbuffer[TBUFFERSIZE];
669 HANDLE ktcMutex = NULL;
671 if (strcmp(server->name, "afs")) {
672 return ForgetOneLocalToken(server);
674 #ifndef AFS_WIN95_ENV
675 ktcMutex = CreateMutex(NULL, TRUE, AFSGlobalKTCMutexName);
676 if (ktcMutex == NULL)
677 return KTC_PIOCTLFAIL;
678 if (GetLastError() == ERROR_ALREADY_EXISTS) {
679 if (WaitForSingleObject(ktcMutex, INFINITE) != WAIT_OBJECT_0) {
680 CloseHandle(ktcMutex);
681 return KTC_PIOCTLFAIL;
684 #endif /* AFS_WIN95_ENV */
689 strcpy(tp, server->cell);
690 tp += strlen(tp) + 1;
694 iob.in_size = (long)(tp - tbuffer);
696 iob.out_size = sizeof(tbuffer);
698 code = pioctl(0, VIOCDELTOK, &iob, 0);
699 #ifndef AFS_WIN95_ENV
700 ReleaseMutex(ktcMutex);
701 CloseHandle(ktcMutex);
702 #endif /* AFS_WIN95_ENV */
707 else if (errno == EDOM)
709 else if (errno == ENODEV)
712 return KTC_PIOCTLFAIL;
714 return KTC_PIOCTLFAIL;
720 ktc_ForgetAllTokens()
722 struct ViceIoctl iob;
723 char tbuffer[TBUFFERSIZE];
725 HANDLE ktcMutex = NULL;
727 (void)ForgetLocalTokens();
729 #ifndef AFS_WIN95_ENV
730 ktcMutex = CreateMutex(NULL, TRUE, AFSGlobalKTCMutexName);
731 if (ktcMutex == NULL)
732 return KTC_PIOCTLFAIL;
733 if (GetLastError() == ERROR_ALREADY_EXISTS) {
734 if (WaitForSingleObject(ktcMutex, INFINITE) != WAIT_OBJECT_0) {
735 CloseHandle(ktcMutex);
736 return KTC_PIOCTLFAIL;
739 #endif /* AFS_WIN95_ENV */
745 iob.out_size = sizeof(tbuffer);
747 code = pioctl(0, VIOCDELALLTOK, &iob, 0);
748 #ifndef AFS_WIN95_ENV
749 ReleaseMutex(ktcMutex);
750 CloseHandle(ktcMutex);
751 #endif /* AFS_WIN95_ENV */
757 return KTC_PIOCTLFAIL;
759 return KTC_PIOCTLFAIL;
771 #define MAXLOCALTOKENS 4
775 struct ktc_principal server;
776 struct ktc_principal client;
777 struct ktc_token token;
778 } local_tokens[MAXLOCALTOKENS] = {
782 SetLocalToken(struct ktc_principal *aserver, struct ktc_token *atoken,
783 struct ktc_principal *aclient, afs_int32 flags)
789 for (i = 0; i < MAXLOCALTOKENS; i++)
790 if (local_tokens[i].valid) {
791 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
792 && (strcmp(local_tokens[i].server.instance, aserver->instance)
794 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
795 found = i; /* replace existing entry */
798 } else if (found == -1)
799 found = i; /* remember empty slot but keep looking for a match */
804 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
805 memcpy(&local_tokens[found].server, aserver,
806 sizeof(struct ktc_principal));
807 memcpy(&local_tokens[found].client, aclient,
808 sizeof(struct ktc_principal));
809 local_tokens[found].valid = 1;
816 GetLocalToken(struct ktc_principal *aserver, struct ktc_token *atoken,
817 int atokenLen, struct ktc_principal *aclient)
822 for (i = 0; i < MAXLOCALTOKENS; i++)
823 if (local_tokens[i].valid
824 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
825 && (strcmp(local_tokens[i].server.instance, aserver->instance) ==
827 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
828 memcpy(atoken, &local_tokens[i].token,
829 min(atokenLen, sizeof(struct ktc_token)));
830 memcpy(aclient, &local_tokens[i].client,
831 sizeof(struct ktc_principal));
846 for (i = 0; i < MAXLOCALTOKENS; i++) {
847 local_tokens[i].valid = 0;
848 memset(&local_tokens[i].token.sessionKey, 0,
849 sizeof(struct ktc_encryptionKey));
857 ForgetOneLocalToken(struct ktc_principal *aserver)
862 for (i = 0; i < MAXLOCALTOKENS; i++) {
863 if (local_tokens[i].valid
864 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
865 && (strcmp(local_tokens[i].server.instance, aserver->instance) ==
867 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
868 local_tokens[i].valid = 0;
869 memset(&local_tokens[i].token.sessionKey, 0,
870 sizeof(struct ktc_encryptionKey));