#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "fetchmail.h"
#include "netrc.h"
#include "i18n.h"
#ifdef STANDALONE
# define xstrdup strdup
# define xmalloc malloc
# define xrealloc realloc
char *program_name = "netrc";
#endif
static void
maybe_add_to_list (netrc_entry **newentry, netrc_entry **list)
{
netrc_entry *a, *l;
a = *newentry;
l = *list;
if (a && ! a->account)
{
if (a->host)
free (a->host);
if (a->password)
free (a->password);
}
else
{
if (a)
{
a->next = l;
l = a;
}
a = (netrc_entry *) xmalloc (sizeof (netrc_entry));
}
memset (a, 0, sizeof(*a));
*newentry = a;
*list = l;
return;
}
netrc_entry *
parse_netrc (file)
char *file;
{
FILE *fp;
char buf[POPBUFSIZE+1], *p, *tok;
const char *premature_token;
netrc_entry *current, *retval;
int ln;
enum
{
tok_nothing, tok_account, tok_login, tok_macdef, tok_machine, tok_password
} last_token = tok_nothing;
current = retval = NULL;
fp = fopen (file, "r");
if (!fp)
{
return NULL;
}
ln = 0;
premature_token = NULL;
while (fgets(buf, POPBUFSIZE, fp))
{
ln++;
for (p = buf + strlen(buf) - 1; (p >= buf) && isspace(*p); p--)
*p = '\0';
p = buf;
if (!*p)
{
if (last_token == tok_macdef)
last_token = tok_nothing;
else
continue;
}
while (*p && last_token != tok_macdef)
{
char quote_char = 0;
char *pp;
while (*p && isspace (*p))
p++;
if (*p == '#')
break;
tok = pp = p;
while (*p && (quote_char || !isspace (*p)))
{
if (quote_char)
{
if (quote_char == *p)
{
quote_char = 0;
p ++;
}
else
{
*pp = *p;
p ++;
pp ++;
}
}
else
{
if (*p == '"' || *p == '\'')
quote_char = *p;
else
{
*pp = *p;
pp ++;
}
p ++;
}
}
if (*p)
*p ++ = '\0';
*pp = 0;
switch (last_token)
{
case tok_login:
if (current)
current->account = (char *) xstrdup (tok);
else
premature_token = "login";
break;
case tok_machine:
maybe_add_to_list (¤t, &retval);
current->host = (char *) xstrdup (tok);
break;
case tok_password:
if (current)
current->password = (char *) xstrdup (tok);
else
premature_token = "password";
break;
case tok_macdef:
if (!current)
premature_token = "macdef";
break;
case tok_account:
if (!current)
premature_token = "account";
break;
case tok_nothing:
break;
}
if (premature_token)
{
#ifdef HAVE_ERROR
error_at_line (0, file, ln,
GT_("warning: found \"%s\" before any host names"),
premature_token);
#else
fprintf (stderr,
GT_("%s:%d: warning: found \"%s\" before any host names\n"),
file, ln, premature_token);
#endif
premature_token = NULL;
}
if (last_token != tok_nothing)
last_token = tok_nothing;
else
{
if (!strcmp (tok, "default"))
{
maybe_add_to_list (¤t, &retval);
}
else if (!strcmp (tok, "login"))
last_token = tok_login;
else if (!strcmp (tok, "user"))
last_token = tok_login;
else if (!strcmp (tok, "macdef"))
last_token = tok_macdef;
else if (!strcmp (tok, "machine"))
last_token = tok_machine;
else if (!strcmp (tok, "password"))
last_token = tok_password;
else if (!strcmp (tok, "passwd"))
last_token = tok_password;
else if (!strcmp (tok, "account"))
last_token = tok_account;
else
{
fprintf (stderr, GT_("%s:%d: warning: unknown token \"%s\"\n"),
file, ln, tok);
}
}
}
}
fclose (fp);
maybe_add_to_list (¤t, &retval);
free (current);
current = retval;
retval = NULL;
while (current)
{
netrc_entry *saved_reference;
saved_reference = current->next;
current->next = retval;
retval = current;
current = saved_reference;
}
return retval;
}
netrc_entry *
search_netrc (list, host, account)
netrc_entry *list;
char *host, *account;
{
while (list)
{
if (list->host && !strcmp(list->host, host))
if (!list->account || !strcmp(list->account, account))
break;
list = list->next;
}
return list;
}
#ifdef STANDALONE
#include <sys/types.h>
#include <sys/stat.h>
extern int errno;
int
main (argc, argv)
int argc;
char **argv;
{
struct stat sb;
char *program_name, *file, *host, *account;
netrc_entry *head, *a;
program_name = argv[0];
file = argv[1];
host = argv[2];
account = argv[3];
if (stat (file, &sb))
{
fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
strerror (errno));
exit (1);
}
head = parse_netrc (file);
if (!head)
{
fprintf (stderr, "%s: no entries found in %s\n", argv[0], file);
exit (1);
}
if (host && account)
{
int i, status;
status = 0;
printf("Host: %s, Account: %s\n", host, account);
a = search_netrc (head, host, account);
if (a)
{
if (a->password)
{
fputc (' ', stdout);
fputs (a->password, stdout);
}
}
fputc ('\n', stdout);
exit (status);
}
a = head;
while (a)
{
if (a->host)
fputs (a->host, stdout);
else
fputs ("DEFAULT", stdout);
fputc (' ', stdout);
fputs (a->account, stdout);
if (a->password)
{
fputc (' ', stdout);
fputs (a->password, stdout);
}
fputc ('\n', stdout);
a = a->next;
}
exit (0);
}
#endif