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( 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( 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( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
102 * @brief system rmdir with afp error code.
104 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. 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_RENAMEAT. 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 char *fullpathname(const char *name)
173 static char wd[ MAXPATHLEN + 1];
175 if ( getcwd( wd , MAXPATHLEN) ) {
176 strlcat(wd, "/", MAXPATHLEN);
177 strlcat(wd, name, MAXPATHLEN);
180 strlcpy(wd, name, MAXPATHLEN);
186 /**************************************************************************
187 * *at semnatics support functions (like openat, renameat standard funcs)
188 **************************************************************************/
191 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
193 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
205 sfd = openat(dirfd, src, O_RDONLY);
207 sfd = open(src, O_RDONLY);
210 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
211 src, dst, src, strerror(errno));
215 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
216 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
217 src, dst, dst, strerror(errno));
222 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
226 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
227 src, dst, src, strerror(errno));
234 if ((cc = write(dfd, filebuf, buflen)) < 0) {
237 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
238 src, dst, dst, strerror(errno));
255 /* don't bother to report an error if there's already one */
256 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
257 src, dst, dst, strerror(errno));
266 * at wrapper for netatalk_unlink
268 int netatalk_unlinkat(int dirfd, const char *name)
274 if (unlinkat(dirfd, name, 0) < 0) {
282 return AFPERR_ACCESS;
289 return netatalk_unlink(name);
297 * @brief This is equivalent of unix rename()
299 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
302 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
303 * @param oldpath (r) guess what
304 * @param dfd (r) same as sfd
305 * @param newpath (r) guess what
307 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
315 if (renameat(sfd, oldpath, dfd, newpath) < 0)
318 if (rename(oldpath, newpath) < 0)
320 #endif /* HAVE_RENAMEAT */
326 * @brief stat/fsstatat multiplexer
328 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
330 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
331 * @param path (r) pathname
332 * @param st (rw) pointer to struct stat
334 int statat(int dirfd, const char *path, struct stat *st)
339 return (fstatat(dirfd, path, st, 0));
341 return (stat(path, st));
349 * @brief lstat/fsstatat multiplexer
351 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
353 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
354 * @param path (r) pathname
355 * @param st (rw) pointer to struct stat
357 int lstatat(int dirfd, const char *path, struct stat *st)
362 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
364 return (lstat(path, st));
372 * @brief opendir wrapper for *at semantics support
374 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
376 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
377 * @param path (r) pathname
379 DIR *opendirat(int dirfd, const char *path)
385 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
393 if (dirfd != -1 && fchdir(cwd) != 0) {
394 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");