1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2000 by Andrew Tridgell
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* support rsync authentication */
23 extern char *password_file;
25 /***************************************************************************
26 encode a buffer using base64 - simple and slow algorithm. null terminates
28 ***************************************************************************/
29 void base64_encode(char *buf, int len, char *out, int pad)
31 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 int bit_offset, byte_offset, idx, i;
33 unsigned char *d = (unsigned char *)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(char *addr, char *challenge)
60 char md4_out[MD4_SUM_LENGTH];
63 memset(input, 0, sizeof input);
65 strlcpy((char *)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);
75 base64_encode(md4_out, MD4_SUM_LENGTH, 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, char *user, char *secret, int len)
83 char *fname = lp_secrets_file(module);
88 if (!fname || !*fname)
91 if ((fd = open(fname, O_RDONLY)) < 0)
94 if (do_stat(fname, &st) == -1) {
95 rsyserr(FLOG, errno, "stat(%s)", fname);
97 } else if (lp_strict_modes(module)) {
98 if ((st.st_mode & 06) != 0) {
99 rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
101 } else if (MY_UID() == 0 && st.st_uid != 0) {
102 rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
107 rprintf(FLOG, "continuing without secrets file\n");
113 /* Reject attempt to match a comment. */
118 /* Try to find a line that starts with the user name and a ':'. */
121 if (read(fd, &ch, 1) != 1) {
130 else if (!*p && ch == ':')
137 /* Slurp the secret into the "secret" buffer. */
140 if (read(fd, p, 1) != 1 || *p == '\n')
153 static char *getpassf(char *filename)
156 char buffer[512], *p;
158 char *envpw = getenv("RSYNC_PASSWORD");
163 if ((fd = open(filename,O_RDONLY)) < 0) {
164 rsyserr(FERROR, errno, "could not open password file \"%s\"",
167 rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
171 if (do_stat(filename, &st) == -1) {
172 rsyserr(FERROR, errno, "stat(%s)", filename);
174 } else if ((st.st_mode & 06) != 0) {
175 rprintf(FERROR,"password file must not be other-accessible\n");
177 } else if (MY_UID() == 0 && st.st_uid != 0) {
178 rprintf(FERROR,"password file must be owned by root when running as root\n");
182 rprintf(FERROR,"continuing without password file\n");
184 rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
190 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
192 n = read(fd, buffer, sizeof buffer - 1);
196 if ((p = strtok(buffer, "\n\r")) != NULL)
203 /* Generate an MD4 hash created from the combination of the password
204 * and the challenge string and return it base64-encoded. */
205 static void generate_hash(char *in, char *challenge, char *out)
207 char buf[MD4_SUM_LENGTH];
210 sum_update(in, strlen(in));
211 sum_update(challenge, strlen(challenge));
214 base64_encode(buf, MD4_SUM_LENGTH, out, 0);
217 /* Possibly negotiate authentication with the client. Use "leader" to
218 * start off the auth if necessary.
220 * Return NULL if authentication failed. Return "" if anonymous access.
221 * Otherwise return username.
223 char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
226 char *users = lp_auth_users(module);
227 char challenge[MD4_SUM_LENGTH*2];
228 char line[BIGPATHBUFLEN];
230 char pass2[MD4_SUM_LENGTH*2];
233 /* if no auth list then allow anyone in! */
234 if (!users || !*users)
237 gen_challenge(addr, challenge);
239 io_printf(f_out, "%s%s\n", leader, challenge);
241 if (!read_line(f_in, line, sizeof line - 1)
242 || (pass = strchr(line, ' ')) == NULL) {
243 rprintf(FLOG, "auth failed on module %s from %s (%s): "
244 "invalid challenge response\n",
245 lp_name(module), host, addr);
250 if (!(users = strdup(users)))
251 out_of_memory("auth_server");
253 for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
254 if (wildmatch(tok, line))
260 rprintf(FLOG, "auth failed on module %s from %s (%s): "
261 "unauthorized user\n",
262 lp_name(module), host, addr);
266 memset(secret, 0, sizeof secret);
267 if (!get_secret(module, line, secret, sizeof secret - 1)) {
268 memset(secret, 0, sizeof secret);
269 rprintf(FLOG, "auth failed on module %s from %s (%s): "
270 "missing secret for user \"%s\"\n",
271 lp_name(module), host, addr, line);
275 generate_hash(secret, challenge, pass2);
276 memset(secret, 0, sizeof secret);
278 if (strcmp(pass, pass2) != 0) {
279 rprintf(FLOG, "auth failed on module %s from %s (%s): "
280 "password mismatch\n",
281 lp_name(module), host, addr);
289 void auth_client(int fd, char *user, char *challenge)
292 char pass2[MD4_SUM_LENGTH*2];
297 if (!(pass = getpassf(password_file))
298 && !(pass = getenv("RSYNC_PASSWORD"))) {
299 /* XXX: cyeoh says that getpass is deprecated, because
300 * it may return a truncated password on some systems,
301 * and it is not in the LSB.
303 * Andrew Klein says that getpassphrase() is present
304 * on Solaris and reads up to 256 characters.
306 * OpenBSD has a readpassphrase() that might be more suitable.
308 pass = getpass("Password: ");
314 generate_hash(pass, challenge, pass2);
315 io_printf(fd, "%s %s\n", user, pass2);