File: parameters.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (51 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download
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
/*
 * 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 Rdman.
 */

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

static const char rcsid[] = "$Id$";

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

void parameters (int wantwriteperms)
{
   struct stat st;

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

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

   if (wantwriteperms)
      if (snuid != geteuid() || sngid != getegid())
      {
         char euid_name[10], egid_name[10];

         strncpy(euid_name, (getpwuid(geteuid()))->pw_name, 10);
         strncpy(egid_name, (getgrgid(getegid()))->gr_name, 10);
         fail(2, "Can't write in %s (I am %s.%s, must be %s.%s)", snroot,
              euid_name, egid_name, (getpwuid(snuid))->pw_name, (getgrgid(sngid))->gr_name);
      }
}