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 #include <afs/param.h>
25 #include <WINNT\afsreg.h>
32 #define MAXINSIZE 1300 /* pioctl complains if data is larger than this */
34 static char space[MAXSIZE];
35 static char tspace[1024];
38 static struct ubik_client *uclient;
39 #endif /* not WIN32 */
42 static char pn[] = "symlink";
43 static int rxInitDone = 0;
54 if (t >= 'A' && t <= 'Z') t += 0x20;
55 if (u >= 'A' && u <= 'Z') u += 0x20;
61 /* this function returns TRUE (1) if the file is in AFS, otherwise false (0) */
62 static int InAFS(register char *apath)
64 struct ViceIoctl blob;
65 register afs_int32 code;
68 blob.out_size = MAXSIZE;
71 code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
73 if ((errno == EINVAL) || (errno == ENOENT))
80 IsFreelanceRoot(char *apath)
82 struct ViceIoctl blob;
86 blob.out_size = MAXSIZE;
89 code = pioctl(apath, VIOC_FILE_CELL_NAME, &blob, 1);
91 return !strcmp("Freelance.Local.Root",space);
92 return 1; /* assume it is because it is more restrictive that way */
95 static const char * NetbiosName(void)
97 static char buffer[1024] = "AFS";
103 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
104 0, KEY_QUERY_VALUE, &parmKey);
105 if (code == ERROR_SUCCESS) {
106 dummyLen = sizeof(buffer);
107 code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
109 RegCloseKey (parmKey);
111 strcpy(buffer, "AFS");
116 #define AFSCLIENT_ADMIN_GROUPNAME "AFS Client Admins"
118 static BOOL IsAdmin (void)
120 static BOOL fAdmin = FALSE;
121 static BOOL fTested = FALSE;
125 /* Obtain the SID for the AFS client admin group. If the group does
126 * not exist, then assume we have AFS client admin privileges.
128 PSID psidAdmin = NULL;
129 DWORD dwSize, dwSize2;
130 char pszAdminGroup[ MAX_COMPUTERNAME_LENGTH + sizeof(AFSCLIENT_ADMIN_GROUPNAME) + 2 ];
131 char *pszRefDomain = NULL;
132 SID_NAME_USE snu = SidTypeGroup;
134 dwSize = sizeof(pszAdminGroup);
136 if (!GetComputerName(pszAdminGroup, &dwSize)) {
137 /* Can't get computer name. We return false in this case.
138 Retain fAdmin and fTested. This shouldn't happen.*/
145 strcat(pszAdminGroup,"\\");
146 strcat(pszAdminGroup, AFSCLIENT_ADMIN_GROUPNAME);
148 LookupAccountName(NULL, pszAdminGroup, NULL, &dwSize, NULL, &dwSize2, &snu);
149 /* that should always fail. */
151 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
152 /* if we can't find the group, then we allow the operation */
157 if (dwSize == 0 || dwSize2 == 0) {
163 psidAdmin = (PSID)malloc(dwSize); memset(psidAdmin,0,dwSize);
165 pszRefDomain = (char *)malloc(dwSize2);
166 assert(pszRefDomain);
168 if (!LookupAccountName(NULL, pszAdminGroup, psidAdmin, &dwSize, pszRefDomain, &dwSize2, &snu)) {
169 /* We can't lookup the group now even though we looked it up earlier.
170 Could this happen? */
173 /* Then open our current ProcessToken */
176 if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &hToken))
179 if (!CheckTokenMembership(hToken, psidAdmin, &fAdmin)) {
180 /* We'll have to allocate a chunk of memory to store the list of
181 * groups to which this user belongs; find out how much memory
185 PTOKEN_GROUPS pGroups;
187 GetTokenInformation (hToken, TokenGroups, NULL, dwSize, &dwSize);
189 pGroups = (PTOKEN_GROUPS)malloc(dwSize);
192 /* Allocate that buffer, and read in the list of groups. */
193 if (GetTokenInformation (hToken, TokenGroups, pGroups, dwSize, &dwSize))
195 /* Look through the list of group SIDs and see if any of them
196 * matches the AFS Client Admin group SID.
199 for (; (!fAdmin) && (iGroup < pGroups->GroupCount); ++iGroup)
201 if (EqualSid (psidAdmin, pGroups->Groups[ iGroup ].Sid)) {
211 /* if do not have permission because we were not explicitly listed
212 * in the Admin Client Group let's see if we are the SYSTEM account
215 PTOKEN_USER pTokenUser;
216 SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
217 PSID pSidLocalSystem = 0;
220 GetTokenInformation(hToken, TokenUser, NULL, 0, &dwSize);
222 pTokenUser = (PTOKEN_USER)malloc(dwSize);
225 if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwSize, &dwSize))
226 gle = GetLastError();
228 if (AllocateAndInitializeSid( &SIDAuth, 1,
229 SECURITY_LOCAL_SYSTEM_RID,
233 if (EqualSid(pTokenUser->User.Sid, pSidLocalSystem)) {
237 FreeSid(pSidLocalSystem);
255 /* return a static pointer to a buffer */
256 static char *Parent(apath)
259 strcpy(tspace, apath);
260 tp = strrchr(tspace, '\\');
262 *(tp+1) = 0; /* lv trailing slash so Parent("k:\foo") is "k:\" not "k:" */
265 fs_ExtractDriveLetter(apath, tspace);
272 static ListLinkCmd(as)
273 register struct cmd_syndesc *as; {
274 register afs_int32 code;
275 struct ViceIoctl blob;
277 register struct cmd_item *ti;
278 char orig_name[1024]; /*Original name, may be modified*/
279 char true_name[1024]; /*``True'' dirname (e.g., symlink target)*/
280 char parent_dir[1024]; /*Parent directory of true name*/
281 register char *last_component; /*Last component of true name*/
283 struct stat statbuff; /*Buffer for status info*/
284 #endif /* not WIN32 */
286 int link_chars_read; /*Num chars read in readlink()*/
287 #endif /* not WIN32 */
288 int thru_symlink; /*Did we get to a mount point via a symlink?*/
291 for(ti=as->parms[0].items; ti; ti=ti->next) {
295 strcpy(orig_name, ti->data);
296 #else /* not WIN32 */
297 sprintf(orig_name, "%s%s",
298 (ti->data[0] == '/') ? "" : "./",
300 #endif /* not WIN32 */
303 if (lstat(orig_name, &statbuff) < 0) {
304 /* if lstat fails, we should still try the pioctl, since it
305 may work (for example, lstat will fail, but pioctl will
306 work if the volume of offline (returning ENODEV). */
307 statbuff.st_mode = S_IFDIR; /* lie like pros */
311 * The lstat succeeded. If the given file is a symlink, substitute
312 * the file name with the link name.
314 if ((statbuff.st_mode & S_IFMT) == S_IFLNK) {
317 * Read name of resolved file.
319 link_chars_read = readlink(orig_name, true_name, 1024);
320 if (link_chars_read <= 0) {
321 fprintf(stderr,"%s: Can't read target name for '%s' symbolic link!\n",
327 * Add a trailing null to what was read, bump the length.
329 true_name[link_chars_read++] = 0;
332 * If the symlink is an absolute pathname, we're fine. Otherwise, we
333 * have to create a full pathname using the original name and the
334 * relative symlink name. Find the rightmost slash in the original
335 * name (we know there is one) and splice in the symlink value.
337 if (true_name[0] != '\\') {
338 last_component = (char *) strrchr(orig_name, '\\');
339 strcpy(++last_component, true_name);
340 strcpy(true_name, orig_name);
344 strcpy(true_name, orig_name);
346 strcpy(true_name, orig_name);
350 * Find rightmost slash, if any.
352 last_component = (char *) strrchr(true_name, '\\');
354 last_component = (char *) strrchr(true_name, '/');
355 if (last_component) {
357 * Found it. Designate everything before it as the parent directory,
358 * everything after it as the final component.
360 strncpy(parent_dir, true_name, last_component - true_name + 1);
361 parent_dir[last_component - true_name + 1] = 0;
362 last_component++; /*Skip the slash*/
365 if (!InAFS(parent_dir)) {
366 const char * nbname = NetbiosName();
367 int len = (int)strlen(nbname);
369 if (parent_dir[0] == '\\' && parent_dir[1] == '\\' &&
370 parent_dir[len+2] == '\\' &&
371 parent_dir[len+3] == '\0' &&
372 !strnicmp(nbname,&parent_dir[2],len))
374 sprintf(parent_dir,"\\\\%s\\all\\", nbname);
381 * No slash appears in the given file name. Set parent_dir to the current
382 * directory, and the last component as the given name.
384 fs_ExtractDriveLetter(true_name, parent_dir);
385 strcat(parent_dir, ".");
386 last_component = true_name;
387 fs_StripDriveLetter(true_name, true_name, sizeof(true_name));
390 if (strcmp(last_component, ".") == 0 || strcmp(last_component, "..") == 0) {
391 fprintf(stderr,"%s: you may not use '.' or '..' as the last component\n", pn);
392 fprintf(stderr,"%s: of a name in the 'symlink list' command.\n", pn);
396 blob.in = last_component;
397 blob.in_size = (long)strlen(last_component)+1;
398 blob.out_size = MAXSIZE;
400 memset(space, 0, MAXSIZE);
402 code = pioctl(parent_dir, VIOC_LISTSYMLINK, &blob, 1);
405 printf("'%s' is a %ssymlink to '%s'\n",
407 (thru_symlink ? "symbolic link, leading to a " : ""),
413 fprintf(stderr,"'%s' is not a symlink.\n",
416 Die(errno, (ti->data ? ti->data : parent_dir));
423 static MakeLinkCmd(as)
424 register struct cmd_syndesc *as; {
425 register afs_int32 code;
426 struct ViceIoctl blob;
428 char path[1024] = "";
430 strcpy(path, as->parms[0].items->data);
431 parent = Parent(path);
433 if (!InAFS(parent)) {
435 const char * nbname = NetbiosName();
436 int len = (int)strlen(nbname);
438 if (parent[0] == '\\' && parent[1] == '\\' &&
439 parent[len+2] == '\\' &&
440 parent[len+3] == '\0' &&
441 !strnicmp(nbname,&parent[2],len))
443 sprintf(path,"%sall\\%s", parent, &as->parms[0].items->data[strlen(parent)]);
444 parent = Parent(path);
445 if (!InAFS(parent)) {
446 fprintf(stderr,"%s: symlinks must be created within the AFS file system\n", pn);
452 fprintf(stderr,"%s: symlinks must be created within the AFS file system\n", pn);
458 if ( IsFreelanceRoot(parent) && !IsAdmin() ) {
459 fprintf(stderr,"%s: Only AFS Client Administrators may alter the root.afs volume\n", pn);
463 /* create symlink with a special pioctl for Windows NT, since it doesn't
464 * have a symlink system call.
467 blob.in_size = 1 + (long)strlen(as->parms[1].items->data);
468 blob.in = as->parms[1].items->data;
470 code = pioctl(path, VIOC_SYMLINK, &blob, 0);
471 #else /* not WIN32 */
472 code = symlink(as->parms[1].items->data, path);
473 #endif /* not WIN32 */
475 Die(errno, as->parms[0].items->data);
482 * Delete AFS symlinks. Variables are used as follows:
483 * tbuffer: Set to point to the null-terminated directory name of the
484 * symlink (or ``.'' if none is provided)
485 * tp: Set to point to the actual name of the symlink to nuke.
487 static RemoveLinkCmd(as)
488 register struct cmd_syndesc *as; {
489 register afs_int32 code=0;
490 struct ViceIoctl blob;
491 register struct cmd_item *ti;
496 for(ti=as->parms[0].items; ti; ti=ti->next) {
498 tp = (char *) strrchr(ti->data, '\\');
500 tp = (char *) strrchr(ti->data, '/');
502 strncpy(tbuffer, ti->data, code=(afs_int32)(tp-ti->data+1)); /* the dir name */
504 tp++; /* skip the slash */
507 if (!InAFS(tbuffer)) {
508 const char * nbname = NetbiosName();
509 int len = (int)strlen(nbname);
511 if (tbuffer[0] == '\\' && tbuffer[1] == '\\' &&
512 tbuffer[len+2] == '\\' &&
513 tbuffer[len+3] == '\0' &&
514 !strnicmp(nbname,&tbuffer[2],len))
516 sprintf(tbuffer,"\\\\%s\\all\\", nbname);
522 fs_ExtractDriveLetter(ti->data, tbuffer);
523 strcat(tbuffer, ".");
525 fs_StripDriveLetter(tp, tp, 0);
528 blob.in_size = (int)strlen(tp)+1;
530 blob.out_size = sizeof(lsbuffer);
531 code = pioctl(tbuffer, VIOC_LISTSYMLINK, &blob, 0);
534 fprintf(stderr,"symlink: '%s' is not a symlink.\n", ti->data);
536 Die(errno, ti->data);
538 continue; /* don't bother trying */
541 if ( IsFreelanceRoot(tbuffer) && !IsAdmin() ) {
542 fprintf(stderr,"symlink: Only AFS Client Administrators may alter the root.afs volume\n");
549 blob.in_size = (long)strlen(tp)+1;
550 code = pioctl(tbuffer, VIOC_DELSYMLINK, &blob, 0);
552 Die(errno, ti->data);
558 static struct ViceIoctl gblob;
559 static int debug = 0;
564 register afs_int32 code;
565 register struct cmd_syndesc *ts;
569 * The following signal action for AIX is necessary so that in case of a
570 * crash (i.e. core is generated) we can include the user's data section
571 * in the core dump. Unfortunately, by default, only a partial core is
572 * generated which, in many cases, isn't too useful.
574 struct sigaction nsa;
576 sigemptyset(&nsa.sa_mask);
577 nsa.sa_handler = SIG_DFL;
578 nsa.sa_flags = SA_FULLDUMP;
579 sigaction(SIGSEGV, &nsa, NULL);
584 WSAStartup(0x0101, &WSAjunk);
587 /* try to find volume location information */
592 ts = cmd_CreateSyntax("list", ListLinkCmd, 0, "list symlink");
593 cmd_AddParm(ts, "-name", CMD_LIST, 0, "name");
595 ts = cmd_CreateSyntax("make", MakeLinkCmd, 0, "make symlink");
596 cmd_AddParm(ts, "-name", CMD_SINGLE, 0, "name");
597 cmd_AddParm(ts, "-to", CMD_SINGLE, 0, "target");
599 ts = cmd_CreateSyntax("remove", RemoveLinkCmd, 0, "remove symlink");
600 cmd_AddParm(ts, "-name", CMD_LIST, 0, "name");
601 cmd_CreateAlias(ts, "rm");
603 code = cmd_Dispatch(argc, argv);
606 if (rxInitDone) rx_Finalize();
607 #endif /* not WIN32 */
612 void Die(code, filename)
617 if (code == EINVAL) {
619 fprintf(stderr,"%s: Invalid argument; it is possible that %s is not in AFS.\n", pn, filename);
620 else fprintf(stderr,"%s: Invalid argument.\n", pn);
622 else if (code == ENOENT) {
623 if (filename) fprintf(stderr,"%s: File '%s' doesn't exist\n", pn, filename);
624 else fprintf(stderr,"%s: no such file returned\n", pn);
626 else if (code == EROFS) fprintf(stderr,"%s: You can not change a backup or readonly volume\n", pn);
627 else if (code == EACCES || code == EPERM) {
628 if (filename) fprintf(stderr,"%s: You don't have the required access rights on '%s'\n", pn, filename);
629 else fprintf(stderr,"%s: You do not have the required rights to do this operation\n", pn);
631 else if (code == ENODEV) {
632 fprintf(stderr,"%s: AFS service may not have started.\n", pn);
634 else if (code == ESRCH) {
635 fprintf(stderr,"%s: Cell name not recognized.\n", pn);
637 else if (code == EPIPE) {
638 fprintf(stderr,"%s: Volume name or ID not recognized.\n", pn);
640 else if (code == EFBIG) {
641 fprintf(stderr,"%s: Cache size too large.\n", pn);
643 else if (code == ETIMEDOUT) {
645 fprintf(stderr,"%s:'%s': Connection timed out", pn, filename);
647 fprintf(stderr,"%s: Connection timed out", pn);
650 if (filename) fprintf(stderr,"%s:'%s'", pn, filename);
651 else fprintf(stderr,"%s", pn);
653 fprintf(stderr, ": code 0x%x\n", code);
654 #else /* not WIN32 */
655 fprintf(stderr,": %s\n", afs_error_message(code));
656 #endif /* not WIN32 */