]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/netrc.cc
merged from lp:~donkult/apt/experimental
[apt.git] / apt-pkg / contrib / netrc.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $
4 /* ######################################################################
5
6 netrc file parser - returns the login and password of a give host in
7 a specified netrc-type file
8
9 Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and
10 placed into the Public Domain, do with it what you will.
11
12 ##################################################################### */
13 /*}}}*/
14 #include <config.h>
15
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/fileutl.h>
18 #include <iostream>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <pwd.h>
24
25 #include "netrc.h"
26
27
28 /* Get user and password from .netrc when given a machine name */
29
30 enum {
31 NOTHING,
32 HOSTFOUND, /* the 'machine' keyword was found */
33 HOSTCOMPLETE, /* the machine name following the keyword was found too */
34 HOSTVALID, /* this is "our" machine! */
35 HOSTEND /* LAST enum */
36 };
37
38 /* make sure we have room for at least this size: */
39 #define LOGINSIZE 64
40 #define PASSWORDSIZE 64
41 #define NETRC DOT_CHAR "netrc"
42
43 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
44 int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
45 {
46 FILE *file;
47 int retcode = 1;
48 int specific_login = (login[0] != 0);
49 char *home = NULL;
50 bool netrc_alloc = false;
51
52 int state_our_login = false; /* With specific_login,
53 found *our* login name */
54
55 if (!netrcfile) {
56 home = getenv ("HOME"); /* portable environment reader */
57
58 if (!home) {
59 struct passwd *pw;
60 pw = getpwuid (geteuid ());
61 if(pw)
62 home = pw->pw_dir;
63 }
64
65 if (!home)
66 return -1;
67
68 asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC);
69 if(!netrcfile)
70 return -1;
71 else
72 netrc_alloc = true;
73 }
74
75 file = fopen (netrcfile, "r");
76 if(file) {
77 char *tok;
78 char *tok_buf;
79 bool done = false;
80 char netrcbuffer[256];
81
82 int state = NOTHING;
83 char state_login = 0; /* Found a login keyword */
84 char state_password = 0; /* Found a password keyword */
85
86 while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) {
87 tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
88 while (!done && tok) {
89 if(login[0] && password[0]) {
90 done = true;
91 break;
92 }
93
94 switch(state) {
95 case NOTHING:
96 if (!strcasecmp ("machine", tok)) {
97 /* the next tok is the machine name, this is in itself the
98 delimiter that starts the stuff entered for this machine,
99 after this we need to search for 'login' and
100 'password'. */
101 state = HOSTFOUND;
102 }
103 break;
104 case HOSTFOUND:
105 /* extended definition of a "machine" if we have a "/"
106 we match the start of the string (host.startswith(token) */
107 if ((strchr(host, '/') && strstr(host, tok) == host) ||
108 (!strcasecmp (host, tok))) {
109 /* and yes, this is our host! */
110 state = HOSTVALID;
111 retcode = 0; /* we did find our host */
112 }
113 else
114 /* not our host */
115 state = NOTHING;
116 break;
117 case HOSTVALID:
118 /* we are now parsing sub-keywords concerning "our" host */
119 if (state_login) {
120 if (specific_login)
121 state_our_login = !strcasecmp (login, tok);
122 else
123 strncpy (login, tok, LOGINSIZE - 1);
124 state_login = 0;
125 } else if (state_password) {
126 if (state_our_login || !specific_login)
127 strncpy (password, tok, PASSWORDSIZE - 1);
128 state_password = 0;
129 } else if (!strcasecmp ("login", tok))
130 state_login = 1;
131 else if (!strcasecmp ("password", tok))
132 state_password = 1;
133 else if(!strcasecmp ("machine", tok)) {
134 /* ok, there's machine here go => */
135 state = HOSTFOUND;
136 state_our_login = false;
137 }
138 break;
139 } /* switch (state) */
140
141 tok = strtok_r (NULL, " \t\n", &tok_buf);
142 } /* while(tok) */
143 } /* while fgets() */
144
145 fclose(file);
146 }
147
148 if (netrc_alloc)
149 free(netrcfile);
150
151 return retcode;
152 }
153
154 void maybe_add_auth (URI &Uri, string NetRCFile)
155 {
156 if (_config->FindB("Debug::Acquire::netrc", false) == true)
157 std::clog << "maybe_add_auth: " << (string)Uri
158 << " " << NetRCFile << std::endl;
159 if (Uri.Password.empty () == true || Uri.User.empty () == true)
160 {
161 if (NetRCFile.empty () == false)
162 {
163 char login[64] = "";
164 char password[64] = "";
165 char *netrcfile = strdup(NetRCFile.c_str());
166
167 // first check for a generic host based netrc entry
168 char *host = strdup(Uri.Host.c_str());
169 if (host && parsenetrc (host, login, password, netrcfile) == 0)
170 {
171 if (_config->FindB("Debug::Acquire::netrc", false) == true)
172 std::clog << "host: " << host
173 << " user: " << login
174 << " pass-size: " << strlen(password)
175 << std::endl;
176 Uri.User = string (login);
177 Uri.Password = string (password);
178 free(netrcfile);
179 free(host);
180 return;
181 }
182 free(host);
183
184 // if host did not work, try Host+Path next, this will trigger
185 // a lookup uri.startswith(host) in the netrc file parser (because
186 // of the "/"
187 char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str());
188 if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0)
189 {
190 if (_config->FindB("Debug::Acquire::netrc", false) == true)
191 std::clog << "hostpath: " << hostpath
192 << " user: " << login
193 << " pass-size: " << strlen(password)
194 << std::endl;
195 Uri.User = string (login);
196 Uri.Password = string (password);
197 }
198 free(netrcfile);
199 free(hostpath);
200 }
201 }
202 }
203
204 #ifdef DEBUG
205 int main(int argc, char* argv[])
206 {
207 char login[64] = "";
208 char password[64] = "";
209
210 if(argc < 2)
211 return -1;
212
213 if(0 == parsenetrc (argv[1], login, password, argv[2])) {
214 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
215 }
216 }
217 #endif