| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <stdlib.h> | 
|  | 18 | #include <stdio.h> | 
|  | 19 | #include <string.h> | 
|  | 20 |  | 
|  | 21 | #include <sys/stat.h> | 
|  | 22 | #include <sys/types.h> | 
|  | 23 | #include <dirent.h> | 
|  | 24 | #include <utime.h> | 
|  | 25 | #include <unistd.h> | 
|  | 26 |  | 
|  | 27 | #include <errno.h> | 
|  | 28 | #include <private/android_filesystem_config.h> | 
|  | 29 |  | 
|  | 30 | #ifndef ADB_NON_ANDROID | 
|  | 31 | #include <selinux/android.h> | 
|  | 32 | #endif | 
|  | 33 | #include "sysdeps.h" | 
|  | 34 |  | 
|  | 35 | #define TRACE_TAG  TRACE_SYNC | 
|  | 36 | #include "adb.h" | 
|  | 37 | #include "file_sync_service.h" | 
|  | 38 |  | 
|  | 39 | /* TODO: use fs_config to configure permissions on /data */ | 
|  | 40 | static bool is_on_system(const char *name) { | 
|  | 41 | const char *SYSTEM = "/system/"; | 
|  | 42 | return (strncmp(SYSTEM, name, strlen(SYSTEM)) == 0); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | static bool is_on_vendor(const char *name) { | 
|  | 46 | const char *VENDOR = "/vendor/"; | 
|  | 47 | return (strncmp(VENDOR, name, strlen(VENDOR)) == 0); | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | static int mkdirs(char *name) | 
|  | 51 | { | 
|  | 52 | int ret; | 
|  | 53 | char *x = name + 1; | 
|  | 54 | uid_t uid = -1; | 
|  | 55 | gid_t gid = -1; | 
|  | 56 | unsigned int mode = 0775; | 
|  | 57 | uint64_t cap = 0; | 
|  | 58 |  | 
|  | 59 | if(name[0] != '/') return -1; | 
|  | 60 |  | 
|  | 61 | for(;;) { | 
|  | 62 | x = adb_dirstart(x); | 
|  | 63 | if(x == 0) return 0; | 
|  | 64 | *x = 0; | 
|  | 65 | if (is_on_system(name) || is_on_vendor(name)) { | 
|  | 66 | fs_config(name, 1, &uid, &gid, &mode, &cap); | 
|  | 67 | } | 
|  | 68 | ret = adb_mkdir(name, mode); | 
|  | 69 | if((ret < 0) && (errno != EEXIST)) { | 
|  | 70 | D("mkdir(\"%s\") -> %s\n", name, strerror(errno)); | 
|  | 71 | *x = '/'; | 
|  | 72 | return ret; | 
|  | 73 | } else if(ret == 0) { | 
|  | 74 | ret = chown(name, uid, gid); | 
|  | 75 | if (ret < 0) { | 
|  | 76 | *x = '/'; | 
|  | 77 | return ret; | 
|  | 78 | } | 
|  | 79 | #ifndef ADB_NON_ANDROID | 
|  | 80 | selinux_android_restorecon(name, 0); | 
|  | 81 | #endif | 
|  | 82 | } | 
|  | 83 | *x++ = '/'; | 
|  | 84 | } | 
|  | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static int do_stat(int s, const char *path) | 
|  | 89 | { | 
|  | 90 | syncmsg msg; | 
|  | 91 | struct stat st; | 
|  | 92 |  | 
|  | 93 | msg.stat.id = ID_STAT; | 
|  | 94 |  | 
|  | 95 | if(lstat(path, &st)) { | 
|  | 96 | msg.stat.mode = 0; | 
|  | 97 | msg.stat.size = 0; | 
|  | 98 | msg.stat.time = 0; | 
|  | 99 | } else { | 
|  | 100 | msg.stat.mode = htoll(st.st_mode); | 
|  | 101 | msg.stat.size = htoll(st.st_size); | 
|  | 102 | msg.stat.time = htoll(st.st_mtime); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | return writex(s, &msg.stat, sizeof(msg.stat)); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | static int do_list(int s, const char *path) | 
|  | 109 | { | 
|  | 110 | DIR *d; | 
|  | 111 | struct dirent *de; | 
|  | 112 | struct stat st; | 
|  | 113 | syncmsg msg; | 
| qjb | 0300fcc | 2023-08-17 03:54:44 -0700 | [diff] [blame^] | 114 | unsigned int len; | 
| rjw | 2e8229f | 2022-02-15 21:08:12 +0800 | [diff] [blame] | 115 |  | 
|  | 116 | char tmp[1024 + 256 + 1]; | 
|  | 117 | char *fname; | 
|  | 118 |  | 
|  | 119 | len = strlen(path); | 
|  | 120 | memcpy(tmp, path, len); | 
|  | 121 | tmp[len] = '/'; | 
|  | 122 | fname = tmp + len + 1; | 
|  | 123 |  | 
|  | 124 | msg.dent.id = ID_DENT; | 
|  | 125 |  | 
|  | 126 | d = opendir(path); | 
|  | 127 | if(d == 0) goto done; | 
|  | 128 |  | 
|  | 129 | while((de = readdir(d))) { | 
|  | 130 | int len = strlen(de->d_name); | 
|  | 131 |  | 
|  | 132 | /* not supposed to be possible, but | 
|  | 133 | if it does happen, let's not buffer overrun */ | 
|  | 134 | if(len > 256) continue; | 
|  | 135 |  | 
|  | 136 | strcpy(fname, de->d_name); | 
|  | 137 | if(lstat(tmp, &st) == 0) { | 
|  | 138 | msg.dent.mode = htoll(st.st_mode); | 
|  | 139 | msg.dent.size = htoll(st.st_size); | 
|  | 140 | msg.dent.time = htoll(st.st_mtime); | 
|  | 141 | msg.dent.namelen = htoll(len); | 
|  | 142 |  | 
|  | 143 | if(writex(s, &msg.dent, sizeof(msg.dent)) || | 
|  | 144 | writex(s, de->d_name, len)) { | 
|  | 145 | closedir(d); | 
|  | 146 | return -1; | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | closedir(d); | 
|  | 152 |  | 
|  | 153 | done: | 
|  | 154 | msg.dent.id = ID_DONE; | 
|  | 155 | msg.dent.mode = 0; | 
|  | 156 | msg.dent.size = 0; | 
|  | 157 | msg.dent.time = 0; | 
|  | 158 | msg.dent.namelen = 0; | 
|  | 159 | return writex(s, &msg.dent, sizeof(msg.dent)); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | static int fail_message(int s, const char *reason) | 
|  | 163 | { | 
|  | 164 | syncmsg msg; | 
|  | 165 | int len = strlen(reason); | 
|  | 166 |  | 
|  | 167 | D("sync: failure: %s\n", reason); | 
|  | 168 |  | 
|  | 169 | msg.data.id = ID_FAIL; | 
|  | 170 | msg.data.size = htoll(len); | 
|  | 171 | if(writex(s, &msg.data, sizeof(msg.data)) || | 
|  | 172 | writex(s, reason, len)) { | 
|  | 173 | return -1; | 
|  | 174 | } else { | 
|  | 175 | return 0; | 
|  | 176 | } | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | static int fail_errno(int s) | 
|  | 180 | { | 
|  | 181 | return fail_message(s, strerror(errno)); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | static int handle_send_file(int s, char *path, uid_t uid, | 
|  | 185 | gid_t gid, mode_t mode, char *buffer, bool do_unlink) | 
|  | 186 | { | 
|  | 187 | syncmsg msg; | 
|  | 188 | unsigned int timestamp = 0; | 
|  | 189 | int fd; | 
|  | 190 |  | 
|  | 191 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); | 
|  | 192 | if(fd < 0 && errno == ENOENT) { | 
|  | 193 | if(mkdirs(path) != 0) { | 
|  | 194 | if(fail_errno(s)) | 
|  | 195 | return -1; | 
|  | 196 | fd = -1; | 
|  | 197 | } else { | 
|  | 198 | fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode); | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 | if(fd < 0 && errno == EEXIST) { | 
|  | 202 | fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode); | 
|  | 203 | } | 
|  | 204 | if(fd < 0) { | 
|  | 205 | if(fail_errno(s)) | 
|  | 206 | return -1; | 
|  | 207 | fd = -1; | 
|  | 208 | } else { | 
|  | 209 | if(fchown(fd, uid, gid) != 0) { | 
|  | 210 | fail_errno(s); | 
|  | 211 | errno = 0; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | /* | 
|  | 215 | * fchown clears the setuid bit - restore it if present. | 
|  | 216 | * Ignore the result of calling fchmod. It's not supported | 
|  | 217 | * by all filesystems. b/12441485 | 
|  | 218 | */ | 
|  | 219 | fchmod(fd, mode); | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | for(;;) { | 
|  | 223 | unsigned int len; | 
|  | 224 |  | 
|  | 225 | if(readx(s, &msg.data, sizeof(msg.data))) | 
|  | 226 | goto fail; | 
|  | 227 |  | 
|  | 228 | if(msg.data.id != ID_DATA) { | 
|  | 229 | if(msg.data.id == ID_DONE) { | 
|  | 230 | timestamp = ltohl(msg.data.size); | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | fail_message(s, "invalid data message"); | 
|  | 234 | goto fail; | 
|  | 235 | } | 
|  | 236 | len = ltohl(msg.data.size); | 
|  | 237 | if(len > SYNC_DATA_MAX) { | 
|  | 238 | fail_message(s, "oversize data message"); | 
|  | 239 | goto fail; | 
|  | 240 | } | 
|  | 241 | if(readx(s, buffer, len)) | 
|  | 242 | goto fail; | 
|  | 243 |  | 
|  | 244 | if(fd < 0) | 
|  | 245 | continue; | 
|  | 246 | if(writex(fd, buffer, len)) { | 
|  | 247 | int saved_errno = errno; | 
|  | 248 | adb_close(fd); | 
|  | 249 | if (do_unlink) adb_unlink(path); | 
|  | 250 | fd = -1; | 
|  | 251 | errno = saved_errno; | 
|  | 252 | if(fail_errno(s)) return -1; | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | if(fd >= 0) { | 
|  | 257 | struct utimbuf u; | 
|  | 258 | adb_close(fd); | 
|  | 259 | #ifndef ADB_NON_ANDROID | 
|  | 260 | selinux_android_restorecon(path, 0); | 
|  | 261 | #endif | 
|  | 262 | u.actime = timestamp; | 
|  | 263 | u.modtime = timestamp; | 
|  | 264 | utime(path, &u); | 
|  | 265 |  | 
|  | 266 | msg.status.id = ID_OKAY; | 
|  | 267 | msg.status.msglen = 0; | 
|  | 268 | if(writex(s, &msg.status, sizeof(msg.status))) | 
|  | 269 | return -1; | 
|  | 270 | } | 
|  | 271 | return 0; | 
|  | 272 |  | 
|  | 273 | fail: | 
|  | 274 | if(fd >= 0) | 
|  | 275 | adb_close(fd); | 
|  | 276 | if (do_unlink) adb_unlink(path); | 
|  | 277 | return -1; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | #ifdef HAVE_SYMLINKS | 
|  | 281 | static int handle_send_link(int s, char *path, char *buffer) | 
|  | 282 | { | 
|  | 283 | syncmsg msg; | 
|  | 284 | unsigned int len; | 
|  | 285 | int ret; | 
|  | 286 |  | 
|  | 287 | if(readx(s, &msg.data, sizeof(msg.data))) | 
|  | 288 | return -1; | 
|  | 289 |  | 
|  | 290 | if(msg.data.id != ID_DATA) { | 
|  | 291 | fail_message(s, "invalid data message: expected ID_DATA"); | 
|  | 292 | return -1; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | len = ltohl(msg.data.size); | 
|  | 296 | if(len > SYNC_DATA_MAX) { | 
|  | 297 | fail_message(s, "oversize data message"); | 
|  | 298 | return -1; | 
|  | 299 | } | 
|  | 300 | if(readx(s, buffer, len)) | 
|  | 301 | return -1; | 
|  | 302 |  | 
|  | 303 | ret = symlink(buffer, path); | 
|  | 304 | if(ret && errno == ENOENT) { | 
|  | 305 | if(mkdirs(path) != 0) { | 
|  | 306 | fail_errno(s); | 
|  | 307 | return -1; | 
|  | 308 | } | 
|  | 309 | ret = symlink(buffer, path); | 
|  | 310 | } | 
|  | 311 | if(ret) { | 
|  | 312 | fail_errno(s); | 
|  | 313 | return -1; | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | if(readx(s, &msg.data, sizeof(msg.data))) | 
|  | 317 | return -1; | 
|  | 318 |  | 
|  | 319 | if(msg.data.id == ID_DONE) { | 
|  | 320 | msg.status.id = ID_OKAY; | 
|  | 321 | msg.status.msglen = 0; | 
|  | 322 | if(writex(s, &msg.status, sizeof(msg.status))) | 
|  | 323 | return -1; | 
|  | 324 | } else { | 
|  | 325 | fail_message(s, "invalid data message: expected ID_DONE"); | 
|  | 326 | return -1; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | return 0; | 
|  | 330 | } | 
|  | 331 | #endif /* HAVE_SYMLINKS */ | 
|  | 332 |  | 
|  | 333 | static int do_send(int s, char *path, char *buffer) | 
|  | 334 | { | 
|  | 335 | char *tmp; | 
|  | 336 | unsigned int mode; | 
|  | 337 | int is_link, ret; | 
|  | 338 | bool do_unlink; | 
|  | 339 |  | 
|  | 340 | tmp = strrchr(path,','); | 
|  | 341 | if(tmp) { | 
|  | 342 | *tmp = 0; | 
|  | 343 | errno = 0; | 
|  | 344 | mode = strtoul(tmp + 1, NULL, 0); | 
|  | 345 | #ifndef HAVE_SYMLINKS | 
|  | 346 | is_link = 0; | 
|  | 347 | #else | 
|  | 348 | is_link = S_ISLNK((mode_t) mode); | 
|  | 349 | #endif | 
|  | 350 | mode &= 0777; | 
|  | 351 | } | 
|  | 352 | if(!tmp || errno) { | 
|  | 353 | mode = 0644; | 
|  | 354 | is_link = 0; | 
|  | 355 | do_unlink = true; | 
|  | 356 | } else { | 
|  | 357 | struct stat st; | 
|  | 358 | /* Don't delete files before copying if they are not "regular" */ | 
|  | 359 | do_unlink = lstat(path, &st) || S_ISREG(st.st_mode) || S_ISLNK(st.st_mode); | 
|  | 360 | if (do_unlink) { | 
|  | 361 | adb_unlink(path); | 
|  | 362 | } | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | #ifdef HAVE_SYMLINKS | 
|  | 366 | if(is_link) | 
|  | 367 | ret = handle_send_link(s, path, buffer); | 
|  | 368 | else { | 
|  | 369 | #else | 
|  | 370 | { | 
|  | 371 | #endif | 
|  | 372 | uid_t uid = -1; | 
|  | 373 | gid_t gid = -1; | 
|  | 374 | uint64_t cap = 0; | 
|  | 375 |  | 
|  | 376 | /* copy user permission bits to "group" and "other" permissions */ | 
|  | 377 | mode |= ((mode >> 3) & 0070); | 
|  | 378 | mode |= ((mode >> 3) & 0007); | 
|  | 379 |  | 
|  | 380 | tmp = path; | 
|  | 381 | if(*tmp == '/') { | 
|  | 382 | tmp++; | 
|  | 383 | } | 
|  | 384 | if (is_on_system(path) || is_on_vendor(path)) { | 
|  | 385 | fs_config(tmp, 0, &uid, &gid, &mode, &cap); | 
|  | 386 | } | 
|  | 387 | ret = handle_send_file(s, path, uid, gid, mode, buffer, do_unlink); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | return ret; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | static int do_recv(int s, const char *path, char *buffer) | 
|  | 394 | { | 
|  | 395 | syncmsg msg; | 
|  | 396 | int fd, r; | 
|  | 397 |  | 
|  | 398 | fd = adb_open(path, O_RDONLY | O_CLOEXEC); | 
|  | 399 | if(fd < 0) { | 
|  | 400 | if(fail_errno(s)) return -1; | 
|  | 401 | return 0; | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | msg.data.id = ID_DATA; | 
|  | 405 | for(;;) { | 
|  | 406 | r = adb_read(fd, buffer, SYNC_DATA_MAX); | 
|  | 407 | if(r <= 0) { | 
|  | 408 | if(r == 0) break; | 
|  | 409 | if(errno == EINTR) continue; | 
|  | 410 | r = fail_errno(s); | 
|  | 411 | adb_close(fd); | 
|  | 412 | return r; | 
|  | 413 | } | 
|  | 414 | msg.data.size = htoll(r); | 
|  | 415 | if(writex(s, &msg.data, sizeof(msg.data)) || | 
|  | 416 | writex(s, buffer, r)) { | 
|  | 417 | adb_close(fd); | 
|  | 418 | return -1; | 
|  | 419 | } | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | adb_close(fd); | 
|  | 423 |  | 
|  | 424 | msg.data.id = ID_DONE; | 
|  | 425 | msg.data.size = 0; | 
|  | 426 | if(writex(s, &msg.data, sizeof(msg.data))) { | 
|  | 427 | return -1; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | return 0; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | void file_sync_service(int fd, void *cookie) | 
|  | 434 | { | 
|  | 435 | syncmsg msg; | 
|  | 436 | char name[1025]; | 
|  | 437 | unsigned namelen; | 
|  | 438 |  | 
|  | 439 | char *buffer = malloc(SYNC_DATA_MAX); | 
|  | 440 | if(buffer == 0) goto fail; | 
|  | 441 |  | 
|  | 442 | for(;;) { | 
|  | 443 | D("sync: waiting for command\n"); | 
|  | 444 |  | 
|  | 445 | if(readx(fd, &msg.req, sizeof(msg.req))) { | 
|  | 446 | fail_message(fd, "command read failure"); | 
|  | 447 | break; | 
|  | 448 | } | 
|  | 449 | namelen = ltohl(msg.req.namelen); | 
|  | 450 | if(namelen > 1024) { | 
|  | 451 | fail_message(fd, "invalid namelen"); | 
|  | 452 | break; | 
|  | 453 | } | 
|  | 454 | if(readx(fd, name, namelen)) { | 
|  | 455 | fail_message(fd, "filename read failure"); | 
|  | 456 | break; | 
|  | 457 | } | 
|  | 458 | name[namelen] = 0; | 
|  | 459 |  | 
|  | 460 | msg.req.namelen = 0; | 
|  | 461 | D("sync: '%s' '%s'\n", (char*) &msg.req, name); | 
|  | 462 |  | 
|  | 463 | switch(msg.req.id) { | 
|  | 464 | case ID_STAT: | 
|  | 465 | if(do_stat(fd, name)) goto fail; | 
|  | 466 | break; | 
|  | 467 | case ID_LIST: | 
|  | 468 | if(do_list(fd, name)) goto fail; | 
|  | 469 | break; | 
|  | 470 | case ID_SEND: | 
|  | 471 | if(do_send(fd, name, buffer)) goto fail; | 
|  | 472 | break; | 
|  | 473 | case ID_RECV: | 
|  | 474 | if(do_recv(fd, name, buffer)) goto fail; | 
|  | 475 | break; | 
|  | 476 | case ID_QUIT: | 
|  | 477 | goto fail; | 
|  | 478 | default: | 
|  | 479 | fail_message(fd, "unknown command"); | 
|  | 480 | goto fail; | 
|  | 481 | } | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | fail: | 
|  | 485 | if(buffer != 0) free(buffer); | 
|  | 486 | D("sync: done\n"); | 
|  | 487 | adb_close(fd); | 
|  | 488 | } |