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 2 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, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 extern char *password_file;
25 /***************************************************************************
26 encode a buffer using base64 - simple and slow algorithm. null terminates
28 ***************************************************************************/
29 void base64_encode(const char *buf, int len, char *out, int pad)
31 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 int bit_offset, byte_offset, idx, i;
33 const uchar *d = (const uchar *)buf;
34 int bytes = (len*8 + 5)/6;
36 for (i = 0; i < bytes; i++) {
37 byte_offset = (i*6)/8;
40 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
42 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
43 if (byte_offset+1 < len) {
44 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
50 while (pad && (i % 4))
56 /* Generate a challenge buffer and return it base64-encoded. */
57 static void gen_challenge(const char *addr, char *challenge)
60 char digest[MAX_DIGEST_LEN];
64 memset(input, 0, sizeof input);
66 strlcpy(input, addr, 17);
67 sys_gettimeofday(&tv);
68 SIVAL(input, 16, tv.tv_sec);
69 SIVAL(input, 20, tv.tv_usec);
70 SIVAL(input, 24, getpid());
73 sum_update(input, sizeof input);
74 len = sum_end(digest);
76 base64_encode(digest, len, challenge, 0);
80 /* Return the secret for a user from the secret file, null terminated.
81 * Maximum length is len (not counting the null). */
82 static int get_secret(int module, const char *user, char *secret, int len)
84 const char *fname = lp_secrets_file(module);
90 if (!fname || !*fname)
93 if ((fd = open(fname, O_RDONLY)) < 0)
96 if (do_stat(fname, &st) == -1) {
97 rsyserr(FLOG, errno, "stat(%s)", fname);
99 } else if (lp_strict_modes(module)) {
100 if ((st.st_mode & 06) != 0) {
101 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
103 } else if (MY_UID() == 0 && st.st_uid != 0) {
104 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
109 rprintf(FLOG, "continuing without secrets file\n");
115 /* Reject attempt to match a comment. */
120 /* Try to find a line that starts with the user name and a ':'. */
123 if (read(fd, &ch, 1) != 1) {
132 else if (!*p && ch == ':')
139 /* Slurp the secret into the "secret" buffer. */
142 if (read(fd, s, 1) != 1 || *s == '\n')
155 static const char *getpassf(const char *filename)
158 char buffer[512], *p;
160 const char *envpw = getenv("RSYNC_PASSWORD");
165 if ((fd = open(filename,O_RDONLY)) < 0) {
166 rsyserr(FERROR, errno, "could not open password file \"%s\"",
169 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
173 if (do_stat(filename, &st) == -1) {
174 rsyserr(FERROR, errno, "stat(%s)", filename);
176 } else if ((st.st_mode & 06) != 0) {
177 rprintf(FERROR,"password file must not be other-accessible\n");
179 } else if (MY_UID() == 0 && st.st_uid != 0) {
180 rprintf(FERROR,"password file must be owned by root when running as root\n");
184 rprintf(FERROR,"continuing without password file\n");
186 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
192 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
194 n = read(fd, buffer, sizeof buffer - 1);
198 if ((p = strtok(buffer, "\n\r")) != NULL)
205 /* Generate an MD4 hash created from the combination of the password
206 * and the challenge string and return it base64-encoded. */
207 static void generate_hash(const char *in, const char *challenge, char *out)
209 char buf[MAX_DIGEST_LEN];
213 sum_update(in, strlen(in));
214 sum_update(challenge, strlen(challenge));
217 base64_encode(buf, len, out, 0);
220 /* Possibly negotiate authentication with the client. Use "leader" to
221 * start off the auth if necessary.
223 * Return NULL if authentication failed. Return "" if anonymous access.
224 * Otherwise return username.
226 char *auth_server(int f_in, int f_out, int module, const char *host,
227 const char *addr, const char *leader)
229 char *users = lp_auth_users(module);
230 char challenge[MAX_DIGEST_LEN*2];
231 char line[BIGPATHBUFLEN];
233 char pass2[MAX_DIGEST_LEN*2];
236 /* if no auth list then allow anyone in! */
237 if (!users || !*users)
240 gen_challenge(addr, challenge);
242 io_printf(f_out, "%s%s\n", leader, challenge);
244 if (!read_line(f_in, line, sizeof line - 1)
245 || (pass = strchr(line, ' ')) == NULL) {
246 rprintf(FLOG, "auth failed on module %s from %s (%s): "
247 "invalid challenge response\n",
248 lp_name(module), host, addr);
253 if (!(users = strdup(users)))
254 out_of_memory("auth_server");
256 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
257 if (wildmatch(tok, line))
263 rprintf(FLOG, "auth failed on module %s from %s (%s): "
264 "unauthorized user\n",
265 lp_name(module), host, addr);
269 memset(secret, 0, sizeof secret);
270 if (!get_secret(module, line, secret, sizeof secret - 1)) {
271 memset(secret, 0, sizeof secret);
272 rprintf(FLOG, "auth failed on module %s from %s (%s): "
273 "missing secret for user \"%s\"\n",
274 lp_name(module), host, addr, line);
278 generate_hash(secret, challenge, pass2);
279 memset(secret, 0, sizeof secret);
281 if (strcmp(pass, pass2) != 0) {
282 rprintf(FLOG, "auth failed on module %s from %s (%s): "
283 "password mismatch\n",
284 lp_name(module), host, addr);
291 void auth_client(int fd, const char *user, const char *challenge)
294 char pass2[MAX_DIGEST_LEN*2];
299 if (!(pass = getpassf(password_file))
300 && !(pass = getenv("RSYNC_PASSWORD"))) {
301 /* XXX: cyeoh says that getpass is deprecated, because
302 * it may return a truncated password on some systems,
303 * and it is not in the LSB.
305 * Andrew Klein says that getpassphrase() is present
306 * on Solaris and reads up to 256 characters.
308 * OpenBSD has a readpassphrase() that might be more suitable.
310 pass = getpass("Password: ");
316 generate_hash(pass, challenge, pass2);
317 io_printf(fd, "%s %s\n", user, pass2);