1117259Sache/*
2117259Sache * Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
3117259Sache *	All rights reserved.
4117259Sache *
5117259Sache * By using this file, you agree to the terms and conditions set
6117259Sache * forth in the LICENSE file which can be found at the top level of
7117259Sache * the sendmail distribution.
8117259Sache *
9174990Sache */
10117259Sache
11117259Sache/*
12117259Sache**  This program checks to see if your version of setgid works.
13117259Sache**  Compile it, make it set-group-ID guest, and run it as yourself (NOT as
14117259Sache**  root and not as member of the group guest).
15117259Sache**
16117259Sache**  Compilation is trivial -- just "cc t_setgid.c".  Make it set-group-ID,
17117259Sache**  guest and then execute it as a non-root user.
18117259Sache*/
19117259Sache
20117259Sache#include <sys/types.h>
21117259Sache#include <unistd.h>
22117259Sache#include <stdio.h>
23117259Sache
24174990Sache#ifndef lint
25117259Sachestatic char id[] = "@(#)$Id: t_setgid.c,v 1.7 2013-11-22 20:52:01 ca Exp $";
26117259Sache#endif /* ! lint */
27117259Sache
28117259Sachestatic void
29117259Sacheprintgids(str, r, e)
30117259Sache	char *str;
31117259Sache	gid_t r, e;
32117259Sache{
33117259Sache	printf("%s (should be %d/%d): r/egid=%d/%d\n", str, (int) r, (int) e,
34117259Sache	       (int) getgid(), (int) getegid());
35117259Sache}
36117259Sache
37117259Sacheint
38117259Sachemain(argc, argv)
39174990Sache	int argc;
40117259Sache	char **argv;
41117259Sache{
42117259Sache	int fail = 0;
43117259Sache	int res;
44117259Sache	gid_t realgid = getgid();
45117259Sache	gid_t effgid = getegid();
46117259Sache
47117259Sache	printgids("initial gids", realgid, effgid);
48117259Sache
49174990Sache	if (effgid == realgid)
50117259Sache	{
51117259Sache		printf("SETUP ERROR: re-run set-group-ID guest\n");
52117259Sache		exit(1);
53117259Sache	}
54117259Sache
55117259Sache#if SM_CONF_SETREGID
56117259Sache	res = setregid(effgid, effgid);
57117259Sache#else /* SM_CONF_SETREGID */
58117259Sache	res = setgid(effgid);
59117259Sache#endif /* SM_CONF_SETREGID */
60117259Sache
61117259Sache	printf("setgid(%d)=%d %s\n", (int) effgid, res,
62117259Sache		res < 0 ? "failure" : "ok");
63117259Sache#if SM_CONF_SETREGID
64117259Sache	printgids("after setregid()", effgid, effgid);
65117259Sache#else /* SM_CONF_SETREGID */
66117259Sache	printgids("after setgid()", effgid, effgid);
67117259Sache#endif /* SM_CONF_SETREGID */
68117259Sache
69117259Sache	if (getegid() != effgid)
70117259Sache	{
71117259Sache		fail++;
72117259Sache		printf("MAYDAY!  Wrong effective gid\n");
73117259Sache	}
74117259Sache
75117259Sache	if (getgid() != effgid)
76117259Sache	{
77117259Sache		fail++;
78117259Sache		printf("MAYDAY!  Wrong real gid\n");
79117259Sache	}
80117259Sache
81117259Sache	/* do activity here */
82117259Sache	if (setgid(0) == 0)
83174990Sache	{
84117259Sache		fail++;
85117259Sache		printf("MAYDAY!  setgid(0) succeeded (should have failed)\n");
86117259Sache	}
87117259Sache	else
88117259Sache	{
89117259Sache		printf("setgid(0) failed (this is correct)\n");
90117259Sache	}
91117259Sache	printgids("after setgid(0)", effgid, effgid);
92117259Sache
93117259Sache	if (getegid() != effgid)
94117259Sache	{
95117259Sache		fail++;
96117259Sache		printf("MAYDAY!  Wrong effective gid\n");
97117259Sache	}
98117259Sache	if (getgid() != effgid)
99117259Sache	{
100117259Sache		fail++;
101117259Sache		printf("MAYDAY!  Wrong real gid\n");
102117259Sache	}
103117259Sache	printf("\n");
104117259Sache
105117259Sache	if (fail > 0)
106	{
107		printf("\nThis system cannot use %s to set the real gid to the effective gid\nand clear the saved gid.\n",
108#if SM_CONF_SETREGID
109			"setregid"
110#else /* SM_CONF_SETREGID */
111			"setgid"
112#endif /* SM_CONF_SETREGID */
113			);
114		exit(1);
115	}
116
117	printf("\nIt is possible to use setgid on this system\n");
118	exit(0);
119}
120