new
[libcurl.git] / lib / netrc.c
blob9fba130e80a35a0521e5ee7aa3110c2a5d676ec2
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
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/
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.
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
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.nu>
32 * Troy Engel <tengel@palladium.net>
33 * Ryan Nelson <ryan@inch.com>
34 * Bjorn Stenberg <Bjorn.Stenberg@haxx.nu>
35 * Angus Mackay <amackay@gus.ml.org>
37 * ------------------------------------------------------------
38 * Main author:
39 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
41 * http://curl.haxx.nu
43 * $Source: /cvsroot/curl/curl/lib/netrc.c,v $
44 * $Revision: 1.1.1.1 $
45 * $Date: 1999-12-29 14:21:35 $
46 * $Author: bagder $
47 * $State: Exp $
48 * $Locker: $
50 * ------------------------------------------------------------
51 ****************************************************************************/
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
57 #include "setup.h"
58 #include "getenv.h"
60 /* Debug this single source file with:
61 'make netrc' then run './netrc'!
63 Oh, make sure you have a .netrc file too ;-)
66 /* Get user and password from .netrc when given a machine name */
68 enum {
69 NOTHING,
70 HOSTFOUND, /* the 'machine' keyword was found */
71 HOSTCOMPLETE, /* the machine name following the keyword was found too */
72 HOSTVALID, /* this is "our" machine! */
74 HOSTEND /* LAST enum */
77 /* make sure we have room for at least this size: */
78 #define LOGINSIZE 64
79 #define PASSWORDSIZE 64
81 int ParseNetrc(char *host,
82 char *login,
83 char *password)
85 FILE *file;
86 char netrcbuffer[256];
87 int retcode=1;
89 char *home = GetEnv("HOME"); /* portable environment reader */
90 int state=NOTHING;
92 char state_login=0;
93 char state_password=0;
95 #define NETRC DOT_CHAR "netrc"
97 if(!home || (strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))))
98 return -1;
100 sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
102 file = fopen(netrcbuffer, "r");
103 if(file) {
104 char *tok;
105 while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
106 tok=strtok(netrcbuffer, " \t\n");
107 while(tok) {
108 switch(state) {
109 case NOTHING:
110 if(strequal("machine", tok)) {
111 /* the next tok is the machine name, this is in itself the
112 delimiter that starts the stuff entered for this machine,
113 after this we need to search for 'login' and
114 'password'. */
115 state=HOSTFOUND;
117 break;
118 case HOSTFOUND:
119 if(strequal(host, tok)) {
120 /* and yes, this is our host! */
121 state=HOSTVALID;
122 #ifdef _NETRC_DEBUG
123 printf("HOST: %s\n", tok);
124 #endif
125 retcode=0; /* we did find our host */
127 else
128 /* not our host */
129 state=NOTHING;
130 break;
131 case HOSTVALID:
132 /* we are now parsing sub-keywords concerning "our" host */
133 if(state_login) {
134 strncpy(login, tok, LOGINSIZE-1);
135 #ifdef _NETRC_DEBUG
136 printf("LOGIN: %s\n", login);
137 #endif
138 state_login=0;
140 else if(state_password) {
141 strncpy(password, tok, PASSWORDSIZE-1);
142 #if _NETRC_DEBUG
143 printf("PASSWORD: %s\n", password);
144 #endif
145 state_password=0;
147 else if(strequal("login", tok))
148 state_login=1;
149 else if(strequal("password", tok))
150 state_password=1;
151 else if(strequal("machine", tok)) {
152 /* ok, there's machine here go => */
153 state = HOSTFOUND;
155 break;
156 } /* switch (state) */
157 tok = strtok(NULL, " \t\n");
158 } /* while (tok) */
159 } /* while fgets() */
161 fclose(file);
164 return retcode;
167 #ifdef _NETRC_DEBUG
168 int main(int argc, char **argv)
170 char login[64]="";
171 char password[64]="";
173 if(argc<2)
174 return -1;
176 if(0 == ParseNetrc(argv[1], login, password)) {
177 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
178 argv[1], login, password);
182 #endif