File: setid.c

package info (click to toggle)
procmail 3.22-24
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,104 kB
  • sloc: ansic: 9,885; sh: 1,957; makefile: 132
file content (39 lines) | stat: -rw-r--r-- 1,508 bytes parent folder | download | duplicates (10)
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
/************************************************************************
 *	setid		executes commands under a different		*
 *			uid/gid (can only be executed by root)		*
 *	This program is used by the SmartList installation script only. *
 ************************************************************************/
/*$Id: setid.c,v 1.12 2000/09/28 01:23:39 guenther Exp $*/
#include "includes.h"

#define CHECK_FILE	"install.sh"

int main(argc,argv)const int argc;const char*const argv[];
{ struct passwd*p;char*nargv[2];
  if(argc!=2&&argc!=3||geteuid()||!(p=getpwnam(argv[1])))
   { fprintf(stderr,"Usage: setid user [directory]\n");
     return EX_USAGE;
   }
  endpwent();
  if(initgroups(argv[1],p->pw_gid)||setgid(p->pw_gid)||setuid(p->pw_uid))
     return EX_OSERR;
  if(fopen(CHECK_FILE,"r"))
   { struct stat stbuf;
     if(argc==2)
	goto nodir;
     if(stat(argv[2],&stbuf)||(stbuf.st_mode&S_IRWXU)!=S_IRWXU)
	fprintf(stderr,"Can't access %s, are you sure it's there?\n",argv[2]);
     else if(stbuf.st_uid!=p->pw_uid)
	fprintf(stderr,"%s is owned by uid %ld!=%s, please fix this first\n",
	 argv[2],(long)stbuf.st_uid,p->pw_name);
     else if(stbuf.st_gid!=p->pw_gid)
	fprintf(stderr,"%s is owned by gid %ld!=%ld, please fix this first\n",
	 argv[2],(long)stbuf.st_gid,(long)p->pw_gid);
     else
nodir:	nargv[0]=getenv("SHELL"),nargv[1]=0,execv(nargv[0],nargv);
   }
  else
     fprintf(stderr,
      "Please make sure %s can read & access the source tree\n",argv[1]);
  return EX_UNAVAILABLE;
}