/* cfgroupid.c */

/* Copyright 1997-2000 by Eberhard Mattes <em-gw@windhager.de>
   Donated to the public domain.  No warranty.

   1997-01-04 Initial version
   1997-04-05 Reorganize library
   1997-07-24 Add prototype for mapgid()
   2000-04-29 set_userid_groupid()
   2000-05-27 Use config_arg_count() */

#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include "firewall.h"
#include "libemfw.h"

/* firewall.h lacks some declarations. */

extern int mapgid (char *);

int libemfw_gid = -1;

int config_groupid (Cfg *confp, int do_set)
{
  Cfg *cf;
  int gid;

  if ((cf = cfg_get ("groupid", confp)) == (Cfg *)0)
    return -1;
  config_arg_count (cf, 1);
  if ((gid = mapgid (cf->argv[0])) == -1)
    {
      syslog (LLEV, "fwtkcfgerr: cannot map %s to gid", cf->argv[0]);
      exit(1);
    }
  if (!do_set)
    libemfw_gid = gid;	/* do_fastdaemon() and set_userid_groupid() */
  else if (setgid (gid) != 0)
    {
      syslog (LLEV, "fwtksyserr: cannot setgid %d: %s", gid, strerror(errno));
      exit (1);
    }
  return gid;
}
