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>
22 #include <sys/types.h>
29 #include <sys/ioctl.h>
34 #include <afs/afsutil.h>
37 extern struct cmd_syndesc *cmd_CreateSyntax();
42 29 Oct 1992 Patch GetTokens to print something reasonable when there are no tokens.
46 /* this is a structure used to communicate with the afs cache mgr, but is
47 * otherwise irrelevant, or worse.
53 afs_int32 BeginTimestamp;
54 afs_int32 EndTimestamp;
59 SetSysname(ahost, auid, sysname)
66 char space[1200], *tp;
67 struct ViceIoctl blob;
70 /* otherwise we've got the token, now prepare to build the pioctl args */
73 pheader[2] = 0; /* group low */
74 pheader[3] = 0; /* group high */
75 pheader[4] = 38 /*VIOC_AFS_SYSNAME */ ; /* sysname pioctl index */
76 pheader[5] = 1; /* NFS protocol exporter # */
79 memcpy(space, pheader, sizeof(pheader));
80 tp = space + sizeof(pheader);
82 /* finally setup the pioctl call's parameters */
83 blob.in_size = sizeof(pheader);
87 memcpy(tp, &setp, sizeof(afs_int32));
88 tp += sizeof(afs_int32);
90 blob.in_size += sizeof(afs_int32) + strlen(sysname) + 1;
91 tp += strlen(sysname);
93 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
102 GetTokens(ahost, auid)
106 struct ViceIoctl iob;
107 afs_int32 pheader[6];
109 register afs_int32 code;
111 char *stp; /* secret token ptr */
112 struct ClearToken ct;
114 afs_int32 temp, gotit = 0;
115 int maxLen; /* biggest ticket we can copy */
116 int tktLen; /* server ticket length */
117 time_t tokenExpireTime;
119 struct ktc_token token;
120 struct ktc_principal clientName;
124 current_time = time(0);
126 /* otherwise we've got the token, now prepare to build the pioctl args */
129 pheader[2] = 0; /* group low */
130 pheader[3] = 0; /* group high */
131 pheader[4] = 8; /* gettoken pioctl index */
132 pheader[5] = 1; /* NFS protocol exporter # */
134 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
135 code = ktc_ListTokens(index, &newIndex, &clientName);
137 if (code == KTC_NOENT) {
140 printf("knfs: there are no tokens here.\n");
143 break; /* done, but failed */
145 if (strcmp(clientName.name, "afs") != 0)
146 continue; /* wrong ticket service */
149 memcpy(tbuffer, pheader, sizeof(pheader));
150 tp = tbuffer + sizeof(pheader);
151 memcpy(tp, &index, sizeof(afs_int32));
152 tp += sizeof(afs_int32);
154 iob.in_size = sizeof(afs_int32) + sizeof(pheader);
156 iob.out_size = sizeof(tbuffer);
157 code = pioctl(NULL, _VICEIOCTL(99), &iob, 0);
158 if (code < 0 && errno == EDOM)
160 else if (code == 0) {
161 /* check to see if this is the right cell/realm */
163 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
164 tktLen = temp; /* remember size of ticket */
165 tp += sizeof(afs_int32);
166 stp = tp; /* remember where ticket is, for later */
167 tp += temp; /* skip ticket for now */
168 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
169 if (temp != sizeof(struct ClearToken))
171 tp += sizeof(afs_int32); /* skip length */
172 memcpy(&ct, tp, temp); /* copy token for later use */
173 tp += temp; /* skip clear token itself */
174 tp += sizeof(afs_int32); /* skip primary flag */
175 /* tp now points to the cell name */
176 if (strcmp(tp, clientName.cell) == 0) {
177 /* closing in now, we've got the right cell */
180 sizeof(token) - sizeof(struct ktc_token) +
184 memcpy(token.ticket, stp, tktLen);
185 token.startTime = ct.BeginTimestamp;
186 token.endTime = ct.EndTimestamp;
187 if (ct.AuthHandle == -1)
189 token.kvno = ct.AuthHandle;
190 memcpy(&token.sessionKey, ct.HandShakeKey,
191 sizeof(struct ktc_encryptionKey));
192 token.ticketLen = tktLen;
193 if ((token.kvno == 999) || /* old style bcrypt ticket */
194 (ct.BeginTimestamp && /* new w/ prserver lookup */
195 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
196 sprintf(clientName.name, "AFS ID %d", ct.ViceId);
197 clientName.instance[0] = 0;
199 sprintf(clientName.name, "Unix UID %d", ct.ViceId);
200 clientName.instance[0] = 0;
202 strcpy(clientName.cell, tp);
204 tokenExpireTime = token.endTime;
205 strcpy(UserName, clientName.name);
206 if (clientName.instance[0] != 0) {
207 strcat(UserName, ".");
208 strcat(UserName, clientName.instance);
210 if (UserName[0] == 0)
212 else if (strncmp(UserName, "AFS ID", 6) == 0) {
213 printf("User's (%s) tokens", UserName);
214 } else if (strncmp(UserName, "Unix UID", 8) == 0) {
217 printf("User %s's tokens", UserName);
218 printf(" for %s%s%s@%s ", clientName.name,
219 clientName.instance[0] ? "." : "", clientName.instance,
221 if (tokenExpireTime <= current_time)
222 printf("[>> Expired <<]\n");
224 expireString = ctime(&tokenExpireTime);
225 expireString += 4; /*Move past the day of week */
226 expireString[12] = '\0';
227 printf("[Expires %s]\n", expireString);
238 NFSUnlog(ahost, auid)
243 afs_int32 pheader[6];
245 struct ViceIoctl blob;
247 /* otherwise we've got the token, now prepare to build the pioctl args */
250 pheader[2] = 0; /* group low */
251 pheader[3] = 0; /* group high */
252 pheader[4] = 9; /* unlog pioctl index */
253 pheader[5] = 1; /* NFS protocol exporter # */
256 memcpy(space, pheader, sizeof(pheader));
258 /* finally setup the pioctl call's parameters */
259 blob.in_size = sizeof(pheader);
263 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
270 /* Copy the AFS service token into the kernel for a particular host and user */
272 NFSCopyToken(ahost, auid)
276 struct ktc_principal client, server;
277 struct ktc_token theTicket;
279 afs_int32 pheader[6];
281 struct ClearToken ct;
282 afs_int32 index, newIndex;
283 afs_int32 temp; /* for bcopy */
285 struct ViceIoctl blob;
287 for (index = 0;; index = newIndex) {
288 code = ktc_ListTokens(index, &newIndex, &server);
290 if (code == KTC_NOENT) {
294 break; /* done, but failed */
296 if (strcmp(server.name, "afs") != 0)
297 continue; /* wrong ticket service */
298 code = ktc_GetToken(&server, &theTicket, sizeof(theTicket), &client);
302 /* otherwise we've got the token, now prepare to build the pioctl args */
305 pheader[2] = 0; /* group low */
306 pheader[3] = 0; /* group high */
307 pheader[4] = 3; /* set token pioctl index */
308 pheader[5] = 1; /* NFS protocol exporter # */
310 /* copy in the header */
311 memcpy(space, pheader, sizeof(pheader));
312 tp = space + sizeof(pheader);
313 /* copy in the size of the encrypted part */
314 memcpy(tp, &theTicket.ticketLen, sizeof(afs_int32));
315 tp += sizeof(afs_int32);
316 /* copy in the ticket itself */
317 memcpy(tp, theTicket.ticket, theTicket.ticketLen);
318 tp += theTicket.ticketLen;
319 /* copy in "clear token"'s size */
320 temp = sizeof(struct ClearToken);
321 memcpy(tp, &temp, sizeof(afs_int32));
322 tp += sizeof(afs_int32);
323 /* create the clear token and copy *it* in */
324 ct.AuthHandle = theTicket.kvno; /* where we hide the key version # */
325 memcpy(ct.HandShakeKey, &theTicket.sessionKey,
326 sizeof(ct.HandShakeKey));
329 ct.BeginTimestamp = theTicket.startTime;
330 ct.EndTimestamp = theTicket.endTime;
331 memcpy(tp, &ct, sizeof(ct));
333 /* copy in obsolete primary flag */
335 memcpy(tp, &temp, sizeof(afs_int32));
336 tp += sizeof(afs_int32);
337 /* copy in cell name, null terminated */
338 strcpy(tp, server.cell);
339 tp += strlen(server.cell) + 1;
341 /* finally setup the pioctl call's parameters */
342 blob.in_size = tp - space;
346 code = pioctl(NULL, _VICEIOCTL(99), &blob, 0);
357 register struct cmd_syndesc *as;
360 register struct hostent *the;
361 char *tp, *sysname = 0;
363 register afs_int32 code;
365 the = (struct hostent *)
366 hostutil_GetHostByName(tp = as->parms[0].items->data);
368 printf("knfs: unknown host '%s'.\n", tp);
371 memcpy(&addr, the->h_addr, sizeof(afs_int32));
373 if (as->parms[1].items) {
374 code = util_GetInt32(tp = as->parms[1].items->data, &uid);
376 printf("knfs: can't parse '%s' as a number (UID)\n", tp);
380 uid = -1; /* means wildcard: match any user on this host */
383 * If not "-id" is passed then we use the getuid() id, unless it's root
384 * that is doing it in which case we only authenticate as "system:anyuser"
385 * as it's appropriate for root. (The cm handles conversions from 0 to
392 if (as->parms[2].items) {
393 sysname = as->parms[2].items->data;
396 if (as->parms[4].items) {
397 /* tokens specified */
398 code = GetTokens(addr, uid);
402 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
404 printf("knfs: failed to get tokens for uid %d (code %d)\n",
410 /* finally, parsing is done, make the call */
411 if (as->parms[3].items) {
412 /* unlog specified */
413 code = NFSUnlog(addr, uid);
417 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
419 printf("knfs: failed to unlog (code %d)\n", code);
422 code = NFSCopyToken(addr, uid);
426 ("knfs: Translator in 'passwd sync' mode; remote uid must be the same as local uid\n");
428 printf("knfs: failed to copy tokens (code %d)\n", code);
431 code = SetSysname(addr, uid, sysname);
433 printf("knfs: failed to set client's @sys to %s (code %d)\n",
441 #include "AFS_component_version_number.c"
447 register struct cmd_syndesc *ts;
448 register afs_int32 code;
452 * The following signal action for AIX is necessary so that in case of a
453 * crash (i.e. core is generated) we can include the user's data section
454 * in the core dump. Unfortunately, by default, only a partial core is
455 * generated which, in many cases, isn't too useful.
457 struct sigaction nsa;
459 sigemptyset(&nsa.sa_mask);
460 nsa.sa_handler = SIG_DFL;
461 nsa.sa_flags = SA_FULLDUMP;
462 sigaction(SIGABRT, &nsa, NULL);
463 sigaction(SIGSEGV, &nsa, NULL);
466 ts = cmd_CreateSyntax(NULL, cmdproc, 0, "copy tickets for NFS");
467 cmd_AddParm(ts, "-host", CMD_SINGLE, CMD_REQUIRED, "host name");
468 cmd_AddParm(ts, "-id", CMD_SINGLE, CMD_OPTIONAL, "user ID (decimal)");
469 cmd_AddParm(ts, "-sysname", CMD_SINGLE, CMD_OPTIONAL,
470 "host's '@sys' value");
471 cmd_AddParm(ts, "-unlog", CMD_FLAG, CMD_OPTIONAL, "unlog remote user");
472 cmd_AddParm(ts, "-tokens", CMD_FLAG, CMD_OPTIONAL,
473 "display all tokens for remote [host,id]");
475 code = cmd_Dispatch(argc, argv);