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