Setting tag vsys-scripts-0.95-31
[vsys-scripts.git] / fd_fusemount.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <sys/mount.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include "fdpass.h"
9
10 unsigned int rcvbuf = 16*1024*1024;
11 unsigned int arg_length = 128;
12 unsigned int mountflags = MS_NODEV | MS_NOSUID;
13
14 FILE *logfd;
15
16 void receive_argument(int control_channel_fd, char *source) {
17     int received;
18     received=recv(control_channel_fd, source, arg_length, 0);
19     if (received<arg_length) {
20         fprintf(logfd,"Error receiving arguments over the control buffer\n");
21         close(logfd);
22         exit(1);
23     }
24 }
25
26 int set_magic_fd (char *data, int new_fd) {
27     char *ptr;
28     int fd;
29
30     data[arg_length-1]='\0';
31     ptr = strstr(data,"fd=");
32     if (!ptr)
33         return -1;
34
35     // Found two fd= expressions
36     if (strstr(ptr+3,"fd="))
37         return -1;
38
39     if (*(ptr+3)!='\0') {
40         char *new_data = (char *) malloc(arg_length);
41         char *head = (char *) malloc(arg_length);
42         char *tail;
43         sscanf(ptr+3,"%d",&fd);
44         strncpy(head, data, ptr - data);
45         tail = strchr(ptr+3,',');
46         sprintf(new_data,"%sfd=%d%s",head,new_fd,tail);
47         strcpy(data,new_data);
48         free(head);
49         free(new_data);
50         return fd;
51     }
52     else
53         return -1;
54 }
55
56 void check_source(char *source) {
57     source[arg_length-1]='\0';
58     if (strchr(source,'/') || strstr(source,"..")) {
59         fprintf(logfd, "Tried mounting with source = %s\n", source);
60         close(logfd);
61         exit(1);
62     }
63 }
64
65 void check_target(char *target) {
66     target[arg_length-1]='\0';
67     if (strstr(target,"..")) {
68         fprintf(logfd,"Tried mounting with target = %s\n", target);
69         close(logfd);
70         exit(1);
71     }
72 }
73
74 void check_fstype(char *filesystemtype) {
75     if (strncmp(filesystemtype,"fuse",4)) {
76         fprintf(logfd,"Tried mounting filesystem type %s\n", filesystemtype);
77         close(logfd);
78         exit(1);
79     }
80 }
81
82 int main(int argc, char *argv[]) {
83     int control_channel_fd, magic_fd, mount_fd;
84     char source[128],target[128],filesystemtype[128],data[128],slice_target[1024];
85
86     int received;
87
88     if (argc < 3) {
89         printf("This script is called by vsys.\n");
90         exit(1);
91     }
92
93     char *slice_name = argv[1];
94
95     logfd=fopen("/tmp/fuselog","w");
96     if (!logfd) {logfd = stderr;}
97     
98     sscanf(argv[2],"%d", &control_channel_fd);
99
100     if (control_channel_fd <3 || control_channel_fd > 1023) {
101         printf ("Got control_channel_fd = %d\n", control_channel_fd);
102         exit(1);
103     }
104
105     receive_argument(control_channel_fd, source);
106     receive_argument(control_channel_fd, target);
107     receive_argument(control_channel_fd, filesystemtype);
108     receive_argument(control_channel_fd, data);
109
110     mount_fd = receive_fd (control_channel_fd);
111
112     if (mount_fd < 2) {
113         printf("mount_fd = %d\n", mount_fd);
114         exit(1);
115     }
116
117     set_magic_fd(data, mount_fd);
118
119
120     check_source(source);
121     check_target(target);
122     check_fstype(filesystemtype);
123
124     sprintf(slice_target,"/vservers/%s/%s", slice_name, target);
125
126     fprintf(logfd, "Mount fd: %d Source: %s slice_target: %s fstype: %s mountflags: %d data: %s\n", mount_fd, source, slice_target, filesystemtype, mountflags, data);
127
128     if (!mount(source, slice_target, filesystemtype, mountflags, data)) {
129         send_fd(control_channel_fd, mount_fd);
130     }
131     else {
132         printf ("Error executing mount\n");
133         exit(1);
134     }
135
136     close(logfd);
137     return 0;
138 }