daf73e04c3628bcea64e86dcec2a98dc4f12996d
[dragonfly.git] / usr.sbin / pkg_install / add / main.c
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  * $FreeBSD: src/usr.sbin/pkg_install/add/main.c,v 1.59 2004/07/28 07:19:15 kan Exp $
21  * $DragonFly: src/usr.sbin/pkg_install/add/Attic/main.c,v 1.8 2004/08/01 04:22:49 drhodus Exp $
22  */
23
24 #include <err.h>
25 #include <sys/param.h>
26 #include <sys/utsname.h>
27 #include "lib.h"
28 #include "add.h"
29
30 static char Options[] = "hvIRfnrp:SMt:C:";
31
32 char    *Prefix         = NULL;
33 char    *Chroot         = NULL;
34 Boolean NoInstall       = FALSE;
35 Boolean NoRecord        = FALSE;
36 Boolean Remote          = FALSE;
37
38 char    *Mode           = NULL;
39 char    *Owner          = NULL;
40 char    *Group          = NULL;
41 char    *PkgName        = NULL;
42 char    *PkgAddCmd      = NULL;
43 char    *Directory      = NULL;
44 char    FirstPen[FILENAME_MAX];
45 add_mode_t AddMode      = NORMAL;
46
47 #define MAX_PKGS        200
48 char    pkgnames[MAX_PKGS][MAXPATHLEN];
49 char    *pkgs[MAX_PKGS];
50
51 struct {
52         int lowver;     /* Lowest version number to match */
53         int hiver;      /* Highest version number to match */
54         const char *directory;  /* Directory it lives in */
55 } releases[] = {
56         { 410000, 410000, "/packages-4.1-release" },
57         { 420000, 420000, "/packages-4.2-release" },
58         { 430000, 430000, "/packages-4.3-release" },
59         { 440000, 440000, "/packages-4.4-release" },
60         { 450000, 450000, "/packages-4.5-release" },
61         { 460000, 460001, "/packages-4.6-release" },
62         { 460002, 460099, "/packages-4.6.2-release" },
63         { 470000, 470099, "/packages-4.7-release" },
64         { 480000, 480099, "/packages-4.8-release" },
65         { 490000, 490099, "/packages-4.9-release" },
66         { 491000, 491099, "/packages-4.10-release" },
67         { 492000, 492099, "/packages-4.11-release" },
68         { 500000, 500099, "/packages-5.0-release" },
69         { 501000, 501099, "/packages-5.1-release" },
70         { 502000, 502009, "/packages-5.2-release" },
71         { 502010, 502099, "/packages-5.2.1-release" },
72         { 503000, 503099, "/packages-5.3-release" },
73         { 300000, 399000, "/packages-3-stable" },
74         { 400000, 499000, "/packages-4-stable" },
75         { 502100, 599000, "/packages-5-current" },
76         { 0, 9999999, "/" },
77         { 0, 0, NULL }
78 };
79
80 static char *getpackagesite(void);
81 int getosreldate(void);
82
83 static void usage(void);
84
85 int
86 main(int argc, char **argv)
87 {
88     int ch, error;
89     char **start;
90     char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
91     static char temppackageroot[MAXPATHLEN];
92     static char pkgaddpath[MAXPATHLEN];
93
94     if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
95         PkgAddCmd = realpath(argv[0], pkgaddpath);
96     else
97         PkgAddCmd = argv[0];
98
99     start = argv;
100     while ((ch = getopt(argc, argv, Options)) != -1) {
101         switch(ch) {
102         case 'v':
103             Verbose = TRUE;
104             break;
105
106         case 'p':
107             Prefix = optarg;
108             break;
109
110         case 'I':
111             NoInstall = TRUE;
112             break;
113
114         case 'R':
115             NoRecord = TRUE;
116             break;
117
118         case 'f':
119             Force = TRUE;
120             break;
121
122         case 'n':
123             Fake = TRUE;
124             Verbose = TRUE;
125             break;
126
127         case 'r':
128             Remote = TRUE;
129             break;
130
131         case 't':
132             if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
133                 errx(1, "-t Argument too long.");
134             break;
135
136         case 'S':
137             AddMode = SLAVE;
138             break;
139
140         case 'M':
141             AddMode = MASTER;
142             break;
143
144         case 'C':
145             Chroot = optarg;
146             break;
147
148         case 'h':
149         case '?':
150         default:
151             usage();
152             break;
153         }
154     }
155     argc -= optind;
156     argv += optind;
157
158     if (argc > MAX_PKGS) {
159         errx(1, "too many packages (max %d)", MAX_PKGS);
160     }
161
162     if (AddMode != SLAVE) {
163         for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
164
165         /* Get all the remaining package names, if any */
166         for (ch = 0; *argv; ch++, argv++) {
167             if (Remote) {
168                 if ((packagesite = getpackagesite()) == NULL)
169                     errx(1, "package name too long");
170                 if (strlcpy(temppackageroot, packagesite,
171                     sizeof(temppackageroot)) >= sizeof(temppackageroot))
172                     errx(1, "package name too long");
173                 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
174                     >= sizeof(temppackageroot))
175                     errx(1, "package name too long");
176                 remotepkg = temppackageroot;
177                 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 
178                         (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
179                         !ptr[4]))
180                     if (strlcat(remotepkg,
181 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
182                         ".tbz",
183 #else
184                         ".tgz",
185 #endif
186                         sizeof(temppackageroot)) >= sizeof(temppackageroot))
187                         errx(1, "package name too long");
188             }
189             if (!strcmp(*argv, "-"))    /* stdin? */
190                 pkgs[ch] = (char *)"-";
191             else if (isURL(*argv)) {    /* preserve URLs */
192                 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
193                     >= sizeof(pkgnames[ch]))
194                     errx(1, "package name too long");
195                 pkgs[ch] = pkgnames[ch];
196             }
197             else if ((Remote) && isURL(remotepkg)) {
198                 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
199                     >= sizeof(pkgnames[ch]))
200                     errx(1, "package name too long");
201                 pkgs[ch] = pkgnames[ch];
202             } else {                    /* expand all pathnames to fullnames */
203                 if (fexists(*argv)) /* refers to a file directly */
204                     pkgs[ch] = realpath(*argv, pkgnames[ch]);
205                 else {          /* look for the file in the expected places */
206                     if (!(cp = fileFindByPath(NULL, *argv))) {
207                         /* let pkg_do() fail later, so that error is reported */
208                         if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
209                             >= sizeof(pkgnames[ch]))
210                             errx(1, "package name too long");
211                         pkgs[ch] = pkgnames[ch];
212                     } else {
213                         if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
214                             >= sizeof(pkgnames[ch]))
215                             errx(1, "package name too long");
216                         pkgs[ch] = pkgnames[ch];
217                     }
218                 }
219             }
220             if (packagesite != NULL)
221                 packagesite[0] = '\0';
222         }
223     }
224     /* If no packages, yelp */
225     else if (!ch) {
226         warnx("missing package name(s)");
227         usage();
228     }
229     else if (ch > 1 && AddMode == MASTER) {
230         warnx("only one package name may be specified with master mode");
231         usage();
232     }
233     /* Perform chroot if requested */
234     if (Chroot != NULL) {
235         if (chroot(Chroot))
236             errx(1, "chroot to %s failed", Chroot);
237     }
238     /* Make sure the sub-execs we invoke get found */
239     setenv("PATH", 
240            "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
241            1);
242
243     /* Set a reasonable umask */
244     umask(022);
245
246     if ((error = pkg_perform(pkgs)) != 0) {
247         if (Verbose)
248             warnx("%d package addition(s) failed", error);
249         return error;
250     }
251     else
252         return 0;
253 }
254
255 static char *
256 getpackagesite(void)
257 {
258     int reldate, i;
259     static char sitepath[MAXPATHLEN];
260     struct utsname u;
261
262     if (getenv("PACKAGESITE")) {
263         if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
264             >= sizeof(sitepath))
265             return NULL;
266         return sitepath;
267     }
268
269     if (getenv("PACKAGEROOT")) {
270         if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
271             >= sizeof(sitepath))
272             return NULL;
273     } else {
274         if (strlcat(sitepath, "http://gobsd.com", sizeof(sitepath))
275             >= sizeof(sitepath))
276             return NULL;
277     }
278
279     if (strlcat(sitepath, "/packages", sizeof(sitepath))
280         >= sizeof(sitepath))
281         return NULL;
282
283 #if 0
284     uname(&u);
285     if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
286         return NULL;
287 #endif
288
289     reldate = getosreldate();
290     for(i = 0; releases[i].directory != NULL; i++) {
291         if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
292             if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
293                 >= sizeof(sitepath))
294                 return NULL;
295             break;
296         }
297     }
298
299     if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
300         return NULL;
301
302     return sitepath;
303
304 }
305
306 static void
307 usage()
308 {
309     fprintf(stderr, "%s\n%s\n",
310         "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix] [-C chrootdir]",
311         "               pkg-name [pkg-name ...]");
312     exit(1);
313 }