00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 #ifdef HAVE_UNISTD_H
00031 #include <unistd.h>
00032 #endif
00033 #ifdef HAVE_PWD_H
00034 #include <pwd.h>
00035 #endif
00036 #ifdef VMS
00037 #include <unixlib.h>
00038 #endif
00039
00040 #include <curl/curl.h>
00041 #include "netrc.h"
00042
00043 #include "strequal.h"
00044 #include "strtok.h"
00045 #include "memory.h"
00046
00047 #define _MPRINTF_REPLACE
00048 #include <curl/mprintf.h>
00049
00050
00051 #include "memdebug.h"
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 enum {
00062 NOTHING,
00063 HOSTFOUND,
00064 HOSTCOMPLETE,
00065 HOSTVALID,
00066
00067 HOSTEND
00068 };
00069
00070
00071 #define LOGINSIZE 64
00072 #define PASSWORDSIZE 64
00073
00074
00075 int Curl_parsenetrc(char *host,
00076 char *login,
00077 char *password,
00078 char *netrcfile)
00079 {
00080 FILE *file;
00081 int retcode=1;
00082 int specific_login = (login[0] != 0);
00083 char *home = NULL;
00084 bool home_alloc = FALSE;
00085 bool netrc_alloc = FALSE;
00086 int state=NOTHING;
00087
00088 char state_login=0;
00089 char state_password=0;
00090 int state_our_login=FALSE;
00091
00092 #define NETRC DOT_CHAR "netrc"
00093
00094 #ifdef CURLDEBUG
00095 {
00096
00097
00098
00099
00100 char *override = curl_getenv("CURL_DEBUG_NETRC");
00101
00102 if (override) {
00103 fprintf(stderr, "NETRC: overridden " NETRC " file: %s\n", override);
00104 netrcfile = override;
00105 netrc_alloc = TRUE;
00106 }
00107 }
00108 #endif
00109 if(!netrcfile) {
00110 home = curl_getenv("HOME");
00111 if(home) {
00112 home_alloc = TRUE;
00113 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
00114 }
00115 else {
00116 struct passwd *pw;
00117 pw= getpwuid(geteuid());
00118 if (pw) {
00119 #ifdef VMS
00120 home = decc$translate_vms(pw->pw_dir);
00121 #else
00122 home = pw->pw_dir;
00123 #endif
00124 }
00125 #endif
00126 }
00127
00128 if(!home)
00129 return -1;
00130
00131 netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
00132 if(!netrcfile) {
00133 if(home_alloc)
00134 free(home);
00135 return -1;
00136 }
00137 netrc_alloc = TRUE;
00138 }
00139
00140 file = fopen(netrcfile, "r");
00141 if(file) {
00142 char *tok;
00143 char *tok_buf;
00144 bool done=FALSE;
00145 char netrcbuffer[256];
00146
00147 while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
00148 tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
00149 while(!done && tok) {
00150
00151 if (login[0] && password[0]) {
00152 done=TRUE;
00153 break;
00154 }
00155
00156 switch(state) {
00157 case NOTHING:
00158 if(strequal("machine", tok)) {
00159
00160
00161
00162
00163 state=HOSTFOUND;
00164 }
00165 break;
00166 case HOSTFOUND:
00167 if(strequal(host, tok)) {
00168
00169 state=HOSTVALID;
00170 #ifdef _NETRC_DEBUG
00171 fprintf(stderr, "HOST: %s\n", tok);
00172 #endif
00173 retcode=0;
00174 }
00175 else
00176
00177 state=NOTHING;
00178 break;
00179 case HOSTVALID:
00180
00181 if(state_login) {
00182 if (specific_login) {
00183 state_our_login = strequal(login, tok);
00184 }
00185 else {
00186 strncpy(login, tok, LOGINSIZE-1);
00187 #ifdef _NETRC_DEBUG
00188 fprintf(stderr, "LOGIN: %s\n", login);
00189 #endif
00190 }
00191 state_login=0;
00192 }
00193 else if(state_password) {
00194 if (state_our_login || !specific_login) {
00195 strncpy(password, tok, PASSWORDSIZE-1);
00196 #ifdef _NETRC_DEBUG
00197 fprintf(stderr, "PASSWORD: %s\n", password);
00198 #endif
00199 }
00200 state_password=0;
00201 }
00202 else if(strequal("login", tok))
00203 state_login=1;
00204 else if(strequal("password", tok))
00205 state_password=1;
00206 else if(strequal("machine", tok)) {
00207
00208 state = HOSTFOUND;
00209 state_our_login = FALSE;
00210 }
00211 break;
00212 }
00213
00214 tok = strtok_r(NULL, " \t\n", &tok_buf);
00215 }
00216 }
00217
00218 fclose(file);
00219 }
00220
00221 if(home_alloc)
00222 free(home);
00223 if(netrc_alloc)
00224 free(netrcfile);
00225
00226 return retcode;
00227 }
00228
00229 #ifdef _NETRC_DEBUG
00230 int main(int argc, argv_item_t argv[])
00231 {
00232 char login[64]="";
00233 char password[64]="";
00234
00235 if(argc<2)
00236 return -1;
00237
00238 if(0 == ParseNetrc(argv[1], login, password)) {
00239 printf("HOST: %s LOGIN: %s PASSWORD: %s\n",
00240 argv[1], login, password);
00241 }
00242 }
00243
00244 #endif