| 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 | ed4b344 | 2022-09-30 12:36:21 -0700 | [diff] [blame] | 5 | * Copyright (C) 2002-2022 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" |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 22 | #include "itypes.h" |
| Wayne Davison | 11eb67e | 2020-06-25 19:59:19 -0700 | [diff] [blame] | 23 | #include "ifuncs.h" |
| Andrew Tridgell | 31593dd | 1998-05-13 09:38:54 +0000 | [diff] [blame] | 24 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 25 | extern int read_only; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 26 | extern char *password_file; |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 27 | extern struct name_num_obj valid_auth_checksums; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 28 | |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 29 | /*************************************************************************** |
| 30 | encode a buffer using base64 - simple and slow algorithm. null terminates |
| 31 | the result. |
| 32 | ***************************************************************************/ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 33 | void base64_encode(const char *buf, int len, char *out, int pad) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 34 | { |
| 35 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 36 | int bit_offset, byte_offset, idx, i; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 37 | const uchar *d = (const uchar *)buf; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 38 | int bytes = (len*8 + 5)/6; |
| 39 | |
| Wayne Davison | 5738512 | 2004-01-03 08:53:36 +0000 | [diff] [blame] | 40 | for (i = 0; i < bytes; i++) { |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 41 | byte_offset = (i*6)/8; |
| 42 | bit_offset = (i*6)%8; |
| 43 | if (bit_offset < 3) { |
| 44 | idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F; |
| 45 | } else { |
| 46 | idx = (d[byte_offset] << (bit_offset-2)) & 0x3F; |
| 47 | if (byte_offset+1 < len) { |
| 48 | idx |= (d[byte_offset+1] >> (8-(bit_offset-2))); |
| 49 | } |
| 50 | } |
| 51 | out[i] = b64[idx]; |
| 52 | } |
| Wayne Davison | 293def6 | 2006-03-06 18:22:20 +0000 | [diff] [blame] | 53 | |
| 54 | while (pad && (i % 4)) |
| 55 | out[i++] = '='; |
| 56 | |
| 57 | out[i] = '\0'; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 60 | /* Generate a challenge buffer and return it base64-encoded. */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 61 | static void gen_challenge(const char *addr, char *challenge) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 62 | { |
| 63 | char input[32]; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 64 | char digest[MAX_DIGEST_LEN]; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 65 | struct timeval tv; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 66 | int len; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 67 | |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 68 | memset(input, 0, sizeof input); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 69 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 70 | strlcpy(input, addr, 17); |
| Andrew Tridgell | 3060d4a | 2000-01-23 02:16:51 +0000 | [diff] [blame] | 71 | sys_gettimeofday(&tv); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 72 | SIVAL(input, 16, tv.tv_sec); |
| 73 | SIVAL(input, 20, tv.tv_usec); |
| 74 | SIVAL(input, 24, getpid()); |
| 75 | |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 76 | len = sum_init(valid_auth_checksums.negotiated_nni, 0); |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 77 | sum_update(input, sizeof input); |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 78 | sum_end(digest); |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 79 | |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 80 | base64_encode(digest, len, challenge, 0); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 83 | /* Generate an MD4 hash created from the combination of the password |
| 84 | * and the challenge string and return it base64-encoded. */ |
| 85 | static void generate_hash(const char *in, const char *challenge, char *out) |
| 86 | { |
| 87 | char buf[MAX_DIGEST_LEN]; |
| 88 | int len; |
| 89 | |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 90 | len = sum_init(valid_auth_checksums.negotiated_nni, 0); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 91 | sum_update(in, strlen(in)); |
| 92 | sum_update(challenge, strlen(challenge)); |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 93 | sum_end(buf); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 94 | |
| 95 | base64_encode(buf, len, out, 0); |
| 96 | } |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 97 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 98 | /* Return the secret for a user from the secret file, null terminated. |
| 99 | * Maximum length is len (not counting the null). */ |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 100 | static const char *check_secret(int module, const char *user, const char *group, |
| 101 | const char *challenge, const char *pass) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 102 | { |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 103 | char line[1024]; |
| 104 | char pass2[MAX_DIGEST_LEN*2]; |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 105 | const char *fname = lp_secrets_file(module); |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 106 | STRUCT_STAT st; |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 107 | int ok = 1; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 108 | int user_len = strlen(user); |
| 109 | int group_len = group ? strlen(group) : 0; |
| 110 | char *err; |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 111 | FILE *fh; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 112 | |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 113 | if (!fname || !*fname || (fh = fopen(fname, "r")) == NULL) |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 114 | return "no secrets file"; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 115 | |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 116 | if (do_fstat(fileno(fh), &st) == -1) { |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 117 | rsyserr(FLOG, errno, "fstat(%s)", fname); |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 118 | ok = 0; |
| David Dykstra | 3ca8e68 | 1999-02-09 19:27:15 +0000 | [diff] [blame] | 119 | } else if (lp_strict_modes(module)) { |
| 120 | if ((st.st_mode & 06) != 0) { |
| Wayne Davison | 30c041f | 2004-09-24 17:04:05 +0000 | [diff] [blame] | 121 | 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] | 122 | ok = 0; |
| Wayne Davison | 59cb358 | 2020-07-06 00:00:36 -0700 | [diff] [blame] | 123 | } else if (MY_UID() == ROOT_UID && st.st_uid != ROOT_UID) { |
| Wayne Davison | 30c041f | 2004-09-24 17:04:05 +0000 | [diff] [blame] | 124 | 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] | 125 | ok = 0; |
| 126 | } |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 127 | } |
| 128 | if (!ok) { |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 129 | fclose(fh); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 130 | return "ignoring secrets file"; |
| David Dykstra | d1be231 | 1998-11-24 19:52:35 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 133 | if (*user == '#') { |
| 134 | /* Reject attempt to match a comment. */ |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 135 | fclose(fh); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 136 | return "invalid username"; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 139 | /* Try to find a line that starts with the user (or @group) name and a ':'. */ |
| 140 | err = "secret not found"; |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 141 | while ((user || group) && fgets(line, sizeof line, fh) != NULL) { |
| 142 | const char **ptr, *s = strtok(line, "\n\r"); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 143 | int len; |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 144 | if (!s) |
| 145 | continue; |
| 146 | if (*s == '@') { |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 147 | ptr = &group; |
| 148 | len = group_len; |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 149 | s++; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 150 | } else { |
| 151 | ptr = &user; |
| 152 | len = user_len; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 153 | } |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 154 | if (!*ptr || strncmp(s, *ptr, len) != 0 || s[len] != ':') |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 155 | continue; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 156 | generate_hash(s+len+1, challenge, pass2); |
| 157 | if (strcmp(pass, pass2) == 0) { |
| 158 | err = NULL; |
| 159 | break; |
| 160 | } |
| 161 | err = "password mismatch"; |
| 162 | *ptr = NULL; /* Don't look for name again. */ |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 163 | } |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 164 | |
| Wayne Davison | 0dedfbc | 2014-04-13 13:44:58 -0700 | [diff] [blame] | 165 | fclose(fh); |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 166 | |
| Wayne Davison | c376170 | 2019-01-08 14:46:41 -0800 | [diff] [blame] | 167 | force_memzero(line, sizeof line); |
| 168 | force_memzero(pass2, sizeof pass2); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 169 | |
| 170 | return err; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 173 | static const char *getpassf(const char *filename) |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 174 | { |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 175 | STRUCT_STAT st; |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 176 | char buffer[512], *p; |
| Wayne Davison | 12505e0 | 2013-06-09 12:04:25 -0700 | [diff] [blame] | 177 | int n; |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 178 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 179 | if (!filename) |
| 180 | return NULL; |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 181 | |
| Wayne Davison | 12505e0 | 2013-06-09 12:04:25 -0700 | [diff] [blame] | 182 | if (strcmp(filename, "-") == 0) { |
| 183 | n = fgets(buffer, sizeof buffer, stdin) == NULL ? -1 : (int)strlen(buffer); |
| 184 | } else { |
| 185 | int fd; |
| 186 | |
| 187 | if ((fd = open(filename,O_RDONLY)) < 0) { |
| 188 | rsyserr(FERROR, errno, "could not open password file %s", filename); |
| 189 | exit_cleanup(RERR_SYNTAX); |
| 190 | } |
| 191 | |
| 192 | if (do_stat(filename, &st) == -1) { |
| 193 | rsyserr(FERROR, errno, "stat(%s)", filename); |
| 194 | exit_cleanup(RERR_SYNTAX); |
| 195 | } |
| 196 | if ((st.st_mode & 06) != 0) { |
| 197 | rprintf(FERROR, "ERROR: password file must not be other-accessible\n"); |
| 198 | exit_cleanup(RERR_SYNTAX); |
| 199 | } |
| Wayne Davison | 59cb358 | 2020-07-06 00:00:36 -0700 | [diff] [blame] | 200 | if (MY_UID() == ROOT_UID && st.st_uid != ROOT_UID) { |
| Wayne Davison | 12505e0 | 2013-06-09 12:04:25 -0700 | [diff] [blame] | 201 | rprintf(FERROR, "ERROR: password file must be owned by root when running as root\n"); |
| 202 | exit_cleanup(RERR_SYNTAX); |
| 203 | } |
| 204 | |
| 205 | n = read(fd, buffer, sizeof buffer - 1); |
| 206 | close(fd); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 207 | } |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 208 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 209 | if (n > 0) { |
| 210 | buffer[n] = '\0'; |
| 211 | if ((p = strtok(buffer, "\n\r")) != NULL) |
| 212 | return strdup(p); |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 213 | } |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 214 | |
| Wayne Davison | 70c4bfb | 2011-09-06 21:18:32 -0700 | [diff] [blame] | 215 | rprintf(FERROR, "ERROR: failed to read a password from %s\n", filename); |
| 216 | exit_cleanup(RERR_SYNTAX); |
| Andrew Tridgell | 65575e9 | 1999-01-08 10:32:56 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 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 | */ |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 225 | char *auth_server(int f_in, int f_out, int module, const char *host, |
| 226 | const char *addr, const char *leader) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 227 | { |
| 228 | char *users = lp_auth_users(module); |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 229 | char challenge[MAX_DIGEST_LEN*2]; |
| Wayne Davison | d999d31 | 2005-07-29 18:31:05 +0000 | [diff] [blame] | 230 | char line[BIGPATHBUFLEN]; |
| Wayne Davison | d6f0342 | 2020-07-05 22:17:09 -0700 | [diff] [blame] | 231 | const char **auth_uid_groups = NULL; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 232 | int auth_uid_groups_cnt = -1; |
| 233 | const char *err = NULL; |
| 234 | int group_match = -1; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 235 | char *tok, *pass; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 236 | char opt_ch = '\0'; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 237 | |
| 238 | /* if no auth list then allow anyone in! */ |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 239 | if (!users || !*users) |
| 240 | return ""; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 241 | |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 242 | negotiate_daemon_auth(f_out, 0); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 243 | gen_challenge(addr, challenge); |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 244 | |
| Wayne Davison | bf011fe | 2005-04-10 17:09:10 +0000 | [diff] [blame] | 245 | io_printf(f_out, "%s%s\n", leader, challenge); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 246 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 247 | if (!read_line_old(f_in, line, sizeof line, 0) |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 248 | || (pass = strchr(line, ' ')) == NULL) { |
| 249 | rprintf(FLOG, "auth failed on module %s from %s (%s): " |
| 250 | "invalid challenge response\n", |
| 251 | lp_name(module), host, addr); |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 252 | return NULL; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 253 | } |
| 254 | *pass++ = '\0'; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 255 | |
| Wayne Davison | 11eb67e | 2020-06-25 19:59:19 -0700 | [diff] [blame] | 256 | users = strdup(users); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 257 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 258 | for (tok = strtok(users, " ,\t"); tok; tok = strtok(NULL, " ,\t")) { |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 259 | char *opts; |
| 260 | /* See if the user appended :deny, :ro, or :rw. */ |
| 261 | if ((opts = strchr(tok, ':')) != NULL) { |
| 262 | *opts++ = '\0'; |
| 263 | opt_ch = isUpper(opts) ? toLower(opts) : *opts; |
| 264 | if (opt_ch == 'r') { /* handle ro and rw */ |
| 265 | opt_ch = isUpper(opts+1) ? toLower(opts+1) : opts[1]; |
| 266 | if (opt_ch == 'o') |
| 267 | opt_ch = 'r'; |
| 268 | else if (opt_ch != 'w') |
| 269 | opt_ch = '\0'; |
| 270 | } else if (opt_ch != 'd') /* if it's not deny, ignore it */ |
| 271 | opt_ch = '\0'; |
| 272 | } else |
| 273 | opt_ch = '\0'; |
| 274 | if (*tok != '@') { |
| 275 | /* Match the username */ |
| 276 | if (wildmatch(tok, line)) |
| 277 | break; |
| 278 | } else { |
| 279 | #ifdef HAVE_GETGROUPLIST |
| 280 | int j; |
| 281 | /* See if authorizing user is a real user, and if so, see |
| 282 | * if it is in a group that matches tok+1 wildmat. */ |
| 283 | if (auth_uid_groups_cnt < 0) { |
| Wayne Davison | 2a7355f | 2015-08-24 11:23:31 -0700 | [diff] [blame] | 284 | item_list gid_list = EMPTY_ITEM_LIST; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 285 | uid_t auth_uid; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 286 | if (!user_to_uid(line, &auth_uid, False) |
| Wayne Davison | 2a7355f | 2015-08-24 11:23:31 -0700 | [diff] [blame] | 287 | || getallgroups(auth_uid, &gid_list) != NULL) |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 288 | auth_uid_groups_cnt = 0; |
| 289 | else { |
| Wayne Davison | 2a7355f | 2015-08-24 11:23:31 -0700 | [diff] [blame] | 290 | gid_t *gid_array = gid_list.items; |
| 291 | auth_uid_groups_cnt = gid_list.count; |
| Wayne Davison | d6f0342 | 2020-07-05 22:17:09 -0700 | [diff] [blame] | 292 | auth_uid_groups = new_array(const char *, auth_uid_groups_cnt); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 293 | for (j = 0; j < auth_uid_groups_cnt; j++) |
| Wayne Davison | 2a7355f | 2015-08-24 11:23:31 -0700 | [diff] [blame] | 294 | auth_uid_groups[j] = gid_to_group(gid_array[j]); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | for (j = 0; j < auth_uid_groups_cnt; j++) { |
| 298 | if (auth_uid_groups[j] && wildmatch(tok+1, auth_uid_groups[j])) { |
| 299 | group_match = j; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (group_match >= 0) |
| 304 | break; |
| 305 | #else |
| 306 | rprintf(FLOG, "your computer doesn't support getgrouplist(), so no @group authorization is possible.\n"); |
| 307 | #endif |
| 308 | } |
| Andrew Tridgell | c8e78d8 | 1998-05-13 12:21:10 +0000 | [diff] [blame] | 309 | } |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 310 | |
| Andrew Tridgell | c8e78d8 | 1998-05-13 12:21:10 +0000 | [diff] [blame] | 311 | free(users); |
| 312 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 313 | if (!tok) |
| 314 | err = "no matching rule"; |
| 315 | else if (opt_ch == 'd') |
| 316 | err = "denied by rule"; |
| 317 | else { |
| Wayne Davison | d6f0342 | 2020-07-05 22:17:09 -0700 | [diff] [blame] | 318 | const char *group = group_match >= 0 ? auth_uid_groups[group_match] : NULL; |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 319 | err = check_secret(module, line, group, challenge, pass); |
| 320 | } |
| 321 | |
| Wayne Davison | c376170 | 2019-01-08 14:46:41 -0800 | [diff] [blame] | 322 | force_memzero(challenge, sizeof challenge); |
| 323 | force_memzero(pass, strlen(pass)); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 324 | |
| 325 | if (auth_uid_groups) { |
| 326 | int j; |
| 327 | for (j = 0; j < auth_uid_groups_cnt; j++) { |
| 328 | if (auth_uid_groups[j]) |
| Wayne Davison | d6f0342 | 2020-07-05 22:17:09 -0700 | [diff] [blame] | 329 | free((char*)auth_uid_groups[j]); |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 330 | } |
| 331 | free(auth_uid_groups); |
| 332 | } |
| 333 | |
| 334 | if (err) { |
| 335 | rprintf(FLOG, "auth failed on module %s from %s (%s) for %s: %s\n", |
| 336 | lp_name(module), host, addr, line, err); |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 337 | return NULL; |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 338 | } |
| Wayne Davison | 18cc8c7 | 2004-05-08 19:37:28 +0000 | [diff] [blame] | 339 | |
| Wayne Davison | 5ebe9a4 | 2010-10-12 08:05:43 -0700 | [diff] [blame] | 340 | if (opt_ch == 'r') |
| 341 | read_only = 1; |
| 342 | else if (opt_ch == 'w') |
| 343 | read_only = 0; |
| Andrew Tridgell | d0d5639 | 1998-05-16 07:45:26 +0000 | [diff] [blame] | 344 | |
| Wayne Davison | 5037cf3 | 2005-04-09 18:11:23 +0000 | [diff] [blame] | 345 | return strdup(line); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 348 | void auth_client(int fd, const char *user, const char *challenge) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 349 | { |
| Wayne Davison | 4a19c3b | 2006-11-19 00:23:21 +0000 | [diff] [blame] | 350 | const char *pass; |
| Wayne Davison | a0456b9 | 2007-03-18 06:00:53 +0000 | [diff] [blame] | 351 | char pass2[MAX_DIGEST_LEN*2]; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 352 | |
| Wayne Davison | ef383c0 | 2004-03-31 18:52:38 +0000 | [diff] [blame] | 353 | if (!user || !*user) |
| Wayne Davison | 4b2f6a7 | 2004-04-01 18:05:40 +0000 | [diff] [blame] | 354 | user = "nobody"; |
| Wayne Davison | 7e2711b | 2022-09-10 09:43:47 -0700 | [diff] [blame] | 355 | negotiate_daemon_auth(-1, 1); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 356 | |
| Wayne Davison | 58c9b4b | 2004-05-08 19:26:53 +0000 | [diff] [blame] | 357 | if (!(pass = getpassf(password_file)) |
| 358 | && !(pass = getenv("RSYNC_PASSWORD"))) { |
| Martin Pool | 64bd756 | 2001-08-29 07:23:30 +0000 | [diff] [blame] | 359 | /* XXX: cyeoh says that getpass is deprecated, because |
| Martin Pool | 908f5a9 | 2003-06-17 04:46:32 +0000 | [diff] [blame] | 360 | * it may return a truncated password on some systems, |
| 361 | * and it is not in the LSB. |
| Wayne Davison | 8475e0e | 2020-04-05 17:00:54 -0700 | [diff] [blame] | 362 | * |
| 363 | * Andrew Klein says that getpassphrase() is present |
| 364 | * on Solaris and reads up to 256 characters. |
| 365 | * |
| 366 | * OpenBSD has a readpassphrase() that might be more suitable. |
| 367 | */ |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 368 | pass = getpass("Password: "); |
| 369 | } |
| 370 | |
| Wayne Davison | 38cab94 | 2004-05-08 18:18:42 +0000 | [diff] [blame] | 371 | if (!pass) |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 372 | pass = ""; |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 373 | |
| 374 | generate_hash(pass, challenge, pass2); |
| Andrew Tridgell | bcb7e50 | 1998-05-13 11:49:05 +0000 | [diff] [blame] | 375 | io_printf(fd, "%s %s\n", user, pass2); |
| 376 | } |