Check MY_UID() directly instead of accessing am_root.
[rsync/rsync.git] / authenticate.c
1 /* -*- c-file-style: "linux"; -*-
2
3    Copyright (C) 1998-2000 by Andrew Tridgell
4
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.
9
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.
14
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.
18 */
19
20 /* support rsync authentication */
21 #include "rsync.h"
22
23 extern char *password_file;
24
25 /***************************************************************************
26 encode a buffer using base64 - simple and slow algorithm. null terminates
27 the result.
28   ***************************************************************************/
29 void base64_encode(char *buf, int len, char *out)
30 {
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;
35
36         memset(out, 0, bytes+1);
37
38         for (i = 0; i < bytes; i++) {
39                 byte_offset = (i*6)/8;
40                 bit_offset = (i*6)%8;
41                 if (bit_offset < 3) {
42                         idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
43                 } else {
44                         idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
45                         if (byte_offset+1 < len) {
46                                 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
47                         }
48                 }
49                 out[i] = b64[idx];
50         }
51 }
52
53 /* Generate a challenge buffer and return it base64-encoded. */
54 static void gen_challenge(char *addr, char *challenge)
55 {
56         char input[32];
57         char md4_out[MD4_SUM_LENGTH];
58         struct timeval tv;
59
60         memset(input, 0, sizeof input);
61
62         strlcpy((char *)input, addr, 17);
63         sys_gettimeofday(&tv);
64         SIVAL(input, 16, tv.tv_sec);
65         SIVAL(input, 20, tv.tv_usec);
66         SIVAL(input, 24, getpid());
67
68         sum_init(0);
69         sum_update(input, sizeof input);
70         sum_end(md4_out);
71
72         base64_encode(md4_out, MD4_SUM_LENGTH, challenge);
73 }
74
75
76 /* Return the secret for a user from the secret file, null terminated.
77  * Maximum length is len (not counting the null). */
78 static int get_secret(int module, char *user, char *secret, int len)
79 {
80         char *fname = lp_secrets_file(module);
81         STRUCT_STAT st;
82         int fd, ok = 1;
83         char ch, *p;
84
85         if (!fname || !*fname)
86                 return 0;
87
88         if ((fd = open(fname, O_RDONLY)) < 0)
89                 return 0;
90
91         if (do_stat(fname, &st) == -1) {
92                 rsyserr(FLOG, errno, "stat(%s)", fname);
93                 ok = 0;
94         } else if (lp_strict_modes(module)) {
95                 if ((st.st_mode & 06) != 0) {
96                         rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n");
97                         ok = 0;
98                 } else if (MY_UID() == 0 && st.st_uid != 0) {
99                         rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n");
100                         ok = 0;
101                 }
102         }
103         if (!ok) {
104                 rprintf(FLOG, "continuing without secrets file\n");
105                 close(fd);
106                 return 0;
107         }
108
109         if (*user == '#') {
110                 /* Reject attempt to match a comment. */
111                 close(fd);
112                 return 0;
113         }
114
115         /* Try to find a line that starts with the user name and a ':'. */
116         p = user;
117         while (1) {
118                 if (read(fd, &ch, 1) != 1) {
119                         close(fd);
120                         return 0;
121                 }
122                 if (ch == '\n')
123                         p = user;
124                 else if (p) {
125                         if (*p == ch)
126                                 p++;
127                         else if (!*p && ch == ':')
128                                 break;
129                         else
130                                 p = NULL;
131                 }
132         }
133
134         /* Slurp the secret into the "secret" buffer. */
135         p = secret;
136         while (len > 0) {
137                 if (read(fd, p, 1) != 1 || *p == '\n')
138                         break;
139                 if (*p == '\r')
140                         continue;
141                 p++;
142                 len--;
143         }
144         *p = '\0';
145         close(fd);
146
147         return 1;
148 }
149
150 static char *getpassf(char *filename)
151 {
152         STRUCT_STAT st;
153         char buffer[512], *p;
154         int fd, n, ok = 1;
155         char *envpw = getenv("RSYNC_PASSWORD");
156
157         if (!filename)
158                 return NULL;
159
160         if ((fd = open(filename,O_RDONLY)) < 0) {
161                 rsyserr(FERROR, errno, "could not open password file \"%s\"",
162                         filename);
163                 if (envpw)
164                         rprintf(FERROR, "falling back to RSYNC_PASSWORD environment variable.\n");
165                 return NULL;
166         }
167
168         if (do_stat(filename, &st) == -1) {
169                 rsyserr(FERROR, errno, "stat(%s)", filename);
170                 ok = 0;
171         } else if ((st.st_mode & 06) != 0) {
172                 rprintf(FERROR,"password file must not be other-accessible\n");
173                 ok = 0;
174         } else if (MY_UID() == 0 && st.st_uid != 0) {
175                 rprintf(FERROR,"password file must be owned by root when running as root\n");
176                 ok = 0;
177         }
178         if (!ok) {
179                 rprintf(FERROR,"continuing without password file\n");
180                 if (envpw)
181                         rprintf(FERROR, "using RSYNC_PASSWORD environment variable.\n");
182                 close(fd);
183                 return NULL;
184         }
185
186         if (envpw)
187                 rprintf(FERROR, "RSYNC_PASSWORD environment variable ignored\n");
188
189         n = read(fd, buffer, sizeof buffer - 1);
190         close(fd);
191         if (n > 0) {
192                 buffer[n] = '\0';
193                 if ((p = strtok(buffer, "\n\r")) != NULL)
194                         return strdup(p);
195         }
196
197         return NULL;
198 }
199
200 /* Generate an MD4 hash created from the combination of the password
201  * and the challenge string and return it base64-encoded. */
202 static void generate_hash(char *in, char *challenge, char *out)
203 {
204         char buf[MD4_SUM_LENGTH];
205
206         sum_init(0);
207         sum_update(in, strlen(in));
208         sum_update(challenge, strlen(challenge));
209         sum_end(buf);
210
211         base64_encode(buf, MD4_SUM_LENGTH, out);
212 }
213
214 /* Possibly negotiate authentication with the client.  Use "leader" to
215  * start off the auth if necessary.
216  *
217  * Return NULL if authentication failed.  Return "" if anonymous access.
218  * Otherwise return username.
219  */
220 char *auth_server(int f_in, int f_out, int module, char *host, char *addr,
221                   char *leader)
222 {
223         char *users = lp_auth_users(module);
224         char challenge[MD4_SUM_LENGTH*2];
225         char line[BIGPATHBUFLEN];
226         char secret[512];
227         char pass2[MD4_SUM_LENGTH*2];
228         char *tok, *pass;
229
230         /* if no auth list then allow anyone in! */
231         if (!users || !*users)
232                 return "";
233
234         gen_challenge(addr, challenge);
235
236         io_printf(f_out, "%s%s\n", leader, challenge);
237
238         if (!read_line(f_in, line, sizeof line - 1)
239          || (pass = strchr(line, ' ')) == NULL) {
240                 rprintf(FLOG, "auth failed on module %s from %s (%s): "
241                         "invalid challenge response\n",
242                         lp_name(module), host, addr);
243                 return NULL;
244         }
245         *pass++ = '\0';
246
247         if (!(users = strdup(users)))
248                 out_of_memory("auth_server");
249
250         for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
251                 if (wildmatch(tok, line))
252                         break;
253         }
254         free(users);
255
256         if (!tok) {
257                 rprintf(FLOG, "auth failed on module %s from %s (%s): "
258                         "unauthorized user\n",
259                         lp_name(module), host, addr);
260                 return NULL;
261         }
262
263         memset(secret, 0, sizeof secret);
264         if (!get_secret(module, line, secret, sizeof secret - 1)) {
265                 memset(secret, 0, sizeof secret);
266                 rprintf(FLOG, "auth failed on module %s from %s (%s): "
267                         "missing secret for user \"%s\"\n",
268                         lp_name(module), host, addr, line);
269                 return NULL;
270         }
271
272         generate_hash(secret, challenge, pass2);
273         memset(secret, 0, sizeof secret);
274
275         if (strcmp(pass, pass2) != 0) {
276                 rprintf(FLOG, "auth failed on module %s from %s (%s): "
277                         "password mismatch\n",
278                         lp_name(module), host, addr);
279                 return NULL;
280         }
281
282         return strdup(line);
283 }
284
285
286 void auth_client(int fd, char *user, char *challenge)
287 {
288         char *pass;
289         char pass2[MD4_SUM_LENGTH*2];
290
291         if (!user || !*user)
292                 user = "nobody";
293
294         if (!(pass = getpassf(password_file))
295          && !(pass = getenv("RSYNC_PASSWORD"))) {
296                 /* XXX: cyeoh says that getpass is deprecated, because
297                  * it may return a truncated password on some systems,
298                  * and it is not in the LSB.
299                  *
300                  * Andrew Klein says that getpassphrase() is present
301                  * on Solaris and reads up to 256 characters.
302                  *
303                  * OpenBSD has a readpassphrase() that might be more suitable.
304                  */
305                 pass = getpass("Password: ");
306         }
307
308         if (!pass)
309                 pass = "";
310
311         generate_hash(pass, challenge, pass2);
312         io_printf(fd, "%s %s\n", user, pass2);
313 }