File: suidprotect.c

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (129 lines) | stat: -rw-r--r-- 3,181 bytes parent folder | download | duplicates (4)
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
/* $Id: suidprotect.c,v 1.6 2003/10/06 00:59:32 dm Exp $ */

/*
 *
 * Copyright (C) 2001 David Mazieres (dm@uun.org)
 *
 * This program 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, or (at
 * your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "sysconf.h"

int
suidsafe (void)
{
  static int safe;
  if (!safe) {
    if (suidprotect && getuid ()
#if defined (HAVE_ISSETUGID)
	     && issetugid ()
#elif defined (HAVE_GETEUID) && defined (HAVE_GETEGID)
	     && (getuid () != geteuid () || getgid () != getegid ())
#endif /* HAVE_GETEUID and HAVE_GETEGID */
	     )
      safe = -1;
    else
      safe = 1;
  }
  return safe < 0 ? 0 : 1;
}

int
execsafe (void)
{
  return (suidprotect || execprotect) ? 0 : 1;
}

char *
safegetenv (const char *name)
{
  return suidsafe () ? getenv (name) : NULL;
}

extern char **environ;

#ifndef PUTENV_COPIES_ARGUMENT
#if defined (__sun__) && defined (__svr4__)
# define BAD_PUTENV 1
#endif /* __sun__ && __svr4__ */

#if BAD_PUTENV
static char **newenvp;
static char **badenvp;
#endif /* BAD_PUTENV */

int
xputenv (const char *s)
{
  char *ss;
  int ret;

#if BAD_PUTENV
  /* Solaris appears to have a very broken putenv implementation that
   * calls realloc on memory not allocated by malloc.  The effects are
   * particularly bad when C++ global constructors call putenv before
   * that start of main.  (Perhaps some gcc initialization code fixes
   * the problem before main is invoked?)  Just to be safe, we make
   * sure environ really points to malloced memory.  */
  if (!newenvp) {
    int n;
    for (n = 0; environ[n]; n++)
      ;
    newenvp = malloc ((n + 2) * sizeof (*newenvp));
    if (!newenvp)
      return -1;
    memcpy (newenvp, environ, (n + 1) * sizeof (environ));
    badenvp = environ;
  }
  /* Somehow, Solaris stubbornly resets environ to point to crap
   * memory, so we need to keep resetting it. */
  if (environ == badenvp)
    environ = newenvp;
#endif /* BAD_PUTENV */

  /* Note:  Don't use xstrdup, because we don't always link this file
   * against libasync.  (suidconnect uses it, too) */
  ss = strdup (s);
  if (!ss)
    return -1;
  ret = putenv (ss);
  if (ret < 0)
    xfree (ss);
#if BAD_PUTENV
  else
    newenvp = environ;
#endif /* BAD_PUTENV */
  return ret;
}
#endif /* !PUTENV_COPIES_ARGUMENT */

#ifndef HAVE_UNSETENV
void
unsetenv (const char *name)
{
  int len = strlen (name);
  char **ep;
  for (ep = environ;; ep++) {
    if (!*ep)
      return;
    else if (!strncmp (name, *ep, len) && (*ep)[len] == '=')
      break;
  }
  while ((ep[0] = ep[1]))
    ep++;
}
#endif /* !HAVE_UNSETENV */