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>
27 #include <atalk/acl.h>
29 /* -----------------------------
30 a dropbox is a folder where w is set but not r eg:
32 rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
34 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
39 /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
40 if ((dropbox & AFPVOL_DROPBOX)) {
43 if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
44 ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
47 if ( seteuid(0) < 0) {
48 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
50 if ( (retval=chmod( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
51 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
53 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
59 #endif /* DROPKLUDGE */
62 * Ignore EPERM errors: We may be dealing with a directory that is
63 * group writable, in which case chmod will fail.
65 if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
66 !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
68 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
75 /* ------------------------- */
76 int dir_rx_set(mode_t mode)
78 return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
81 /* --------------------- */
82 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
85 mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */
88 if (lstat(name, &sb) != 0)
93 if (S_ISLNK(st->st_mode))
94 return 0; /* we don't want to change link permissions */
96 mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
98 if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) {
105 * @brief system rmdir with afp error code.
107 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
109 int netatalk_rmdir_all_errors(int dirfd, const char *name)
116 err = unlinkat(dirfd, name, AT_REMOVEDIR);
126 return AFPERR_DIRNEMPT;
129 return AFPERR_ACCESS;
140 * @brief System rmdir with afp error code, but ENOENT is not an error.
142 * Supports *at semantics (cf openat) if HAVE_RENAMEAT. Pass dirfd=-1 to ignore this.
144 int netatalk_rmdir(int dirfd, const char *name)
146 int ret = netatalk_rmdir_all_errors(dirfd, name);
147 if (ret == AFPERR_NOOBJ)
152 /* -------------------
153 system unlink with afp error code.
154 ENOENT is not an error.
156 int netatalk_unlink(const char *name)
158 if (unlink(name) < 0) {
166 return AFPERR_ACCESS;
174 char *fullpathname(const char *name)
176 static char wd[ MAXPATHLEN + 1];
178 if ( getcwd( wd , MAXPATHLEN) ) {
179 strlcat(wd, "/", MAXPATHLEN);
180 strlcat(wd, name, MAXPATHLEN);
183 strlcpy(wd, name, MAXPATHLEN);
189 /**************************************************************************
190 * *at semnatics support functions (like openat, renameat standard funcs)
191 **************************************************************************/
194 * Supports *at semantics if HAVE_RENAMEAT, pass dirfd=-1 to ignore this
196 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
208 sfd = openat(dirfd, src, O_RDONLY);
210 sfd = open(src, O_RDONLY);
213 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
214 src, dst, src, strerror(errno));
218 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
219 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
220 src, dst, dst, strerror(errno));
225 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
229 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
230 src, dst, src, strerror(errno));
237 if ((cc = write(dfd, filebuf, buflen)) < 0) {
240 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
241 src, dst, dst, strerror(errno));
258 /* don't bother to report an error if there's already one */
259 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
260 src, dst, dst, strerror(errno));
269 * at wrapper for netatalk_unlink
271 int netatalk_unlinkat(int dirfd, const char *name)
277 if (unlinkat(dirfd, name, 0) < 0) {
285 return AFPERR_ACCESS;
292 return netatalk_unlink(name);
300 * @brief This is equivalent of unix rename()
302 * unix_rename mulitplexes rename and renameat. If we dont HAVE_RENAMEAT, sfd and dfd
305 * @param sfd (r) if we HAVE_RENAMEAT, -1 gives AT_FDCWD
306 * @param oldpath (r) guess what
307 * @param dfd (r) same as sfd
308 * @param newpath (r) guess what
310 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
318 if (renameat(sfd, oldpath, dfd, newpath) < 0)
321 if (rename(oldpath, newpath) < 0)
323 #endif /* HAVE_RENAMEAT */
329 * @brief stat/fsstatat multiplexer
331 * statat mulitplexes stat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
333 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
334 * @param path (r) pathname
335 * @param st (rw) pointer to struct stat
337 int statat(int dirfd, const char *path, struct stat *st)
342 return (fstatat(dirfd, path, st, 0));
344 return (stat(path, st));
352 * @brief lstat/fsstatat multiplexer
354 * lstatat mulitplexes lstat and fstatat. If we dont HAVE_RENAMEAT, dirfd is ignored.
356 * @param dirfd (r) Only used if HAVE_RENAMEAT, ignored else, -1 gives AT_FDCWD
357 * @param path (r) pathname
358 * @param st (rw) pointer to struct stat
360 int lstatat(int dirfd, const char *path, struct stat *st)
365 return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
367 return (lstat(path, st));
375 * @brief opendir wrapper for *at semantics support
377 * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
379 * @param dirfd (r) if != -1, chdir(dirfd) before opendir(path)
380 * @param path (r) pathname
382 DIR *opendirat(int dirfd, const char *path)
388 if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
396 if (dirfd != -1 && fchdir(cwd) != 0) {
397 LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");