2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
13 #include <sys/param.h>
14 #include <sys/types.h>
18 #include <atalk/afp.h>
19 #include <atalk/util.h>
20 #include <atalk/directory.h>
21 #include <atalk/volume.h>
22 #include <atalk/logger.h>
23 #include <atalk/unix.h>
24 #include <atalk/acl.h>
26 /* -----------------------------
27 a dropbox is a folder where w is set but not r eg:
29 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
31 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
36 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
37 if ((dropbox & AFPVOL_DROPBOX)) {
40 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
41 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
44 if ( seteuid(0) < 0) {
45 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
47 if ( (retval=chmod_acl( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
48 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
50 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
56 #endif /* DROPKLUDGE */
59 * Ignore EPERM errors: We may be dealing with a directory that is
60 * group writable, in which case chmod will fail.
62 if ( (chmod_acl( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
63 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
65 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
72 /* ------------------------- */
73 int dir_rx_set(mode_t mode)
75 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
78 /* --------------------- */
79 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
82 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
85 if (lstat(name, &sb) != 0)
90 if (S_ISLNK(st->st_mode))
91 return 0; /* we don't want to change link permissions */
93 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
95 if ( chmod_acl( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
102 * @brief system rmdir with afp error code.
104 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
106 int netatalk_rmdir_all_errors(int dirfd, const char *name)
113 err = unlinkat(dirfd, name, AT_REMOVEDIR);
123 return AFPERR_DIRNEMPT;
126 return AFPERR_ACCESS;
137 * @brief System rmdir with afp error code, but ENOENT is not an error.
139 * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
141 int netatalk_rmdir(int dirfd, const char *name)
143 int ret = netatalk_rmdir_all_errors(dirfd, name);
144 if (ret == AFPERR_NOOBJ)
149 /* -------------------
150 system unlink with afp error code.
151 ENOENT is not an error.
153 int netatalk_unlink(const char *name)
155 if (unlink(name) < 0) {
163 return AFPERR_ACCESS;
171 /**************************************************************************
172 * *at semnatics support functions (like openat, renameat standard funcs)
173 **************************************************************************/
176 * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
178 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
190 sfd = openat(dirfd, src, O_RDONLY);
192 sfd = open(src, O_RDONLY);
195 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
196 src, dst, src, strerror(errno));
200 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
201 LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
202 src, dst, dst, strerror(errno));
207 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
211 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
212 src, dst, src, strerror(errno));
219 if ((cc = write(dfd, filebuf, buflen)) < 0) {
222 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
223 src, dst, dst, strerror(errno));
240 /* don't bother to report an error if there's already one */
241 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
242 src, dst, dst, strerror(errno));
251 * at wrapper for netatalk_unlink
253 int netatalk_unlinkat(int dirfd, const char *name)
259 if (unlinkat(dirfd, name, 0) < 0) {
267 return AFPERR_ACCESS;
274 return netatalk_unlink(name);
282 * @brief This is equivalent of unix rename()
284 * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
287 * @param sfd (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
288 * @param oldpath (r) guess what
289 * @param dfd (r) same as sfd
290 * @param newpath (r) guess what
292 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
300 if (renameat(sfd, oldpath, dfd, newpath) < 0)
303 if (rename(oldpath, newpath) < 0)
305 #endif /* HAVE_ATFUNCS */
311 * @brief stat/fsstatat multiplexer
313 * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
315 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
316 * @param path (r) pathname
317 * @param st (rw) pointer to struct stat
319 int statat(int dirfd, const char *path, struct stat *st)
324 return (fstatat(dirfd, path, st, 0));
326 return (stat(path, st));
334 * @brief lstat/fsstatat multiplexer
336 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
338 * @param dirfd (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
339 * @param path (r) pathname
340 * @param st (rw) pointer to struct stat
342 int lstatat(int dirfd, const char *path, struct stat *st)
347 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
349 return (lstat(path, st));
357 * @brief opendir wrapper for *at semantics support
359 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
361 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
362 * @param path (r) pathname
364 DIR *opendirat(int dirfd, const char *path)
370 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
378 if (dirfd != -1 && fchdir(cwd) != 0) {
379 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");