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>
25 /* -----------------------------
26 a dropbox is a folder where w is set but not r eg:
28 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
30 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
35 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
36 if ((dropbox & AFPVOL_DROPBOX)) {
39 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
40 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
43 if ( seteuid(0) < 0) {
44 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
46 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
47 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
49 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
55 #endif /* DROPKLUDGE */
58 * Ignore EPERM errors: We may be dealing with a directory that is
59 * group writable, in which case chmod will fail.
61 if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
62 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
64 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
71 /* ------------------------- */
72 int dir_rx_set(mode_t mode)
74 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
77 /* --------------------- */
78 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
81 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
84 if (lstat(name, &sb) != 0)
89 if (S_ISLNK(st->st_mode))
90 return 0; /* we don't want to change link permissions */
92 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
94 if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
101 * @brief system rmdir with afp error code.
103 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
105 int netatalk_rmdir_all_errors(int dirfd, const char *name)
112 err = unlinkat(dirfd, name, AT_REMOVEDIR);
122 return AFPERR_DIRNEMPT;
125 return AFPERR_ACCESS;
136 * @brief System rmdir with afp error code, but ENOENT is not an error.
138 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
140 int netatalk_rmdir(int dirfd, const char *name)
142 int ret = netatalk_rmdir_all_errors(dirfd, name);
143 if (ret == AFPERR_NOOBJ)
148 /* -------------------
149 system unlink with afp error code.
150 ENOENT is not an error.
152 int netatalk_unlink(const char *name)
154 if (unlink(name) < 0) {
162 return AFPERR_ACCESS;
170 char *fullpathname(const char *name)
172 static char wd[ MAXPATHLEN + 1];
174 if ( getcwd( wd , MAXPATHLEN) ) {
175 strlcat(wd, "/", MAXPATHLEN);
176 strlcat(wd, name, MAXPATHLEN);
179 strlcpy(wd, name, MAXPATHLEN);
185 /**************************************************************************
186 * *at semnatics support functions (like openat, renameat standard funcs)
187 **************************************************************************/
190 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
192 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
204 sfd = openat(dirfd, src, O_RDONLY);
206 sfd = open(src, O_RDONLY);
209 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
210 src, dst, src, strerror(errno));
214 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
215 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
216 src, dst, dst, strerror(errno));
221 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
225 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
226 src, dst, src, strerror(errno));
233 if ((cc = write(dfd, filebuf, buflen)) < 0) {
236 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
237 src, dst, dst, strerror(errno));
254 /* don't bother to report an error if there's already one */
255 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
256 src, dst, dst, strerror(errno));
265 * at wrapper for netatalk_unlink
267 int netatalk_unlinkat(int dirfd, const char *name)
273 if (unlinkat(dirfd, name, 0) < 0) {
281 return AFPERR_ACCESS;
288 return netatalk_unlink(name);
296 * @brief This is equivalent of unix rename()
298 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
301 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
302 * @param oldpath (r) guess what
303 * @param dfd (r) same as sfd
304 * @param newpath (r) guess what
306 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
314 if (renameat(sfd, oldpath, dfd, newpath) < 0)
317 if (rename(oldpath, newpath) < 0)
319 #endif /* HAVE_RENAMEAT */
325 * @brief stat/fsstatat multiplexer
327 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
329 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
330 * @param path (r) pathname
331 * @param st (rw) pointer to struct stat
333 int statat(int dirfd, const char *path, struct stat *st)
338 return (fstatat(dirfd, path, st, 0));
340 return (stat(path, st));
348 * @brief lstat/fsstatat multiplexer
350 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
352 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
353 * @param path (r) pathname
354 * @param st (rw) pointer to struct stat
356 int lstatat(int dirfd, const char *path, struct stat *st)
361 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
363 return (lstat(path, st));
371 * @brief opendir wrapper for *at semantics support
373 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
375 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
376 * @param path (r) pathname
378 DIR *opendirat(int dirfd, const char *path)
384 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
392 if (dirfd != -1 && fchdir(cwd) != 0) {
393 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");