aboutsummaryrefslogtreecommitdiff
path: root/lib/netrc.c
blob: 4572d775d952540daca8265768d71262d5d0ddc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* This file is part of GNU Pies
   Copyright (C) 2015, 2017 Sergey Poznyakoff
  
   GNU Pies is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Pies is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "libpies.h"
#include <unistd.h>
#include <pwd.h>
#include <grecs.h>
#include <wordsplit.h>
#include <errno.h>
#include <netdb.h>

/* Compare two hostnames. Return 0 if they have the same address type,
   address length *and* at least one of the addresses of A matches
   B */
static int
hostcmp (const char *a, const char *b)
{
  struct hostent *hp = gethostbyname (a);
  char **addrlist;
  char *dptr;
  char **addr;
  size_t i, count;
  size_t entry_length;
  int entry_type;

  if (!hp)
    return 1;

  for (count = 1, addr = hp->h_addr_list; *addr; addr++)
    count++;
  addrlist = grecs_malloc (count * (sizeof *addrlist + hp->h_length)
			   - hp->h_length);
  dptr = (char *) (addrlist + count);
  for (i = 0; i < count - 1; i++)
    {
      memcpy (dptr, hp->h_addr_list[i], hp->h_length);
      addrlist[i] = dptr;
      dptr += hp->h_length;
    }
  addrlist[i] = NULL;
  entry_length = hp->h_length;
  entry_type = hp->h_addrtype;

  hp = gethostbyname (b);
  if (!hp || entry_length != hp->h_length || entry_type != hp->h_addrtype)
    {
      grecs_free (addrlist);
      return 1;
    }

  for (addr = addrlist; *addr; addr++)
    {
      char **p;

      for (p = hp->h_addr_list; *p; p++)
	{
	  if (memcmp (*addr, *p, entry_length) == 0)
	    {
	      grecs_free (addrlist);
	      return 0;
	    }
	}
    }
  grecs_free (addrlist);
  return 1;
}

static int
match_url (size_t argc, char **argv, struct pies_url *url)
{
  if (hostcmp (argv[1], url->host ? url->host : "localhost") == 0)
    {
      if (argc >= 4 && strcmp (argv[2], "port") == 0)
	{
          unsigned long n = strtoul (argv[3], NULL, 10);
	  if (n == url->port)
	    return 1;
	}
      else
	return 1;
    }
  return 0;
}

static void
parse_args (struct grecs_locus *loc, char **argv,
	    char **username, char **password)
{
  if (*username)
    {
      grecs_free (*username);
      *username = NULL;
    }
  if (*password)
    {
      grecs_free (*password);
      *password = NULL;
    }
  
  while (*argv)
    {
      if (!argv[1])
	{
	  grecs_error (loc, 0, _("incomplete sentence"));
	  break;
	}
      if (strcmp (*argv, "login") == 0)
	*username = grecs_strdup (argv[1]);
      else if (strcmp (*argv, "password") == 0)
	*password = grecs_strdup (argv[1]);
      argv += 2;
    }
}

/* Parse traditional .netrc file. Set up auth_args fields in accordance with
   it. */
void
netrc_scan_file (FILE *fp, struct grecs_locus *loc, struct pies_url *url)
{
  char *buf = NULL;
  size_t n = 0;
  struct wordsplit ws;
  int wsflags = WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_SQUEEZE_DELIMS;
  char *username = NULL;
  char *password = NULL;

  while (grecs_getline (&buf, &n, fp) > 0)
    {
      loc->beg.line++;

      if (wordsplit (buf, &ws, wsflags))
	{
	  grecs_error (loc, 0, "wordsplit: %s", wordsplit_strerror (&ws));
	  continue;
	}
      wsflags |= WRDSF_REUSE;

      if (ws.ws_wordc == 0)
	continue;

      if (strcmp (ws.ws_wordv[0], "machine") == 0)
	{
	  if (match_url (ws.ws_wordc, ws.ws_wordv, url))
	    {
	      parse_args (loc, ws.ws_wordv + 2, &username, &password);
	      break;
	    }
	}
      else if (strcmp (ws.ws_wordv[0], "default") == 0)
	parse_args (loc, ws.ws_wordv + 1, &username, &password);
      else
	grecs_error (loc, 0, _("ignoring unrecognized line"));
    }
  grecs_free (buf);

  if (wsflags & WRDSF_REUSE)
    wordsplit_free (&ws);

  url->user = username;
  url->passwd = password;
}

void
netrc_scan (struct pies_url *url)
{
  FILE *fp;
  struct grecs_locus loc;
  char *filename;
  char *homedir;

  if (url->user)
    return;
  
  homedir = getenv ("HOME");
  if (!homedir)
    {
      struct passwd *pwd = getpwuid (getuid ());
      if (!pwd)
	return;
      homedir = pwd->pw_dir;
    }

  filename = mkfilename (homedir, ".netrc", NULL);
  fp = fopen (filename, "r");
  if (!fp)
    {
      if (errno != ENOENT)
	grecs_error (NULL, 0,
		     _("cannot open configuration file %s: %s"),
		     filename, strerror (errno));
      free (filename);
      return;
    }

  loc.beg.file = loc.end.file = (char*) filename;
  loc.beg.col = loc.end.col = 0;
  loc.beg.line = 0;
  netrc_scan_file (fp, &loc, url);
  fclose (fp);
  free (filename);
}

Return to:

Send suggestions and report system problems to the System administrator.