nsinstall.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. ** install command.
  43. **
  44. ** Brendan Eich, 7/20/95
  45. */
  46. #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
  47. #include <assert.h>
  48. #include <fcntl.h>
  49. #include <grp.h>
  50. #include <pwd.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #include <utime.h>
  56. #include <sys/types.h>
  57. #include <sys/stat.h>
  58. #include "pathsub.h"
  59. #define HAVE_LCHOWN
  60. #if defined(AIXV3) || defined(BSDI) || defined(HPUX) || defined(LINUX) || defined(SUNOS4) || defined(SCO) || defined(SNI)
  61. #undef HAVE_LCHOWN
  62. #endif
  63. #ifdef LINUX
  64. # include <getopt.h>
  65. #endif
  66. #if defined(SCO) || defined(UNIXWARE) || defined(SNI) || defined (NCR) || defined(UnixWare)
  67. #if !defined(S_ISLNK) && defined(S_IFLNK)
  68. #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
  69. #endif
  70. #endif
  71. static void
  72. usage(void)
  73. {
  74. fprintf(stderr,
  75. "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
  76. " %*s [-DdltR] file [file ...] directory\n",
  77. program, strlen(program), "");
  78. exit(2);
  79. }
  80. static int
  81. mkdirs(char *path, mode_t mode)
  82. {
  83. char *cp;
  84. struct stat sb;
  85. while (*path == '/' && path[1] == '/')
  86. path++;
  87. while ((cp = strrchr(path, '/')) && cp[1] == '\0')
  88. *cp = '\0';
  89. if (cp && cp != path) {
  90. *cp = '\0';
  91. if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  92. mkdirs(path, mode) < 0) {
  93. return -1;
  94. }
  95. *cp = '/';
  96. }
  97. return mkdir(path, mode);
  98. }
  99. static uid_t
  100. touid(char *owner)
  101. {
  102. struct passwd *pw;
  103. uid_t uid;
  104. char *cp;
  105. pw = getpwnam(owner);
  106. if (pw)
  107. return pw->pw_uid;
  108. uid = strtol(owner, &cp, 0);
  109. if (uid == 0 && cp == owner)
  110. fail("cannot find uid for %s", owner);
  111. return uid;
  112. }
  113. static gid_t
  114. togid(char *group)
  115. {
  116. struct group *gr;
  117. gid_t gid;
  118. char *cp;
  119. gr = getgrnam(group);
  120. if (gr)
  121. return gr->gr_gid;
  122. gid = strtol(group, &cp, 0);
  123. if (gid == 0 && cp == group)
  124. fail("cannot find gid for %s", group);
  125. return gid;
  126. }
  127. int
  128. main(int argc, char **argv)
  129. {
  130. int onlydir, dodir, dolink, dorelsymlink, dotimes;
  131. mode_t mode;
  132. char *linkprefix, *owner, *group;
  133. int opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
  134. char *cp, *cwd, *todir, *toname, *name, *base, *linkname;
  135. uid_t uid;
  136. gid_t gid;
  137. char *bp, buf[BUFSIZ];
  138. struct stat sb, tosb;
  139. struct utimbuf utb;
  140. program = argv[0];
  141. cwd = 0;
  142. onlydir = dodir = dolink = dorelsymlink = dotimes = 0;
  143. mode = 0755;
  144. linkprefix = owner = group = 0;
  145. while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
  146. switch (opt) {
  147. case 'C':
  148. cwd = optarg;
  149. break;
  150. case 'D':
  151. onlydir = 1;
  152. break;
  153. case 'd':
  154. dodir = 1;
  155. break;
  156. case 'l':
  157. dolink = 1;
  158. break;
  159. case 'L':
  160. linkprefix = optarg;
  161. lplen = strlen(linkprefix);
  162. dolink = 1;
  163. break;
  164. case 'R':
  165. dolink = dorelsymlink = 1;
  166. break;
  167. case 'm':
  168. mode = strtoul(optarg, &cp, 8);
  169. if (mode == 0 && cp == optarg)
  170. usage();
  171. break;
  172. case 'o':
  173. owner = optarg;
  174. break;
  175. case 'g':
  176. group = optarg;
  177. break;
  178. case 't':
  179. dotimes = 1;
  180. break;
  181. default:
  182. usage();
  183. }
  184. }
  185. argc -= optind;
  186. argv += optind;
  187. if (argc < 2 - onlydir)
  188. usage();
  189. todir = argv[argc-1];
  190. if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  191. mkdirs(todir, 0777) < 0) {
  192. fail("cannot make directory %s", todir);
  193. }
  194. if (onlydir)
  195. return 0;
  196. if (!cwd)
  197. cwd = getcwd(0, PATH_MAX);
  198. xchdir(todir);
  199. todir = getcwd(0, PATH_MAX);
  200. tdlen = strlen(todir);
  201. xchdir(cwd);
  202. tdlen = strlen(todir);
  203. uid = owner ? touid(owner) : -1;
  204. gid = group ? togid(group) : -1;
  205. while (--argc > 0) {
  206. name = *argv++;
  207. len = strlen(name);
  208. base = xbasename(name);
  209. bnlen = strlen(base);
  210. toname = xmalloc(tdlen + 1 + bnlen + 1);
  211. sprintf(toname, "%s/%s", todir, base);
  212. exists = (lstat(toname, &tosb) == 0);
  213. if (dodir) {
  214. /* -d means create a directory, always */
  215. if (exists && !S_ISDIR(tosb.st_mode)) {
  216. (void) unlink(toname);
  217. exists = 0;
  218. }
  219. if (!exists && mkdir(toname, mode) < 0)
  220. fail("cannot make directory %s", toname);
  221. if ((owner || group) && chown(toname, uid, gid) < 0)
  222. fail("cannot change owner of %s", toname);
  223. } else if (dolink) {
  224. if (*name == '/') {
  225. /* source is absolute pathname, link to it directly */
  226. linkname = 0;
  227. } else {
  228. if (linkprefix) {
  229. /* -L implies -l and prefixes names with a $cwd arg. */
  230. len += lplen + 1;
  231. linkname = xmalloc(len + 1);
  232. sprintf(linkname, "%s/%s", linkprefix, name);
  233. } else if (dorelsymlink) {
  234. /* Symlink the relative path from todir to source name. */
  235. linkname = xmalloc(PATH_MAX);
  236. if (*todir == '/') {
  237. /* todir is absolute: skip over common prefix. */
  238. lplen = relatepaths(todir, cwd, linkname);
  239. strcpy(linkname + lplen, name);
  240. } else {
  241. /* todir is named by a relative path: reverse it. */
  242. reversepath(todir, name, len, linkname);
  243. xchdir(cwd);
  244. }
  245. len = strlen(linkname);
  246. }
  247. name = linkname;
  248. }
  249. /* Check for a pre-existing symlink with identical content. */
  250. if (exists &&
  251. (!S_ISLNK(tosb.st_mode) ||
  252. readlink(toname, buf, sizeof buf) != len ||
  253. strncmp(buf, name, len) != 0)) {
  254. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  255. exists = 0;
  256. }
  257. if (!exists && symlink(name, toname) < 0)
  258. fail("cannot make symbolic link %s", toname);
  259. #ifdef HAVE_LCHOWN
  260. if ((owner || group) && lchown(toname, uid, gid) < 0)
  261. fail("cannot change owner of %s", toname);
  262. #endif
  263. if (linkname) {
  264. free(linkname);
  265. linkname = 0;
  266. }
  267. } else {
  268. /* Copy from name to toname, which might be the same file. */
  269. fromfd = open(name, O_RDONLY);
  270. if (fromfd < 0 || fstat(fromfd, &sb) < 0)
  271. fail("cannot access %s", name);
  272. if (exists && (!S_ISREG(tosb.st_mode) || access(toname, W_OK) < 0))
  273. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  274. tofd = open(toname, O_CREAT | O_WRONLY, 0666);
  275. if (tofd < 0)
  276. fail("cannot create %s", toname);
  277. bp = buf;
  278. while ((cc = read(fromfd, bp, sizeof buf)) > 0) {
  279. while ((wc = write(tofd, bp, cc)) > 0) {
  280. if ((cc -= wc) == 0)
  281. break;
  282. bp += wc;
  283. }
  284. if (wc < 0)
  285. fail("cannot write to %s", toname);
  286. }
  287. if (cc < 0)
  288. fail("cannot read from %s", name);
  289. if (ftruncate(tofd, sb.st_size) < 0)
  290. fail("cannot truncate %s", toname);
  291. if (dotimes) {
  292. utb.actime = sb.st_atime;
  293. utb.modtime = sb.st_mtime;
  294. if (utime(toname, &utb) < 0)
  295. fail("cannot set times of %s", toname);
  296. }
  297. if (fchmod(tofd, mode) < 0)
  298. fail("cannot change mode of %s", toname);
  299. if ((owner || group) && fchown(tofd, uid, gid) < 0)
  300. fail("cannot change owner of %s", toname);
  301. /* Must check for delayed (NFS) write errors on close. */
  302. if (close(tofd) < 0)
  303. fail("cannot write to %s", toname);
  304. close(fromfd);
  305. }
  306. free(toname);
  307. }
  308. free(cwd);
  309. free(todir);
  310. return 0;
  311. }