/*
 * module.c
 *
 * Common module-management functions
 */

#include <stdio.h>
#include <syslog.h>
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include "automount.h"

struct lookup_mod *open_lookup(char *name, char *err_prefix,
			       char *mapfmt, int argc, char **argv)
{
  struct lookup_mod *mod;
  char *fnbuf;
  void *dh;
  int *ver;

  mod = malloc(sizeof(struct lookup_mod));
  if ( !mod ) {
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  fnbuf = malloc(strlen(name) + strlen(AUTOFS_LIB_DIR) + 13);
  if ( !fnbuf ) {
    free(mod);
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  sprintf(fnbuf, "%s//lookup_%s.so", AUTOFS_LIB_DIR, name);
  
  if ( !(dh = dlopen(fnbuf, RTLD_NOW)) ) {
    syslog(LOG_CRIT, "%scannot open lookup module %s", err_prefix, name);
    free(mod);
    free(fnbuf);
    return NULL;
  }
  free(fnbuf);

  if ( !(ver = (int *) dlsym(dh, "lookup_version"))
       || *ver != AUTOFS_LOOKUP_VERSION ) {
    syslog(LOG_CRIT, "%slookup module %s version mismatch", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( !(mod->lookup_init = (lookup_init_t) dlsym(dh, "lookup_init")) ||
       !(mod->lookup_mount = (lookup_mount_t) dlsym(dh, "lookup_mount")) ||
       !(mod->lookup_done = (lookup_done_t) dlsym(dh, "lookup_done")) ) {
    syslog(LOG_CRIT, "%slookup module %s corrupt", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( mod->lookup_init(mapfmt,argc,argv,&mod->context) ) {
    dlclose(dh);
    free(mod);
    return NULL;
  }
  mod->dlhandle = dh;
  return mod;
}

int close_lookup(struct lookup_mod *mod)
{
  int rv = mod->lookup_done(mod->context);
  dlclose(mod->dlhandle);
  free(mod);
  return rv;
}

struct parse_mod *open_parse(char *name, char *err_prefix,
			      int argc, char **argv)
{
  struct parse_mod *mod;
  char *fnbuf;
  void *dh;
  int *ver;

  mod = malloc(sizeof(struct parse_mod));
  if ( !mod ) {
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  fnbuf = malloc(strlen(name) + strlen(AUTOFS_LIB_DIR) + 12);
  if ( !fnbuf ) {
    free(mod);
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  sprintf(fnbuf, "%s//parse_%s.so", AUTOFS_LIB_DIR, name);
  
  if ( !(dh = dlopen(fnbuf, RTLD_NOW)) ) {
    syslog(LOG_CRIT, "%scannot open parse module %s", err_prefix, name);
    free(mod);
    free(fnbuf);
    return NULL;
  }
  free(fnbuf);

  if ( !(ver = (int *) dlsym(dh, "parse_version"))
       || *ver != AUTOFS_PARSE_VERSION ) {
    syslog(LOG_CRIT, "%sparse module %s version mismatch", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( !(mod->parse_init = (parse_init_t) dlsym(dh, "parse_init")) ||
       !(mod->parse_mount = (parse_mount_t) dlsym(dh, "parse_mount")) ||
       !(mod->parse_done = (parse_done_t) dlsym(dh, "parse_done")) ) {
    syslog(LOG_CRIT, "%sparse module %s corrupt", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( mod->parse_init(argc,argv,&mod->context) ) {
    dlclose(dh);
    free(mod);
    return NULL;
  }
  mod->dlhandle = dh;
  return mod;
}

int close_parse(struct parse_mod *mod)
{
  int rv = mod->parse_done(mod->context);
  dlclose(mod->dlhandle);
  free(mod);
  return rv;
}

struct mount_mod *open_mount(char *name, char *err_prefix)
{
  struct mount_mod *mod;
  char *fnbuf;
  void *dh;
  int *ver;

  mod = malloc(sizeof(struct mount_mod));
  if ( !mod ) {
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  fnbuf = malloc(strlen(name) + strlen(AUTOFS_LIB_DIR) + 12);
  if ( !fnbuf ) {
    free(mod);
    syslog(LOG_CRIT, "%s%m", err_prefix);
    return NULL;
  }
  sprintf(fnbuf, "%s//mount_%s.so", AUTOFS_LIB_DIR, name);
  
  if ( !(dh = dlopen(fnbuf, RTLD_NOW)) ) {
    syslog(LOG_CRIT, "%scannot open mount module %s", err_prefix, name);
    free(mod);
    free(fnbuf);
    return NULL;
  }
  free(fnbuf);

  if ( !(ver = (int *) dlsym(dh, "mount_version"))
       || *ver != AUTOFS_MOUNT_VERSION ) {
    syslog(LOG_CRIT, "%smount module %s version mismatch", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( !(mod->mount_init = (mount_init_t) dlsym(dh, "mount_init")) ||
       !(mod->mount_mount = (mount_mount_t) dlsym(dh, "mount_mount")) ||
       !(mod->mount_done = (mount_done_t) dlsym(dh, "mount_done")) ) {
    syslog(LOG_CRIT, "%smount module %s corrupt", err_prefix, name);
    dlclose(dh);
    free(mod);
    return NULL;
  }

  if ( mod->mount_init(&mod->context) ) {
    dlclose(dh);
    free(mod);
    return NULL;
  }
  mod->dlhandle = dh;
  return mod;
}

int close_mount(struct mount_mod *mod)
{
  int rv = mod->mount_done(mod->context);
  dlclose(mod->dlhandle);
  free(mod);
  return rv;
}

