1 /*
2 * SysDB - src/utils/os.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "utils/os.h"
33 #include "utils/error.h"
35 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
41 #include <dirent.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include <libgen.h>
49 #include <netdb.h>
50 #include <pwd.h>
52 /*
53 * public API
54 */
56 char *
57 sdb_get_homedir(void)
58 {
59 char *username = sdb_get_current_user();
61 struct passwd pw_entry;
62 struct passwd *result = NULL;
64 char buf[4096];
66 int status;
68 if (username) {
69 memset(&pw_entry, 0, sizeof(pw_entry));
70 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
71 }
72 else
73 status = -1;
75 if (status || (! result)) {
76 char errbuf[1024];
77 sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
78 "for user %s: %s", username,
79 sdb_strerror(errno, errbuf, sizeof(errbuf)));
80 free(username);
81 return NULL;
82 }
83 free(username);
84 return strdup(result->pw_dir);
85 } /* sdb_get_homedir */
87 int
88 sdb_mkdir_all(const char *pathname, mode_t mode)
89 {
90 struct stat st;
91 char *pathname_copy;
92 char *base_dir;
94 int status = 0;
96 if ((! pathname) || (! *pathname)) {
97 errno = EINVAL;
98 return -1;
99 }
101 memset(&st, 0, sizeof(st));
102 if (! stat(pathname, &st)) {
103 if (! S_ISDIR(st.st_mode)) {
104 errno = ENOTDIR;
105 return -1;
106 }
107 return 0;
108 }
110 if (errno != ENOENT)
111 /* pathname exists but we cannot access it */
112 return -1;
114 pathname_copy = strdup(pathname);
115 if (! pathname_copy)
116 return -1;
117 base_dir = dirname(pathname_copy);
119 status = sdb_mkdir_all(base_dir, mode);
120 if (! status)
121 status = mkdir(pathname, mode);
123 free(pathname_copy);
124 return status;
125 } /* sdb_mkdir_all */
127 int
128 sdb_remove_all(const char *pathname)
129 {
130 struct stat st;
132 if ((! pathname) || (! *pathname)) {
133 errno = EINVAL;
134 return -1;
135 }
137 memset(&st, 0, sizeof(st));
138 if (stat(pathname, &st))
139 return -1;
141 if (S_ISDIR(st.st_mode)) {
142 DIR *d = opendir(pathname);
144 if (! d)
145 return -1;
147 while (42) {
148 struct dirent de;
149 struct dirent *res = NULL;
151 char filename[strlen(pathname) + sizeof(de.d_name) + 2];
153 memset(&de, 0, sizeof(de));
154 if (readdir_r(d, &de, &res)) {
155 closedir(d);
156 return -1;
157 }
159 if (! res)
160 break;
162 if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
163 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
164 continue;
166 snprintf(filename, sizeof(filename),
167 "%s/%s", pathname, de.d_name);
168 if (sdb_remove_all(filename)) {
169 closedir(d);
170 return -1;
171 }
172 }
173 closedir(d);
174 }
175 return remove(pathname);
176 } /* sdb_remove_all */
178 char *
179 sdb_get_current_user(void)
180 {
181 struct passwd pw_entry;
182 struct passwd *result = NULL;
184 uid_t uid;
186 char buf[4096];
187 int status;
189 uid = geteuid();
190 memset(&pw_entry, 0, sizeof(pw_entry));
191 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
193 if (status || (! result)) {
194 char errbuf[1024];
195 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
196 sdb_strerror(errno, errbuf, sizeof(errbuf)));
197 return NULL;
198 }
199 return strdup(result->pw_name);
200 } /* sdb_get_current_user */
202 int
203 sdb_select(int fd, int type)
204 {
205 fd_set fds;
206 fd_set *readfds = NULL;
207 fd_set *writefds = NULL;
208 fd_set *exceptfds = NULL;
210 if (fd < 0) {
211 errno = EBADF;
212 return -1;
213 }
215 FD_ZERO(&fds);
217 switch (type) {
218 case SDB_SELECTIN:
219 readfds = &fds;
220 break;
221 case SDB_SELECTOUT:
222 writefds = &fds;
223 break;
224 case SDB_SELECTERR:
225 exceptfds = &fds;
226 break;
227 default:
228 errno = EINVAL;
229 return -1;
230 }
232 FD_SET(fd, &fds);
234 while (42) {
235 int n;
236 errno = 0;
237 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
239 if ((n < 0) && (errno != EINTR))
240 return n;
241 if (n > 0)
242 break;
243 }
244 return 0;
245 } /* sdb_select */
247 ssize_t
248 sdb_write(int fd, size_t msg_len, const void *msg)
249 {
250 const char *buf;
251 size_t len;
253 if ((fd < 0) || (msg_len && (! msg)))
254 return -1;
255 if (! msg_len)
256 return 0;
258 buf = msg;
259 len = msg_len;
260 while (len > 0) {
261 ssize_t status;
263 if (sdb_select(fd, SDB_SELECTOUT))
264 return -1;
266 errno = 0;
267 status = write(fd, buf, len);
268 if (status < 0) {
269 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
270 continue;
271 if (errno == EINTR)
272 continue;
274 return status;
275 }
277 len -= (size_t)status;
278 buf += status;
279 }
281 return (ssize_t)msg_len;
282 } /* sdb_write */
284 int
285 sdb_resolve(int network, const char *address, struct addrinfo **res)
286 {
287 struct addrinfo ai_hints;
288 const char *host;
289 char *port;
290 int status;
292 if (! res) {
293 errno = EINVAL;
294 return EAI_SYSTEM;
295 }
297 if (address) {
298 host = address;
299 port = strchr(host, ':');
300 if (port) {
301 *port = '\0';
302 ++port;
303 }
304 if (! *host)
305 host = NULL;
306 }
307 else {
308 host = NULL;
309 port = NULL;
310 }
312 memset(&ai_hints, 0, sizeof(ai_hints));
313 ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
314 if (network & SDB_NET_V4)
315 ai_hints.ai_family = AF_INET;
316 else if (network & SDB_NET_V6)
317 ai_hints.ai_family = AF_INET6;
318 else
319 ai_hints.ai_family = AF_UNSPEC;
321 if ((network & SDB_NET_IP) == SDB_NET_IP) {
322 ai_hints.ai_socktype = 0;
323 ai_hints.ai_protocol = 0;
324 }
325 else if (network & SDB_NET_TCP) {
326 ai_hints.ai_socktype = SOCK_STREAM;
327 ai_hints.ai_protocol = IPPROTO_TCP;
328 }
329 else if (network & SDB_NET_UDP) {
330 ai_hints.ai_socktype = SOCK_DGRAM;
331 ai_hints.ai_protocol = IPPROTO_UDP;
332 }
334 status = getaddrinfo(host, port, &ai_hints, res);
335 if (port) {
336 --port;
337 *port = ':';
338 }
339 return status;
340 } /* sdb_resolve */
342 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */