The ndx arg passed to increment_active_files() is now the unique,
[rsync/rsync.git] / authenticate.c
1 /*
2  * Support rsync daemon authentication.
3  *
4  * Copyright (C) 1998-2000 Andrew Tridgell
5  * Copyright (C) 2002, 2004, 2005, 2006 Wayne Davison
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
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  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21
22 #include "rsync.h"
23
24 extern char *password_file;
25
26 /***************************************************************************
27 encode a buffer using base64 - simple and slow algorithm. null terminates
28 the result.
29   ***************************************************************************/
30 void base64_encode(const char *buf, int len, char *out, int pad)
31 {
32         char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33         int bit_offset, byte_offset, idx, i;
34         const uchar *d = (const uchar *)buf;
35         int bytes = (len*8 + 5)/6;
36
37         for (i = 0; i < bytes; i++) {
38                 byte_offset = (i*6)/8;
39                 bit_offset = (i*6)%8;
40                 if (bit_offset < 3) {
41                         idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
42                 } else {
43                         idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
44                         if (byte_offset+1 < len) {
45                                 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
46                         }
47                 }
48                 out[i] = b64[idx];
49         }
50
51         while (pad && (i % 4))
52                 out[i++] = '=';
53
54         out[i] = '\0';
55 }
56
57 /* Generate a challenge buffer and return it base64-encoded. */
58 static void gen_challenge(const char *addr, char *challenge)
59 {
60         char input[32];
61         char md4_out[MD4_SUM_LENGTH];
62         struct timeval tv;
63
64         memset(input, 0, sizeof input);
65
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());
71
72         sum_init(0);
73         sum_update(input, sizeof input);
74         sum_end(md4_out);
75
76         base64_encode(md4_out, MD4_SUM_LENGTH, challenge, 0);
77 }
78
79
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)
83 {
84         const char *fname = lp_secrets_file(module);
85         STRUCT_STAT st;
86         int fd, ok = 1;
87         const char *p;
88         char ch, *s;
89
90         if (!fname || !*fname)
91                 return 0;
92
93         if ((fd = open(fname, O_RDONLY)) < 0)
94                 return 0;
95
96         if (do_stat(fname, &st) == -1) {
97                 rsyserr(FLOG, errno, "stat(%s)", fname);
98                 ok = 0;
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");
102                         ok = 0;
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");
105                         ok = 0;
106                 }
107         }
108         if (!ok) {
109                 rprintf(FLOG, "continuing without secrets file\n");
110                 close(fd);
111                 return 0;
112         }
113
114         if (*user == '#') {
115                 /* Reject attempt to match a comment. */
116                 close(fd);
117                 return 0;
118         }
119
120         /* Try to find a line that starts with the user name and a ':'. */
121         p = user;
122         while (1) {
123                 if (read(fd, &ch, 1) != 1) {
124                         close(fd);
125                         return 0;
126                 }
127                 if (ch == '\n')
128                         p = user;
129                 else if (p) {
130                         if (*p == ch)
131                                 p++;
132                         else if (!*p && ch == ':')
133                                 break;
134                         else
135                                 p = NULL;
136                 }
137         }
138
139         /* Slurp the secret into the "secret" buffer. */
140         s = secret;
141         while (len > 0) {
142                 if (read(fd, s, 1) != 1 || *s == '\n')
143                         break;
144                 if (*s == '\r')
145                         continue;
146                 s++;
147                 len--;
148         }
149         *s = '\0';
150         close(fd);
151
152         return 1;
153 }
154
155 static const char *getpassf(const char *filename)
156 {
157         STRUCT_STAT st;
158         char buffer[512], *p;
159         int fd, n, ok = 1;
160         const char *envpw = getenv("RSYNC_PASSWORD");
161
162         if (!filename)
163                 return NULL;
164
165         if ((fd = open(filename,O_RDONLY)) < 0) {
166                 rsyserr(FERROR, errno, "could not open password file \"%s\"",
167                         filename);
168                 if (envpw)
169                         rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
170                 return NULL;
171         }
172
173         if (do_stat(filename, &st) == -1) {
174                 rsyserr(FERROR, errno, "stat(%s)", filename);
175                 ok = 0;
176         } else if ((st.st_mode & 06) != 0) {
177                 rprintf(FERROR,"password file must not be other-accessible\n");
178                 ok = 0;
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");
181                 ok = 0;
182         }
183         if (!ok) {
184                 rprintf(FERROR,"continuing without password file\n");
185                 if (envpw)
186                         rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
187                 close(fd);
188                 return NULL;
189         }
190
191         if (envpw)
192                 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
193
194         n = read(fd, buffer, sizeof buffer - 1);
195         close(fd);
196         if (n > 0) {
197                 buffer[n] = '\0';
198                 if ((p = strtok(buffer, "\n\r")) != NULL)
199                         return strdup(p);
200         }
201
202         return NULL;
203 }
204
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)
208 {
209         char buf[MD4_SUM_LENGTH];
210
211         sum_init(0);
212         sum_update(in, strlen(in));
213         sum_update(challenge, strlen(challenge));
214         sum_end(buf);
215
216         base64_encode(buf, MD4_SUM_LENGTH, out, 0);
217 }
218
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  */
225 char *auth_server(int f_in, int f_out, int module, const char *host,
226                   const char *addr, const char *leader)
227 {
228         char *users = lp_auth_users(module);
229         char challenge[MD4_SUM_LENGTH*2];
230         char line[BIGPATHBUFLEN];
231         char secret[512];
232         char pass2[MD4_SUM_LENGTH*2];
233         char *tok, *pass;
234
235         /* if no auth list then allow anyone in! */
236         if (!users || !*users)
237                 return "";
238
239         gen_challenge(addr, challenge);
240
241         io_printf(f_out, "%s%s\n", leader, challenge);
242
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);
248                 return NULL;
249         }
250         *pass++ = '\0';
251
252         if (!(users = strdup(users)))
253                 out_of_memory("auth_server");
254
255         for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
256                 if (wildmatch(tok, line))
257                         break;
258         }
259         free(users);
260
261         if (!tok) {
262                 rprintf(FLOG, "auth failed on module %s from %s (%s): "
263                         "unauthorized user\n",
264                         lp_name(module), host, addr);
265                 return NULL;
266         }
267
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);
274                 return NULL;
275         }
276
277         generate_hash(secret, challenge, pass2);
278         memset(secret, 0, sizeof secret);
279
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);
284                 return NULL;
285         }
286
287         return strdup(line);
288 }
289
290 void auth_client(int fd, const char *user, const char *challenge)
291 {
292         const char *pass;
293         char pass2[MD4_SUM_LENGTH*2];
294
295         if (!user || !*user)
296                 user = "nobody";
297
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.
303                  *
304                  * Andrew Klein says that getpassphrase() is present
305                  * on Solaris and reads up to 256 characters.
306                  *
307                  * OpenBSD has a readpassphrase() that might be more suitable.
308                  */
309                 pass = getpass("Password: ");
310         }
311
312         if (!pass)
313                 pass = "";
314
315         generate_hash(pass, challenge, pass2);
316         io_printf(fd, "%s %s\n", user, pass2);
317 }