File: setreuid.c

package info (click to toggle)
zmailer 2.99.55-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,516 kB
  • ctags: 9,694
  • sloc: ansic: 120,953; sh: 3,862; makefile: 3,166; perl: 2,695; python: 115; awk: 22
file content (277 lines) | stat: -rw-r--r-- 7,268 bytes parent folder | download | duplicates (2)
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
276
277
/*
 * Several 'setreuid()' replacements for those systems that
 * don't have it, or have it under different names...
 */
#include "hostenv.h"

/*
 * From Ross Ridge's Xenix port:
 * - setreuid, setreuid, *sigh* this going to a big mondo problem porting
 *   Zmailer to a box without it or SysVR3's saved set-user ID.  Xenix is
 *   one of these beasties, so I resorted to a desperate hack: I wrote
 *   setreuid function that opens /dev/kmem and fiddles with the u area.
 */

/*
 * Simpleton ports of HPUX  setresuid() to setreuid() replacement..
 * Though maybe it is better to be done on macrolevel ?
 * This REQUIRES  "SETREUID" in the  hostenv/HPUX8 -file!
 */

/* Several tricks for IBM AIX for Zmailer
   by Matti Aarnio <mea@utu.fi> <mea@nic.funet.fi>

   Mostly these are pulled in from various sources :)

   On your AIX machine, see:

      /usr/lpp/bos/bsdport(.tr)

   file for the information about how to do the porting of
   BSD-oriented software into AIX systems.
   (A PostScript rendered version is on Zmailers  doc/aix-bsdport.ps)

*/
#if (defined(AIX) || defined(_AIX)) && !defined(_AIX41)

#include <sys/types.h>
#include <sys/param.h>
#include <sys/id.h>
#include <sys/priv.h>


int
setreuid(ruid,euid)
uid_t ruid, euid;
{
	/* Pulled in from WUARCHIVE's FTPD source.. */

	/* AIX 3 lossage.  Don't ask.  It's undocumented.  */
	/* [mea]: WU-people did err, it IS documented; AIX is just
	          a bit peculiar.. */
	priv_t priv;

	priv.pv_priv[0] = 0;
	priv.pv_priv[1] = 0;
	setgroups(NULL, NULL);

	if (setpriv(PRIV_SET|PRIV_INHERITED|PRIV_EFFECTIVE|PRIV_BEQUEATH,
		    &priv, sizeof(priv_t)) < 0)
	  return -1;

	if (ruid != (uid_t)-1)
	  if (setuidx(ID_REAL|ID_EFFECTIVE, ruid) < 0)
	    return -1;

	if (euid != (uid_t)-1)
	  if (seteuid(euid) < 0)
	    return -1;

#ifdef UID_DEBUG
	lreply(230, "ruid=%d, euid=%d, suid=%d, luid=%d", getuidx(ID_REAL),
	       getuidx(ID_EFFECTIVE), getuidx(ID_SAVED), getuidx(ID_LOGIN));
	lreply(230, "rgid=%d, egid=%d, sgid=%d, lgid=%d", getgidx(ID_REAL),
	       getgidx(ID_EFFECTIVE), getgidx(ID_SAVED), getgidx(ID_LOGIN));
#endif
	return 0;
}

#else /* Not AIX */
#ifdef HAVE_SETREUID /* AIX has a sort of setreuid() at its libc, however
			it does not work... All others either have it, or
			don't, and need emulation: */
/* static int dummy = 0; */
#else /* .. else need emulation */

#ifdef HAVE_SETEUID	/* Pure SysVR4 ? */
int setreuid(ruid, euid)
     uid_t ruid, euid;
{
	/* THIS IS NOT PURE IMPLEMENTATION! */
	/* INTENTION IS TO PROVIDE SOMETHING WORKABLE
	   WITHOUT TOO COMPLEX A PROGRAM... */

	int rc = 0;
	if (euid >= 0) {
	  rc = seteuid(euid);
	  if (ruid < 0)
	    return rc;
	}
	if (ruid >= 0)
	  return setuid(ruid);
	return rc;
}
#else

#ifdef	__hpux	/* HP-UX ?? */
#include <sys/types.h>

int
setreuid(ruid, euid)
uid_t ruid, euid;
{
	return setresuid(ruid,euid,(uid_t)-1);
}


#else /* Not HPUX 9.xx, (8.xx ?) */
/* Else something else funny.. */

/* We go via SysVr3 methods, which are horrible.. */

#ifdef	SYSV

/* Uhh...  We open process uarea from kernel memory, and poke it
   directly.  Seriously NOT FUN, but ...  */

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/page.h>
#include <sys/seg.h>
#include <sys/sysmacros.h>
#include <sys/user.h>

#undef TEST

#ifndef TEST
#include "libsupport.h"
#include "sysprotos.h"
#endif

static struct user uu;
static int kmem_fd = -1;

static int
read_u() {
        if (lseek(kmem_fd, (long) SPTADDR, 0) == -1) {
                perror("lseek failed!");
                return -1;
        }
        if (read(kmem_fd, &uu, sizeof uu) == -1) {
                perror("read failed!");
                return -1;
        }
        return 0;
}

static int
write_uid(int uid, int euid) {
        if (lseek(kmem_fd,
                  (char *) &uu.u_ruid - (char *) &uu + (long) SPTADDR,
                  0) == -1L) {
                perror("lseek failed!");
                return -1;
        }
        uu.u_ruid = uid;
        if (write(kmem_fd, &uu.u_ruid, sizeof uu.u_ruid) == -1) {
                perror("write failed!");
                return -1;
        }
        if (lseek(kmem_fd,
                  (char *) &uu.u_uid - (char *) &uu + (long) SPTADDR,
                  0) == -1L) {
                perror("lseek failed!");
                return -1;
        }
        uu.u_uid = euid;
        if (write(kmem_fd, &uu.u_uid, sizeof uu.u_uid) == -1) {
                perror("write failed!");
                return -1;
        }
        return 0;
}

int
setreuid(int uid, int euid) {
        int cmask;

        if (kmem_fd == -1) {
                kmem_fd = open("/dev/kmem", O_RDWR);
                if (kmem_fd == -1) {
                        return -1;
                }
                if (read_u() == -1) {
                        abort(); /* kvm IO-error ! */
                }
                cmask = umask(0);
                umask(cmask);
                if (uu.u_ruid != getuid() || uu.u_rgid != getgid()
                    || uu.u_uid != geteuid() || uu.u_gid != getegid()
                    || uu.u_cmask != cmask) {
                        fprintf(stderr, "setreuid check failed!\n");
                        abort(); /* kvm IO-error ! */
                }
                if (fcntl(kmem_fd, F_SETFD, 1) == -1) {
                        perror("fcntl failed!");
                        abort(); /* kvm IO-error ! */
                }
        }

        if (uid == -1) {
                uid = getuid();
        } else if ((unsigned) uid >= MAXUID) {
                errno = EINVAL;
                return -1;
        }

        if (euid == -1) {
                euid = geteuid();
        } else if ((unsigned) euid >= MAXUID) {
                errno = EINVAL;
                return -1;
        }

        if (write_uid(uid, euid) == -1) {
                abort(); /* kvm IO-error ! */
        }
        return 0;
}

#ifdef TEST
main() {
        kmem_fd = open("/dev/kmem", O_RDWR);
        if (kmem_fd == -1) {
                perror("open failed!");
                return 1;
        }

        printf("SPTADDR = %#lx\n", SPTADDR);

        if (read_u() == -1) {
                return 1;
        }
        printf("u_uid: %d u_gid: %d u_rgid: %d u_ruid: %d\n",
               uu.u_uid, uu.u_gid, uu.u_rgid, uu.u_ruid);
        printf("u_cmask: %#03o u_limit: %ld\n", uu.u_cmask, (long) uu.u_limit);
        printf("u_psargs: '%-0.40s'\n", uu.u_psargs);

        setreuid(0, 1);
        printf("\ngeteuid() = %d\n", geteuid());

        if (read_u() == -1) {
                return 1;
        }
        printf("u_uid: %d u_gid: %d u_rgid: %d u_ruid: %d\n",
               uu.u_uid, uu.u_gid, uu.u_rgid, uu.u_ruid);
        printf("u_cmask: %#03o u_limit: %ld\n", uu.u_cmask, (long) uu.u_limit);
        printf("u_psargs: '%-0.40s'\n", uu.u_psargs);

        setreuid(0, 0);
        printf("\ngeteuid() = %d\n", geteuid());

        close(kmem_fd);
        return 0;
}
#endif

#else /* not SYSV - 386 ? ... */
static int foovar = 0; /* some systems want something into .o -file */
#endif /* Not SYSV */
#endif /* Not SYSV, not __hpux */
#endif /* HAVE_SETEUID */
#endif /* HAVE_SETREUID */
#endif /* Not AIX either.. */