File: parameters.c

package info (click to toggle)
sn 0.3.8-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 976 kB
  • sloc: ansic: 9,255; sh: 467; makefile: 210
file content (62 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (6)
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
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright � 1998-2000 Harold Tay.
 * Copyright � 2000- Patrik R�dman.
 */

#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <out.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include "config.h"

static const char ver_ctrl_id[] = "$Id: parameters.c 29 2004-04-24 23:02:38Z patrik $";

char *snroot = SNROOT;
uid_t snuid;
gid_t sngid;

void parameters (bool wantwriteperms)
{
   struct stat st;

   if (!(snroot = getenv("SNROOT")))
      snroot = SNROOT;

   if (-1 == stat(snroot, &st))
      fail(2, "Can't find spool directory \"%s\":%m", snroot);
   if (!S_ISDIR(st.st_mode))
      fail(2, "\"%s\" is not a directory", snroot);
   snuid = st.st_uid;
   sngid = st.st_gid;
   if (0 == geteuid())
   {
      setgid(sngid);
      setuid(snuid);
   }

   if (wantwriteperms)
      if (snuid != geteuid() || sngid != getegid())
      {
         struct passwd *pwuid;
         struct group *grgid;
         char euid_name[40], egid_name[40];
         char snuid_name[40], sngid_name[40];
         
         pwuid = getpwuid(geteuid());
         grgid = getgrgid(getegid());
         strncpy(euid_name, (pwuid ? pwuid->pw_name : "(unknown)"), 40);
         strncpy(egid_name, (grgid ? grgid->gr_name : "(unknown)"), 40);
         pwuid = getpwuid(snuid);
         grgid = getgrgid(sngid);
         strncpy(snuid_name, (pwuid ? pwuid->pw_name : "(unknown)"), 40);
         strncpy(sngid_name, (grgid ? grgid->gr_name : "(unknown)"), 40);
         
         fail(2, "Can't write in spool directory \"%s\" (I am %s:%s, must be %s:%s)",
              snroot, euid_name, egid_name, snuid_name, sngid_name);
      }
}