if () => if()
[platform/upstream/curl.git] / lib / netrc.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, 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  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_PWD_H
34 #include <pwd.h>
35 #endif
36 #ifdef VMS
37 #include <unixlib.h>
38 #endif
39
40 #include <curl/curl.h>
41 #include "netrc.h"
42
43 #include "strequal.h"
44 #include "strtok.h"
45 #include "memory.h"
46
47 #define _MPRINTF_REPLACE /* use our functions only */
48 #include <curl/mprintf.h>
49
50 /* The last #include file should be: */
51 #include "memdebug.h"
52
53 /* Debug this single source file with:
54    'make netrc' then run './netrc'!
55
56    Oh, make sure you have a .netrc file too ;-)
57  */
58
59 /* Get user and password from .netrc when given a machine name */
60
61 enum {
62   NOTHING,
63   HOSTFOUND,    /* the 'machine' keyword was found */
64   HOSTCOMPLETE, /* the machine name following the keyword was found too */
65   HOSTVALID,    /* this is "our" machine! */
66
67   HOSTEND /* LAST enum */
68 };
69
70 /* make sure we have room for at least this size: */
71 #define LOGINSIZE 64
72 #define PASSWORDSIZE 64
73
74 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
75 int Curl_parsenetrc(char *host,
76                     char *login,
77                     char *password,
78                     char *netrcfile)
79 {
80   FILE *file;
81   int retcode=1;
82   int specific_login = (login[0] != 0);
83   char *home = NULL;
84   bool home_alloc = FALSE;
85   bool netrc_alloc = FALSE;
86   int state=NOTHING;
87
88   char state_login=0;      /* Found a login keyword */
89   char state_password=0;   /* Found a password keyword */
90   int state_our_login=FALSE;  /* With specific_login, found *our* login name */
91
92 #define NETRC DOT_CHAR "netrc"
93
94 #ifdef CURLDEBUG
95   {
96     /* This is a hack to allow testing.
97      * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
98      * then it's the path to a substitute .netrc for testing purposes *only* */
99
100     char *override = curl_getenv("CURL_DEBUG_NETRC");
101
102     if(override) {
103       fprintf(stderr, "NETRC: overridden " NETRC " file: %s\n", override);
104       netrcfile = override;
105       netrc_alloc = TRUE;
106     }
107   }
108 #endif /* CURLDEBUG */
109   if(!netrcfile) {
110     home = curl_getenv("HOME"); /* portable environment reader */
111     if(home) {
112       home_alloc = TRUE;
113 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
114     }
115     else {
116       struct passwd *pw;
117       pw= getpwuid(geteuid());
118       if(pw) {
119 #ifdef  VMS
120         home = decc$translate_vms(pw->pw_dir);
121 #else
122         home = pw->pw_dir;
123 #endif
124       }
125 #endif
126     }
127
128     if(!home)
129       return -1;
130
131     netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
132     if(!netrcfile) {
133       if(home_alloc)
134         free(home);
135       return -1;
136     }
137     netrc_alloc = TRUE;
138   }
139
140   file = fopen(netrcfile, "r");
141   if(file) {
142     char *tok;
143     char *tok_buf;
144     bool done=FALSE;
145     char netrcbuffer[256];
146
147     while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
148       tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
149       while(!done && tok) {
150
151         if(login[0] && password[0]) {
152           done=TRUE;
153           break;
154         }
155
156         switch(state) {
157         case NOTHING:
158           if(strequal("machine", tok)) {
159             /* the next tok is the machine name, this is in itself the
160                delimiter that starts the stuff entered for this machine,
161                after this we need to search for 'login' and
162                'password'. */
163             state=HOSTFOUND;
164           }
165           break;
166         case HOSTFOUND:
167           if(strequal(host, tok)) {
168             /* and yes, this is our host! */
169             state=HOSTVALID;
170 #ifdef _NETRC_DEBUG
171             fprintf(stderr, "HOST: %s\n", tok);
172 #endif
173             retcode=0; /* we did find our host */
174           }
175           else
176             /* not our host */
177             state=NOTHING;
178           break;
179         case HOSTVALID:
180           /* we are now parsing sub-keywords concerning "our" host */
181           if(state_login) {
182             if(specific_login) {
183               state_our_login = strequal(login, tok);
184             }
185             else {
186               strncpy(login, tok, LOGINSIZE-1);
187 #ifdef _NETRC_DEBUG
188               fprintf(stderr, "LOGIN: %s\n", login);
189 #endif
190             }
191             state_login=0;
192           }
193           else if(state_password) {
194             if(state_our_login || !specific_login) {
195               strncpy(password, tok, PASSWORDSIZE-1);
196 #ifdef _NETRC_DEBUG
197               fprintf(stderr, "PASSWORD: %s\n", password);
198 #endif
199             }
200             state_password=0;
201           }
202           else if(strequal("login", tok))
203             state_login=1;
204           else if(strequal("password", tok))
205             state_password=1;
206           else if(strequal("machine", tok)) {
207             /* ok, there's machine here go => */
208             state = HOSTFOUND;
209             state_our_login = FALSE;
210           }
211           break;
212         } /* switch (state) */
213
214         tok = strtok_r(NULL, " \t\n", &tok_buf);
215       } /* while(tok) */
216     } /* while fgets() */
217
218     fclose(file);
219   }
220
221   if(home_alloc)
222     free(home);
223   if(netrc_alloc)
224     free(netrcfile);
225
226   return retcode;
227 }
228
229 #ifdef _NETRC_DEBUG
230 int main(int argc, argv_item_t argv[])
231 {
232   char login[64]="";
233   char password[64]="";
234
235   if(argc<2)
236     return -1;
237
238   if(0 == ParseNetrc(argv[1], login, password)) {
239     printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
240            argv[1], login, password);
241   }
242 }
243
244 #endif