-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-posix.c
351 lines (277 loc) · 5.44 KB
/
install-posix.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#define INSTALL_IMPLEMENTATION
#include "install.h"
#if INSTALL_OS_TYPE == INSTALL_OS_POSIX
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static struct install_status_t
iposix_init (void)
{
struct install_status_t status = INSTALL_STATUS_INIT;
status.status = INSTALL_STATUS_OK;
inst_exec_suffix [0] = 0;
return status;
}
/*
* Error.
*/
static install_error_t
iposix_error_current (void)
{
install_error_t e;
e.value = (unsigned long) errno;
return e;
}
static const char *
iposix_error_message (install_error_t error)
{
return strerror (error.value);
}
static const char *
iposix_error_message_current (void)
{
return iposix_error_message (iposix_error_current ());
}
static void
iposix_error_reset (void)
{
errno = 0;
}
/*
* GID.
*/
static int
iposix_gid_compare (group_id_t a, group_id_t b)
{
return a.value == b.value;
}
static unsigned int
iposix_gid_format (char *buffer, group_id_t gid)
{
assert (buffer != NULL);
unsigned int size = snprintf (buffer, INSTALL_FMT_GID, "%d", gid.value);
return size;
}
static unsigned int
iposix_gid_scan (const char *buffer, group_id_t *gid)
{
assert (buffer != NULL);
assert (gid != NULL);
unsigned int size = sscanf (buffer, "%d", &gid->value);
return size;
}
static int
iposix_gid_current (group_id_t *gid)
{
assert (gid != NULL);
gid->value = getgid ();
return 1;
}
static int
iposix_gid_lookup (const char *group, group_id_t *gid)
{
struct group *grp;
assert (group != NULL);
assert (gid != NULL);
grp = getgrnam (group);
if (!grp) return 0;
gid->value = grp->gr_gid;
return 1;
}
static int
iposix_gid_valid (group_id_t gid)
{
return gid.value != -1;
}
static void
iposix_gid_free (group_id_t *gid)
{
assert (gid != NULL);
}
/*
* UID.
*/
static int
iposix_uid_compare (user_id_t a, user_id_t b)
{
return a.value == b.value;
}
static unsigned int
iposix_uid_format (char *buffer, user_id_t uid)
{
assert (buffer != NULL);
unsigned int size = snprintf (buffer, INSTALL_FMT_UID, "%d", uid.value);
return size;
}
static unsigned int
iposix_uid_scan (const char *buffer, user_id_t *uid)
{
assert (buffer != NULL);
assert (uid != NULL);
unsigned int size = sscanf (buffer, "%d", &uid->value);
return size;
}
static int
iposix_uid_current (user_id_t *uid)
{
assert (uid != NULL);
uid->value = getuid ();
return 1;
}
static int
iposix_uid_lookup (const char *user, user_id_t *uid)
{
struct passwd *pwd;
assert (user != NULL);
assert (uid != NULL);
pwd = getpwnam (user);
if (!pwd) return 0;
uid->value = pwd->pw_uid;
return 1;
}
static int
iposix_uid_valid (user_id_t uid)
{
return uid.value != -1;
}
static void
iposix_uid_free (user_id_t *uid)
{
assert (uid != NULL);
}
/*
* User name.
*/
const char *
iposix_user_name_current (void)
{
return getlogin ();
}
/*
* Directory.
*/
static int
iposix_mkdir (const char *dir, permissions_t mode)
{
assert (dir != NULL);
return mkdir (dir, mode.value) == 0;
}
/*
* File.
*/
static int
iposix_file_mode_set (const char *file, permissions_t mode)
{
assert (file != NULL);
return chmod (file, mode.value) == 0;
}
static int
iposix_file_mode_get (const char *file, permissions_t *mode)
{
struct stat sb;
assert (file != NULL);
assert (mode != NULL);
if (stat (file, &sb) == -1) return 0;
mode->value = sb.st_mode & 0755;
return 1;
}
static int
iposix_file_ownership_set (const char *file, user_id_t uid, group_id_t gid)
{
assert (file != NULL);
if (chown (file, uid.value, gid.value) == -1) return 0;
return 1;
}
static int
iposix_file_ownership_get (const char *file, user_id_t *uid, group_id_t *gid)
{
struct stat sb;
assert (file != NULL);
assert (uid != NULL);
assert (gid != NULL);
if (stat (file, &sb) == -1) return 0;
uid->value = sb.st_uid;
gid->value = sb.st_gid;
return 1;
}
static int
iposix_file_size (const char *file, size_t *size)
{
struct stat sb;
assert (file != NULL);
assert (size != NULL);
if (stat (file, &sb) == -1) return 0;
*size = sb.st_size;
return 1;
}
static int
iposix_file_link (const char *src, const char *dst)
{
assert (src != NULL);
assert (dst != NULL);
if (symlink (src, dst) == -1) return 0;
return 1;
}
/*
* Misc.
*/
static unsigned int
iposix_umask (unsigned int m)
{
return umask (m);
}
static int
iposix_can_set_ownership (user_id_t uid)
{
assert (uid.value != -1);
return uid.value == 0; /* Only the superuser can chown() files. */
}
static int
iposix_supports_symlinks (void)
{
return 1;
}
static int
iposix_supports_posix_modes (void)
{
return 1;
}
const struct install_platform_callbacks_t install_platform_posix = {
&iposix_init,
&iposix_error_message,
&iposix_error_message_current,
&iposix_error_current,
&iposix_error_reset,
&iposix_gid_compare,
&iposix_gid_format,
&iposix_gid_scan,
&iposix_gid_current,
&iposix_gid_lookup,
&iposix_gid_valid,
&iposix_gid_free,
&iposix_uid_compare,
&iposix_uid_format,
&iposix_uid_scan,
&iposix_uid_current,
&iposix_uid_lookup,
&iposix_uid_valid,
&iposix_uid_free,
&iposix_user_name_current,
&iposix_mkdir,
&iposix_file_mode_get,
&iposix_file_mode_set,
&iposix_file_ownership_get,
&iposix_file_ownership_set,
&iposix_file_size,
&iposix_file_link,
&iposix_can_set_ownership,
&iposix_supports_symlinks,
&iposix_supports_posix_modes,
&iposix_umask,
};
#endif