| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1998 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* support rsync authentication */ |
| 20 | #include "rsync.h" |
| 21 | |
| 22 | /*************************************************************************** |
| 23 | encode a buffer using base64 - simple and slow algorithm. null terminates |
| 24 | the result. |
| 25 | ***************************************************************************/ |
| 26 | static void base64_encode(char *buf, int len, char *out) |
| 27 | { |
| 28 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 29 | int bit_offset, byte_offset, idx, i; |
| 30 | unsigned char *d = (unsigned char *)buf; |
| 31 | char *p; |
| 32 | int bytes = (len*8 + 5)/6; |
| 33 | |
| 34 | memset(out, 0, bytes+1); |
| 35 | |
| 36 | for (i=0;i<bytes;i++) { |
| 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 | } |
| 49 | } |
| 50 | |
| 51 | /* create a 16 byte challenge buffer */ |
| 52 | static void gen_challenge(char *addr, char *challenge) |
| 53 | { |
| 54 | char input[32]; |
| 55 | struct timeval tv; |
| 56 | |
| 57 | memset(input, 0, sizeof(input)); |
| 58 | |
| 59 | strncpy((char *)input, addr, 16); |
| 60 | gettimeofday(&tv, NULL); |
| 61 | 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 */ |
| 73 | static int get_secret(int module, char *user, char *secret, int len) |
| 74 | { |
| 75 | char *fname = lp_secrets_file(module); |
| 76 | int fd, found=0; |
| 77 | char line[1024]; |
| 78 | char *p, *pass; |
| 79 | |
| 80 | if (!fname || !*fname) return 0; |
| 81 | |
| 82 | fd = open(fname,O_RDONLY); |
| 83 | if (fd == -1) return 0; |
| 84 | |
| 85 | while (!found) { |
| 86 | int i = 0; |
| 87 | memset(line, 0, sizeof(line)); |
| 88 | while (i<(sizeof(line)-1)) { |
| 89 | if (read(fd, &line[i], 1) != 1) { |
| 90 | memset(line, 0, sizeof(line)); |
| 91 | close(fd); |
| 92 | return 0; |
| 93 | } |
| 94 | if (line[i] == '\r') continue; |
| 95 | if (line[i] == '\n') break; |
| 96 | i++; |
| 97 | } |
| 98 | line[i] = 0; |
| 99 | if (line[0] == '#') continue; |
| 100 | p = strchr(line,':'); |
| 101 | if (!p) continue; |
| 102 | *p = 0; |
| 103 | if (strcmp(user, line)) continue; |
| 104 | pass = p+1; |
| 105 | found = 1; |
| 106 | } |
| 107 | |
| 108 | close(fd); |
| 109 | if (!found) return 0; |
| 110 | |
| 111 | if (strlen(pass) > len-1) { |
| 112 | memset(line, 0, sizeof(line)); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | strcpy(secret, pass); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | /* generate a 16 byte hash from a password and challenge */ |
| 121 | void generate_hash(char *in, char *challenge, char *out) |
| 122 | { |
| 123 | char buf[16]; |
| 124 | |
| 125 | sum_init(); |
| 126 | sum_update(in, strlen(in)); |
| 127 | sum_update(challenge, strlen(challenge)); |
| 128 | sum_end(buf); |
| 129 | |
| 130 | base64_encode(buf, 16, out); |
| 131 | } |
| 132 | |
| 133 | /* possible negotiate authentication with the client. Use "leader" to |
| 134 | start off the auth if necessary */ |
| 135 | int auth_server(int fd, int module, char *addr, char *leader) |
| 136 | { |
| 137 | char *users = lp_auth_users(module); |
| 138 | char challenge[16]; |
| 139 | char b64_challenge[30]; |
| 140 | char line[1024]; |
| 141 | char user[100]; |
| 142 | char secret[100]; |
| 143 | char pass[30]; |
| 144 | char pass2[30]; |
| 145 | char *tok; |
| 146 | |
| 147 | /* if no auth list then allow anyone in! */ |
| 148 | if (!users || !*users) return 1; |
| 149 | |
| 150 | gen_challenge(addr, challenge); |
| 151 | |
| 152 | base64_encode(challenge, 16, b64_challenge); |
| 153 | |
| 154 | io_printf(fd,"%s%s\n", leader, b64_challenge); |
| 155 | |
| 156 | if (!read_line(fd, line, sizeof(line)-1)) { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | memset(user, 0, sizeof(user)); |
| 161 | memset(pass, 0, sizeof(pass)); |
| 162 | |
| 163 | if (sscanf(line,"%99s %29s", user, pass) != 2) { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | users = strdup(users); |
| 168 | if (!users) return 0; |
| 169 | |
| 170 | for (tok=strtok(users," ,\t"); tok; tok = strtok(NULL," ,\t")) { |
| 171 | if (strcmp(tok, user) == 0) break; |
| 172 | } |
| 173 | free(users); |
| 174 | |
| 175 | if (!tok) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | memset(secret, 0, sizeof(secret)); |
| 180 | if (!get_secret(module, user, secret, sizeof(secret)-1)) { |
| 181 | memset(secret, 0, sizeof(secret)); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | generate_hash(secret, b64_challenge, pass2); |
| 186 | memset(secret, 0, sizeof(secret)); |
| 187 | |
| 188 | return (strcmp(pass, pass2) == 0); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | void auth_client(int fd, char *user, char *challenge) |
| 193 | { |
| 194 | char *pass; |
| 195 | char pass2[30]; |
| 196 | |
| 197 | if (!user || !*user) return; |
| 198 | |
| 199 | if (!(pass=getenv("RSYNC_PASSWORD"))) { |
| 200 | pass = getpass("Password: "); |
| 201 | } |
| 202 | |
| 203 | if (!pass || !*pass) { |
| 204 | pass = ""; |
| 205 | } |
| 206 | |
| 207 | generate_hash(pass, challenge, pass2); |
| 208 | |
| 209 | io_printf(fd, "%s %s\n", user, pass2); |
| 210 | } |
| 211 | |