| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1996 |
| 3 | Copyright (C) Paul Mackerras 1996 |
| 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 | /* compatability routines for older rsync protocol versions */ |
| 21 | |
| 22 | #include "rsync.h" |
| 23 | |
| 24 | extern int am_server; |
| 25 | |
| 26 | extern int csum_length; |
| 27 | |
| 28 | extern int preserve_links; |
| 29 | extern int preserve_perms; |
| 30 | extern int preserve_devices; |
| 31 | extern int preserve_uid; |
| 32 | extern int preserve_gid; |
| 33 | extern int preserve_times; |
| 34 | extern int always_checksum; |
| 35 | extern int checksum_seed; |
| 36 | |
| 37 | |
| 38 | extern int remote_version; |
| 39 | |
| 40 | void (*send_file_entry)(struct file_struct *file,int f) = NULL; |
| 41 | void (*receive_file_entry)(struct file_struct *file, |
| 42 | unsigned char flags,int f) = NULL; |
| 43 | |
| 44 | |
| 45 | void send_file_entry_v10(struct file_struct *file,int f) |
| 46 | { |
| 47 | unsigned char flags; |
| 48 | static mode_t last_mode=0; |
| 49 | static dev_t last_rdev=0; |
| 50 | static uid_t last_uid=0; |
| 51 | static gid_t last_gid=0; |
| 52 | static char lastdir[MAXPATHLEN]=""; |
| 53 | char *p=NULL; |
| 54 | |
| 55 | if (f == -1) return; |
| 56 | |
| 57 | if (!file) { |
| 58 | write_byte(f,0); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | flags = FILE_VALID; |
| 63 | |
| 64 | if (file->mode == last_mode) flags |= SAME_MODE; |
| 65 | if (file->rdev == last_rdev) flags |= SAME_RDEV; |
| 66 | if (file->uid == last_uid) flags |= SAME_UID; |
| 67 | if (file->gid == last_gid) flags |= SAME_GID; |
| 68 | |
| 69 | if (strncmp(file->name,lastdir,strlen(lastdir)) == 0) { |
| 70 | flags |= SAME_DIR; |
| 71 | p = file->name + strlen(lastdir); |
| 72 | } else { |
| 73 | p = file->name; |
| 74 | } |
| 75 | |
| 76 | write_byte(f,flags); |
| 77 | if (flags & SAME_DIR) |
| 78 | write_byte(f,strlen(p)); |
| 79 | else |
| 80 | write_int(f,strlen(p)); |
| 81 | write_buf(f,p,strlen(p)); |
| 82 | write_int(f,(int)file->modtime); |
| 83 | write_int(f,(int)file->length); |
| 84 | if (!(flags & SAME_MODE)) |
| 85 | write_int(f,(int)file->mode); |
| 86 | if (preserve_uid && !(flags & SAME_UID)) |
| 87 | write_int(f,(int)file->uid); |
| 88 | if (preserve_gid && !(flags & SAME_GID)) |
| 89 | write_int(f,(int)file->gid); |
| 90 | if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV)) |
| 91 | write_int(f,(int)file->rdev); |
| 92 | |
| 93 | #if SUPPORT_LINKS |
| 94 | if (preserve_links && S_ISLNK(file->mode)) { |
| 95 | write_int(f,strlen(file->link)); |
| 96 | write_buf(f,file->link,strlen(file->link)); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | if (always_checksum) { |
| 101 | write_buf(f,file->sum,csum_length); |
| 102 | } |
| 103 | |
| 104 | last_mode = file->mode; |
| 105 | last_rdev = file->rdev; |
| 106 | last_uid = file->uid; |
| 107 | last_gid = file->gid; |
| 108 | p = strrchr(file->name,'/'); |
| 109 | if (p) { |
| 110 | int l = (int)(p - file->name) + 1; |
| 111 | strncpy(lastdir,file->name,l); |
| 112 | lastdir[l] = 0; |
| 113 | } else { |
| 114 | strcpy(lastdir,""); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | void receive_file_entry_v10(struct file_struct *file, |
| 121 | unsigned char flags,int f) |
| 122 | { |
| 123 | static mode_t last_mode=0; |
| 124 | static dev_t last_rdev=0; |
| 125 | static uid_t last_uid=0; |
| 126 | static gid_t last_gid=0; |
| 127 | static char lastdir[MAXPATHLEN]=""; |
| 128 | char *p=NULL; |
| 129 | int l1,l2; |
| 130 | |
| 131 | if (flags & SAME_DIR) { |
| 132 | l1 = read_byte(f); |
| 133 | l2 = strlen(lastdir); |
| 134 | } else { |
| 135 | l1 = read_int(f); |
| 136 | l2 = 0; |
| 137 | } |
| 138 | |
| 139 | file->name = (char *)malloc(l1+l2+1); |
| 140 | if (!file->name) out_of_memory("receive_file_entry"); |
| 141 | |
| 142 | strncpy(file->name,lastdir,l2); |
| 143 | read_buf(f,file->name+l2,l1); |
| 144 | file->name[l1+l2] = 0; |
| 145 | |
| 146 | file->modtime = (time_t)read_int(f); |
| 147 | file->length = (off_t)read_int(f); |
| 148 | file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f); |
| 149 | if (preserve_uid) |
| 150 | file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f); |
| 151 | if (preserve_gid) |
| 152 | file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f); |
| 153 | if (preserve_devices && IS_DEVICE(file->mode)) |
| 154 | file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f); |
| 155 | |
| 156 | #if SUPPORT_LINKS |
| 157 | if (preserve_links && S_ISLNK(file->mode)) { |
| 158 | int l = read_int(f); |
| 159 | file->link = (char *)malloc(l+1); |
| 160 | if (!file->link) out_of_memory("receive_file_entry"); |
| 161 | read_buf(f,file->link,l); |
| 162 | file->link[l] = 0; |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | if (always_checksum) |
| 167 | read_buf(f,file->sum,csum_length); |
| 168 | |
| 169 | last_mode = file->mode; |
| 170 | last_rdev = file->rdev; |
| 171 | last_uid = file->uid; |
| 172 | last_gid = file->gid; |
| 173 | p = strrchr(file->name,'/'); |
| 174 | if (p) { |
| 175 | int l = (int)(p - file->name) + 1; |
| 176 | strncpy(lastdir,file->name,l); |
| 177 | lastdir[l] = 0; |
| 178 | } else { |
| 179 | strcpy(lastdir,""); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | void setup_protocol(int f_out,int f_in) |
| 187 | { |
| 188 | if (am_server) { |
| 189 | remote_version = read_int(f_in); |
| 190 | write_int(f_out,PROTOCOL_VERSION); |
| 191 | write_flush(f_out); |
| 192 | } else { |
| 193 | write_int(f_out,PROTOCOL_VERSION); |
| 194 | write_flush(f_out); |
| 195 | remote_version = read_int(f_in); |
| 196 | } |
| 197 | |
| 198 | if (remote_version < MIN_PROTOCOL_VERSION || |
| 199 | remote_version > MAX_PROTOCOL_VERSION) { |
| 200 | fprintf(FERROR,"protocol version mismatch - is your shell clean?\n"); |
| 201 | exit_cleanup(1); |
| 202 | } |
| 203 | |
| 204 | if (remote_version == 10) { |
| 205 | send_file_entry = send_file_entry_v10; |
| 206 | receive_file_entry = receive_file_entry_v10; |
| 207 | } else { |
| 208 | send_file_entry = send_file_entry_v11; |
| 209 | receive_file_entry = receive_file_entry_v11; |
| 210 | } |
| 211 | |
| 212 | if (remote_version >= 12) { |
| 213 | if (am_server) { |
| 214 | checksum_seed = time(NULL); |
| 215 | write_int(f_out,checksum_seed); |
| 216 | } else { |
| 217 | checksum_seed = read_int(f_in); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | checksum_init(); |
| 222 | } |
| 223 | |