-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
164 lines (138 loc) · 2.6 KB
/
utils.c
File metadata and controls
164 lines (138 loc) · 2.6 KB
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
/* utils.c */
#include <arpa/inet.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include "utils.h"
void *
xmalloc(size_t len)
{
void * p;
p = malloc(len);
if (!p) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
memset(p, 0, len);
return p;
}
uint32_t
read_uint32(FILE * f)
{
size_t ret;
uint32_t t;
ret = fread(&t, 4, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while reading from file.\n");
exit(EXIT_FAILURE);
}
t = ntohl(t);
return t;
}
uint16_t
read_uint16(FILE * f)
{
size_t ret;
uint16_t t;
ret = fread(&t, 2, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while reading from file.\n");
exit(EXIT_FAILURE);
}
t = ntohs(t);
return t;
}
uint8_t
read_uint8(FILE * f)
{
size_t ret;
uint8_t t;
ret = fread(&t, 1, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while reading from file.\n");
exit(EXIT_FAILURE);
}
return t;
}
void
write_uint32(FILE * f, uint32_t t)
{
size_t ret;
t = htonl(t);
ret = fwrite(&t, 4, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while writing to file.\n");
exit(EXIT_FAILURE);
}
}
void
write_uint16(FILE * f, uint16_t t)
{
size_t ret;
t = htons(t);
ret = fwrite(&t, 2, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while writing to file.\n");
exit(EXIT_FAILURE);
}
}
void
write_uint8(FILE * f, uint8_t t)
{
size_t ret;
ret = fwrite(&t, 1, 1, f);
if (ret != 1) {
fprintf(stderr, "Error while writing to file.\n");
exit(EXIT_FAILURE);
}
}
inline static void
passwd_clear(struct passwd * pwd)
{
if (!pwd) fatal("passwd_clear called with null");
/* check for existance of each pointer and clear the string */
if (pwd->pw_name)
memset(pwd->pw_name, 0, strlen(pwd->pw_name));
if (pwd->pw_passwd)
memset(pwd->pw_passwd, 0, strlen(pwd->pw_passwd));
if (pwd->pw_gecos)
memset(pwd->pw_gecos, 0, strlen(pwd->pw_gecos));
if (pwd->pw_dir)
memset(pwd->pw_dir, 0, strlen(pwd->pw_dir));
if (pwd->pw_shell)
memset(pwd->pw_shell, 0, strlen(pwd->pw_shell));
/* clear the passwd struct itself */
memset(pwd, 0, sizeof(struct passwd));
}
void
fatal(const char * fmt)
{
fprintf(stderr, "%s\n", fmt);
exit(EXIT_FAILURE);
}
void
privdrop(const char * username)
{
uid_t uid;
gid_t gid;
struct passwd * pwd;
int ret;
pwd = getpwnam(username);
if (!pwd) fatal("getpwnam failed so cannot privdrop");
uid = pwd->pw_uid;
gid = pwd->pw_gid;
ret = setregid(gid, gid);
if (ret < 0) goto err;
ret = setreuid(uid, uid);
if (ret < 0) goto err;
passwd_clear(pwd);
return;
err:
passwd_clear(pwd);
fatal("privdrop failed");
}
/* EOF */