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