2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Netatalk utility functions
22 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
38 #include <atalk/adouble.h>
40 #include <atalk/afp.h>
41 #include <atalk/logger.h>
42 #include <atalk/vfs.h>
43 #include <atalk/util.h>
44 #include <atalk/unix.h>
45 #include <atalk/compat.h>
46 #include <atalk/errchk.h>
48 /* close all FDs >= a specified value */
49 static void closeall(int fd)
51 int fdlimit = sysconf(_SC_OPEN_MAX);
58 * Run command in a child and wait for it to finish
60 int run_cmd(const char *cmd, char **cmd_argv)
64 sigset_t sigs, oldsigs;
68 pthread_sigmask(SIG_SETMASK, &sigs, &oldsigs);
70 if ((pid = fork()) < 0) {
71 LOG(log_error, logtype_default, "run_cmd: fork: %s", strerror(errno));
78 execvp("mv", cmd_argv);
82 while ((wpid = waitpid(pid, &status, 0)) < 0) {
88 LOG(log_error, logtype_default, "waitpid(%d): %s", (int)pid, strerror(errno));
92 if (WIFEXITED(status))
93 status = WEXITSTATUS(status);
94 else if (WIFSIGNALED(status))
95 status = WTERMSIG(status);
97 LOG(log_note, logtype_default, "run_cmd(\"%s\"): status: %d", cmd, status);
102 pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
109 * Fork, exit parent, setsid(), optionally chdir("/"), optionally close all fds
111 * returns -1 on failure, but you can't do much except exit in that case
112 * since we may already have forked
114 int daemonize(int nochdir, int noclose)
142 open("/dev/null",O_RDWR);
150 static uid_t saved_uid = -1;
153 * seteuid(0) and back, if either fails and panic != 0 we PANIC
155 void become_root(void)
158 saved_uid = geteuid();
160 AFP_PANIC("Can't seteuid(0)");
164 void unbecome_root(void)
167 if (saved_uid == -1 || seteuid(saved_uid) < 0)
168 AFP_PANIC("Can't seteuid back");
174 * @brief get cwd in static buffer
176 * @returns pointer to path or pointer to error messages on error
178 const char *getcwdpath(void)
180 static char cwd[MAXPATHLEN + 1];
183 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
186 return strerror(errno);
190 * @brief Request absolute path
192 * @returns Absolute filesystem path to object
194 const char *fullpathname(const char *name)
196 static char wd[MAXPATHLEN + 1];
201 if (getcwd(wd , MAXPATHLEN)) {
202 strlcat(wd, "/", MAXPATHLEN);
203 strlcat(wd, name, MAXPATHLEN);
205 strlcpy(wd, name, MAXPATHLEN);
212 * Takes a buffer with a path, strips slashs, returns basename
216 * "[/][dir/[...]]file"
218 * "[/][dir/[...]]dir/[/]"
219 * Result is "file" or "dir"
221 * @returns pointer to basename in path buffer, buffer is possibly modified
223 char *stripped_slashes_basename(char *p)
225 int i = strlen(p) - 1;
226 while (i > 0 && p[i] == '/')
228 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
231 /*********************************************************************************
232 * chdir(), chmod(), chown(), stat() wrappers taking an additional option.
233 * Currently the only used options are O_NOFOLLOW, used to switch between symlink
234 * behaviour, and O_NETATALK_ACL for ochmod() indicating chmod_acl() shall be
235 * called which does special ACL handling depending on the filesytem
236 *********************************************************************************/
238 int ostat(const char *path, struct stat *buf, int options)
240 if (options & O_NOFOLLOW)
241 return lstat(path, buf);
243 return stat(path, buf);
246 int ochown(const char *path, uid_t owner, gid_t group, int options)
248 if (options & O_NOFOLLOW)
249 return lchown(path, owner, group);
251 return chown(path, owner, group);
255 * chmod() wrapper for symlink and ACL handling
257 * @param path (r) path
258 * @param mode (r) requested mode
259 * @param sb (r) stat() of path or NULL
260 * @param option (r) O_NOFOLLOW | O_NETATALK_ACL
262 * Options description:
263 * O_NOFOLLOW: don't chmod() symlinks, do nothing, return 0
264 * O_NETATALK_ACL: call chmod_acl() instead of chmod()
266 int ochmod(char *path, mode_t mode, const struct stat *st, int options)
271 if (lstat(path, &sb) != 0)
276 if (options & O_NOFOLLOW)
277 if (S_ISLNK(st->st_mode))
280 if (options & O_NETATALK_ACL) {
281 return chmod_acl(path, mode);
283 return chmod(path, mode);
288 * @brief ostat/fsstatat multiplexer
290 * ostatat mulitplexes ostat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
292 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
293 * @param path (r) pathname
294 * @param st (rw) pointer to struct stat
296 int ostatat(int dirfd, const char *path, struct stat *st, int options)
301 return fstatat(dirfd, path, st, (options & O_NOFOLLOW) ? AT_SYMLINK_NOFOLLOW : 0);
303 return ostat(path, st, options);
311 * @brief symlink safe chdir replacement
313 * Only chdirs to dir if it doesn't contain symlinks or if symlink checking
316 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
318 int ochdir(const char *dir, int options)
320 char buf[MAXPATHLEN+1];
321 char cwd[MAXPATHLEN+1];
325 if (!(options & O_NOFOLLOW))
329 dir is a canonical path (without "../" "./" "//" )
334 if (getcwd(cwd, MAXPATHLEN) == NULL)
342 * chdir request | realpath result | ret
343 * (after getwcwd) | |
344 * =======================================
347 * /a/b/. | /c/d/e/f | 1
349 if (getcwd(buf, MAXPATHLEN) == NULL)
354 /* relative path requested,
357 for (; cwd[i]; i++) {
358 if (buf[i] != cwd[i])
369 for (i = 0; test[i]; i++) {
370 if (test[i] != dir[i]) {
389 * Store n random bytes an buf
391 void randombytes(void *buf, int n)
393 char *p = (char *)buf;
397 if ((fd = open("/dev/urandom", O_RDONLY)) != -1) {
398 /* generate from /dev/urandom */
399 if (read(fd, buf, n) != n) {
404 /* fd now != -1, so srandom wont be called below */
409 gettimeofday(&tv, NULL);
410 srandom((unsigned int)tv.tv_usec);
411 for (i=0 ; i < n ; i++)
412 p[i] = random() & 0xFF;
418 int gmem(gid_t gid, int ngroups, gid_t *groups)
422 for ( i = 0; i < ngroups; i++ ) {
423 if ( groups[ i ] == gid ) {
431 * realpath() replacement that always allocates storage for returned path
433 char *realpath_safe(const char *path)
437 #ifdef REALPATH_TAKES_NULL
438 if ((resolved_path = realpath(path, NULL)) == NULL) {
439 LOG(log_error, logtype_afpd, "realpath() cannot resolve path \"%s\"", path);
442 return resolved_path;
444 if ((resolved_path = malloc(MAXPATHLEN+1)) == NULL)
446 if (realpath(path, resolved_path) == NULL) {
448 LOG(log_error, logtype_afpd, "realpath() cannot resolve path \"%s\"", path);
451 /* Safe some memory */
453 if ((tmp = strdup(resolved_path)) == NULL) {
459 return resolved_path;
464 * Returns pointer to static buffer with basename of path
466 const char *basename_safe(const char *path)
468 static char buf[MAXPATHLEN+1];
469 strlcpy(buf, path, MAXPATHLEN);
470 return basename(buf);
474 * extended strtok allows the quoted strings
475 * modified strtok.c in glibc 2.0.6
477 char *strtok_quote(char *s, const char *delim)
479 static char *olds = NULL;
485 /* Scan leading delimiters. */
486 s += strspn (s, delim);
490 /* Find the end of the token. */
493 if (token[0] == '\"') {
495 s = strpbrk (token, "\"");
497 s = strpbrk (token, delim);
501 /* This token finishes the string. */
502 olds = strchr (token, '\0');
504 /* Terminate the token and make OLDS point past it. */
511 int set_groups(AFPObj *obj, struct passwd *pwd)
513 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
514 LOG(log_error, logtype_afpd, "initgroups(%s, %d): %s", pwd->pw_name, pwd->pw_gid, strerror(errno));
516 if ((obj->ngroups = getgroups(0, NULL)) < 0) {
517 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno));
523 if (NULL == (obj->groups = calloc(obj->ngroups, sizeof(gid_t))) ) {
524 LOG(log_error, logtype_afpd, "login: %s calloc: %d", obj->ngroups);
528 if ((obj->ngroups = getgroups(obj->ngroups, obj->groups)) < 0 ) {
529 LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno));
536 #define GROUPSTR_BUFSIZE 1024
537 const char *print_groups(int ngroups, gid_t *groups)
539 static char groupsstr[GROUPSTR_BUFSIZE];
546 for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
547 s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);