#include "setup.h" moved first of all includes
[platform/upstream/curl.git] / lib / netrc.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  *  The contents of this file are subject to the Mozilla Public License
9  *  Version 1.0 (the "License"); you may not use this file except in
10  *  compliance with the License. You may obtain a copy of the License at
11  *  http://www.mozilla.org/MPL/
12  *
13  *  Software distributed under the License is distributed on an "AS IS"
14  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  *  License for the specific language governing rights and limitations
16  *  under the License.
17  *
18  *  The Original Code is Curl.
19  *
20  *  The Initial Developer of the Original Code is Daniel Stenberg.
21  *
22  *  Portions created by the Initial Developer are Copyright (C) 1998.
23  *  All Rights Reserved.
24  *
25  *  Contributor(s):
26  *   Rafael Sagula <sagula@inf.ufrgs.br>
27  *   Sampo Kellomaki <sampo@iki.fi>
28  *   Linas Vepstas <linas@linas.org>
29  *   Bjorn Reese <breese@imada.ou.dk>
30  *   Johan Anderson <johan@homemail.com>
31  *   Kjell Ericson <Kjell.Ericson@haxx.se>
32  *   Troy Engel <tengel@palladium.net>
33  *   Ryan Nelson <ryan@inch.com>
34  *   Bjorn Stenberg <Bjorn.Stenberg@haxx.se>
35  *   Angus Mackay <amackay@gus.ml.org>
36  *
37  * ------------------------------------------------------------
38  * Main author:
39  * - Daniel Stenberg <daniel@haxx.se>
40  *
41  *      http://curl.haxx.se
42  *
43  * $Source$
44  * $Revision$
45  * $Date$
46  * $Author$
47  * $State$
48  * $Locker$
49  *
50  * ------------------------------------------------------------
51  ****************************************************************************/
52
53 #include "setup.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include "getenv.h"
60 #include "strequal.h"
61
62 /* Debug this single source file with:
63    'make netrc' then run './netrc'!
64
65    Oh, make sure you have a .netrc file too ;-)
66  */
67
68 /* Get user and password from .netrc when given a machine name */
69
70 enum {
71   NOTHING,
72   HOSTFOUND,    /* the 'machine' keyword was found */
73   HOSTCOMPLETE, /* the machine name following the keyword was found too */
74   HOSTVALID,    /* this is "our" machine! */
75
76   HOSTEND /* LAST enum */
77 };
78
79 /* make sure we have room for at least this size: */
80 #define LOGINSIZE 64
81 #define PASSWORDSIZE 64
82
83 int ParseNetrc(char *host,
84                char *login,
85                char *password)
86 {
87   FILE *file;
88   char netrcbuffer[256];
89   int retcode=1;
90   
91   char *home = GetEnv("HOME"); /* portable environment reader */
92   int state=NOTHING;
93
94   char state_login=0;
95   char state_password=0;
96
97 #define NETRC DOT_CHAR "netrc"
98
99   if(!home)
100     return -1;
101
102   if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
103     free(home);
104     return -1;
105   }
106
107   sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
108
109   file = fopen(netrcbuffer, "r");
110   if(file) {
111     char *tok;
112     while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
113       tok=strtok(netrcbuffer, " \t\n");
114       while(tok) {
115         switch(state) {
116         case NOTHING:
117           if(strequal("machine", tok)) {
118             /* the next tok is the machine name, this is in itself the
119                delimiter that starts the stuff entered for this machine,
120                after this we need to search for 'login' and
121                'password'. */
122             state=HOSTFOUND;
123           }
124           break;
125         case HOSTFOUND:
126           if(strequal(host, tok)) {
127             /* and yes, this is our host! */
128             state=HOSTVALID;
129 #ifdef _NETRC_DEBUG
130             printf("HOST: %s\n", tok);
131 #endif
132             retcode=0; /* we did find our host */
133           }
134           else
135             /* not our host */
136             state=NOTHING;
137           break;
138         case HOSTVALID:
139           /* we are now parsing sub-keywords concerning "our" host */
140           if(state_login) {
141             strncpy(login, tok, LOGINSIZE-1);
142 #ifdef _NETRC_DEBUG
143             printf("LOGIN: %s\n", login);
144 #endif
145             state_login=0;
146           }
147           else if(state_password) {
148             strncpy(password, tok, PASSWORDSIZE-1);
149 #if _NETRC_DEBUG
150             printf("PASSWORD: %s\n", password);
151 #endif
152             state_password=0;
153           }
154           else if(strequal("login", tok))
155             state_login=1;
156           else if(strequal("password", tok))
157             state_password=1;
158           else if(strequal("machine", tok)) {
159             /* ok, there's machine here go => */
160             state = HOSTFOUND;
161           }
162           break;
163         } /* switch (state) */
164         tok = strtok(NULL, " \t\n");
165       } /* while (tok) */
166     } /* while fgets() */
167
168     fclose(file);
169   }
170
171   free(home);
172
173   return retcode;
174 }
175
176 #ifdef _NETRC_DEBUG
177 int main(int argc, char **argv)
178 {
179   char login[64]="";
180   char password[64]="";
181
182   if(argc<2)
183     return -1;
184
185   if(0 == ParseNetrc(argv[1], login, password)) {
186     printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
187            argv[1], login, password);
188   }
189 }
190
191 #endif