]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/unix.c
Update NEWS
[netatalk.git] / libatalk / vfs / unix.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <unistd.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <string.h>
17
18 #include <atalk/afp.h>
19 #include <atalk/util.h>
20 #include <atalk/directory.h>
21 #include <atalk/volume.h>
22 #include <atalk/logger.h>
23 #include <atalk/unix.h>
24 #include <atalk/acl.h>
25
26 /* -----------------------------
27    a dropbox is a folder where w is set but not r eg:
28    rwx-wx-wx or rwx-wx--
29    rwx----wx (is not asked by a Mac with OS >= 8.0 ?)
30 */
31 int stickydirmode(const char *name, const mode_t mode, const int dropbox, const mode_t v_umask)
32 {
33     int retval = 0;
34
35 #ifdef DROPKLUDGE
36     /* Turn on the sticky bit if this is a drop box, also turn off the setgid bit */
37     if ((dropbox & AFPVOL_DROPBOX)) {
38         int uid;
39
40         if ( ( (mode & S_IWOTH) && !(mode & S_IROTH)) ||
41              ( (mode & S_IWGRP) && !(mode & S_IRGRP)) )
42         {
43             uid=geteuid();
44             if ( seteuid(0) < 0) {
45                 LOG(log_error, logtype_afpd, "stickydirmode: unable to seteuid root: %s", strerror(errno));
46             }
47             if ( (retval=chmod_acl( name, ( (DIRBITS | mode | S_ISVTX) & ~v_umask) )) < 0) {
48                 LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
49             } else {
50                 LOG(log_debug, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(retval) );
51             }
52             seteuid(uid);
53             return retval;
54         }
55     }
56 #endif /* DROPKLUDGE */
57
58     /*
59      *  Ignore EPERM errors:  We may be dealing with a directory that is
60      *  group writable, in which case chmod will fail.
61      */
62     if ( (chmod_acl( name, (DIRBITS | mode) & ~v_umask ) < 0) && errno != EPERM &&
63          !(errno == ENOENT && (dropbox & AFPVOL_NOADOUBLE)) )
64     {
65         LOG(log_error, logtype_afpd, "stickydirmode: chmod \"%s\": %s", fullpathname(name), strerror(errno) );
66         retval = -1;
67     }
68
69     return retval;
70 }
71
72 /* ------------------------- */
73 int dir_rx_set(mode_t mode)
74 {
75     return (mode & (S_IXUSR | S_IRUSR)) == (S_IXUSR | S_IRUSR);
76 }
77
78 /* --------------------- */
79 int setfilmode(const char * name, mode_t mode, struct stat *st, mode_t v_umask)
80 {
81     struct stat sb;
82     mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;  /* rwx for owner group and other, by default */
83
84     if (!st) {
85         if (lstat(name, &sb) != 0)
86             return -1;
87         st = &sb;
88     }
89
90     if (S_ISLNK(st->st_mode))
91         return 0; /* we don't want to change link permissions */
92     
93     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
94
95     if ( chmod_acl( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
96         return -1;
97     }
98     return 0;
99 }
100
101 /*
102  * @brief system rmdir with afp error code.
103  *
104  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
105  */
106 int netatalk_rmdir_all_errors(int dirfd, const char *name)
107 {
108     int err;
109
110 #ifdef HAVE_ATFUNCS
111     if (dirfd == -1)
112         dirfd = AT_FDCWD;
113     err = unlinkat(dirfd, name, AT_REMOVEDIR);
114 #else
115     err = rmdir(name);
116 #endif
117
118     if (err < 0) {
119         switch ( errno ) {
120         case ENOENT :
121             return AFPERR_NOOBJ;
122         case ENOTEMPTY :
123             return AFPERR_DIRNEMPT;
124         case EPERM:
125         case EACCES :
126             return AFPERR_ACCESS;
127         case EROFS:
128             return AFPERR_VLOCK;
129         default :
130             return AFPERR_PARAM;
131         }
132     }
133     return AFP_OK;
134 }
135
136 /*
137  * @brief System rmdir with afp error code, but ENOENT is not an error.
138  *
139  * Supports *at semantics (cf openat) if HAVE_ATFUNCS. Pass dirfd=-1 to ignore this.
140  */
141 int netatalk_rmdir(int dirfd, const char *name)
142 {
143     int ret = netatalk_rmdir_all_errors(dirfd, name);
144     if (ret == AFPERR_NOOBJ)
145         return AFP_OK;
146     return ret;
147 }
148
149 /* -------------------
150    system unlink with afp error code.
151    ENOENT is not an error.
152 */
153 int netatalk_unlink(const char *name)
154 {
155     if (unlink(name) < 0) {
156         switch (errno) {
157         case ENOENT :
158             break;
159         case EROFS:
160             return AFPERR_VLOCK;
161         case EPERM:
162         case EACCES :
163             return AFPERR_ACCESS;
164         default :
165             return AFPERR_PARAM;
166         }
167     }
168     return AFP_OK;
169 }
170
171 /**************************************************************************
172  * *at semnatics support functions (like openat, renameat standard funcs)
173  **************************************************************************/
174
175 /* 
176  * Supports *at semantics if HAVE_ATFUNCS, pass dirfd=-1 to ignore this
177  */
178 int copy_file(int dirfd, const char *src, const char *dst, mode_t mode)
179 {
180     int    ret = 0;
181     int    sfd = -1;
182     int    dfd = -1;
183     ssize_t cc;
184     size_t  buflen;
185     char   filebuf[8192];
186
187 #ifdef HAVE_ATFUNCS
188     if (dirfd == -1)
189         dirfd = AT_FDCWD;
190     sfd = openat(dirfd, src, O_RDONLY);
191 #else
192     sfd = open(src, O_RDONLY);
193 #endif
194     if (sfd < 0) {
195         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
196             src, dst, src, strerror(errno));
197         return -1;
198     }
199
200     if ((dfd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
201         LOG(log_info, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
202             src, dst, dst, strerror(errno));
203         ret = -1;
204         goto exit;
205     }
206
207     while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
208         if (cc < 0) {
209             if (errno == EINTR)
210                 continue;
211             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
212                 src, dst, src, strerror(errno));
213             ret = -1;
214             goto exit;
215         }
216
217         buflen = cc;
218         while (buflen > 0) {
219             if ((cc = write(dfd, filebuf, buflen)) < 0) {
220                 if (errno == EINTR)
221                     continue;
222                 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
223                     src, dst, dst, strerror(errno));
224                 ret = -1;
225                 goto exit;
226             }
227             buflen -= cc;
228         }
229     }
230
231 exit:
232     if (sfd != -1)
233         close(sfd);
234
235     if (dfd != -1) {
236         int err;
237
238         err = close(dfd);
239         if (!ret && err) {
240             /* don't bother to report an error if there's already one */
241             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
242                 src, dst, dst, strerror(errno));
243             ret = -1;
244         }
245     }
246
247     return ret;
248 }
249
250 /* 
251  * at wrapper for netatalk_unlink
252  */
253 int netatalk_unlinkat(int dirfd, const char *name)
254 {
255 #ifdef HAVE_ATFUNCS
256     if (dirfd == -1)
257         dirfd = AT_FDCWD;
258
259     if (unlinkat(dirfd, name, 0) < 0) {
260         switch (errno) {
261         case ENOENT :
262             break;
263         case EROFS:
264             return AFPERR_VLOCK;
265         case EPERM:
266         case EACCES :
267             return AFPERR_ACCESS;
268         default :
269             return AFPERR_PARAM;
270         }
271     }
272     return AFP_OK;
273 #else
274     return netatalk_unlink(name);
275 #endif
276
277     /* DEADC0DE */
278     return 0;
279 }
280
281 /*
282  * @brief This is equivalent of unix rename()
283  *
284  * unix_rename mulitplexes rename and renameat. If we dont HAVE_ATFUNCS, sfd and dfd
285  * are ignored.
286  *
287  * @param sfd        (r) if we HAVE_ATFUNCS, -1 gives AT_FDCWD
288  * @param oldpath    (r) guess what
289  * @param dfd        (r) same as sfd
290  * @param newpath    (r) guess what
291  */
292 int unix_rename(int sfd, const char *oldpath, int dfd, const char *newpath)
293 {
294 #ifdef HAVE_ATFUNCS
295     if (sfd == -1)
296         sfd = AT_FDCWD;
297     if (dfd == -1)
298         dfd = AT_FDCWD;
299
300     if (renameat(sfd, oldpath, dfd, newpath) < 0)
301         return -1;        
302 #else
303     if (rename(oldpath, newpath) < 0)
304         return -1;
305 #endif  /* HAVE_ATFUNCS */
306
307     return 0;
308 }
309
310 /* 
311  * @brief stat/fsstatat multiplexer
312  *
313  * statat mulitplexes stat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
314  *
315  * @param dirfd   (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
316  * @param path    (r) pathname
317  * @param st      (rw) pointer to struct stat
318  */
319 int statat(int dirfd, const char *path, struct stat *st)
320 {
321 #ifdef HAVE_ATFUNCS
322     if (dirfd == -1)
323         dirfd = AT_FDCWD;
324     return (fstatat(dirfd, path, st, 0));
325 #else
326     return (stat(path, st));
327 #endif            
328
329     /* DEADC0DE */
330     return -1;
331 }
332
333 /* 
334  * @brief lstat/fsstatat multiplexer
335  *
336  * lstatat mulitplexes lstat and fstatat. If we dont HAVE_ATFUNCS, dirfd is ignored.
337  *
338  * @param dirfd   (r) Only used if HAVE_ATFUNCS, ignored else, -1 gives AT_FDCWD
339  * @param path    (r) pathname
340  * @param st      (rw) pointer to struct stat
341  */
342 int lstatat(int dirfd, const char *path, struct stat *st)
343 {
344 #ifdef HAVE_ATFUNCS
345     if (dirfd == -1)
346         dirfd = AT_FDCWD;
347     return (fstatat(dirfd, path, st, AT_SYMLINK_NOFOLLOW));
348 #else
349     return (lstat(path, st));
350 #endif            
351
352     /* DEADC0DE */
353     return -1;
354 }
355
356 /* 
357  * @brief opendir wrapper for *at semantics support
358  *
359  * opendirat chdirs to dirfd if dirfd != -1 before calling opendir on path.
360  *
361  * @param dirfd   (r) if != -1, chdir(dirfd) before opendir(path)
362  * @param path    (r) pathname
363  */
364 DIR *opendirat(int dirfd, const char *path)
365 {
366     DIR *ret;
367     int cwd = -1;
368
369     if (dirfd != -1) {
370         if (((cwd = open(".", O_RDONLY)) == -1) || (fchdir(dirfd) != 0)) {
371             ret = NULL;
372             goto exit;
373         }
374     }
375
376     ret = opendir(path);
377
378     if (dirfd != -1 && fchdir(cwd) != 0) {
379         LOG(log_error, logtype_afpd, "opendirat: cant chdir back. exit!");
380         exit(EXITERR_SYS);
381     }
382
383 exit:
384     if (cwd != -1)
385         close(cwd);
386
387     return ret;
388 }