]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
merged lp:~mvo/apt/mvo
[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/error.h>
19 #include <apt-pkg/fileutl.h>
20
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
30 using std::string;
31
32 /* Get user and password from .netrc when given a machine name */
33
34 enum {
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: */
43 #define LOGINSIZE 256
44 #define PASSWORDSIZE 256
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 */
48 int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
49 {
50 FILE *file;
51 int retcode = 1;
52 int specific_login = (login[0] != 0);
53 char *home = NULL;
54 bool netrc_alloc = false;
55
56 int state_our_login = false; /* With specific_login,
57 found *our* login name */
58
59 if (!netrcfile) {
60 home = getenv ("HOME"); /* portable environment reader */
61
62 if (!home) {
63 struct passwd *pw;
64 pw = getpwuid (geteuid ());
65 if(pw)
66 home = pw->pw_dir;
67 }
68
69 if (!home)
70 return -1;
71
72 if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL)
73 return -1;
74 else
75 netrc_alloc = true;
76 }
77
78 file = fopen (netrcfile, "r");
79 if(file) {
80 char *tok;
81 char *tok_buf;
82 bool done = false;
83 char netrcbuffer[256];
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 && fgets(netrcbuffer, sizeof (netrcbuffer), file)) {
90 tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
91 while (!done && tok) {
92 if(login[0] && password[0]) {
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, tok);
125 else
126 {
127 if (strlen(tok) > LOGINSIZE)
128 _error->Error("login token too long %i (max: %i)",
129 strlen(tok), LOGINSIZE);
130 strncpy (login, tok, LOGINSIZE - 1);
131 }
132 state_login = 0;
133 } else if (state_password) {
134 if (state_our_login || !specific_login)
135 {
136 if (strlen(tok) > PASSWORDSIZE)
137 _error->Error("password token too long %i (max %i)",
138 strlen(tok), PASSWORDSIZE);
139 strncpy (password, tok, PASSWORDSIZE - 1);
140 }
141 state_password = 0;
142 } else if (!strcasecmp ("login", tok))
143 state_login = 1;
144 else if (!strcasecmp ("password", tok))
145 state_password = 1;
146 else if(!strcasecmp ("machine", tok)) {
147 /* ok, there's machine here go => */
148 state = HOSTFOUND;
149 state_our_login = false;
150 }
151 break;
152 } /* switch (state) */
153
154 tok = strtok_r (NULL, " \t\n", &tok_buf);
155 } /* while(tok) */
156 } /* while fgets() */
157
158 fclose(file);
159 }
160
161 if (netrc_alloc)
162 free(netrcfile);
163
164 return retcode;
165 }
166
167 void maybe_add_auth (URI &Uri, string NetRCFile)
168 {
169 if (_config->FindB("Debug::Acquire::netrc", false) == true)
170 std::clog << "maybe_add_auth: " << (string)Uri
171 << " " << NetRCFile << std::endl;
172 if (Uri.Password.empty () == true || Uri.User.empty () == true)
173 {
174 if (NetRCFile.empty () == false)
175 {
176 char login[LOGINSIZE] = "";
177 char password[PASSWORDSIZE] = "";
178 char *netrcfile = strdup(NetRCFile.c_str());
179
180 // first check for a generic host based netrc entry
181 char *host = strdup(Uri.Host.c_str());
182 if (host && parsenetrc (host, login, password, netrcfile) == 0)
183 {
184 if (_config->FindB("Debug::Acquire::netrc", false) == true)
185 std::clog << "host: " << host
186 << " user: " << login
187 << " pass-size: " << strlen(password)
188 << std::endl;
189 Uri.User = string (login);
190 Uri.Password = string (password);
191 free(netrcfile);
192 free(host);
193 return;
194 }
195 free(host);
196
197 // if host did not work, try Host+Path next, this will trigger
198 // a lookup uri.startswith(host) in the netrc file parser (because
199 // of the "/"
200 char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str());
201 if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0)
202 {
203 if (_config->FindB("Debug::Acquire::netrc", false) == true)
204 std::clog << "hostpath: " << hostpath
205 << " user: " << login
206 << " pass-size: " << strlen(password)
207 << std::endl;
208 Uri.User = string (login);
209 Uri.Password = string (password);
210 }
211 free(netrcfile);
212 free(hostpath);
213 }
214 }
215 }
216
217 #ifdef DEBUG
218 int main(int argc, char* argv[])
219 {
220 char login[64] = "";
221 char password[64] = "";
222
223 if(argc < 2)
224 return -1;
225
226 if(0 == parsenetrc (argv[1], login, password, argv[2])) {
227 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
228 }
229 }
230 #endif