1 1.1 christos /* $NetBSD: pawd.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1.1.3 christos * Copyright (c) 1997-2014 Erez Zadok 5 1.1 christos * Copyright (c) 1990 Jan-Simon Pendry 6 1.1 christos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 7 1.1 christos * Copyright (c) 1990 The Regents of the University of California. 8 1.1 christos * All rights reserved. 9 1.1 christos * 10 1.1 christos * This code is derived from software contributed to Berkeley by 11 1.1 christos * Jan-Simon Pendry at Imperial College, London. 12 1.1 christos * 13 1.1 christos * Redistribution and use in source and binary forms, with or without 14 1.1 christos * modification, are permitted provided that the following conditions 15 1.1 christos * are met: 16 1.1 christos * 1. Redistributions of source code must retain the above copyright 17 1.1 christos * notice, this list of conditions and the following disclaimer. 18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 19 1.1 christos * notice, this list of conditions and the following disclaimer in the 20 1.1 christos * documentation and/or other materials provided with the distribution. 21 1.1.1.3 christos * 3. Neither the name of the University nor the names of its contributors 22 1.1 christos * may be used to endorse or promote products derived from this software 23 1.1 christos * without specific prior written permission. 24 1.1 christos * 25 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 christos * SUCH DAMAGE. 36 1.1 christos * 37 1.1 christos * 38 1.1 christos * File: am-utils/amq/pawd.c 39 1.1 christos * 40 1.1 christos */ 41 1.1 christos 42 1.1 christos /* 43 1.1 christos * pawd is similar to pwd, except that it returns more "natural" versions of 44 1.1 christos * pathnames for directories automounted with the amd automounter. If any 45 1.1 christos * arguments are given, the "more natural" form of the given pathnames are 46 1.1 christos * printed. 47 1.1 christos * 48 1.1 christos * Paul Anderson (paul (at) ed.lfcs) 49 1.1 christos * 50 1.1 christos */ 51 1.1 christos 52 1.1 christos #ifdef HAVE_CONFIG_H 53 1.1 christos # include <config.h> 54 1.1 christos #endif /* HAVE_CONFIG_H */ 55 1.1 christos #include <am_defs.h> 56 1.1 christos #include <amq.h> 57 1.1 christos 58 1.1 christos 59 1.1 christos /* statics */ 60 1.1 christos static char *localhost = "localhost"; 61 1.1 christos static char transform[MAXPATHLEN]; 62 1.1 christos 63 1.1 christos 64 1.1 christos #ifdef HAVE_CNODEID 65 1.1 christos static char * 66 1.1 christos cluster_server(void) 67 1.1 christos { 68 1.1 christos # ifdef HAVE_EXTERN_GETCCENT 69 1.1 christos struct cct_entry *cp; 70 1.1 christos # endif /* HAVE_EXTERN_GETCCENT */ 71 1.1 christos 72 1.1 christos if (cnodeid() == 0) 73 1.1 christos return localhost; 74 1.1 christos 75 1.1 christos # ifdef HAVE_EXTERN_GETCCENT 76 1.1 christos while ((cp = getccent())) 77 1.1 christos if (cp->cnode_type == 'r') 78 1.1 christos return cp->cnode_name; 79 1.1 christos # endif /* HAVE_EXTERN_GETCCENT */ 80 1.1 christos 81 1.1 christos return localhost; 82 1.1 christos } 83 1.1 christos #endif /* HAVE_CNODEID */ 84 1.1 christos 85 1.1 christos 86 1.1 christos /* DISK_HOME_HACK added by gdmr */ 87 1.1 christos #ifdef DISK_HOME_HACK 88 1.1 christos static char * 89 1.1 christos hack_name(char *dir) 90 1.1 christos { 91 1.1 christos char partition[MAXPATHLEN]; 92 1.1 christos char username[MAXPATHLEN]; 93 1.1 christos char hesiod_lookup[MAXPATHLEN]; 94 1.1 christos char *to, *ch, *hes_name, *dot; 95 1.1 christos char **hes; 96 1.1 christos 97 1.1 christos #ifdef DEBUG 98 1.1 christos fprintf(stderr, "hack_name(%s)\n", dir); 99 1.1 christos #endif /* DEBUG */ 100 1.1 christos 101 1.1 christos if (dir[0] == '/' && dir[1] == 'a' && dir[2] == '/') { 102 1.1 christos /* Could be /a/server/disk/home/partition/user... */ 103 1.1 christos ch = dir + 3; 104 1.1 christos while (*ch && *ch != '/') ch++; /* Skip server */ 105 1.1 christos if (!NSTREQ(ch, "/disk/home/", 11)) 106 1.1 christos return NULL; /* Nope */ 107 1.1 christos /* Looking promising, next should be the partition name */ 108 1.1 christos ch += 11; 109 1.1 christos to = partition; 110 1.1 christos while (*ch && *ch != '/') *to++ = *ch++; 111 1.1 christos to = '\0'; 112 1.1 christos if (!(*ch)) 113 1.1 christos return NULL; /* Off the end */ 114 1.1 christos /* Now the username */ 115 1.1 christos ch++; 116 1.1 christos to = username; 117 1.1 christos while (*ch && *ch != '/') *to++ = *ch++; 118 1.1 christos to = '\0'; 119 1.1 christos #ifdef DEBUG 120 1.1 christos fprintf(stderr, "partition %s, username %s\n", partition, username); 121 1.1 christos #endif /* DEBUG */ 122 1.1 christos 123 1.1 christos xsnprintf(hesiod_lookup, sizeof(hesiod_lookup), 124 1.1 christos "%s.homes-remote", username); 125 1.1 christos hes = hes_resolve(hesiod_lookup, "amd"); 126 1.1 christos if (!hes) 127 1.1 christos return NULL; 128 1.1 christos #ifdef DEBUG 129 1.1 christos fprintf(stderr, "hesiod -> <%s>\n", *hes); 130 1.1 christos #endif /* DEBUG */ 131 1.1 christos hes_name = strstr(*hes, "/homes/remote/"); 132 1.1 christos if (!hes_name) return NULL; 133 1.1 christos hes_name += 14; 134 1.1 christos #ifdef DEBUG 135 1.1 christos fprintf(stderr, "hesiod -> <%s>\n", hes_name); 136 1.1 christos #endif /* DEBUG */ 137 1.1 christos dot = hes_name; 138 1.1 christos while (*dot && *dot != '.') dot++; 139 1.1 christos *dot = '\0'; 140 1.1 christos #ifdef DEBUG 141 1.1 christos fprintf(stderr, "hesiod -> <%s>\n", hes_name); 142 1.1 christos #endif /* DEBUG */ 143 1.1 christos 144 1.1 christos if (strcmp(partition, hes_name)) return NULL; 145 1.1 christos #ifdef DEBUG 146 1.1 christos fprintf(stderr, "A match, munging....\n"); 147 1.1 christos #endif /* DEBUG */ 148 1.1 christos xstrlcpy(transform, "/home/", sizeof(transform)); 149 1.1 christos xstrlcat(transform, username, sizeof(transform)); 150 1.1 christos if (*ch) 151 1.1 christos xstrlcat(transform, ch, sizeof(transform)); 152 1.1 christos #ifdef DEBUG 153 1.1 christos fprintf(stderr, "Munged to <%s>\n", transform); 154 1.1 christos #endif /* DEBUG */ 155 1.1 christos return transform; 156 1.1 christos } 157 1.1 christos return NULL; 158 1.1 christos } 159 1.1 christos #endif /* DISK_HOME_HACK */ 160 1.1 christos 161 1.1 christos 162 1.1 christos /* 163 1.1 christos * The routine transform_dir(path) transforms pathnames of directories 164 1.1 christos * mounted with the amd automounter to produce a more "natural" version. 165 1.1 christos * The automount table is obtained from the local amd via the rpc interface 166 1.1 christos * and reverse lookups are repeatedly performed on the directory name 167 1.1 christos * substituting the name of the automount link for the value of the link 168 1.1 christos * whenever it occurs as a prefix of the directory name. 169 1.1 christos */ 170 1.1 christos static char * 171 1.1 christos transform_dir(char *dir) 172 1.1 christos { 173 1.1 christos #ifdef DISK_HOME_HACK 174 1.1 christos char *ch; 175 1.1 christos #endif /* DISK_HOME_HACK */ 176 1.1 christos char *server; 177 1.1 christos struct sockaddr_in server_addr; 178 1.1 christos int s = RPC_ANYSOCK; 179 1.1 christos CLIENT *clnt; 180 1.1 christos struct hostent *hp; 181 1.1 christos struct timeval tmo = {10, 0}; 182 1.1 christos char *dummystr; 183 1.1 christos amq_string *spp; 184 1.1 christos 185 1.1 christos #ifdef DISK_HOME_HACK 186 1.1 christos if (ch = hack_name(dir)) 187 1.1 christos return ch; 188 1.1 christos #endif /* DISK_HOME_HACK */ 189 1.1 christos 190 1.1 christos #ifdef HAVE_CNODEID 191 1.1 christos server = cluster_server(); 192 1.1 christos #else /* not HAVE_CNODEID */ 193 1.1 christos server = localhost; 194 1.1 christos #endif /* not HAVE_CNODEID */ 195 1.1 christos 196 1.1 christos if ((hp = gethostbyname(server)) == NULL) 197 1.1 christos return dir; 198 1.1 christos memset(&server_addr, 0, sizeof(server_addr)); 199 1.1 christos /* as per POSIX, sin_len need not be set (used internally by kernel) */ 200 1.1 christos server_addr.sin_family = AF_INET; 201 1.1 christos server_addr.sin_addr = *(struct in_addr *) hp->h_addr; 202 1.1 christos 203 1.1 christos clnt = clntudp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, tmo, &s); 204 1.1 christos if (clnt == NULL) 205 1.1 christos clnt = clnttcp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, &s, 0, 0); 206 1.1 christos if (clnt == NULL) 207 1.1 christos return dir; 208 1.1 christos 209 1.1 christos xstrlcpy(transform, dir, sizeof(transform)); 210 1.1 christos dummystr = transform; 211 1.1 christos spp = amqproc_pawd_1((amq_string *) &dummystr, clnt); 212 1.1 christos if (spp && *spp && **spp) { 213 1.1 christos xstrlcpy(transform, *spp, sizeof(transform)); 214 1.1 christos XFREE(*spp); 215 1.1 christos } 216 1.1 christos clnt_destroy(clnt); 217 1.1 christos return transform; 218 1.1 christos } 219 1.1 christos 220 1.1 christos 221 1.1 christos /* getawd() is a substitute for getwd() which transforms the path */ 222 1.1 christos static char * 223 1.1 christos getawd(char *path, size_t l) 224 1.1 christos { 225 1.1 christos #ifdef HAVE_GETCWD 226 1.1 christos char *wd = getcwd(path, MAXPATHLEN); 227 1.1 christos #else /* not HAVE_GETCWD */ 228 1.1 christos char *wd = getwd(path); 229 1.1 christos #endif /* not HAVE_GETCWD */ 230 1.1 christos 231 1.1 christos if (wd == NULL) { 232 1.1 christos return NULL; 233 1.1 christos } 234 1.1 christos xstrlcpy(path, transform_dir(wd), l); 235 1.1 christos return path; 236 1.1 christos } 237 1.1 christos 238 1.1 christos 239 1.1 christos int 240 1.1 christos main(int argc, char *argv[]) 241 1.1 christos { 242 1.1 christos char tmp_buf[MAXPATHLEN], *wd; 243 1.1 christos 244 1.1 christos if (argc == 1) { 245 1.1 christos wd = getawd(tmp_buf, sizeof(tmp_buf)); 246 1.1 christos if (wd == NULL) { 247 1.1 christos fprintf(stderr, "pawd: %s\n", tmp_buf); 248 1.1 christos exit(1); 249 1.1 christos } else { 250 1.1 christos fprintf(stdout, "%s\n", wd); 251 1.1 christos } 252 1.1 christos } else { 253 1.1 christos while (--argc) { 254 1.1 christos wd = transform_dir(*++argv); 255 1.1 christos fprintf(stdout, "%s\n", wd); 256 1.1 christos } 257 1.1 christos } 258 1.1 christos exit(0); 259 1.1 christos } 260