X-Git-Url: https://arthur.barton.de/gitweb/?a=blobdiff_plain;f=libatalk%2Fvfs%2Funix.c;h=6679c296eac5d0fd22edb177e29e1145b73527e3;hb=1b20936596f89b2706f1122ca2fabad6ffe00c98;hp=eaf2568698014baf7e38fb52987fab096a9ed54a;hpb=98dfba54d8de0dd743774021ff1f129d3717d6c4;p=netatalk.git diff --git a/libatalk/vfs/unix.c b/libatalk/vfs/unix.c index eaf25686..6679c296 100644 --- a/libatalk/vfs/unix.c +++ b/libatalk/vfs/unix.c @@ -1,5 +1,5 @@ /* - * $Id: unix.c,v 1.3 2009-10-16 00:40:48 didg Exp $ + * $Id: unix.c,v 1.6.2.1 2010-01-02 10:22:33 franklahm Exp $ * * Copyright (c) 1990,1993 Regents of The University of Michigan. * All Rights Reserved. See COPYRIGHT. @@ -26,7 +26,7 @@ /* ----------------------------- a dropbox is a folder where w is set but not r eg: - rwx-wx-wx or rwx-wx-- + rwx-wx-wx or rwx-wx-- rwx----wx (is not asked by a Mac with OS >= 8.0 ?) */ int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask) @@ -60,8 +60,8 @@ int stickydirmode(const char *name, const mode_t mode, const int dropbox, const * Ignore EPERM errors: We may be dealing with a directory that is * group writable, in which case chmod will fail. */ - if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM && - !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) ) + if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM && + !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) ) { LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) ); retval = -1; @@ -79,33 +79,35 @@ int dir_rx_set(mode_t mode) /* --------------------- */ int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask) { -struct stat sb; -mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */ + struct stat sb; + mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO; /* rwx for owner group and other, by default */ if (!st) { - if (stat(name, &sb) != 0) + if (lstat(name, &sb) != 0) return -1; st = &sb; } - - mode |= st->st_mode & ~mask; /* keep other bits from previous mode */ - if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) { - return -1; - } - return 0; + + if (S_ISLNK(st->st_mode)) return 0; /* we don't want to change link permissions */ + + mode |= st->st_mode & ~mask; /* keep other bits from previous mode */ + if ( chmod( name, mode & ~v_umask ) < 0 && errno != EPERM ) { + return -1; + } + return 0; } /* ------------------- system rmdir with afp error code. ENOENT is not an error. - */ +*/ int netatalk_rmdir(const char *name) { if (rmdir(name) < 0) { switch ( errno ) { case ENOENT : break; - case ENOTEMPTY : + case ENOTEMPTY : return AFPERR_DIRNEMPT; case EPERM: case EACCES : @@ -122,7 +124,7 @@ int netatalk_rmdir(const char *name) /* ------------------- system unlink with afp error code. ENOENT is not an error. - */ +*/ int netatalk_unlink(const char *name) { if (unlink(name) < 0) { @@ -161,6 +163,7 @@ int copy_file(const char *src, const char *dst, mode_t mode) int sfd = -1; int dfd = -1; ssize_t cc; + size_t buflen; char filebuf[8192]; if ((sfd = open(src, O_RDONLY)) < 0) { @@ -186,8 +189,9 @@ int copy_file(const char *src, const char *dst, mode_t mode) goto exit; } - while (cc > 0) { - if ((cc -= write(dfd, filebuf, cc)) < 0) { + buflen = cc; + while (buflen > 0) { + if ((cc = write(dfd, filebuf, buflen)) < 0) { if (errno == EINTR) continue; LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s", @@ -195,14 +199,67 @@ int copy_file(const char *src, const char *dst, mode_t mode) ret = -1; goto exit; } + buflen -= cc; } } exit: if (sfd != -1) close(sfd); - if (dfd != -1) - close(dfd); + + if (dfd != -1) { + int err; + + err = close(dfd); + if (!ret && err) { + /* don't bother to report an error if there's already one */ + LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s", + src, dst, dst, strerror(errno)); + ret = -1; + } + } return ret; } + +/* This is equivalent of unix rename(). */ +int unix_rename(const char *oldpath, const char *newpath) +{ +#if 0 + char pd_name[PATH_MAX+1]; + int i; + struct stat pd_stat; + uid_t uid; +#endif + + if (rename(oldpath, newpath) < 0) + return -1; +#if 0 + for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++) + pd_name[i] = newpath[i]; + pd_name[i] = '\0'; + + while (i > 0 && pd_name[i] != '/') i--; + if (pd_name[i] == '/') i++; + + pd_name[i++] = '.'; pd_name[i++] = '\0'; + + if (stat(pd_name, &pd_stat) < 0) { + LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s", + pd_name, geteuid(), strerror(errno)); + return 0; + } + + /* So we have SGID bit set... */ + if ((S_ISGID & pd_stat.st_mode) != 0) { + uid = geteuid(); + if (seteuid(0) < 0) + LOG(log_error, logtype_afpd, "seteuid() failed: %s", strerror(errno)); + if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0) + LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s", + pd_name, geteuid(), strerror(errno)); + seteuid(uid); + } +#endif + return 0; +}