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>
17 #include <sys/param.h>
25 #define MIN(a,b) (((a) < (b))?(a):(b))
28 extern CONNECTION conn;
31 extern int code, errno, uid;
33 extern int have_authorization, have_file, done;
34 extern C_Block session;
35 static des_key_schedule sched;
36 static des_cblock ivec;
49 * linear checksum of bytes
52 * (initial protocol already done)
53 * <<< (int)code (can we accept the file?)
59 * >>> (STRING)data (last data block)
60 * <<< (int)code (from read, write, checksum verify)
64 * 0 for success, 1 for failure
67 * perform initial preparations and receive file as
68 * a single string, storing it into <pathname>
73 get_file(pathname, file_size, checksum, mode, encrypt)
83 if (!have_authorization) {
87 if (done) /* re-initialize data */
89 if (setreuid(0, uid) < 0) {
90 com_err(whoami, errno, "Unable to setuid to %d\n", uid);
94 if (!config_lookup("noclobber"))
95 (void) unlink(pathname);
96 /* open file descriptor */
97 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
100 sprintf(buf, "%s: creating file %s (get_file)",
101 error_message(code), pathname);
103 report_error("reporting file creation error (get_file)");
105 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
110 /* check to see if we've got the disk space */
112 while (n_written < file_size) {
113 register int n_wrote;
114 n_wrote = write(fd, buf, sizeof(buf));
117 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
118 error_message(code), pathname);
120 /* do all we can to free the space */
121 (void) unlink(pathname);
122 (void) ftruncate(fd, 0);
124 report_error("reporting test-write error (get_file)");
126 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
131 n_written += n_wrote;
135 lose("sending okay for file transfer (get_file)");
138 com_err(whoami, 0, "Session %02x %02x %02x %02x %02x %02x %02x %02x",
139 session[0], session[1], session[2], session[3],
140 session[4], session[5], session[6], session[7]);
142 des_key_sched(session, sched);
143 bcopy(session, ivec, sizeof(ivec));
146 while (n_written < file_size && code == 0) {
147 int n_got = get_block(fd, file_size - n_written, encrypt);
149 /* get_block has already printed a message */
152 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
158 if (n_written != file_size)
160 lose("receiving data");
163 code = connection_errno(conn);
164 report_error("reading file (get_file)");
166 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
172 ftruncate(fd, file_size);
176 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
179 /* validate checksum */
180 found_checksum = checksum_file(pathname);
181 if (checksum != found_checksum) {
182 code = MR_MISSINGFILE;
183 com_err(whoami, code, ": expected = %d, found = %d",
184 checksum, found_checksum);
185 report_error("checksum error");
188 /* send ack or nack */
191 code = connection_errno(conn);
192 (void) unlink(pathname);
193 lose("sending ok after file transfer (get_file)");
200 get_block(fd, max_size, encrypt)
206 unsigned char dst[UPDATE_BUFSIZ + 8], *src;
209 code = receive_object(conn, (char *)&data, STRING_T);
211 code = connection_errno(conn);
212 lose("receiving data file (get_file)");
216 src = (unsigned char *)STRING_DATA(data);
217 n = MAX_STRING_SIZE(data);
218 des_pcbc_encrypt(src, dst, n, sched, ivec, 1);
219 for (i = 0; i < 8; i++)
220 ivec[i] = src[n - 8 + i] ^ dst[n - 8 + i];
221 bcopy(dst, STRING_DATA(data), n);
224 n_read = MIN(MAX_STRING_SIZE(data), max_size);
227 register int n_wrote;
228 n_wrote = write(fd, STRING_DATA(data)+n,
232 sprintf(buf, "%s: writing file (get_file)", error_message(code));
235 report_error("reporting write error (get_file)");