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