1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
/*
* lookup_yp.c
*
* Module for Linux automountd to access a YP (NIS) automount map
*/
#include <stdio.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
#define MODULE_LOOKUP
#include "automount.h"
#define MAPFMT_DEFAULT "sun"
#define MODPREFIX "lookup(yp): "
struct lookup_context {
char *domainname;
char *mapname;
struct parse_mod *parse;
};
int lookup_version = AUTOFS_LOOKUP_VERSION; /* Required by protocol */
int lookup_init(char *mapfmt, int argc, char **argv, void **context)
{
struct lookup_context *ctxt;
int err;
if ( !(*context = ctxt = malloc(sizeof(struct lookup_context))) ) {
syslog(LOG_CRIT, MODPREFIX "%m");
return 1;
}
if ( argc < 1 ) {
syslog(LOG_CRIT, MODPREFIX "No map name");
return 1;
}
ctxt->mapname = argv[0];
err = yp_get_default_domain(&ctxt->domainname);
if ( err ) {
syslog(LOG_CRIT, MODPREFIX "map %s: %s\n", ctxt->mapname, yperr_string(err));
return 1;
}
if ( !mapfmt )
mapfmt = MAPFMT_DEFAULT;
return !(ctxt->parse = open_parse(mapfmt,MODPREFIX,argc-1,argv+1));
}
int lookup_mount(char *root, char *name, int name_len, void *context)
{
struct lookup_context *ctxt = (struct lookup_context *) context;
char *mapent;
int mapent_len;
int err, rv;
syslog(LOG_DEBUG, MODPREFIX "looking up %s", name);
err = yp_match(ctxt->domainname,ctxt->mapname,name,name_len,&mapent,&mapent_len);
if ( err == YPERR_KEY ) {
/* Try to get the "*" entry if there is one - note that we *don't*
modify "name" so & -> the name we used, not "*" */
err = yp_match(ctxt->domainname,ctxt->mapname,"*",1,&mapent,&mapent_len);
}
if ( err ) {
syslog(LOG_NOTICE, MODPREFIX "lookup for %s failed: %s", name, yperr_string(err));
return 1;
}
mapent[mapent_len] = '\0';
syslog(LOG_DEBUG, MODPREFIX "%s -> %s", name, mapent);
rv = ctxt->parse->parse_mount(root,name,name_len,mapent,ctxt->parse->context);
free(mapent);
return rv;
}
int lookup_done(void *context)
{
struct lookup_context *ctxt = (struct lookup_context *) context;
int rv = close_parse(ctxt->parse);
free(ctxt);
return rv;
}
|