5 /* (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6 /* For copying and distribution information, please see the file */
7 /* <mit-copyright.h>. */
10 static char *rcsid_get_file_c = "$Header$";
13 #include <mit-copyright.h>
18 #include <sys/param.h>
27 #define MIN(a,b) (((a) < (b))?(a):(b))
30 extern CONNECTION conn;
31 extern char buf[BUFSIZ];
33 extern int code, errno, uid;
36 extern int have_authorization, have_file, done;
37 extern C_Block session;
38 static des_key_schedule sched;
39 static des_cblock ivec;
41 static int get_block();
52 * linear checksum of bytes
55 * (initial protocol already done)
56 * <<< (int)code (can we accept the file?)
62 * >>> (STRING)data (last data block)
63 * <<< (int)code (from read, write, checksum verify)
67 * 0 for success, 1 for failure
70 * perform initial preparations and receive file as
71 * a single string, storing it into <pathname>
76 get_file(pathname, file_size, checksum, mode, encrypt)
86 if (!have_authorization) {
90 if (done) /* re-initialize data */
93 if (setuid(uid) < 0) {
95 if (setreuid(0, uid) < 0) {
97 com_err(whoami, errno, "Unable to setuid to %d\n", uid);
100 /* unlink old file */
101 if (!config_lookup("noclobber"))
102 (void) unlink(pathname);
103 /* open file descriptor */
104 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
107 sprintf(buf, "%s: creating file %s (get_file)",
108 error_message(code), pathname);
110 report_error("reporting file creation error (get_file)");
112 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
117 /* check to see if we've got the disk space */
119 while (n_written < file_size) {
120 register int n_wrote;
121 n_wrote = write(fd, buf, sizeof(buf));
124 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
125 error_message(code), pathname);
127 /* do all we can to free the space */
128 (void) unlink(pathname);
129 (void) ftruncate(fd, 0);
131 report_error("reporting test-write error (get_file)");
133 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
138 n_written += n_wrote;
142 lose("sending okay for file transfer (get_file)");
145 com_err(whoami, 0, "Session %02x %02x %02x %02x %02x %02x %02x %02x",
146 session[0], session[1], session[2], session[3],
147 session[4], session[5], session[6], session[7]);
149 des_key_sched(session, sched);
150 memcpy(ivec, session, sizeof(ivec));
153 while (n_written < file_size && code == 0) {
154 int n_got = get_block(fd, file_size - n_written, encrypt);
156 /* get_block has already printed a message */
159 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
165 if (n_written != file_size)
167 lose("receiving data");
170 code = connection_errno(conn);
171 report_error("reading file (get_file)");
173 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
179 ftruncate(fd, file_size);
183 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
186 /* validate checksum */
187 found_checksum = checksum_file(pathname);
188 if (checksum != found_checksum) {
189 code = MR_MISSINGFILE;
190 com_err(whoami, code, ": expected = %d, found = %d",
191 checksum, found_checksum);
192 report_error("checksum error");
195 /* send ack or nack */
198 code = connection_errno(conn);
199 (void) unlink(pathname);
200 lose("sending ok after file transfer (get_file)");
207 get_block(fd, max_size, encrypt)
213 unsigned char dst[UPDATE_BUFSIZ + 8], *src;
216 code = receive_object(conn, (char *)&data, STRING_T);
218 code = connection_errno(conn);
219 lose("receiving data file (get_file)");
223 src = (unsigned char *)STRING_DATA(data);
224 n = MAX_STRING_SIZE(data);
225 des_pcbc_encrypt(src, dst, n, sched, ivec, 1);
226 for (i = 0; i < 8; i++)
227 ivec[i] = src[n - 8 + i] ^ dst[n - 8 + i];
228 memcpy(STRING_DATA(data), dst, n);
231 n_read = MIN(MAX_STRING_SIZE(data), max_size);
234 register int n_wrote;
235 n_wrote = write(fd, STRING_DATA(data)+n,
239 sprintf(buf, "%s: writing file (get_file)", error_message(code));
242 report_error("reporting write error (get_file)");