1/* 2 * Copyright (c) 1997-2014 Erez Zadok 3 * Copyright (c) 1990 Jan-Simon Pendry 4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 5 * Copyright (c) 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry at Imperial College, London. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * 36 * File: am-utils/amd/get_args.c 37 * 38 */ 39 40/* 41 * Argument decode 42 */ 43 44#ifdef HAVE_CONFIG_H 45# include <config.h> 46#endif /* HAVE_CONFIG_H */ 47#include <am_defs.h> 48#include <amd.h> 49 50/* include auto-generated version file */ 51#include <build_version.h> 52 53char *amu_conf_file = "/etc/amd.conf"; /* default amd configuration file */ 54char *conf_tag = NULL; /* default conf file tags to use */ 55int usage = 0; 56int use_conf_file = 0; /* default don't use amd.conf file */ 57char *mnttab_file_name = NULL; /* symbol must be available always */ 58 59 60/* 61 * Return the version string (dynamic buffer) 62 */ 63char * 64get_version_string(void) 65{ 66 char *vers = NULL; 67 char tmpbuf[1024]; 68 char *wire_buf; 69 int wire_buf_len = 0; 70 size_t len; /* max allocated length (to avoid buf overflow) */ 71 72 /* 73 * First get dynamic string listing all known networks. 74 * This could be a long list, if host has lots of interfaces. 75 */ 76 wire_buf = print_wires(); 77 if (wire_buf) 78 wire_buf_len = strlen(wire_buf); 79 80 len = 2048 + wire_buf_len; 81 vers = xmalloc(len); 82 xsnprintf(vers, len, "%s\n%s\n%s\n%s\n", 83 "Copyright (c) 1997-2014 Erez Zadok", 84 "Copyright (c) 1990 Jan-Simon Pendry", 85 "Copyright (c) 1990 Imperial College of Science, Technology & Medicine", 86 "Copyright (c) 1990 The Regents of the University of California."); 87 xsnprintf(tmpbuf, sizeof(tmpbuf), "%s version %s (build %d).\n", 88 PACKAGE_NAME, PACKAGE_VERSION, AMU_BUILD_VERSION); 89 xstrlcat(vers, tmpbuf, len); 90 xsnprintf(tmpbuf, sizeof(tmpbuf), "Report bugs to %s.\n", PACKAGE_BUGREPORT); 91 xstrlcat(vers, tmpbuf, len); 92#if 0 93 /* 94 * XXX This block (between from the #if 0 to #endif was in the 95 * XXX original was in the original merge however in the interest 96 * XXX of reproduceable builds and the fact that this is redundant 97 * XXX information, it is effectively removed. 98 */ 99 xsnprintf(tmpbuf, sizeof(tmpbuf), "Configured by %s@%s on date %s.\n", 100 USER_NAME, HOST_NAME, CONFIG_DATE); 101 xstrlcat(vers, tmpbuf, len); 102 xsnprintf(tmpbuf, sizeof(tmpbuf), "Built by %s@%s on date %s.\n", 103 BUILD_USER, BUILD_HOST, BUILD_DATE); 104 xstrlcat(vers, tmpbuf, len); 105#endif 106 xsnprintf(tmpbuf, sizeof(tmpbuf), "cpu=%s (%s-endian), arch=%s, karch=%s.\n", 107 cpu, endian, gopt.arch, gopt.karch); 108 xstrlcat(vers, tmpbuf, len); 109 xsnprintf(tmpbuf, sizeof(tmpbuf), "full_os=%s, os=%s, osver=%s, vendor=%s, distro=%s.\n", 110 gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor, DISTRO_NAME); 111 xstrlcat(vers, tmpbuf, len); 112 xsnprintf(tmpbuf, sizeof(tmpbuf), "domain=%s, host=%s, hostd=%s.\n", 113 hostdomain, am_get_hostname(), hostd); 114 xstrlcat(vers, tmpbuf, len); 115 116 xstrlcat(vers, "Map support for: ", len); 117 mapc_showtypes(tmpbuf, sizeof(tmpbuf)); 118 xstrlcat(vers, tmpbuf, len); 119 xstrlcat(vers, ".\nAMFS: ", len); 120 ops_showamfstypes(tmpbuf, sizeof(tmpbuf)); 121 xstrlcat(vers, tmpbuf, len); 122 xstrlcat(vers, ", inherit.\nFS: ", len); /* hack: "show" that we support type:=inherit */ 123 ops_showfstypes(tmpbuf, sizeof(tmpbuf)); 124 xstrlcat(vers, tmpbuf, len); 125 126 /* append list of networks if available */ 127 if (wire_buf) { 128 xstrlcat(vers, wire_buf, len); 129 XFREE(wire_buf); 130 } 131 132 return vers; 133} 134 135 136static void 137show_usage(void) 138{ 139 fprintf(stderr, 140 "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\ 141\t[-k kernel_arch] [-l logfile%s\n\ 142\t[-t timeout.retrans] [-w wait_timeout] [-A arch] [-C cluster_name]\n\ 143\t[-o op_sys_ver] [-O op_sys_name]\n\ 144\t[-F conf_file] [-T conf_tag]", am_get_progname(), 145#ifdef HAVE_SYSLOG 146# ifdef LOG_DAEMON 147 "|\"syslog[:facility]\"]" 148# else /* not LOG_DAEMON */ 149 "|\"syslog\"]" 150# endif /* not LOG_DAEMON */ 151#else /* not HAVE_SYSLOG */ 152 "]" 153#endif /* not HAVE_SYSLOG */ 154 ); 155 156#ifdef HAVE_MAP_NIS 157 fputs(" [-y nis-domain]\n", stderr); 158#else /* not HAVE_MAP_NIS */ 159 fputc('\n', stderr); 160#endif /* HAVE_MAP_NIS */ 161 162 show_opts('x', xlog_opt); 163#ifdef DEBUG 164 show_opts('D', dbg_opt); 165#endif /* DEBUG */ 166 fprintf(stderr, "\t[directory mapname [-map_options]] ...\n"); 167} 168 169 170void 171get_args(int argc, char *argv[]) 172{ 173 int opt_ch, i; 174 FILE *fp = stdin; 175 char getopt_arguments[] = "+nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:HA:"; 176 char *getopt_args; 177 int print_version = 0; /* 1 means we should print version info */ 178 179#ifdef HAVE_GNU_GETOPT 180 getopt_args = getopt_arguments; 181#else /* ! HAVE_GNU_GETOPT */ 182 getopt_args = &getopt_arguments[1]; 183#endif /* HAVE_GNU_GETOPT */ 184 185 /* if no arguments were passed, try to use /etc/amd.conf file */ 186 if (argc <= 1) 187 use_conf_file = 1; 188 189 while ((opt_ch = getopt(argc, argv, getopt_args)) != -1) 190 switch (opt_ch) { 191 192 case 'a': 193 if (*optarg != '/') { 194 fprintf(stderr, "%s: -a option must begin with a '/'\n", 195 am_get_progname()); 196 exit(1); 197 } 198 gopt.auto_dir = optarg; 199 break; 200 201 case 'c': 202 gopt.am_timeo = atoi(optarg); 203 if (gopt.am_timeo <= 0) 204 gopt.am_timeo = AM_TTL; 205 break; 206 207 case 'd': 208 gopt.sub_domain = optarg; 209 break; 210 211 case 'k': 212 gopt.karch = optarg; 213 break; 214 215 case 'l': 216 gopt.logfile = optarg; 217 break; 218 219 case 'n': 220 gopt.flags |= CFM_NORMALIZE_HOSTNAMES; 221 break; 222 223 case 'o': 224 gopt.op_sys_ver = optarg; 225 break; 226 227 case 'p': 228 gopt.flags |= CFM_PRINT_PID; 229 break; 230 231 case 'r': 232 gopt.flags |= CFM_RESTART_EXISTING_MOUNTS; 233 break; 234 235 case 't': 236 /* timeo.retrans (also affects toplvl mounts) */ 237 { 238 char *dot = strchr(optarg, '.'); 239 int i; 240 if (dot) 241 *dot = '\0'; 242 if (*optarg) { 243 for (i=0; i<AMU_TYPE_MAX; ++i) 244 gopt.amfs_auto_timeo[i] = atoi(optarg); 245 } 246 if (dot) { 247 for (i=0; i<AMU_TYPE_MAX; ++i) 248 gopt.amfs_auto_retrans[i] = atoi(dot + 1); 249 *dot = '.'; 250 } 251 } 252 break; 253 254 case 'v': 255 /* 256 * defer to print version info after every variable had been 257 * initialized. 258 */ 259 print_version++; 260 break; 261 262 case 'w': 263 gopt.am_timeo_w = atoi(optarg); 264 if (gopt.am_timeo_w <= 0) 265 gopt.am_timeo_w = AM_TTL_W; 266 break; 267 268 case 'x': 269 usage += switch_option(optarg); 270 break; 271 272 case 'y': 273#ifdef HAVE_MAP_NIS 274 gopt.nis_domain = optarg; 275#else /* not HAVE_MAP_NIS */ 276 plog(XLOG_USER, "-y: option ignored. No NIS support available."); 277#endif /* not HAVE_MAP_NIS */ 278 break; 279 280 case 'A': 281 gopt.arch = optarg; 282 break; 283 284 case 'C': 285 gopt.cluster = optarg; 286 break; 287 288 case 'D': 289#ifdef DEBUG 290 usage += debug_option(optarg); 291#else /* not DEBUG */ 292 fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n", 293 am_get_progname()); 294#endif /* not DEBUG */ 295 break; 296 297 case 'F': 298 amu_conf_file = optarg; 299 use_conf_file = 1; 300 break; 301 302 case 'H': 303 show_usage(); 304 exit(1); 305 break; 306 307 case 'O': 308 gopt.op_sys = optarg; 309 break; 310 311 case 'S': 312 gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */ 313 break; 314 315 case 'T': 316 conf_tag = optarg; 317 break; 318 319 default: 320 usage = 1; 321 break; 322 } 323 324 /* 325 * amd.conf file: if not command-line arguments were used, or if -F was 326 * specified, then use that amd.conf file. If the file cannot be opened, 327 * abort amd. If it can be found, open it, parse it, and then close it. 328 */ 329 if (use_conf_file && amu_conf_file) { 330 fp = fopen(amu_conf_file, "r"); 331 if (!fp) { 332 char buf[128]; 333 xsnprintf(buf, sizeof(buf), "Amd configuration file (%s)", 334 amu_conf_file); 335 perror(buf); 336 exit(1); 337 } 338 conf_in = fp; 339 conf_parse(); 340 fclose(fp); 341 if (process_all_regular_maps() != 0) 342 exit(1); 343 } 344 345#ifdef DEBUG 346 usage += switch_option("debug"); 347 /* initialize debug options */ 348 if (!debug_flags) 349 debug_flags = D_CONTROL; /* CONTROL = "daemon,amq,fork" */ 350#endif /* DEBUG */ 351 352 /* log information regarding amd.conf file */ 353 if (use_conf_file && amu_conf_file) 354 plog(XLOG_INFO, "using configuration file %s", amu_conf_file); 355 356#ifdef HAVE_MAP_LDAP 357 /* ensure that if ldap_base is specified, that also ldap_hostports is */ 358 if (gopt.ldap_hostports && !gopt.ldap_base) { 359 fprintf(stderr, "must specify both ldap_hostports and ldap_base\n"); 360 exit(1); 361 } 362#endif /* HAVE_MAP_LDAP */ 363 364 if (usage) { 365 show_usage(); 366 exit(1); 367 } 368 369 while (optind <= argc - 2) { 370 char *dir = argv[optind++]; 371 char *map = argv[optind++]; 372 char *opts = ""; 373 if (argv[optind] && *argv[optind] == '-') 374 opts = &argv[optind++][1]; 375 376 root_newmap(dir, opts, map, NULL); 377 } 378 379 if (optind == argc) { 380 /* 381 * Append domain name to hostname. 382 * sub_domain overrides hostdomain 383 * if given. 384 */ 385 if (gopt.sub_domain) 386 hostdomain = gopt.sub_domain; 387 if (*hostdomain == '.') 388 hostdomain++; 389 xstrlcat(hostd, ".", sizeof(hostd)); 390 xstrlcat(hostd, hostdomain, sizeof(hostd)); 391 392#ifdef MOUNT_TABLE_ON_FILE 393 if (amuDebug(D_MTAB)) 394 if (gopt.debug_mtab_file) 395 mnttab_file_name = gopt.debug_mtab_file; /* user supplied debug mtab path */ 396 else 397 mnttab_file_name = DEBUG_MNTTAB_FILE; /* default debug mtab path */ 398 else 399 mnttab_file_name = MNTTAB_FILE_NAME; 400#else /* not MOUNT_TABLE_ON_FILE */ 401 if (amuDebug(D_MTAB)) 402 dlog("-D mtab option ignored"); 403# ifdef MNTTAB_FILE_NAME 404 mnttab_file_name = MNTTAB_FILE_NAME; 405# endif /* MNTTAB_FILE_NAME */ 406#endif /* not MOUNT_TABLE_ON_FILE */ 407 408 /* 409 * If the kernel architecture was not specified 410 * then use the machine architecture. 411 */ 412 if (gopt.karch == NULL) 413 gopt.karch = gopt.arch; 414 415 if (gopt.cluster == NULL) 416 gopt.cluster = hostdomain; 417 418 /* sanity checking, normalize values just in case (toplvl too) */ 419 for (i=0; i<AMU_TYPE_MAX; ++i) { 420 if (gopt.amfs_auto_timeo[i] == 0) 421 gopt.amfs_auto_timeo[i] = AMFS_AUTO_TIMEO; 422 if (gopt.amfs_auto_retrans[i] == 0) 423 gopt.amfs_auto_retrans[i] = AMFS_AUTO_RETRANS(i); 424 if (gopt.amfs_auto_retrans[i] == 0) 425 gopt.amfs_auto_retrans[i] = 3; /* under very unusual circumstances, could be zero */ 426 } 427 } 428 429 /* finally print version string and exit, if asked for */ 430 if (print_version) { 431 fputs(get_version_string(), stderr); 432 exit(0); 433 } 434 435 if (switch_to_logfile(gopt.logfile, orig_umask, 436 (gopt.flags & CFM_TRUNCATE_LOG)) != 0) 437 plog(XLOG_USER, "Cannot switch logfile"); 438 439 return; 440} 441