2 * $Id: unix.c,v 1.9 2010-02-10 14:05:37 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 ) {
102 /* -------------------
103 system rmdir with afp error code.
105 int netatalk_rmdir_all_errors(const char *name)
107 if (rmdir(name) < 0) {
112 return AFPERR_DIRNEMPT;
115 return AFPERR_ACCESS;
125 /* -------------------
126 system rmdir with afp error code.
127 ENOENT is not an error.
129 int netatalk_rmdir(const char *name)
131 int ret = netatalk_rmdir_all_errors(name);
132 if (ret == AFPERR_NOOBJ)
137 /* -------------------
138 system unlink with afp error code.
139 ENOENT is not an error.
141 int netatalk_unlink(const char *name)
143 if (unlink(name) < 0) {
151 return AFPERR_ACCESS;
159 char *fullpathname(const char *name)
161 static char wd[ MAXPATHLEN + 1];
163 if ( getcwd( wd , MAXPATHLEN) ) {
164 strlcat(wd, "/", MAXPATHLEN);
165 strlcat(wd, name, MAXPATHLEN);
168 strlcpy(wd, name, MAXPATHLEN);
173 int copy_file(const char *src, const char *dst, mode_t mode)
182 if ((sfd = open(src, O_RDONLY)) < 0) {
183 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
184 src, dst, src, strerror(errno));
188 if ((dfd = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
189 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
190 src, dst, dst, strerror(errno));
195 while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
199 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
200 src, dst, src, strerror(errno));
207 if ((cc = write(dfd, filebuf, buflen)) < 0) {
210 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
211 src, dst, dst, strerror(errno));
228 /* don't bother to report an error if there's already one */
229 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
230 src, dst, dst, strerror(errno));
238 /* This is equivalent of unix rename(). */
239 int unix_rename(const char *oldpath, const char *newpath)
242 char pd_name[PATH_MAX+1];
248 if (rename(oldpath, newpath) < 0)
251 for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
252 pd_name[i] = newpath[i];
255 while (i > 0 && pd_name[i] != '/') i--;
256 if (pd_name[i] == '/') i++;
258 pd_name[i++] = '.'; pd_name[i++] = '\0';
260 if (stat(pd_name, &pd_stat) < 0) {
261 LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s",
262 pd_name, geteuid(), strerror(errno));
266 /* So we have SGID bit set... */
267 if ((S_ISGID & pd_stat.st_mode) != 0) {
270 LOG(log_error, logtype_afpd, "seteuid() failed: %s", strerror(errno));
271 if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
272 LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s",
273 pd_name, geteuid(), strerror(errno));