Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Utility routines used in rsync. | |
5cb37436 | 3 | * |
0f78b815 WD |
4 | * Copyright (C) 1996-2000 Andrew Tridgell |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> | |
7 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison | |
5cb37436 | 8 | * |
0ecfbf27 MP |
9 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
5cb37436 | 13 | * |
0ecfbf27 MP |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
5cb37436 | 18 | * |
e7c67065 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0ecfbf27 | 22 | */ |
c627d613 | 23 | |
c627d613 AT |
24 | #include "rsync.h" |
25 | ||
c7c11a0d | 26 | extern int verbose; |
bc6ebcd2 WD |
27 | extern int dry_run; |
28 | extern int module_id; | |
29 | extern int modify_window; | |
7e43da81 | 30 | extern int relative_paths; |
e175fb07 | 31 | extern int human_readable; |
a8167c66 | 32 | extern unsigned int module_dirlen; |
e9357a2d | 33 | extern mode_t orig_umask; |
a7260c40 | 34 | extern char *partial_dir; |
7842418b | 35 | extern struct filter_list_struct server_filter_list; |
c7c11a0d | 36 | |
0ecfbf27 MP |
37 | int sanitize_paths = 0; |
38 | ||
a8167c66 WD |
39 | char curr_dir[MAXPATHLEN]; |
40 | unsigned int curr_dir_len; | |
41 | int curr_dir_depth; /* This is only set for a sanitizing daemon. */ | |
42 | ||
0f78b815 | 43 | /* Set a fd into nonblocking mode. */ |
f0359dd0 AT |
44 | void set_nonblocking(int fd) |
45 | { | |
46 | int val; | |
47 | ||
0ecfbf27 | 48 | if ((val = fcntl(fd, F_GETFL, 0)) == -1) |
f0359dd0 AT |
49 | return; |
50 | if (!(val & NONBLOCK_FLAG)) { | |
51 | val |= NONBLOCK_FLAG; | |
52 | fcntl(fd, F_SETFL, val); | |
53 | } | |
54 | } | |
55 | ||
0f78b815 | 56 | /* Set a fd into blocking mode. */ |
36349ea0 AT |
57 | void set_blocking(int fd) |
58 | { | |
59 | int val; | |
60 | ||
0ecfbf27 | 61 | if ((val = fcntl(fd, F_GETFL, 0)) == -1) |
36349ea0 AT |
62 | return; |
63 | if (val & NONBLOCK_FLAG) { | |
64 | val &= ~NONBLOCK_FLAG; | |
65 | fcntl(fd, F_SETFL, val); | |
66 | } | |
67 | } | |
68 | ||
ac13ad10 | 69 | /** |
0ecfbf27 MP |
70 | * Create a file descriptor pair - like pipe() but use socketpair if |
71 | * possible (because of blocking issues on pipes). | |
5cb37436 | 72 | * |
0ecfbf27 | 73 | * Always set non-blocking. |
f0359dd0 | 74 | */ |
08f15335 AT |
75 | int fd_pair(int fd[2]) |
76 | { | |
f0359dd0 AT |
77 | int ret; |
78 | ||
4f5b0756 | 79 | #ifdef HAVE_SOCKETPAIR |
f0359dd0 | 80 | ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); |
08f15335 | 81 | #else |
f0359dd0 | 82 | ret = pipe(fd); |
08f15335 | 83 | #endif |
f0359dd0 AT |
84 | |
85 | if (ret == 0) { | |
86 | set_nonblocking(fd[0]); | |
87 | set_nonblocking(fd[1]); | |
88 | } | |
0ecfbf27 | 89 | |
f0359dd0 | 90 | return ret; |
08f15335 AT |
91 | } |
92 | ||
0ecfbf27 | 93 | void print_child_argv(char **cmd) |
5ad0e46f | 94 | { |
1bbd10fe | 95 | rprintf(FINFO, "opening connection using "); |
5ad0e46f MP |
96 | for (; *cmd; cmd++) { |
97 | /* Look for characters that ought to be quoted. This | |
98 | * is not a great quoting algorithm, but it's | |
99 | * sufficient for a log message. */ | |
100 | if (strspn(*cmd, "abcdefghijklmnopqrstuvwxyz" | |
101 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
102 | "0123456789" | |
103 | ",.-_=+@/") != strlen(*cmd)) { | |
785abd48 | 104 | rprintf(FINFO, "\"%s\" ", *cmd); |
5ad0e46f | 105 | } else { |
785abd48 | 106 | rprintf(FINFO, "%s ", *cmd); |
5ad0e46f MP |
107 | } |
108 | } | |
109 | rprintf(FINFO, "\n"); | |
110 | } | |
111 | ||
c627d613 AT |
112 | void out_of_memory(char *str) |
113 | { | |
c284f34a WD |
114 | rprintf(FERROR, "ERROR: out of memory in %s\n", str); |
115 | exit_cleanup(RERR_MALLOC); | |
575f2fca AT |
116 | } |
117 | ||
a1f99493 | 118 | void overflow_exit(char *str) |
575f2fca | 119 | { |
c284f34a WD |
120 | rprintf(FERROR, "ERROR: buffer overflow in %s\n", str); |
121 | exit_cleanup(RERR_MALLOC); | |
c627d613 AT |
122 | } |
123 | ||
25007999 | 124 | int set_modtime(char *fname, time_t modtime, mode_t mode) |
c627d613 | 125 | { |
25007999 WD |
126 | #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES |
127 | if (S_ISLNK(mode)) | |
128 | return 1; | |
129 | #endif | |
130 | ||
404e813c MP |
131 | if (verbose > 2) { |
132 | rprintf(FINFO, "set modtime of %s to (%ld) %s", | |
785abd48 | 133 | fname, (long)modtime, |
404e813c MP |
134 | asctime(localtime(&modtime))); |
135 | } | |
5cb37436 | 136 | |
15778afb WD |
137 | if (dry_run) |
138 | return 0; | |
139 | ||
31e12522 | 140 | { |
25007999 WD |
141 | #ifdef HAVE_UTIMES |
142 | struct timeval t[2]; | |
143 | t[0].tv_sec = time(NULL); | |
144 | t[0].tv_usec = 0; | |
145 | t[1].tv_sec = modtime; | |
146 | t[1].tv_usec = 0; | |
147 | # ifdef HAVE_LUTIMES | |
148 | if (S_ISLNK(mode)) | |
149 | return lutimes(fname, t); | |
150 | # endif | |
151 | return utimes(fname, t); | |
152 | #elif defined HAVE_UTIMBUF | |
5cb37436 | 153 | struct utimbuf tbuf; |
31e12522 AT |
154 | tbuf.actime = time(NULL); |
155 | tbuf.modtime = modtime; | |
156 | return utime(fname,&tbuf); | |
4f5b0756 | 157 | #elif defined HAVE_UTIME |
31e12522 AT |
158 | time_t t[2]; |
159 | t[0] = time(NULL); | |
160 | t[1] = modtime; | |
161 | return utime(fname,t); | |
c627d613 | 162 | #else |
25007999 | 163 | #error No file-time-modification routine found! |
c627d613 | 164 | #endif |
31e12522 | 165 | } |
c627d613 | 166 | } |
94481d91 | 167 | |
e9357a2d WD |
168 | /* This creates a new directory with default permissions. Since there |
169 | * might be some directory-default permissions affecting this, we can't | |
170 | * force the permissions directly using the original umask and mkdir(). */ | |
171 | int mkdir_defmode(char *fname) | |
172 | { | |
173 | int ret; | |
174 | ||
175 | umask(orig_umask); | |
176 | ret = do_mkdir(fname, ACCESSPERMS); | |
177 | umask(0); | |
178 | ||
179 | return ret; | |
180 | } | |
181 | ||
85c41757 WD |
182 | /* Create any necessary directories in fname. Any missing directories are |
183 | * created with default permissions. */ | |
e9357a2d | 184 | int create_directory_path(char *fname) |
6574b4f7 | 185 | { |
6574b4f7 | 186 | char *p; |
85c41757 | 187 | int ret = 0; |
6574b4f7 | 188 | |
c284f34a WD |
189 | while (*fname == '/') |
190 | fname++; | |
191 | while (strncmp(fname, "./", 2) == 0) | |
192 | fname += 2; | |
6574b4f7 | 193 | |
85c41757 | 194 | umask(orig_umask); |
6574b4f7 | 195 | p = fname; |
c284f34a | 196 | while ((p = strchr(p,'/')) != NULL) { |
85c41757 WD |
197 | *p = '\0'; |
198 | if (do_mkdir(fname, ACCESSPERMS) < 0 && errno != EEXIST) | |
199 | ret = -1; | |
200 | *p++ = '/'; | |
6574b4f7 | 201 | } |
85c41757 WD |
202 | umask(0); |
203 | ||
204 | return ret; | |
6574b4f7 | 205 | } |
950ab32d | 206 | |
ac13ad10 MP |
207 | /** |
208 | * Write @p len bytes at @p ptr to descriptor @p desc, retrying if | |
209 | * interrupted. | |
210 | * | |
211 | * @retval len upon success | |
212 | * | |
213 | * @retval <0 write's (negative) error code | |
214 | * | |
215 | * Derived from GNU C's cccp.c. | |
216 | */ | |
6566d205 | 217 | int full_write(int desc, char *ptr, size_t len) |
950ab32d AT |
218 | { |
219 | int total_written; | |
5cb37436 | 220 | |
950ab32d AT |
221 | total_written = 0; |
222 | while (len > 0) { | |
5c1b7bfd | 223 | int written = write(desc, ptr, len); |
950ab32d | 224 | if (written < 0) { |
950ab32d AT |
225 | if (errno == EINTR) |
226 | continue; | |
950ab32d AT |
227 | return written; |
228 | } | |
229 | total_written += written; | |
230 | ptr += written; | |
231 | len -= written; | |
232 | } | |
233 | return total_written; | |
234 | } | |
235 | ||
ac13ad10 MP |
236 | /** |
237 | * Read @p len bytes at @p ptr from descriptor @p desc, retrying if | |
238 | * interrupted. | |
239 | * | |
240 | * @retval >0 the actual number of bytes read | |
241 | * | |
242 | * @retval 0 for EOF | |
243 | * | |
244 | * @retval <0 for an error. | |
245 | * | |
246 | * Derived from GNU C's cccp.c. */ | |
9dd891bb | 247 | static int safe_read(int desc, char *ptr, size_t len) |
950ab32d AT |
248 | { |
249 | int n_chars; | |
5cb37436 | 250 | |
9dd891bb | 251 | if (len == 0) |
950ab32d | 252 | return len; |
5cb37436 | 253 | |
950ab32d AT |
254 | do { |
255 | n_chars = read(desc, ptr, len); | |
256 | } while (n_chars < 0 && errno == EINTR); | |
5cb37436 | 257 | |
950ab32d AT |
258 | return n_chars; |
259 | } | |
260 | ||
ac13ad10 MP |
261 | /** Copy a file. |
262 | * | |
3e13004b WD |
263 | * This is used in conjunction with the --temp-dir, --backup, and |
264 | * --copy-dest options. */ | |
63cf5ae7 | 265 | int copy_file(const char *source, const char *dest, mode_t mode) |
950ab32d AT |
266 | { |
267 | int ifd; | |
268 | int ofd; | |
269 | char buf[1024 * 8]; | |
270 | int len; /* Number of bytes read into `buf'. */ | |
271 | ||
8c9fd200 | 272 | ifd = do_open(source, O_RDONLY, 0); |
950ab32d | 273 | if (ifd == -1) { |
d62bcc17 | 274 | rsyserr(FERROR, errno, "open %s", full_fname(source)); |
950ab32d AT |
275 | return -1; |
276 | } | |
277 | ||
c7c11a0d | 278 | if (robust_unlink(dest) && errno != ENOENT) { |
d62bcc17 | 279 | rsyserr(FERROR, errno, "unlink %s", full_fname(dest)); |
950ab32d AT |
280 | return -1; |
281 | } | |
282 | ||
31e12522 | 283 | ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); |
c46ded46 | 284 | if (ofd == -1) { |
d62bcc17 | 285 | rsyserr(FERROR, errno, "open %s", full_fname(dest)); |
950ab32d AT |
286 | close(ifd); |
287 | return -1; | |
288 | } | |
289 | ||
5cb37436 | 290 | while ((len = safe_read(ifd, buf, sizeof buf)) > 0) { |
950ab32d | 291 | if (full_write(ofd, buf, len) < 0) { |
d62bcc17 | 292 | rsyserr(FERROR, errno, "write %s", full_fname(dest)); |
950ab32d AT |
293 | close(ifd); |
294 | close(ofd); | |
295 | return -1; | |
296 | } | |
297 | } | |
298 | ||
8b602edd | 299 | if (len < 0) { |
d62bcc17 | 300 | rsyserr(FERROR, errno, "read %s", full_fname(source)); |
8b602edd WD |
301 | close(ifd); |
302 | close(ofd); | |
303 | return -1; | |
304 | } | |
305 | ||
9f27cd8c | 306 | if (close(ifd) < 0) { |
d62bcc17 WD |
307 | rsyserr(FINFO, errno, "close failed on %s", |
308 | full_fname(source)); | |
9f27cd8c WD |
309 | } |
310 | ||
311 | if (close(ofd) < 0) { | |
d62bcc17 WD |
312 | rsyserr(FERROR, errno, "close failed on %s", |
313 | full_fname(dest)); | |
9f27cd8c WD |
314 | return -1; |
315 | } | |
950ab32d | 316 | |
950ab32d AT |
317 | return 0; |
318 | } | |
feaa89c4 | 319 | |
c7c11a0d DD |
320 | /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */ |
321 | #define MAX_RENAMES_DIGITS 3 | |
322 | #define MAX_RENAMES 1000 | |
323 | ||
ac13ad10 | 324 | /** |
b4235b31 MP |
325 | * Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so |
326 | * rename to <path>/.rsyncNNN instead. | |
327 | * | |
328 | * Note that successive rsync runs will shuffle the filenames around a | |
329 | * bit as long as the file is still busy; this is because this function | |
330 | * does not know if the unlink call is due to a new file coming in, or | |
331 | * --delete trying to remove old .rsyncNNN files, hence it renames it | |
332 | * each time. | |
333 | **/ | |
63cf5ae7 | 334 | int robust_unlink(const char *fname) |
c7c11a0d DD |
335 | { |
336 | #ifndef ETXTBSY | |
337 | return do_unlink(fname); | |
338 | #else | |
339 | static int counter = 1; | |
340 | int rc, pos, start; | |
341 | char path[MAXPATHLEN]; | |
342 | ||
343 | rc = do_unlink(fname); | |
c284f34a | 344 | if (rc == 0 || errno != ETXTBSY) |
c7c11a0d DD |
345 | return rc; |
346 | ||
c284f34a WD |
347 | if ((pos = strlcpy(path, fname, MAXPATHLEN)) >= MAXPATHLEN) |
348 | pos = MAXPATHLEN - 1; | |
c7c11a0d | 349 | |
c284f34a WD |
350 | while (pos > 0 && path[pos-1] != '/') |
351 | pos--; | |
5cb37436 | 352 | pos += strlcpy(path+pos, ".rsync", MAXPATHLEN-pos); |
c7c11a0d DD |
353 | |
354 | if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) { | |
355 | errno = ETXTBSY; | |
356 | return -1; | |
357 | } | |
358 | ||
359 | /* start where the last one left off to reduce chance of clashes */ | |
360 | start = counter; | |
361 | do { | |
362 | sprintf(&path[pos], "%03d", counter); | |
363 | if (++counter >= MAX_RENAMES) | |
364 | counter = 1; | |
c284f34a | 365 | } while ((rc = access(path, 0)) == 0 && counter != start); |
c7c11a0d | 366 | |
4791825d | 367 | if (verbose > 0) { |
c7c11a0d | 368 | rprintf(FINFO,"renaming %s to %s because of text busy\n", |
785abd48 | 369 | fname, path); |
4791825d | 370 | } |
c7c11a0d DD |
371 | |
372 | /* maybe we should return rename()'s exit status? Nah. */ | |
373 | if (do_rename(fname, path) != 0) { | |
374 | errno = ETXTBSY; | |
375 | return -1; | |
376 | } | |
377 | return 0; | |
378 | #endif | |
379 | } | |
380 | ||
630f548f | 381 | /* Returns 0 on successful rename, 1 if we successfully copied the file |
3ed8eafc WD |
382 | * across filesystems, -2 if copy_file() failed, and -1 on other errors. |
383 | * If partialptr is not NULL and we need to do a copy, copy the file into | |
384 | * the active partial-dir instead of over the destination file. */ | |
385 | int robust_rename(char *from, char *to, char *partialptr, | |
386 | int mode) | |
c7c11a0d | 387 | { |
62c9e6b3 WD |
388 | int tries = 4; |
389 | ||
390 | while (tries--) { | |
391 | if (do_rename(from, to) == 0) | |
392 | return 0; | |
393 | ||
394 | switch (errno) { | |
395 | #ifdef ETXTBSY | |
396 | case ETXTBSY: | |
397 | if (robust_unlink(to) != 0) | |
398 | return -1; | |
399 | break; | |
c7c11a0d | 400 | #endif |
62c9e6b3 | 401 | case EXDEV: |
3ed8eafc WD |
402 | if (partialptr) { |
403 | if (!handle_partial_dir(partialptr,PDIR_CREATE)) | |
404 | return -1; | |
405 | to = partialptr; | |
406 | } | |
62c9e6b3 WD |
407 | if (copy_file(from, to, mode) != 0) |
408 | return -2; | |
409 | do_unlink(from); | |
630f548f | 410 | return 1; |
62c9e6b3 WD |
411 | default: |
412 | return -1; | |
413 | } | |
414 | } | |
415 | return -1; | |
feaa89c4 | 416 | } |
3ba62a83 | 417 | |
3ba62a83 AT |
418 | static pid_t all_pids[10]; |
419 | static int num_pids; | |
420 | ||
4cf64834 | 421 | /** Fork and record the pid of the child. **/ |
3ba62a83 AT |
422 | pid_t do_fork(void) |
423 | { | |
424 | pid_t newpid = fork(); | |
5cb37436 | 425 | |
4cf64834 | 426 | if (newpid != 0 && newpid != -1) { |
3ba62a83 AT |
427 | all_pids[num_pids++] = newpid; |
428 | } | |
429 | return newpid; | |
430 | } | |
431 | ||
4cf64834 MP |
432 | /** |
433 | * Kill all children. | |
434 | * | |
435 | * @todo It would be kind of nice to make sure that they are actually | |
436 | * all our children before we kill them, because their pids may have | |
437 | * been recycled by some other process. Perhaps when we wait for a | |
438 | * child, we should remove it from this array. Alternatively we could | |
439 | * perhaps use process groups, but I think that would not work on | |
440 | * ancient Unix versions that don't support them. | |
441 | **/ | |
3ba62a83 AT |
442 | void kill_all(int sig) |
443 | { | |
444 | int i; | |
4cf64834 MP |
445 | |
446 | for (i = 0; i < num_pids; i++) { | |
447 | /* Let's just be a little careful where we | |
448 | * point that gun, hey? See kill(2) for the | |
449 | * magic caused by negative values. */ | |
450 | pid_t p = all_pids[i]; | |
451 | ||
452 | if (p == getpid()) | |
453 | continue; | |
454 | if (p <= 0) | |
455 | continue; | |
456 | ||
457 | kill(p, sig); | |
3ba62a83 AT |
458 | } |
459 | } | |
9486289c | 460 | |
ac13ad10 | 461 | /** Turn a user name into a uid */ |
8ef4ffd6 AT |
462 | int name_to_uid(char *name, uid_t *uid) |
463 | { | |
464 | struct passwd *pass; | |
b5bd5542 WD |
465 | if (!name || !*name) |
466 | return 0; | |
8ef4ffd6 AT |
467 | pass = getpwnam(name); |
468 | if (pass) { | |
469 | *uid = pass->pw_uid; | |
470 | return 1; | |
471 | } | |
472 | return 0; | |
473 | } | |
474 | ||
ac13ad10 | 475 | /** Turn a group name into a gid */ |
8ef4ffd6 AT |
476 | int name_to_gid(char *name, gid_t *gid) |
477 | { | |
478 | struct group *grp; | |
b5bd5542 WD |
479 | if (!name || !*name) |
480 | return 0; | |
8ef4ffd6 AT |
481 | grp = getgrnam(name); |
482 | if (grp) { | |
483 | *gid = grp->gr_gid; | |
484 | return 1; | |
485 | } | |
486 | return 0; | |
487 | } | |
488 | ||
ac13ad10 | 489 | /** Lock a byte range in a open file */ |
31593dd6 | 490 | int lock_range(int fd, int offset, int len) |
0c515f17 | 491 | { |
31593dd6 | 492 | struct flock lock; |
0c515f17 | 493 | |
31593dd6 AT |
494 | lock.l_type = F_WRLCK; |
495 | lock.l_whence = SEEK_SET; | |
496 | lock.l_start = offset; | |
497 | lock.l_len = len; | |
498 | lock.l_pid = 0; | |
5cb37436 | 499 | |
31593dd6 | 500 | return fcntl(fd,F_SETLK,&lock) == 0; |
0c515f17 | 501 | } |
874895d5 | 502 | |
7842418b | 503 | static int filter_server_path(char *arg) |
4791825d WD |
504 | { |
505 | char *s; | |
4791825d | 506 | |
7842418b | 507 | if (server_filter_list.head) { |
4791825d WD |
508 | for (s = arg; (s = strchr(s, '/')) != NULL; ) { |
509 | *s = '\0'; | |
7842418b | 510 | if (check_filter(&server_filter_list, arg, 1) < 0) { |
4791825d WD |
511 | /* We must leave arg truncated! */ |
512 | return 1; | |
513 | } | |
514 | *s++ = '/'; | |
515 | } | |
516 | } | |
517 | return 0; | |
518 | } | |
874895d5 | 519 | |
b7061c82 WD |
520 | static void glob_expand_one(char *s, char ***argv_ptr, int *argc_ptr, |
521 | int *maxargs_ptr) | |
874895d5 | 522 | { |
b7061c82 | 523 | char **argv = *argv_ptr; |
b5bd5542 | 524 | int argc = *argc_ptr; |
b7061c82 | 525 | int maxargs = *maxargs_ptr; |
4f5b0756 | 526 | #if !defined HAVE_GLOB || !defined HAVE_GLOB_H |
b7061c82 WD |
527 | if (argc == maxargs) { |
528 | maxargs += MAX_ARGS; | |
529 | if (!(argv = realloc_array(argv, char *, maxargs))) | |
530 | out_of_memory("glob_expand_one"); | |
531 | *argv_ptr = argv; | |
532 | *maxargs_ptr = maxargs; | |
533 | } | |
4135d091 WD |
534 | if (!*s) |
535 | s = "."; | |
b5bd5542 | 536 | s = argv[argc++] = strdup(s); |
7842418b | 537 | filter_server_path(s); |
874895d5 AT |
538 | #else |
539 | glob_t globbuf; | |
874895d5 | 540 | |
b5bd5542 WD |
541 | if (maxargs <= argc) |
542 | return; | |
4135d091 WD |
543 | if (!*s) |
544 | s = "."; | |
e42c9458 | 545 | |
4135d091 | 546 | if (sanitize_paths) |
a8167c66 | 547 | s = sanitize_path(NULL, s, "", 0, NULL); |
84a63795 WD |
548 | else |
549 | s = strdup(s); | |
087bf010 | 550 | |
5cb37436 | 551 | memset(&globbuf, 0, sizeof globbuf); |
7842418b | 552 | if (!filter_server_path(s)) |
4791825d | 553 | glob(s, 0, NULL, &globbuf); |
b7061c82 WD |
554 | if (MAX((int)globbuf.gl_pathc, 1) > maxargs - argc) { |
555 | maxargs += globbuf.gl_pathc + MAX_ARGS; | |
556 | if (!(argv = realloc_array(argv, char *, maxargs))) | |
557 | out_of_memory("glob_expand_one"); | |
558 | *argv_ptr = argv; | |
559 | *maxargs_ptr = maxargs; | |
560 | } | |
b5bd5542 WD |
561 | if (globbuf.gl_pathc == 0) |
562 | argv[argc++] = s; | |
563 | else { | |
c8933031 | 564 | int i; |
b5bd5542 | 565 | free(s); |
c8933031 | 566 | for (i = 0; i < (int)globbuf.gl_pathc; i++) { |
b5bd5542 WD |
567 | if (!(argv[argc++] = strdup(globbuf.gl_pathv[i]))) |
568 | out_of_memory("glob_expand_one"); | |
569 | } | |
874895d5 AT |
570 | } |
571 | globfree(&globbuf); | |
874895d5 | 572 | #endif |
b5bd5542 | 573 | *argc_ptr = argc; |
874895d5 | 574 | } |
5a96ee05 | 575 | |
4791825d | 576 | /* This routine is only used in daemon mode. */ |
b7061c82 | 577 | void glob_expand(char *base1, char ***argv_ptr, int *argc_ptr, int *maxargs_ptr) |
087bf010 | 578 | { |
b7061c82 | 579 | char *s = (*argv_ptr)[*argc_ptr]; |
087bf010 | 580 | char *p, *q; |
ba5e128d | 581 | char *base = base1; |
4791825d | 582 | int base_len = strlen(base); |
087bf010 | 583 | |
b5bd5542 WD |
584 | if (!s || !*s) |
585 | return; | |
087bf010 | 586 | |
4791825d WD |
587 | if (strncmp(s, base, base_len) == 0) |
588 | s += base_len; | |
e42c9458 | 589 | |
b5bd5542 WD |
590 | if (!(s = strdup(s))) |
591 | out_of_memory("glob_expand"); | |
087bf010 | 592 | |
b5bd5542 WD |
593 | if (asprintf(&base," %s/", base1) <= 0) |
594 | out_of_memory("glob_expand"); | |
4791825d | 595 | base_len++; |
ba5e128d | 596 | |
b5bd5542 WD |
597 | for (q = s; *q; q = p + base_len) { |
598 | if ((p = strstr(q, base)) != NULL) | |
599 | *p = '\0'; /* split it at this point */ | |
b7061c82 | 600 | glob_expand_one(q, argv_ptr, argc_ptr, maxargs_ptr); |
b5bd5542 WD |
601 | if (!p) |
602 | break; | |
087bf010 AT |
603 | } |
604 | ||
087bf010 | 605 | free(s); |
ba5e128d | 606 | free(base); |
087bf010 | 607 | } |
5a96ee05 | 608 | |
ac13ad10 MP |
609 | /** |
610 | * Convert a string to lower case | |
611 | **/ | |
5a96ee05 AT |
612 | void strlower(char *s) |
613 | { | |
614 | while (*s) { | |
b5bd5542 WD |
615 | if (isupper(*(unsigned char *)s)) |
616 | *s = tolower(*(unsigned char *)s); | |
5a96ee05 AT |
617 | s++; |
618 | } | |
619 | } | |
e42c9458 | 620 | |
368ad70e WD |
621 | /* Join strings p1 & p2 into "dest" with a guaranteed '/' between them. (If |
622 | * p1 ends with a '/', no extra '/' is inserted.) Returns the length of both | |
a8f7e4b8 WD |
623 | * strings + 1 (if '/' was inserted), regardless of whether the null-terminated |
624 | * string fits into destsize. */ | |
368ad70e WD |
625 | size_t pathjoin(char *dest, size_t destsize, const char *p1, const char *p2) |
626 | { | |
627 | size_t len = strlcpy(dest, p1, destsize); | |
628 | if (len < destsize - 1) { | |
629 | if (!len || dest[len-1] != '/') | |
630 | dest[len++] = '/'; | |
631 | if (len < destsize - 1) | |
632 | len += strlcpy(dest + len, p2, destsize - len); | |
633 | else { | |
634 | dest[len] = '\0'; | |
635 | len += strlen(p2); | |
636 | } | |
637 | } | |
638 | else | |
639 | len += strlen(p2) + 1; /* Assume we'd insert a '/'. */ | |
640 | return len; | |
641 | } | |
642 | ||
643 | /* Join any number of strings together, putting them in "dest". The return | |
a8f7e4b8 WD |
644 | * value is the length of all the strings, regardless of whether the null- |
645 | * terminated whole fits in destsize. Your list of string pointers must end | |
646 | * with a NULL to indicate the end of the list. */ | |
368ad70e WD |
647 | size_t stringjoin(char *dest, size_t destsize, ...) |
648 | { | |
5cb37436 | 649 | va_list ap; |
368ad70e WD |
650 | size_t len, ret = 0; |
651 | const char *src; | |
652 | ||
653 | va_start(ap, destsize); | |
654 | while (1) { | |
655 | if (!(src = va_arg(ap, const char *))) | |
656 | break; | |
657 | len = strlen(src); | |
658 | ret += len; | |
659 | if (destsize > 1) { | |
660 | if (len >= destsize) | |
661 | len = destsize - 1; | |
662 | memcpy(dest, src, len); | |
663 | destsize -= len; | |
664 | dest += len; | |
665 | } | |
666 | } | |
667 | *dest = '\0'; | |
668 | va_end(ap); | |
669 | ||
670 | return ret; | |
671 | } | |
672 | ||
1d6b8f9a WD |
673 | int count_dir_elements(const char *p) |
674 | { | |
675 | int cnt = 0, new_component = 1; | |
676 | while (*p) { | |
677 | if (*p++ == '/') | |
a8167c66 | 678 | new_component = (*p != '.' || (p[1] != '/' && p[1] != '\0')); |
1d6b8f9a WD |
679 | else if (new_component) { |
680 | new_component = 0; | |
681 | cnt++; | |
682 | } | |
683 | } | |
684 | return cnt; | |
685 | } | |
686 | ||
b92693da WD |
687 | /* Turns multiple adjacent slashes into a single slash, gets rid of "./" |
688 | * elements (but not a trailing dot dir), removes a trailing slash, and | |
689 | * optionally collapses ".." elements (except for those at the start of the | |
690 | * string). If the resulting name would be empty, change it into a ".". */ | |
691 | unsigned int clean_fname(char *name, BOOL collapse_dot_dot) | |
5243c216 | 692 | { |
e012b94f | 693 | char *limit = name - 1, *t = name, *f = name; |
ebdd24d6 | 694 | int anchored; |
5243c216 | 695 | |
b5bd5542 | 696 | if (!name) |
3104620c | 697 | return 0; |
5243c216 | 698 | |
ebdd24d6 WD |
699 | if ((anchored = *f == '/') != 0) |
700 | *t++ = *f++; | |
701 | while (*f) { | |
702 | /* discard extra slashes */ | |
703 | if (*f == '/') { | |
704 | f++; | |
705 | continue; | |
5243c216 | 706 | } |
ebdd24d6 WD |
707 | if (*f == '.') { |
708 | /* discard "." dirs (but NOT a trailing '.'!) */ | |
709 | if (f[1] == '/') { | |
e012b94f | 710 | f += 2; |
ebdd24d6 WD |
711 | continue; |
712 | } | |
713 | /* collapse ".." dirs */ | |
b92693da WD |
714 | if (collapse_dot_dot |
715 | && f[1] == '.' && (f[2] == '/' || !f[2])) { | |
ebdd24d6 WD |
716 | char *s = t - 1; |
717 | if (s == name && anchored) { | |
718 | f += 2; | |
719 | continue; | |
720 | } | |
721 | while (s > limit && *--s != '/') {} | |
e012b94f | 722 | if (s != t - 1 && (s < name || *s == '/')) { |
ebdd24d6 WD |
723 | t = s + 1; |
724 | f += 2; | |
725 | continue; | |
726 | } | |
f55c2dfc | 727 | limit = t + 2; |
5243c216 AT |
728 | } |
729 | } | |
ebdd24d6 | 730 | while (*f && (*t++ = *f++) != '/') {} |
5243c216 | 731 | } |
ebdd24d6 WD |
732 | |
733 | if (t > name+anchored && t[-1] == '/') | |
734 | t--; | |
735 | if (t == name) | |
736 | *t++ = '.'; | |
737 | *t = '\0'; | |
3104620c WD |
738 | |
739 | return t - name; | |
5243c216 AT |
740 | } |
741 | ||
84a63795 WD |
742 | /* Make path appear as if a chroot had occurred. This handles a leading |
743 | * "/" (either removing it or expanding it) and any leading or embedded | |
744 | * ".." components that attempt to escape past the module's top dir. | |
b4235b31 | 745 | * |
1d6b8f9a WD |
746 | * If dest is NULL, a buffer is allocated to hold the result. It is legal |
747 | * to call with the dest and the path (p) pointing to the same buffer, but | |
748 | * rootdir will be ignored to avoid expansion of the string. | |
b4235b31 | 749 | * |
1d6b8f9a WD |
750 | * The rootdir string contains a value to use in place of a leading slash. |
751 | * Specify NULL to get the default of lp_path(module_id). | |
ac13ad10 | 752 | * |
a8167c66 WD |
753 | * The depth var is a count of how many '..'s to allow at the start of the |
754 | * path. If symlink is set, combine its value with the "p" value to get | |
755 | * the target path, and **return NULL if any '..'s try to escape**. | |
ac13ad10 | 756 | * |
b92693da | 757 | * We also clean the path in a manner similar to clean_fname() but with a |
0f78b815 | 758 | * few differences: |
b92693da WD |
759 | * |
760 | * Turns multiple adjacent slashes into a single slash, gets rid of "." dir | |
761 | * elements (INCLUDING a trailing dot dir), PRESERVES a trailing slash, and | |
762 | * ALWAYS collapses ".." elements (except for those at the start of the | |
763 | * string up to "depth" deep). If the resulting name would be empty, | |
764 | * change it into a ".". */ | |
a8167c66 WD |
765 | char *sanitize_path(char *dest, const char *p, const char *rootdir, int depth, |
766 | const char *symlink) | |
1b8e662a | 767 | { |
a8167c66 | 768 | char *start, *sanp, *save_dest = dest; |
7e43da81 | 769 | int rlen = 0, leave_one_dotdir = relative_paths; |
84a63795 | 770 | |
a8167c66 WD |
771 | if (symlink && *symlink == '/') { |
772 | p = symlink; | |
773 | symlink = ""; | |
774 | } | |
775 | ||
84a63795 WD |
776 | if (dest != p) { |
777 | int plen = strlen(p); | |
1d6b8f9a WD |
778 | if (*p == '/') { |
779 | if (!rootdir) | |
780 | rootdir = lp_path(module_id); | |
781 | rlen = strlen(rootdir); | |
782 | depth = 0; | |
84a63795 WD |
783 | p++; |
784 | } | |
785 | if (dest) { | |
786 | if (rlen + plen + 1 >= MAXPATHLEN) | |
787 | return NULL; | |
788 | } else if (!(dest = new_array(char, rlen + plen + 1))) | |
789 | out_of_memory("sanitize_path"); | |
790 | if (rlen) { | |
1d6b8f9a | 791 | memcpy(dest, rootdir, rlen); |
84a63795 WD |
792 | if (rlen > 1) |
793 | dest[rlen++] = '/'; | |
794 | } | |
795 | } | |
cb13abfe | 796 | |
84a63795 | 797 | start = sanp = dest + rlen; |
a8167c66 WD |
798 | while (1) { |
799 | if (*p == '\0') { | |
800 | if (!symlink || !*symlink) | |
801 | break; | |
802 | while (sanp != start && sanp[-1] != '/') { | |
803 | /* strip last element */ | |
804 | sanp--; | |
805 | } | |
806 | /* Append a relative symlink */ | |
807 | p = symlink; | |
808 | symlink = ""; | |
809 | } | |
2d41264e WD |
810 | /* discard leading or extra slashes */ |
811 | if (*p == '/') { | |
812 | p++; | |
813 | continue; | |
814 | } | |
b5f9e67d | 815 | /* this loop iterates once per filename component in p. |
44e2e578 | 816 | * both p (and sanp if the original had a slash) should |
b5f9e67d DD |
817 | * always be left pointing after a slash |
818 | */ | |
c284f34a | 819 | if (*p == '.' && (p[1] == '/' || p[1] == '\0')) { |
35812ea1 | 820 | if (leave_one_dotdir && p[1]) |
7e43da81 WD |
821 | leave_one_dotdir = 0; |
822 | else { | |
823 | /* skip "." component */ | |
824 | p++; | |
825 | continue; | |
826 | } | |
cb13abfe | 827 | } |
c284f34a | 828 | if (*p == '.' && p[1] == '.' && (p[2] == '/' || p[2] == '\0')) { |
cb13abfe | 829 | /* ".." component followed by slash or end */ |
8e5f029e | 830 | if (depth <= 0 || sanp != start) { |
a8167c66 WD |
831 | if (symlink && sanp == start) { |
832 | if (!save_dest) | |
833 | free(dest); | |
834 | return NULL; | |
835 | } | |
8e5f029e WD |
836 | p += 2; |
837 | if (sanp != start) { | |
838 | /* back up sanp one level */ | |
839 | --sanp; /* now pointing at slash */ | |
840 | while (sanp > start && sanp[-1] != '/') { | |
841 | /* skip back up to slash */ | |
842 | sanp--; | |
843 | } | |
b5f9e67d | 844 | } |
8e5f029e | 845 | continue; |
1b8e662a | 846 | } |
8e5f029e WD |
847 | /* allow depth levels of .. at the beginning */ |
848 | depth--; | |
849 | /* move the virtual beginning to leave the .. alone */ | |
850 | start = sanp + 3; | |
1b8e662a | 851 | } |
2d41264e WD |
852 | /* copy one component through next slash */ |
853 | while (*p && (*sanp++ = *p++) != '/') {} | |
1b8e662a | 854 | } |
84a63795 | 855 | if (sanp == dest) { |
b5f9e67d | 856 | /* ended up with nothing, so put in "." component */ |
44e2e578 | 857 | *sanp++ = '.'; |
b5f9e67d | 858 | } |
44e2e578 | 859 | *sanp = '\0'; |
1b8e662a | 860 | |
84a63795 | 861 | return dest; |
14b61c63 | 862 | } |
5243c216 | 863 | |
a8167c66 WD |
864 | /* If sanitize_paths is not set, this works exactly the same as do_stat(). |
865 | * Otherwise, we verify that no symlink takes us outside the module path. | |
866 | * If we encounter an escape attempt, we return a symlink's stat info! */ | |
867 | int safe_stat(const char *fname, STRUCT_STAT *stp) | |
868 | { | |
869 | #ifdef SUPPORT_LINKS | |
870 | char tmpbuf[MAXPATHLEN], linkbuf[MAXPATHLEN], *mod_path; | |
871 | int i, llen, mod_path_len; | |
5243c216 | 872 | |
a8167c66 WD |
873 | if (!sanitize_paths) |
874 | return do_stat(fname, stp); | |
875 | ||
876 | mod_path = lp_path(module_id); | |
877 | mod_path_len = strlen(mod_path); | |
878 | ||
879 | for (i = 0; i < 16; i++) { | |
880 | #ifdef DEBUG | |
881 | if (*fname == '/') | |
882 | assert(strncmp(fname, mod_path, mod_path_len) == 0 && fname[mod_path_len] == '/'); | |
883 | #endif | |
884 | if (do_lstat(fname, stp) < 0) | |
885 | return -1; | |
886 | if (!S_ISLNK(stp->st_mode)) | |
887 | return 0; | |
888 | if ((llen = readlink(fname, linkbuf, sizeof linkbuf - 1)) < 0) | |
889 | return -1; | |
890 | linkbuf[llen] = '\0'; | |
891 | if (*fname == '/') | |
892 | fname += mod_path_len; | |
893 | if (!(fname = sanitize_path(tmpbuf, fname, mod_path, curr_dir_depth, linkbuf))) | |
894 | break; | |
895 | } | |
896 | ||
897 | return 0; /* Leave *stp set to the last symlink. */ | |
898 | #else | |
899 | return do_stat(fname, stp); | |
900 | #endif | |
901 | } | |
902 | ||
903 | /* Like chdir(), but it keeps track of the current directory (in the | |
a16d8f2b | 904 | * global "curr_dir"), and ensures that the path size doesn't overflow. |
a8167c66 | 905 | * Also cleans the path using the clean_fname() function. */ |
4af8fe4e | 906 | int push_dir(char *dir) |
5243c216 | 907 | { |
5243c216 | 908 | static int initialised; |
4af8fe4e | 909 | unsigned int len; |
5243c216 AT |
910 | |
911 | if (!initialised) { | |
912 | initialised = 1; | |
5cb37436 | 913 | getcwd(curr_dir, sizeof curr_dir - 1); |
4af8fe4e | 914 | curr_dir_len = strlen(curr_dir); |
5243c216 AT |
915 | } |
916 | ||
4af8fe4e WD |
917 | if (!dir) /* this call was probably just to initialize */ |
918 | return 0; | |
c226b7c2 | 919 | |
4af8fe4e WD |
920 | len = strlen(dir); |
921 | if (len == 1 && *dir == '.') | |
922 | return 1; | |
5243c216 | 923 | |
4af8fe4e WD |
924 | if ((*dir == '/' ? len : curr_dir_len + 1 + len) >= sizeof curr_dir) |
925 | return 0; | |
926 | ||
927 | if (chdir(dir)) | |
928 | return 0; | |
5243c216 AT |
929 | |
930 | if (*dir == '/') { | |
4af8fe4e WD |
931 | memcpy(curr_dir, dir, len + 1); |
932 | curr_dir_len = len; | |
933 | } else { | |
934 | curr_dir[curr_dir_len++] = '/'; | |
935 | memcpy(curr_dir + curr_dir_len, dir, len + 1); | |
936 | curr_dir_len += len; | |
5243c216 AT |
937 | } |
938 | ||
b92693da | 939 | curr_dir_len = clean_fname(curr_dir, 1); |
a8167c66 WD |
940 | if (sanitize_paths) { |
941 | if (module_dirlen > curr_dir_len) | |
942 | module_dirlen = curr_dir_len; | |
943 | curr_dir_depth = count_dir_elements(curr_dir + module_dirlen); | |
944 | } | |
5243c216 | 945 | |
4af8fe4e | 946 | return 1; |
5243c216 AT |
947 | } |
948 | ||
a16d8f2b WD |
949 | /** |
950 | * Reverse a push_dir() call. You must pass in an absolute path | |
951 | * that was copied from a prior value of "curr_dir". | |
952 | **/ | |
5243c216 AT |
953 | int pop_dir(char *dir) |
954 | { | |
4af8fe4e WD |
955 | if (chdir(dir)) |
956 | return 0; | |
5243c216 | 957 | |
4af8fe4e WD |
958 | curr_dir_len = strlcpy(curr_dir, dir, sizeof curr_dir); |
959 | if (curr_dir_len >= sizeof curr_dir) | |
960 | curr_dir_len = sizeof curr_dir - 1; | |
a8167c66 WD |
961 | if (sanitize_paths) |
962 | curr_dir_depth = count_dir_elements(curr_dir + module_dirlen); | |
5243c216 | 963 | |
4af8fe4e | 964 | return 1; |
5243c216 | 965 | } |
aa9b77a5 | 966 | |
eb61be19 WD |
967 | /** |
968 | * Return a quoted string with the full pathname of the indicated filename. | |
969 | * The string " (in MODNAME)" may also be appended. The returned pointer | |
970 | * remains valid until the next time full_fname() is called. | |
971 | **/ | |
9a5ade18 | 972 | char *full_fname(const char *fn) |
eb61be19 | 973 | { |
eb61be19 WD |
974 | static char *result = NULL; |
975 | char *m1, *m2, *m3; | |
976 | char *p1, *p2; | |
977 | ||
978 | if (result) | |
979 | free(result); | |
980 | ||
981 | if (*fn == '/') | |
982 | p1 = p2 = ""; | |
983 | else { | |
984 | p1 = curr_dir; | |
bc83274a WD |
985 | for (p2 = p1; *p2 == '/'; p2++) {} |
986 | if (*p2) | |
987 | p2 = "/"; | |
eb61be19 WD |
988 | } |
989 | if (module_id >= 0) { | |
990 | m1 = " (in "; | |
991 | m2 = lp_name(module_id); | |
992 | m3 = ")"; | |
bc83274a | 993 | if (p1 == curr_dir) { |
eb61be19 WD |
994 | if (!lp_use_chroot(module_id)) { |
995 | char *p = lp_path(module_id); | |
996 | if (*p != '/' || p[1]) | |
997 | p1 += strlen(p); | |
998 | } | |
eb61be19 | 999 | } |
eb61be19 WD |
1000 | } else |
1001 | m1 = m2 = m3 = ""; | |
1002 | ||
1003 | asprintf(&result, "\"%s%s%s\"%s%s%s", p1, p2, fn, m1, m2, m3); | |
1004 | ||
1005 | return result; | |
1006 | } | |
1007 | ||
a7260c40 WD |
1008 | static char partial_fname[MAXPATHLEN]; |
1009 | ||
1010 | char *partial_dir_fname(const char *fname) | |
1011 | { | |
1012 | char *t = partial_fname; | |
1013 | int sz = sizeof partial_fname; | |
1014 | const char *fn; | |
1015 | ||
1016 | if ((fn = strrchr(fname, '/')) != NULL) { | |
1017 | fn++; | |
1018 | if (*partial_dir != '/') { | |
1019 | int len = fn - fname; | |
1020 | strncpy(t, fname, len); /* safe */ | |
1021 | t += len; | |
1022 | sz -= len; | |
1023 | } | |
1024 | } else | |
1025 | fn = fname; | |
1026 | if ((int)pathjoin(t, sz, partial_dir, fn) >= sz) | |
1027 | return NULL; | |
5aa7b20a WD |
1028 | if (server_filter_list.head) { |
1029 | static int len; | |
1030 | if (!len) | |
1031 | len = strlen(partial_dir); | |
1032 | t[len] = '\0'; | |
1033 | if (check_filter(&server_filter_list, partial_fname, 1) < 0) | |
1034 | return NULL; | |
1035 | t[len] = '/'; | |
1036 | if (check_filter(&server_filter_list, partial_fname, 0) < 0) | |
1037 | return NULL; | |
1038 | } | |
a7260c40 WD |
1039 | |
1040 | return partial_fname; | |
1041 | } | |
1042 | ||
1043 | /* If no --partial-dir option was specified, we don't need to do anything | |
1044 | * (the partial-dir is essentially '.'), so just return success. */ | |
1045 | int handle_partial_dir(const char *fname, int create) | |
1046 | { | |
1047 | char *fn, *dir; | |
1048 | ||
1049 | if (fname != partial_fname) | |
1050 | return 1; | |
1051 | if (!create && *partial_dir == '/') | |
1052 | return 1; | |
1053 | if (!(fn = strrchr(partial_fname, '/'))) | |
1054 | return 1; | |
1055 | ||
1056 | *fn = '\0'; | |
1057 | dir = partial_fname; | |
1058 | if (create) { | |
1059 | STRUCT_STAT st; | |
a7260c40 | 1060 | int statret = do_lstat(dir, &st); |
a7260c40 WD |
1061 | if (statret == 0 && !S_ISDIR(st.st_mode)) { |
1062 | if (do_unlink(dir) < 0) | |
1063 | return 0; | |
1064 | statret = -1; | |
1065 | } | |
1066 | if (statret < 0 && do_mkdir(dir, 0700) < 0) | |
1067 | return 0; | |
1068 | } else | |
1069 | do_rmdir(dir); | |
1070 | *fn = '/'; | |
1071 | ||
1072 | return 1; | |
1073 | } | |
1074 | ||
ac13ad10 MP |
1075 | /** |
1076 | * Determine if a symlink points outside the current directory tree. | |
036e70b0 MP |
1077 | * This is considered "unsafe" because e.g. when mirroring somebody |
1078 | * else's machine it might allow them to establish a symlink to | |
1079 | * /etc/passwd, and then read it through a web server. | |
1080 | * | |
4e5db0ad MP |
1081 | * Null symlinks and absolute symlinks are always unsafe. |
1082 | * | |
1083 | * Basically here we are concerned with symlinks whose target contains | |
1084 | * "..", because this might cause us to walk back up out of the | |
1085 | * transferred directory. We are not allowed to go back up and | |
1086 | * reenter. | |
1087 | * | |
036e70b0 MP |
1088 | * @param dest Target of the symlink in question. |
1089 | * | |
25d34a5c | 1090 | * @param src Top source directory currently applicable. Basically this |
036e70b0 | 1091 | * is the first parameter to rsync in a simple invocation, but it's |
25d34a5c | 1092 | * modified by flist.c in slightly complex ways. |
036e70b0 MP |
1093 | * |
1094 | * @retval True if unsafe | |
1095 | * @retval False is unsafe | |
4e5db0ad MP |
1096 | * |
1097 | * @sa t_unsafe.c | |
ac13ad10 | 1098 | **/ |
7afa3a4a | 1099 | int unsafe_symlink(const char *dest, const char *src) |
4b957c22 | 1100 | { |
7afa3a4a | 1101 | const char *name, *slash; |
4b957c22 AT |
1102 | int depth = 0; |
1103 | ||
1104 | /* all absolute and null symlinks are unsafe */ | |
b5bd5542 WD |
1105 | if (!dest || !*dest || *dest == '/') |
1106 | return 1; | |
4b957c22 AT |
1107 | |
1108 | /* find out what our safety margin is */ | |
7afa3a4a WD |
1109 | for (name = src; (slash = strchr(name, '/')) != 0; name = slash+1) { |
1110 | if (strncmp(name, "../", 3) == 0) { | |
c284f34a | 1111 | depth = 0; |
7afa3a4a | 1112 | } else if (strncmp(name, "./", 2) == 0) { |
4b957c22 AT |
1113 | /* nothing */ |
1114 | } else { | |
1115 | depth++; | |
1116 | } | |
1117 | } | |
7afa3a4a WD |
1118 | if (strcmp(name, "..") == 0) |
1119 | depth = 0; | |
4b957c22 | 1120 | |
7afa3a4a WD |
1121 | for (name = dest; (slash = strchr(name, '/')) != 0; name = slash+1) { |
1122 | if (strncmp(name, "../", 3) == 0) { | |
1123 | /* if at any point we go outside the current directory | |
1124 | then stop - it is unsafe */ | |
1125 | if (--depth < 0) | |
1126 | return 1; | |
1127 | } else if (strncmp(name, "./", 2) == 0) { | |
4b957c22 AT |
1128 | /* nothing */ |
1129 | } else { | |
1130 | depth++; | |
1131 | } | |
4b957c22 | 1132 | } |
7afa3a4a WD |
1133 | if (strcmp(name, "..") == 0) |
1134 | depth--; | |
4b957c22 | 1135 | |
4b957c22 AT |
1136 | return (depth < 0); |
1137 | } | |
375a4556 | 1138 | |
e175fb07 WD |
1139 | /* Return the int64 number as a string. If the --human-readable option was |
1140 | * specified, we may output the number in K, M, or G units. We can return | |
1141 | * up to 4 buffers at a time. */ | |
1142 | char *human_num(int64 num) | |
1143 | { | |
1144 | static char bufs[4][128]; /* more than enough room */ | |
1145 | static unsigned int n; | |
1146 | char *s; | |
1147 | ||
1148 | n = (n + 1) % (sizeof bufs / sizeof bufs[0]); | |
1149 | ||
1150 | if (human_readable) { | |
1151 | char units = '\0'; | |
7794db7c | 1152 | int mult = human_readable == 1 ? 1000 : 1024; |
e175fb07 WD |
1153 | double dnum = 0; |
1154 | if (num > mult*mult*mult) { | |
1155 | dnum = (double)num / (mult*mult*mult); | |
1156 | units = 'G'; | |
1157 | } else if (num > mult*mult) { | |
1158 | dnum = (double)num / (mult*mult); | |
1159 | units = 'M'; | |
1160 | } else if (num > mult) { | |
1161 | dnum = (double)num / mult; | |
1162 | units = 'K'; | |
1163 | } | |
1164 | if (units) { | |
1165 | sprintf(bufs[n], "%.2f%c", dnum, units); | |
1166 | return bufs[n]; | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | s = bufs[n] + sizeof bufs[0] - 1; | |
1171 | *s = '\0'; | |
1172 | ||
1173 | if (!num) | |
1174 | *--s = '0'; | |
1175 | while (num) { | |
1176 | *--s = (num % 10) + '0'; | |
1177 | num /= 10; | |
1178 | } | |
1179 | return s; | |
1180 | } | |
1181 | ||
1182 | /* Return the double number as a string. If the --human-readable option was | |
1183 | * specified, we may output the number in K, M, or G units. We use a buffer | |
1184 | * from human_num() to return our result. */ | |
1185 | char *human_dnum(double dnum, int decimal_digits) | |
1186 | { | |
1187 | char *buf = human_num(dnum); | |
1188 | int len = strlen(buf); | |
1189 | if (isdigit(*(uchar*)(buf+len-1))) { | |
1190 | /* There's extra room in buf prior to the start of the num. */ | |
1191 | buf -= decimal_digits + 1; | |
1192 | snprintf(buf, len + decimal_digits + 2, "%.*f", decimal_digits, dnum); | |
1193 | } | |
1194 | return buf; | |
1195 | } | |
f7632fc6 | 1196 | |
ac13ad10 | 1197 | /** |
b4235b31 MP |
1198 | * Return the date and time as a string |
1199 | **/ | |
f7632fc6 AT |
1200 | char *timestring(time_t t) |
1201 | { | |
1202 | static char TimeBuf[200]; | |
1203 | struct tm *tm = localtime(&t); | |
afa73c75 | 1204 | char *p; |
f7632fc6 | 1205 | |
4f5b0756 | 1206 | #ifdef HAVE_STRFTIME |
5cb37436 | 1207 | strftime(TimeBuf, sizeof TimeBuf - 1, "%Y/%m/%d %H:%M:%S", tm); |
f7632fc6 | 1208 | #else |
5cb37436 | 1209 | strlcpy(TimeBuf, asctime(tm), sizeof TimeBuf); |
f7632fc6 AT |
1210 | #endif |
1211 | ||
afa73c75 WD |
1212 | if ((p = strchr(TimeBuf, '\n')) != NULL) |
1213 | *p = '\0'; | |
f7632fc6 | 1214 | |
afa73c75 | 1215 | return TimeBuf; |
f7632fc6 AT |
1216 | } |
1217 | ||
e1bd49d6 MP |
1218 | /** |
1219 | * Sleep for a specified number of milliseconds. | |
1220 | * | |
1221 | * Always returns TRUE. (In the future it might return FALSE if | |
1222 | * interrupted.) | |
1223 | **/ | |
1224 | int msleep(int t) | |
9ec16c83 | 1225 | { |
c284f34a WD |
1226 | int tdiff = 0; |
1227 | struct timeval tval, t1, t2; | |
9ec16c83 AT |
1228 | |
1229 | gettimeofday(&t1, NULL); | |
5cb37436 | 1230 | |
9ec16c83 AT |
1231 | while (tdiff < t) { |
1232 | tval.tv_sec = (t-tdiff)/1000; | |
1233 | tval.tv_usec = 1000*((t-tdiff)%1000); | |
5cb37436 | 1234 | |
9ec16c83 AT |
1235 | errno = 0; |
1236 | select(0,NULL,NULL, NULL, &tval); | |
1237 | ||
1238 | gettimeofday(&t2, NULL); | |
5cb37436 | 1239 | tdiff = (t2.tv_sec - t1.tv_sec)*1000 + |
9ec16c83 AT |
1240 | (t2.tv_usec - t1.tv_usec)/1000; |
1241 | } | |
e1bd49d6 MP |
1242 | |
1243 | return True; | |
9ec16c83 AT |
1244 | } |
1245 | ||
c2b54076 WD |
1246 | /* Determine if two time_t values are equivalent (either exact, or in |
1247 | * the modification timestamp window established by --modify-window). | |
ac13ad10 MP |
1248 | * |
1249 | * @retval 0 if the times should be treated as the same | |
1250 | * | |
1251 | * @retval +1 if the first is later | |
1252 | * | |
1253 | * @retval -1 if the 2nd is later | |
1254 | **/ | |
c2b54076 | 1255 | int cmp_time(time_t file1, time_t file2) |
5b56cc19 | 1256 | { |
5b56cc19 | 1257 | if (file2 > file1) { |
bc6ebcd2 WD |
1258 | if (file2 - file1 <= modify_window) |
1259 | return 0; | |
5b56cc19 AT |
1260 | return -1; |
1261 | } | |
bc6ebcd2 WD |
1262 | if (file1 - file2 <= modify_window) |
1263 | return 0; | |
5b56cc19 AT |
1264 | return 1; |
1265 | } | |
1266 | ||
1267 | ||
1268 | #ifdef __INSURE__XX | |
0f8f98c8 AT |
1269 | #include <dlfcn.h> |
1270 | ||
ac13ad10 MP |
1271 | /** |
1272 | This routine is a trick to immediately catch errors when debugging | |
1273 | with insure. A xterm with a gdb is popped up when insure catches | |
1274 | a error. It is Linux specific. | |
1275 | **/ | |
0f8f98c8 AT |
1276 | int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6) |
1277 | { | |
1278 | static int (*fn)(); | |
1279 | int ret; | |
8950ac03 | 1280 | char *cmd; |
0f8f98c8 | 1281 | |
5cb37436 | 1282 | asprintf(&cmd, "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'", |
0f8f98c8 AT |
1283 | getpid(), getpid(), getpid()); |
1284 | ||
1285 | if (!fn) { | |
1286 | static void *h; | |
1287 | h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY); | |
1288 | fn = dlsym(h, "_Insure_trap_error"); | |
1289 | } | |
1290 | ||
1291 | ret = fn(a1, a2, a3, a4, a5, a6); | |
1292 | ||
1293 | system(cmd); | |
1294 | ||
8950ac03 AT |
1295 | free(cmd); |
1296 | ||
0f8f98c8 AT |
1297 | return ret; |
1298 | } | |
1299 | #endif | |
58cadc86 | 1300 | |
58cadc86 WD |
1301 | #define MALLOC_MAX 0x40000000 |
1302 | ||
1303 | void *_new_array(unsigned int size, unsigned long num) | |
1304 | { | |
1305 | if (num >= MALLOC_MAX/size) | |
1306 | return NULL; | |
1307 | return malloc(size * num); | |
1308 | } | |
1309 | ||
1310 | void *_realloc_array(void *ptr, unsigned int size, unsigned long num) | |
1311 | { | |
1312 | if (num >= MALLOC_MAX/size) | |
1313 | return NULL; | |
1314 | /* No realloc should need this, but just in case... */ | |
1315 | if (!ptr) | |
1316 | return malloc(size * num); | |
1317 | return realloc(ptr, size * num); | |
1318 | } | |
e64ae6d7 WD |
1319 | |
1320 | /* Take a filename and filename length and return the most significant | |
1321 | * filename suffix we can find. This ignores suffixes such as "~", | |
1322 | * ".bak", ".orig", ".~1~", etc. */ | |
1323 | const char *find_filename_suffix(const char *fn, int fn_len, int *len_ptr) | |
1324 | { | |
1325 | const char *suf, *s; | |
1326 | BOOL had_tilde; | |
1327 | int s_len; | |
1328 | ||
1329 | /* One or more dots at the start aren't a suffix. */ | |
1330 | while (fn_len && *fn == '.') fn++, fn_len--; | |
1331 | ||
1332 | /* Ignore the ~ in a "foo~" filename. */ | |
1333 | if (fn_len > 1 && fn[fn_len-1] == '~') | |
1334 | fn_len--, had_tilde = True; | |
1335 | else | |
1336 | had_tilde = False; | |
1337 | ||
1338 | /* Assume we don't find an suffix. */ | |
1339 | suf = ""; | |
1340 | *len_ptr = 0; | |
1341 | ||
1342 | /* Find the last significant suffix. */ | |
1343 | for (s = fn + fn_len; fn_len > 1; ) { | |
1344 | while (*--s != '.' && s != fn) {} | |
1345 | if (s == fn) | |
1346 | break; | |
1347 | s_len = fn_len - (s - fn); | |
1348 | fn_len = s - fn; | |
6012eaa1 | 1349 | if (s_len == 4) { |
e64ae6d7 WD |
1350 | if (strcmp(s+1, "bak") == 0 |
1351 | || strcmp(s+1, "old") == 0) | |
1352 | continue; | |
6012eaa1 | 1353 | } else if (s_len == 5) { |
e64ae6d7 WD |
1354 | if (strcmp(s+1, "orig") == 0) |
1355 | continue; | |
1356 | } else if (s_len > 2 && had_tilde | |
73253721 | 1357 | && s[1] == '~' && isdigit(*(uchar*)(s+2))) |
e64ae6d7 WD |
1358 | continue; |
1359 | *len_ptr = s_len; | |
1360 | suf = s; | |
1361 | if (s_len == 1) | |
1362 | break; | |
1363 | /* Determine if the suffix is all digits. */ | |
1364 | for (s++, s_len--; s_len > 0; s++, s_len--) { | |
73253721 | 1365 | if (!isdigit(*(uchar*)s)) |
e64ae6d7 WD |
1366 | return suf; |
1367 | } | |
1368 | /* An all-digit suffix may not be that signficant. */ | |
1369 | s = suf; | |
1370 | } | |
1371 | ||
1372 | return suf; | |
1373 | } | |
1374 | ||
1375 | /* This is an implementation of the Levenshtein distance algorithm. It | |
1376 | * was implemented to avoid needing a two-dimensional matrix (to save | |
1377 | * memory). It was also tweaked to try to factor in the ASCII distance | |
1378 | * between changed characters as a minor distance quantity. The normal | |
1379 | * Levenshtein units of distance (each signifying a single change between | |
1380 | * the two strings) are defined as a "UNIT". */ | |
1381 | ||
1382 | #define UNIT (1 << 16) | |
1383 | ||
1384 | uint32 fuzzy_distance(const char *s1, int len1, const char *s2, int len2) | |
1385 | { | |
1386 | uint32 a[MAXPATHLEN], diag, above, left, diag_inc, above_inc, left_inc; | |
1387 | int32 cost; | |
1388 | int i1, i2; | |
1389 | ||
1390 | if (!len1 || !len2) { | |
1391 | if (!len1) { | |
1392 | s1 = s2; | |
1393 | len1 = len2; | |
1394 | } | |
1395 | for (i1 = 0, cost = 0; i1 < len1; i1++) | |
1396 | cost += s1[i1]; | |
1397 | return (int32)len1 * UNIT + cost; | |
1398 | } | |
1399 | ||
1400 | for (i2 = 0; i2 < len2; i2++) | |
1401 | a[i2] = (i2+1) * UNIT; | |
1402 | ||
1403 | for (i1 = 0; i1 < len1; i1++) { | |
1404 | diag = i1 * UNIT; | |
1405 | above = (i1+1) * UNIT; | |
1406 | for (i2 = 0; i2 < len2; i2++) { | |
1407 | left = a[i2]; | |
1408 | if ((cost = *((uchar*)s1+i1) - *((uchar*)s2+i2)) != 0) { | |
1409 | if (cost < 0) | |
1410 | cost = UNIT - cost; | |
1411 | else | |
1412 | cost = UNIT + cost; | |
1413 | } | |
1414 | diag_inc = diag + cost; | |
1415 | left_inc = left + UNIT + *((uchar*)s1+i1); | |
1416 | above_inc = above + UNIT + *((uchar*)s2+i2); | |
1417 | a[i2] = above = left < above | |
1418 | ? (left_inc < diag_inc ? left_inc : diag_inc) | |
1419 | : (above_inc < diag_inc ? above_inc : diag_inc); | |
1420 | diag = left; | |
1421 | } | |
1422 | } | |
1423 | ||
1424 | return a[len2-1]; | |
1425 | } | |
c2b54076 WD |
1426 | |
1427 | #define BB_SLOT_SIZE (16*1024) /* Desired size in bytes */ | |
1428 | #define BB_PER_SLOT_BITS (BB_SLOT_SIZE * 8) /* Number of bits per slot */ | |
1429 | #define BB_PER_SLOT_INTS (BB_SLOT_SIZE / 4) /* Number of int32s per slot */ | |
1430 | ||
1431 | struct bitbag { | |
1432 | uint32 **bits; | |
1433 | int slot_cnt; | |
1434 | }; | |
1435 | ||
1436 | struct bitbag *bitbag_create(int max_ndx) | |
1437 | { | |
1438 | struct bitbag *bb = new(struct bitbag); | |
1439 | bb->slot_cnt = (max_ndx + BB_PER_SLOT_BITS - 1) / BB_PER_SLOT_BITS; | |
1440 | ||
1441 | if (!(bb->bits = (uint32**)calloc(bb->slot_cnt, sizeof (uint32*)))) | |
1442 | out_of_memory("bitbag_create"); | |
1443 | ||
1444 | return bb; | |
1445 | } | |
1446 | ||
1447 | void bitbag_set_bit(struct bitbag *bb, int ndx) | |
1448 | { | |
1449 | int slot = ndx / BB_PER_SLOT_BITS; | |
1450 | ndx %= BB_PER_SLOT_BITS; | |
1451 | ||
1452 | if (!bb->bits[slot]) { | |
1453 | if (!(bb->bits[slot] = (uint32*)calloc(BB_PER_SLOT_INTS, 4))) | |
1454 | out_of_memory("bitbag_set_bit"); | |
1455 | } | |
1456 | ||
1457 | bb->bits[slot][ndx/32] |= 1u << (ndx % 32); | |
1458 | } | |
1459 | ||
1460 | #if 0 /* not needed yet */ | |
1461 | void bitbag_clear_bit(struct bitbag *bb, int ndx) | |
1462 | { | |
1463 | int slot = ndx / BB_PER_SLOT_BITS; | |
1464 | ndx %= BB_PER_SLOT_BITS; | |
1465 | ||
1466 | if (!bb->bits[slot]) | |
1467 | return; | |
1468 | ||
1469 | bb->bits[slot][ndx/32] &= ~(1u << (ndx % 32)); | |
1470 | } | |
1471 | ||
1472 | int bitbag_check_bit(struct bitbag *bb, int ndx) | |
1473 | { | |
1474 | int slot = ndx / BB_PER_SLOT_BITS; | |
1475 | ndx %= BB_PER_SLOT_BITS; | |
1476 | ||
1477 | if (!bb->bits[slot]) | |
1478 | return 0; | |
1479 | ||
1480 | return bb->bits[slot][ndx/32] & (1u << (ndx % 32)) ? 1 : 0; | |
1481 | } | |
1482 | #endif | |
1483 | ||
1484 | /* Call this with -1 to start checking from 0. Returns -1 at the end. */ | |
1485 | int bitbag_next_bit(struct bitbag *bb, int after) | |
1486 | { | |
1487 | uint32 bits, mask; | |
1488 | int i, ndx = after + 1; | |
1489 | int slot = ndx / BB_PER_SLOT_BITS; | |
1490 | ndx %= BB_PER_SLOT_BITS; | |
1491 | ||
1492 | mask = (1u << (ndx % 32)) - 1; | |
1493 | for (i = ndx / 32; slot < bb->slot_cnt; slot++, i = mask = 0) { | |
1494 | if (!bb->bits[slot]) | |
1495 | continue; | |
1496 | for ( ; i < BB_PER_SLOT_INTS; i++, mask = 0) { | |
1497 | if (!(bits = bb->bits[slot][i] & ~mask)) | |
1498 | continue; | |
1499 | /* The xor magic figures out the lowest enabled bit in | |
1500 | * bits, and the switch quickly computes log2(bit). */ | |
1501 | switch (bits ^ (bits & (bits-1))) { | |
1502 | #define LOG2(n) case 1u << n: return slot*BB_PER_SLOT_BITS + i*32 + n | |
1503 | LOG2(0); LOG2(1); LOG2(2); LOG2(3); | |
1504 | LOG2(4); LOG2(5); LOG2(6); LOG2(7); | |
1505 | LOG2(8); LOG2(9); LOG2(10); LOG2(11); | |
1506 | LOG2(12); LOG2(13); LOG2(14); LOG2(15); | |
1507 | LOG2(16); LOG2(17); LOG2(18); LOG2(19); | |
1508 | LOG2(20); LOG2(21); LOG2(22); LOG2(23); | |
1509 | LOG2(24); LOG2(25); LOG2(26); LOG2(27); | |
1510 | LOG2(28); LOG2(29); LOG2(30); LOG2(31); | |
1511 | } | |
1512 | return -1; /* impossible... */ | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | return -1; | |
1517 | } |