]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/netrc.cc
properly handle SIGWINCH in PackageManagerFancy again
[apt.git] / apt-pkg / contrib / netrc.cc
CommitLineData
592b7800
MV
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $
4/* ######################################################################
5
6 netrc file parser - returns the login and password of a give host in
7 a specified netrc-type file
8
9 Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and
10 placed into the Public Domain, do with it what you will.
11
12 ##################################################################### */
13 /*}}}*/
ea542140 14#include <config.h>
592b7800 15
f1c081b6 16#include <apt-pkg/configuration.h>
472ff00e 17#include <apt-pkg/strutl.h>
96b1e40d 18#include <apt-pkg/error.h>
01fc8930 19#include <apt-pkg/fileutl.h>
472ff00e 20
592b7800
MV
21#include <iostream>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <pwd.h>
27
28#include "netrc.h"
29
8f3ba4e8 30using std::string;
592b7800
MV
31
32/* Get user and password from .netrc when given a machine name */
33
34enum {
35 NOTHING,
36 HOSTFOUND, /* the 'machine' keyword was found */
37 HOSTCOMPLETE, /* the machine name following the keyword was found too */
38 HOSTVALID, /* this is "our" machine! */
39 HOSTEND /* LAST enum */
40};
41
42/* make sure we have room for at least this size: */
7735ad05
MV
43#define LOGINSIZE 256
44#define PASSWORDSIZE 256
592b7800
MV
45#define NETRC DOT_CHAR "netrc"
46
47/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
b8c50bd0 48static int parsenetrc_string (char *host, std::string &login, std::string &password, char *netrcfile = NULL)
592b7800
MV
49{
50 FILE *file;
51 int retcode = 1;
b8c50bd0 52 int specific_login = (login.empty() == false);
592b7800 53 bool netrc_alloc = false;
592b7800 54
592b7800 55 if (!netrcfile) {
69c2ecbd 56 char const * home = getenv ("HOME"); /* portable environment reader */
592b7800
MV
57
58 if (!home) {
59 struct passwd *pw;
60 pw = getpwuid (geteuid ());
61 if(pw)
62 home = pw->pw_dir;
63 }
64
65 if (!home)
66 return -1;
67
31bda500 68 if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL)
592b7800
MV
69 return -1;
70 else
71 netrc_alloc = true;
72 }
73
74 file = fopen (netrcfile, "r");
75 if(file) {
76 char *tok;
77 char *tok_buf;
78 bool done = false;
7f18595b
DK
79 char *netrcbuffer = NULL;
80 size_t netrcbuffer_size = 0;
592b7800 81
91c03d37
DK
82 int state = NOTHING;
83 char state_login = 0; /* Found a login keyword */
84 char state_password = 0; /* Found a password keyword */
69c2ecbd
DK
85 int state_our_login = false; /* With specific_login,
86 found *our* login name */
91c03d37 87
7f18595b 88 while (!done && getline(&netrcbuffer, &netrcbuffer_size, file) != -1) {
592b7800
MV
89 tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
90 while (!done && tok) {
b8c50bd0 91 if(login.empty() == false && password.empty() == false) {
592b7800
MV
92 done = true;
93 break;
94 }
95
96 switch(state) {
97 case NOTHING:
98 if (!strcasecmp ("machine", tok)) {
99 /* the next tok is the machine name, this is in itself the
100 delimiter that starts the stuff entered for this machine,
101 after this we need to search for 'login' and
102 'password'. */
103 state = HOSTFOUND;
104 }
105 break;
106 case HOSTFOUND:
278835da
MV
107 /* extended definition of a "machine" if we have a "/"
108 we match the start of the string (host.startswith(token) */
109 if ((strchr(host, '/') && strstr(host, tok) == host) ||
110 (!strcasecmp (host, tok))) {
592b7800
MV
111 /* and yes, this is our host! */
112 state = HOSTVALID;
113 retcode = 0; /* we did find our host */
114 }
115 else
116 /* not our host */
117 state = NOTHING;
118 break;
119 case HOSTVALID:
120 /* we are now parsing sub-keywords concerning "our" host */
121 if (state_login) {
122 if (specific_login)
b8c50bd0 123 state_our_login = !strcasecmp (login.c_str(), tok);
592b7800 124 else
b8c50bd0 125 login = tok;
592b7800
MV
126 state_login = 0;
127 } else if (state_password) {
128 if (state_our_login || !specific_login)
b8c50bd0 129 password = tok;
592b7800
MV
130 state_password = 0;
131 } else if (!strcasecmp ("login", tok))
132 state_login = 1;
133 else if (!strcasecmp ("password", tok))
134 state_password = 1;
135 else if(!strcasecmp ("machine", tok)) {
136 /* ok, there's machine here go => */
137 state = HOSTFOUND;
138 state_our_login = false;
139 }
140 break;
141 } /* switch (state) */
142
143 tok = strtok_r (NULL, " \t\n", &tok_buf);
144 } /* while(tok) */
7f18595b 145 } /* while getline() */
592b7800 146
7f18595b 147 free(netrcbuffer);
592b7800
MV
148 fclose(file);
149 }
150
151 if (netrc_alloc)
152 free(netrcfile);
153
154 return retcode;
155}
b8c50bd0
DK
156// for some unknown reason this method is exported so keep a compatible interface for now …
157int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
158{
159 std::string login_string, password_string;
160 int const ret = parsenetrc_string(host, login_string, password_string, netrcfile);
161 if (ret < 0)
162 return ret;
163 strncpy(login, login_string.c_str(), LOGINSIZE - 1);
164 strncpy(password, password_string.c_str(), PASSWORDSIZE - 1);
165 return ret;
166}
167
592b7800
MV
168
169void maybe_add_auth (URI &Uri, string NetRCFile)
170{
f1c081b6 171 if (_config->FindB("Debug::Acquire::netrc", false) == true)
01fc8930
MV
172 std::clog << "maybe_add_auth: " << (string)Uri
173 << " " << NetRCFile << std::endl;
f1c081b6 174 if (Uri.Password.empty () == true || Uri.User.empty () == true)
592b7800
MV
175 {
176 if (NetRCFile.empty () == false)
177 {
b8c50bd0 178 std::string login, password;
fc3eb5b2 179 char *netrcfile = strdup(NetRCFile.c_str());
592b7800 180
01fc8930 181 // first check for a generic host based netrc entry
fc3eb5b2 182 char *host = strdup(Uri.Host.c_str());
b8c50bd0 183 if (host && parsenetrc_string(host, login, password, netrcfile) == 0)
592b7800 184 {
01fc8930
MV
185 if (_config->FindB("Debug::Acquire::netrc", false) == true)
186 std::clog << "host: " << host
187 << " user: " << login
b8c50bd0 188 << " pass-size: " << password.size()
01fc8930 189 << std::endl;
b8c50bd0
DK
190 Uri.User = login;
191 Uri.Password = password;
fc3eb5b2
DK
192 free(netrcfile);
193 free(host);
01fc8930 194 return;
592b7800 195 }
fc3eb5b2 196 free(host);
592b7800 197
278835da
MV
198 // if host did not work, try Host+Path next, this will trigger
199 // a lookup uri.startswith(host) in the netrc file parser (because
200 // of the "/"
fc3eb5b2 201 char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str());
b8c50bd0 202 if (hostpath && parsenetrc_string(hostpath, login, password, netrcfile) == 0)
01fc8930
MV
203 {
204 if (_config->FindB("Debug::Acquire::netrc", false) == true)
205 std::clog << "hostpath: " << hostpath
206 << " user: " << login
b8c50bd0 207 << " pass-size: " << password.size()
01fc8930 208 << std::endl;
b8c50bd0
DK
209 Uri.User = login;
210 Uri.Password = password;
01fc8930 211 }
fc3eb5b2
DK
212 free(netrcfile);
213 free(hostpath);
592b7800
MV
214 }
215 }
216}
217
218#ifdef DEBUG
219int main(int argc, char* argv[])
220{
221 char login[64] = "";
222 char password[64] = "";
223
224 if(argc < 2)
225 return -1;
226
227 if(0 == parsenetrc (argv[1], login, password, argv[2])) {
228 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
229 }
230}
231#endif