chiark / gitweb /
xacpi-simple close bug
[chiark-utils.git] / cprogs / really.c
index d6ea15bc9bc87290fa7802eba04009aff4cb6211..46db5749510169b2302bb826d694bd749be3de56 100644 (file)
@@ -1,11 +1,11 @@
 /*
  * really.c - program for gaining privilege
  *
 /*
  * really.c - program for gaining privilege
  *
- * Copyright (C) 1992-3 Ian Jackson <iwj10@cus.cam.ac.uk>
+ * Copyright (C) 1992-3 Ian Jackson <ian@davenant.greenend.org.uk>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2,
+ * published by the Free Software Foundation; either version 3,
  * or (at your option) any later version.
  *
  * This is distributed in the hope that it will be useful, but
  * or (at your option) any later version.
  *
  * This is distributed in the hope that it will be useful, but
@@ -14,8 +14,9 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public
- * License along with this file; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * License along with this file; if not, consult the Free Software
+ * Foundation's website at www.fsf.org, or the GNU Project website at
+ * www.gnu.org.
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
@@ -98,28 +99,30 @@ static const struct cmdinfo cmdinfos[]= {
 };
 
 #ifdef REALLY_CHECK_FILE
 };
 
 #ifdef REALLY_CHECK_FILE
-static void checkroot(void) {
+static int checkroot(void) {
   int r;
   r= access(REALLY_CHECK_FILE,W_OK);
   int r;
   r= access(REALLY_CHECK_FILE,W_OK);
-  if (r) { perror("sorry"); exit(-1); }
+  if (r) return -1;
+  return 0;
 }
 #endif
 #ifdef REALLY_CHECK_GID
 }
 #endif
 #ifdef REALLY_CHECK_GID
-static void checkroot(void) {
+static int checkroot(void) {
   gid_t groups[512];
   int r, i;
 
   gid_t groups[512];
   int r, i;
 
-  r= getgid(); if (r==REALLY_CHECK_GID) return;
+  r= getgid(); if (r==REALLY_CHECK_GID) return 0;
   if (r<0) { perror("getgid check"); exit(-1); }
   r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
   if (r<0) { perror("getgroups check"); exit(-1); }
   for (i=0; i<r; i++)
   if (r<0) { perror("getgid check"); exit(-1); }
   r= getgroups(sizeof(groups)/sizeof(groups[0]),groups);
   if (r<0) { perror("getgroups check"); exit(-1); }
   for (i=0; i<r; i++)
-    if (groups[i] == REALLY_CHECK_GID) return;
-  fputs("sorry\n",stderr); exit(-1);
+    if (groups[i] == REALLY_CHECK_GID) return 0;
+  return -1;
 }
 #endif
 #ifdef REALLY_CHECK_NONE
 }
 #endif
 #ifdef REALLY_CHECK_NONE
-static void checkroot(void) {
+static int checkroot(void) {
+  return 0;
 }
 #endif
 
 }
 #endif
 
@@ -129,7 +132,8 @@ int main(int argc, const char *const *argv) {
   int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
   const char *cp;
   
   int i, j, ngroups, ngroups_in, maingid, orgmaingid, mainuid, orgmainuid, r;
   const char *cp;
   
-  checkroot();
+  orgmainuid= getuid();
+  if (orgmainuid && checkroot()) { perror("sorry"); exit(-1); }
   myopt(&argv,cmdinfos);
 
   if (opt_groupsclear && !opt_ngids)
   myopt(&argv,cmdinfos);
 
   if (opt_groupsclear && !opt_ngids)
@@ -154,7 +158,6 @@ int main(int argc, const char *const *argv) {
     if (chroot(opt_chroot)) { perror("chroot failed"); exit(-1); }
   }
   orgmaingid= getgid();
     if (chroot(opt_chroot)) { perror("chroot failed"); exit(-1); }
   }
   orgmaingid= getgid();
-  orgmainuid= getuid();
   if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
   if (opt_user) {
     r= initgroups(opt_user,pw->pw_gid);
   if (orgmaingid<0) { perror("getgid failed"); exit(-1); }
   if (opt_user) {
     r= initgroups(opt_user,pw->pw_gid);