| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Support rsync daemon authentication. |
| 3 | * |
| 4 | * Copyright (C) 1998-2000 Andrew Tridgell |
| Wayne Davison | 445640e | 2009-01-03 10:57:14 -0800 | [diff] [blame] | 5 | * Copyright (C) 2002-2009 Wayne Davison |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| Wayne Davison | 8e41b68 | 2007-07-10 13:55:49 +0000 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 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 | * |
| Wayne Davison | e7c6706 | 2006-04-25 23:51:12 +0000 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License along |
| Wayne Davison | 4fd842f | 2007-07-07 05:33:14 +0000 | [diff] [blame] | 18 | * with this program; if not, visit the http://fsf.org website. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 19 | */ |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 20 | |
| Andrew Tridgell | 31593dd | 1998-05-13 09:38:54 +0000 | [diff] [blame] | 21 | #include "rsync.h" |
| 22 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 23 | extern char *password_file; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 24 | |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 25 | /*************************************************************************** |
| 26 | encode a buffer using base64 - simple and slow algorithm. null terminates |
| 27 | the result. |
| 28 | ***************************************************************************/ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 29 | void base64_encode(const char *buf, int len, char *out, int pad) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 30 | { |
| 31 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 32 | int bit_offset, byte_offset, idx, i; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 33 | const uchar *d = (const uchar *)buf; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 34 | int bytes = (len*8 + 5)/6; |
| 35 | |
| Wayne Davison | 5738512 | 2004-01-03 08:53:36 +0000 | [diff] [blame] | 36 | for (i = 0; i < bytes; i++) { |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 37 | 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 | } |
| Wayne Davison | 293def6 | 2006-03-06 18:22:20 +0000 | [diff] [blame] | 49 | |
| 50 | while (pad && (i % 4)) |
| 51 | out[i++] = '='; |
| 52 | |
| 53 | out[i] = '\0'; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 56 | /* Generate a challenge buffer and return it base64-encoded. */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 57 | static void gen_challenge(const char *addr, char *challenge) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 58 | { |
| 59 | char input[32]; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 60 | char digest[MAX_DIGEST_LEN]; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 61 | struct timeval tv; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 62 | int len; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 63 | |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 64 | memset(input, 0, sizeof input); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 65 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 66 | strlcpy(input, addr, 17); |
| Andrew Tridgell | 3060d4a | 2000-01-23 02:16:51 +0000 | [diff] [blame] | 67 | sys_gettimeofday(&tv); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 68 | SIVAL(input, 16, tv.tv_sec); |
| 69 | SIVAL(input, 20, tv.tv_usec); |
| 70 | SIVAL(input, 24, getpid()); |
| 71 | |
| Wayne Davison | ba582f7 | 2004-05-21 08:27:04 +0000 | [diff] [blame] | 72 | sum_init(0); |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 73 | sum_update(input, sizeof input); |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 74 | len = sum_end(digest); |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 75 | |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 76 | base64_encode(digest, len, challenge, 0); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 80 | /* Return the secret for a user from the secret file, null terminated. |
| 81 | * Maximum length is len (not counting the null). */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 82 | static int get_secret(int module, const char *user, char *secret, int len) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 83 | { |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 84 | const char *fname = lp_secrets_file(module); |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 85 | STRUCT_STAT st; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 86 | int fd, ok = 1; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 87 | const char *p; |
| 88 | char ch, *s; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 89 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 90 | if (!fname || !*fname) |
| 91 | return 0; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 92 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 93 | if ((fd = open(fname, O_RDONLY)) < 0) |
| 94 | return 0; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 95 | |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 96 | if (do_stat(fname, &st) == -1) { |
| Wayne Davison | 45c49b5 | 2006-01-13 21:17:09 +0000 | [diff] [blame] | 97 | rsyserr(FLOG, errno, "stat(%s)", fname); |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 98 | ok = 0; |
| David Dykstra | 3ca8e68 | 1999-02-09 19:27:15 +0000 | [diff] [blame] | 99 | } else if (lp_strict_modes(module)) { |
| 100 | if ((st.st_mode & 06) != 0) { |
| Wayne Davison | 30c041f | 2004-09-24 17:04:05 +0000 | [diff] [blame] | 101 | rprintf(FLOG, "secrets file must not be other-accessible (see strict modes option)\n"); |
| David Dykstra | 3ca8e68 | 1999-02-09 19:27:15 +0000 | [diff] [blame] | 102 | ok = 0; |
| Wayne Davison | 351f5e2 | 2006-01-26 11:01:00 +0000 | [diff] [blame] | 103 | } else if (MY_UID() == 0 && st.st_uid != 0) { |
| Wayne Davison | 30c041f | 2004-09-24 17:04:05 +0000 | [diff] [blame] | 104 | rprintf(FLOG, "secrets file must be owned by root when running as root (see strict modes)\n"); |
| David Dykstra | 3ca8e68 | 1999-02-09 19:27:15 +0000 | [diff] [blame] | 105 | ok = 0; |
| 106 | } |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 107 | } |
| 108 | if (!ok) { |
| Wayne Davison | 30c041f | 2004-09-24 17:04:05 +0000 | [diff] [blame] | 109 | rprintf(FLOG, "continuing without secrets file\n"); |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 110 | close(fd); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 114 | if (*user == '#') { |
| 115 | /* Reject attempt to match a comment. */ |
| 116 | close(fd); |
| 117 | return 0; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 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 | } |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 138 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 139 | /* Slurp the secret into the "secret" buffer. */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 140 | s = secret; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 141 | while (len > 0) { |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 142 | if (read(fd, s, 1) != 1 || *s == '\n') |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 143 | break; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 144 | if (*s == '\r') |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 145 | continue; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 146 | s++; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 147 | len--; |
| 148 | } |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 149 | *s = '\0'; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 150 | close(fd); |
| 151 | |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 152 | return 1; |
| 153 | } |
| 154 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 155 | static const char *getpassf(const char *filename) |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 156 | { |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 157 | STRUCT_STAT st; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 158 | char buffer[512], *p; |
| Wayne Davison | 5a9933c | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 159 | int fd, n; |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 160 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 161 | if (!filename) |
| 162 | return NULL; |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 163 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 164 | if ((fd = open(filename,O_RDONLY)) < 0) { |
| Wayne Davison | 5a9933c | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 165 | rsyserr(FERROR, errno, "could not open password file %s", filename); |
| 166 | exit_cleanup(RERR_SYNTAX); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 167 | } |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 168 | |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 169 | if (do_stat(filename, &st) == -1) { |
| Wayne Davison | 5a9933c | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 170 | rsyserr(FERROR, errno, "stat(%s)", filename); |
| 171 | exit_cleanup(RERR_SYNTAX); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 172 | } |
| Wayne Davison | 5a9933c | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 173 | if ((st.st_mode & 06) != 0) { |
| 174 | rprintf(FERROR, "ERROR: password file must not be other-accessible\n"); |
| 175 | exit_cleanup(RERR_SYNTAX); |
| 176 | } |
| 177 | if (MY_UID() == 0 && st.st_uid != 0) { |
| 178 | rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n"); |
| 179 | exit_cleanup(RERR_SYNTAX); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 182 | n = read(fd, buffer, sizeof buffer - 1); |
| 183 | close(fd); |
| 184 | if (n > 0) { |
| 185 | buffer[n] = '\0'; |
| 186 | if ((p = strtok(buffer, "\n\r")) != NULL) |
| 187 | return strdup(p); |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 188 | } |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 189 | |
| Wayne Davison | 5a9933c | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 190 | rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename); |
| 191 | exit_cleanup(RERR_SYNTAX); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 194 | /* Generate an MD4 hash created from the combination of the password |
| 195 | * and the challenge string and return it base64-encoded. */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 196 | static void generate_hash(const char *in, const char *challenge, char *out) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 197 | { |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 198 | char buf[MAX_DIGEST_LEN]; |
| 199 | int len; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 200 | |
| Wayne Davison | ba582f7 | 2004-05-21 08:27:04 +0000 | [diff] [blame] | 201 | sum_init(0); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 202 | sum_update(in, strlen(in)); |
| 203 | sum_update(challenge, strlen(challenge)); |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 204 | len = sum_end(buf); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 205 | |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 206 | base64_encode(buf, len, out, 0); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 209 | /* Possibly negotiate authentication with the client. Use "leader" to |
| 210 | * start off the auth if necessary. |
| 211 | * |
| 212 | * Return NULL if authentication failed. Return "" if anonymous access. |
| 213 | * Otherwise return username. |
| 214 | */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 215 | char *auth_server(int f_in, int f_out, int module, const char *host, |
| 216 | const char *addr, const char *leader) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 217 | { |
| 218 | char *users = lp_auth_users(module); |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 219 | char challenge[MAX_DIGEST_LEN*2]; |
| Wayne Davison | d999d31 | 2005-07-29 18:31:05 +0000 | [diff] [blame] | 220 | char line[BIGPATHBUFLEN]; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 221 | char secret[512]; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 222 | char pass2[MAX_DIGEST_LEN*2]; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 223 | char *tok, *pass; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 224 | |
| 225 | /* if no auth list then allow anyone in! */ |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 226 | if (!users || !*users) |
| 227 | return ""; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 228 | |
| 229 | gen_challenge(addr, challenge); |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 230 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 231 | io_printf(f_out, "%s%s\n", leader, challenge); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 232 | |
| Wayne Davison | f74a3d8 | 2007-09-09 07:53:02 +0000 | [diff] [blame] | 233 | if (!read_line_old(f_in, line, sizeof line) |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 234 | || (pass = strchr(line, ' ')) == NULL) { |
| 235 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
| 236 | "invalid challenge response\n", |
| 237 | lp_name(module), host, addr); |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 238 | return NULL; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 239 | } |
| 240 | *pass++ = '\0'; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 241 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 242 | if (!(users = strdup(users))) |
| 243 | out_of_memory("auth_server"); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 244 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 245 | for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) { |
| 246 | if (wildmatch(tok, line)) |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 247 | break; |
| Andrew Tridgell | c8e78d8 | 1998-05-13 12:21:10 +0000 | [diff] [blame] | 248 | } |
| 249 | free(users); |
| 250 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 251 | if (!tok) { |
| 252 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
| 253 | "unauthorized user\n", |
| 254 | lp_name(module), host, addr); |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 255 | return NULL; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 256 | } |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 257 | |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 258 | memset(secret, 0, sizeof secret); |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 259 | if (!get_secret(module, line, secret, sizeof secret - 1)) { |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 260 | memset(secret, 0, sizeof secret); |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 261 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
| 262 | "missing secret for user \"%s\"\n", |
| 263 | lp_name(module), host, addr, line); |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 264 | return NULL; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 267 | generate_hash(secret, challenge, pass2); |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 268 | memset(secret, 0, sizeof secret); |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 269 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 270 | if (strcmp(pass, pass2) != 0) { |
| 271 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
| 272 | "password mismatch\n", |
| 273 | lp_name(module), host, addr); |
| 274 | return NULL; |
| 275 | } |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 276 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 277 | return strdup(line); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| Dylan Simon | ec883bc | 2010-03-08 22:01:26 -0500 | [diff] [blame] | 280 | #ifdef __BIONIC__ |
| 281 | #include <termios.h> |
| 282 | static const char *getpass(char *prompt) |
| 283 | { |
| 284 | struct termios old, new; |
| 285 | static char res[256]; |
| 286 | char *p; |
| 287 | |
| 288 | /* Turn echoing off and fail if we can't. */ |
| 289 | if (tcgetattr (0, &old) != 0) |
| 290 | return NULL; |
| 291 | new = old; |
| 292 | new.c_lflag &= ~ECHO; |
| 293 | if (tcsetattr (0, TCSAFLUSH, &new) != 0) |
| 294 | return NULL; |
| 295 | |
| 296 | fprintf(stderr, "%s", prompt); |
| 297 | fflush(stderr); |
| 298 | fgets(res, 256, stdin); |
| 299 | if ((p = strchr(res, '\n'))) |
| 300 | *p = 0; |
| 301 | |
| 302 | /* Restore terminal. */ |
| 303 | tcsetattr (0, TCSAFLUSH, &old); |
| 304 | |
| 305 | return res; |
| 306 | } |
| 307 | |
| 308 | #endif |
| 309 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 310 | void auth_client(int fd, const char *user, const char *challenge) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 311 | { |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 312 | const char *pass; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 313 | char pass2[MAX_DIGEST_LEN*2]; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 314 | |
| Wayne Davison | ef383c0 | 2004-03-31 18:52:38 +0000 | [diff] [blame] | 315 | if (!user || !*user) |
| Wayne Davison | 4b2f6a7 | 2004-04-01 18:05:40 +0000 | [diff] [blame] | 316 | user = "nobody"; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 317 | |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 318 | if (!(pass = getpassf(password_file)) |
| 319 | && !(pass = getenv("RSYNC_PASSWORD"))) { |
| Martin Pool | 64bd756 | 2001-08-29 07:23:30 +0000 | [diff] [blame] | 320 | /* XXX: cyeoh says that getpass is deprecated, because |
| Martin Pool | 908f5a9 | 2003-06-17 04:46:32 +0000 | [diff] [blame] | 321 | * it may return a truncated password on some systems, |
| 322 | * and it is not in the LSB. |
| 323 | * |
| 324 | * Andrew Klein says that getpassphrase() is present |
| 325 | * on Solaris and reads up to 256 characters. |
| 326 | * |
| 327 | * OpenBSD has a readpassphrase() that might be more suitable. |
| 328 | */ |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 329 | pass = getpass("Password: "); |
| 330 | } |
| 331 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 332 | if (!pass) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 333 | pass = ""; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 334 | |
| 335 | generate_hash(pass, challenge, pass2); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 336 | io_printf(fd, "%s %s\n", user, pass2); |
| 337 | } |