17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*a237e38eSth199096 * Common Development and Distribution License (the "License"). 6*a237e38eSth199096 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 20*a237e38eSth199096 */ 21*a237e38eSth199096 22*a237e38eSth199096 /* 23*a237e38eSth199096 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* 287c478bd9Sstevel@tonic-gate * where.c - get full pathname including host: 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <netdb.h> 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <string.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <fcntl.h> 397c478bd9Sstevel@tonic-gate #include <errno.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 457c478bd9Sstevel@tonic-gate #include <sys/param.h> 467c478bd9Sstevel@tonic-gate #include <sys/stat.h> 477c478bd9Sstevel@tonic-gate 48*a237e38eSth199096 #include <sharefs/share.h> 497c478bd9Sstevel@tonic-gate #include "sharetab.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate extern FILE *setmntent(); 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate FILE *setsharetab(); 547c478bd9Sstevel@tonic-gate void endsharetab(); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate extern int Debug; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * where(pn, host, fsname, within) 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * pn is the pathname we are looking for, 647c478bd9Sstevel@tonic-gate * host gets the name of the host owning the file system, 657c478bd9Sstevel@tonic-gate * fsname gets the file system name on the host, 667c478bd9Sstevel@tonic-gate * within gets whatever is left from the pathname 677c478bd9Sstevel@tonic-gate * 687c478bd9Sstevel@tonic-gate * Returns: 0 if ERROR, 1 if OK 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate int 717c478bd9Sstevel@tonic-gate where(pn, host, fsname, within) 727c478bd9Sstevel@tonic-gate char *pn; 737c478bd9Sstevel@tonic-gate char *host; 747c478bd9Sstevel@tonic-gate char *fsname; 757c478bd9Sstevel@tonic-gate char *within; 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate struct stat sb; 787c478bd9Sstevel@tonic-gate char curdir[MAXPATHLEN]; 797c478bd9Sstevel@tonic-gate char qualpn[MAXPATHLEN]; 807c478bd9Sstevel@tonic-gate char *p; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (Debug) 837c478bd9Sstevel@tonic-gate printf("where: pn %s\n", pn); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (stat(pn, &sb) < 0) { 867c478bd9Sstevel@tonic-gate char *errstr; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if ((errstr = strerror(errno)) == NULL) 897c478bd9Sstevel@tonic-gate errstr = "unknown error"; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (Debug) 927c478bd9Sstevel@tonic-gate printf("where: stat failed"); 937c478bd9Sstevel@tonic-gate strcpy(within, errstr); 947c478bd9Sstevel@tonic-gate return (0); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate /* 977c478bd9Sstevel@tonic-gate * first get the working directory, 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate if (getcwd(curdir, MAXPATHLEN) == NULL) { 1007c478bd9Sstevel@tonic-gate sprintf(within, "Unable to get working directory (%s)", 1017c478bd9Sstevel@tonic-gate curdir); 1027c478bd9Sstevel@tonic-gate return (0); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate if (chdir(pn) == 0) { 1057c478bd9Sstevel@tonic-gate getcwd(qualpn, MAXPATHLEN); 1067c478bd9Sstevel@tonic-gate chdir(curdir); 1077c478bd9Sstevel@tonic-gate } else { 1087c478bd9Sstevel@tonic-gate if (p = strrchr(pn, '/')) { 1097c478bd9Sstevel@tonic-gate *p = 0; 1107c478bd9Sstevel@tonic-gate chdir(pn); 1117c478bd9Sstevel@tonic-gate (void) getcwd(qualpn, MAXPATHLEN); 1127c478bd9Sstevel@tonic-gate chdir(curdir); 1137c478bd9Sstevel@tonic-gate strcat(qualpn, "/"); 1147c478bd9Sstevel@tonic-gate strcat(qualpn, p+1); 1157c478bd9Sstevel@tonic-gate } else { 1167c478bd9Sstevel@tonic-gate strcpy(qualpn, curdir); 1177c478bd9Sstevel@tonic-gate strcat(qualpn, "/"); 1187c478bd9Sstevel@tonic-gate strcat(qualpn, pn); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate return (findmount(qualpn, host, fsname, within)); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * findmount(qualpn, host, fsname, within) 1267c478bd9Sstevel@tonic-gate * 1277c478bd9Sstevel@tonic-gate * Searches the mount table to find the appropriate file system 1287c478bd9Sstevel@tonic-gate * for a given absolute path name. 1297c478bd9Sstevel@tonic-gate * host gets the name of the host owning the file system, 1307c478bd9Sstevel@tonic-gate * fsname gets the file system name on the host, 1317c478bd9Sstevel@tonic-gate * within gets whatever is left from the pathname 1327c478bd9Sstevel@tonic-gate * 1337c478bd9Sstevel@tonic-gate * Returns: 0 on failure, 1 on success. 1347c478bd9Sstevel@tonic-gate */ 1357c478bd9Sstevel@tonic-gate int 1367c478bd9Sstevel@tonic-gate findmount(qualpn, host, fsname, within) 1377c478bd9Sstevel@tonic-gate char *qualpn; 1387c478bd9Sstevel@tonic-gate char *host; 1397c478bd9Sstevel@tonic-gate char *fsname; 1407c478bd9Sstevel@tonic-gate char *within; 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate FILE *mfp; 1437c478bd9Sstevel@tonic-gate char bestname[MAXPATHLEN]; 1447c478bd9Sstevel@tonic-gate int bestlen = 0, 1457c478bd9Sstevel@tonic-gate bestnfs = 0; 1467c478bd9Sstevel@tonic-gate struct share *exp; 1477c478bd9Sstevel@tonic-gate struct mnttab mp, 1487c478bd9Sstevel@tonic-gate *mnt; 1497c478bd9Sstevel@tonic-gate char *endhost; /* points past the colon in name */ 1507c478bd9Sstevel@tonic-gate int i, 1517c478bd9Sstevel@tonic-gate len; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate if (Debug) 1547c478bd9Sstevel@tonic-gate printf("findmount: qualpn %s\n", qualpn); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate for (i = 0; i < 10; i++) { 1577c478bd9Sstevel@tonic-gate mfp = setmntent("/etc/mnttab", "r"); 1587c478bd9Sstevel@tonic-gate if (mfp != NULL) 1597c478bd9Sstevel@tonic-gate break; 1607c478bd9Sstevel@tonic-gate sleep(1); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (mfp == NULL) { 1647c478bd9Sstevel@tonic-gate sprintf(within, "mount table problem"); 1657c478bd9Sstevel@tonic-gate return (0); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate bestname[0] = '\0'; 1697c478bd9Sstevel@tonic-gate while ((getmntent(mfp, &mp)) == 0) { 1707c478bd9Sstevel@tonic-gate if (strcmp(mp.mnt_fstype, "nfs") != 0) 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * If it is not nfs filesystem type, skip the 1737c478bd9Sstevel@tonic-gate * entry 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate continue; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate len = preflen(qualpn, mp.mnt_mountp); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (Debug) 1807c478bd9Sstevel@tonic-gate printf("preflen: %d %s %s", len, qualpn, mp.mnt_mountp); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (qualpn[len] != '/' && qualpn[len] != '\0' && len > 1) 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * If the last matching character is neither / nor 1857c478bd9Sstevel@tonic-gate * the end of the pathname, not a real match 1867c478bd9Sstevel@tonic-gate * (except for matching root, len==1) 1877c478bd9Sstevel@tonic-gate */ 1887c478bd9Sstevel@tonic-gate continue; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (len > bestlen) { 1917c478bd9Sstevel@tonic-gate bestlen = len; 1927c478bd9Sstevel@tonic-gate strncpy(bestname, mp.mnt_special, sizeof (bestname)); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate if (Debug) 1957c478bd9Sstevel@tonic-gate printf(" %s\n", bestname); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate endmntent(mfp); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate endhost = strchr(bestname, ':'); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * If the file system was of type NFS, then there should already 2047c478bd9Sstevel@tonic-gate * be a host name, otherwise, use ours. 2057c478bd9Sstevel@tonic-gate */ 2067c478bd9Sstevel@tonic-gate if (endhost) { 2077c478bd9Sstevel@tonic-gate *endhost++ = 0; 2087c478bd9Sstevel@tonic-gate strncpy(host, bestname, MAXHOSTNAMELEN); 2097c478bd9Sstevel@tonic-gate strncpy(fsname, endhost, MAXPATHLEN); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* 2127c478bd9Sstevel@tonic-gate * special case to keep the "/" when we match root 2137c478bd9Sstevel@tonic-gate */ 2147c478bd9Sstevel@tonic-gate if (bestlen == 1) 2157c478bd9Sstevel@tonic-gate bestlen = 0; 2167c478bd9Sstevel@tonic-gate } else { 2177c478bd9Sstevel@tonic-gate gethostname(host, MAXHOSTNAMELEN); 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * If this is our file system, try for an even longer 2217c478bd9Sstevel@tonic-gate * match from /etc/xtab. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate if (mfp = setsharetab()) { 2247c478bd9Sstevel@tonic-gate while (getshare(mfp, &exp) > 0) 2257c478bd9Sstevel@tonic-gate if (len = preflen(qualpn, exp->sh_path)) 2267c478bd9Sstevel@tonic-gate if (len > bestlen) { 2277c478bd9Sstevel@tonic-gate bestlen = len; 2287c478bd9Sstevel@tonic-gate strncpy(bestname, exp->sh_path, 2297c478bd9Sstevel@tonic-gate sizeof (bestname)); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate endsharetab(mfp); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate strncpy(fsname, qualpn, bestlen); 2347c478bd9Sstevel@tonic-gate fsname[bestlen] = 0; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate strncpy(within, qualpn + bestlen, MAXPATHLEN); 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (Debug) 2397c478bd9Sstevel@tonic-gate printf("findmount: qualpn %s\nhost %s\nfsname %s\nwithin %s\n", 2407c478bd9Sstevel@tonic-gate qualpn, host, fsname, within); 2417c478bd9Sstevel@tonic-gate return (1); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Returns: length of second argument if it is a prefix of the 2467c478bd9Sstevel@tonic-gate * first argument, otherwise zero. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate int 2497c478bd9Sstevel@tonic-gate preflen(str, pref) 2507c478bd9Sstevel@tonic-gate char *str, *pref; 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate int len; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate len = strlen(pref); 2557c478bd9Sstevel@tonic-gate if (strncmp(str, pref, len) == 0) 2567c478bd9Sstevel@tonic-gate return (len); 2577c478bd9Sstevel@tonic-gate return (0); 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate FILE 2617c478bd9Sstevel@tonic-gate *setsharetab() 2627c478bd9Sstevel@tonic-gate { 2637c478bd9Sstevel@tonic-gate FILE *f; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate f = fopen(SHARETAB, "r"); 2667c478bd9Sstevel@tonic-gate if (f == NULL) { 2677c478bd9Sstevel@tonic-gate return (NULL); 2687c478bd9Sstevel@tonic-gate } 269*a237e38eSth199096 2707c478bd9Sstevel@tonic-gate return (f); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate void 2757c478bd9Sstevel@tonic-gate endsharetab(f) 2767c478bd9Sstevel@tonic-gate FILE *f; 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate (void) fclose(f); 2797c478bd9Sstevel@tonic-gate } 280