2 * $Id: unix.c,v 1.10 2010-03-12 15:16:49 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
11 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
16 #include <sys/types.h>
20 #include <atalk/afp.h>
21 #include <atalk/util.h>
22 #include <atalk/directory.h>
23 #include <atalk/volume.h>
24 #include <atalk/logger.h>
25 #include <atalk/unix.h>
27 /* -----------------------------
28 a dropbox is a folder where w is set but not r eg:
30 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
32 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
37 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
38 if ((dropbox & AFPVOL_DROPBOX)) {
41 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
42 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
45 if ( seteuid(0) < 0) {
46 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
48 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
49 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
51 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
57 #endif /* DROPKLUDGE */
60 * Ignore EPERM errors: We may be dealing with a directory that is
61 * group writable, in which case chmod will fail.
63 if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
64 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
66 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
73 /* ------------------------- */
74 int dir_rx_set(mode_t mode)
76 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
79 /* --------------------- */
80 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
83 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
86 if (lstat(name, &sb) != 0)
91 if (S_ISLNK(st->st_mode))
92 return 0; /* we don't want to change link permissions */
94 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
96 if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
103 * @brief system rmdir with afp error code.
105 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
107 int netatalk_rmdir_all_errors(int dirfd, const char *name)
114 err = unlinkat(dirfd, name, AT_REMOVEDIR);
124 return AFPERR_DIRNEMPT;
127 return AFPERR_ACCESS;
138 * @brief System rmdir with afp error code, but ENOENT is not an error.
140 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
142 int netatalk_rmdir(int dirfd, const char *name)
144 int ret = netatalk_rmdir_all_errors(dirfd, name);
145 if (ret == AFPERR_NOOBJ)
150 /* -------------------
151 system unlink with afp error code.
152 ENOENT is not an error.
154 int netatalk_unlink(const char *name)
156 if (unlink(name) < 0) {
164 return AFPERR_ACCESS;
172 char *fullpathname(const char *name)
174 static char wd[ MAXPATHLEN + 1];
176 if ( getcwd( wd , MAXPATHLEN) ) {
177 strlcat(wd, "/", MAXPATHLEN);
178 strlcat(wd, name, MAXPATHLEN);
181 strlcpy(wd, name, MAXPATHLEN);
187 /**************************************************************************
188 * *at semnatics support functions (like openat, renameat standard funcs)
189 **************************************************************************/
192 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
194 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
206 sfd = openat(dirfd, src, O_RDONLY);
208 sfd = open(src, O_RDONLY);
211 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
212 src, dst, src, strerror(errno));
216 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
217 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
218 src, dst, dst, strerror(errno));
223 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
227 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
228 src, dst, src, strerror(errno));
235 if ((cc = write(dfd, filebuf, buflen)) < 0) {
238 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
239 src, dst, dst, strerror(errno));
256 /* don't bother to report an error if there's already one */
257 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
258 src, dst, dst, strerror(errno));
267 * at wrapper for netatalk_unlink
269 int netatalk_unlinkat(int dirfd, const char *name)
275 if (unlinkat(dirfd, name, 0) < 0) {
283 return AFPERR_ACCESS;
290 return netatalk_unlink(name);
298 * @brief This is equivalent of unix rename()
300 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
303 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
304 * @param oldpath (r) guess what
305 * @param dfd (r) same as sfd
306 * @param newpath (r) guess what
308 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
316 if (renameat(sfd, oldpath, dfd, newpath) < 0)
319 if (rename(oldpath, newpath) < 0)
321 #endif /* HAVE_RENAMEAT */
327 * @brief stat/fsstatat multiplexer
329 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
331 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
332 * @param path (r) pathname
333 * @param st (rw) pointer to struct stat
335 int statat(int dirfd, const char *path, struct stat *st)
340 return (fstatat(dirfd, path, st, 0));
342 return (stat(path, st));
350 * @brief lstat/fsstatat multiplexer
352 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
354 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
355 * @param path (r) pathname
356 * @param st (rw) pointer to struct stat
358 int lstatat(int dirfd, const char *path, struct stat *st)
363 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
365 return (lstat(path, st));
373 * @brief opendir wrapper for *at semantics support
375 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
377 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
378 * @param path (r) pathname
380 DIR *opendirat(int dirfd, const char *path)
386 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
394 if (dirfd != -1 && fchdir(cwd) != 0) {
395 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");