/src/PROJ/curl/lib/netrc.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | #ifndef CURL_DISABLE_NETRC |
27 | | |
28 | | #ifdef HAVE_PWD_H |
29 | | #ifdef __AMIGA__ |
30 | | #undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */ |
31 | | #endif |
32 | | #include <pwd.h> |
33 | | #ifdef __AMIGA__ |
34 | | #define __NO_NET_API |
35 | | #endif |
36 | | #endif |
37 | | |
38 | | #include <curl/curl.h> |
39 | | #include "netrc.h" |
40 | | #include "strcase.h" |
41 | | #include "curl_get_line.h" |
42 | | #include "curlx/fopen.h" |
43 | | #include "curlx/strparse.h" |
44 | | |
45 | | /* The last 2 #include files should be in this order */ |
46 | | #include "curl_memory.h" |
47 | | #include "memdebug.h" |
48 | | |
49 | | /* Get user and password from .netrc when given a machine name */ |
50 | | |
51 | | enum host_lookup_state { |
52 | | NOTHING, |
53 | | HOSTFOUND, /* the 'machine' keyword was found */ |
54 | | HOSTVALID, /* this is "our" machine! */ |
55 | | MACDEF |
56 | | }; |
57 | | |
58 | | enum found_state { |
59 | | NONE, |
60 | | LOGIN, |
61 | | PASSWORD |
62 | | }; |
63 | | |
64 | 0 | #define FOUND_LOGIN 1 |
65 | 0 | #define FOUND_PASSWORD 2 |
66 | | |
67 | 0 | #define MAX_NETRC_LINE 16384 |
68 | 0 | #define MAX_NETRC_FILE (128*1024) |
69 | 0 | #define MAX_NETRC_TOKEN 4096 |
70 | | |
71 | | /* convert a dynbuf call CURLcode error to a NETRCcode error */ |
72 | | #define curl2netrc(result) \ |
73 | 0 | (((result) == CURLE_OUT_OF_MEMORY) ? \ |
74 | 0 | NETRC_OUT_OF_MEMORY : NETRC_SYNTAX_ERROR) |
75 | | |
76 | | static NETRCcode file2memory(const char *filename, struct dynbuf *filebuf) |
77 | 0 | { |
78 | 0 | NETRCcode ret = NETRC_FILE_MISSING; /* if it cannot open the file */ |
79 | 0 | FILE *file = curlx_fopen(filename, FOPEN_READTEXT); |
80 | 0 | struct dynbuf linebuf; |
81 | 0 | curlx_dyn_init(&linebuf, MAX_NETRC_LINE); |
82 | |
|
83 | 0 | if(file) { |
84 | 0 | CURLcode result = CURLE_OK; |
85 | 0 | bool eof; |
86 | 0 | ret = NETRC_OK; |
87 | 0 | do { |
88 | 0 | const char *line; |
89 | 0 | result = Curl_get_line(&linebuf, file, &eof); |
90 | 0 | if(!result) { |
91 | 0 | line = curlx_dyn_ptr(&linebuf); |
92 | | /* skip comments on load */ |
93 | 0 | curlx_str_passblanks(&line); |
94 | 0 | if(*line == '#') |
95 | 0 | continue; |
96 | 0 | result = curlx_dyn_add(filebuf, line); |
97 | 0 | } |
98 | 0 | if(result) { |
99 | 0 | curlx_dyn_free(filebuf); |
100 | 0 | ret = curl2netrc(result); |
101 | 0 | break; |
102 | 0 | } |
103 | 0 | } while(!eof); |
104 | 0 | } |
105 | 0 | curlx_dyn_free(&linebuf); |
106 | 0 | if(file) |
107 | 0 | curlx_fclose(file); |
108 | 0 | return ret; |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * Returns zero on success. |
113 | | */ |
114 | | static NETRCcode parsenetrc(struct store_netrc *store, |
115 | | const char *host, |
116 | | char **loginp, /* might point to a username */ |
117 | | char **passwordp, |
118 | | const char *netrcfile) |
119 | 0 | { |
120 | 0 | NETRCcode retcode = NETRC_NO_MATCH; |
121 | 0 | char *login = *loginp; |
122 | 0 | char *password = NULL; |
123 | 0 | bool specific_login = !!login; /* points to something */ |
124 | 0 | enum host_lookup_state state = NOTHING; |
125 | 0 | enum found_state keyword = NONE; |
126 | 0 | unsigned char found = 0; /* login + password found bits, as they can come in |
127 | | any order */ |
128 | 0 | bool our_login = FALSE; /* found our login name */ |
129 | 0 | bool done = FALSE; |
130 | 0 | char *netrcbuffer; |
131 | 0 | struct dynbuf token; |
132 | 0 | struct dynbuf *filebuf = &store->filebuf; |
133 | 0 | DEBUGASSERT(!*passwordp); |
134 | 0 | curlx_dyn_init(&token, MAX_NETRC_TOKEN); |
135 | |
|
136 | 0 | if(!store->loaded) { |
137 | 0 | NETRCcode ret = file2memory(netrcfile, filebuf); |
138 | 0 | if(ret) |
139 | 0 | return ret; |
140 | 0 | store->loaded = TRUE; |
141 | 0 | } |
142 | | |
143 | 0 | netrcbuffer = curlx_dyn_ptr(filebuf); |
144 | |
|
145 | 0 | while(!done) { |
146 | 0 | const char *tok = netrcbuffer; |
147 | 0 | while(tok && !done) { |
148 | 0 | const char *tok_end; |
149 | 0 | bool quoted; |
150 | 0 | curlx_dyn_reset(&token); |
151 | 0 | curlx_str_passblanks(&tok); |
152 | | /* tok is first non-space letter */ |
153 | 0 | if(state == MACDEF) { |
154 | 0 | if((*tok == '\n') || (*tok == '\r')) |
155 | 0 | state = NOTHING; /* end of macro definition */ |
156 | 0 | } |
157 | |
|
158 | 0 | if(!*tok || (*tok == '\n')) |
159 | | /* end of line */ |
160 | 0 | break; |
161 | | |
162 | | /* leading double-quote means quoted string */ |
163 | 0 | quoted = (*tok == '\"'); |
164 | |
|
165 | 0 | tok_end = tok; |
166 | 0 | if(!quoted) { |
167 | 0 | size_t len = 0; |
168 | 0 | CURLcode result; |
169 | 0 | while(*tok_end > ' ') { |
170 | 0 | tok_end++; |
171 | 0 | len++; |
172 | 0 | } |
173 | 0 | if(!len) { |
174 | 0 | retcode = NETRC_SYNTAX_ERROR; |
175 | 0 | goto out; |
176 | 0 | } |
177 | 0 | result = curlx_dyn_addn(&token, tok, len); |
178 | 0 | if(result) { |
179 | 0 | retcode = curl2netrc(result); |
180 | 0 | goto out; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | else { |
184 | 0 | bool escape = FALSE; |
185 | 0 | bool endquote = FALSE; |
186 | 0 | tok_end++; /* pass the leading quote */ |
187 | 0 | while(*tok_end) { |
188 | 0 | CURLcode result; |
189 | 0 | char s = *tok_end; |
190 | 0 | if(escape) { |
191 | 0 | escape = FALSE; |
192 | 0 | switch(s) { |
193 | 0 | case 'n': |
194 | 0 | s = '\n'; |
195 | 0 | break; |
196 | 0 | case 'r': |
197 | 0 | s = '\r'; |
198 | 0 | break; |
199 | 0 | case 't': |
200 | 0 | s = '\t'; |
201 | 0 | break; |
202 | 0 | } |
203 | 0 | } |
204 | 0 | else if(s == '\\') { |
205 | 0 | escape = TRUE; |
206 | 0 | tok_end++; |
207 | 0 | continue; |
208 | 0 | } |
209 | 0 | else if(s == '\"') { |
210 | 0 | tok_end++; /* pass the ending quote */ |
211 | 0 | endquote = TRUE; |
212 | 0 | break; |
213 | 0 | } |
214 | 0 | result = curlx_dyn_addn(&token, &s, 1); |
215 | 0 | if(result) { |
216 | 0 | retcode = curl2netrc(result); |
217 | 0 | goto out; |
218 | 0 | } |
219 | 0 | tok_end++; |
220 | 0 | } |
221 | 0 | if(escape || !endquote) { |
222 | | /* bad syntax, get out */ |
223 | 0 | retcode = NETRC_SYNTAX_ERROR; |
224 | 0 | goto out; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | if(curlx_dyn_len(&token)) |
229 | 0 | tok = curlx_dyn_ptr(&token); |
230 | 0 | else |
231 | | /* since tok might actually be NULL for no content, set it to blank |
232 | | to avoid having to deal with it being NULL */ |
233 | 0 | tok = ""; |
234 | |
|
235 | 0 | switch(state) { |
236 | 0 | case NOTHING: |
237 | 0 | if(curl_strequal("macdef", tok)) |
238 | | /* Define a macro. A macro is defined with the specified name; its |
239 | | contents begin with the next .netrc line and continue until a |
240 | | null line (consecutive new-line characters) is encountered. */ |
241 | 0 | state = MACDEF; |
242 | 0 | else if(curl_strequal("machine", tok)) { |
243 | | /* the next tok is the machine name, this is in itself the delimiter |
244 | | that starts the stuff entered for this machine, after this we |
245 | | need to search for 'login' and 'password'. */ |
246 | 0 | state = HOSTFOUND; |
247 | 0 | keyword = NONE; |
248 | 0 | found = 0; |
249 | 0 | our_login = FALSE; |
250 | 0 | Curl_safefree(password); |
251 | 0 | if(!specific_login) |
252 | 0 | Curl_safefree(login); |
253 | 0 | } |
254 | 0 | else if(curl_strequal("default", tok)) { |
255 | 0 | state = HOSTVALID; |
256 | 0 | retcode = NETRC_OK; /* we did find our host */ |
257 | 0 | } |
258 | 0 | break; |
259 | 0 | case MACDEF: |
260 | 0 | if(!*tok) |
261 | 0 | state = NOTHING; |
262 | 0 | break; |
263 | 0 | case HOSTFOUND: |
264 | 0 | if(curl_strequal(host, tok)) { |
265 | | /* and yes, this is our host! */ |
266 | 0 | state = HOSTVALID; |
267 | 0 | retcode = NETRC_OK; /* we did find our host */ |
268 | 0 | } |
269 | 0 | else |
270 | | /* not our host */ |
271 | 0 | state = NOTHING; |
272 | 0 | break; |
273 | 0 | case HOSTVALID: |
274 | | /* we are now parsing sub-keywords concerning "our" host */ |
275 | 0 | if(keyword == LOGIN) { |
276 | 0 | if(specific_login) |
277 | 0 | our_login = !Curl_timestrcmp(login, tok); |
278 | 0 | else { |
279 | 0 | our_login = TRUE; |
280 | 0 | free(login); |
281 | 0 | login = strdup(tok); |
282 | 0 | if(!login) { |
283 | 0 | retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */ |
284 | 0 | goto out; |
285 | 0 | } |
286 | 0 | } |
287 | 0 | found |= FOUND_LOGIN; |
288 | 0 | keyword = NONE; |
289 | 0 | } |
290 | 0 | else if(keyword == PASSWORD) { |
291 | 0 | free(password); |
292 | 0 | password = strdup(tok); |
293 | 0 | if(!password) { |
294 | 0 | retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */ |
295 | 0 | goto out; |
296 | 0 | } |
297 | 0 | if(!specific_login || our_login) |
298 | 0 | found |= FOUND_PASSWORD; |
299 | 0 | keyword = NONE; |
300 | 0 | } |
301 | 0 | else if(curl_strequal("login", tok)) |
302 | 0 | keyword = LOGIN; |
303 | 0 | else if(curl_strequal("password", tok)) |
304 | 0 | keyword = PASSWORD; |
305 | 0 | else if(curl_strequal("machine", tok)) { |
306 | | /* a new machine here */ |
307 | 0 | if(found & FOUND_PASSWORD) { |
308 | 0 | done = TRUE; |
309 | 0 | break; |
310 | 0 | } |
311 | 0 | state = HOSTFOUND; |
312 | 0 | keyword = NONE; |
313 | 0 | found = 0; |
314 | 0 | Curl_safefree(password); |
315 | 0 | if(!specific_login) |
316 | 0 | Curl_safefree(login); |
317 | 0 | } |
318 | 0 | else if(curl_strequal("default", tok)) { |
319 | 0 | state = HOSTVALID; |
320 | 0 | retcode = NETRC_OK; /* we did find our host */ |
321 | 0 | Curl_safefree(password); |
322 | 0 | if(!specific_login) |
323 | 0 | Curl_safefree(login); |
324 | 0 | } |
325 | 0 | if((found == (FOUND_PASSWORD|FOUND_LOGIN)) && our_login) { |
326 | 0 | done = TRUE; |
327 | 0 | break; |
328 | 0 | } |
329 | 0 | break; |
330 | 0 | } /* switch (state) */ |
331 | 0 | tok = ++tok_end; |
332 | 0 | } |
333 | 0 | if(!done) { |
334 | 0 | char *nl = NULL; |
335 | 0 | if(tok) |
336 | 0 | nl = strchr(tok, '\n'); |
337 | 0 | if(!nl) |
338 | 0 | break; |
339 | | /* point to next line */ |
340 | 0 | netrcbuffer = &nl[1]; |
341 | 0 | } |
342 | 0 | } /* while !done */ |
343 | | |
344 | 0 | out: |
345 | 0 | curlx_dyn_free(&token); |
346 | 0 | if(!retcode) { |
347 | 0 | if(!password && our_login) { |
348 | | /* success without a password, set a blank one */ |
349 | 0 | password = strdup(""); |
350 | 0 | if(!password) |
351 | 0 | retcode = NETRC_OUT_OF_MEMORY; /* out of memory */ |
352 | 0 | } |
353 | 0 | else if(!login && !password) |
354 | | /* a default with no credentials */ |
355 | 0 | retcode = NETRC_NO_MATCH; |
356 | 0 | } |
357 | 0 | if(!retcode) { |
358 | | /* success */ |
359 | 0 | if(!specific_login) |
360 | 0 | *loginp = login; |
361 | 0 | *passwordp = password; |
362 | 0 | } |
363 | 0 | else { |
364 | 0 | curlx_dyn_free(filebuf); |
365 | 0 | store->loaded = FALSE; |
366 | 0 | if(!specific_login) |
367 | 0 | free(login); |
368 | 0 | free(password); |
369 | 0 | } |
370 | |
|
371 | 0 | return retcode; |
372 | 0 | } |
373 | | |
374 | | const char *Curl_netrc_strerror(NETRCcode ret) |
375 | 0 | { |
376 | 0 | switch(ret) { |
377 | 0 | default: |
378 | 0 | return ""; /* not a legit error */ |
379 | 0 | case NETRC_FILE_MISSING: |
380 | 0 | return "no such file"; |
381 | 0 | case NETRC_NO_MATCH: |
382 | 0 | return "no matching entry"; |
383 | 0 | case NETRC_OUT_OF_MEMORY: |
384 | 0 | return "out of memory"; |
385 | 0 | case NETRC_SYNTAX_ERROR: |
386 | 0 | return "syntax error"; |
387 | 0 | } |
388 | | /* never reached */ |
389 | 0 | } |
390 | | |
391 | | /* |
392 | | * @unittest: 1304 |
393 | | * |
394 | | * *loginp and *passwordp MUST be allocated if they are not NULL when passed |
395 | | * in. |
396 | | */ |
397 | | NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host, |
398 | | char **loginp, char **passwordp, |
399 | | const char *netrcfile) |
400 | 0 | { |
401 | 0 | NETRCcode retcode = NETRC_OK; |
402 | 0 | char *filealloc = NULL; |
403 | |
|
404 | 0 | if(!netrcfile) { |
405 | 0 | char *home = NULL; |
406 | 0 | char *homea = NULL; |
407 | 0 | #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID) |
408 | 0 | char pwbuf[1024]; |
409 | 0 | #endif |
410 | 0 | filealloc = curl_getenv("NETRC"); |
411 | 0 | if(!filealloc) { |
412 | 0 | homea = curl_getenv("HOME"); /* portable environment reader */ |
413 | 0 | if(homea) { |
414 | 0 | home = homea; |
415 | 0 | #if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID) |
416 | 0 | } |
417 | 0 | else { |
418 | 0 | struct passwd pw, *pw_res; |
419 | 0 | if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res) |
420 | 0 | && pw_res) { |
421 | 0 | home = pw.pw_dir; |
422 | 0 | } |
423 | | #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID) |
424 | | } |
425 | | else { |
426 | | struct passwd *pw; |
427 | | pw = getpwuid(geteuid()); |
428 | | if(pw) { |
429 | | home = pw->pw_dir; |
430 | | } |
431 | | #elif defined(_WIN32) |
432 | | } |
433 | | else { |
434 | | homea = curl_getenv("USERPROFILE"); |
435 | | if(homea) { |
436 | | home = homea; |
437 | | } |
438 | | #endif |
439 | 0 | } |
440 | |
|
441 | 0 | if(!home) |
442 | 0 | return NETRC_FILE_MISSING; /* no home directory found (or possibly out |
443 | | of memory) */ |
444 | | |
445 | 0 | filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR); |
446 | 0 | if(!filealloc) { |
447 | 0 | free(homea); |
448 | 0 | return NETRC_OUT_OF_MEMORY; |
449 | 0 | } |
450 | 0 | } |
451 | 0 | retcode = parsenetrc(store, host, loginp, passwordp, filealloc); |
452 | 0 | free(filealloc); |
453 | | #ifdef _WIN32 |
454 | | if(retcode == NETRC_FILE_MISSING) { |
455 | | /* fallback to the old-style "_netrc" file */ |
456 | | filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR); |
457 | | if(!filealloc) { |
458 | | free(homea); |
459 | | return NETRC_OUT_OF_MEMORY; |
460 | | } |
461 | | retcode = parsenetrc(store, host, loginp, passwordp, filealloc); |
462 | | free(filealloc); |
463 | | } |
464 | | #endif |
465 | 0 | free(homea); |
466 | 0 | } |
467 | 0 | else |
468 | 0 | retcode = parsenetrc(store, host, loginp, passwordp, netrcfile); |
469 | 0 | return retcode; |
470 | 0 | } |
471 | | |
472 | | void Curl_netrc_init(struct store_netrc *s) |
473 | 0 | { |
474 | 0 | curlx_dyn_init(&s->filebuf, MAX_NETRC_FILE); |
475 | 0 | s->loaded = FALSE; |
476 | 0 | } |
477 | | void Curl_netrc_cleanup(struct store_netrc *s) |
478 | 0 | { |
479 | 0 | curlx_dyn_free(&s->filebuf); |
480 | | s->loaded = FALSE; |
481 | 0 | } |
482 | | #endif |