2 * $Id: unix.c,v 1.11 2010-04-18 16:14:51 hat001 Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
11 #endif /* HAVE_CONFIG_H */
16 #include <sys/param.h>
17 #include <sys/types.h>
21 #include <atalk/afp.h>
22 #include <atalk/util.h>
23 #include <atalk/directory.h>
24 #include <atalk/volume.h>
25 #include <atalk/logger.h>
26 #include <atalk/unix.h>
28 /* -----------------------------
29 a dropbox is a folder where w is set but not r eg:
31 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
33 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
38 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
39 if ((dropbox & AFPVOL_DROPBOX)) {
42 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
43 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
46 if ( seteuid(0) < 0) {
47 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
49 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
50 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
52 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
58 #endif /* DROPKLUDGE */
61 * Ignore EPERM errors: We may be dealing with a directory that is
62 * group writable, in which case chmod will fail.
64 if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
65 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
67 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
74 /* ------------------------- */
75 int dir_rx_set(mode_t mode)
77 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
80 /* --------------------- */
81 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
84 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
87 if (lstat(name, &sb) != 0)
92 if (S_ISLNK(st->st_mode))
93 return 0; /* we don't want to change link permissions */
95 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
97 if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
104 * @brief system rmdir with afp error code.
106 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
108 int netatalk_rmdir_all_errors(int dirfd, const char *name)
115 err = unlinkat(dirfd, name, AT_REMOVEDIR);
125 return AFPERR_DIRNEMPT;
128 return AFPERR_ACCESS;
139 * @brief System rmdir with afp error code, but ENOENT is not an error.
141 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
143 int netatalk_rmdir(int dirfd, const char *name)
145 int ret = netatalk_rmdir_all_errors(dirfd, name);
146 if (ret == AFPERR_NOOBJ)
151 /* -------------------
152 system unlink with afp error code.
153 ENOENT is not an error.
155 int netatalk_unlink(const char *name)
157 if (unlink(name) < 0) {
165 return AFPERR_ACCESS;
173 char *fullpathname(const char *name)
175 static char wd[ MAXPATHLEN + 1];
177 if ( getcwd( wd , MAXPATHLEN) ) {
178 strlcat(wd, "/", MAXPATHLEN);
179 strlcat(wd, name, MAXPATHLEN);
182 strlcpy(wd, name, MAXPATHLEN);
188 /**************************************************************************
189 * *at semnatics support functions (like openat, renameat standard funcs)
190 **************************************************************************/
193 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
195 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
207 sfd = openat(dirfd, src, O_RDONLY);
209 sfd = open(src, O_RDONLY);
212 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
213 src, dst, src, strerror(errno));
217 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
218 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
219 src, dst, dst, strerror(errno));
224 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
228 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
229 src, dst, src, strerror(errno));
236 if ((cc = write(dfd, filebuf, buflen)) < 0) {
239 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
240 src, dst, dst, strerror(errno));
257 /* don't bother to report an error if there's already one */
258 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
259 src, dst, dst, strerror(errno));
268 * at wrapper for netatalk_unlink
270 int netatalk_unlinkat(int dirfd, const char *name)
276 if (unlinkat(dirfd, name, 0) < 0) {
284 return AFPERR_ACCESS;
291 return netatalk_unlink(name);
299 * @brief This is equivalent of unix rename()
301 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
304 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
305 * @param oldpath (r) guess what
306 * @param dfd (r) same as sfd
307 * @param newpath (r) guess what
309 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
317 if (renameat(sfd, oldpath, dfd, newpath) < 0)
320 if (rename(oldpath, newpath) < 0)
322 #endif /* HAVE_RENAMEAT */
328 * @brief stat/fsstatat multiplexer
330 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
332 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
333 * @param path (r) pathname
334 * @param st (rw) pointer to struct stat
336 int statat(int dirfd, const char *path, struct stat *st)
341 return (fstatat(dirfd, path, st, 0));
343 return (stat(path, st));
351 * @brief lstat/fsstatat multiplexer
353 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
355 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
356 * @param path (r) pathname
357 * @param st (rw) pointer to struct stat
359 int lstatat(int dirfd, const char *path, struct stat *st)
364 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
366 return (lstat(path, st));
374 * @brief opendir wrapper for *at semantics support
376 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
378 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
379 * @param path (r) pathname
381 DIR *opendirat(int dirfd, const char *path)
387 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
395 if (dirfd != -1 && fchdir(cwd) != 0) {
396 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");