10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*13093SRoger.Faulkner@Oracle.COM
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <ctype.h>
270Sstevel@tonic-gate #include <errno.h>
280Sstevel@tonic-gate #include <locale.h>
290Sstevel@tonic-gate #include <stdarg.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate #include <nfs/nfs.h>
360Sstevel@tonic-gate #include <assert.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/stat.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include "nfslog_config.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate #define ERROR_BUFSZ 100
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * This flag controls where error messages go.
470Sstevel@tonic-gate * Zero means that messages go to stderr.
480Sstevel@tonic-gate * Non-zero means that messages go to syslog.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate boolean_t nfsl_errs_to_syslog;
510Sstevel@tonic-gate
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate * Pointer to the global entry in the list
540Sstevel@tonic-gate */
550Sstevel@tonic-gate static nfsl_config_t *global = NULL;
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * Pointer to the raw global entry in the list, this is the
590Sstevel@tonic-gate * global entry without the expanded paths. This is used to
600Sstevel@tonic-gate * complete configurations.
610Sstevel@tonic-gate */
620Sstevel@tonic-gate static nfsl_config_t *global_raw = NULL;
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * Last modification time to config file.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static timestruc_t config_last_modification = { 0 };
680Sstevel@tonic-gate
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Whitespace characters to delimit fields in a line.
710Sstevel@tonic-gate */
720Sstevel@tonic-gate static const char *whitespace = " \t";
730Sstevel@tonic-gate
740Sstevel@tonic-gate static int getconfiglist(nfsl_config_t **, boolean_t);
750Sstevel@tonic-gate static nfsl_config_t *create_config(char *, char *, char *, char *, char *,
760Sstevel@tonic-gate char *, int, boolean_t, int *);
770Sstevel@tonic-gate static nfsl_config_t *create_global_raw(int *);
780Sstevel@tonic-gate static int update_config(nfsl_config_t *, char *, char *, char *,
790Sstevel@tonic-gate char *, char *, char *, int, boolean_t, boolean_t);
800Sstevel@tonic-gate static int update_field(char **, char *, char *, boolean_t *);
810Sstevel@tonic-gate static nfsl_config_t *findconfig(nfsl_config_t **, char *, boolean_t,
820Sstevel@tonic-gate nfsl_config_t **);
830Sstevel@tonic-gate static nfsl_config_t *getlastconfig(nfsl_config_t *);
840Sstevel@tonic-gate static void complete_with_global(char **, char **, char **, char **,
850Sstevel@tonic-gate char **, int *);
860Sstevel@tonic-gate #ifdef DEBUG
870Sstevel@tonic-gate static void remove_config(nfsl_config_t **, nfsl_config_t *, nfsl_config_t **);
880Sstevel@tonic-gate void nfsl_printconfig(nfsl_config_t *);
890Sstevel@tonic-gate #endif /* DEBUG */
90*13093SRoger.Faulkner@Oracle.COM static char *gataline(FILE *, char *, char *, int);
910Sstevel@tonic-gate static int get_info(char *, char **, char **, char **, char **, char **,
920Sstevel@tonic-gate char **, int *);
930Sstevel@tonic-gate static void free_config(nfsl_config_t *);
940Sstevel@tonic-gate static int is_legal_tag(char *);
950Sstevel@tonic-gate static boolean_t is_complete_config(char *, char *, char *, char *);
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * Read the configuration file and create a list of configuration
990Sstevel@tonic-gate * parameters. Returns zero for success or an errno value.
1000Sstevel@tonic-gate * The caller is responsible for freeing the returned configlist by calling
1010Sstevel@tonic-gate * nfsl_freeconfig_list().
1020Sstevel@tonic-gate *
1030Sstevel@tonic-gate * If the configuration file does not exist, *listpp points to a config entry
1040Sstevel@tonic-gate * containing the hardwired defaults.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate int
nfsl_getconfig_list(nfsl_config_t ** listpp)1070Sstevel@tonic-gate nfsl_getconfig_list(nfsl_config_t **listpp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate int error = 0;
1100Sstevel@tonic-gate char *locale;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Set the locale correctly so that we can correctly identify
1140Sstevel@tonic-gate * alphabetic characters.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate if ((locale = getenv("LC_ALL")) != NULL)
1170Sstevel@tonic-gate (void) setlocale(LC_ALL, locale);
1180Sstevel@tonic-gate else if ((locale = getenv("LC_CTYPE")) != NULL)
1190Sstevel@tonic-gate (void) setlocale(LC_CTYPE, locale);
1200Sstevel@tonic-gate else if ((locale = getenv("LANG")) != NULL)
1210Sstevel@tonic-gate (void) setlocale(LC_CTYPE, locale);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * Allocate 'global_raw' structure, its contents are
1250Sstevel@tonic-gate * indirectly allocated by create_config().
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate assert(global_raw == NULL);
1280Sstevel@tonic-gate global_raw = create_global_raw(&error);
1290Sstevel@tonic-gate if (global_raw == NULL)
1300Sstevel@tonic-gate return (error);
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * Build global entry with hardwired defaults first.
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate assert(global == NULL);
1360Sstevel@tonic-gate global = create_config(DEFAULTTAG, DEFAULTDIR, BUFFERPATH, NULL,
1370Sstevel@tonic-gate FHPATH, LOGPATH, TRANSLOG_BASIC, B_TRUE, &error);
1380Sstevel@tonic-gate *listpp = global;
1390Sstevel@tonic-gate if (global == NULL) {
1400Sstevel@tonic-gate free_config(global_raw);
1410Sstevel@tonic-gate return (error);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (error = getconfiglist(listpp, B_FALSE))
1450Sstevel@tonic-gate nfsl_freeconfig_list(listpp);
1460Sstevel@tonic-gate else {
1470Sstevel@tonic-gate assert(global != NULL);
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * The global entry was replaced with the one in the file,
1500Sstevel@tonic-gate * clear the UPDATED flag
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate global->nc_flags &= ~NC_UPDATED;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate return (error);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * Allocates memory for the 'global_raw' structure.
1590Sstevel@tonic-gate * The actual allocation of values for its components happens in
1600Sstevel@tonic-gate * update_config().
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate static nfsl_config_t *
create_global_raw(int * error)1630Sstevel@tonic-gate create_global_raw(int *error)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate nfsl_config_t *p;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate *error = 0;
1680Sstevel@tonic-gate if (p = (nfsl_config_t *)malloc(sizeof (*p)))
1690Sstevel@tonic-gate (void) memset((void *)p, 0, sizeof (*p));
1700Sstevel@tonic-gate else
1710Sstevel@tonic-gate *error = ENOMEM;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate return (p);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Checks if the the configuration file has been modified since we last
1780Sstevel@tonic-gate * read it, if not simply returns, otherwise it re-reads it adding new
1790Sstevel@tonic-gate * configuration entries. Note that existing entries that no longer
1800Sstevel@tonic-gate * exist in the configuration file are not removed. Existing entries
1810Sstevel@tonic-gate * that are modified in the configuration file are updated in the list
1820Sstevel@tonic-gate * as well.
1830Sstevel@tonic-gate * if 'updated' is defined then it is set to TRUE if the list was modified.
1840Sstevel@tonic-gate *
1850Sstevel@tonic-gate * Note that if an error occurs, the list may be corrupted.
1860Sstevel@tonic-gate * It is the responsibility of the caller to free the list.
1870Sstevel@tonic-gate * If the configuration file does not exist, we simply return the list
1880Sstevel@tonic-gate * that we previously had, log a message and return success.
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate int
nfsl_checkconfig_list(nfsl_config_t ** listpp,boolean_t * updated)1910Sstevel@tonic-gate nfsl_checkconfig_list(nfsl_config_t **listpp, boolean_t *updated)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate struct stat st;
1940Sstevel@tonic-gate int error = 0;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if (updated != NULL)
1970Sstevel@tonic-gate *updated = B_FALSE;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate if (stat(NFSL_CONFIG_FILE_PATH, &st) == -1) {
2000Sstevel@tonic-gate error = errno;
2010Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
2020Sstevel@tonic-gate syslog(LOG_ERR, gettext(
2030Sstevel@tonic-gate "Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
2040Sstevel@tonic-gate strerror(error));
2050Sstevel@tonic-gate } else {
2060Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2070Sstevel@tonic-gate "Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
2080Sstevel@tonic-gate strerror(error));
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (config_last_modification.tv_sec == st.st_mtim.tv_sec &&
2140Sstevel@tonic-gate config_last_modification.tv_nsec == st.st_mtim.tv_nsec)
2150Sstevel@tonic-gate return (0);
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (updated != NULL)
2180Sstevel@tonic-gate *updated = B_TRUE;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate return (getconfiglist(listpp, B_TRUE));
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate * Does the real work. Reads the configuration file and creates the
2250Sstevel@tonic-gate * list of entries. Assumes that *listpp contains at least one entry.
2260Sstevel@tonic-gate * The caller is responsible for freeing any config entries added to
2270Sstevel@tonic-gate * the list whether this routine returns an error or not.
2280Sstevel@tonic-gate *
2290Sstevel@tonic-gate * Returns 0 on success and updates the '*listpp' config list,
2300Sstevel@tonic-gate * Returns non-zero error value otherwise.
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate static int
getconfiglist(nfsl_config_t ** listpp,boolean_t updating)2330Sstevel@tonic-gate getconfiglist(nfsl_config_t **listpp, boolean_t updating)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate FILE *fp;
2360Sstevel@tonic-gate int error = 0;
2370Sstevel@tonic-gate nfsl_config_t *listp = NULL, *tail = NULL;
2380Sstevel@tonic-gate char linebuf[MAX_LINESZ];
2390Sstevel@tonic-gate char errorbuf[ERROR_BUFSZ];
2400Sstevel@tonic-gate char *tag, *defaultdir, *bufferpath, *rpclogpath, *fhpath, *logpath;
2410Sstevel@tonic-gate int logformat;
2420Sstevel@tonic-gate flock_t flock;
2430Sstevel@tonic-gate struct stat st;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate fp = fopen(NFSL_CONFIG_FILE_PATH, "r");
2460Sstevel@tonic-gate if (fp == NULL) {
2470Sstevel@tonic-gate if (updating) {
2480Sstevel@tonic-gate (void) sprintf(errorbuf, "Can't open %s",
2490Sstevel@tonic-gate NFSL_CONFIG_FILE_PATH);
2500Sstevel@tonic-gate } else {
2510Sstevel@tonic-gate (void) sprintf(errorbuf,
2520Sstevel@tonic-gate "Can't open %s - using hardwired defaults",
2530Sstevel@tonic-gate NFSL_CONFIG_FILE_PATH);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate * Use hardwired config.
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate if (nfsl_errs_to_syslog)
2600Sstevel@tonic-gate syslog(LOG_ERR, gettext("%s"), errorbuf);
2610Sstevel@tonic-gate else
2620Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s\n"), errorbuf);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate return (0);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate (void) memset((void *) &flock, 0, sizeof (flock));
2680Sstevel@tonic-gate flock.l_type = F_RDLCK;
2690Sstevel@tonic-gate if (fcntl(fileno(fp), F_SETLKW, &flock) == -1) {
2700Sstevel@tonic-gate error = errno;
2710Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
2720Sstevel@tonic-gate syslog(LOG_ERR, gettext(
2730Sstevel@tonic-gate "Can't lock %s - %s"), NFSL_CONFIG_FILE_PATH,
2740Sstevel@tonic-gate strerror(error));
2750Sstevel@tonic-gate } else {
2760Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2770Sstevel@tonic-gate "Can't lock %s - %s\n"), NFSL_CONFIG_FILE_PATH,
2780Sstevel@tonic-gate strerror(error));
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate goto done;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate assert (*listpp != NULL);
2840Sstevel@tonic-gate tail = getlastconfig(*listpp);
2850Sstevel@tonic-gate
286*13093SRoger.Faulkner@Oracle.COM while (gataline(fp, NFSL_CONFIG_FILE_PATH, linebuf, sizeof (linebuf))) {
2870Sstevel@tonic-gate if (linebuf[0] == '\0') {
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * ignore lines that exceed max size
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate continue;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate if (error = get_info(linebuf, &tag, &defaultdir, &bufferpath,
2950Sstevel@tonic-gate &rpclogpath, &fhpath, &logpath, &logformat))
2960Sstevel@tonic-gate break;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate if (listp = findconfig(listpp, tag, B_FALSE, &tail)) {
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * An entry with the same tag name exists,
3010Sstevel@tonic-gate * update the fields that changed.
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate error = update_config(listp, tag, defaultdir,
3040Sstevel@tonic-gate bufferpath, rpclogpath, fhpath, logpath,
3050Sstevel@tonic-gate logformat, B_TRUE, B_TRUE);
3060Sstevel@tonic-gate if (error)
3070Sstevel@tonic-gate break;
3080Sstevel@tonic-gate } else {
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * New entry, create it.
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate listp = create_config(tag, defaultdir,
3130Sstevel@tonic-gate bufferpath, rpclogpath, fhpath,
3140Sstevel@tonic-gate logpath, logformat, B_TRUE, &error);
3150Sstevel@tonic-gate if (listp == NULL)
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate if (*listpp == NULL)
3190Sstevel@tonic-gate *listpp = listp;
3200Sstevel@tonic-gate else
3210Sstevel@tonic-gate tail->nc_next = listp;
3220Sstevel@tonic-gate tail = listp;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate assert(global != NULL);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate if (!error) {
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * Get mtime while we have file locked
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate if (error = fstat(fileno(fp), &st)) {
3330Sstevel@tonic-gate error = errno;
3340Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
3350Sstevel@tonic-gate syslog(LOG_ERR, gettext(
3360Sstevel@tonic-gate "Can't stat %s - %s"), NFSL_CONFIG_FILE_PATH,
3370Sstevel@tonic-gate strerror(error));
3380Sstevel@tonic-gate } else {
3390Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3400Sstevel@tonic-gate "Can't stat %s - %s\n"), NFSL_CONFIG_FILE_PATH,
3410Sstevel@tonic-gate strerror(error));
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate config_last_modification = st.st_mtim;
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate done:
3480Sstevel@tonic-gate (void) fclose(fp);
3490Sstevel@tonic-gate return (error);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * Creates the config structure with the values specified by the
3540Sstevel@tonic-gate * parameters. If defaultdir has been specified, all relative paths
3550Sstevel@tonic-gate * are prepended with this defaultdir.
3560Sstevel@tonic-gate * If 'complete' is set then this must represent a complete config entry
3570Sstevel@tonic-gate * as specified by is_complete_config(), otherwise no work is perfomed, and
3580Sstevel@tonic-gate * NULL is returned.
3590Sstevel@tonic-gate *
3600Sstevel@tonic-gate * Returns the newly created config structure on success.
3610Sstevel@tonic-gate * Returns NULL on failure and sets error to the appropriate error.
3620Sstevel@tonic-gate */
3630Sstevel@tonic-gate static nfsl_config_t *
create_config(char * tag,char * defaultdir,char * bufferpath,char * rpclogpath,char * fhpath,char * logpath,int logformat,boolean_t complete,int * error)3640Sstevel@tonic-gate create_config(
3650Sstevel@tonic-gate char *tag,
3660Sstevel@tonic-gate char *defaultdir,
3670Sstevel@tonic-gate char *bufferpath,
3680Sstevel@tonic-gate char *rpclogpath,
3690Sstevel@tonic-gate char *fhpath,
3700Sstevel@tonic-gate char *logpath,
3710Sstevel@tonic-gate int logformat,
3720Sstevel@tonic-gate boolean_t complete,
3730Sstevel@tonic-gate int *error)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate nfsl_config_t *config;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate if ((config = (nfsl_config_t *)malloc(sizeof (*config))) == NULL) {
3780Sstevel@tonic-gate *error = ENOMEM;
3790Sstevel@tonic-gate return (NULL);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate (void) memset((void *)config, 0, sizeof (*config));
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate *error = update_config(config, tag, defaultdir, bufferpath, rpclogpath,
3840Sstevel@tonic-gate fhpath, logpath, logformat, complete, B_TRUE);
3850Sstevel@tonic-gate if (*error) {
3860Sstevel@tonic-gate free(config);
3870Sstevel@tonic-gate return (NULL);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate config->nc_flags &= ~NC_UPDATED; /* This is a new entry */
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate return (config);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate * Updates the configuration entry with the new information provided,
3980Sstevel@tonic-gate * sets NC_UPDATED to indicate so. The entry is left untouched if all
3990Sstevel@tonic-gate * the fields are the same (except for 'nc_rpccookie', 'nc_transcookie'
4000Sstevel@tonic-gate * and 'nc_next').
4010Sstevel@tonic-gate * Prepends each path component with 'defauldir' if 'prepend' is set.
4020Sstevel@tonic-gate *
4030Sstevel@tonic-gate * Returns 0 on success, error otherwise.
4040Sstevel@tonic-gate * On error, the config entry is left in an inconsistent state.
4050Sstevel@tonic-gate * The only thing the caller can really do with it is free it.
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate static int
update_config(nfsl_config_t * config,char * tag,char * defaultdir,char * bufferpath,char * rpclogpath,char * fhpath,char * logpath,int logformat,boolean_t complete,boolean_t prepend)4080Sstevel@tonic-gate update_config(
4090Sstevel@tonic-gate nfsl_config_t *config,
4100Sstevel@tonic-gate char *tag,
4110Sstevel@tonic-gate char *defaultdir,
4120Sstevel@tonic-gate char *bufferpath,
4130Sstevel@tonic-gate char *rpclogpath,
4140Sstevel@tonic-gate char *fhpath,
4150Sstevel@tonic-gate char *logpath,
4160Sstevel@tonic-gate int logformat,
4170Sstevel@tonic-gate boolean_t complete,
4180Sstevel@tonic-gate boolean_t prepend)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate boolean_t updated, config_updated = B_FALSE;
4210Sstevel@tonic-gate int error = 0;
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate if (complete && !is_complete_config(tag, bufferpath, fhpath, logpath)) {
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * Not a complete entry
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
4280Sstevel@tonic-gate syslog(LOG_ERR, gettext(
4290Sstevel@tonic-gate "update_config: \"%s\" not a complete config entry."),
4300Sstevel@tonic-gate tag);
4310Sstevel@tonic-gate } else {
4320Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4330Sstevel@tonic-gate "update_config: \"%s\" not a complete config entry.\n"),
4340Sstevel@tonic-gate tag);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate return (EINVAL);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate assert(tag != NULL);
4400Sstevel@tonic-gate if (config->nc_name == NULL) {
4410Sstevel@tonic-gate /*
4420Sstevel@tonic-gate * New entry
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate if ((config->nc_name = strdup(tag)) == NULL) {
4450Sstevel@tonic-gate error = ENOMEM;
4460Sstevel@tonic-gate goto errout;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate } else
4490Sstevel@tonic-gate assert(strcmp(config->nc_name, tag) == 0);
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate if (error = update_field(
4520Sstevel@tonic-gate &config->nc_defaultdir, defaultdir, NULL, &updated))
4530Sstevel@tonic-gate goto errout;
4540Sstevel@tonic-gate if (!prepend) {
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * Do not prepend default directory.
4570Sstevel@tonic-gate */
4580Sstevel@tonic-gate defaultdir = NULL;
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate config_updated |= updated;
4610Sstevel@tonic-gate if (error = update_field(
4620Sstevel@tonic-gate &config->nc_bufferpath, bufferpath, defaultdir, &updated))
4630Sstevel@tonic-gate goto errout;
4640Sstevel@tonic-gate config_updated |= updated;
4650Sstevel@tonic-gate if (error = update_field(
4660Sstevel@tonic-gate &config->nc_rpclogpath, rpclogpath, defaultdir, &updated))
4670Sstevel@tonic-gate goto errout;
4680Sstevel@tonic-gate config_updated |= updated;
4690Sstevel@tonic-gate if (error = update_field(
4700Sstevel@tonic-gate &config->nc_fhpath, fhpath, defaultdir, &updated))
4710Sstevel@tonic-gate goto errout;
4720Sstevel@tonic-gate config_updated |= updated;
4730Sstevel@tonic-gate if (error = update_field(
4740Sstevel@tonic-gate &config->nc_logpath, logpath, defaultdir, &updated))
4750Sstevel@tonic-gate goto errout;
4760Sstevel@tonic-gate config_updated |= updated;
4770Sstevel@tonic-gate updated = (config->nc_logformat != logformat);
4780Sstevel@tonic-gate if (updated)
4790Sstevel@tonic-gate config->nc_logformat = logformat;
4800Sstevel@tonic-gate config_updated |= updated;
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate if (config_updated)
4830Sstevel@tonic-gate config->nc_flags |= NC_UPDATED;
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate if (strcmp(tag, DEFAULTTAG) == 0) {
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * Have the default global config point to this entry.
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate global = config;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate * Update the global_raw configuration entry.
4930Sstevel@tonic-gate * Make sure no expanding of paths occurs.
4940Sstevel@tonic-gate */
4950Sstevel@tonic-gate if (error = update_config(global_raw, DEFAULTRAWTAG, defaultdir,
4960Sstevel@tonic-gate bufferpath, rpclogpath, fhpath, logpath, logformat,
4970Sstevel@tonic-gate complete, B_FALSE))
4980Sstevel@tonic-gate goto errout;
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate return (error);
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate errout:
5040Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
5050Sstevel@tonic-gate syslog(LOG_ERR, gettext(
5060Sstevel@tonic-gate "update_config: Can't process \"%s\" config entry: %s"),
5070Sstevel@tonic-gate tag, strerror(error));
5080Sstevel@tonic-gate } else {
5090Sstevel@tonic-gate (void) fprintf(stderr, gettext(
5100Sstevel@tonic-gate "update_config: Can't process \"%s\" config entry: %s\n"),
5110Sstevel@tonic-gate tag, strerror(error));
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate return (error);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate * Prepends 'prependir' to 'new' if 'prependir' is defined.
5180Sstevel@tonic-gate * Compares the value of '*old' with 'new', if it has changed,
5190Sstevel@tonic-gate * then sets whatever 'old' references equal to 'new'.
5200Sstevel@tonic-gate * Returns 0 on success, error otherwise.
5210Sstevel@tonic-gate * Sets '*updated' to B_TRUE if field was modified.
5220Sstevel@tonic-gate * The value of '*updated' is undefined on error.
5230Sstevel@tonic-gate */
5240Sstevel@tonic-gate static int
update_field(char ** old,char * new,char * prependdir,boolean_t * updated)5250Sstevel@tonic-gate update_field(
5260Sstevel@tonic-gate char **old, /* pointer to config field */
5270Sstevel@tonic-gate char *new, /* updated value */
5280Sstevel@tonic-gate char *prependdir, /* prepend this directory to new */
5290Sstevel@tonic-gate boolean_t *updated) /* field was modified */
5300Sstevel@tonic-gate {
5310Sstevel@tonic-gate char *tmp_new = NULL;
5320Sstevel@tonic-gate int need_update = 0;
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate if (new != NULL) {
5350Sstevel@tonic-gate if (prependdir != NULL && new[0] != '/') {
5360Sstevel@tonic-gate tmp_new = malloc(strlen(prependdir) + strlen(new) + 2);
5370Sstevel@tonic-gate if (tmp_new == NULL)
5380Sstevel@tonic-gate return (ENOMEM);
5390Sstevel@tonic-gate (void) sprintf(tmp_new, "%s/%s", prependdir, new);
5400Sstevel@tonic-gate } else {
5410Sstevel@tonic-gate if ((tmp_new = strdup(new)) == NULL)
5420Sstevel@tonic-gate return (ENOMEM);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate if (tmp_new != NULL) {
5470Sstevel@tonic-gate if (*old == NULL)
5480Sstevel@tonic-gate need_update++;
5490Sstevel@tonic-gate else if (strcmp(tmp_new, *old) != 0) {
5500Sstevel@tonic-gate free(*old);
5510Sstevel@tonic-gate need_update++;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate if (need_update)
5540Sstevel@tonic-gate *old = tmp_new;
5550Sstevel@tonic-gate } else if (*old != NULL) {
5560Sstevel@tonic-gate need_update++;
5570Sstevel@tonic-gate free(*old);
5580Sstevel@tonic-gate *old = NULL;
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate *updated = need_update != 0;
5620Sstevel@tonic-gate return (0);
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate #ifdef DEBUG
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate * Removes and frees the 'config' entry from the list
5680Sstevel@tonic-gate * pointed to by '*listpp'.
5690Sstevel@tonic-gate * No error is reported if the entry does not exist.
5700Sstevel@tonic-gate * Updates '*tail' to point to the last item in the list.
5710Sstevel@tonic-gate */
5720Sstevel@tonic-gate static void
remove_config(nfsl_config_t ** listpp,nfsl_config_t * config,nfsl_config_t ** tail)5730Sstevel@tonic-gate remove_config(
5740Sstevel@tonic-gate nfsl_config_t **listpp,
5750Sstevel@tonic-gate nfsl_config_t *config,
5760Sstevel@tonic-gate nfsl_config_t **tail)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate nfsl_config_t *p, *prev;
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate prev = *listpp;
5810Sstevel@tonic-gate for (p = *listpp; p != NULL; p = p->nc_next) {
5820Sstevel@tonic-gate if (p == config) {
5830Sstevel@tonic-gate if (p == prev) {
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate * first element of the list
5860Sstevel@tonic-gate */
5870Sstevel@tonic-gate *listpp = prev->nc_next;
5880Sstevel@tonic-gate } else
5890Sstevel@tonic-gate prev->nc_next = p->nc_next;
5900Sstevel@tonic-gate free_config(p);
5910Sstevel@tonic-gate break;
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate prev = p;
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate * Find tail of the list.
5980Sstevel@tonic-gate */
5990Sstevel@tonic-gate for (*tail = prev; (*tail)->nc_next != NULL; *tail = (*tail)->nc_next)
6000Sstevel@tonic-gate ;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate #endif /* DEBUG */
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate static void
free_config(nfsl_config_t * config)6050Sstevel@tonic-gate free_config(nfsl_config_t *config)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate if (config == NULL)
6080Sstevel@tonic-gate return;
6090Sstevel@tonic-gate if (config->nc_name)
6100Sstevel@tonic-gate free(config->nc_name);
6110Sstevel@tonic-gate if (config->nc_defaultdir)
6120Sstevel@tonic-gate free(config->nc_defaultdir);
6130Sstevel@tonic-gate if (config->nc_bufferpath)
6140Sstevel@tonic-gate free(config->nc_bufferpath);
6150Sstevel@tonic-gate if (config->nc_rpclogpath)
6160Sstevel@tonic-gate free(config->nc_rpclogpath);
6170Sstevel@tonic-gate if (config->nc_fhpath)
6180Sstevel@tonic-gate free(config->nc_fhpath);
6190Sstevel@tonic-gate if (config->nc_logpath)
6200Sstevel@tonic-gate free(config->nc_logpath);
6210Sstevel@tonic-gate if (config == global)
6220Sstevel@tonic-gate global = NULL;
6230Sstevel@tonic-gate if (config == global_raw)
6240Sstevel@tonic-gate global_raw = NULL;
6250Sstevel@tonic-gate free(config);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate void
nfsl_freeconfig_list(nfsl_config_t ** listpp)6290Sstevel@tonic-gate nfsl_freeconfig_list(nfsl_config_t **listpp)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate nfsl_config_t *next;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (*listpp == NULL)
6340Sstevel@tonic-gate return;
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate do {
6370Sstevel@tonic-gate next = (*listpp)->nc_next;
6380Sstevel@tonic-gate free_config(*listpp);
6390Sstevel@tonic-gate *listpp = next;
6400Sstevel@tonic-gate } while (*listpp);
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate free_config(global_raw);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate * Returns a pointer to the first instance of 'tag' in the list.
6470Sstevel@tonic-gate * If 'remove' is true, then the entry is removed from the list and
6480Sstevel@tonic-gate * a pointer to it is returned.
6490Sstevel@tonic-gate * If '*tail' is not NULL, then it will point to the last element of
6500Sstevel@tonic-gate * the list. Note that this function assumes that *tail already
6510Sstevel@tonic-gate * points at the last element of the list.
6520Sstevel@tonic-gate * Returns NULL if the entry does not exist.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate static nfsl_config_t *
findconfig(nfsl_config_t ** listpp,char * tag,boolean_t remove,nfsl_config_t ** tail)6550Sstevel@tonic-gate findconfig(
6560Sstevel@tonic-gate nfsl_config_t **listpp,
6570Sstevel@tonic-gate char *tag, boolean_t remove,
6580Sstevel@tonic-gate nfsl_config_t **tail)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate nfsl_config_t *p, *prev;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate prev = *listpp;
6630Sstevel@tonic-gate for (p = *listpp; p != NULL; p = p->nc_next) {
6640Sstevel@tonic-gate if (strcmp(p->nc_name, tag) == 0) {
6650Sstevel@tonic-gate if (remove) {
6660Sstevel@tonic-gate if (p == prev) {
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate * first element of the list
6690Sstevel@tonic-gate */
6700Sstevel@tonic-gate *listpp = prev->nc_next;
6710Sstevel@tonic-gate } else
6720Sstevel@tonic-gate prev->nc_next = p->nc_next;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate if (tail != NULL && p == *tail) {
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate * Only update *tail if we removed
6770Sstevel@tonic-gate * the last element of the list, and we
6780Sstevel@tonic-gate * requested *tail to be updated.
6790Sstevel@tonic-gate */
6800Sstevel@tonic-gate *tail = prev;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate return (p);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate prev = p;
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate return (NULL);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate static nfsl_config_t *
getlastconfig(nfsl_config_t * listp)6920Sstevel@tonic-gate getlastconfig(nfsl_config_t *listp)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate nfsl_config_t *lastp = NULL;
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate for (; listp != NULL; listp = listp->nc_next)
6970Sstevel@tonic-gate lastp = listp;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate return (lastp);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate * Returns a pointer to the first instance of 'tag' in the list.
7040Sstevel@tonic-gate * Returns NULL if the entry does not exist.
7050Sstevel@tonic-gate * Sets 'error' if the update of the list failed if necessary, and
7060Sstevel@tonic-gate * returns NULL.
7070Sstevel@tonic-gate */
7080Sstevel@tonic-gate nfsl_config_t *
nfsl_findconfig(nfsl_config_t * listp,char * tag,int * error)7090Sstevel@tonic-gate nfsl_findconfig(nfsl_config_t *listp, char *tag, int *error)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate nfsl_config_t *config;
7120Sstevel@tonic-gate boolean_t updated;
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate *error = 0;
7150Sstevel@tonic-gate config = findconfig(&listp, tag, B_FALSE, (nfsl_config_t **)NULL);
7160Sstevel@tonic-gate if (config == NULL) {
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate * Rebuild our list if the file has changed.
7190Sstevel@tonic-gate */
7200Sstevel@tonic-gate if (*error = nfsl_checkconfig_list(&listp, &updated)) {
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate * List may be corrupted, notify caller.
7230Sstevel@tonic-gate */
7240Sstevel@tonic-gate return (NULL);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate if (updated) {
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate * Search for tag again.
7290Sstevel@tonic-gate */
7300Sstevel@tonic-gate config = findconfig(&listp, tag, B_FALSE,
7310Sstevel@tonic-gate (nfsl_config_t **)NULL);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate return (config);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate * Use the raw global values if any of the parameters is not defined.
7400Sstevel@tonic-gate */
7410Sstevel@tonic-gate static void
complete_with_global(char ** defaultdir,char ** bufferpath,char ** rpclogpath,char ** fhpath,char ** logpath,int * logformat)7420Sstevel@tonic-gate complete_with_global(
7430Sstevel@tonic-gate char **defaultdir,
7440Sstevel@tonic-gate char **bufferpath,
7450Sstevel@tonic-gate char **rpclogpath,
7460Sstevel@tonic-gate char **fhpath,
7470Sstevel@tonic-gate char **logpath,
7480Sstevel@tonic-gate int *logformat)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate if (*defaultdir == NULL)
7510Sstevel@tonic-gate *defaultdir = global_raw->nc_defaultdir;
7520Sstevel@tonic-gate if (*bufferpath == NULL)
7530Sstevel@tonic-gate *bufferpath = global_raw->nc_bufferpath;
7540Sstevel@tonic-gate if (*rpclogpath == NULL)
7550Sstevel@tonic-gate *rpclogpath = global_raw->nc_rpclogpath;
7560Sstevel@tonic-gate if (*fhpath == NULL)
7570Sstevel@tonic-gate *fhpath = global_raw->nc_fhpath;
7580Sstevel@tonic-gate if (*logpath == NULL)
7590Sstevel@tonic-gate *logpath = global_raw->nc_logpath;
7600Sstevel@tonic-gate if (*logformat == 0)
7610Sstevel@tonic-gate *logformat = global_raw->nc_logformat;
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate * Parses 'linebuf'. Returns 0 if a valid tag is found, otherwise non-zero.
7660Sstevel@tonic-gate * Unknown tokens are silently ignored.
7670Sstevel@tonic-gate * It is the responsibility of the caller to make a copy of the non-NULL
7680Sstevel@tonic-gate * parameters if they need to be used before linebuf is freed.
7690Sstevel@tonic-gate */
7700Sstevel@tonic-gate static int
get_info(char * linebuf,char ** tag,char ** defaultdir,char ** bufferpath,char ** rpclogpath,char ** fhpath,char ** logpath,int * logformat)7710Sstevel@tonic-gate get_info(
7720Sstevel@tonic-gate char *linebuf,
7730Sstevel@tonic-gate char **tag,
7740Sstevel@tonic-gate char **defaultdir,
7750Sstevel@tonic-gate char **bufferpath,
7760Sstevel@tonic-gate char **rpclogpath,
7770Sstevel@tonic-gate char **fhpath,
7780Sstevel@tonic-gate char **logpath,
7790Sstevel@tonic-gate int *logformat)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate char *tok;
7820Sstevel@tonic-gate char *tmp;
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate /* tag */
7850Sstevel@tonic-gate *tag = NULL;
7860Sstevel@tonic-gate tok = strtok(linebuf, whitespace);
7870Sstevel@tonic-gate if (tok == NULL)
7880Sstevel@tonic-gate goto badtag;
7890Sstevel@tonic-gate if (!is_legal_tag(tok))
7900Sstevel@tonic-gate goto badtag;
7910Sstevel@tonic-gate *tag = tok;
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate *defaultdir = *bufferpath = *rpclogpath = NULL;
7940Sstevel@tonic-gate *fhpath = *logpath = NULL;
7950Sstevel@tonic-gate *logformat = 0;
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate while (tok = strtok(NULL, whitespace)) {
7980Sstevel@tonic-gate if (strncmp(tok, "defaultdir=", strlen("defaultdir=")) == 0) {
7990Sstevel@tonic-gate *defaultdir = tok + strlen("defaultdir=");
8000Sstevel@tonic-gate } else if (strncmp(tok, "buffer=", strlen("buffer=")) == 0) {
8010Sstevel@tonic-gate *bufferpath = tok + strlen("buffer=");
8020Sstevel@tonic-gate } else if (strncmp(tok, "rpclog=", strlen("rpclog=")) == 0) {
8030Sstevel@tonic-gate *rpclogpath = tok + strlen("rpclog=");
8040Sstevel@tonic-gate } else if (strncmp(tok, "fhtable=", strlen("fhtable=")) == 0) {
8050Sstevel@tonic-gate *fhpath = tok + strlen("fhtable=");
8060Sstevel@tonic-gate } else if (strncmp(tok, "log=", strlen("log=")) == 0) {
8070Sstevel@tonic-gate *logpath = tok + strlen("log=");
8080Sstevel@tonic-gate } else if (strncmp(tok, "logformat=",
8090Sstevel@tonic-gate strlen("logformat=")) == 0) {
8100Sstevel@tonic-gate tmp = tok + strlen("logformat=");
8110Sstevel@tonic-gate if (strncmp(tmp, "extended", strlen("extended")) == 0) {
8120Sstevel@tonic-gate *logformat = TRANSLOG_EXTENDED;
8130Sstevel@tonic-gate } else {
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate * Use transaction log basic format if
8160Sstevel@tonic-gate * 'extended' was not specified.
8170Sstevel@tonic-gate */
8180Sstevel@tonic-gate *logformat = TRANSLOG_BASIC;
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate if (strcmp(*tag, DEFAULTTAG) != 0) {
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate * Use global values for fields not specified if
8260Sstevel@tonic-gate * this tag is not the global tag.
8270Sstevel@tonic-gate */
8280Sstevel@tonic-gate complete_with_global(defaultdir, bufferpath,
8290Sstevel@tonic-gate rpclogpath, fhpath, logpath, logformat);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate return (0);
8330Sstevel@tonic-gate
8340Sstevel@tonic-gate badtag:
8350Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
8360Sstevel@tonic-gate syslog(LOG_ERR, gettext(
8370Sstevel@tonic-gate "Bad tag found in config file."));
8380Sstevel@tonic-gate } else {
8390Sstevel@tonic-gate (void) fprintf(stderr, gettext(
8400Sstevel@tonic-gate "Bad tag found in config file.\n"));
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate return (-1);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate * Returns True if we have all the elements of a complete configuration
8470Sstevel@tonic-gate * entry. A complete configuration has tag, bufferpath, fhpath and logpath
8480Sstevel@tonic-gate * defined to non-zero strings.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate static boolean_t
is_complete_config(char * tag,char * bufferpath,char * fhpath,char * logpath)8510Sstevel@tonic-gate is_complete_config(
8520Sstevel@tonic-gate char *tag,
8530Sstevel@tonic-gate char *bufferpath,
8540Sstevel@tonic-gate char *fhpath,
8550Sstevel@tonic-gate char *logpath)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate assert(tag != NULL);
8580Sstevel@tonic-gate assert(strlen(tag) > 0);
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate if ((bufferpath != NULL && strlen(bufferpath) > 0) &&
8610Sstevel@tonic-gate (fhpath != NULL && strlen(fhpath) > 0) &&
8620Sstevel@tonic-gate (logpath != NULL && strlen(logpath) > 0))
8630Sstevel@tonic-gate return (B_TRUE);
8640Sstevel@tonic-gate return (B_FALSE);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate #ifdef DEBUG
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate * Prints the configuration entry to stdout.
8700Sstevel@tonic-gate */
8710Sstevel@tonic-gate void
nfsl_printconfig(nfsl_config_t * config)8720Sstevel@tonic-gate nfsl_printconfig(nfsl_config_t *config)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate if (config->nc_name)
8750Sstevel@tonic-gate (void) printf("tag=%s\t", config->nc_name);
8760Sstevel@tonic-gate if (config->nc_defaultdir)
8770Sstevel@tonic-gate (void) printf("defaultdir=%s\t", config->nc_defaultdir);
8780Sstevel@tonic-gate if (config->nc_logpath)
8790Sstevel@tonic-gate (void) printf("logpath=%s\t", config->nc_logpath);
8800Sstevel@tonic-gate if (config->nc_fhpath)
8810Sstevel@tonic-gate (void) printf("fhpath=%s\t", config->nc_fhpath);
8820Sstevel@tonic-gate if (config->nc_bufferpath)
8830Sstevel@tonic-gate (void) printf("bufpath=%s\t", config->nc_bufferpath);
8840Sstevel@tonic-gate if (config->nc_rpclogpath)
8850Sstevel@tonic-gate (void) printf("rpclogpath=%s\t", config->nc_rpclogpath);
8860Sstevel@tonic-gate if (config->nc_logformat == TRANSLOG_BASIC)
8870Sstevel@tonic-gate (void) printf("logformat=basic");
8880Sstevel@tonic-gate else if (config->nc_logformat == TRANSLOG_EXTENDED)
8890Sstevel@tonic-gate (void) printf("logformat=extended");
8900Sstevel@tonic-gate else
8910Sstevel@tonic-gate (void) printf("config->nc_logformat=UNKNOWN");
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate if (config->nc_flags & NC_UPDATED)
8940Sstevel@tonic-gate (void) printf("\tflags=NC_UPDATED");
8950Sstevel@tonic-gate (void) printf("\n");
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate /*
8990Sstevel@tonic-gate * Prints the configuration list to stdout.
9000Sstevel@tonic-gate */
9010Sstevel@tonic-gate void
nfsl_printconfig_list(nfsl_config_t * listp)9020Sstevel@tonic-gate nfsl_printconfig_list(nfsl_config_t *listp)
9030Sstevel@tonic-gate {
9040Sstevel@tonic-gate for (; listp != NULL; listp = listp->nc_next) {
9050Sstevel@tonic-gate nfsl_printconfig(listp);
9060Sstevel@tonic-gate (void) printf("\n");
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate #endif /* DEBUG */
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate /*
9120Sstevel@tonic-gate * Returns non-zero if the given string is allowable for a tag, zero if
9130Sstevel@tonic-gate * not.
9140Sstevel@tonic-gate */
9150Sstevel@tonic-gate static int
is_legal_tag(char * tag)9160Sstevel@tonic-gate is_legal_tag(char *tag)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate int i;
9190Sstevel@tonic-gate int len;
9200Sstevel@tonic-gate
9210Sstevel@tonic-gate if (tag == NULL)
9220Sstevel@tonic-gate return (0);
9230Sstevel@tonic-gate len = strlen(tag);
9240Sstevel@tonic-gate if (len == 0)
9250Sstevel@tonic-gate return (0);
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate for (i = 0; i < len; i++) {
9280Sstevel@tonic-gate char c;
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate c = tag[i];
9310Sstevel@tonic-gate if (!(isalnum((unsigned char)c) || c == '_'))
9320Sstevel@tonic-gate return (0);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate return (1);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate /*
939*13093SRoger.Faulkner@Oracle.COM * gataline attempts to get a line from the configuration file,
9400Sstevel@tonic-gate * upto LINESZ. A line in the file is a concatenation of lines if the
9410Sstevel@tonic-gate * continuation symbol '\' is used at the end of the line. Returns
9420Sstevel@tonic-gate * line on success, a NULL on EOF, and an empty string on lines > linesz.
9430Sstevel@tonic-gate */
9440Sstevel@tonic-gate static char *
gataline(FILE * fp,char * path,char * line,int linesz)945*13093SRoger.Faulkner@Oracle.COM gataline(FILE *fp, char *path, char *line, int linesz) {
9460Sstevel@tonic-gate register char *p = line;
9470Sstevel@tonic-gate register int len;
9480Sstevel@tonic-gate int excess = 0;
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate *p = '\0';
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate for (;;) {
9530Sstevel@tonic-gate if (fgets(p, linesz - (p-line), fp) == NULL) {
9540Sstevel@tonic-gate return (*line ? line : NULL); /* EOF */
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate len = strlen(line);
9580Sstevel@tonic-gate if (len <= 0) {
9590Sstevel@tonic-gate p = line;
9600Sstevel@tonic-gate continue;
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate p = &line[len - 1];
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate /*
9650Sstevel@tonic-gate * Is input line too long?
9660Sstevel@tonic-gate */
9670Sstevel@tonic-gate if (*p != '\n') {
9680Sstevel@tonic-gate excess = 1;
9690Sstevel@tonic-gate /*
9700Sstevel@tonic-gate * Perhaps last char read was '\'. Reinsert it
9710Sstevel@tonic-gate * into the stream to ease the parsing when we
9720Sstevel@tonic-gate * read the rest of the line to discard.
9730Sstevel@tonic-gate */
9740Sstevel@tonic-gate (void) ungetc(*p, fp);
9750Sstevel@tonic-gate break;
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate trim:
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate /* trim trailing white space */
9800Sstevel@tonic-gate while (p >= line && isspace(*(uchar_t *)p))
9810Sstevel@tonic-gate *p-- = '\0';
9820Sstevel@tonic-gate if (p < line) { /* empty line */
9830Sstevel@tonic-gate p = line;
9840Sstevel@tonic-gate continue;
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate if (*p == '\\') { /* continuation */
9880Sstevel@tonic-gate *p = '\0';
9890Sstevel@tonic-gate continue;
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate * Ignore comments. Comments start with '#'
9940Sstevel@tonic-gate * which must be preceded by a whitespace, unless
9950Sstevel@tonic-gate * '#' is the first character in the line.
9960Sstevel@tonic-gate */
9970Sstevel@tonic-gate p = line;
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate while (p = strchr(p, '#')) {
10000Sstevel@tonic-gate if (p == line || isspace(*(p-1))) {
10010Sstevel@tonic-gate *p-- = '\0';
10020Sstevel@tonic-gate goto trim;
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate p++;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate
10070Sstevel@tonic-gate break;
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate if (excess) {
10100Sstevel@tonic-gate int c;
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate /*
10130Sstevel@tonic-gate * discard rest of line and return an empty string.
10140Sstevel@tonic-gate * done to set the stream to the correct place when
10150Sstevel@tonic-gate * we are done with this line.
10160Sstevel@tonic-gate */
10170Sstevel@tonic-gate while ((c = getc(fp)) != EOF) {
10180Sstevel@tonic-gate *p = c;
10190Sstevel@tonic-gate if (*p == '\n') /* end of the long line */
10200Sstevel@tonic-gate break;
10210Sstevel@tonic-gate else if (*p == '\\') { /* continuation */
10220Sstevel@tonic-gate if (getc(fp) == EOF) /* ignore next char */
10230Sstevel@tonic-gate break;
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate if (nfsl_errs_to_syslog) {
10270Sstevel@tonic-gate syslog(LOG_ERR, gettext(
10280Sstevel@tonic-gate "%s: line too long - ignored (max %d chars)"),
10290Sstevel@tonic-gate path, linesz-1);
10300Sstevel@tonic-gate } else {
10310Sstevel@tonic-gate (void) fprintf(stderr, gettext(
10320Sstevel@tonic-gate "%s: line too long - ignored (max %d chars)\n"),
10330Sstevel@tonic-gate path, linesz-1);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate *line = '\0';
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate return (line);
10390Sstevel@tonic-gate }
1040