]>
Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Support rsync daemon authentication. | |
3 | * | |
4 | * Copyright (C) 1998-2000 Andrew Tridgell | |
ed4b3448 | 5 | * Copyright (C) 2002-2022 Wayne Davison |
0f78b815 WD |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 3 of the License, or | |
10 | * (at your option) any later version. | |
0f78b815 WD |
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 | 17 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 18 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 19 | */ |
18cc8c7e | 20 | |
31593dd6 | 21 | #include "rsync.h" |
5ebe9a46 | 22 | #include "itypes.h" |
11eb67ee | 23 | #include "ifuncs.h" |
31593dd6 | 24 | |
5ebe9a46 | 25 | extern int read_only; |
38cab94d | 26 | extern char *password_file; |
7e2711bb | 27 | extern struct name_num_obj valid_auth_checksums; |
38cab94d | 28 | |
bcb7e502 AT |
29 | /*************************************************************************** |
30 | encode a buffer using base64 - simple and slow algorithm. null terminates | |
31 | the result. | |
32 | ***************************************************************************/ | |
4a19c3b2 | 33 | void base64_encode(const char *buf, int len, char *out, int pad) |
bcb7e502 AT |
34 | { |
35 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
36 | int bit_offset, byte_offset, idx, i; | |
4a19c3b2 | 37 | const uchar *d = (const uchar *)buf; |
bcb7e502 AT |
38 | int bytes = (len*8 + 5)/6; |
39 | ||
57385128 | 40 | for (i = 0; i < bytes; i++) { |
bcb7e502 AT |
41 | byte_offset = (i*6)/8; |
42 | bit_offset = (i*6)%8; | |
43 | if (bit_offset < 3) { | |
44 | idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F; | |
45 | } else { | |
46 | idx = (d[byte_offset] << (bit_offset-2)) & 0x3F; | |
47 | if (byte_offset+1 < len) { | |
48 | idx |= (d[byte_offset+1] >> (8-(bit_offset-2))); | |
49 | } | |
50 | } | |
51 | out[i] = b64[idx]; | |
52 | } | |
293def60 WD |
53 | |
54 | while (pad && (i % 4)) | |
55 | out[i++] = '='; | |
56 | ||
57 | out[i] = '\0'; | |
bcb7e502 AT |
58 | } |
59 | ||
bf011fed | 60 | /* Generate a challenge buffer and return it base64-encoded. */ |
4a19c3b2 | 61 | static void gen_challenge(const char *addr, char *challenge) |
bcb7e502 AT |
62 | { |
63 | char input[32]; | |
a0456b9c | 64 | char digest[MAX_DIGEST_LEN]; |
bcb7e502 | 65 | struct timeval tv; |
a0456b9c | 66 | int len; |
bcb7e502 | 67 | |
58c9b4b7 | 68 | memset(input, 0, sizeof input); |
bcb7e502 | 69 | |
4a19c3b2 | 70 | strlcpy(input, addr, 17); |
3060d4aa | 71 | sys_gettimeofday(&tv); |
bcb7e502 AT |
72 | SIVAL(input, 16, tv.tv_sec); |
73 | SIVAL(input, 20, tv.tv_usec); | |
74 | SIVAL(input, 24, getpid()); | |
75 | ||
7e2711bb | 76 | len = sum_init(valid_auth_checksums.negotiated_nni, 0); |
58c9b4b7 | 77 | sum_update(input, sizeof input); |
7e2711bb | 78 | sum_end(digest); |
bf011fed | 79 | |
a0456b9c | 80 | base64_encode(digest, len, challenge, 0); |
bcb7e502 AT |
81 | } |
82 | ||
5ebe9a46 WD |
83 | /* Generate an MD4 hash created from the combination of the password |
84 | * and the challenge string and return it base64-encoded. */ | |
85 | static void generate_hash(const char *in, const char *challenge, char *out) | |
86 | { | |
87 | char buf[MAX_DIGEST_LEN]; | |
88 | int len; | |
89 | ||
7e2711bb | 90 | len = sum_init(valid_auth_checksums.negotiated_nni, 0); |
5ebe9a46 WD |
91 | sum_update(in, strlen(in)); |
92 | sum_update(challenge, strlen(challenge)); | |
7e2711bb | 93 | sum_end(buf); |
5ebe9a46 WD |
94 | |
95 | base64_encode(buf, len, out, 0); | |
96 | } | |
bcb7e502 | 97 | |
38cab94d WD |
98 | /* Return the secret for a user from the secret file, null terminated. |
99 | * Maximum length is len (not counting the null). */ | |
5ebe9a46 WD |
100 | static const char *check_secret(int module, const char *user, const char *group, |
101 | const char *challenge, const char *pass) | |
bcb7e502 | 102 | { |
5ebe9a46 WD |
103 | char line[1024]; |
104 | char pass2[MAX_DIGEST_LEN*2]; | |
4a19c3b2 | 105 | const char *fname = lp_secrets_file(module); |
d1be2312 | 106 | STRUCT_STAT st; |
0dedfbce | 107 | int ok = 1; |
5ebe9a46 WD |
108 | int user_len = strlen(user); |
109 | int group_len = group ? strlen(group) : 0; | |
110 | char *err; | |
0dedfbce | 111 | FILE *fh; |
bcb7e502 | 112 | |
0dedfbce | 113 | if (!fname || !*fname || (fh = fopen(fname, "r")) == NULL) |
5ebe9a46 | 114 | return "no secrets file"; |
bcb7e502 | 115 | |
0dedfbce | 116 | if (do_fstat(fileno(fh), &st) == -1) { |
5ebe9a46 | 117 | rsyserr(FLOG, errno, "fstat(%s)", fname); |
d1be2312 | 118 | ok = 0; |
3ca8e68f DD |
119 | } else if (lp_strict_modes(module)) { |
120 | if ((st.st_mode & 06) != 0) { | |
30c041f9 | 121 | rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n"); |
3ca8e68f | 122 | ok = 0; |
59cb358f | 123 | } else if (MY_UID() == ROOT_UID && st.st_uid != ROOT_UID) { |
30c041f9 | 124 | rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n"); |
3ca8e68f DD |
125 | ok = 0; |
126 | } | |
d1be2312 DD |
127 | } |
128 | if (!ok) { | |
0dedfbce | 129 | fclose(fh); |
5ebe9a46 | 130 | return "ignoring secrets file"; |
d1be2312 DD |
131 | } |
132 | ||
38cab94d WD |
133 | if (*user == '#') { |
134 | /* Reject attempt to match a comment. */ | |
0dedfbce | 135 | fclose(fh); |
5ebe9a46 | 136 | return "invalid username"; |
38cab94d WD |
137 | } |
138 | ||
5ebe9a46 WD |
139 | /* Try to find a line that starts with the user (or @group) name and a ':'. */ |
140 | err = "secret not found"; | |
0dedfbce WD |
141 | while ((user || group) && fgets(line, sizeof line, fh) != NULL) { |
142 | const char **ptr, *s = strtok(line, "\n\r"); | |
5ebe9a46 | 143 | int len; |
0dedfbce WD |
144 | if (!s) |
145 | continue; | |
146 | if (*s == '@') { | |
5ebe9a46 WD |
147 | ptr = &group; |
148 | len = group_len; | |
0dedfbce | 149 | s++; |
5ebe9a46 WD |
150 | } else { |
151 | ptr = &user; | |
152 | len = user_len; | |
38cab94d | 153 | } |
5ebe9a46 WD |
154 | if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':') |
155 | continue; | |
156 | generate_hash(s+len+1, challenge, pass2); | |
157 | if (strcmp(pass, pass2) == 0) { | |
158 | err = NULL; | |
159 | break; | |
bcb7e502 | 160 | } |
5ebe9a46 WD |
161 | err = "password mismatch"; |
162 | *ptr = NULL; /* Don't look for name again. */ | |
bcb7e502 AT |
163 | } |
164 | ||
0dedfbce | 165 | fclose(fh); |
bcb7e502 | 166 | |
c3761706 WD |
167 | force_memzero(line, sizeof line); |
168 | force_memzero(pass2, sizeof pass2); | |
5ebe9a46 WD |
169 | |
170 | return err; | |
bcb7e502 AT |
171 | } |
172 | ||
4a19c3b2 | 173 | static const char *getpassf(const char *filename) |
65575e96 | 174 | { |
65575e96 | 175 | STRUCT_STAT st; |
38cab94d | 176 | char buffer[512], *p; |
12505e02 | 177 | int n; |
65575e96 | 178 | |
38cab94d WD |
179 | if (!filename) |
180 | return NULL; | |
65575e96 | 181 | |
12505e02 WD |
182 | if (strcmp(filename, "-") == 0) { |
183 | n = fgets(buffer, sizeof buffer, stdin) == NULL ? -1 : (int)strlen(buffer); | |
184 | } else { | |
185 | int fd; | |
18cc8c7e | 186 | |
12505e02 WD |
187 | if ((fd = open(filename,O_RDONLY)) < 0) { |
188 | rsyserr(FERROR, errno, "could not open password file %s", filename); | |
189 | exit_cleanup(RERR_SYNTAX); | |
190 | } | |
191 | ||
192 | if (do_stat(filename, &st) == -1) { | |
193 | rsyserr(FERROR, errno, "stat(%s)", filename); | |
194 | exit_cleanup(RERR_SYNTAX); | |
195 | } | |
196 | if ((st.st_mode & 06) != 0) { | |
197 | rprintf(FERROR, "ERROR: password file must not be other-accessible\n"); | |
198 | exit_cleanup(RERR_SYNTAX); | |
199 | } | |
59cb358f | 200 | if (MY_UID() == ROOT_UID && st.st_uid != ROOT_UID) { |
12505e02 WD |
201 | rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n"); |
202 | exit_cleanup(RERR_SYNTAX); | |
203 | } | |
204 | ||
205 | n = read(fd, buffer, sizeof buffer - 1); | |
206 | close(fd); | |
65575e96 AT |
207 | } |
208 | ||
38cab94d WD |
209 | if (n > 0) { |
210 | buffer[n] = '\0'; | |
211 | if ((p = strtok(buffer, "\n\r")) != NULL) | |
212 | return strdup(p); | |
18cc8c7e | 213 | } |
65575e96 | 214 | |
70c4bfb7 WD |
215 | rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename); |
216 | exit_cleanup(RERR_SYNTAX); | |
65575e96 AT |
217 | } |
218 | ||
18cc8c7e WD |
219 | /* Possibly negotiate authentication with the client. Use "leader" to |
220 | * start off the auth if necessary. | |
221 | * | |
222 | * Return NULL if authentication failed. Return "" if anonymous access. | |
223 | * Otherwise return username. | |
224 | */ | |
4a19c3b2 WD |
225 | char *auth_server(int f_in, int f_out, int module, const char *host, |
226 | const char *addr, const char *leader) | |
bcb7e502 AT |
227 | { |
228 | char *users = lp_auth_users(module); | |
a0456b9c | 229 | char challenge[MAX_DIGEST_LEN*2]; |
d999d312 | 230 | char line[BIGPATHBUFLEN]; |
d6f0342a | 231 | const char **auth_uid_groups = NULL; |
5ebe9a46 WD |
232 | int auth_uid_groups_cnt = -1; |
233 | const char *err = NULL; | |
234 | int group_match = -1; | |
5037cf3a | 235 | char *tok, *pass; |
5ebe9a46 | 236 | char opt_ch = '\0'; |
bcb7e502 AT |
237 | |
238 | /* if no auth list then allow anyone in! */ | |
58c9b4b7 WD |
239 | if (!users || !*users) |
240 | return ""; | |
bcb7e502 | 241 | |
7e2711bb | 242 | negotiate_daemon_auth(f_out, 0); |
bcb7e502 | 243 | gen_challenge(addr, challenge); |
18cc8c7e | 244 | |
bf011fed | 245 | io_printf(f_out, "%s%s\n", leader, challenge); |
bcb7e502 | 246 | |
5ebe9a46 | 247 | if (!read_line_old(f_in, line, sizeof line, 0) |
5037cf3a WD |
248 | || (pass = strchr(line, ' ')) == NULL) { |
249 | rprintf(FLOG, "auth failed on module %s from %s (%s): " | |
250 | "invalid challenge response\n", | |
251 | lp_name(module), host, addr); | |
d0d56395 | 252 | return NULL; |
5037cf3a WD |
253 | } |
254 | *pass++ = '\0'; | |
18cc8c7e | 255 | |
11eb67ee | 256 | users = strdup(users); |
c8e78d87 | 257 | |
5037cf3a | 258 | for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) { |
5ebe9a46 WD |
259 | char *opts; |
260 | /* See if the user appended :deny, :ro, or :rw. */ | |
261 | if ((opts = strchr(tok, ':')) != NULL) { | |
262 | *opts++ = '\0'; | |
263 | opt_ch = isUpper(opts) ? toLower(opts) : *opts; | |
264 | if (opt_ch == 'r') { /* handle ro and rw */ | |
265 | opt_ch = isUpper(opts+1) ? toLower(opts+1) : opts[1]; | |
266 | if (opt_ch == 'o') | |
267 | opt_ch = 'r'; | |
268 | else if (opt_ch != 'w') | |
269 | opt_ch = '\0'; | |
270 | } else if (opt_ch != 'd') /* if it's not deny, ignore it */ | |
271 | opt_ch = '\0'; | |
272 | } else | |
273 | opt_ch = '\0'; | |
274 | if (*tok != '@') { | |
275 | /* Match the username */ | |
276 | if (wildmatch(tok, line)) | |
277 | break; | |
278 | } else { | |
279 | #ifdef HAVE_GETGROUPLIST | |
280 | int j; | |
281 | /* See if authorizing user is a real user, and if so, see | |
282 | * if it is in a group that matches tok+1 wildmat. */ | |
283 | if (auth_uid_groups_cnt < 0) { | |
2a7355fb | 284 | item_list gid_list = EMPTY_ITEM_LIST; |
5ebe9a46 | 285 | uid_t auth_uid; |
5ebe9a46 | 286 | if (!user_to_uid(line, &auth_uid, False) |
2a7355fb | 287 | || getallgroups(auth_uid, &gid_list) != NULL) |
5ebe9a46 WD |
288 | auth_uid_groups_cnt = 0; |
289 | else { | |
2a7355fb WD |
290 | gid_t *gid_array = gid_list.items; |
291 | auth_uid_groups_cnt = gid_list.count; | |
d6f0342a | 292 | auth_uid_groups = new_array(const char *, auth_uid_groups_cnt); |
5ebe9a46 | 293 | for (j = 0; j < auth_uid_groups_cnt; j++) |
2a7355fb | 294 | auth_uid_groups[j] = gid_to_group(gid_array[j]); |
5ebe9a46 WD |
295 | } |
296 | } | |
297 | for (j = 0; j < auth_uid_groups_cnt; j++) { | |
298 | if (auth_uid_groups[j] && wildmatch(tok+1, auth_uid_groups[j])) { | |
299 | group_match = j; | |
300 | break; | |
301 | } | |
302 | } | |
303 | if (group_match >= 0) | |
304 | break; | |
305 | #else | |
306 | rprintf(FLOG, "your computer doesn't support getgrouplist(), so no @group authorization is possible.\n"); | |
307 | #endif | |
308 | } | |
c8e78d87 | 309 | } |
5ebe9a46 | 310 | |
c8e78d87 AT |
311 | free(users); |
312 | ||
5ebe9a46 WD |
313 | if (!tok) |
314 | err = "no matching rule"; | |
315 | else if (opt_ch == 'd') | |
316 | err = "denied by rule"; | |
317 | else { | |
d6f0342a | 318 | const char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL; |
5ebe9a46 | 319 | err = check_secret(module, line, group, challenge, pass); |
5037cf3a | 320 | } |
18cc8c7e | 321 | |
c3761706 WD |
322 | force_memzero(challenge, sizeof challenge); |
323 | force_memzero(pass, strlen(pass)); | |
bcb7e502 | 324 | |
5ebe9a46 WD |
325 | if (auth_uid_groups) { |
326 | int j; | |
327 | for (j = 0; j < auth_uid_groups_cnt; j++) { | |
328 | if (auth_uid_groups[j]) | |
d6f0342a | 329 | free((char*)auth_uid_groups[j]); |
5ebe9a46 WD |
330 | } |
331 | free(auth_uid_groups); | |
332 | } | |
18cc8c7e | 333 | |
5ebe9a46 WD |
334 | if (err) { |
335 | rprintf(FLOG, "auth failed on module %s from %s (%s) for %s: %s\n", | |
336 | lp_name(module), host, addr, line, err); | |
5037cf3a WD |
337 | return NULL; |
338 | } | |
d0d56395 | 339 | |
5ebe9a46 WD |
340 | if (opt_ch == 'r') |
341 | read_only = 1; | |
342 | else if (opt_ch == 'w') | |
343 | read_only = 0; | |
344 | ||
5037cf3a | 345 | return strdup(line); |
bcb7e502 AT |
346 | } |
347 | ||
4a19c3b2 | 348 | void auth_client(int fd, const char *user, const char *challenge) |
bcb7e502 | 349 | { |
4a19c3b2 | 350 | const char *pass; |
a0456b9c | 351 | char pass2[MAX_DIGEST_LEN*2]; |
bcb7e502 | 352 | |
ef383c0d | 353 | if (!user || !*user) |
4b2f6a7c | 354 | user = "nobody"; |
7e2711bb | 355 | negotiate_daemon_auth(-1, 1); |
bcb7e502 | 356 | |
58c9b4b7 WD |
357 | if (!(pass = getpassf(password_file)) |
358 | && !(pass = getenv("RSYNC_PASSWORD"))) { | |
64bd7568 | 359 | /* XXX: cyeoh says that getpass is deprecated, because |
908f5a9f MP |
360 | * it may return a truncated password on some systems, |
361 | * and it is not in the LSB. | |
8475e0e4 WD |
362 | * |
363 | * Andrew Klein says that getpassphrase() is present | |
364 | * on Solaris and reads up to 256 characters. | |
365 | * | |
366 | * OpenBSD has a readpassphrase() that might be more suitable. | |
367 | */ | |
bcb7e502 AT |
368 | pass = getpass("Password: "); |
369 | } | |
370 | ||
38cab94d | 371 | if (!pass) |
bcb7e502 | 372 | pass = ""; |
bcb7e502 AT |
373 | |
374 | generate_hash(pass, challenge, pass2); | |
bcb7e502 AT |
375 | io_printf(fd, "%s %s\n", user, pass2); |
376 | } |