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
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
28 changes: 17 additions & 11 deletions src/netrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ netrc_init(const char *path)
int
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
netrc_parse(netrc_t *netrc, const char *host, char *user, size_t user_len, char *pass, size_t pass_len)
{
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
bool matched;
bool matched = false;
buffer_t tok, save_buf = {};
struct parser {
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
const char * const key;
Expand All @@ -186,6 +186,7 @@ netrc_parse(netrc_t *netrc, const char *host, char *user, size_t user_len, char
struct parser *p = parser;
while (p < parser + parser_len && strncmp(p->key, tok.data, tok.len))
p++;

/* unknown token? -> abort */
if (p >= parser + parser_len)
return 0;
Expand All @@ -198,19 +199,24 @@ netrc_parse(netrc_t *netrc, const char *host, char *user, size_t user_len, char
tok = memtok(NULL, 0, tok_delim, &save_buf);
if (!strncmp(host, tok.data, tok.len))
matched = true;
} else
} else if (tok.data[0] == 'd') {
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
matched = true;
continue;
}
} else {
tok = memtok(NULL, 0, tok_delim, &save_buf);
if (matched) {
// FIXME should we be aborting?
if (tok.len >= p->len) {
tok.len = p->len - 1;
p->dst[tok.len] = 0;
}

/* need strlcpy here, as tok.data isn't NULL-terminated */
strlcpy(p->dst, tok.data, tok.len + 1);
davidpolverari marked this conversation as resolved.
Show resolved Hide resolved
}
}

tok = memtok(NULL, 0, tok_delim, &save_buf);
if (!matched)
continue;
// FIXME should we be aborting?
if (tok.len >= p->len) {
tok.len = p->len - 1;
p->dst[tok.len] = 0;
}
memcpy(p->dst, tok.data, tok.len + 1);
}
return 1;
}