]> arthur.barton.de Git - netatalk.git/blob - libatalk/vfs/unix.c
Symlink patch from Anton Starikov
[netatalk.git] / libatalk / vfs / unix.c
1 /*
2  * $Id: unix.c,v 1.6.2.1 2010-01-02 10:22:33 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)) return 0; /* we don't want to change link permissions */
92     
93     mode |= st->st_mode & ~mask; /* keep other bits from previous mode */
94     if ( chmod( name,  mode & ~v_umask ) < 0 && errno != EPERM ) {
95         return -1;
96     }
97     return 0;
98 }
99
100 /* -------------------
101    system rmdir with afp error code.
102    ENOENT is not an error.
103 */
104 int netatalk_rmdir(const char *name)
105 {
106     if (rmdir(name) < 0) {
107         switch ( errno ) {
108         case ENOENT :
109             break;
110         case ENOTEMPTY :
111             return AFPERR_DIRNEMPT;
112         case EPERM:
113         case EACCES :
114             return AFPERR_ACCESS;
115         case EROFS:
116             return AFPERR_VLOCK;
117         default :
118             return AFPERR_PARAM;
119         }
120     }
121     return AFP_OK;
122 }
123
124 /* -------------------
125    system unlink with afp error code.
126    ENOENT is not an error.
127 */
128 int netatalk_unlink(const char *name)
129 {
130     if (unlink(name) < 0) {
131         switch (errno) {
132         case ENOENT :
133             break;
134         case EROFS:
135             return AFPERR_VLOCK;
136         case EPERM:
137         case EACCES :
138             return AFPERR_ACCESS;
139         default :
140             return AFPERR_PARAM;
141         }
142     }
143     return AFP_OK;
144 }
145
146 char *fullpathname(const char *name)
147 {
148     static char wd[ MAXPATHLEN + 1];
149
150     if ( getcwd( wd , MAXPATHLEN) ) {
151         strlcat(wd, "/", MAXPATHLEN);
152         strlcat(wd, name, MAXPATHLEN);
153     }
154     else {
155         strlcpy(wd, name, MAXPATHLEN);
156     }
157     return wd;
158 }
159
160 int copy_file(const char *src, const char *dst, mode_t mode)
161 {
162     int    ret = 0;
163     int    sfd = -1;
164     int    dfd = -1;
165     ssize_t cc;
166     size_t  buflen;
167     char   filebuf[8192];
168
169     if ((sfd = open(src, O_RDONLY)) < 0) {
170         LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
171             src, dst, src, strerror(errno));
172         return -1;
173     }
174
175     if ((dfd = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
176         LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): open '%s' error: %s",
177             src, dst, dst, strerror(errno));
178         ret = -1;
179         goto exit;
180     }
181
182     while ((cc = read(sfd, filebuf, sizeof(filebuf)))) {
183         if (cc < 0) {
184             if (errno == EINTR)
185                 continue;
186             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
187                 src, dst, src, strerror(errno));
188             ret = -1;
189             goto exit;
190         }
191
192         buflen = cc;
193         while (buflen > 0) {
194             if ((cc = write(dfd, filebuf, buflen)) < 0) {
195                 if (errno == EINTR)
196                     continue;
197                 LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): read '%s' error: %s",
198                     src, dst, dst, strerror(errno));
199                 ret = -1;
200                 goto exit;
201             }
202             buflen -= cc;
203         }
204     }
205
206 exit:
207     if (sfd != -1)
208         close(sfd);
209
210     if (dfd != -1) {
211         int err;
212
213         err = close(dfd);
214         if (!ret && err) {
215             /* don't bother to report an error if there's already one */
216             LOG(log_error, logtype_afpd, "copy_file('%s'/'%s'): close '%s' error: %s",
217                 src, dst, dst, strerror(errno));
218             ret = -1;
219         }
220     }
221
222     return ret;
223 }
224
225 /* This is equivalent of unix rename(). */
226 int unix_rename(const char *oldpath, const char *newpath)
227 {
228 #if 0
229     char pd_name[PATH_MAX+1];
230     int i;
231     struct stat pd_stat;
232     uid_t uid;
233 #endif
234
235     if (rename(oldpath, newpath) < 0)
236         return -1;
237 #if 0
238     for (i = 0; i <= PATH_MAX && newpath[i] != '\0'; i++)
239         pd_name[i] = newpath[i];
240     pd_name[i] = '\0';
241
242     while (i > 0 && pd_name[i] != '/') i--;
243     if (pd_name[i] == '/') i++;
244
245     pd_name[i++] = '.'; pd_name[i++] = '\0';
246
247     if (stat(pd_name, &pd_stat) < 0) {
248         LOG(log_error, logtype_afpd, "stat() of parent dir failed: pd_name = %s, uid = %d: %s",
249             pd_name, geteuid(), strerror(errno));
250         return 0;
251     }
252
253     /* So we have SGID bit set... */
254     if ((S_ISGID & pd_stat.st_mode) != 0) {
255         uid = geteuid();
256         if (seteuid(0) < 0)
257             LOG(log_error, logtype_afpd, "seteuid() failed: %s", strerror(errno));
258         if (recursive_chown(newpath, uid, pd_stat.st_gid) < 0)
259             LOG(log_error, logtype_afpd, "chown() of parent dir failed: newpath=%s, uid=%d: %s",
260                 pd_name, geteuid(), strerror(errno));
261         seteuid(uid);
262     }
263 #endif
264     return 0;
265 }