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