main.c revision 80463
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 80463 2001-07-28 01:59:58Z obrien $"; 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, 152 sizeof(temppackageroot)) >= 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)) >= sizeof(temppackageroot)) 158 errx(1, "package name too long"); 159 } 160 if (!strcmp(*argv, "-")) /* stdin? */ 161 pkgs[ch] = "-"; 162 else if (isURL(*argv)) { /* preserve URLs */ 163 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch])) >= 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])) >= sizeof(pkgnames[ch])) 169 errx(1, "package name too long"); 170 pkgs[ch] = pkgnames[ch]; 171 } else { /* expand all pathnames to fullnames */ 172 if (fexists(*argv)) /* refers to a file directly */ 173 pkgs[ch] = realpath(*argv, pkgnames[ch]); 174 else { /* look for the file in the expected places */ 175 if (!(cp = fileFindByPath(NULL, *argv))) { 176 /* let pkg_do() fail later, so that error is reported */ 177 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch])) >= sizeof(pkgnames[ch])) 178 errx(1, "package name too long"); 179 pkgs[ch] = pkgnames[ch]; 180 } else { 181 if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch])) >= sizeof(pkgnames[ch])) 182 errx(1, "package name too long"); 183 pkgs[ch] = pkgnames[ch]; 184 } 185 } 186 } 187 } 188 } 189 /* If no packages, yelp */ 190 else if (!ch) { 191 warnx("missing package name(s)"); 192 usage(); 193 } 194 else if (ch > 1 && AddMode == MASTER) { 195 warnx("only one package name may be specified with master mode"); 196 usage(); 197 } 198 /* Make sure the sub-execs we invoke get found */ 199 setenv("PATH", 200 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin", 201 1); 202 203 /* Set a reasonable umask */ 204 umask(022); 205 206 if ((error = pkg_perform(pkgs)) != 0) { 207 if (Verbose) 208 warnx("%d package addition(s) failed", error); 209 return error; 210 } 211 else 212 return 0; 213} 214 215static char * 216getpackagesite(void) 217{ 218 int reldate, i; 219 static char sitepath[MAXPATHLEN]; 220 struct utsname u; 221 222 if (getenv("PACKAGESITE")) { 223 if (strlcpy(sitepath, getenv("PACKAGESITE"), 224 sizeof(sitepath)) >= sizeof(sitepath)) 225 return NULL; 226 return sitepath; 227 } 228 229 if (getenv("PACKAGEROOT")) { 230 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath)) >= sizeof(sitepath)) 231 return NULL; 232 } else { 233 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath)) >= sizeof(sitepath)) 234 return NULL; 235 } 236 237 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath)) >= sizeof(sitepath)) 238 return NULL; 239 240 uname(&u); 241 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath)) 242 return NULL; 243 244 reldate = getosreldate(); 245 for(i = 0; releases[i].directory != NULL; i++) { 246 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 247 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath)) >= sizeof(sitepath)) 248 return NULL; 249 break; 250 } 251 } 252 253 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath)) 254 return NULL; 255 256 return sitepath; 257 258} 259 260static void 261usage() 262{ 263 fprintf(stderr, "%s\n%s\n", 264 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]", 265 " pkg-name [pkg-name ...]"); 266 exit(1); 267} 268