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