port netrc support from maemon
[ntk/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
15 #include <iostream>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <pwd.h>
21
22 #include "netrc.h"
23
24
25 /* Get user and password from .netrc when given a machine name */
26
27 enum {
28 NOTHING,
29 HOSTFOUND, /* the 'machine' keyword was found */
30 HOSTCOMPLETE, /* the machine name following the keyword was found too */
31 HOSTVALID, /* this is "our" machine! */
32 HOSTEND /* LAST enum */
33 };
34
35 /* make sure we have room for at least this size: */
36 #define LOGINSIZE 64
37 #define PASSWORDSIZE 64
38 #define NETRC DOT_CHAR "netrc"
39
40 /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
41 int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL)
42 {
43 FILE *file;
44 int retcode = 1;
45 int specific_login = (login[0] != 0);
46 char *home = NULL;
47 bool netrc_alloc = false;
48 int state = NOTHING;
49
50 char state_login = 0; /* Found a login keyword */
51 char state_password = 0; /* Found a password keyword */
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 while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) {
83 tok = strtok_r (netrcbuffer, " \t\n", &tok_buf);
84 while (!done && tok) {
85 if(login[0] && password[0]) {
86 done = true;
87 break;
88 }
89
90 switch(state) {
91 case NOTHING:
92 if (!strcasecmp ("machine", tok)) {
93 /* the next tok is the machine name, this is in itself the
94 delimiter that starts the stuff entered for this machine,
95 after this we need to search for 'login' and
96 'password'. */
97 state = HOSTFOUND;
98 }
99 break;
100 case HOSTFOUND:
101 if (!strcasecmp (host, tok)) {
102 /* and yes, this is our host! */
103 state = HOSTVALID;
104 retcode = 0; /* we did find our host */
105 }
106 else
107 /* not our host */
108 state = NOTHING;
109 break;
110 case HOSTVALID:
111 /* we are now parsing sub-keywords concerning "our" host */
112 if (state_login) {
113 if (specific_login)
114 state_our_login = !strcasecmp (login, tok);
115 else
116 strncpy (login, tok, LOGINSIZE - 1);
117 state_login = 0;
118 } else if (state_password) {
119 if (state_our_login || !specific_login)
120 strncpy (password, tok, PASSWORDSIZE - 1);
121 state_password = 0;
122 } else if (!strcasecmp ("login", tok))
123 state_login = 1;
124 else if (!strcasecmp ("password", tok))
125 state_password = 1;
126 else if(!strcasecmp ("machine", tok)) {
127 /* ok, there's machine here go => */
128 state = HOSTFOUND;
129 state_our_login = false;
130 }
131 break;
132 } /* switch (state) */
133
134 tok = strtok_r (NULL, " \t\n", &tok_buf);
135 } /* while(tok) */
136 } /* while fgets() */
137
138 fclose(file);
139 }
140
141 if (netrc_alloc)
142 free(netrcfile);
143
144 return retcode;
145 }
146
147 void maybe_add_auth (URI &Uri, string NetRCFile)
148 {
149 if (Uri.Password.empty () == true && Uri.User.empty () == true)
150 {
151 if (NetRCFile.empty () == false)
152 {
153 char login[64] = "";
154 char password[64] = "";
155 char *netrcfile = strdup (NetRCFile.c_str ());
156 char *host = strdup (Uri.Host.c_str ());
157
158 if (host && 0 == parsenetrc (host, login, password, netrcfile))
159 {
160 Uri.User = string (login);
161 Uri.Password = string (password);
162 }
163
164 if (host)
165 free (host);
166 free (netrcfile);
167 }
168 }
169 }
170
171 #ifdef DEBUG
172 int main(int argc, char* argv[])
173 {
174 char login[64] = "";
175 char password[64] = "";
176
177 if(argc < 2)
178 return -1;
179
180 if(0 == parsenetrc (argv[1], login, password, argv[2])) {
181 printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password);
182 }
183 }
184 #endif