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