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
14 #include <afsconfig.h>
15 #include <afs/param.h>
20 #include <sys/types.h>
27 #include <sys/ioctl.h>
32 #include <afs/afsutil.h>
37 29 Oct 1992 Patch GetTokens to print something reasonable when there are no tokens.
41 /* this is a structure used to communicate with the afs cache mgr, but is
42 * otherwise irrelevant, or worse.
48 afs_int32 BeginTimestamp;
49 afs_int32 EndTimestamp;
54 SetSysname(ahost, auid, sysname)
61 char space[1200], *tp;
62 struct ViceIoctl blob;
65 /* otherwise we've got the token, now prepare to build the pioctl args */
68 pheader[2] = 0; /* group low */
69 pheader[3] = 0; /* group high */
70 pheader[4] = 38 /*VIOC_AFS_SYSNAME */ ; /* sysname pioctl index */
71 pheader[5] = 1; /* NFS protocol exporter # */
74 memcpy(space, pheader, sizeof(pheader));
75 tp = space + sizeof(pheader);
77 /* finally setup the pioctl call's parameters */
78 blob.in_size = sizeof(pheader);
82 memcpy(tp, &setp, sizeof(afs_int32));
83 tp += sizeof(afs_int32);
85 blob.in_size += sizeof(afs_int32) + strlen(sysname) + 1;
86 tp += strlen(sysname);
88 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
97 GetTokens(ahost, auid)
101 struct ViceIoctl iob;
102 afs_int32 pheader[6];
106 char *stp; /* secret token ptr */
107 struct ClearToken ct;
109 afs_int32 temp, gotit = 0;
110 int maxLen; /* biggest ticket we can copy */
111 int tktLen; /* server ticket length */
112 time_t tokenExpireTime;
114 struct ktc_token token;
115 struct ktc_principal clientName;
119 current_time = time(0);
121 /* otherwise we've got the token, now prepare to build the pioctl args */
124 pheader[2] = 0; /* group low */
125 pheader[3] = 0; /* group high */
126 pheader[4] = 8; /* gettoken pioctl index */
127 pheader[5] = 1; /* NFS protocol exporter # */
129 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
130 code = ktc_ListTokens(index, &newIndex, &clientName);
132 if (code == KTC_NOENT) {
135 printf("knfs: there are no tokens here.\n");
138 break; /* done, but failed */
140 if (strcmp(clientName.name, "afs") != 0)
141 continue; /* wrong ticket service */
144 memcpy(tbuffer, pheader, sizeof(pheader));
145 tp = tbuffer + sizeof(pheader);
146 memcpy(tp, &index, sizeof(afs_int32));
147 tp += sizeof(afs_int32);
149 iob.in_size = sizeof(afs_int32) + sizeof(pheader);
151 iob.out_size = sizeof(tbuffer);
152 code = pioctl(NULL, _VICEIOCTL(99), &iob, 0);
153 if (code < 0 && errno == EDOM)
155 else if (code == 0) {
156 /* check to see if this is the right cell/realm */
158 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
159 tktLen = temp; /* remember size of ticket */
160 tp += sizeof(afs_int32);
161 stp = tp; /* remember where ticket is, for later */
162 tp += temp; /* skip ticket for now */
163 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
164 if (temp != sizeof(struct ClearToken))
166 tp += sizeof(afs_int32); /* skip length */
167 memcpy(&ct, tp, temp); /* copy token for later use */
168 tp += temp; /* skip clear token itself */
169 tp += sizeof(afs_int32); /* skip primary flag */
170 /* tp now points to the cell name */
171 if (strcmp(tp, clientName.cell) == 0) {
172 /* closing in now, we've got the right cell */
175 sizeof(token) - sizeof(struct ktc_token) +
179 memcpy(token.ticket, stp, tktLen);
180 token.startTime = ct.BeginTimestamp;
181 token.endTime = ct.EndTimestamp;
182 if (ct.AuthHandle == -1)
184 token.kvno = ct.AuthHandle;
185 memcpy(&token.sessionKey, ct.HandShakeKey,
186 sizeof(struct ktc_encryptionKey));
187 token.ticketLen = tktLen;
188 if ((token.kvno == 999) || /* old style bcrypt ticket */
189 (ct.BeginTimestamp && /* new w/ prserver lookup */
190 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
191 sprintf(clientName.name, "AFS ID %d", ct.ViceId);
192 clientName.instance[0] = 0;
194 sprintf(clientName.name, "Unix UID %d", ct.ViceId);
195 clientName.instance[0] = 0;
197 strcpy(clientName.cell, tp);
199 tokenExpireTime = token.endTime;
200 strcpy(UserName, clientName.name);
201 if (clientName.instance[0] != 0) {
202 strcat(UserName, ".");
203 strcat(UserName, clientName.instance);
205 if (UserName[0] == 0)
207 else if (strncmp(UserName, "AFS ID", 6) == 0) {
208 printf("User's (%s) tokens", UserName);
209 } else if (strncmp(UserName, "Unix UID", 8) == 0) {
212 printf("User %s's tokens", UserName);
213 printf(" for %s%s%s@%s ", clientName.name,
214 clientName.instance[0] ? "." : "", clientName.instance,
216 if (tokenExpireTime <= current_time)
217 printf("[>> Expired <<]\n");
219 expireString = ctime(&tokenExpireTime);
220 expireString += 4; /*Move past the day of week */
221 expireString[12] = '\0';
222 printf("[Expires %s]\n", expireString);
233 NFSUnlog(ahost, auid)
238 afs_int32 pheader[6];
240 struct ViceIoctl blob;
242 /* otherwise we've got the token, now prepare to build the pioctl args */
245 pheader[2] = 0; /* group low */
246 pheader[3] = 0; /* group high */
247 pheader[4] = 9; /* unlog pioctl index */
248 pheader[5] = 1; /* NFS protocol exporter # */
251 memcpy(space, pheader, sizeof(pheader));
253 /* finally setup the pioctl call's parameters */
254 blob.in_size = sizeof(pheader);
258 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
265 /* Copy the AFS service token into the kernel for a particular host and user */
267 NFSCopyToken(ahost, auid)
271 struct ktc_principal client, server;
272 struct ktc_token theTicket;
274 afs_int32 pheader[6];
276 struct ClearToken ct;
277 afs_int32 index, newIndex;
278 afs_int32 temp; /* for bcopy */
280 struct ViceIoctl blob;
282 for (index = 0;; index = newIndex) {
283 code = ktc_ListTokens(index, &newIndex, &server);
285 if (code == KTC_NOENT) {
289 break; /* done, but failed */
291 if (strcmp(server.name, "afs") != 0)
292 continue; /* wrong ticket service */
293 code = ktc_GetToken(&server, &theTicket, sizeof(theTicket), &client);
297 /* otherwise we've got the token, now prepare to build the pioctl args */
300 pheader[2] = 0; /* group low */
301 pheader[3] = 0; /* group high */
302 pheader[4] = 3; /* set token pioctl index */
303 pheader[5] = 1; /* NFS protocol exporter # */
305 /* copy in the header */
306 memcpy(space, pheader, sizeof(pheader));
307 tp = space + sizeof(pheader);
308 /* copy in the size of the encrypted part */
309 memcpy(tp, &theTicket.ticketLen, sizeof(afs_int32));
310 tp += sizeof(afs_int32);
311 /* copy in the ticket itself */
312 memcpy(tp, theTicket.ticket, theTicket.ticketLen);
313 tp += theTicket.ticketLen;
314 /* copy in "clear token"'s size */
315 temp = sizeof(struct ClearToken);
316 memcpy(tp, &temp, sizeof(afs_int32));
317 tp += sizeof(afs_int32);
318 /* create the clear token and copy *it* in */
319 ct.AuthHandle = theTicket.kvno; /* where we hide the key version # */
320 memcpy(ct.HandShakeKey, &theTicket.sessionKey,
321 sizeof(ct.HandShakeKey));
324 ct.BeginTimestamp = theTicket.startTime;
325 ct.EndTimestamp = theTicket.endTime;
326 memcpy(tp, &ct, sizeof(ct));
328 /* copy in obsolete primary flag */
330 memcpy(tp, &temp, sizeof(afs_int32));
331 tp += sizeof(afs_int32);
332 /* copy in cell name, null terminated */
333 strcpy(tp, server.cell);
334 tp += strlen(server.cell) + 1;
336 /* finally setup the pioctl call's parameters */
337 blob.in_size = tp - space;
341 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
351 cmdproc(register struct cmd_syndesc *as, void *arock)
353 register struct hostent *the;
354 char *tp, *sysname = 0;
356 register afs_int32 code;
358 the = (struct hostent *)
359 hostutil_GetHostByName(tp = as->parms[0].items->data);
361 printf("knfs: unknown host '%s'.\n", tp);
364 memcpy(&addr, the->h_addr, sizeof(afs_int32));
366 if (as->parms[1].items) {
367 code = util_GetInt32(tp = as->parms[1].items->data, &uid);
369 printf("knfs: can't parse '%s' as a number (UID)\n", tp);
373 uid = -1; /* means wildcard: match any user on this host */
376 * If not "-id" is passed then we use the getuid() id, unless it's root
377 * that is doing it in which case we only authenticate as "system:anyuser"
378 * as it's appropriate for root. (The cm handles conversions from 0 to
385 if (as->parms[2].items) {
386 sysname = as->parms[2].items->data;
389 if (as->parms[4].items) {
390 /* tokens specified */
391 code = GetTokens(addr, uid);
395 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
397 printf("knfs: failed to get tokens for uid %d (code %d)\n",
403 /* finally, parsing is done, make the call */
404 if (as->parms[3].items) {
405 /* unlog specified */
406 code = NFSUnlog(addr, uid);
410 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
412 printf("knfs: failed to unlog (code %d)\n", code);
415 code = NFSCopyToken(addr, uid);
419 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
421 printf("knfs: failed to copy tokens (code %d)\n", code);
424 code = SetSysname(addr, uid, sysname);
426 printf("knfs: failed to set client's @sys to %s (code %d)\n",
434 #include "AFS_component_version_number.c"
440 register struct cmd_syndesc *ts;
441 register afs_int32 code;
445 * The following signal action for AIX is necessary so that in case of a
446 * crash (i.e. core is generated) we can include the user's data section
447 * in the core dump. Unfortunately, by default, only a partial core is
448 * generated which, in many cases, isn't too useful.
450 struct sigaction nsa;
452 sigemptyset(&nsa.sa_mask);
453 nsa.sa_handler = SIG_DFL;
454 nsa.sa_flags = SA_FULLDUMP;
455 sigaction(SIGABRT, &nsa, NULL);
456 sigaction(SIGSEGV, &nsa, NULL);
459 ts = cmd_CreateSyntax(NULL, cmdproc, NULL, "copy tickets for NFS");
460 cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_REQUIRED, "host name");
461 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "user ID (decimal)");
462 cmd_AddParm(ts, "-sysname", CMD_SINGLE, CMD_OPTIONAL,
463 "host's '@sys' value");
464 cmd_AddParm(ts, "-unlog", CMD_FLAG, CMD_OPTIONAL, "unlog remote user");
465 cmd_AddParm(ts, "-tokens", CMD_FLAG, CMD_OPTIONAL,
466 "display all tokens for remote [host,id]");
468 code = cmd_Dispatch(argc, argv);