From e8d26b1581dab3026009757a94a8134f0fd851ae Mon Sep 17 00:00:00 2001 From: jkh Date: Thu, 20 Jun 1996 18:33:55 +0000 Subject: Make pkg_install understand and use libftpio instead of its own copies of the ftp support routines. Also some cosmetic and minor bug fixes I've been meaning to incorporate for awhile. --- usr.sbin/pkg_install/lib/Makefile | 2 +- usr.sbin/pkg_install/lib/file.c | 63 ++---- usr.sbin/pkg_install/lib/ftp.c | 424 -------------------------------------- usr.sbin/pkg_install/lib/ftp.h | 26 --- usr.sbin/pkg_install/lib/lib.h | 5 +- usr.sbin/pkg_install/lib/pen.c | 4 +- usr.sbin/pkg_install/lib/plist.c | 4 +- 7 files changed, 30 insertions(+), 498 deletions(-) delete mode 100644 usr.sbin/pkg_install/lib/ftp.c delete mode 100644 usr.sbin/pkg_install/lib/ftp.h (limited to 'usr.sbin/pkg_install/lib') diff --git a/usr.sbin/pkg_install/lib/Makefile b/usr.sbin/pkg_install/lib/Makefile index 223a33d..3876b10 100644 --- a/usr.sbin/pkg_install/lib/Makefile +++ b/usr.sbin/pkg_install/lib/Makefile @@ -1,5 +1,5 @@ LIB= install -SRCS= file.c ftp.c msg.c plist.c str.c exec.c global.c pen.c +SRCS= file.c msg.c plist.c str.c exec.c global.c pen.c CFLAGS+= ${DEBUG} NOPROFILE= yes NOPIC= yes diff --git a/usr.sbin/pkg_install/lib/file.c b/usr.sbin/pkg_install/lib/file.c index 19ce013..8f69b96 100644 --- a/usr.sbin/pkg_install/lib/file.c +++ b/usr.sbin/pkg_install/lib/file.c @@ -1,5 +1,5 @@ #ifndef lint -static const char *rcsid = "$Id: file.c,v 1.20 1996/02/19 02:35:56 mpp Exp $"; +static const char *rcsid = "$Id: file.c,v 1.21 1996/03/12 06:12:43 jkh Exp $"; #endif /* @@ -23,9 +23,10 @@ static const char *rcsid = "$Id: file.c,v 1.20 1996/02/19 02:35:56 mpp Exp $"; */ #include "lib.h" -#include "ftp.h" +#include #include #include +#include /* Quick check to see if a file exists */ Boolean @@ -123,7 +124,7 @@ fileURLHost(char *fname, char *where, int max) /* Don't ever call this on a bad URL! */ fname += strlen("ftp://"); /* Do we have a place to stick our work? */ - if (ret = where) { + if ((ret = where) != NULL) { while (*fname && *fname != '/' && max--) *where++ = *fname++; *where = '\0'; @@ -148,7 +149,7 @@ fileURLFilename(char *fname, char *where, int max) /* Don't ever call this on a bad URL! */ fname += strlen("ftp://"); /* Do we have a place to stick our work? */ - if (ret = where) { + if ((ret = where) != NULL) { while (*fname && *fname != '/') ++fname; if (*fname == '/') { @@ -172,24 +173,20 @@ fileURLFilename(char *fname, char *where, int max) char * fileGetURL(char *base, char *spec) { - char host[HOSTNAME_MAX], file[FILENAME_MAX], dir[FILENAME_MAX]; - char pword[HOSTNAME_MAX + 40], *uname, *cp, *rp, *tmp; + char host[HOSTNAME_MAX], file[FILENAME_MAX]; + char pword[HOSTNAME_MAX + 40], *uname, *cp, *rp; char fname[FILENAME_MAX]; char pen[FILENAME_MAX]; struct passwd *pw; - FTP_t ftp; + FILE *ftp; pid_t tpid; - int fd, fd2, i, len = 0; - char ch; - time_t start, stop; + int i; char *hint; rp = NULL; /* Special tip that sysinstall left for us */ hint = getenv("PKG_ADD_BASE"); if (!isURL(spec)) { - int len; - if (!base && !hint) return NULL; /* We've been given an existing URL (that's known-good) and now we need @@ -219,7 +216,6 @@ fileGetURL(char *base, char *spec) } else strcpy(fname, spec); - ftp = FtpInit(); cp = fileURLHost(fname, host, HOSTNAME_MAX); if (!*cp) { whinge("URL `%s' has bad host part!", fname); @@ -241,37 +237,23 @@ fileGetURL(char *base, char *spec) whinge("Can't get user name for ID %d\n.", getuid()); strcpy(pword, "joe@"); } - else - snprintf(pword, HOSTNAME_MAX + 40, "%s@%s", pw->pw_name, host); + else { + char me[HOSTNAME_MAX]; - if (Verbose) - printf("Trying to log into %s as %s.\n", host, uname); - FtpOpen(ftp, host, uname, pword); - if (getenv("FTP_PASSIVE_MODE")) - FtpPassive(ftp, TRUE); - - strcpy(dir, file); - for (i = strlen(dir); i && dir[i] != '/'; i--); - dir[i] = '\0'; - - if (dir[0]) { - if (Verbose) printf("FTP: chdir to %s\n", dir); - FtpChdir(ftp, dir); + gethostname(me, HOSTNAME_MAX); + snprintf(pword, HOSTNAME_MAX + 40, "%s@%s", pw->pw_name, me); } - FtpBinary(ftp, TRUE); - if (Verbose) printf("FTP: trying to get %s\n", basename_of(file)); - tmp = basename_of(file); - if (!strstr(tmp, ".tgz")) - tmp = strconcat(tmp, ".tgz"); - fd = FtpGet(ftp, tmp); - if (fd >= 0) { + if (Verbose) + printf("Trying to fetch %s.\n", fname); + ftp = ftpGetURL(fname, uname, pword); + if (ftp) { pen[0] = '\0'; - if (rp = make_playpen(pen, 0)) { + if ((rp = make_playpen(pen, 0)) != NULL) { if (Verbose) printf("Extracting from FTP connection into %s\n", pen); tpid = fork(); if (!tpid) { - dup2(fd, 0); + dup2(fileno(ftp), 0); i = execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0); if (Verbose) printf("tar command returns %d status\n", i); @@ -280,7 +262,7 @@ fileGetURL(char *base, char *spec) else { int pstat; - close(fd); + fclose(ftp); tpid = waitpid(tpid, &pstat, 0); } } @@ -288,9 +270,8 @@ fileGetURL(char *base, char *spec) printf("Error: Unable to construct a new playpen for FTP!\n"); } else - printf("Error: FTP Unable to get %s\n", basename_of(file)); - FtpEOF(ftp); - FtpClose(ftp); + printf("Error: FTP Unable to get %s\n", fname); + fclose(ftp); return rp; } diff --git a/usr.sbin/pkg_install/lib/ftp.c b/usr.sbin/pkg_install/lib/ftp.c deleted file mode 100644 index 7b78ee4..0000000 --- a/usr.sbin/pkg_install/lib/ftp.c +++ /dev/null @@ -1,424 +0,0 @@ -/* - * ---------------------------------------------------------------------------- - * "THE BEER-WARE LICENSE" (Revision 42): - * wrote this file. As long as you retain this notice you - * can do whatever you want with this stuff. If we meet some day, and you think - * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp - * ---------------------------------------------------------------------------- - * - * $Id: ftp.c,v 1.2 1995/07/31 02:27:58 jkh Exp $ - * - * Return values have been sanitized: - * -1 error, but you (still) have a session. - * -2 error, your session is dead. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "ftp.h" -#include -#include -#include - -/* Handy global for us to stick the port # */ -int FtpPort; - -static void -debug(FTP_t ftp, const char *fmt, ...) -{ - char p[BUFSIZ]; - va_list ap; - va_start(ap, fmt); - strcpy(p,"LIBFTP: "); - (void) vsnprintf(p+strlen(p), sizeof p - strlen(p), fmt, ap); - va_end(ap); - write(ftp->fd_debug,p,strlen(p)); -} - -static int -writes(int fd, char *s) -{ - int i = strlen(s); - if (i != write(fd,s,i)) - return -2; - return 0; -} - -static __inline char* -get_a_line(FTP_t ftp) -{ - static char buf[BUFSIZ]; - int i,j; - - for(i=0;ifd_ctrl,buf+i,1); - if (j != 1) - return 0; - if (buf[i] == '\r' || buf[i] == '\n') { - if (!i) - continue; - buf[i] = '\0'; - debug(ftp, "received <%s>\n",buf); - return buf; - } - i++; - } - return buf; -} - -static int -get_a_number(FTP_t ftp, char **q) -{ - char *p; - int i = -1,j; - - while(1) { - p = get_a_line(ftp); - if (!p) - return -2; - if (!(isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2]))) - continue; - if (i == -1 && p[3] == '-') { - i = strtol(p, 0, 0); - continue; - } - if (p[3] != ' ' && p[3] != '\t') - continue; - j = strtol(p, 0, 0); - if (i == -1) { - if (q) *q = p+4; - return j; - } else if (j == i) { - if (q) *q = p+4; - return j; - } - } -} - -static int -zap(FTP_t ftp) -{ - int i; - - i = writes(ftp->fd_ctrl,"QUIT\r\n"); - debug(ftp, "Zapping ftp connection on %d returns %d\n", ftp->fd_ctrl, i); - close(ftp->fd_ctrl); ftp->fd_ctrl = -1; - close(ftp->fd_xfer); ftp->fd_xfer = -1; - ftp->state = init; - return -2; -} - -static int -botch(FTP_t ftp, char *func, char *state) -{ - debug(ftp, "Botch: %s called outside state %s\n",func,state); - return -2; -} - -static int -cmd(FTP_t ftp, const char *fmt, ...) -{ - char p[BUFSIZ]; - int i; - - va_list ap; - va_start(ap, fmt); - (void) vsnprintf(p, sizeof p, fmt, ap); - va_end(ap); - - debug(ftp, "send <%s>\n",p); - strcat(p,"\r\n"); - if (writes(ftp->fd_ctrl,p)) - return -2; - i = get_a_number(ftp,0); - return i; -} - -FTP_t -FtpInit() -{ - FTP_t ftp; - - ftp = malloc(sizeof *ftp); - if (!ftp) - return ftp; - memset(ftp, 0, sizeof *ftp); - ftp->fd_ctrl = -1; - ftp->fd_xfer = -1; - ftp->fd_debug = -1; - ftp->state = init; - return ftp; -} - -void -FtpDebug(FTP_t ftp, int i) -{ - ftp->fd_debug = i; -} - -int -FtpOpen(FTP_t ftp, char *host, char *user, char *passwd) -{ - struct hostent *he = NULL; - struct sockaddr_in sin; - int s; - unsigned long temp; - int i; - - if (ftp->state != init) - return botch(ftp,"FtpOpen","init"); - - if (!user) - user = "ftp"; - - if (!passwd) - passwd = "??@??(FreeBSD:libftp)"; /* XXX */ - - debug(ftp, "FtpOpen(ftp, %s, %s, %s)\n", host, user, passwd); - - temp = inet_addr(host); - if (temp != INADDR_NONE) { - debug(ftp, "Using dotted IP address `%s'\n", host); - ftp->addrtype = sin.sin_family = AF_INET; - sin.sin_addr.s_addr = temp; - } - else { - debug(ftp, "Trying to resolve `%s'\n", host); - he = gethostbyname(host); - if (!he) { - debug(ftp, "Lookup of `%s' failed!\n", host); - return zap(ftp); - } - ftp->addrtype = sin.sin_family = he->h_addrtype; - bcopy(he->h_addr, (char *)&sin.sin_addr, he->h_length); - } - - sin.sin_port = htons(FtpPort ? FtpPort : 21); - - if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0) - { - debug(ftp, "Socket open failed: %s (%i)\n", strerror(errno), errno); - return zap(ftp); - } - - if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - debug(ftp,"Connection failed: %s (%i)\n", strerror(errno), errno); - (void)close(s); - return zap(ftp); - } - - ftp->fd_ctrl = s; - - debug(ftp, "open (%d)\n",get_a_number(ftp,0)); - - i = cmd(ftp,"USER %s",user); - if (i >= 300 && i < 400) - i = cmd(ftp,"PASS %s",passwd); - if (i >= 299 || i < 0) { - close(ftp->fd_ctrl); ftp->fd_ctrl = -1; - return zap(ftp); - } - ftp->state = isopen; - return 0; -} - -void -FtpClose(FTP_t ftp) -{ - if (ftp->state != init) - return; - - if (ftp->state != isopen) - botch(ftp,"FtpClose","open or init"); - - debug(ftp, "FtpClose(ftp)\n"); - zap(ftp); -} - -int -FtpChdir(FTP_t ftp, char *dir) -{ - int i; - if (ftp->state != isopen) - return botch(ftp,"FtpChdir","open"); - i = cmd(ftp,"CWD %s",dir); - if (i < 0) - return i; - else if (i != 250) - return -1; - return 0; -} - -int -FtpGet(FTP_t ftp, char *file) -{ - int i,s; - char *q; - unsigned char addr[64]; - struct sockaddr_in sin; - u_long a; - - debug(ftp, "FtpGet(ftp,%s)\n",file); - if (ftp->state != isopen) - return botch(ftp,"FtpGet","open"); - if(ftp->binary) { - i = cmd(ftp,"TYPE I"); - if (i < 0) - return zap(ftp); - if (i > 299) - return -1; - } else { - return -1; - } - - if ((s = socket(ftp->addrtype, SOCK_STREAM, 0)) < 0) - return zap(ftp); - - if (ftp->passive) { - debug(ftp, "send <%s>\n","PASV"); - if (writes(ftp->fd_ctrl,"PASV\r\n")) - return zap(ftp); - i = get_a_number(ftp,&q); - if (i < 0) - return zap(ftp); - if (i != 227) - return zap(ftp); - while (*q && !isdigit(*q)) - q++; - if (!*q) - return zap(ftp); - q--; - for(i=0;i<6;i++) { - q++; - addr[i] = strtol(q,&q,10); - } - - sin.sin_family = ftp->addrtype; - bcopy(addr, (char *)&sin.sin_addr, 4); - bcopy(addr+4, (char *)&sin.sin_port, 2); - debug(ftp, "Opening active socket to %s : %u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port)); - - debug(ftp, "Connecting to %s:%u\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port)); - if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - (void)close(s); - debug(ftp, "connect: %s (%d)\n", strerror(errno), errno); - return -1; - } - ftp->fd_xfer = s; - i = cmd(ftp,"RETR %s",file); - if (i < 0) { - close(s); - return zap(ftp); - } - else if (i > 299) { - debug(ftp, "FTP: No such file %s, moving on.\n", file); - close(s); - return -1; - } - ftp->state = xfer; - return s; - } else { - i = sizeof sin; - getsockname(ftp->fd_ctrl,(struct sockaddr *)&sin,&i); - sin.sin_port = 0; - i = sizeof sin; - if (bind(s,(struct sockaddr *)&sin, i) < 0) { - close (s); - debug(ftp,"bind failed %d\n",errno); - return zap(ftp); - } - getsockname(s,(struct sockaddr *)&sin,&i); - if (listen(s,1) < 0) { - close (s); - debug(ftp,"listen failed %d\n",errno); - return zap(ftp); - } - a = ntohl(sin.sin_addr.s_addr); - i = cmd(ftp,"PORT %d,%d,%d,%d,%d,%d", - (a >> 24) & 0xff, - (a >> 16) & 0xff, - (a >> 8) & 0xff, - a & 0xff, - (ntohs(sin.sin_port) >> 8) & 0xff, - ntohs(sin.sin_port) & 0xff); - if (i != 200) - return -1; - i = cmd(ftp,"RETR %s",file); - if (i < 0) { - close(s); - return zap(ftp); - } - else if (i > 299) { - debug(ftp, "FTP: No such file %s, moving on.\n", file); - close(s); - return -1; - } - ftp->fd_xfer = accept(s, 0, 0); - if (ftp->fd_xfer < 0) { - close(s); - return zap(ftp); - } - ftp->state = xfer; - close(s); - return(ftp->fd_xfer); - } -} - -int -FtpEOF(FTP_t ftp) -{ - int i; - - if (ftp->state != xfer) - return botch(ftp,"FtpEOF","xfer"); - debug(ftp, "FtpEOF(ftp)\n"); - close(ftp->fd_xfer); ftp->fd_xfer = -1; - ftp->state = isopen; - i = get_a_number(ftp,0); - if (i < 0) - return zap(ftp); - else if (i != 250 && i != 226) - return -1; - else - return 0; -} - -#ifdef STANDALONE_FTP - -/* main.c */ -int -main(int argc, char **argv) -{ - FTP_t ftp; - int i; - char c; - - ftp = FtpInit(); - if (!ftp) - err(1, "FtpInit()"); - - FtpDebug(ftp, 1); - i = FtpOpen(ftp, "freefall.cdrom.com", "ftp", "phk-libftp@"); - FtpBinary(ftp, 1); - FtpPassive(ftp, 0); - FtpChdir(ftp, "/pub"); - FtpChdir(ftp, "FreeBSD"); - i = FtpGet(ftp, "README"); - while (1 == read(i, &c, 1)) - putchar(c); - FtpEOF(ftp); - return 0; -} - -#endif /*STANDALONE_FTP*/ diff --git a/usr.sbin/pkg_install/lib/ftp.h b/usr.sbin/pkg_install/lib/ftp.h deleted file mode 100644 index 9275dd4..0000000 --- a/usr.sbin/pkg_install/lib/ftp.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _FTP_H_INCLUDE -#define _FTP_H_INCLUDE - -typedef struct { - enum {init, isopen, xfer} state; - int fd_ctrl; - int fd_xfer; - int fd_debug; - int binary; - int passive; - int addrtype; - char *host; - char *file; -} *FTP_t; - -FTP_t FtpInit(); -int FtpOpen(FTP_t, char *host, char *user, char *passwd); -#define FtpBinary(ftp,bool) { (ftp)->binary = (bool); } -#define FtpPassive(ftp,bool) { (ftp)->passive = (bool); } -int FtpChdir(FTP_t, char *); -int FtpGet(FTP_t, char *); -int FtpEOF(FTP_t); -void FtpClose(FTP_t); - -#endif -/* _FTP_H_INCLUDE */ diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h index 2dafbc6..95ac9a1 100644 --- a/usr.sbin/pkg_install/lib/lib.h +++ b/usr.sbin/pkg_install/lib/lib.h @@ -1,4 +1,4 @@ -/* $Id: lib.h,v 1.19 1995/10/25 15:38:34 jkh Exp $ */ +/* $Id: lib.h,v 1.20 1996/06/08 00:46:32 alex Exp $ */ /* * FreeBSD install - a package for the installation and maintainance @@ -123,6 +123,8 @@ char *strconcat(char *, char *); /* File */ Boolean fexists(char *); Boolean isdir(char *); +Boolean isemptydir(char *fname); +Boolean isemptyfile(char *fname); Boolean isfile(char *); Boolean isempty(char *); Boolean isURL(char *); @@ -156,6 +158,7 @@ void mark_plist(Package *); void csum_plist_entry(char *, PackingList); void add_plist(Package *, plist_t, char *); void add_plist_top(Package *, plist_t, char *); +void delete_plist(Package *pkg, Boolean all, plist_t type, char *name); void write_plist(Package *, FILE *); void read_plist(Package *, FILE *); int plist_cmd(char *, char **); diff --git a/usr.sbin/pkg_install/lib/pen.c b/usr.sbin/pkg_install/lib/pen.c index 98a7d71..33ace5f 100644 --- a/usr.sbin/pkg_install/lib/pen.c +++ b/usr.sbin/pkg_install/lib/pen.c @@ -1,5 +1,5 @@ #ifndef lint -static const char *rcsid = "$Id: pen.c,v 1.20 1996/02/09 12:05:26 jkh Exp $"; +static const char *rcsid = "$Id: pen.c,v 1.21 1996/06/08 00:46:33 alex Exp $"; #endif /* @@ -72,8 +72,6 @@ find_play_pen(char *pen, size_t sz) char * make_playpen(char *pen, size_t sz) { - char *tmp; - if (!find_play_pen(pen, sz)) return NULL; diff --git a/usr.sbin/pkg_install/lib/plist.c b/usr.sbin/pkg_install/lib/plist.c index 132c6e1..2a5c674 100644 --- a/usr.sbin/pkg_install/lib/plist.c +++ b/usr.sbin/pkg_install/lib/plist.c @@ -1,5 +1,5 @@ #ifndef lint -static const char *rcsid = "$Id: plist.c,v 1.14 1995/07/28 01:50:35 ache Exp $"; +static const char *rcsid = "$Id: plist.c,v 1.15 1995/11/12 04:55:40 jkh Exp $"; #endif /* @@ -378,7 +378,7 @@ delete_package(Boolean ign_err, Boolean nukedirs, Package *pkg) sprintf(full_name, "%s/%s", Where, p->name); if (isdir(full_name) && p->type == PLIST_FILE) { - warn("Attempting to delete directory `%s' as a file\n" + whinge("Attempting to delete directory `%s' as a file\n" "This packing list is incorrect - ignoring delete request.\n", full_name); } else { -- cgit v1.1