Blame modules/lookup_sss.c

Packit 8480eb
/* ----------------------------------------------------------------------- *
Packit 8480eb
 *
Packit 8480eb
 *  lookup_sss.c - module for Linux automount to query sss service
Packit 8480eb
 *
Packit 8480eb
 *   Copyright 2012 Ian Kent <raven@themaw.net>
Packit 8480eb
 *   Copyright 2012 Red Hat, Inc.
Packit 8480eb
 *
Packit 8480eb
 *   This program is free software; you can redistribute it and/or modify
Packit 8480eb
 *   it under the terms of the GNU General Public License as published by
Packit 8480eb
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
Packit 8480eb
 *   USA; either version 2 of the License, or (at your option) any later
Packit 8480eb
 *   version; incorporated herein by reference.
Packit 8480eb
 *
Packit 8480eb
 * ----------------------------------------------------------------------- */
Packit 8480eb
Packit 8480eb
#include <stdio.h>
Packit 8480eb
#include <malloc.h>
Packit 8480eb
#include <stdlib.h>
Packit 8480eb
#include <ctype.h>
Packit 8480eb
#include <dlfcn.h>
Packit 8480eb
#include <errno.h>
Packit 8480eb
#include <string.h>
Packit 8480eb
#include <sys/param.h>
Packit 8480eb
#include <sys/types.h>
Packit 8480eb
#include <sys/stat.h>
Packit 8480eb
Packit 8480eb
#define MODULE_LOOKUP
Packit 8480eb
#include "automount.h"
Packit 8480eb
#include "nsswitch.h"
Packit 8480eb
Packit 8480eb
#define MAPFMT_DEFAULT "sun"
Packit 8480eb
Packit Service 145c60
/* Half a second between retries */
Packit Service 145c60
#define SETAUTOMOUNTENT_MASTER_INTERVAL	500000000
Packit 8480eb
Packit 8480eb
#define MODPREFIX "lookup(sss): "
Packit 8480eb
Packit 8480eb
#define SSS_SO_NAME "libsss_autofs"
Packit 8480eb
Packit 8480eb
int _sss_setautomntent(const char *, void **);
Packit 8480eb
int _sss_getautomntent_r(char **, char **, void *);
Packit 8480eb
int _sss_getautomntbyname_r(char *, char **, void *);
Packit 8480eb
int _sss_endautomntent(void **);
Packit 8480eb
Packit 8480eb
typedef int (*setautomntent_t) (const char *, void **);
Packit 8480eb
typedef int (*getautomntent_t) (char **, char **, void *);
Packit 8480eb
typedef int (*getautomntbyname_t) (char *, char **, void *);
Packit 8480eb
typedef int (*endautomntent_t) (void **);
Packit 8480eb
Packit 8480eb
struct lookup_context {
Packit 8480eb
	const char *mapname;
Packit 8480eb
    	void *dlhandle;
Packit 8480eb
	setautomntent_t setautomntent;
Packit 8480eb
	getautomntent_t getautomntent_r;
Packit 8480eb
	getautomntbyname_t getautomntbyname_r;
Packit 8480eb
	endautomntent_t endautomntent;
Packit 8480eb
	struct parse_mod *parse;
Packit 8480eb
};
Packit 8480eb
Packit 8480eb
int lookup_version = AUTOFS_LOOKUP_VERSION;	/* Required by protocol */
Packit 8480eb
Packit 8480eb
static int open_sss_lib(struct lookup_context *ctxt)
Packit 8480eb
{
Packit 8480eb
	char dlbuf[PATH_MAX];
Packit 8480eb
	char *estr;
Packit 8480eb
	void *dh;
Packit 8480eb
	size_t size;
Packit 8480eb
Packit 8480eb
	size = snprintf(dlbuf, sizeof(dlbuf),
Packit 8480eb
			"%s/%s.so", SSS_LIB_DIR, SSS_SO_NAME);
Packit 8480eb
	if (size >= sizeof(dlbuf)) {
Packit 8480eb
		logmsg(MODPREFIX "sss library path too long");
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	dh = dlopen(dlbuf, RTLD_LAZY);
Packit 8480eb
	if (!dh)
Packit 8480eb
		return 1;
Packit 8480eb
	ctxt->dlhandle = dh;
Packit 8480eb
Packit 8480eb
	ctxt->setautomntent = (setautomntent_t) dlsym(dh, "_sss_setautomntent");
Packit 8480eb
	if (!ctxt->setautomntent)
Packit 8480eb
		goto lib_names_fail;
Packit 8480eb
Packit 8480eb
	ctxt->getautomntent_r = (getautomntent_t) dlsym(dh, "_sss_getautomntent_r");
Packit 8480eb
	if (!ctxt->getautomntent_r)
Packit 8480eb
		goto lib_names_fail;
Packit 8480eb
Packit 8480eb
	ctxt->getautomntbyname_r = (getautomntbyname_t) dlsym(dh, "_sss_getautomntbyname_r");
Packit 8480eb
	if (!ctxt->getautomntbyname_r)
Packit 8480eb
		goto lib_names_fail;
Packit 8480eb
Packit 8480eb
	ctxt->endautomntent = (endautomntent_t) dlsym(dh, "_sss_endautomntent");
Packit Service 145c60
	if (!ctxt->setautomntent)
Packit 8480eb
		goto lib_names_fail;
Packit 8480eb
Packit 8480eb
	return 0;
Packit 8480eb
Packit 8480eb
lib_names_fail:
Packit 8480eb
	if ((estr = dlerror()) == NULL)
Packit 8480eb
		logmsg(MODPREFIX "failed to locate sss library entry points");
Packit 8480eb
	else
Packit 8480eb
		logerr(MODPREFIX "dlsym: %s", estr);
Packit 8480eb
	dlclose(dh);
Packit 8480eb
Packit 8480eb
	return 1;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
static int do_init(const char *mapfmt,
Packit 8480eb
		   int argc, const char *const *argv,
Packit 8480eb
		   struct lookup_context *ctxt, unsigned int reinit)
Packit 8480eb
{
Packit 8480eb
	int ret = 0;
Packit 8480eb
Packit 8480eb
	if (argc < 1) {
Packit 8480eb
		logerr(MODPREFIX "No map name");
Packit 8480eb
		ret = 1;
Packit 8480eb
		goto out;
Packit 8480eb
	}
Packit 8480eb
	ctxt->mapname = argv[0];
Packit 8480eb
Packit 8480eb
	if (!mapfmt)
Packit 8480eb
		mapfmt = MAPFMT_DEFAULT;
Packit 8480eb
Packit 8480eb
	if (!reinit) {
Packit 8480eb
		ret = open_sss_lib(ctxt);
Packit 8480eb
		if (ret)
Packit 8480eb
			goto out;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	if (reinit) {
Packit 8480eb
		ret = reinit_parse(ctxt->parse, mapfmt, MODPREFIX, argc - 1, argv + 1);
Packit 8480eb
		if (ret)
Packit 8480eb
			logmsg(MODPREFIX "failed to reinit parse context");
Packit 8480eb
	} else {
Packit 8480eb
		ctxt->parse = open_parse(mapfmt, MODPREFIX, argc - 1, argv + 1);
Packit 8480eb
		if (!ctxt->parse) {
Packit 8480eb
			logmsg(MODPREFIX "failed to open parse context");
Packit 8480eb
			dlclose(ctxt->dlhandle);
Packit 8480eb
			ret = 1;
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
out:
Packit 8480eb
	return ret;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_init(const char *mapfmt,
Packit 8480eb
		int argc, const char *const *argv, void **context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt;
Packit 8480eb
	char buf[MAX_ERR_BUF];
Packit 8480eb
	char *estr;
Packit 8480eb
Packit 8480eb
	*context = NULL;
Packit 8480eb
Packit 8480eb
	ctxt = malloc(sizeof(struct lookup_context));
Packit 8480eb
	if (!ctxt) {
Packit 8480eb
		estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
		logerr(MODPREFIX "malloc: %s", estr);
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	if (do_init(mapfmt, argc, argv, ctxt, 0)) {
Packit 8480eb
		free(ctxt);
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	*context = ctxt;
Packit 8480eb
Packit 8480eb
	return 0;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_reinit(const char *mapfmt,
Packit 8480eb
		  int argc, const char *const *argv, void **context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt = (struct lookup_context *) *context;
Packit 8480eb
	struct lookup_context *new;
Packit 8480eb
	char buf[MAX_ERR_BUF];
Packit 8480eb
	int ret;
Packit 8480eb
Packit 8480eb
	new = malloc(sizeof(struct lookup_context));
Packit 8480eb
	if (!new) {
Packit 8480eb
		char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
		logerr(MODPREFIX "malloc: %s", estr);
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	new->parse = ctxt->parse;
Packit 8480eb
	ret = do_init(mapfmt, argc, argv, new, 1);
Packit 8480eb
	if (ret) {
Packit 8480eb
		free(new);
Packit 8480eb
		return 1;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	new->dlhandle = ctxt->dlhandle;
Packit 8480eb
	new->setautomntent = ctxt->setautomntent;
Packit 8480eb
	new->getautomntent_r = ctxt->getautomntent_r;
Packit 8480eb
	new->getautomntbyname_r = ctxt->getautomntbyname_r;
Packit 8480eb
	new->endautomntent = ctxt->endautomntent;
Packit 8480eb
Packit 8480eb
	*context = new;
Packit 8480eb
Packit 8480eb
	free(ctxt);
Packit 8480eb
Packit 8480eb
	return 0;
Packit 8480eb
}
Packit 8480eb
Packit Service 145c60
static int setautomntent(unsigned int logopt,
Packit Service 145c60
			 struct lookup_context *ctxt, const char *mapname,
Packit Service 145c60
			 void **sss_ctxt)
Packit 8480eb
{
Packit Service 145c60
	int ret = ctxt->setautomntent(mapname, sss_ctxt);
Packit Service 145c60
	if (ret) {
Packit Service 145c60
		char buf[MAX_ERR_BUF];
Packit Service 145c60
		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
		error(logopt, MODPREFIX "setautomntent: %s", estr);
Packit Service 145c60
		if (*sss_ctxt)
Packit Service 145c60
			free(*sss_ctxt);
Packit Service 9e4465
	}
Packit Service 145c60
	return ret;
Packit Service ee4988
}
Packit Service ee4988
Packit Service ee4988
static int setautomntent_wait(unsigned int logopt,
Packit Service 145c60
			      struct lookup_context *ctxt,
Packit Service 145c60
			      const char *mapname,
Packit Service 145c60
			      void **sss_ctxt, unsigned int retries)
Packit Service ee4988
{
Packit Service ee4988
	unsigned int retry = 0;
Packit Service ee4988
	int ret = 0;
Packit Service ee4988
Packit Service ee4988
	*sss_ctxt = NULL;
Packit Service ee4988
Packit Service 145c60
	while (++retry < retries) {
Packit Service 145c60
		struct timespec t = { 0, SETAUTOMOUNTENT_MASTER_INTERVAL };
Packit 8480eb
		struct timespec r;
Packit 8480eb
Packit Service 145c60
		ret = ctxt->setautomntent(mapname, sss_ctxt);
Packit Service 145c60
		if (ret != ENOENT)
Packit Service 145c60
			break;
Packit 8480eb
Packit 8480eb
		if (*sss_ctxt) {
Packit 8480eb
			free(*sss_ctxt);
Packit 8480eb
			*sss_ctxt = NULL;
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		while (nanosleep(&t, &r) == -1 && errno == EINTR)
Packit 8480eb
			memcpy(&t, &r, sizeof(struct timespec));
Packit 8480eb
	}
Packit 8480eb
Packit Service 145c60
Packit Service 145c60
	if (ret) {
Packit Service 145c60
		char buf[MAX_ERR_BUF];
Packit Service 145c60
		char *estr;
Packit Service 145c60
Packit 8480eb
		if (*sss_ctxt) {
Packit 8480eb
			free(*sss_ctxt);
Packit 8480eb
			*sss_ctxt = NULL;
Packit 8480eb
		}
Packit 8480eb
Packit Service 145c60
		if (retry == retries)
Packit 8480eb
			ret = ETIMEDOUT;
Packit 8480eb
Packit Service 145c60
		estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
		error(logopt, MODPREFIX "setautomntent: %s", estr);
Packit Service 0ff584
	}
Packit Service 0ff584
Packit Service 145c60
	return ret;
Packit Service 0ff584
}
Packit Service 0ff584
Packit 8480eb
static int endautomntent(unsigned int logopt,
Packit 8480eb
			 struct lookup_context *ctxt, void **sss_ctxt)
Packit 8480eb
{
Packit 8480eb
	int ret = ctxt->endautomntent(sss_ctxt);
Packit 8480eb
	if (ret) {
Packit 8480eb
		char buf[MAX_ERR_BUF];
Packit 8480eb
		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit 8480eb
		error(logopt, MODPREFIX "endautomntent: %s", estr);
Packit 8480eb
	}
Packit 8480eb
	return ret;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_read_master(struct master *master, time_t age, void *context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt = (struct lookup_context *) context;
Packit 8480eb
	unsigned int timeout = master->default_timeout;
Packit 8480eb
	unsigned int logging = master->default_logging;
Packit 8480eb
	unsigned int logopt = master->logopt;
Packit 8480eb
	void *sss_ctxt = NULL;
Packit 8480eb
	char buf[MAX_ERR_BUF];
Packit 8480eb
	char *buffer;
Packit 8480eb
	size_t buffer_len;
Packit 8480eb
	char *key;
Packit 8480eb
	char *value = NULL;
Packit 8480eb
	int count, ret;
Packit Service 9e4465
Packit Service 145c60
	ret = setautomntent(logopt, ctxt, ctxt->mapname, &sss_ctxt);
Packit Service 145c60
	if (ret) {
Packit Service 145c60
		unsigned int retries;
Packit 8480eb
Packit Service 145c60
		if (ret != ENOENT)
Packit Service 145c60
			return NSS_STATUS_UNAVAIL;
Packit Service 145c60
Packit Service 145c60
		retries = defaults_get_sss_master_map_wait() * 2;
Packit Service 145c60
		if (retries <= 0)
Packit Service 145c60
			return NSS_STATUS_NOTFOUND;
Packit Service 145c60
Packit Service 145c60
		ret = setautomntent_wait(logopt,
Packit Service 145c60
					 ctxt, ctxt->mapname, &sss_ctxt,
Packit Service 145c60
					 retries);
Packit Service 145c60
		if (ret) {
Packit Service 145c60
			if (ret == ENOENT)
Packit Service 145c60
				return NSS_STATUS_NOTFOUND;
Packit Service 145c60
			return NSS_STATUS_UNAVAIL;
Packit Service 145c60
		}
Packit Service 145c60
	}
Packit 8480eb
Packit 8480eb
	count = 0;
Packit 8480eb
	while (1) {
Packit 8480eb
	        key = NULL;
Packit 8480eb
	        value = NULL;
Packit Service 145c60
		ret = ctxt->getautomntent_r(&key, &value, sss_ctxt);
Packit Service 145c60
		if (ret && ret != ENOENT) {
Packit Service 145c60
			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
			error(logopt, MODPREFIX "getautomntent_r: %s", estr);
Packit 8480eb
			endautomntent(logopt, ctxt, &sss_ctxt);
Packit Service 145c60
			if (key)
Packit Service 145c60
				free(key);
Packit Service 145c60
			if (value)
Packit Service 145c60
				free(value);
Packit Service 145c60
			return NSS_STATUS_UNAVAIL;
Packit 8480eb
		}
Packit Service 145c60
		if (ret == ENOENT) {
Packit Service 145c60
			if (!count) {
Packit Service 145c60
				char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
				error(logopt, MODPREFIX "getautomntent_r: %s", estr);
Packit Service 145c60
				endautomntent(logopt, ctxt, &sss_ctxt);
Packit Service 145c60
				if (key)
Packit Service 145c60
					free(key);
Packit Service 145c60
				if (value)
Packit Service 145c60
					free(value);
Packit Service 145c60
				return NSS_STATUS_NOTFOUND;
Packit Service 145c60
			}
Packit Service 28dd5b
			break;
Packit Service 145c60
		}
Packit 8480eb
		count++;
Packit 8480eb
Packit 8480eb
		buffer_len = strlen(key) + 1 + strlen(value) + 2;
Packit 8480eb
		buffer = malloc(buffer_len);
Packit 8480eb
		if (!buffer) {
Packit 8480eb
			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
Packit 8480eb
			error(logopt, MODPREFIX "malloc: %s", estr);
Packit 8480eb
			endautomntent(logopt, ctxt, &sss_ctxt);
Packit 8480eb
			free(key);
Packit 8480eb
			free(value);
Packit 8480eb
			return NSS_STATUS_UNAVAIL;
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		/*
Packit 8480eb
		 * TODO: implement sun % hack for key translation for
Packit 8480eb
		 * mixed case keys in schema that are single case only.
Packit 8480eb
		 */
Packit 8480eb
Packit 8480eb
		strcpy(buffer, key);
Packit 8480eb
		strcat(buffer, " ");
Packit 8480eb
		strcat(buffer, value);
Packit 8480eb
Packit 8480eb
		/*
Packit 8480eb
		 * TODO: handle cancelation. This almost certainly isn't
Packit 8480eb
		 * handled properly by other lookup modules either so it
Packit 8480eb
		 * should be done when cancelation is reviewed for the
Packit 8480eb
		 * other modules. Ditto for the other lookup module entry
Packit 8480eb
		 * points.
Packit 8480eb
		 */
Packit 8480eb
		master_parse_entry(buffer, timeout, logging, age);
Packit 8480eb
Packit 8480eb
		free(buffer);
Packit 8480eb
		free(key);
Packit 8480eb
		free(value);
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	endautomntent(logopt, ctxt, &sss_ctxt);
Packit 8480eb
Packit 8480eb
	return NSS_STATUS_SUCCESS;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_read_map(struct autofs_point *ap, time_t age, void *context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt = (struct lookup_context *) context;
Packit 8480eb
	struct map_source *source;
Packit 8480eb
	struct mapent_cache *mc;
Packit 8480eb
	void *sss_ctxt = NULL;
Packit Service 145c60
	char buf[MAX_ERR_BUF];
Packit 8480eb
	char *key;
Packit 8480eb
	char *value = NULL;
Packit 8480eb
	char *s_key;
Packit 8480eb
	int count, ret;
Packit 8480eb
Packit 8480eb
	source = ap->entry->current;
Packit 8480eb
	ap->entry->current = NULL;
Packit 8480eb
	master_source_current_signal(ap->entry);
Packit 8480eb
Packit 8480eb
	mc = source->mc;
Packit 8480eb
Packit 8480eb
	/*
Packit 8480eb
	 * If we don't need to create directories (or don't need
Packit 8480eb
	 * to read an amd cache:=all map) then there's no use
Packit 8480eb
	 * reading the map. We always need to read the whole map
Packit 8480eb
	 * for direct mounts in order to mount the triggers.
Packit 8480eb
	 */
Packit 8480eb
	if (ap->type != LKP_DIRECT &&
Packit 8480eb
	    !(ap->flags & (MOUNT_FLAG_GHOST|MOUNT_FLAG_AMD_CACHE_ALL))) {
Packit 8480eb
		debug(ap->logopt, "map read not needed, so not done");
Packit 8480eb
		return NSS_STATUS_SUCCESS;
Packit 8480eb
	}
Packit 8480eb
Packit Service 145c60
	ret = setautomntent(ap->logopt, ctxt, ctxt->mapname, &sss_ctxt);
Packit Service 145c60
	if (ret) {
Packit Service 145c60
		if (ret == ENOENT)
Packit Service 145c60
			return NSS_STATUS_NOTFOUND;
Packit Service 145c60
		return NSS_STATUS_UNAVAIL;
Packit Service 145c60
	}
Packit 8480eb
Packit 8480eb
	count = 0;
Packit 8480eb
	while (1) {
Packit 8480eb
	        key = NULL;
Packit 8480eb
	        value = NULL;
Packit Service 145c60
		ret = ctxt->getautomntent_r(&key, &value, sss_ctxt);
Packit Service 145c60
		if (ret && ret != ENOENT) {
Packit Service 145c60
			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
			error(ap->logopt,
Packit Service 145c60
			      MODPREFIX "getautomntent_r: %s", estr);
Packit 8480eb
			endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
			if (key)
Packit Service 145c60
				free(key);
Packit Service 145c60
			if (value)
Packit Service 145c60
				free(value);
Packit Service 145c60
			return NSS_STATUS_UNAVAIL;
Packit 8480eb
		}
Packit Service 145c60
		if (ret == ENOENT) {
Packit Service 145c60
			if (!count) {
Packit Service 145c60
				char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
				error(ap->logopt,
Packit Service 145c60
				      MODPREFIX "getautomntent_r: %s", estr);
Packit Service 145c60
				endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
				if (key)
Packit Service 145c60
					free(key);
Packit Service 145c60
				if (value)
Packit Service 145c60
					free(value);
Packit Service 145c60
				return NSS_STATUS_NOTFOUND;
Packit Service 145c60
			}
Packit Service 28dd5b
			break;
Packit Service 145c60
		}
Packit Service 28dd5b
Packit 8480eb
		/*
Packit 8480eb
		 * Ignore keys beginning with '+' as plus map
Packit 8480eb
		 * inclusion is only valid in file maps.
Packit 8480eb
		 */
Packit 8480eb
		if (*key == '+') {
Packit 8480eb
			warn(ap->logopt,
Packit 8480eb
			     MODPREFIX "ignoring '+' map entry - not in file map");
Packit 8480eb
			free(key);
Packit 8480eb
			free(value);
Packit 8480eb
			continue;
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		if (*key == '/' && strlen(key) == 1) {
Packit 8480eb
			if (ap->type == LKP_DIRECT) {
Packit 8480eb
				free(key);
Packit 8480eb
				free(value);
Packit 8480eb
				continue;
Packit 8480eb
			}
Packit 8480eb
			*key = '*';
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		/*
Packit 8480eb
		 * TODO: implement sun % hack for key translation for
Packit 8480eb
		 * mixed case keys in schema that are single case only.
Packit 8480eb
		 */
Packit 8480eb
Packit 8480eb
		s_key = sanitize_path(key, strlen(key), ap->type, ap->logopt);
Packit 8480eb
		if (!s_key) {
Packit 8480eb
			error(ap->logopt, MODPREFIX "invalid path %s", key);
Packit 8480eb
			endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit 8480eb
			free(key);
Packit 8480eb
			free(value);
Packit 8480eb
			return NSS_STATUS_NOTFOUND;
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		count++;
Packit 8480eb
Packit 8480eb
		cache_writelock(mc);
Packit 8480eb
		cache_update(mc, source, s_key, value, age);
Packit 8480eb
		cache_unlock(mc);
Packit 8480eb
Packit 8480eb
		free(s_key);
Packit 8480eb
		free(key);
Packit 8480eb
		free(value);
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit 8480eb
Packit 8480eb
	source->age = age;
Packit 8480eb
Packit 8480eb
	return NSS_STATUS_SUCCESS;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
static int lookup_one(struct autofs_point *ap,
Packit 8480eb
		char *qKey, int qKey_len, struct lookup_context *ctxt)
Packit 8480eb
{
Packit 8480eb
	struct map_source *source;
Packit 8480eb
	struct mapent_cache *mc;
Packit 8480eb
	struct mapent *we;
Packit 8480eb
	void *sss_ctxt = NULL;
Packit 8480eb
	time_t age = monotonic_time(NULL);
Packit Service 145c60
	char buf[MAX_ERR_BUF];
Packit 8480eb
	char *value = NULL;
Packit 8480eb
	char *s_key;
Packit Service 145c60
	int ret;
Packit 8480eb
Packit 8480eb
	source = ap->entry->current;
Packit 8480eb
	ap->entry->current = NULL;
Packit 8480eb
	master_source_current_signal(ap->entry);
Packit 8480eb
Packit 8480eb
	mc = source->mc;
Packit 8480eb
Packit Service 145c60
	ret = setautomntent(ap->logopt, ctxt, ctxt->mapname, &sss_ctxt);
Packit Service bf9736
	if (ret) {
Packit Service 145c60
		if (ret == ENOENT)
Packit Service 145c60
			return NSS_STATUS_NOTFOUND;
Packit Service 145c60
		return NSS_STATUS_UNAVAIL;
Packit 8480eb
	}
Packit Service bf9736
Packit Service 145c60
	ret = ctxt->getautomntbyname_r(qKey, &value, sss_ctxt);
Packit Service 145c60
	if (ret && ret != ENOENT) {
Packit Service 145c60
		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
		error(ap->logopt,
Packit Service 145c60
		      MODPREFIX "getautomntbyname_r: %s", estr);
Packit Service 145c60
		endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
		if (value)
Packit Service 145c60
			free(value);
Packit Service 145c60
		return NSS_STATUS_UNAVAIL;
Packit Service 145c60
	}
Packit Service 145c60
	if (ret != ENOENT) {
Packit Service 145c60
		/*
Packit Service 145c60
		 * TODO: implement sun % hack for key translation for
Packit Service 145c60
		 * mixed case keys in schema that are single case only.
Packit Service 145c60
		 */
Packit Service 145c60
		s_key = sanitize_path(qKey, qKey_len, ap->type, ap->logopt);
Packit Service 145c60
		if (!s_key) {
Packit Service 145c60
			free(value);
Packit Service 145c60
			value = NULL;
Packit Service 145c60
			goto wild;
Packit Service 145c60
		}
Packit Service 145c60
		cache_writelock(mc);
Packit Service 145c60
		ret = cache_update(mc, source, s_key, value, age);
Packit Service 145c60
		cache_unlock(mc);
Packit Service 145c60
		endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
		free(s_key);
Packit 8480eb
		free(value);
Packit Service 145c60
		return NSS_STATUS_SUCCESS;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
wild:
Packit Service 145c60
	ret = ctxt->getautomntbyname_r("/", &value, sss_ctxt);
Packit Service 145c60
	if (ret && ret != ENOENT) {
Packit Service 145c60
		char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
		error(ap->logopt,
Packit Service 145c60
		      MODPREFIX "getautomntbyname_r: %s", estr);
Packit Service 145c60
		endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
		if (value)
Packit Service 145c60
			free(value);
Packit Service 145c60
		return NSS_STATUS_UNAVAIL;
Packit Service 145c60
	}
Packit Service 145c60
	if (ret == ENOENT) {
Packit Service 145c60
		ret = ctxt->getautomntbyname_r("*", &value, sss_ctxt);
Packit Service 145c60
		if (ret && ret != ENOENT) {
Packit Service 145c60
			char *estr = strerror_r(ret, buf, MAX_ERR_BUF);
Packit Service 145c60
			error(ap->logopt,
Packit Service 145c60
			      MODPREFIX "getautomntbyname_r: %s", estr);
Packit Service bf9736
			endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit Service 145c60
			if (value)
Packit Service 145c60
				free(value);
Packit Service 145c60
			return NSS_STATUS_UNAVAIL;
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
Packit Service 145c60
	if (ret == ENOENT) {
Packit 8480eb
		/* Failed to find wild entry, update cache if needed */
Packit 8480eb
		cache_writelock(mc);
Packit 8480eb
		we = cache_lookup_distinct(mc, "*");
Packit 8480eb
		if (we) {
Packit 8480eb
			/* Wildcard entry existed and is now gone */
Packit 8480eb
			if (we->source == source) {
Packit 8480eb
				cache_delete(mc, "*");
Packit 8480eb
				source->stale = 1;
Packit 8480eb
			}
Packit 8480eb
		}
Packit 8480eb
Packit 8480eb
		/* Not found in the map but found in the cache */
Packit 8480eb
		struct mapent *exists = cache_lookup_distinct(mc, qKey);
Packit 8480eb
		if (exists && exists->source == source) {
Packit 8480eb
			if (exists->mapent) {
Packit 8480eb
				free(exists->mapent);
Packit 8480eb
				exists->mapent = NULL;
Packit 8480eb
				source->stale = 1;
Packit 8480eb
				exists->status = 0;
Packit 8480eb
			}
Packit 8480eb
		}
Packit 8480eb
		cache_unlock(mc);
Packit 8480eb
		endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit 8480eb
		return NSS_STATUS_NOTFOUND;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	cache_writelock(mc);
Packit Service 145c60
	/* Wildcard not in map but now is */
Packit Service 145c60
	we = cache_lookup_distinct(mc, "*");
Packit Service 145c60
	if (!we)
Packit Service 27e606
		source->stale = 1;
Packit Service 145c60
	ret = cache_update(mc, source, "*", value, age);
Packit Service 145c60
	cache_unlock(mc);
Packit 8480eb
Packit 8480eb
	endautomntent(ap->logopt, ctxt, &sss_ctxt);
Packit 8480eb
        free(value);
Packit 8480eb
Packit 8480eb
	return NSS_STATUS_SUCCESS;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
static int check_map_indirect(struct autofs_point *ap,
Packit 8480eb
			      char *key, int key_len,
Packit 8480eb
			      struct lookup_context *ctxt)
Packit 8480eb
{
Packit 8480eb
	struct map_source *source;
Packit 8480eb
	struct mapent_cache *mc;
Packit Service 145c60
	struct mapent *me;
Packit Service 145c60
	time_t now = monotonic_time(NULL);
Packit Service 145c60
	time_t t_last_read;
Packit 8480eb
	int ret, cur_state;
Packit 8480eb
Packit 8480eb
	source = ap->entry->current;
Packit 8480eb
	ap->entry->current = NULL;
Packit 8480eb
	master_source_current_signal(ap->entry);
Packit 8480eb
Packit 8480eb
	mc = source->mc;
Packit 8480eb
Packit 8480eb
	master_source_current_wait(ap->entry);
Packit 8480eb
	ap->entry->current = source;
Packit 8480eb
Packit 8480eb
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cur_state);
Packit 8480eb
	ret = lookup_one(ap, key, key_len, ctxt);
Packit 8480eb
	if (ret == NSS_STATUS_NOTFOUND) {
Packit 8480eb
		pthread_setcancelstate(cur_state, NULL);
Packit 8480eb
		return ret;
Packit 8480eb
	} else if (ret == NSS_STATUS_UNAVAIL) {
Packit 8480eb
		/*
Packit 8480eb
		 * If the server is down and the entry exists in the cache
Packit 8480eb
		 * and belongs to this map return success and use the entry.
Packit 8480eb
		 */
Packit 8480eb
		struct mapent *exists = cache_lookup(mc, key);
Packit 8480eb
		if (exists && exists->source == source) {
Packit 8480eb
			pthread_setcancelstate(cur_state, NULL);
Packit 8480eb
			return NSS_STATUS_SUCCESS;
Packit 8480eb
		}
Packit 8480eb
		pthread_setcancelstate(cur_state, NULL);
Packit 8480eb
Packit 8480eb
		warn(ap->logopt,
Packit 8480eb
		     MODPREFIX "lookup for %s failed: connection failed", key);
Packit 8480eb
Packit 8480eb
		return ret;
Packit 8480eb
	}
Packit 8480eb
	pthread_setcancelstate(cur_state, NULL);
Packit 8480eb
Packit Service 145c60
	/*
Packit Service 145c60
	 * Check for map change and update as needed for
Packit Service 145c60
	 * following cache lookup.
Packit Service 145c60
	 */
Packit Service 145c60
	cache_readlock(mc);
Packit Service 145c60
	t_last_read = ap->exp_runfreq + 1;
Packit Service 145c60
	me = cache_lookup_first(mc);
Packit Service 145c60
	while (me) {
Packit Service 145c60
		if (me->source == source) {
Packit Service 145c60
			t_last_read = now - me->age;
Packit Service 145c60
			break;
Packit Service 145c60
		}
Packit Service 145c60
		me = cache_lookup_next(mc, me);
Packit Service 145c60
	}
Packit Service 145c60
	cache_unlock(mc);
Packit Service 145c60
Packit Service 145c60
	if (t_last_read > ap->exp_runfreq && ret & CHE_UPDATED)
Packit Service 145c60
		source->stale = 1;
Packit Service 145c60
Packit Service 145c60
	cache_readlock(mc);
Packit Service 145c60
	me = cache_lookup_distinct(mc, "*");
Packit Service 145c60
	if (ret == CHE_MISSING && (!me || me->source != source)) {
Packit Service 145c60
		cache_unlock(mc);
Packit Service 145c60
		return NSS_STATUS_NOTFOUND;
Packit Service 145c60
	}
Packit Service 145c60
	cache_unlock(mc);
Packit Service 145c60
Packit 8480eb
	return NSS_STATUS_SUCCESS;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_mount(struct autofs_point *ap, const char *name, int name_len, void *context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt = (struct lookup_context *) context;
Packit 8480eb
	struct map_source *source;
Packit 8480eb
	struct mapent_cache *mc;
Packit 8480eb
	struct mapent *me;
Packit 8480eb
	char key[KEY_MAX_LEN + 1];
Packit 8480eb
	int key_len;
Packit 8480eb
	char *mapent = NULL;
Packit 8480eb
	char mapent_buf[MAPENT_MAX_LEN + 1];
Packit 8480eb
	int ret;
Packit 8480eb
Packit 8480eb
	source = ap->entry->current;
Packit 8480eb
	ap->entry->current = NULL;
Packit 8480eb
	master_source_current_signal(ap->entry);
Packit 8480eb
Packit 8480eb
	mc = source->mc;
Packit 8480eb
Packit 8480eb
	debug(ap->logopt, MODPREFIX "looking up %s", name);
Packit 8480eb
Packit 8480eb
	key_len = snprintf(key, KEY_MAX_LEN + 1, "%s", name);
Packit 8480eb
	if (key_len > KEY_MAX_LEN)
Packit 8480eb
		return NSS_STATUS_NOTFOUND;
Packit 8480eb
Packit 8480eb
	/* Check if we recorded a mount fail for this key anywhere */
Packit 8480eb
	me = lookup_source_mapent(ap, key, LKP_DISTINCT);
Packit 8480eb
	if (me) {
Packit 8480eb
		if (me->status >= monotonic_time(NULL)) {
Packit 8480eb
			cache_unlock(me->mc);
Packit 8480eb
			return NSS_STATUS_NOTFOUND;
Packit 8480eb
		} else {
Packit 8480eb
			struct mapent_cache *smc = me->mc;
Packit 8480eb
			struct mapent *sme;
Packit 8480eb
Packit 8480eb
			if (me->mapent)
Packit 8480eb
				cache_unlock(smc);
Packit 8480eb
			else {
Packit 8480eb
				cache_unlock(smc);
Packit 8480eb
				cache_writelock(smc);
Packit 8480eb
				sme = cache_lookup_distinct(smc, key);
Packit 8480eb
				/* Negative timeout expired for non-existent entry. */
Packit 8480eb
				if (sme && !sme->mapent) {
Packit 8480eb
					if (cache_pop_mapent(sme) == CHE_FAIL)
Packit 8480eb
						cache_delete(smc, key);
Packit 8480eb
				}
Packit 8480eb
				cache_unlock(smc);
Packit 8480eb
			}
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
        /*
Packit 8480eb
	 * We can't check the direct mount map as if it's not in
Packit 8480eb
	 * the map cache already we never get a mount lookup, so
Packit 8480eb
	 * we never know about it.
Packit 8480eb
	 */
Packit 8480eb
	if (ap->type == LKP_INDIRECT && *key != '/') {
Packit 8480eb
		int status;
Packit 8480eb
		char *lkp_key;
Packit 8480eb
Packit 8480eb
		cache_readlock(mc);
Packit 8480eb
		me = cache_lookup_distinct(mc, key);
Packit 8480eb
		if (me && me->multi)
Packit 8480eb
			lkp_key = strdup(me->multi->key);
Packit 8480eb
		else
Packit 8480eb
			lkp_key = strdup(key);
Packit 8480eb
		cache_unlock(mc);
Packit 8480eb
Packit 8480eb
		if (!lkp_key)
Packit 8480eb
			return NSS_STATUS_UNKNOWN;
Packit 8480eb
Packit 8480eb
		master_source_current_wait(ap->entry);
Packit 8480eb
		ap->entry->current = source;
Packit 8480eb
Packit 8480eb
		status = check_map_indirect(ap, lkp_key, strlen(lkp_key), ctxt);
Packit 8480eb
		free(lkp_key);
Packit 8480eb
		if (status)
Packit 8480eb
			return status;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	/*
Packit 8480eb
	 * We can't take the writelock for direct mounts. If we're
Packit 8480eb
	 * starting up or trying to re-connect to an existing direct
Packit 8480eb
	 * mount we could be iterating through the map entries with
Packit 8480eb
	 * the readlock held. But we don't need to update the cache
Packit 8480eb
	 * when we're starting up so just take the readlock in that
Packit 8480eb
	 */
Packit 8480eb
	if (ap->flags & MOUNT_FLAG_REMOUNT)
Packit Service 1c53f7
		cache_writelock(mc);
Packit Service 145c60
	else
Packit Service 145c60
		cache_readlock(mc);
Packit 8480eb
	me = cache_lookup(mc, key);
Packit 8480eb
	/* Stale mapent => check for entry in alternate source or wildcard */
Packit 8480eb
	if (me && !me->mapent) {
Packit 8480eb
		while ((me = cache_lookup_key_next(me)))
Packit 8480eb
			if (me->source == source)
Packit 8480eb
				break;
Packit 8480eb
		if (!me)
Packit 8480eb
			me = cache_lookup_distinct(mc, "*");
Packit 8480eb
	}
Packit 8480eb
	if (me && me->mapent) {
Packit 8480eb
		/*
Packit 8480eb
		 * If this is a lookup add wildcard match for later validation
Packit 8480eb
		 * checks and negative cache lookups.
Packit 8480eb
		 */
Packit 8480eb
		if (ap->type == LKP_INDIRECT && *me->key == '*' &&
Packit 8480eb
		   !(ap->flags & MOUNT_FLAG_REMOUNT)) {
Packit 8480eb
			ret = cache_update(mc, source, key, me->mapent, me->age);
Packit 8480eb
			if (!(ret & (CHE_OK | CHE_UPDATED)))
Packit 8480eb
				me = NULL;
Packit 8480eb
		}
Packit 8480eb
		if (me && (me->source == source || *me->key == '/')) {
Packit 8480eb
			strcpy(mapent_buf, me->mapent);
Packit 8480eb
			mapent = mapent_buf;
Packit 8480eb
		}
Packit 8480eb
	}
Packit 8480eb
	cache_unlock(mc);
Packit 8480eb
Packit 8480eb
	if (!mapent)
Packit 8480eb
		return NSS_STATUS_TRYAGAIN;
Packit 8480eb
Packit 8480eb
	master_source_current_wait(ap->entry);
Packit 8480eb
	ap->entry->current = source;
Packit 8480eb
Packit 8480eb
	debug(ap->logopt, MODPREFIX "%s -> %s", key, mapent);
Packit 8480eb
	ret = ctxt->parse->parse_mount(ap, key, key_len,
Packit 8480eb
				       mapent, ctxt->parse->context);
Packit 8480eb
	if (ret) {
Packit 8480eb
		/* Don't update negative cache when re-connecting */
Packit 8480eb
		if (ap->flags & MOUNT_FLAG_REMOUNT)
Packit 8480eb
			return NSS_STATUS_TRYAGAIN;
Packit 8480eb
		cache_writelock(mc);
Packit 8480eb
		cache_update_negative(mc, source, key, ap->negative_timeout);
Packit 8480eb
		cache_unlock(mc);
Packit 8480eb
		return NSS_STATUS_TRYAGAIN;
Packit 8480eb
	}
Packit 8480eb
Packit 8480eb
	return NSS_STATUS_SUCCESS;
Packit 8480eb
}
Packit 8480eb
Packit 8480eb
int lookup_done(void *context)
Packit 8480eb
{
Packit 8480eb
	struct lookup_context *ctxt = (struct lookup_context *) context;
Packit 8480eb
	int rv = close_parse(ctxt->parse);
Packit 8480eb
	dlclose(ctxt->dlhandle);
Packit 8480eb
	free(ctxt);
Packit 8480eb
	return rv;
Packit 8480eb
}