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