1 /* $Id$ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include <ctype.h>
23 #include <string.h>
24
25 #include "fdm.h"
26
27 int netrc_token(FILE *, char **);
28
29 FILE *
netrc_open(const char * home,char ** cause)30 netrc_open(const char *home, char **cause)
31 {
32 char path[MAXPATHLEN];
33 struct stat sb;
34 FILE *f;
35
36 if (ppath(path, sizeof path, "%s/%s", home, ".netrc") != 0) {
37 xasprintf(cause, "%s", strerror(errno));
38 return (NULL);
39 }
40
41 if (stat(path, &sb) != 0) {
42 xasprintf(cause, "%s: %s", path, strerror(errno));
43 return (NULL);
44 }
45 if ((sb.st_mode & (S_IROTH|S_IWOTH)) != 0) {
46 xasprintf(cause, "%s: world readable or writable", path);
47 return (NULL);
48 }
49
50 if ((f = fopen(path, "r")) == NULL) {
51 xasprintf(cause, "%s: %s", path, strerror(errno));
52 return (NULL);
53 }
54
55 return (f);
56 }
57
58 void
netrc_close(FILE * f)59 netrc_close(FILE *f)
60 {
61 fclose(f);
62 }
63
64 int
netrc_lookup(FILE * f,const char * host,char ** user,char ** pass)65 netrc_lookup(FILE *f, const char *host, char **user, char **pass)
66 {
67 char *token;
68 int found;
69
70 if (user != NULL)
71 *user = NULL;
72 if (pass != NULL)
73 *pass = NULL;
74
75 found = 0;
76 for (;;) {
77 switch (netrc_token(f, &token)) {
78 case 1:
79 return (0);
80 case -1:
81 return (-1);
82 }
83
84 if (!found) {
85 if (strcmp(token, "machine") == 0) {
86 if (netrc_token(f, &token) != 0)
87 return (-1);
88 if (strcmp(token, host) == 0)
89 found = 1;
90 } else if (strcmp(token, "default") == 0)
91 found = 1;
92 } else {
93 if (strcmp(token, "machine") == 0)
94 break;
95 if (strcmp(token, "default") == 0)
96 break;
97
98 if (user != NULL && strcmp(token, "login") == 0) {
99 if (netrc_token(f, &token) != 0)
100 return (-1);
101 if (*token == '\0')
102 return (-1);
103 *user = xstrdup(token);
104 }
105 if (pass != NULL && strcmp(token, "password") == 0) {
106 if (netrc_token(f, &token) != 0)
107 return (-1);
108 if (*token == '\0')
109 return (-1);
110 *pass = xstrdup(token);
111 }
112 }
113 }
114
115 return (0);
116 }
117
118 /*
119 * Function below modified from token() in OpenBSD's usr.bin/ftp/ruserpass.c.
120 *
121 * $OpenBSD: ruserpass.c,v 1.20 2006/05/16 23:43:16 ray Exp $
122 * $NetBSD: ruserpass.c,v 1.14 1997/07/20 09:46:01 lukem Exp $
123 *
124 * Copyright (c) 1985, 1993, 1994
125 * The Regents of the University of California. All rights reserved.
126 *
127 * Redistribution and use in source and binary forms, with or without
128 * modification, are permitted provided that the following conditions
129 * are met:
130 * 1. Redistributions of source code must retain the above copyright
131 * notice, this list of conditions and the following disclaimer.
132 * 2. Redistributions in binary form must reproduce the above copyright
133 * notice, this list of conditions and the following disclaimer in the
134 * documentation and/or other materials provided with the distribution.
135 * 3. Neither the name of the University nor the names of its contributors
136 * may be used to endorse or promote products derived from this software
137 * without specific prior written permission.
138 *
139 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
140 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
141 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
142 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
143 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
144 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
145 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
146 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
147 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
148 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
149 * SUCH DAMAGE.
150 */
151
152 int
netrc_token(FILE * f,char ** cpp)153 netrc_token(FILE *f, char **cpp)
154 {
155 static char token[BUFSIZ];
156 char *cp;
157 int c;
158
159 if (feof(f) || ferror(f))
160 return (1);
161
162 while ((c = fgetc(f)) != EOF && (isspace(c) || c == ','))
163 ;
164 if (c == EOF)
165 return (1);
166
167 cp = token;
168 if (c == '"') {
169 while ((c = fgetc(f)) != EOF && c != '"') {
170 if (c == '\\' && (c = fgetc(f)) == EOF)
171 break;
172 *cp++ = c;
173 if (cp == token + (sizeof token))
174 return (-1);
175 }
176 if (c == EOF) /* missing " */
177 return (-1);
178 } else {
179 *cp++ = c;
180 while ((c = fgetc(f)) != EOF && !isspace(c) && c != ',') {
181 if (c == '\\' && (c = fgetc(f)) == EOF)
182 break;
183 *cp++ = c;
184 if (cp == token + (sizeof token))
185 return (-1);
186 }
187 }
188 *cp = '\0';
189
190 *cpp = token;
191 return (0);
192 }
193