summaryrefslogtreecommitdiffabout
path: root/src/userprivs.c
blob: 9f9724fd45813149d48ce72333b733d6ec23aeff (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* This file is part of GNU Pies.
   Copyright (C) 2007-2011, 2013, 2016 Sergey Poznyakoff

   GNU Pies 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 3, or (at your option)
   any later version.

   GNU Pies is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <sysexits.h>
#include <stdbool.h>
#include "pies.h"

struct grecs_list *
get_user_groups (struct grecs_list *init_list, const char *user)
{
  int rc;
  struct group *gr;
  struct grecs_list *list;

  list = grecs_list_create ();
  
  if (init_list)
    {
      struct grecs_list_entry *ep;

      for (ep = init_list->head; ep; ep = ep->next)
	{
	  grecs_list_append (list, grecs_strdup ((char*)ep->data));
	}
    }

  setgrent ();
  for (rc = 0; rc == 0 && (gr = getgrent ());)
    {
      char **p;
      for (p = gr->gr_mem; *p; p++)
	if (strcmp (*p, user) == 0)
	  {
	    grecs_list_append (list, grecs_strdup (gr->gr_name));
	    break;
	  }
    }
  endgrent ();
  return list;
}

/* Switch to the given UID/GID */
int
switch_to_privs (uid_t uid, gid_t gid, struct grecs_list *retain_groups)
{
  int rc = 0;
  gid_t *emptygidset;
  size_t size = 1, j = 1;

  /* Create a list of supplementary groups */
  size = 1 + (retain_groups ? grecs_list_size (retain_groups) : 0);
  emptygidset = grecs_calloc (size, sizeof emptygidset[0]);
  emptygidset[0] = gid ? gid : getegid ();

  if (retain_groups)
    {
      struct grecs_list_entry *ep;

      for (ep = retain_groups->head; ep; ep = ep->next)
	{
	  const char *grname = ep->data;
	  struct group *group = getgrnam (grname);
	  if (!group)
	    {
	      logmsg (LOG_ERR, _("unknown group: %s"), grname);
	      free (emptygidset);
	      return 1;
	    }
	  emptygidset[j++] = group->gr_gid;
	}
    }

  /* Reset group permissions */
  if (geteuid () == 0 && setgroups (j, emptygidset))
    {
      logmsg (LOG_ERR, _("setgroups(1, %lu) failed: %s"),
	      (unsigned long) emptygidset[0], strerror (errno));
      rc = 1;
    }
  grecs_free (emptygidset);

  /* Switch to the user's gid. On some OSes the effective gid must
     be reset first */

#if defined(HAVE_SETEGID)
  if ((rc = setegid (gid)) < 0)
    logmsg (LOG_ERR, _("setegid(%lu) failed: %s"),
	      (unsigned long) gid, strerror (errno));
#elif defined(HAVE_SETREGID)
  if ((rc = setregid (gid, gid)) < 0)
    logmsg (LOG_ERR, _("setregid(%lu,%lu) failed: %s"),
	      (unsigned long) gid, (unsigned long) gid, strerror (errno));
#elif defined(HAVE_SETRESGID)
  if ((rc = setresgid (gid, gid, gid)) < 0)
    logmsg (LOG_ERR, _("setresgid(%lu,%lu,%lu) failed: %s"),
	      (unsigned long) gid,
	      (unsigned long) gid, (unsigned long) gid, strerror (errno));
#endif

  if (rc == 0 && gid != 0)
    {
      if ((rc = setgid (gid)) < 0 && getegid () != gid)
	logmsg (LOG_ERR, _("setgid(%lu) failed: %s"),
		  (unsigned long) gid, strerror (errno));
      if (rc == 0 && getegid () != gid)
	{
	  logmsg (LOG_ERR, _("cannot set effective gid to %lu"),
		    (unsigned long) gid);
	  rc = 1;
	}
    }

  /* Now reset uid */
  if (rc == 0 && uid != 0)
    {
      uid_t euid;

      if (setuid (uid)
	  || geteuid () != uid
	  || (getuid () != uid && (geteuid () == 0 || getuid () == 0)))
	{

#if defined(HAVE_SETREUID)
	  if (geteuid () != uid)
	    {
	      if (setreuid (uid, -1) < 0)
		{
		  logmsg (LOG_ERR, _("setreuid(%lu,-1) failed: %s"),
			    (unsigned long) uid, strerror (errno));
		  rc = 1;
		}
	      if (setuid (uid) < 0)
		{
		  logmsg (LOG_ERR, _("second setuid(%lu) failed: %s"),
			    (unsigned long) uid, strerror (errno));
		  rc = 1;
		}
	    }
	  else
#endif
	    {
	      logmsg (LOG_ERR, _("setuid(%lu) failed: %s"),
			(unsigned long) uid, strerror (errno));
	      rc = 1;
	    }
	}

      euid = geteuid ();
      if (uid != 0 && setuid (0) == 0)
	{
	  logmsg (LOG_ERR, _("seteuid(0) succeeded when it should not"));
	  rc = 1;
	}
      else if (uid != euid && setuid (euid) == 0)
	{
	  logmsg (LOG_ERR, _("cannot drop non-root setuid privileges"));
	  rc = 1;
	}

    }

  return rc;
}


void
pies_priv_setup (struct pies_privs *privs)
{
  struct passwd *pw;
  struct grecs_list *grplist = 0;

  if (!privs || !privs->user)
    return;

  pw = getpwnam (privs->user);
  if (!pw)
    {
      logmsg (LOG_ERR, _("no such user: %s"), privs->user);
      exit (EX_CONFIG);
    }

  if (privs->allgroups)
    grplist = get_user_groups (privs->groups, privs->user);
  if (switch_to_privs (pw->pw_uid, pw->pw_gid,
		       grplist ? grplist : privs->groups))
    exit (EX_SOFTWARE);
  if (grplist)
    grecs_list_free (grplist);
}


void
pies_epriv_setup (struct pies_privs *privs)
{
  uid_t uid;
  gid_t gid;

  if (privs)
    {
      struct passwd *pw;
      if (!privs->user)
	return;

      pw = getpwnam (privs->user);
      if (!pw)
	{
	  logmsg (LOG_ERR, _("no such user: %s"), privs->user);
	  exit (EX_CONFIG);
	}
      uid = pw->pw_uid;
      gid = pw->pw_gid;
    }
  else
    {
      uid = 0;
      gid = 0;
    }

  if (setegid (gid))
    {
      logmsg (LOG_ERR, _("cannot switch to EGID %lu: %s"),
		(unsigned long) gid, strerror (errno));
      exit (EX_USAGE);
    }
  if (seteuid (uid))
    {
      logmsg (LOG_ERR, _("cannot switch to EUID %lu: %s"),
	      (unsigned long) uid, strerror (errno));
      exit (EX_USAGE);
    }
}

int
pies_privs_cmp (struct pies_privs const *a, struct pies_privs const *b)
{
  if (!a)
    return !!b;
  else if (!b)
    return 1;
  if (safe_strcmp (a->user, b->user))
    return 1;
  if (a->allgroups != b->allgroups)
    return 1;
  return grecs_list_compare (a->groups, b->groups);
}

void
pies_privs_free (struct pies_privs *p)
{
  grecs_free (p->user);
  grecs_list_free (p->groups);
}

  

Return to:

Send suggestions and report system problems to the System administrator.