2 * Support rsync daemon authentication.
4 * Copyright (C) 1998-2000 Andrew Tridgell
5 * Copyright (C) 2002-2007 Wayne Davison
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, visit the http://fsf.org website.
22 extern char *password_file;
24 /***************************************************************************
25 encode a buffer using base64 - simple and slow algorithm. null terminates
27 ***************************************************************************/
28 void base64_encode(const char *buf, int len, char *out, int pad)
30 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
31 int bit_offset, byte_offset, idx, i;
32 const uchar *d = (const uchar *)buf;
33 int bytes = (len*8 + 5)/6;
35 for (i = 0; i < bytes; i++) {
36 byte_offset = (i*6)/8;
39 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
41 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
42 if (byte_offset+1 < len) {
43 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
49 while (pad && (i % 4))
55 /* Generate a challenge buffer and return it base64-encoded. */
56 static void gen_challenge(const char *addr, char *challenge)
59 char digest[MAX_DIGEST_LEN];
63 memset(input, 0, sizeof input);
65 strlcpy(input, addr, 17);
66 sys_gettimeofday(&tv);
67 SIVAL(input, 16, tv.tv_sec);
68 SIVAL(input, 20, tv.tv_usec);
69 SIVAL(input, 24, getpid());
72 sum_update(input, sizeof input);
73 len = sum_end(digest);
75 base64_encode(digest, len, challenge, 0);
79 /* Return the secret for a user from the secret file, null terminated.
80 * Maximum length is len (not counting the null). */
81 static int get_secret(int module, const char *user, char *secret, int len)
83 const char *fname = lp_secrets_file(module);
89 if (!fname || !*fname)
92 if ((fd = open(fname, O_RDONLY)) < 0)
95 if (do_stat(fname, &st) == -1) {
96 rsyserr(FLOG, errno, "stat(%s)", fname);
98 } else if (lp_strict_modes(module)) {
99 if ((st.st_mode & 06) != 0) {
100 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
102 } else if (MY_UID() == 0 && st.st_uid != 0) {
103 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
108 rprintf(FLOG, "continuing without secrets file\n");
114 /* Reject attempt to match a comment. */
119 /* Try to find a line that starts with the user name and a ':'. */
122 if (read(fd, &ch, 1) != 1) {
131 else if (!*p && ch == ':')
138 /* Slurp the secret into the "secret" buffer. */
141 if (read(fd, s, 1) != 1 || *s == '\n')
154 static const char *getpassf(const char *filename)
157 char buffer[512], *p;
159 const char *envpw = getenv("RSYNC_PASSWORD");
164 if ((fd = open(filename,O_RDONLY)) < 0) {
165 rsyserr(FERROR, errno, "could not open password file \"%s\"",
168 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
172 if (do_stat(filename, &st) == -1) {
173 rsyserr(FERROR, errno, "stat(%s)", filename);
175 } else if ((st.st_mode & 06) != 0) {
176 rprintf(FERROR,"password file must not be other-accessible\n");
178 } else if (MY_UID() == 0 && st.st_uid != 0) {
179 rprintf(FERROR,"password file must be owned by root when running as root\n");
183 rprintf(FERROR,"continuing without password file\n");
185 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
191 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
193 n = read(fd, buffer, sizeof buffer - 1);
197 if ((p = strtok(buffer, "\n\r")) != NULL)
204 /* Generate an MD4 hash created from the combination of the password
205 * and the challenge string and return it base64-encoded. */
206 static void generate_hash(const char *in, const char *challenge, char *out)
208 char buf[MAX_DIGEST_LEN];
212 sum_update(in, strlen(in));
213 sum_update(challenge, strlen(challenge));
216 base64_encode(buf, len, out, 0);
219 /* Possibly negotiate authentication with the client. Use "leader" to
220 * start off the auth if necessary.
222 * Return NULL if authentication failed. Return "" if anonymous access.
223 * Otherwise return username.
225 char *auth_server(int f_in, int f_out, int module, const char *host,
226 const char *addr, const char *leader)
228 char *users = lp_auth_users(module);
229 char challenge[MAX_DIGEST_LEN*2];
230 char line[BIGPATHBUFLEN];
232 char pass2[MAX_DIGEST_LEN*2];
235 /* if no auth list then allow anyone in! */
236 if (!users || !*users)
239 gen_challenge(addr, challenge);
241 io_printf(f_out, "%s%s\n", leader, challenge);
243 if (!read_line(f_in, line, sizeof line - 1)
244 || (pass = strchr(line, ' ')) == NULL) {
245 rprintf(FLOG, "auth failed on module %s from %s (%s): "
246 "invalid challenge response\n",
247 lp_name(module), host, addr);
252 if (!(users = strdup(users)))
253 out_of_memory("auth_server");
255 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
256 if (wildmatch(tok, line))
262 rprintf(FLOG, "auth failed on module %s from %s (%s): "
263 "unauthorized user\n",
264 lp_name(module), host, addr);
268 memset(secret, 0, sizeof secret);
269 if (!get_secret(module, line, secret, sizeof secret - 1)) {
270 memset(secret, 0, sizeof secret);
271 rprintf(FLOG, "auth failed on module %s from %s (%s): "
272 "missing secret for user \"%s\"\n",
273 lp_name(module), host, addr, line);
277 generate_hash(secret, challenge, pass2);
278 memset(secret, 0, sizeof secret);
280 if (strcmp(pass, pass2) != 0) {
281 rprintf(FLOG, "auth failed on module %s from %s (%s): "
282 "password mismatch\n",
283 lp_name(module), host, addr);
290 void auth_client(int fd, const char *user, const char *challenge)
293 char pass2[MAX_DIGEST_LEN*2];
298 if (!(pass = getpassf(password_file))
299 && !(pass = getenv("RSYNC_PASSWORD"))) {
300 /* XXX: cyeoh says that getpass is deprecated, because
301 * it may return a truncated password on some systems,
302 * and it is not in the LSB.
304 * Andrew Klein says that getpassphrase() is present
305 * on Solaris and reads up to 256 characters.
307 * OpenBSD has a readpassphrase() that might be more suitable.
309 pass = getpass("Password: ");
315 generate_hash(pass, challenge, pass2);
316 io_printf(fd, "%s %s\n", user, pass2);