netrc: treat failure to find home dir same as missing netrc file
[platform/upstream/curl.git] / lib / netrc.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_PWD_H
26 #include <pwd.h>
27 #endif
28
29 #include <curl/curl.h>
30 #include "netrc.h"
31
32 #include "strequal.h"
33 #include "strtok.h"
34 #include "curl_memory.h"
35 #include "rawstr.h"
36
37 #define _MPRINTF_REPLACE /* use our functions only */
38 #include <curl/mprintf.h>
39
40 /* The last #include file should be: */
41 #include "memdebug.h"
42
43 /* Get user and password from .netrc when given a machine name */
44
45 enum host_lookup_state {
46   NOTHING,
47   HOSTFOUND,    /* the 'machine' keyword was found */
48   HOSTVALID     /* this is "our" machine! */
49 };
50
51 /*
52  * @unittest: 1304
53  *
54  * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
55  * in.
56  */
57 int Curl_parsenetrc(const char *host,
58                     char **loginp,
59                     char **passwordp,
60                     char *netrcfile)
61 {
62   FILE *file;
63   int retcode=1;
64   int specific_login = (*loginp && **loginp != 0);
65   bool netrc_alloc = FALSE;
66   enum host_lookup_state state=NOTHING;
67
68   char state_login=0;      /* Found a login keyword */
69   char state_password=0;   /* Found a password keyword */
70   int state_our_login=FALSE;  /* With specific_login, found *our* login name */
71
72 #define NETRC DOT_CHAR "netrc"
73
74   if(!netrcfile) {
75     bool home_alloc = FALSE;
76     char *home = curl_getenv("HOME"); /* portable environment reader */
77     if(home) {
78       home_alloc = TRUE;
79 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
80     }
81     else {
82       struct passwd *pw;
83       pw= getpwuid(geteuid());
84       if(pw) {
85         home = pw->pw_dir;
86       }
87 #endif
88     }
89
90     if(!home)
91       return retcode; /* no home directory found (or possibly out of memory) */
92
93     netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
94     if(home_alloc)
95       Curl_safefree(home);
96     if(!netrcfile) {
97       return -1;
98     }
99     netrc_alloc = TRUE;
100   }
101
102   file = fopen(netrcfile, "r");
103   if(netrc_alloc)
104     Curl_safefree(netrcfile);
105   if(file) {
106     char *tok;
107     char *tok_buf;
108     bool done=FALSE;
109     char netrcbuffer[256];
110     int  netrcbuffsize = (int)sizeof(netrcbuffer);
111
112     while(!done && fgets(netrcbuffer, netrcbuffsize, file)) {
113       tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
114       while(!done && tok) {
115
116         if((*loginp && **loginp) && (*passwordp && **passwordp)) {
117           done=TRUE;
118           break;
119         }
120
121         switch(state) {
122         case NOTHING:
123           if(Curl_raw_equal("machine", tok)) {
124             /* the next tok is the machine name, this is in itself the
125                delimiter that starts the stuff entered for this machine,
126                after this we need to search for 'login' and
127                'password'. */
128             state=HOSTFOUND;
129           }
130           break;
131         case HOSTFOUND:
132           if(Curl_raw_equal(host, tok)) {
133             /* and yes, this is our host! */
134             state=HOSTVALID;
135             retcode=0; /* we did find our host */
136           }
137           else
138             /* not our host */
139             state=NOTHING;
140           break;
141         case HOSTVALID:
142           /* we are now parsing sub-keywords concerning "our" host */
143           if(state_login) {
144             if(specific_login) {
145               state_our_login = Curl_raw_equal(*loginp, tok);
146             }
147             else {
148               free(*loginp);
149               *loginp = strdup(tok);
150               if(!*loginp) {
151                 retcode = -1; /* allocation failed */
152                 goto out;
153               }
154             }
155             state_login=0;
156           }
157           else if(state_password) {
158             if(state_our_login || !specific_login) {
159               free(*passwordp);
160               *passwordp = strdup(tok);
161               if(!*passwordp) {
162                 retcode = -1; /* allocation failed */
163                 goto out;
164               }
165             }
166             state_password=0;
167           }
168           else if(Curl_raw_equal("login", tok))
169             state_login=1;
170           else if(Curl_raw_equal("password", tok))
171             state_password=1;
172           else if(Curl_raw_equal("machine", tok)) {
173             /* ok, there's machine here go => */
174             state = HOSTFOUND;
175             state_our_login = FALSE;
176           }
177           break;
178         } /* switch (state) */
179
180         tok = strtok_r(NULL, " \t\n", &tok_buf);
181       } /* while(tok) */
182     } /* while fgets() */
183
184     out:
185     fclose(file);
186   }
187
188   return retcode;
189 }