1997-05-12 21:28:15 +08:00
|
|
|
#include "miscfn.h"
|
1997-01-18 00:20:18 +08:00
|
|
|
|
1996-10-15 11:15:52 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1997-01-18 00:20:18 +08:00
|
|
|
#if HAVE_ALLOCA_H
|
1996-12-12 11:32:22 +08:00
|
|
|
# include <alloca.h>
|
|
|
|
#endif
|
|
|
|
|
1996-10-15 11:15:52 +08:00
|
|
|
#include "ftp.h"
|
|
|
|
#include "messages.h"
|
1997-05-15 22:04:39 +08:00
|
|
|
#include "miscfn.h"
|
1996-10-15 11:15:52 +08:00
|
|
|
#include "rpmlib.h"
|
|
|
|
#include "url.h"
|
|
|
|
|
|
|
|
struct pwcacheEntry {
|
|
|
|
char * machine;
|
|
|
|
char * account;
|
|
|
|
char * pw;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
static char * getFtpPassword(char * machine, char * account, int mustAsk);
|
|
|
|
static int urlFtpLogin(char * url, char ** fileNamePtr);
|
|
|
|
static int urlFtpSplit(char * url, char ** user, char ** pw, char ** host,
|
|
|
|
char ** path);
|
|
|
|
|
|
|
|
static char * getFtpPassword(char * machine, char * account, int mustAsk) {
|
|
|
|
static struct pwcacheEntry * pwCache = NULL;
|
|
|
|
static int pwCount = 0;
|
|
|
|
int i;
|
|
|
|
char * prompt;
|
|
|
|
|
|
|
|
for (i = 0; i < pwCount; i++) {
|
|
|
|
if (!strcmp(pwCache[i].machine, machine) &&
|
|
|
|
!strcmp(pwCache[i].account, account))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < pwCount && !mustAsk) {
|
|
|
|
return pwCache[i].pw;
|
|
|
|
} else if (i == pwCount) {
|
|
|
|
pwCount++;
|
|
|
|
if (pwCache)
|
|
|
|
pwCache = realloc(pwCache, sizeof(*pwCache) * pwCount);
|
|
|
|
else
|
|
|
|
pwCache = malloc(sizeof(*pwCache));
|
|
|
|
|
|
|
|
pwCache[i].machine = strdup(machine);
|
|
|
|
pwCache[i].account = strdup(account);
|
|
|
|
} else
|
|
|
|
free(pwCache[i].pw);
|
|
|
|
|
|
|
|
prompt = alloca(strlen(machine) + strlen(account) + 50);
|
|
|
|
sprintf(prompt, "Password for %s@%s: ", account, machine);
|
|
|
|
|
|
|
|
pwCache[i].pw = strdup(getpass(prompt));
|
|
|
|
|
|
|
|
return pwCache[i].pw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int urlFtpSplit(char * url, char ** user, char ** pw, char ** host,
|
|
|
|
char ** path) {
|
|
|
|
char * chptr, * machineName, * fileName;
|
|
|
|
char * userName = NULL;
|
|
|
|
char * password = NULL;
|
|
|
|
|
|
|
|
url += 6; /* skip ftp:// */
|
|
|
|
|
|
|
|
chptr = url;
|
|
|
|
while (*chptr && (*chptr != '/')) chptr++;
|
|
|
|
if (!*chptr) return -1;
|
|
|
|
|
|
|
|
machineName = url; /* could still have user:pass@ though */
|
|
|
|
fileName = chptr;
|
|
|
|
|
|
|
|
*path = strdup(chptr);
|
|
|
|
|
|
|
|
*chptr = '\0';
|
|
|
|
|
|
|
|
chptr = fileName;
|
|
|
|
while (chptr > url && *chptr != '@') chptr--;
|
|
|
|
if (chptr > url) { /* we have a username */
|
|
|
|
*chptr = '\0';
|
|
|
|
userName = machineName;
|
|
|
|
machineName = chptr + 1;
|
|
|
|
|
|
|
|
chptr = userName;
|
|
|
|
while (*chptr && *chptr != ':') chptr++;
|
|
|
|
if (*chptr) { /* we have a password */
|
|
|
|
*chptr = '\0';
|
|
|
|
password = chptr + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userName && !password) {
|
|
|
|
password = getFtpPassword(machineName, userName, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userName)
|
|
|
|
*user = strdup(userName);
|
|
|
|
|
|
|
|
if (password)
|
|
|
|
*pw = strdup(password);
|
|
|
|
|
|
|
|
*host = strdup(machineName);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int urlFtpLogin(char * url, char ** fileNamePtr) {
|
|
|
|
char * buf;
|
|
|
|
char * machineName, * fileName;
|
|
|
|
char * userName = NULL;
|
|
|
|
char * password = NULL;
|
|
|
|
char * proxy;
|
1996-10-21 02:24:06 +08:00
|
|
|
char * portStr, * endPtr;
|
|
|
|
int port;
|
1996-10-15 11:15:52 +08:00
|
|
|
int ftpconn;
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, "getting %s via anonymous ftp\n", url);
|
1996-10-15 11:15:52 +08:00
|
|
|
|
|
|
|
buf = alloca(strlen(url) + 1);
|
|
|
|
strcpy(buf, url);
|
|
|
|
|
|
|
|
urlFtpSplit(buf, &userName, &password, &machineName, &fileName);
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, "logging into %s as %s, pw %s\n", machineName,
|
1996-10-15 11:15:52 +08:00
|
|
|
userName ? userName : "ftp",
|
|
|
|
password ? password : "(username)");
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
proxy = rpmGetVar(RPMVAR_FTPPROXY);
|
|
|
|
portStr = rpmGetVar(RPMVAR_FTPPORT);
|
1996-10-21 02:24:06 +08:00
|
|
|
if (!portStr) {
|
|
|
|
port = -1;
|
|
|
|
} else {
|
|
|
|
port = strtol(portStr, &endPtr, 0);
|
|
|
|
if (*endPtr) {
|
|
|
|
fprintf(stderr, "error: ftpport must be a number\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1996-10-15 11:15:52 +08:00
|
|
|
|
1996-10-21 02:24:06 +08:00
|
|
|
ftpconn = ftpOpen(machineName, userName, password, proxy, port);
|
1996-10-15 11:15:52 +08:00
|
|
|
|
|
|
|
free(machineName);
|
|
|
|
free(userName);
|
|
|
|
free(password);
|
|
|
|
|
|
|
|
if (ftpconn < 0) {
|
|
|
|
free(fileName);
|
|
|
|
return ftpconn;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fileNamePtr = fileName;
|
|
|
|
|
|
|
|
return ftpconn;
|
|
|
|
}
|
|
|
|
|
|
|
|
int urlGetFd(char * url, struct urlContext * context) {
|
|
|
|
char * fileName;
|
|
|
|
int fd;
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, "getting %s via anonymous ftp\n", url);
|
1996-10-15 11:15:52 +08:00
|
|
|
|
|
|
|
if ((context->ftpControl = urlFtpLogin(url, &fileName)) < 0)
|
|
|
|
return context->ftpControl;
|
|
|
|
|
|
|
|
fd = ftpGetFileDesc(context->ftpControl, fileName);
|
|
|
|
|
|
|
|
free(fileName);
|
|
|
|
|
|
|
|
if (fd < 0) ftpClose(context->ftpControl);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int urlFinishedFd(struct urlContext * context) {
|
|
|
|
ftpClose(context->ftpControl);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int urlGetFile(char * url, char * dest) {
|
|
|
|
char * fileName;
|
|
|
|
int ftpconn;
|
|
|
|
int rc;
|
|
|
|
int fd;
|
|
|
|
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, "getting %s via anonymous ftp\n", url);
|
1996-10-15 11:15:52 +08:00
|
|
|
|
|
|
|
if ((ftpconn = urlFtpLogin(url, &fileName)) < 0) return ftpconn;
|
|
|
|
|
|
|
|
fd = creat(dest, 0600);
|
|
|
|
|
|
|
|
if (fd < 0) {
|
1996-11-19 02:02:36 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, "failed to create %s\n", dest);
|
1996-10-15 11:15:52 +08:00
|
|
|
ftpClose(ftpconn);
|
|
|
|
free(fileName);
|
|
|
|
return FTPERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rc = ftpGetFile(ftpconn, fileName, fd))) {
|
|
|
|
free(fileName);
|
|
|
|
unlink(dest);
|
|
|
|
close(fd);
|
|
|
|
ftpClose(ftpconn);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(fileName);
|
|
|
|
|
|
|
|
ftpClose(ftpconn);
|
|
|
|
|
1996-10-21 02:24:06 +08:00
|
|
|
return rc;
|
1996-10-15 11:15:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int urlIsURL(char * url) {
|
|
|
|
if (!strncmp(url, "ftp://", 6)) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|