1 /************************************************************************
2 * setid executes commands under a different *
3 * uid/gid (can only be executed by root) *
4 * This program is used by the SmartList installation script only. *
5 ************************************************************************/
6 /*$Id: setid.c,v 1.12 2000/09/28 01:23:39 guenther Exp $*/
7 #include "includes.h"
8
9 #define CHECK_FILE "install.sh"
10
main(argc,argv)11 int main(argc,argv)const int argc;const char*const argv[];
12 { struct passwd*p;char*nargv[2];
13 if(argc!=2&&argc!=3||geteuid()||!(p=getpwnam(argv[1])))
14 { fprintf(stderr,"Usage: setid user [directory]\n");
15 return EX_USAGE;
16 }
17 endpwent();
18 if(initgroups(argv[1],p->pw_gid)||setgid(p->pw_gid)||setuid(p->pw_uid))
19 return EX_OSERR;
20 if(fopen(CHECK_FILE,"r"))
21 { struct stat stbuf;
22 if(argc==2)
23 goto nodir;
24 if(stat(argv[2],&stbuf)||(stbuf.st_mode&S_IRWXU)!=S_IRWXU)
25 fprintf(stderr,"Can't access %s, are you sure it's there?\n",argv[2]);
26 else if(stbuf.st_uid!=p->pw_uid)
27 fprintf(stderr,"%s is owned by uid %ld!=%s, please fix this first\n",
28 argv[2],(long)stbuf.st_uid,p->pw_name);
29 else if(stbuf.st_gid!=p->pw_gid)
30 fprintf(stderr,"%s is owned by gid %ld!=%ld, please fix this first\n",
31 argv[2],(long)stbuf.st_gid,(long)p->pw_gid);
32 else
33 nodir: nargv[0]=getenv("SHELL"),nargv[1]=0,execv(nargv[0],nargv);
34 }
35 else
36 fprintf(stderr,
37 "Please make sure %s can read & access the source tree\n",argv[1]);
38 return EX_UNAVAILABLE;
39 }
40