]> andersk Git - moira.git/blobdiff - update/get_file.c
New database and column names for Moira2.
[moira.git] / update / get_file.c
index 79baacc7710e1e292993bdd530aa0859a9bfae7c..e13f74951abcf69069c1c2b3e62a0ef02caf0ab1 100644 (file)
@@ -16,13 +16,17 @@ static char *rcsid_get_file_c = "$Header$";
 #include <ctype.h>
 #include <sys/param.h>
 #include <sys/file.h>
-#include <sms.h>
+#include <moira.h>
 #include "update.h"
 
+#ifndef MIN
+#define MIN(a,b)    (((a) < (b))?(a):(b))
+#endif /* MIN */
+
 extern CONNECTION conn;
 char buf[BUFSIZ];
 
-extern int code, errno;
+extern int code, errno, uid;
 
 extern int have_authorization, have_file, done;
 
@@ -61,30 +65,40 @@ int get_block();
  */
 
 int
-get_file(pathname, file_size, checksum)
+get_file(pathname, file_size, checksum, mode)
     char *pathname;
     int file_size;
     int checksum;
+    int mode;
 {
     int fd, n_written;
     int found_checksum;
     
     if (!have_authorization) {
-       reject_call(SMS_PERM);
+       reject_call(MR_PERM);
        return(1);
     }
     if (done)                  /* re-initialize data */
        initialize();
+    if (setreuid(0, uid) < 0) {
+       com_err(whoami, errno, "Unable to setuid to %d\n", uid);
+       exit(1);
+    }
     /* unlink old file */
-    (void) unlink(pathname);
+    if (!config_lookup("noclobber"))
+      (void) unlink(pathname);
     /* open file descriptor */
-    fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, 0700);
+    fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
     if (fd == -1) {
        code = errno;
        sprintf(buf, "%s: creating file %s (get_file)",
                error_message(code), pathname);
-       sms_log_error(buf);
+       mr_log_error(buf);
        report_error("reporting file creation error (get_file)");
+       if (setuid(0) < 0) {
+           com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
+           exit(1);
+       }
        return(1);
     }
     /* check to see if we've got the disk space */
@@ -96,12 +110,16 @@ get_file(pathname, file_size, checksum)
            code = errno;
            sprintf(buf, "%s: verifying free disk space for %s (get_file)",
                    error_message(code), pathname);
-           sms_log_error(buf);
+           mr_log_error(buf);
            /* do all we can to free the space */
            (void) unlink(pathname);
            (void) ftruncate(fd, 0);
            (void) close(fd);
            report_error("reporting test-write error (get_file)");
+           if (setuid(0) < 0) {
+               com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
+               exit(1);
+           }
            return(1);
        }
        n_written += n_wrote;
@@ -115,6 +133,10 @@ get_file(pathname, file_size, checksum)
        if (n_got == -1) {
            /* get_block has already printed a message */
            unlink(pathname);
+           if (setuid(0) < 0) {
+               com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
+               exit(1);
+           }
            return(1);
        }
        n_written += n_got;
@@ -125,16 +147,24 @@ get_file(pathname, file_size, checksum)
     if (code) {
        code = connection_errno(conn);
        report_error("reading file (get_file)");
+       if (setuid(0) < 0) {
+           com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
+           exit(1);
+       }
        return(1);
     }
     fsync(fd);
     ftruncate(fd, file_size);
     fsync(fd);
     close(fd);
+    if (setuid(0) < 0) {
+       com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
+       exit(1);
+    }
     /* validate checksum */
     found_checksum = checksum_file(pathname);
     if (checksum != found_checksum) {
-       code = SMS_MISSINGFILE;
+       code = MR_MISSINGFILE;
        com_err(whoami, code, ": expected = %d, found = %d",
                checksum, found_checksum);
        report_error("checksum error");
@@ -173,7 +203,7 @@ get_block(fd, max_size)
        if (n_wrote == -1) {
            code = errno;
            sprintf(buf, "%s: writing file (get_file)", error_message(code));
-           sms_log_error(buf);
+           mr_log_error(buf);
            string_free(&data);
            report_error("reporting write error (get_file)");
            close(fd);
This page took 0.036671 seconds and 4 git commands to generate.