~ubuntu-branches/ubuntu/dapper/curl/dapper-updates

« back to all changes in this revision

Viewing changes to lib/netrc.c

  • Committer: Bazaar Package Importer
  • Author(s): Domenico Andreoli
  • Date: 2004-06-04 19:09:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040604190925-wy048bp31320r2z6
Tags: 7.12.0.is.7.11.2-1
* Reverted to version 7.11.2 (closes: #252348).
* Disabled support for libidn (closes: #252367). This is to leave
  curl in unstable as much similar as possible to the one in testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
 
1
/***************************************************************************
2
2
 *                                  _   _ ____  _     
3
3
 *  Project                     ___| | | |  _ \| |    
4
4
 *                             / __| | | | |_) | |    
5
5
 *                            | (__| |_| |  _ <| |___ 
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
9
 
 *
10
 
 * In order to be useful for every potential user, curl and libcurl are
11
 
 * dual-licensed under the MPL and the MIT/X-derivate licenses.
12
 
 *
 
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
 * 
13
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
14
15
 * copies of the Software, and permit persons to whom the Software is
15
 
 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
16
 
 * licenses. You may pick one of these licenses.
 
16
 * furnished to do so, under the terms of the COPYING file.
17
17
 *
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: netrc.c,v 1.18 2002/01/18 13:04:48 bagder Exp $
22
 
 *****************************************************************************/
 
21
 * $Id: netrc.c,v 1.31 2004/03/23 15:30:12 bagder Exp $
 
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
25
25
 
41
41
#endif
42
42
 
43
43
#include <curl/curl.h>
 
44
#include "netrc.h"
44
45
 
45
46
#include "strequal.h"
46
47
#include "strtok.h"
47
48
 
 
49
#define _MPRINTF_REPLACE /* use our functions only */
 
50
#include <curl/mprintf.h>
 
51
 
48
52
/* The last #include file should be: */
49
 
#ifdef MALLOCDEBUG
 
53
#ifdef CURLDEBUG
50
54
#include "memdebug.h"
51
55
#endif
52
56
 
71
75
#define LOGINSIZE 64
72
76
#define PASSWORDSIZE 64
73
77
 
 
78
/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
74
79
int Curl_parsenetrc(char *host,
75
80
                    char *login,
76
 
                    char *password)
 
81
                    char *password,
 
82
                    char *netrcfile)
77
83
{
78
84
  FILE *file;
79
 
  char netrcbuffer[256];
80
85
  int retcode=1;
81
 
  
 
86
  int specific_login = (login[0] != 0);
82
87
  char *home = NULL; 
 
88
  bool home_alloc = FALSE;
 
89
  bool netrc_alloc = FALSE;
83
90
  int state=NOTHING;
84
91
 
85
 
  char state_login=0;
86
 
  char state_password=0;
 
92
  char state_login=0;      /* Found a login keyword */
 
93
  char state_password=0;   /* Found a password keyword */
 
94
  int state_our_login=FALSE;  /* With specific_login, found *our* login name */
87
95
 
88
96
#define NETRC DOT_CHAR "netrc"
89
97
 
 
98
#ifdef CURLDEBUG
 
99
  {
 
100
    /* This is a hack to allow testing.
 
101
     * If compiled with --enable-debug and CURL_DEBUG_NETRC is defined,
 
102
     * then it's the path to a substitute .netrc for testing purposes *only* */
 
103
 
 
104
    char *override = curl_getenv("CURL_DEBUG_NETRC");
 
105
 
 
106
    if (override) {
 
107
      printf("NETRC: overridden " NETRC " file: %s\n", home);
 
108
      netrcfile = override;
 
109
      netrc_alloc = TRUE;
 
110
    }
 
111
  }
 
112
#endif /* CURLDEBUG */
 
113
  if(!netrcfile) {
 
114
    home = curl_getenv("HOME"); /* portable environment reader */
 
115
    if(home) {
 
116
      home_alloc = TRUE;
90
117
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
91
 
  struct passwd *pw;
92
 
  pw= getpwuid(geteuid());
93
 
  if (pw) {
 
118
    }
 
119
    else {
 
120
      struct passwd *pw;
 
121
      pw= getpwuid(geteuid());
 
122
      if (pw) {
94
123
#ifdef  VMS
95
 
    home = decc$translate_vms(pw->pw_dir);
96
 
#else
97
 
    home = pw->pw_dir;
98
 
#endif
99
 
  }
100
 
#else
101
 
  void *pw=NULL;
102
 
#endif
103
 
  
104
 
  if(NULL == pw) {
105
 
    home = curl_getenv("HOME"); /* portable environment reader */
106
 
    if(!home) {
107
 
      return -1;
108
 
    }
109
 
  }
110
 
 
111
 
  if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
112
 
    if(NULL==pw)
113
 
      free(home);
114
 
    return -1;
115
 
  }
116
 
 
117
 
  sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
118
 
 
119
 
  file = fopen(netrcbuffer, "r");
 
124
        home = decc$translate_vms(pw->pw_dir);
 
125
#else
 
126
        home = pw->pw_dir;
 
127
#endif
 
128
      }
 
129
#endif
 
130
    }
 
131
 
 
132
    if(!home)
 
133
      return -1;
 
134
 
 
135
    netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
 
136
    if(!netrcfile) {
 
137
      if(home_alloc)
 
138
        free(home);
 
139
      return -1;
 
140
    }
 
141
    netrc_alloc = TRUE;
 
142
  }
 
143
 
 
144
  file = fopen(netrcfile, "r");
120
145
  if(file) {
121
146
    char *tok;
122
 
        char *tok_buf;
123
 
    while(fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
 
147
    char *tok_buf;
 
148
    bool done=FALSE;
 
149
    char netrcbuffer[256];
 
150
 
 
151
    while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
124
152
      tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
125
 
      while(tok) {
 
153
      while(!done && tok) {
 
154
 
 
155
        if (login[0] && password[0]) {
 
156
          done=TRUE;
 
157
          break;
 
158
        }
 
159
 
126
160
        switch(state) {
127
161
        case NOTHING:
128
162
          if(strequal("machine", tok)) {
149
183
        case HOSTVALID:
150
184
          /* we are now parsing sub-keywords concerning "our" host */
151
185
          if(state_login) {
152
 
            strncpy(login, tok, LOGINSIZE-1);
 
186
            if (specific_login) {
 
187
              state_our_login = strequal(login, tok);
 
188
            }
 
189
            else {
 
190
              strncpy(login, tok, LOGINSIZE-1);
153
191
#ifdef _NETRC_DEBUG
154
 
            printf("LOGIN: %s\n", login);
 
192
              printf("LOGIN: %s\n", login);
155
193
#endif
 
194
            }
156
195
            state_login=0;
157
196
          }
158
197
          else if(state_password) {
159
 
            strncpy(password, tok, PASSWORDSIZE-1);
 
198
            if (state_our_login || !specific_login) {
 
199
              strncpy(password, tok, PASSWORDSIZE-1);
160
200
#ifdef _NETRC_DEBUG
161
 
            printf("PASSWORD: %s\n", password);
 
201
              printf("PASSWORD: %s\n", password);
162
202
#endif
 
203
            }
163
204
            state_password=0;
164
205
          }
165
206
          else if(strequal("login", tok))
169
210
          else if(strequal("machine", tok)) {
170
211
            /* ok, there's machine here go => */
171
212
            state = HOSTFOUND;
 
213
            state_our_login = FALSE;
172
214
          }
173
215
          break;
174
216
        } /* switch (state) */
 
217
 
175
218
        tok = strtok_r(NULL, " \t\n", &tok_buf);
176
219
      } /* while (tok) */
177
220
    } /* while fgets() */
179
222
    fclose(file);
180
223
  }
181
224
 
182
 
  if(NULL==pw)
 
225
  if(home_alloc)
183
226
    free(home);
 
227
  if(netrc_alloc)
 
228
    free(netrcfile);
184
229
 
185
230
  return retcode;
186
231
}
201
246
}
202
247
 
203
248
#endif
204
 
 
205
 
/*
206
 
 * local variables:
207
 
 * eval: (load-file "../curl-mode.el")
208
 
 * end:
209
 
 * vim600: fdm=marker
210
 
 * vim: et sw=2 ts=2 sts=2 tw=78
211
 
 */