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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#ident "$Id: lookup_program.c,v 1.3 1999/12/17 19:02:47 hpa Exp $"
/* ----------------------------------------------------------------------- *
*
* lookup_program.c - module for Linux automount to access an
* automount map via a query program
*
* Copyright 1997 Transmeta Corporation - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; either version 2 of the License, or (at your option) any later
* version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MODULE_LOOKUP
#include "automount.h"
#define MAPFMT_DEFAULT "sun"
#define MODPREFIX "lookup(program): "
#define MAPENT_MAX_LEN 4095
struct lookup_context {
const char *mapname;
struct parse_mod *parse;
};
int lookup_version = AUTOFS_LOOKUP_VERSION; /* Required by protocol */
int lookup_init(const char *mapfmt, int argc, const char * const *argv,
void **context)
{
struct lookup_context *ctxt;
if ( !(*context = ctxt = malloc(sizeof(struct lookup_context))) ) {
syslog(LOG_CRIT, MODPREFIX "malloc: %m");
return 1;
}
if ( argc < 1 ) {
syslog(LOG_CRIT, MODPREFIX "No map name");
return 1;
}
ctxt->mapname = argv[0];
if (ctxt->mapname[0] != '/') {
syslog(LOG_CRIT, MODPREFIX "program map %s is not an absolute pathname",
ctxt->mapname);
return 1;
}
if ( access(ctxt->mapname, X_OK) ) {
syslog(LOG_WARNING, MODPREFIX "program map %s missing or not executable",
ctxt->mapname);
}
if ( !mapfmt )
mapfmt = MAPFMT_DEFAULT;
return !(ctxt->parse = open_parse(mapfmt,MODPREFIX,argc-1,argv+1));
}
int lookup_mount(const char *root, const char *name, int name_len,
void *context)
{
struct lookup_context *ctxt = (struct lookup_context *) context;
char mapent[MAPENT_MAX_LEN+1], *mapp;
char errbuf[1024], *errp;
char *p, ch;
int pipefd[2], epipefd[2];
pid_t f;
int files_left;
int status;
fd_set readfds, ourfds;
syslog(LOG_DEBUG, MODPREFIX "looking up %s", name);
/* We don't use popen because we don't want to run /bin/sh plus we
want to send stderr to the syslog, and we don't use spawnl()
because we need the pipe hooks */
if ( pipe(pipefd) ) {
syslog(LOG_ERR, MODPREFIX "pipe: %m");
return 1;
}
if ( pipe(epipefd) ) {
close(pipefd[0]);
close(pipefd[1]);
return 1;
}
f = fork();
if ( f < 0 ) {
close(pipefd[0]);
close(pipefd[1]);
close(epipefd[0]);
close(epipefd[1]);
syslog(LOG_ERR, MODPREFIX "fork: %m");
return 1;
} else if ( f == 0 ) {
close(pipefd[0]);
close(epipefd[0]);
dup2(pipefd[1],STDOUT_FILENO);
dup2(epipefd[1],STDERR_FILENO);
close(pipefd[1]);
close(epipefd[1]);
execl(ctxt->mapname, ctxt->mapname, name, NULL);
_exit(255); /* execl() failed */
}
close(pipefd[1]);
close(epipefd[1]);
mapp = mapent;
errp = errbuf;
FD_ZERO(&ourfds);
FD_SET(pipefd[0],&ourfds);
FD_SET(epipefd[0],&ourfds);
files_left = 2;
while (files_left) {
readfds = ourfds;
if ( select(OPEN_MAX, &readfds, NULL, NULL, NULL) < 0 &&
errno != EINTR )
break;
if ( FD_ISSET(pipefd[0],&readfds) ) {
if ( read(pipefd[0], &ch, 1) < 1 ) {
FD_CLR(pipefd[0], &ourfds);
files_left--;
} else if ( mapp ) {
if ( ch == '\n' ) {
*mapp = '\0';
mapp = NULL; /* End of line reached */
} else if ( mapp-mapent < MAPENT_MAX_LEN )
*(mapp++) = ch;
}
}
if ( FD_ISSET(epipefd[0],&readfds) ) {
if ( read(epipefd[0], &ch, 1) < 1 ) {
FD_CLR(epipefd[0], &ourfds);
files_left--;
} else if ( ch == '\n' ) {
*errp = '\0';
if ( errbuf[0] )
syslog(LOG_NOTICE, ">> %s", errbuf);
errp = errbuf;
} else {
if ( errp >= &errbuf[1023] ) {
*errp = '\0';
syslog(LOG_NOTICE, ">> %s", errbuf);
errp = errbuf;
}
*(errp++) = ch;
}
}
}
if ( mapp )
*mapp = '\0';
if ( errp > errbuf ) {
*errp = '\0';
syslog(LOG_NOTICE, ">> %s", errbuf);
}
close(pipefd[0]);
close(epipefd[0]);
if ( waitpid(f,&status,0) != f ) {
syslog(LOG_ERR, MODPREFIX "waitpid: %m");
return 1;
}
if ( mapp == mapent || !WIFEXITED(status) || WEXITSTATUS(status) != 0 ) {
syslog(LOG_NOTICE, MODPREFIX "lookup for %s failed", name);
return 1;
}
if ( (p = strchr(mapent,'\n')) ) *p = '\0';
syslog(LOG_DEBUG, MODPREFIX "%s -> %s", name, mapent);
return ctxt->parse->parse_mount(root,name,name_len,mapent,
ctxt->parse->context);
}
int lookup_done(void *context)
{
struct lookup_context *ctxt = (struct lookup_context *) context;
int rv = close_parse(ctxt->parse);
free(ctxt);
return rv;
}
|