~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/CTest/Curl/netrc.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-06-18 16:34:11 UTC
  • mfrom: (1.4.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060618163411-pi234s3v6jwlcmof
Tags: 2.4.2-1
* New upstream release (Closes: #338324)
* Put cmake .vim files into /usr/share/vim/addons/plugin/
  where they can be used. (Closes: #366663)
* Install cmake-mode.el so it can be used. (Closes: #366664)
* Ensure cmake FindKDE locates KDE libraries on Debian
  based distributions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: netrc.c,v 1.7 2004/10/05 16:42:38 hoffman Exp $
 
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
#include "netrc.h"
 
45
 
 
46
#include "strequal.h"
 
47
#include "strtok.h"
 
48
#include "curl_memory.h"
 
49
 
 
50
#define _MPRINTF_REPLACE /* use our functions only */
 
51
#include <curl/mprintf.h>
 
52
 
 
53
/* The last #include file should be: */
 
54
#include "memdebug.h"
 
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
  int state_our_login=FALSE;  /* 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
    home = curl_getenv("HOME"); /* portable environment reader */
 
114
    if(home) {
 
115
      home_alloc = TRUE;
 
116
#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
 
117
    }
 
118
    else {
 
119
      struct passwd *pw;
 
120
      pw= getpwuid(geteuid());
 
121
      if (pw) {
 
122
#ifdef  VMS
 
123
        home = decc$translate_vms(pw->pw_dir);
 
124
#else
 
125
        home = pw->pw_dir;
 
126
#endif
 
127
      }
 
128
#endif
 
129
    }
 
130
 
 
131
    if(!home)
 
132
      return -1;
 
133
 
 
134
    netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
 
135
    if(!netrcfile) {
 
136
      if(home_alloc)
 
137
        free(home);
 
138
      return -1;
 
139
    }
 
140
    netrc_alloc = TRUE;
 
141
  }
 
142
 
 
143
  file = fopen(netrcfile, "r");
 
144
  if(file) {
 
145
    char *tok;
 
146
    char *tok_buf;
 
147
    bool done=FALSE;
 
148
    char netrcbuffer[256];
 
149
 
 
150
    while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
 
151
      tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
 
152
      while(!done && tok) {
 
153
 
 
154
        if (login[0] && password[0]) {
 
155
          done=TRUE;
 
156
          break;
 
157
        }
 
158
 
 
159
        switch(state) {
 
160
        case NOTHING:
 
161
          if(strequal("machine", tok)) {
 
162
            /* the next tok is the machine name, this is in itself the
 
163
               delimiter that starts the stuff entered for this machine,
 
164
               after this we need to search for 'login' and
 
165
               'password'. */
 
166
            state=HOSTFOUND;
 
167
          }
 
168
          break;
 
169
        case HOSTFOUND:
 
170
          if(strequal(host, tok)) {
 
171
            /* and yes, this is our host! */
 
172
            state=HOSTVALID;
 
173
#ifdef _NETRC_DEBUG
 
174
            printf("HOST: %s\n", tok);
 
175
#endif
 
176
            retcode=0; /* we did find our host */
 
177
          }
 
178
          else
 
179
            /* not our host */
 
180
            state=NOTHING;
 
181
          break;
 
182
        case HOSTVALID:
 
183
          /* we are now parsing sub-keywords concerning "our" host */
 
184
          if(state_login) {
 
185
            if (specific_login) {
 
186
              state_our_login = strequal(login, tok);
 
187
            }
 
188
            else {
 
189
              strncpy(login, tok, LOGINSIZE-1);
 
190
#ifdef _NETRC_DEBUG
 
191
              printf("LOGIN: %s\n", login);
 
192
#endif
 
193
            }
 
194
            state_login=0;
 
195
          }
 
196
          else if(state_password) {
 
197
            if (state_our_login || !specific_login) {
 
198
              strncpy(password, tok, PASSWORDSIZE-1);
 
199
#ifdef _NETRC_DEBUG
 
200
              printf("PASSWORD: %s\n", password);
 
201
#endif
 
202
            }
 
203
            state_password=0;
 
204
          }
 
205
          else if(strequal("login", tok))
 
206
            state_login=1;
 
207
          else if(strequal("password", tok))
 
208
            state_password=1;
 
209
          else if(strequal("machine", tok)) {
 
210
            /* ok, there's machine here go => */
 
211
            state = HOSTFOUND;
 
212
            state_our_login = FALSE;
 
213
          }
 
214
          break;
 
215
        } /* switch (state) */
 
216
 
 
217
        tok = strtok_r(NULL, " \t\n", &tok_buf);
 
218
      } /* while (tok) */
 
219
    } /* while fgets() */
 
220
 
 
221
    fclose(file);
 
222
  }
 
223
 
 
224
  if(home_alloc)
 
225
    free(home);
 
226
  if(netrc_alloc)
 
227
    free(netrcfile);
 
228
 
 
229
  return retcode;
 
230
}
 
231
 
 
232
#ifdef _NETRC_DEBUG
 
233
int main(int argc, char **argv)
 
234
{
 
235
  char login[64]="";
 
236
  char password[64]="";
 
237
 
 
238
  if(argc<2)
 
239
    return -1;
 
240
 
 
241
  if(0 == ParseNetrc(argv[1], login, password)) {
 
242
    printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
 
243
           argv[1], login, password);
 
244
  }
 
245
}
 
246
 
 
247
#endif