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