(no commit message)
[vsys-scripts.git] / fd_fusemount.c
index 6280244..de05b55 100644 (file)
@@ -1,16 +1,19 @@
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <sys/mount.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
+#include <string.h>
 #include "fdpass.h"
 
 unsigned int rcvbuf = 16*1024*1024;
 unsigned int arg_length = 128;
+unsigned int mountflags = MS_NODEV | MS_NOSUID;
 
 void receive_argument(int control_channel_fd, char *source) {
     int received;
-    received=recv(control_channel_fd, source, arg_length);
+    received=recv(control_channel_fd, source, arg_length, 0);
     if (received<arg_length) {
         printf("Error receiving arguments over the control buffer\n");
         exit(1);
@@ -38,9 +41,31 @@ int get_magic_fd (char *data) {
         return -1;
 }
 
+void check_source(char *source) {
+    source[arg_length-1]='\0';
+    if (strchr(source,'/') || strstr(source,"..")) {
+        printf("Tried mounting with source = %s\n", source);
+        exit(1);
+    }
+}
+
+void check_target(char *target) {
+    source[arg_length-1]='\0';
+    if (strstr(source,"..")) {
+        printf("Tried mounting with target = %s\n", target);
+        exit(1);
+    }
+}
+
+void check_fstype(char *filesystemtype) {
+    if (strncmp(filesystemtype,"fuse",4)) {
+        printf("Tried mounting filesystem type %s\n", filesystemtype);
+    }
+}
+
 int main(int argc, char *argv[]) {
-    int control_channel_fd, magic_fd;
-    char source[128],target[128],filesystemtype[128],data[129];
+    int control_channel_fd, magic_fd, mount_fd;
+    char source[128],target[128],filesystemtype[128],data[128],slice_target[256];
 
     int received;
 
@@ -49,8 +74,15 @@ int main(int argc, char *argv[]) {
         exit(1);
     }
 
+    char *slice_name = argv[1];
+    
     sscanf(argv[2],"%d", &control_channel_fd);
 
+    if (control_channel_fd <3 || control_channel_fd > 1023) {
+        printf ("Got control_channel_fd = %d\n", control_channel_fd);
+        exit(1);
+    }
+
     receive_argument(control_channel_fd, source);
     receive_argument(control_channel_fd, target);
     receive_argument(control_channel_fd, filesystemtype);
@@ -63,6 +95,21 @@ int main(int argc, char *argv[]) {
         exit(1);
     }
 
+    mount_fd = receive_fd (control_channel_fd);
+
+    if (mount_fd != magic_fd) {
+        printf("mount_fd (%d) != magic_fd (%d)\n", mount_fd, magic_fd);
+        exit(1);
+    }
+
+    check_source(source);
+    check_target(target);
+    check_fstype(filesystemtype);
 
+    sprintf(slice_target,"/vservers/%s/%s", target);
+
+    if (!mount(source, slice_target, filesystemtype, mountflags, data)) {
+        send_fd(control_channel_fd, magic_fd);
+    }
 
 }