Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Support rsync daemon authentication. | |
3 | * | |
4 | * Copyright (C) 1998-2000 Andrew Tridgell | |
5 | * Copyright (C) 2002, 2004, 2005, 2006 Wayne Davison | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
e7c67065 WD |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 20 | */ |
18cc8c7e | 21 | |
31593dd6 AT |
22 | #include "rsync.h" |
23 | ||
38cab94d | 24 | extern char *password_file; |
38cab94d | 25 | |
bcb7e502 AT |
26 | /*************************************************************************** |
27 | encode a buffer using base64 - simple and slow algorithm. null terminates | |
28 | the result. | |
29 | ***************************************************************************/ | |
293def60 | 30 | void base64_encode(char *buf, int len, char *out, int pad) |
bcb7e502 AT |
31 | { |
32 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
33 | int bit_offset, byte_offset, idx, i; | |
34 | unsigned char *d = (unsigned char *)buf; | |
bcb7e502 AT |
35 | int bytes = (len*8 + 5)/6; |
36 | ||
57385128 | 37 | for (i = 0; i < bytes; i++) { |
bcb7e502 AT |
38 | byte_offset = (i*6)/8; |
39 | bit_offset = (i*6)%8; | |
40 | if (bit_offset < 3) { | |
41 | idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F; | |
42 | } else { | |
43 | idx = (d[byte_offset] << (bit_offset-2)) & 0x3F; | |
44 | if (byte_offset+1 < len) { | |
45 | idx |= (d[byte_offset+1] >> (8-(bit_offset-2))); | |
46 | } | |
47 | } | |
48 | out[i] = b64[idx]; | |
49 | } | |
293def60 WD |
50 | |
51 | while (pad && (i % 4)) | |
52 | out[i++] = '='; | |
53 | ||
54 | out[i] = '\0'; | |
bcb7e502 AT |
55 | } |
56 | ||
bf011fed | 57 | /* Generate a challenge buffer and return it base64-encoded. */ |
bcb7e502 AT |
58 | static void gen_challenge(char *addr, char *challenge) |
59 | { | |
60 | char input[32]; | |
bf011fed | 61 | char md4_out[MD4_SUM_LENGTH]; |
bcb7e502 AT |
62 | struct timeval tv; |
63 | ||
58c9b4b7 | 64 | memset(input, 0, sizeof input); |
bcb7e502 | 65 | |
37f9805d | 66 | strlcpy((char *)input, addr, 17); |
3060d4aa | 67 | sys_gettimeofday(&tv); |
bcb7e502 AT |
68 | SIVAL(input, 16, tv.tv_sec); |
69 | SIVAL(input, 20, tv.tv_usec); | |
70 | SIVAL(input, 24, getpid()); | |
71 | ||
ba582f75 | 72 | sum_init(0); |
58c9b4b7 | 73 | sum_update(input, sizeof input); |
bf011fed WD |
74 | sum_end(md4_out); |
75 | ||
293def60 | 76 | base64_encode(md4_out, MD4_SUM_LENGTH, challenge, 0); |
bcb7e502 AT |
77 | } |
78 | ||
79 | ||
38cab94d WD |
80 | /* Return the secret for a user from the secret file, null terminated. |
81 | * Maximum length is len (not counting the null). */ | |
bcb7e502 AT |
82 | static int get_secret(int module, char *user, char *secret, int len) |
83 | { | |
84 | char *fname = lp_secrets_file(module); | |
d1be2312 | 85 | STRUCT_STAT st; |
38cab94d WD |
86 | int fd, ok = 1; |
87 | char ch, *p; | |
bcb7e502 | 88 | |
38cab94d WD |
89 | if (!fname || !*fname) |
90 | return 0; | |
bcb7e502 | 91 | |
38cab94d WD |
92 | if ((fd = open(fname, O_RDONLY)) < 0) |
93 | return 0; | |
bcb7e502 | 94 | |
d1be2312 | 95 | if (do_stat(fname, &st) == -1) { |
45c49b52 | 96 | rsyserr(FLOG, errno, "stat(%s)", fname); |
d1be2312 | 97 | ok = 0; |
3ca8e68f DD |
98 | } else if (lp_strict_modes(module)) { |
99 | if ((st.st_mode & 06) != 0) { | |
30c041f9 | 100 | rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n"); |
3ca8e68f | 101 | ok = 0; |
351f5e2f | 102 | } else if (MY_UID() == 0 && st.st_uid != 0) { |
30c041f9 | 103 | rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n"); |
3ca8e68f DD |
104 | ok = 0; |
105 | } | |
d1be2312 DD |
106 | } |
107 | if (!ok) { | |
30c041f9 | 108 | rprintf(FLOG, "continuing without secrets file\n"); |
d1be2312 DD |
109 | close(fd); |
110 | return 0; | |
111 | } | |
112 | ||
38cab94d WD |
113 | if (*user == '#') { |
114 | /* Reject attempt to match a comment. */ | |
115 | close(fd); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | /* Try to find a line that starts with the user name and a ':'. */ | |
120 | p = user; | |
121 | while (1) { | |
122 | if (read(fd, &ch, 1) != 1) { | |
123 | close(fd); | |
124 | return 0; | |
125 | } | |
126 | if (ch == '\n') | |
127 | p = user; | |
128 | else if (p) { | |
129 | if (*p == ch) | |
130 | p++; | |
131 | else if (!*p && ch == ':') | |
132 | break; | |
133 | else | |
134 | p = NULL; | |
bcb7e502 | 135 | } |
bcb7e502 AT |
136 | } |
137 | ||
38cab94d WD |
138 | /* Slurp the secret into the "secret" buffer. */ |
139 | p = secret; | |
140 | while (len > 0) { | |
141 | if (read(fd, p, 1) != 1 || *p == '\n') | |
142 | break; | |
143 | if (*p == '\r') | |
144 | continue; | |
145 | p++; | |
146 | len--; | |
147 | } | |
148 | *p = '\0'; | |
bcb7e502 | 149 | close(fd); |
bcb7e502 | 150 | |
bcb7e502 AT |
151 | return 1; |
152 | } | |
153 | ||
65575e96 AT |
154 | static char *getpassf(char *filename) |
155 | { | |
65575e96 | 156 | STRUCT_STAT st; |
38cab94d WD |
157 | char buffer[512], *p; |
158 | int fd, n, ok = 1; | |
159 | char *envpw = getenv("RSYNC_PASSWORD"); | |
65575e96 | 160 | |
38cab94d WD |
161 | if (!filename) |
162 | return NULL; | |
65575e96 | 163 | |
38cab94d | 164 | if ((fd = open(filename,O_RDONLY)) < 0) { |
4875d6b6 | 165 | rsyserr(FERROR, errno, "could not open password file \"%s\"", |
45c49b52 | 166 | filename); |
38cab94d | 167 | if (envpw) |
18cc8c7e | 168 | rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n"); |
65575e96 AT |
169 | return NULL; |
170 | } | |
18cc8c7e | 171 | |
65575e96 | 172 | if (do_stat(filename, &st) == -1) { |
45c49b52 | 173 | rsyserr(FERROR, errno, "stat(%s)", filename); |
65575e96 AT |
174 | ok = 0; |
175 | } else if ((st.st_mode & 06) != 0) { | |
176 | rprintf(FERROR,"password file must not be other-accessible\n"); | |
177 | ok = 0; | |
351f5e2f | 178 | } else if (MY_UID() == 0 && st.st_uid != 0) { |
65575e96 AT |
179 | rprintf(FERROR,"password file must be owned by root when running as root\n"); |
180 | ok = 0; | |
181 | } | |
182 | if (!ok) { | |
183 | rprintf(FERROR,"continuing without password file\n"); | |
38cab94d WD |
184 | if (envpw) |
185 | rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n"); | |
65575e96 AT |
186 | close(fd); |
187 | return NULL; | |
188 | } | |
189 | ||
38cab94d WD |
190 | if (envpw) |
191 | rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n"); | |
65575e96 | 192 | |
38cab94d WD |
193 | n = read(fd, buffer, sizeof buffer - 1); |
194 | close(fd); | |
195 | if (n > 0) { | |
196 | buffer[n] = '\0'; | |
197 | if ((p = strtok(buffer, "\n\r")) != NULL) | |
198 | return strdup(p); | |
18cc8c7e | 199 | } |
65575e96 AT |
200 | |
201 | return NULL; | |
202 | } | |
203 | ||
bf011fed WD |
204 | /* Generate an MD4 hash created from the combination of the password |
205 | * and the challenge string and return it base64-encoded. */ | |
6e4fb64e | 206 | static void generate_hash(char *in, char *challenge, char *out) |
bcb7e502 | 207 | { |
5037cf3a | 208 | char buf[MD4_SUM_LENGTH]; |
bcb7e502 | 209 | |
ba582f75 | 210 | sum_init(0); |
bcb7e502 AT |
211 | sum_update(in, strlen(in)); |
212 | sum_update(challenge, strlen(challenge)); | |
213 | sum_end(buf); | |
214 | ||
293def60 | 215 | base64_encode(buf, MD4_SUM_LENGTH, out, 0); |
bcb7e502 AT |
216 | } |
217 | ||
18cc8c7e WD |
218 | /* Possibly negotiate authentication with the client. Use "leader" to |
219 | * start off the auth if necessary. | |
220 | * | |
221 | * Return NULL if authentication failed. Return "" if anonymous access. | |
222 | * Otherwise return username. | |
223 | */ | |
5037cf3a WD |
224 | char *auth_server(int f_in, int f_out, int module, char *host, char *addr, |
225 | char *leader) | |
bcb7e502 AT |
226 | { |
227 | char *users = lp_auth_users(module); | |
bf011fed | 228 | char challenge[MD4_SUM_LENGTH*2]; |
d999d312 | 229 | char line[BIGPATHBUFLEN]; |
5037cf3a WD |
230 | char secret[512]; |
231 | char pass2[MD4_SUM_LENGTH*2]; | |
232 | char *tok, *pass; | |
bcb7e502 AT |
233 | |
234 | /* if no auth list then allow anyone in! */ | |
58c9b4b7 WD |
235 | if (!users || !*users) |
236 | return ""; | |
bcb7e502 AT |
237 | |
238 | gen_challenge(addr, challenge); | |
18cc8c7e | 239 | |
bf011fed | 240 | io_printf(f_out, "%s%s\n", leader, challenge); |
bcb7e502 | 241 | |
5037cf3a WD |
242 | if (!read_line(f_in, line, sizeof line - 1) |
243 | || (pass = strchr(line, ' ')) == NULL) { | |
244 | rprintf(FLOG, "auth failed on module %s from %s (%s): " | |
245 | "invalid challenge response\n", | |
246 | lp_name(module), host, addr); | |
d0d56395 | 247 | return NULL; |
5037cf3a WD |
248 | } |
249 | *pass++ = '\0'; | |
18cc8c7e | 250 | |
5037cf3a WD |
251 | if (!(users = strdup(users))) |
252 | out_of_memory("auth_server"); | |
c8e78d87 | 253 | |
5037cf3a WD |
254 | for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) { |
255 | if (wildmatch(tok, line)) | |
58c9b4b7 | 256 | break; |
c8e78d87 AT |
257 | } |
258 | free(users); | |
259 | ||
5037cf3a WD |
260 | if (!tok) { |
261 | rprintf(FLOG, "auth failed on module %s from %s (%s): " | |
262 | "unauthorized user\n", | |
263 | lp_name(module), host, addr); | |
d0d56395 | 264 | return NULL; |
5037cf3a | 265 | } |
18cc8c7e | 266 | |
58c9b4b7 | 267 | memset(secret, 0, sizeof secret); |
5037cf3a | 268 | if (!get_secret(module, line, secret, sizeof secret - 1)) { |
58c9b4b7 | 269 | memset(secret, 0, sizeof secret); |
5037cf3a WD |
270 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
271 | "missing secret for user \"%s\"\n", | |
272 | lp_name(module), host, addr, line); | |
d0d56395 | 273 | return NULL; |
bcb7e502 AT |
274 | } |
275 | ||
bf011fed | 276 | generate_hash(secret, challenge, pass2); |
58c9b4b7 | 277 | memset(secret, 0, sizeof secret); |
18cc8c7e | 278 | |
5037cf3a WD |
279 | if (strcmp(pass, pass2) != 0) { |
280 | rprintf(FLOG, "auth failed on module %s from %s (%s): " | |
281 | "password mismatch\n", | |
282 | lp_name(module), host, addr); | |
283 | return NULL; | |
284 | } | |
d0d56395 | 285 | |
5037cf3a | 286 | return strdup(line); |
bcb7e502 AT |
287 | } |
288 | ||
289 | ||
290 | void auth_client(int fd, char *user, char *challenge) | |
291 | { | |
292 | char *pass; | |
5037cf3a | 293 | char pass2[MD4_SUM_LENGTH*2]; |
bcb7e502 | 294 | |
ef383c0d | 295 | if (!user || !*user) |
4b2f6a7c | 296 | user = "nobody"; |
bcb7e502 | 297 | |
58c9b4b7 WD |
298 | if (!(pass = getpassf(password_file)) |
299 | && !(pass = getenv("RSYNC_PASSWORD"))) { | |
64bd7568 | 300 | /* XXX: cyeoh says that getpass is deprecated, because |
908f5a9f MP |
301 | * it may return a truncated password on some systems, |
302 | * and it is not in the LSB. | |
303 | * | |
304 | * Andrew Klein says that getpassphrase() is present | |
305 | * on Solaris and reads up to 256 characters. | |
306 | * | |
307 | * OpenBSD has a readpassphrase() that might be more suitable. | |
308 | */ | |
bcb7e502 AT |
309 | pass = getpass("Password: "); |
310 | } | |
311 | ||
38cab94d | 312 | if (!pass) |
bcb7e502 | 313 | pass = ""; |
bcb7e502 AT |
314 | |
315 | generate_hash(pass, challenge, pass2); | |
bcb7e502 AT |
316 | io_printf(fd, "%s %s\n", user, pass2); |
317 | } |