]>
Commit | Line | Data |
---|---|---|
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 | 30 | using std::string; |
592b7800 MV |
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: */ | |
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 */ | |
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; | |
592b7800 | 55 | |
592b7800 MV |
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 | asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC); | |
73 | if(!netrcfile) | |
74 | return -1; | |
75 | else | |
76 | netrc_alloc = true; | |
77 | } | |
78 | ||
79 | file = fopen (netrcfile, "r"); | |
80 | if(file) { | |
81 | char *tok; | |
82 | char *tok_buf; | |
83 | bool done = false; | |
84 | char netrcbuffer[256]; | |
85 | ||
91c03d37 DK |
86 | int state = NOTHING; |
87 | char state_login = 0; /* Found a login keyword */ | |
88 | char state_password = 0; /* Found a password keyword */ | |
89 | ||
592b7800 MV |
90 | while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) { |
91 | tok = strtok_r (netrcbuffer, " \t\n", &tok_buf); | |
92 | while (!done && tok) { | |
93 | if(login[0] && password[0]) { | |
94 | done = true; | |
95 | break; | |
96 | } | |
97 | ||
98 | switch(state) { | |
99 | case NOTHING: | |
100 | if (!strcasecmp ("machine", tok)) { | |
101 | /* the next tok is the machine name, this is in itself the | |
102 | delimiter that starts the stuff entered for this machine, | |
103 | after this we need to search for 'login' and | |
104 | 'password'. */ | |
105 | state = HOSTFOUND; | |
106 | } | |
107 | break; | |
108 | case HOSTFOUND: | |
278835da MV |
109 | /* extended definition of a "machine" if we have a "/" |
110 | we match the start of the string (host.startswith(token) */ | |
111 | if ((strchr(host, '/') && strstr(host, tok) == host) || | |
112 | (!strcasecmp (host, tok))) { | |
592b7800 MV |
113 | /* and yes, this is our host! */ |
114 | state = HOSTVALID; | |
115 | retcode = 0; /* we did find our host */ | |
116 | } | |
117 | else | |
118 | /* not our host */ | |
119 | state = NOTHING; | |
120 | break; | |
121 | case HOSTVALID: | |
122 | /* we are now parsing sub-keywords concerning "our" host */ | |
123 | if (state_login) { | |
124 | if (specific_login) | |
125 | state_our_login = !strcasecmp (login, tok); | |
126 | else | |
96b1e40d MV |
127 | { |
128 | if (strlen(tok) > LOGINSIZE) | |
129 | _error->Error("login token too long %i (max: %i)", | |
130 | strlen(tok), LOGINSIZE); | |
592b7800 | 131 | strncpy (login, tok, LOGINSIZE - 1); |
96b1e40d | 132 | } |
592b7800 MV |
133 | state_login = 0; |
134 | } else if (state_password) { | |
96b1e40d MV |
135 | if (state_our_login || !specific_login) |
136 | { | |
137 | if (strlen(tok) > PASSWORDSIZE) | |
138 | _error->Error("password token too long %i (max %i)", | |
139 | strlen(tok), PASSWORDSIZE); | |
592b7800 | 140 | strncpy (password, tok, PASSWORDSIZE - 1); |
96b1e40d | 141 | } |
592b7800 MV |
142 | state_password = 0; |
143 | } else if (!strcasecmp ("login", tok)) | |
144 | state_login = 1; | |
145 | else if (!strcasecmp ("password", tok)) | |
146 | state_password = 1; | |
147 | else if(!strcasecmp ("machine", tok)) { | |
148 | /* ok, there's machine here go => */ | |
149 | state = HOSTFOUND; | |
150 | state_our_login = false; | |
151 | } | |
152 | break; | |
153 | } /* switch (state) */ | |
154 | ||
155 | tok = strtok_r (NULL, " \t\n", &tok_buf); | |
156 | } /* while(tok) */ | |
157 | } /* while fgets() */ | |
158 | ||
159 | fclose(file); | |
160 | } | |
161 | ||
162 | if (netrc_alloc) | |
163 | free(netrcfile); | |
164 | ||
165 | return retcode; | |
166 | } | |
167 | ||
168 | void maybe_add_auth (URI &Uri, string NetRCFile) | |
169 | { | |
f1c081b6 | 170 | if (_config->FindB("Debug::Acquire::netrc", false) == true) |
01fc8930 MV |
171 | std::clog << "maybe_add_auth: " << (string)Uri |
172 | << " " << NetRCFile << std::endl; | |
f1c081b6 | 173 | if (Uri.Password.empty () == true || Uri.User.empty () == true) |
592b7800 MV |
174 | { |
175 | if (NetRCFile.empty () == false) | |
176 | { | |
96b1e40d MV |
177 | char login[LOGINSIZE] = ""; |
178 | char password[PASSWORDSIZE] = ""; | |
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()); |
01fc8930 | 183 | if (host && parsenetrc (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 | |
188 | << " pass-size: " << strlen(password) | |
189 | << std::endl; | |
592b7800 MV |
190 | Uri.User = string (login); |
191 | Uri.Password = string (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()); |
01fc8930 MV |
202 | if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0) |
203 | { | |
204 | if (_config->FindB("Debug::Acquire::netrc", false) == true) | |
205 | std::clog << "hostpath: " << hostpath | |
206 | << " user: " << login | |
207 | << " pass-size: " << strlen(password) | |
208 | << std::endl; | |
209 | Uri.User = string (login); | |
210 | Uri.Password = string (password); | |
01fc8930 | 211 | } |
fc3eb5b2 DK |
212 | free(netrcfile); |
213 | free(hostpath); | |
592b7800 MV |
214 | } |
215 | } | |
216 | } | |
217 | ||
218 | #ifdef DEBUG | |
219 | int 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 |