Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netrc: Implement .netrc parsing #244

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ conn_init(conn_t *conn)
conn->ftp->ftp_mode = FTP_PASSIVE;
conn->ftp->tcp.ai_family = conn->conf->ai_family;
if (conn->conf->use_netrc)
netrc_parse(conn);
netrc_parse(conn->conf->netrc_filename, conn->host, conn->user, conn->pass);
printf("host: %s, user: %s, pass: %s\n", conn->host, conn->user, conn->pass);
if (!ftp_connect(conn->ftp, conn->proto, conn->host, conn->port,
conn->user, conn->pass,
conn->conf->io_timeout)) {
Expand Down
179 changes: 114 additions & 65 deletions src/netrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,78 +37,127 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "axel.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "netrc.h"

enum lookup_state {
NOTHING,
DEFAULT,
MACHINE,
HOST_ENTRY,
LOGIN,
PASSWORD
};

void
netrc_parse(conn_t *conn)
#define REL_NETRC "/.netrc"
#define IS_DELIM(x) (*x == ' ' || *x == '\t' || *x == '\n')

static int fd;
static size_t sz;
static char *p;
static char *s_addr;
static char *e_addr;

static size_t file_size(void)
{
struct stat st;
fstat(fd, &st);
return st.st_size;
}

static int netrc_open(const char * const filename)
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
{
FILE *fp;
bool found = false;
int ret;
char *path;
char *netrc;
char *home;
char *tok, *ntok;
char line[MAX_STRING];
char const *delims = " \t\n";
enum lookup_state state = NOTHING;

if (conn->conf->netrc_filename[0] == '\0') {
home = getenv("HOME");
snprintf(conn->conf->netrc_filename, MAX_STRING, "%s/.netrc", home);

ret = 0;
if (filename && filename[0] != '\0') {
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
path = strdup(filename);
} else {
if ((netrc = getenv("NETRC"))) {
path = strdup(netrc);
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
} else if ((home = getenv("HOME"))) {
size_t i = strlen(home) + strlen(REL_NETRC) + 1;
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
path = malloc(i);
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
snprintf(path, i, "%s%s", home, REL_NETRC);
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
} else {
return ret;
}
}
if ((fp = fopen(conn->conf->netrc_filename, "r")) == NULL)
return;
while (!found && fgets(line, MAX_STRING, fp)) {
tok = strtok_r(line, delims, &ntok);
if (tok && *tok == '#')
continue;
while (tok != NULL) {
switch (state) {
case NOTHING:
if (!strcmp("machine", tok)) {
state = MACHINE;
} else if (!strcmp("default", tok)) {
state = DEFAULT;
}
break;
case MACHINE:
if (!strcmp(conn->host, tok)) {
state = HOST_ENTRY;
} else {
state = NOTHING;
}
break;
case DEFAULT:
state = HOST_ENTRY;
// fall through
case HOST_ENTRY:
if (!strcmp("login", tok)) {
state = LOGIN;
} else if (!strcmp("password", tok)) {
state = PASSWORD;
}
break;
case LOGIN:
strcpy(conn->user, tok);
state = HOST_ENTRY;
break;
case PASSWORD:
strcpy(conn->pass, tok);
found = true;
break;
default:
if ((fd = open(path, O_RDONLY, 0)) == -1) {
ret = 0;
} else {
sz = file_size();
s_addr = mmap(NULL, sz, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
e_addr = s_addr + sz;
ret = 1;
}
free(path);
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
return ret;
}

static void netrc_close(void)
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
{
munmap(s_addr, sz);
close(fd);
}

static int next_token(char **t)
{
char *q;
int len;

if (!p)
p = s_addr;
while (IS_DELIM(p) && p < e_addr)
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
p++;
q = p;
while (!IS_DELIM(q) && q < e_addr)
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
q++;
if (q == e_addr)
return 0;
len = q-p;
*t = p;
p = q;
return len;
}

static void get_creds(char *user, char *pwd)
{
int len;
char *tok;

while ((len = next_token(&tok))) {
if (!strncmp("login", tok, len)) {
len = next_token(&tok);
strncpy(user, tok, len);
user[len] = '\0';
} else if (!strncmp("password", tok, len)) {
len = next_token(&tok);
strncpy(pwd, tok, len);
pwd[len] = '\0';
} else if (!strncmp("machine", tok, len) || !strncmp("default", tok, len)) {
p -= len;
break;
}
}
}

int
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
netrc_parse(const char *filename, const char *host, char *user, char *password)
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
{
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
int len;
char *tok;

if (!netrc_open(filename))
return 0;
while ((len = next_token(&tok))) {
if (!strncmp("default", tok, len)) {
get_creds(user, password);
break;
} else if (!strncmp("machine", tok, len)) {
if ((len = next_token(&tok)) && !strncmp(host, tok, len)) {
get_creds(user, password);
break;
}
tok = strtok_r(NULL, delims, &ntok);
}
}
netrc_close();
return 0;
}
2 changes: 1 addition & 1 deletion src/netrc.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
#ifndef AXEL_NETRC_H
#define AXEL_NETRC_H

void netrc_parse(conn_t *conn);
int netrc_parse(const char *filename, const char *host, char *user, char *pwd);

#endif /* AXEL_NETRC_H */