]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/unix.c
Merge symlink branch
[netatalk.git] / libatalk / vfs / unix.c
1 /*
2  * $Id: unix.c,v 1.9 2010-02-10 14:05:37 franklahm Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  *
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <string.h>
19
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>
26
27 /* -----------------------------
28    a dropbox is a folder where w is set but not r eg:
29    rwx-wx-wx or rwx-wx--
30    rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
31 */
32 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
33 {
34     int retval = 0;
35
36 #ifdef DROPKLUDGE
37     /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
38     if ((dropbox & AFPVOL_DROPBOX)) {
39         int uid;
40
41         if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
42              ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
43         {
44             uid=geteuid();
45             if ( seteuid(0) < 0) {
46                 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
47             }
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) );
50             } else {
51                 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
52             }
53             seteuid(uid);
54             return retval;
55         }
56     }
57 #endif /* DROPKLUDGE */
58
59     /*
60      *  Ignore EPERM errors:  We may be dealing with a directory that is
61      *  group writable, in which case chmod will fail.
62      */
63     if ( (chmod( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
64          !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
65     {
66         LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
67         retval = -1;
68     }
69
70     return retval;
71 }
72
73 /* ------------------------- */
74 int dir_rx_set(mode_t mode)
75 {
76     return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
77 }
78
79 /* --------------------- */
80 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
81 {
82     struct stat sb;
83     mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
84
85     if (!st) {
86         if (lstat(name, &sb) != 0)
87             return -1;
88         st = &sb;
89     }
90
91     if (S_ISLNK(st->st_mode))
92         return 0; /* we don't want to change link permissions */
93     
94     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
95
96     if ( chmod( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
97         return -1;
98     }
99     return 0;
100 }
101
102 /* -------------------
103    system rmdir with afp error code.
104 */
105 int netatalk_rmdir_all_errors(const char *name)
106 {
107     if (rmdir(name) < 0) {
108         switch ( errno ) {
109         case ENOENT :
110             return AFPERR_NOOBJ;
111         case ENOTEMPTY :
112             return AFPERR_DIRNEMPT;
113         case EPERM:
114         case EACCES :
115             return AFPERR_ACCESS;
116         case EROFS:
117             return AFPERR_VLOCK;
118         default :
119             return AFPERR_PARAM;
120         }
121     }
122     return AFP_OK;
123 }
124
125 /* -------------------
126    system rmdir with afp error code.
127    ENOENT is not an error.
128 */
129 int netatalk_rmdir(const char *name)
130 {
131     int ret = netatalk_rmdir_all_errors(name);
132     if (ret == AFPERR_NOOBJ)
133         return AFP_OK;
134     return ret;
135 }
136
137 /* -------------------
138    system unlink with afp error code.
139    ENOENT is not an error.
140 */
141 int netatalk_unlink(const char *name)
142 {
143     if (unlink(name) < 0) {
144         switch (errno) {
145         case ENOENT :
146             break;
147         case EROFS:
148             return AFPERR_VLOCK;
149         case EPERM:
150         case EACCES :
151             return AFPERR_ACCESS;
152         default :
153             return AFPERR_PARAM;
154         }
155     }
156     return AFP_OK;
157 }
158
159 char *fullpathname(const char *name)
160 {
161     static char wd[ MAXPATHLEN + 1];
162
163     if ( getcwd( wd , MAXPATHLEN) ) {
164         strlcat(wd, "/", MAXPATHLEN);
165         strlcat(wd, name, MAXPATHLEN);
166     }
167     else {
168         strlcpy(wd, name, MAXPATHLEN);
169     }
170     return wd;
171 }
172
173 int copy_file(const char *src, const char *dst, mode_t mode)
174 {
175     int    ret = 0;
176     int    sfd = -1;
177     int    dfd = -1;
178     ssize_t cc;
179     size_t  buflen;
180     char   filebuf[8192];
181
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));
185         return -1;
186     }
187
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));
191         ret = -1;
192         goto exit;
193     }
194
195     while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
196         if (cc < 0) {
197             if (errno == EINTR)
198                 continue;
199             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
200                 src, dst, src, strerror(errno));
201             ret = -1;
202             goto exit;
203         }
204
205         buflen = cc;
206         while (buflen > 0) {
207             if ((cc = write(dfd, filebuf, buflen)) < 0) {
208                 if (errno == EINTR)
209                     continue;
210                 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
211                     src, dst, dst, strerror(errno));
212                 ret = -1;
213                 goto exit;
214             }
215             buflen -= cc;
216         }
217     }
218
219 exit:
220     if (sfd != -1)
221         close(sfd);
222
223     if (dfd != -1) {
224         int err;
225
226         err = close(dfd);
227         if (!ret && err) {
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));
231             ret = -1;
232         }
233     }
234
235     return ret;
236 }
237
238 /* This is equivalent of unix rename(). */
239 int unix_rename(const char *oldpath, const char *newpath)
240 {
241 #if 0
242     char pd_name[PATH_MAX+1];
243     int i;
244     struct stat pd_stat;
245     uid_t uid;
246 #endif
247
248     if (rename(oldpath, newpath) < 0)
249         return -1;
250 #if 0
251     for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
252         pd_name[i] = newpath[i];
253     pd_name[i] = '\0';
254
255     while (i > 0 && pd_name[i] != '/') i--;
256     if (pd_name[i] == '/') i++;
257
258     pd_name[i++] = '.'; pd_name[i++] = '\0';
259
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));
263         return 0;
264     }
265
266     /* So we have SGID bit set... */
267     if ((S_ISGID & pd_stat.st_mode) != 0) {
268         uid = geteuid();
269         if (seteuid(0) < 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));
274         seteuid(uid);
275     }
276 #endif
277     return 0;
278 }