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;
33 extern int code, errno, uid;
35 extern int have_authorization, have_file, done;
36 extern C_Block session;
37 static des_key_schedule sched;
38 static des_cblock ivec;
51 * linear checksum of bytes
54 * (initial protocol already done)
55 * <<< (int)code (can we accept the file?)
61 * >>> (STRING)data (last data block)
62 * <<< (int)code (from read, write, checksum verify)
66 * 0 for success, 1 for failure
69 * perform initial preparations and receive file as
70 * a single string, storing it into <pathname>
75 get_file(pathname, file_size, checksum, mode, encrypt)
85 if (!have_authorization) {
89 if (done) /* re-initialize data */
92 if (setuid(uid) < 0) {
94 if (setreuid(0, uid) < 0) {
96 com_err(whoami, errno, "Unable to setuid to %d\n", uid);
100 if (!config_lookup("noclobber"))
101 (void) unlink(pathname);
102 /* open file descriptor */
103 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
106 sprintf(buf, "%s: creating file %s (get_file)",
107 error_message(code), pathname);
109 report_error("reporting file creation error (get_file)");
111 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
116 /* check to see if we've got the disk space */
118 while (n_written < file_size) {
119 register int n_wrote;
120 n_wrote = write(fd, buf, sizeof(buf));
123 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
124 error_message(code), pathname);
126 /* do all we can to free the space */
127 (void) unlink(pathname);
128 (void) ftruncate(fd, 0);
130 report_error("reporting test-write error (get_file)");
132 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
137 n_written += n_wrote;
141 lose("sending okay for file transfer (get_file)");
144 com_err(whoami, 0, "Session %02x %02x %02x %02x %02x %02x %02x %02x",
145 session[0], session[1], session[2], session[3],
146 session[4], session[5], session[6], session[7]);
148 des_key_sched(session, sched);
149 memcpy(ivec, session, sizeof(ivec));
152 while (n_written < file_size && code == 0) {
153 int n_got = get_block(fd, file_size - n_written, encrypt);
155 /* get_block has already printed a message */
158 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
164 if (n_written != file_size)
166 lose("receiving data");
169 code = connection_errno(conn);
170 report_error("reading file (get_file)");
172 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
178 ftruncate(fd, file_size);
182 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
185 /* validate checksum */
186 found_checksum = checksum_file(pathname);
187 if (checksum != found_checksum) {
188 code = MR_MISSINGFILE;
189 com_err(whoami, code, ": expected = %d, found = %d",
190 checksum, found_checksum);
191 report_error("checksum error");
194 /* send ack or nack */
197 code = connection_errno(conn);
198 (void) unlink(pathname);
199 lose("sending ok after file transfer (get_file)");
206 get_block(fd, max_size, encrypt)
212 unsigned char dst[UPDATE_BUFSIZ + 8], *src;
215 code = receive_object(conn, (char *)&data, STRING_T);
217 code = connection_errno(conn);
218 lose("receiving data file (get_file)");
222 src = (unsigned char *)STRING_DATA(data);
223 n = MAX_STRING_SIZE(data);
224 des_pcbc_encrypt(src, dst, n, sched, ivec, 1);
225 for (i = 0; i < 8; i++)
226 ivec[i] = src[n - 8 + i] ^ dst[n - 8 + i];
227 memcpy(STRING_DATA(data), dst, n);
230 n_read = MIN(MAX_STRING_SIZE(data), max_size);
233 register int n_wrote;
234 n_wrote = write(fd, STRING_DATA(data)+n,
238 sprintf(buf, "%s: writing file (get_file)", error_message(code));
241 report_error("reporting write error (get_file)");