-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetpwnam.c
212 lines (181 loc) · 4.99 KB
/
setpwnam.c
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
/* setpwnam.c edit an entry in a password database. */
/* because I use getpwent(), putpwent(), etc... */
#define _SVID_SOURCE
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/resource.h>
#ifdef BSD43
#include <sys/file.h>
#endif
#include <paths.h>
#include "setpwnam.h"
extern int errno;
typedef int boolean;
#define false 0
#define true 1
#define PASSWD_FILE _PATH_PASSWD
#define PTMP_FILE _PATH_PTMP
#define PTMPTMP_FILE _PATH_PTMPTMP
static int copy_pwd (struct passwd *src, struct passwd *dest);
static char *xstrdup (char *str);
static void pw_init(void);
/*
* setpwnam () --
* takes a struct passwd in which every field is filled in and valid.
* If the given username exists in the passwd file, his entry is
* replaced with the given entry.
*/
int setpwnam (struct passwd *pwd)
{
FILE *fp;
int x, save_errno, fd, ret;
struct passwd *entry;
boolean found;
struct passwd spwd;
int oldumask;
/* getpwent() returns a pointer to a static buffer.
* "pwd" might have some from getpwent(), so we have to copy it to
* some other buffer before calling getpwent().
*/
if (copy_pwd (pwd, &spwd) < 0)
return (-1);
oldumask = umask(0); /* Create with exact permissions */
pw_init();
/* sanity check */
for (x = 0; x < 3; x++) {
if (x > 0) sleep (1);
fd = open (PTMPTMP_FILE, O_WRONLY|O_CREAT, 0644);
if(fd == -1) {
perror(PTMPTMP_FILE);
umask(oldumask);
return (-1);
}
ret = link(PTMPTMP_FILE, PTMP_FILE);
unlink(PTMPTMP_FILE);
if(ret == -1)
close(fd);
else
break;
}
umask(oldumask);
if (ret == -1) return (-1);
/* ptmp should be owned by root.root or root.wheel */
if (chown (PTMP_FILE, (uid_t) 0, (gid_t) 0) < 0)
perror ("chown");
/* open ptmp for writing and passwd for reading */
fp = fdopen (fd, "w");
if (! fp) goto fail;
setpwent ();
/* parse the passwd file */
found = false;
while ((entry = getpwent ()) != NULL) {
if (! strcmp (spwd.pw_name, entry->pw_name)) {
entry = &spwd;
found = true;
}
if (putpwent (entry, fp) < 0) goto fail;
}
if (fclose (fp) < 0) goto fail;
close (fd);
endpwent ();
if (! found) {
errno = ENOENT; /* give me something better */
goto fail;
}
/* we don't care if we can't remove the backup file */
unlink (PASSWD_FILE".OLD");
/* we don't care if we can't create the backup file */
link (PASSWD_FILE, PASSWD_FILE".OLD");
/* we DO care if we can't rename to the passwd file */
if (rename (PTMP_FILE, PASSWD_FILE) < 0)
goto fail;
/* finally: success */
return 0;
fail:
save_errno = errno;
if (fp) fclose (fp);
if (fd >= 0) close (fd);
endpwent ();
unlink (PTMP_FILE);
errno = save_errno;
return (-1);
}
#define memzero(ptr, size) memset((char *) ptr, 0, size)
static int failed;
static int copy_pwd (struct passwd *src, struct passwd *dest)
{
/* this routine destroys abstraction barriers. it's not portable
* across systems, or even across different versions of the C library
* on a given system. it's dangerous and evil and wrong and I dispise
* getpwent() for forcing me to write this.
*/
failed = 0;
memzero (dest, sizeof (struct passwd));
dest->pw_name = xstrdup (src->pw_name);
dest->pw_passwd = xstrdup (src->pw_passwd);
dest->pw_uid = src->pw_uid;
dest->pw_gid = src->pw_gid;
dest->pw_gecos = xstrdup (src->pw_gecos);
dest->pw_dir = xstrdup (src->pw_dir);
dest->pw_shell = xstrdup (src->pw_shell);
return (failed);
}
static char *xstrdup (char *str)
{
char *dup;
if (! str)
return NULL;
dup = (char *) malloc (strlen (str) + 1);
if (! dup) {
failed = -1;
return NULL;
}
strcpy (dup, str);
return dup;
}
#ifdef NO_PUTPWENT
int putpwent (const struct passwd *p, FILE *stream)
{
if (p == NULL || stream == NULL) {
errno = EINVAL;
return (-1);
}
if (fprintf (stream, "%s:%s:%u:%u:%s:%s:%s\n",
p->pw_name, p->pw_passwd, p->pw_uid, p->pw_gid,
p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
return (-1);
return(0);
}
#endif
static void
pw_init()
{
struct rlimit rlim;
/* Unlimited resource limits. */
rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
(void)setrlimit(RLIMIT_CPU, &rlim);
(void)setrlimit(RLIMIT_FSIZE, &rlim);
(void)setrlimit(RLIMIT_STACK, &rlim);
(void)setrlimit(RLIMIT_DATA, &rlim);
(void)setrlimit(RLIMIT_RSS, &rlim);
/* Don't drop core (not really necessary, but GP's). */
rlim.rlim_cur = rlim.rlim_max = 0;
(void)setrlimit(RLIMIT_CORE, &rlim);
/* Turn off signals. */
(void)signal(SIGALRM, SIG_IGN);
(void)signal(SIGHUP, SIG_IGN);
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGPIPE, SIGQUIT);
(void)signal(SIGQUIT, SIG_IGN);
(void)signal(SIGTERM, SIG_IGN);
(void)signal(SIGTSTP, SIG_IGN);
(void)signal(SIGTTOU, SIG_IGN);
/* Create with exact permissions. */
(void)umask(0);
}