blob: 9254fb0bf2e9b6705d358c1538cfcbdeeadbc829 [file] [log] [blame]
Martin Pool4a13b9d2000-10-26 07:31:29 +00001/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2000 by Andrew Tridgell
Andrew Tridgell31593dd1998-05-13 09:38:54 +00004
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
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000023/***************************************************************************
24encode a buffer using base64 - simple and slow algorithm. null terminates
25the result.
26 ***************************************************************************/
Wayne Davison57385122004-01-03 08:53:36 +000027void base64_encode(char *buf, int len, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000028{
29 char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30 int bit_offset, byte_offset, idx, i;
31 unsigned char *d = (unsigned char *)buf;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000032 int bytes = (len*8 + 5)/6;
33
34 memset(out, 0, bytes+1);
35
Wayne Davison57385122004-01-03 08:53:36 +000036 for (i = 0; i < bytes; i++) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000037 byte_offset = (i*6)/8;
38 bit_offset = (i*6)%8;
39 if (bit_offset < 3) {
40 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
41 } else {
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)));
45 }
46 }
47 out[i] = b64[idx];
48 }
49}
50
51/* create a 16 byte challenge buffer */
52static void gen_challenge(char *addr, char *challenge)
53{
54 char input[32];
55 struct timeval tv;
56
57 memset(input, 0, sizeof(input));
58
Andrew Tridgell37f98051998-11-14 23:31:58 +000059 strlcpy((char *)input, addr, 17);
Andrew Tridgell3060d4a2000-01-23 02:16:51 +000060 sys_gettimeofday(&tv);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000061 SIVAL(input, 16, tv.tv_sec);
62 SIVAL(input, 20, tv.tv_usec);
63 SIVAL(input, 24, getpid());
64
65 sum_init();
66 sum_update(input, sizeof(input));
67 sum_end(challenge);
68}
69
70
71/* return the secret for a user from the sercret file. maximum length
72 is len. null terminate it */
73static int get_secret(int module, char *user, char *secret, int len)
74{
75 char *fname = lp_secrets_file(module);
76 int fd, found=0;
Andrew Tridgelle42c9451998-05-15 09:26:01 +000077 char line[MAXPATHLEN];
78 char *p, *pass=NULL;
David Dykstrad1be2311998-11-24 19:52:35 +000079 STRUCT_STAT st;
80 int ok = 1;
81 extern int am_root;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000082
83 if (!fname || !*fname) return 0;
84
Wayne Davison73ff7202003-01-26 19:37:54 +000085 fd = open(fname,O_RDONLY);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +000086 if (fd == -1) return 0;
87
David Dykstrad1be2311998-11-24 19:52:35 +000088 if (do_stat(fname, &st) == -1) {
Martin Poola0397492000-10-26 07:24:18 +000089 rsyserr(FERROR, errno, "stat(%s)", fname);
David Dykstrad1be2311998-11-24 19:52:35 +000090 ok = 0;
David Dykstra3ca8e681999-02-09 19:27:15 +000091 } else if (lp_strict_modes(module)) {
92 if ((st.st_mode & 06) != 0) {
93 rprintf(FERROR,"secrets file must not be other-accessible (see strict modes option)\n");
94 ok = 0;
95 } else if (am_root && (st.st_uid != 0)) {
96 rprintf(FERROR,"secrets file must be owned by root when running as root (see strict modes)\n");
97 ok = 0;
98 }
David Dykstrad1be2311998-11-24 19:52:35 +000099 }
100 if (!ok) {
101 rprintf(FERROR,"continuing without secrets file\n");
102 close(fd);
103 return 0;
104 }
105
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000106 while (!found) {
107 int i = 0;
Martin Pool707de532002-01-24 02:33:45 +0000108 memset(line, 0, sizeof line);
109 while ((size_t) i < (sizeof(line)-1)) {
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000110 if (read(fd, &line[i], 1) != 1) {
111 memset(line, 0, sizeof(line));
112 close(fd);
113 return 0;
114 }
115 if (line[i] == '\r') continue;
116 if (line[i] == '\n') break;
117 i++;
118 }
119 line[i] = 0;
120 if (line[0] == '#') continue;
121 p = strchr(line,':');
122 if (!p) continue;
123 *p = 0;
124 if (strcmp(user, line)) continue;
125 pass = p+1;
126 found = 1;
127 }
128
129 close(fd);
130 if (!found) return 0;
131
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000132 strlcpy(secret, pass, len);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000133 return 1;
134}
135
Andrew Tridgell65575e91999-01-08 10:32:56 +0000136static char *getpassf(char *filename)
137{
138 char buffer[100];
Andrew Tridgell65575e91999-01-08 10:32:56 +0000139 int fd=0;
140 STRUCT_STAT st;
141 int ok = 1;
142 extern int am_root;
143 char *envpw=getenv("RSYNC_PASSWORD");
144
145 if (!filename) return NULL;
146
Wayne Davison73ff7202003-01-26 19:37:54 +0000147 if ( (fd=open(filename,O_RDONLY)) == -1) {
Martin Pool4a13b9d2000-10-26 07:31:29 +0000148 rsyserr(FERROR, errno, "could not open password file \"%s\"",filename);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000149 if (envpw) rprintf(FERROR,"falling back to RSYNC_PASSWORD environment variable.\n");
150 return NULL;
151 }
152
153 if (do_stat(filename, &st) == -1) {
Martin Poola0397492000-10-26 07:24:18 +0000154 rsyserr(FERROR, errno, "stat(%s)", filename);
Andrew Tridgell65575e91999-01-08 10:32:56 +0000155 ok = 0;
156 } else if ((st.st_mode & 06) != 0) {
157 rprintf(FERROR,"password file must not be other-accessible\n");
158 ok = 0;
159 } else if (am_root && (st.st_uid != 0)) {
160 rprintf(FERROR,"password file must be owned by root when running as root\n");
161 ok = 0;
162 }
163 if (!ok) {
164 rprintf(FERROR,"continuing without password file\n");
165 if (envpw) rprintf(FERROR,"using RSYNC_PASSWORD environment variable.\n");
166 close(fd);
167 return NULL;
168 }
169
170 if (envpw) rprintf(FERROR,"RSYNC_PASSWORD environment variable ignored\n");
171
172 buffer[sizeof(buffer)-1]='\0';
Andrew Tridgellae682c32000-08-19 15:25:05 +0000173 if (read(fd,buffer,sizeof(buffer)-1) > 0)
Andrew Tridgell65575e91999-01-08 10:32:56 +0000174 {
Andrew Tridgell379e6891999-01-08 10:42:29 +0000175 char *p = strtok(buffer,"\n\r");
Andrew Tridgell65575e91999-01-08 10:32:56 +0000176 close(fd);
Andrew Tridgell379e6891999-01-08 10:42:29 +0000177 if (p) p = strdup(p);
178 return p;
Andrew Tridgell65575e91999-01-08 10:32:56 +0000179 }
180
181 return NULL;
182}
183
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000184/* generate a 16 byte hash from a password and challenge */
Andrew Tridgell6e4fb641998-09-09 05:57:34 +0000185static void generate_hash(char *in, char *challenge, char *out)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000186{
187 char buf[16];
188
189 sum_init();
190 sum_update(in, strlen(in));
191 sum_update(challenge, strlen(challenge));
192 sum_end(buf);
193
194 base64_encode(buf, 16, out);
195}
196
197/* possible negotiate authentication with the client. Use "leader" to
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000198 start off the auth if necessary
199
200 return NULL if authentication failed
201
202 return "" if anonymous access
203
204 otherwise return username
205*/
Wayne Davison973007d2002-08-01 00:36:54 +0000206char *auth_server(int f_in, int f_out, int module, char *addr, char *leader)
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000207{
208 char *users = lp_auth_users(module);
209 char challenge[16];
210 char b64_challenge[30];
Andrew Tridgelle42c9451998-05-15 09:26:01 +0000211 char line[MAXPATHLEN];
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000212 static char user[100];
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000213 char secret[100];
214 char pass[30];
215 char pass2[30];
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000216 char *tok;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000217
218 /* if no auth list then allow anyone in! */
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000219 if (!users || !*users) return "";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000220
221 gen_challenge(addr, challenge);
222
223 base64_encode(challenge, 16, b64_challenge);
224
Wayne Davison973007d2002-08-01 00:36:54 +0000225 io_printf(f_out, "%s%s\n", leader, b64_challenge);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000226
Wayne Davison973007d2002-08-01 00:36:54 +0000227 if (!read_line(f_in, line, sizeof(line)-1)) {
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000228 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000229 }
230
231 memset(user, 0, sizeof(user));
232 memset(pass, 0, sizeof(pass));
233
234 if (sscanf(line,"%99s %29s", user, pass) != 2) {
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000235 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000236 }
Andrew Tridgell5d78a102001-06-22 10:16:04 +0000237
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000238 users = strdup(users);
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000239 if (!users) return NULL;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000240
241 for (tok=strtok(users," ,\t"); tok; tok = strtok(NULL," ,\t")) {
Wayne Davisonfe332032003-07-30 06:12:27 +0000242 if (wildmatch(tok, user)) break;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000243 }
244 free(users);
245
246 if (!tok) {
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000247 return NULL;
Andrew Tridgellc8e78d81998-05-13 12:21:10 +0000248 }
249
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000250 memset(secret, 0, sizeof(secret));
251 if (!get_secret(module, user, secret, sizeof(secret)-1)) {
252 memset(secret, 0, sizeof(secret));
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000253 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000254 }
255
256 generate_hash(secret, b64_challenge, pass2);
257 memset(secret, 0, sizeof(secret));
258
Andrew Tridgelld0d56391998-05-16 07:45:26 +0000259 if (strcmp(pass, pass2) == 0)
260 return user;
261
262 return NULL;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000263}
264
265
266void auth_client(int fd, char *user, char *challenge)
267{
268 char *pass;
269 char pass2[30];
Andrew Tridgell65575e91999-01-08 10:32:56 +0000270 extern char *password_file;
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000271
Wayne Davisonef383c02004-03-31 18:52:38 +0000272 if (!user || !*user)
273 user = "guest";
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000274
Andrew Tridgell65575e91999-01-08 10:32:56 +0000275 if (!(pass=getpassf(password_file)) && !(pass=getenv("RSYNC_PASSWORD"))) {
Martin Pool64bd7562001-08-29 07:23:30 +0000276 /* XXX: cyeoh says that getpass is deprecated, because
Martin Pool908f5a92003-06-17 04:46:32 +0000277 * it may return a truncated password on some systems,
278 * and it is not in the LSB.
279 *
280 * Andrew Klein says that getpassphrase() is present
281 * on Solaris and reads up to 256 characters.
282 *
283 * OpenBSD has a readpassphrase() that might be more suitable.
284 */
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000285 pass = getpass("Password: ");
286 }
287
288 if (!pass || !*pass) {
289 pass = "";
290 }
291
292 generate_hash(pass, challenge, pass2);
Andrew Tridgellbcb7e501998-05-13 11:49:05 +0000293 io_printf(fd, "%s %s\n", user, pass2);
294}
295
Andrew Tridgell65575e91999-01-08 10:32:56 +0000296