security/nss/coreconf/nsinstall/nsinstall.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 ** Netscape portable install command.
michael@0 7 */
michael@0 8 #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
michael@0 9 #include <assert.h>
michael@0 10 #include <fcntl.h>
michael@0 11 #include <string.h>
michael@0 12 #if defined(_WINDOWS)
michael@0 13 #include <windows.h>
michael@0 14 typedef unsigned int mode_t;
michael@0 15 #else
michael@0 16 #include <grp.h>
michael@0 17 #include <pwd.h>
michael@0 18 #include <errno.h>
michael@0 19 #include <stdlib.h>
michael@0 20 #include <unistd.h>
michael@0 21 #include <utime.h>
michael@0 22 #endif
michael@0 23 #include <sys/types.h>
michael@0 24 #include <sys/stat.h>
michael@0 25 #include "pathsub.h"
michael@0 26
michael@0 27 #define HAVE_LCHOWN
michael@0 28
michael@0 29 #if defined(AIX) || defined(BSDI) || defined(HPUX) || defined(LINUX) || defined(SUNOS4) || defined(SCO) || defined(UNIXWARE) || defined(NTO) || defined(DARWIN) || defined(BEOS) || defined(__riscos__)
michael@0 30 #undef HAVE_LCHOWN
michael@0 31 #endif
michael@0 32
michael@0 33 #define HAVE_FCHMOD
michael@0 34
michael@0 35 #if defined(BEOS)
michael@0 36 #undef HAVE_FCHMOD
michael@0 37 #endif
michael@0 38
michael@0 39 #ifdef LINUX
michael@0 40 #include <getopt.h>
michael@0 41 #endif
michael@0 42
michael@0 43 #if defined(SCO) || defined(UNIXWARE) || defined(SNI) || defined(NCR) || defined(NEC)
michael@0 44 #if !defined(S_ISLNK) && defined(S_IFLNK)
michael@0 45 #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
michael@0 46 #endif
michael@0 47 #endif
michael@0 48
michael@0 49 #if defined(SNI)
michael@0 50 extern int fchmod(int fildes, mode_t mode);
michael@0 51 #endif
michael@0 52
michael@0 53
michael@0 54 #ifdef GETCWD_CANT_MALLOC
michael@0 55 /*
michael@0 56 * this should probably go into a utility library in case other applications
michael@0 57 * need it.
michael@0 58 */
michael@0 59 static char *
michael@0 60 getcwd_do_malloc(char *path, int len) {
michael@0 61
michael@0 62 if (!path) {
michael@0 63 path = malloc(PATH_MAX +1);
michael@0 64 if (!path) return NULL;
michael@0 65 }
michael@0 66 return getcwd(path, PATH_MAX);
michael@0 67 }
michael@0 68 #define GETCWD getcwd_do_malloc
michael@0 69 #else
michael@0 70 #define GETCWD getcwd
michael@0 71 #endif
michael@0 72
michael@0 73
michael@0 74 static void
michael@0 75 usage(void)
michael@0 76 {
michael@0 77 fprintf(stderr,
michael@0 78 "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
michael@0 79 " %*s [-DdltR] file [file ...] directory\n",
michael@0 80 program, (int)strlen(program), "");
michael@0 81 exit(2);
michael@0 82 }
michael@0 83
michael@0 84 /* this is more-or-less equivalent to mkdir -p */
michael@0 85 static int
michael@0 86 mkdirs(char *path, mode_t mode)
michael@0 87 {
michael@0 88 char * cp;
michael@0 89 int rv;
michael@0 90 struct stat sb;
michael@0 91
michael@0 92 if (!path || !path[0])
michael@0 93 fail("Null pointer or empty string passed to mkdirs()");
michael@0 94 while (*path == '/' && path[1] == '/')
michael@0 95 path++;
michael@0 96 for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
michael@0 97 if (cp && cp != path) {
michael@0 98 *cp = '\0';
michael@0 99 if ((stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
michael@0 100 mkdirs(path, mode) < 0) {
michael@0 101 return -1;
michael@0 102 }
michael@0 103 *cp = '/';
michael@0 104 }
michael@0 105 rv = mkdir(path, mode);
michael@0 106 if (rv) {
michael@0 107 if (errno != EEXIST)
michael@0 108 fail("mkdirs cannot make %s", path);
michael@0 109 fprintf(stderr, "directory creation race: %s\n", path);
michael@0 110 if (!stat(path, &sb) && S_ISDIR(sb.st_mode))
michael@0 111 rv = 0;
michael@0 112 }
michael@0 113 return rv;
michael@0 114 }
michael@0 115
michael@0 116 static uid_t
michael@0 117 touid(char *owner)
michael@0 118 {
michael@0 119 struct passwd *pw;
michael@0 120 uid_t uid;
michael@0 121 char *cp;
michael@0 122
michael@0 123 if (!owner || !owner[0])
michael@0 124 fail("Null pointer or empty string passed to touid()");
michael@0 125 pw = getpwnam(owner);
michael@0 126 if (pw)
michael@0 127 return pw->pw_uid;
michael@0 128 uid = strtol(owner, &cp, 0);
michael@0 129 if (uid == 0 && cp == owner)
michael@0 130 fail("cannot find uid for %s", owner);
michael@0 131 return uid;
michael@0 132 }
michael@0 133
michael@0 134 static gid_t
michael@0 135 togid(char *group)
michael@0 136 {
michael@0 137 struct group *gr;
michael@0 138 gid_t gid;
michael@0 139 char *cp;
michael@0 140
michael@0 141 if (!group || !group[0])
michael@0 142 fail("Null pointer or empty string passed to togid()");
michael@0 143 gr = getgrnam(group);
michael@0 144 if (gr)
michael@0 145 return gr->gr_gid;
michael@0 146 gid = strtol(group, &cp, 0);
michael@0 147 if (gid == 0 && cp == group)
michael@0 148 fail("cannot find gid for %s", group);
michael@0 149 return gid;
michael@0 150 }
michael@0 151
michael@0 152 void * const uninit = (void *)0xdeadbeef;
michael@0 153
michael@0 154 int
michael@0 155 main(int argc, char **argv)
michael@0 156 {
michael@0 157 char * base = uninit;
michael@0 158 char * bp = uninit;
michael@0 159 char * cp = uninit;
michael@0 160 char * cwd = 0;
michael@0 161 char * group = 0;
michael@0 162 char * linkname = 0;
michael@0 163 char * linkprefix = 0;
michael@0 164 char * name = uninit;
michael@0 165 char * owner = 0;
michael@0 166 char * todir = uninit;
michael@0 167 char * toname = uninit;
michael@0 168
michael@0 169 int bnlen = -1;
michael@0 170 int cc = 0;
michael@0 171 int dodir = 0;
michael@0 172 int dolink = 0;
michael@0 173 int dorelsymlink = 0;
michael@0 174 int dotimes = 0;
michael@0 175 int exists = 0;
michael@0 176 int fromfd = -1;
michael@0 177 int len = -1;
michael@0 178 int lplen = 0;
michael@0 179 int onlydir = 0;
michael@0 180 int opt = -1;
michael@0 181 int tdlen = -1;
michael@0 182 int tofd = -1;
michael@0 183 int wc = -1;
michael@0 184
michael@0 185 mode_t mode = 0755;
michael@0 186
michael@0 187 uid_t uid = -1;
michael@0 188 gid_t gid = -1;
michael@0 189
michael@0 190 struct stat sb;
michael@0 191 struct stat tosb;
michael@0 192 struct utimbuf utb;
michael@0 193 char buf[BUFSIZ];
michael@0 194
michael@0 195 program = strrchr(argv[0], '/');
michael@0 196 if (!program)
michael@0 197 program = strrchr(argv[0], '\\');
michael@0 198 program = program ? program+1 : argv[0];
michael@0 199
michael@0 200
michael@0 201 while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
michael@0 202 switch (opt) {
michael@0 203 case 'C': cwd = optarg; break;
michael@0 204 case 'D': onlydir = 1; break;
michael@0 205 case 'd': dodir = 1; break;
michael@0 206 case 'l': dolink = 1; break;
michael@0 207 case 'L':
michael@0 208 linkprefix = optarg;
michael@0 209 lplen = strlen(linkprefix);
michael@0 210 dolink = 1;
michael@0 211 break;
michael@0 212 case 'R': dolink = dorelsymlink = 1; break;
michael@0 213 case 'm':
michael@0 214 mode = strtoul(optarg, &cp, 8);
michael@0 215 if (mode == 0 && cp == optarg)
michael@0 216 usage();
michael@0 217 break;
michael@0 218 case 'o': owner = optarg; break;
michael@0 219 case 'g': group = optarg; break;
michael@0 220 case 't': dotimes = 1; break;
michael@0 221 default: usage();
michael@0 222 }
michael@0 223 }
michael@0 224
michael@0 225 argc -= optind;
michael@0 226 argv += optind;
michael@0 227 if (argc < 2 - onlydir)
michael@0 228 usage();
michael@0 229
michael@0 230 todir = argv[argc-1];
michael@0 231 if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
michael@0 232 mkdirs(todir, 0777) < 0) {
michael@0 233 fail("cannot mkdir -p %s", todir);
michael@0 234 }
michael@0 235 if (onlydir)
michael@0 236 return 0;
michael@0 237
michael@0 238 if (!cwd) {
michael@0 239 cwd = GETCWD(0, PATH_MAX);
michael@0 240 if (!cwd)
michael@0 241 fail("could not get CWD");
michael@0 242 }
michael@0 243
michael@0 244 /* make sure we can get into todir. */
michael@0 245 xchdir(todir);
michael@0 246 todir = GETCWD(0, PATH_MAX);
michael@0 247 if (!todir)
michael@0 248 fail("could not get CWD in todir");
michael@0 249 tdlen = strlen(todir);
michael@0 250
michael@0 251 /* back to original directory. */
michael@0 252 xchdir(cwd);
michael@0 253
michael@0 254 uid = owner ? touid(owner) : -1;
michael@0 255 gid = group ? togid(group) : -1;
michael@0 256
michael@0 257 while (--argc > 0) {
michael@0 258 name = *argv++;
michael@0 259 len = strlen(name);
michael@0 260 base = xbasename(name);
michael@0 261 bnlen = strlen(base);
michael@0 262 toname = (char*)xmalloc(tdlen + 1 + bnlen + 1);
michael@0 263 sprintf(toname, "%s/%s", todir, base);
michael@0 264 retry:
michael@0 265 exists = (lstat(toname, &tosb) == 0);
michael@0 266
michael@0 267 if (dodir) {
michael@0 268 /* -d means create a directory, always */
michael@0 269 if (exists && !S_ISDIR(tosb.st_mode)) {
michael@0 270 int rv = unlink(toname);
michael@0 271 if (rv)
michael@0 272 fail("cannot unlink %s", toname);
michael@0 273 exists = 0;
michael@0 274 }
michael@0 275 if (!exists && mkdir(toname, mode) < 0) {
michael@0 276 /* we probably have two nsinstall programs in a race here. */
michael@0 277 if (errno == EEXIST && !stat(toname, &sb) &&
michael@0 278 S_ISDIR(sb.st_mode)) {
michael@0 279 fprintf(stderr, "directory creation race: %s\n", toname);
michael@0 280 goto retry;
michael@0 281 }
michael@0 282 fail("cannot make directory %s", toname);
michael@0 283 }
michael@0 284 if ((owner || group) && chown(toname, uid, gid) < 0)
michael@0 285 fail("cannot change owner of %s", toname);
michael@0 286 } else if (dolink) {
michael@0 287 if (*name == '/') {
michael@0 288 /* source is absolute pathname, link to it directly */
michael@0 289 linkname = 0;
michael@0 290 } else {
michael@0 291 if (linkprefix) {
michael@0 292 /* -L implies -l and prefixes names with a $cwd arg. */
michael@0 293 len += lplen + 1;
michael@0 294 linkname = (char*)xmalloc(len + 1);
michael@0 295 sprintf(linkname, "%s/%s", linkprefix, name);
michael@0 296 } else if (dorelsymlink) {
michael@0 297 /* Symlink the relative path from todir to source name. */
michael@0 298 linkname = (char*)xmalloc(PATH_MAX);
michael@0 299
michael@0 300 if (*todir == '/') {
michael@0 301 /* todir is absolute: skip over common prefix. */
michael@0 302 lplen = relatepaths(todir, cwd, linkname);
michael@0 303 strcpy(linkname + lplen, name);
michael@0 304 } else {
michael@0 305 /* todir is named by a relative path: reverse it. */
michael@0 306 reversepath(todir, name, len, linkname);
michael@0 307 xchdir(cwd);
michael@0 308 }
michael@0 309
michael@0 310 len = strlen(linkname);
michael@0 311 }
michael@0 312 name = linkname;
michael@0 313 }
michael@0 314
michael@0 315 /* Check for a pre-existing symlink with identical content. */
michael@0 316 if (exists &&
michael@0 317 (!S_ISLNK(tosb.st_mode) ||
michael@0 318 readlink(toname, buf, sizeof buf) != len ||
michael@0 319 strncmp(buf, name, len) != 0)) {
michael@0 320 int rmrv;
michael@0 321 rmrv = (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
michael@0 322 if (rmrv < 0) {
michael@0 323 fail("destination exists, cannot remove %s", toname);
michael@0 324 }
michael@0 325 exists = 0;
michael@0 326 }
michael@0 327 if (!exists && symlink(name, toname) < 0) {
michael@0 328 if (errno == EEXIST) {
michael@0 329 fprintf(stderr, "symlink creation race: %s\n", toname);
michael@0 330 fail("symlink was attempted in working directory %s "
michael@0 331 "from %s to %s.\n", cwd, name, toname);
michael@0 332 goto retry;
michael@0 333 }
michael@0 334 diagnosePath(toname);
michael@0 335 fail("cannot make symbolic link %s", toname);
michael@0 336 }
michael@0 337 #ifdef HAVE_LCHOWN
michael@0 338 if ((owner || group) && lchown(toname, uid, gid) < 0)
michael@0 339 fail("cannot change owner of %s", toname);
michael@0 340 #endif
michael@0 341
michael@0 342 if (linkname) {
michael@0 343 free(linkname);
michael@0 344 linkname = 0;
michael@0 345 }
michael@0 346 } else {
michael@0 347 /* Copy from name to toname, which might be the same file. */
michael@0 348 fromfd = open(name, O_RDONLY);
michael@0 349 if (fromfd < 0 || fstat(fromfd, &sb) < 0)
michael@0 350 fail("cannot access %s", name);
michael@0 351 if (exists &&
michael@0 352 (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0)) {
michael@0 353 int rmrv;
michael@0 354 rmrv = (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
michael@0 355 if (rmrv < 0) {
michael@0 356 fail("destination exists, cannot remove %s", toname);
michael@0 357 }
michael@0 358 }
michael@0 359 tofd = open(toname, O_CREAT | O_WRONLY, 0666);
michael@0 360 if (tofd < 0)
michael@0 361 fail("cannot create %s", toname);
michael@0 362
michael@0 363 bp = buf;
michael@0 364 while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
michael@0 365 while ((wc = write(tofd, bp, cc)) > 0) {
michael@0 366 if ((cc -= wc) == 0)
michael@0 367 break;
michael@0 368 bp += wc;
michael@0 369 }
michael@0 370 if (wc < 0)
michael@0 371 fail("cannot write to %s", toname);
michael@0 372 }
michael@0 373 if (cc < 0)
michael@0 374 fail("cannot read from %s", name);
michael@0 375
michael@0 376 if (ftruncate(tofd, sb.st_size) < 0)
michael@0 377 fail("cannot truncate %s", toname);
michael@0 378 if (dotimes) {
michael@0 379 utb.actime = sb.st_atime;
michael@0 380 utb.modtime = sb.st_mtime;
michael@0 381 if (utime(toname, &utb) < 0)
michael@0 382 fail("cannot set times of %s", toname);
michael@0 383 }
michael@0 384 #ifdef HAVE_FCHMOD
michael@0 385 if (fchmod(tofd, mode) < 0)
michael@0 386 #else
michael@0 387 if (chmod(toname, mode) < 0)
michael@0 388 #endif
michael@0 389 fail("cannot change mode of %s", toname);
michael@0 390
michael@0 391 if ((owner || group) && fchown(tofd, uid, gid) < 0)
michael@0 392 fail("cannot change owner of %s", toname);
michael@0 393
michael@0 394 /* Must check for delayed (NFS) write errors on close. */
michael@0 395 if (close(tofd) < 0)
michael@0 396 fail("close reports write error on %s", toname);
michael@0 397 close(fromfd);
michael@0 398 }
michael@0 399
michael@0 400 free(toname);
michael@0 401 }
michael@0 402
michael@0 403 free(cwd);
michael@0 404 free(todir);
michael@0 405 return 0;
michael@0 406 }
michael@0 407

mercurial