2 * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 ** This program checks to see if your version of setgid works.
13 ** Compile it, make it set-group-ID guest, and run it as yourself (NOT as
14 ** root and not as member of the group guest).
16 ** Compilation is trivial -- just "cc t_dropgid.c". Make it set-group-ID
17 ** guest and then execute it as a non-root user.
20 #include <sys/types.h>
25 static char id[] = "@(#)$Id: t_dropgid.c,v 1.6 2001/09/28 16:36:28 ca Exp $";
33 printf("%s (should be %d/%d): r/egid=%d/%d\n", str, (int) r, (int) e,
34 (int) getgid(), (int) getegid());
37 /* define only one of these */
39 # define SETGIDCALL "setegid"
40 #endif /* HASSETEGID */
42 # define SETGIDCALL "setregid"
43 #endif /* HASSETREGID */
45 # define SETGIDCALL "setresgid"
46 #endif /* HASSETRESGID */
49 # define SETGIDCALL "setgid"
50 #endif /* ! SETGIDCALL */
59 gid_t realgid = getgid();
60 gid_t effgid = getegid();
63 printgids("initial gids", realgid, effgid);
65 if (effgid == realgid)
67 printf("SETUP ERROR: re-run set-group-ID guest\n");
68 printf("Use chgrp(1) and chmod(1)\n");
69 printf("For example, do this as root ");
70 printf("(nobody is the name of a group in this example):\n");
71 printf("# chgrp nobody %s\n", prg);
72 printf("# chmod g+s nobody %s\n", prg);
77 res = setregid(realgid, realgid);
78 printf("setregid(%d)=%d %s\n", (int) realgid, res,
79 res < 0 ? "failure" : "ok");
80 printgids("after setregid()", realgid, realgid);
81 #endif /* HASSETREGID */
83 res = setresgid(realgid, realgid, realgid);
84 printf("setresgid(%d)=%d %s\n", (int) realgid, res,
85 res < 0 ? "failure" : "ok");
86 printgids("after setresgid()", realgid, realgid);
87 #endif /* HASSETRESGID */
89 res = setegid(realgid);
90 printf("setegid(%d)=%d %s\n", (int) realgid, res,
91 res < 0 ? "failure" : "ok");
92 printgids("after setegid()", realgid, realgid);
93 #endif /* HASSETEGID */
94 res = setgid(realgid);
95 printf("setgid(%d)=%d %s\n", (int) realgid, res,
96 res < 0 ? "failure" : "ok");
97 printgids("after setgid()", realgid, realgid);
99 if (getegid() != realgid)
102 printf("MAYDAY! Wrong effective gid\n");
105 if (getgid() != realgid)
108 printf("MAYDAY! Wrong real gid\n");
111 /* do activity here */
112 if (setgid(effgid) == 0)
115 printf("MAYDAY! setgid(%d) succeeded (should have failed)\n",
120 printf("setgid(%d) failed (this is correct)\n", effgid);
122 printgids("after setgid() to egid", realgid, realgid);
124 if (getegid() != realgid)
127 printf("MAYDAY! Wrong effective gid\n");
129 if (getgid() != realgid)
132 printf("MAYDAY! Wrong real gid\n");
138 printf("\nThis system cannot use %s to give up set-group-ID rights\n",
141 printf("Maybe compile with -DHASSETEGID and try again\n");
142 #endif /* !HASSETEGID */
144 printf("Maybe compile with -DHASSETREGID and try again\n");
145 #endif /* !HASSETREGID */
147 printf("Maybe compile with -DHASSETRESGID and try again\n");
148 #endif /* !HASSETRESGID */
152 printf("\nIt is possible to use %s on this system\n", SETGIDCALL);