Deleted Added
full compact
main.c (173412) main.c (173513)
1/*
2 *
3 * FreeBSD install - a package for the installation and maintainance
4 * of non-core utilities.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Jordan K. Hubbard
16 * 18 July 1993
17 *
18 * This is the add module.
19 */
20
21#include <sys/cdefs.h>
1/*
2 *
3 * FreeBSD install - a package for the installation and maintainance
4 * of non-core utilities.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * Jordan K. Hubbard
16 * 18 July 1993
17 *
18 * This is the add module.
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/main.c 173412 2007-11-07 10:53:41Z kevlo $");
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/main.c 173513 2007-11-10 09:40:39Z krion $");
23
24#include <err.h>
25#include <sys/param.h>
26#include <sys/utsname.h>
27#include "lib.h"
28#include "add.h"
29
23
24#include <err.h>
25#include <sys/param.h>
26#include <sys/utsname.h>
27#include "lib.h"
28#include "add.h"
29
30static char Options[] = "hvIRfFnrp:P:SMt:C:K";
30static char Options[] = "hviIRfFnrp:P:SMt:C:K";
31
32char *Prefix = NULL;
33Boolean PrefixRecursive = FALSE;
34char *Chroot = NULL;
35Boolean NoInstall = FALSE;
36Boolean NoRecord = FALSE;
37Boolean Remote = FALSE;
38Boolean KeepPackage = FALSE;
39Boolean FailOnAlreadyInstalled = TRUE;
31
32char *Prefix = NULL;
33Boolean PrefixRecursive = FALSE;
34char *Chroot = NULL;
35Boolean NoInstall = FALSE;
36Boolean NoRecord = FALSE;
37Boolean Remote = FALSE;
38Boolean KeepPackage = FALSE;
39Boolean FailOnAlreadyInstalled = TRUE;
40Boolean IgnoreDeps = FALSE;
40
41char *Mode = NULL;
42char *Owner = NULL;
43char *Group = NULL;
44char *PkgName = NULL;
45char *PkgAddCmd = NULL;
46char *Directory = NULL;
47char FirstPen[FILENAME_MAX];
48add_mode_t AddMode = NORMAL;
49
50char **pkgs;
51
52struct {
53 int lowver; /* Lowest version number to match */
54 int hiver; /* Highest version number to match */
55 const char *directory; /* Directory it lives in */
56} releases[] = {
57 { 410000, 410000, "/packages-4.1-release" },
58 { 420000, 420000, "/packages-4.2-release" },
59 { 430000, 430000, "/packages-4.3-release" },
60 { 440000, 440000, "/packages-4.4-release" },
61 { 450000, 450000, "/packages-4.5-release" },
62 { 460000, 460001, "/packages-4.6-release" },
63 { 460002, 460099, "/packages-4.6.2-release" },
64 { 470000, 470099, "/packages-4.7-release" },
65 { 480000, 480099, "/packages-4.8-release" },
66 { 490000, 490099, "/packages-4.9-release" },
67 { 491000, 491099, "/packages-4.10-release" },
68 { 492000, 492099, "/packages-4.11-release" },
69 { 500000, 500099, "/packages-5.0-release" },
70 { 501000, 501099, "/packages-5.1-release" },
71 { 502000, 502009, "/packages-5.2-release" },
72 { 502010, 502099, "/packages-5.2.1-release" },
73 { 503000, 503099, "/packages-5.3-release" },
74 { 504000, 504099, "/packages-5.4-release" },
75 { 505000, 505099, "/packages-5.5-release" },
76 { 600000, 600099, "/packages-6.0-release" },
77 { 601000, 601099, "/packages-6.1-release" },
78 { 602000, 602099, "/packages-6.2-release" },
79 { 300000, 399000, "/packages-3-stable" },
80 { 400000, 499000, "/packages-4-stable" },
81 { 502100, 502128, "/packages-5-current" },
82 { 503100, 599000, "/packages-5-stable" },
83 { 600100, 699000, "/packages-6-stable" },
84 { 700000, 799000, "/packages-7-stable" },
85 { 800000, 899000, "/packages-8-current" },
86 { 0, 9999999, "/packages-current" },
87 { 0, 0, NULL }
88};
89
90static char *getpackagesite(void);
91int getosreldate(void);
92
93static void usage(void);
94
95int
96main(int argc, char **argv)
97{
98 int ch, error;
99 char **start;
100 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
101 static char temppackageroot[MAXPATHLEN];
102 static char pkgaddpath[MAXPATHLEN];
103
104 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
105 PkgAddCmd = realpath(argv[0], pkgaddpath);
106 else
107 PkgAddCmd = argv[0];
108
109 start = argv;
110 while ((ch = getopt(argc, argv, Options)) != -1) {
111 switch(ch) {
112 case 'v':
113 Verbose++;
114 break;
115
116 case 'p':
117 Prefix = optarg;
118 PrefixRecursive = FALSE;
119 break;
120
121 case 'P':
122 Prefix = optarg;
123 PrefixRecursive = TRUE;
124 break;
125
126 case 'I':
127 NoInstall = TRUE;
128 break;
129
130 case 'R':
131 NoRecord = TRUE;
132 break;
133
134 case 'f':
135 Force = TRUE;
136 break;
137
138 case 'F':
139 FailOnAlreadyInstalled = FALSE;
140 break;
141
142 case 'K':
143 KeepPackage = TRUE;
144 break;
145
146 case 'n':
147 Fake = TRUE;
148 break;
149
150 case 'r':
151 Remote = TRUE;
152 break;
153
154 case 't':
155 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
156 errx(1, "-t Argument too long.");
157 break;
158
159 case 'S':
160 AddMode = SLAVE;
161 break;
162
163 case 'M':
164 AddMode = MASTER;
165 break;
166
167 case 'C':
168 Chroot = optarg;
169 break;
41
42char *Mode = NULL;
43char *Owner = NULL;
44char *Group = NULL;
45char *PkgName = NULL;
46char *PkgAddCmd = NULL;
47char *Directory = NULL;
48char FirstPen[FILENAME_MAX];
49add_mode_t AddMode = NORMAL;
50
51char **pkgs;
52
53struct {
54 int lowver; /* Lowest version number to match */
55 int hiver; /* Highest version number to match */
56 const char *directory; /* Directory it lives in */
57} releases[] = {
58 { 410000, 410000, "/packages-4.1-release" },
59 { 420000, 420000, "/packages-4.2-release" },
60 { 430000, 430000, "/packages-4.3-release" },
61 { 440000, 440000, "/packages-4.4-release" },
62 { 450000, 450000, "/packages-4.5-release" },
63 { 460000, 460001, "/packages-4.6-release" },
64 { 460002, 460099, "/packages-4.6.2-release" },
65 { 470000, 470099, "/packages-4.7-release" },
66 { 480000, 480099, "/packages-4.8-release" },
67 { 490000, 490099, "/packages-4.9-release" },
68 { 491000, 491099, "/packages-4.10-release" },
69 { 492000, 492099, "/packages-4.11-release" },
70 { 500000, 500099, "/packages-5.0-release" },
71 { 501000, 501099, "/packages-5.1-release" },
72 { 502000, 502009, "/packages-5.2-release" },
73 { 502010, 502099, "/packages-5.2.1-release" },
74 { 503000, 503099, "/packages-5.3-release" },
75 { 504000, 504099, "/packages-5.4-release" },
76 { 505000, 505099, "/packages-5.5-release" },
77 { 600000, 600099, "/packages-6.0-release" },
78 { 601000, 601099, "/packages-6.1-release" },
79 { 602000, 602099, "/packages-6.2-release" },
80 { 300000, 399000, "/packages-3-stable" },
81 { 400000, 499000, "/packages-4-stable" },
82 { 502100, 502128, "/packages-5-current" },
83 { 503100, 599000, "/packages-5-stable" },
84 { 600100, 699000, "/packages-6-stable" },
85 { 700000, 799000, "/packages-7-stable" },
86 { 800000, 899000, "/packages-8-current" },
87 { 0, 9999999, "/packages-current" },
88 { 0, 0, NULL }
89};
90
91static char *getpackagesite(void);
92int getosreldate(void);
93
94static void usage(void);
95
96int
97main(int argc, char **argv)
98{
99 int ch, error;
100 char **start;
101 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
102 static char temppackageroot[MAXPATHLEN];
103 static char pkgaddpath[MAXPATHLEN];
104
105 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
106 PkgAddCmd = realpath(argv[0], pkgaddpath);
107 else
108 PkgAddCmd = argv[0];
109
110 start = argv;
111 while ((ch = getopt(argc, argv, Options)) != -1) {
112 switch(ch) {
113 case 'v':
114 Verbose++;
115 break;
116
117 case 'p':
118 Prefix = optarg;
119 PrefixRecursive = FALSE;
120 break;
121
122 case 'P':
123 Prefix = optarg;
124 PrefixRecursive = TRUE;
125 break;
126
127 case 'I':
128 NoInstall = TRUE;
129 break;
130
131 case 'R':
132 NoRecord = TRUE;
133 break;
134
135 case 'f':
136 Force = TRUE;
137 break;
138
139 case 'F':
140 FailOnAlreadyInstalled = FALSE;
141 break;
142
143 case 'K':
144 KeepPackage = TRUE;
145 break;
146
147 case 'n':
148 Fake = TRUE;
149 break;
150
151 case 'r':
152 Remote = TRUE;
153 break;
154
155 case 't':
156 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
157 errx(1, "-t Argument too long.");
158 break;
159
160 case 'S':
161 AddMode = SLAVE;
162 break;
163
164 case 'M':
165 AddMode = MASTER;
166 break;
167
168 case 'C':
169 Chroot = optarg;
170 break;
171 case 'i':
172 IgnoreDeps = TRUE;
173 break;
170
171 case 'h':
172 case '?':
173 default:
174 usage();
175 break;
176 }
177 }
178 argc -= optind;
179 argv += optind;
180
181 if (AddMode != SLAVE) {
182 pkgs = (char **)malloc((argc+1) * sizeof(char *));
183 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
184
185 /* Get all the remaining package names, if any */
186 for (ch = 0; *argv; ch++, argv++) {
187 char temp[MAXPATHLEN];
188 if (Remote) {
189 if ((packagesite = getpackagesite()) == NULL)
190 errx(1, "package name too long");
191 if (strlcpy(temppackageroot, packagesite,
192 sizeof(temppackageroot)) >= sizeof(temppackageroot))
193 errx(1, "package name too long");
194 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
195 >= sizeof(temppackageroot))
196 errx(1, "package name too long");
197 remotepkg = temppackageroot;
198 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
199 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
200 !ptr[4]))
201 if (strlcat(remotepkg,
202#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
203 ".tbz",
204#else
205 ".tgz",
206#endif
207 sizeof(temppackageroot)) >= sizeof(temppackageroot))
208 errx(1, "package name too long");
209 }
210 if (!strcmp(*argv, "-")) /* stdin? */
211 pkgs[ch] = (char *)"-";
212 else if (isURL(*argv)) { /* preserve URLs */
213 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
214 errx(1, "package name too long");
215 pkgs[ch] = strdup(temp);
216 }
217 else if ((Remote) && isURL(remotepkg)) {
218 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
219 errx(1, "package name too long");
220 pkgs[ch] = strdup(temp);
221 } else { /* expand all pathnames to fullnames */
222 if (fexists(*argv)) /* refers to a file directly */
223 pkgs[ch] = strdup(realpath(*argv, temp));
224 else { /* look for the file in the expected places */
225 if (!(cp = fileFindByPath(NULL, *argv))) {
226 /* let pkg_do() fail later, so that error is reported */
227 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
228 errx(1, "package name too long");
229 pkgs[ch] = strdup(temp);
230 } else {
231 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
232 errx(1, "package name too long");
233 pkgs[ch] = strdup(temp);
234 }
235 }
236 }
237 if (packagesite != NULL)
238 packagesite[0] = '\0';
239 }
240 }
241 /* If no packages, yelp */
242 else if (!ch) {
243 warnx("missing package name(s)");
244 usage();
245 }
246 else if (ch > 1 && AddMode == MASTER) {
247 warnx("only one package name may be specified with master mode");
248 usage();
249 }
250 /* Perform chroot if requested */
251 if (Chroot != NULL) {
252 if (chroot(Chroot))
253 errx(1, "chroot to %s failed", Chroot);
254 }
255 /* Make sure the sub-execs we invoke get found */
256 setenv("PATH",
257 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
258 1);
259
260 /* Set a reasonable umask */
261 umask(022);
262
263 if ((error = pkg_perform(pkgs)) != 0) {
264 if (Verbose)
265 warnx("%d package addition(s) failed", error);
266 return error;
267 }
268 else
269 return 0;
270}
271
272static char *
273getpackagesite(void)
274{
275 int reldate, i;
276 static char sitepath[MAXPATHLEN];
277 struct utsname u;
278
279 if (getenv("PACKAGESITE")) {
280 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
281 >= sizeof(sitepath))
282 return NULL;
283 return sitepath;
284 }
285
286 if (getenv("PACKAGEROOT")) {
287 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
288 >= sizeof(sitepath))
289 return NULL;
290 } else {
291 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
292 >= sizeof(sitepath))
293 return NULL;
294 }
295
296 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
297 >= sizeof(sitepath))
298 return NULL;
299
300 uname(&u);
301 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
302 return NULL;
303
304 reldate = getosreldate();
305 for(i = 0; releases[i].directory != NULL; i++) {
306 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
307 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
308 >= sizeof(sitepath))
309 return NULL;
310 break;
311 }
312 }
313
314 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
315 return NULL;
316
317 return sitepath;
318
319}
320
321static void
322usage()
323{
324 fprintf(stderr, "%s\n%s\n",
174
175 case 'h':
176 case '?':
177 default:
178 usage();
179 break;
180 }
181 }
182 argc -= optind;
183 argv += optind;
184
185 if (AddMode != SLAVE) {
186 pkgs = (char **)malloc((argc+1) * sizeof(char *));
187 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
188
189 /* Get all the remaining package names, if any */
190 for (ch = 0; *argv; ch++, argv++) {
191 char temp[MAXPATHLEN];
192 if (Remote) {
193 if ((packagesite = getpackagesite()) == NULL)
194 errx(1, "package name too long");
195 if (strlcpy(temppackageroot, packagesite,
196 sizeof(temppackageroot)) >= sizeof(temppackageroot))
197 errx(1, "package name too long");
198 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
199 >= sizeof(temppackageroot))
200 errx(1, "package name too long");
201 remotepkg = temppackageroot;
202 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
203 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
204 !ptr[4]))
205 if (strlcat(remotepkg,
206#if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
207 ".tbz",
208#else
209 ".tgz",
210#endif
211 sizeof(temppackageroot)) >= sizeof(temppackageroot))
212 errx(1, "package name too long");
213 }
214 if (!strcmp(*argv, "-")) /* stdin? */
215 pkgs[ch] = (char *)"-";
216 else if (isURL(*argv)) { /* preserve URLs */
217 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
218 errx(1, "package name too long");
219 pkgs[ch] = strdup(temp);
220 }
221 else if ((Remote) && isURL(remotepkg)) {
222 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
223 errx(1, "package name too long");
224 pkgs[ch] = strdup(temp);
225 } else { /* expand all pathnames to fullnames */
226 if (fexists(*argv)) /* refers to a file directly */
227 pkgs[ch] = strdup(realpath(*argv, temp));
228 else { /* look for the file in the expected places */
229 if (!(cp = fileFindByPath(NULL, *argv))) {
230 /* let pkg_do() fail later, so that error is reported */
231 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
232 errx(1, "package name too long");
233 pkgs[ch] = strdup(temp);
234 } else {
235 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
236 errx(1, "package name too long");
237 pkgs[ch] = strdup(temp);
238 }
239 }
240 }
241 if (packagesite != NULL)
242 packagesite[0] = '\0';
243 }
244 }
245 /* If no packages, yelp */
246 else if (!ch) {
247 warnx("missing package name(s)");
248 usage();
249 }
250 else if (ch > 1 && AddMode == MASTER) {
251 warnx("only one package name may be specified with master mode");
252 usage();
253 }
254 /* Perform chroot if requested */
255 if (Chroot != NULL) {
256 if (chroot(Chroot))
257 errx(1, "chroot to %s failed", Chroot);
258 }
259 /* Make sure the sub-execs we invoke get found */
260 setenv("PATH",
261 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
262 1);
263
264 /* Set a reasonable umask */
265 umask(022);
266
267 if ((error = pkg_perform(pkgs)) != 0) {
268 if (Verbose)
269 warnx("%d package addition(s) failed", error);
270 return error;
271 }
272 else
273 return 0;
274}
275
276static char *
277getpackagesite(void)
278{
279 int reldate, i;
280 static char sitepath[MAXPATHLEN];
281 struct utsname u;
282
283 if (getenv("PACKAGESITE")) {
284 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
285 >= sizeof(sitepath))
286 return NULL;
287 return sitepath;
288 }
289
290 if (getenv("PACKAGEROOT")) {
291 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
292 >= sizeof(sitepath))
293 return NULL;
294 } else {
295 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
296 >= sizeof(sitepath))
297 return NULL;
298 }
299
300 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
301 >= sizeof(sitepath))
302 return NULL;
303
304 uname(&u);
305 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
306 return NULL;
307
308 reldate = getosreldate();
309 for(i = 0; releases[i].directory != NULL; i++) {
310 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
311 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
312 >= sizeof(sitepath))
313 return NULL;
314 break;
315 }
316 }
317
318 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
319 return NULL;
320
321 return sitepath;
322
323}
324
325static void
326usage()
327{
328 fprintf(stderr, "%s\n%s\n",
325 "usage: pkg_add [-vInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
329 "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
326 " pkg-name [pkg-name ...]");
327 exit(1);
328}
330 " pkg-name [pkg-name ...]");
331 exit(1);
332}