annotate ie-vagrant.c @ 21:13646e64d98b

fix indent
author taiki <taiki@cr.ie.u-ryukyu.ac.jp>
date Sat, 05 Apr 2014 04:15:57 +0900
parents fb9f3738a8e6
children 66a88f51993f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
1
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
2
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
3 #include <stdlib.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
4 #include <unistd.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
5
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
6 #include <stdio.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
7 #include <sys/types.h>
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
8 #include <sys/wait.h>
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
9 #include <unistd.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
10 #include <signal.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
11 #include <string.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
12
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
13 #include <sys/types.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
14 #include <regex.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
15
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
16 #include <time.h>
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
17
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
18 /********************************************
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
19 * Vagrant Wrapper - Secure Yourself *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
20 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
21 * 2007 - Mike Golvach - eggi@comcast.net *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
22 * 2013 - Shinji KONO kono@ie.u-rykyu.ac.jp *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
23 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
24 * Usage: COMMAND [init|up|destroy|ssh] *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
25 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
26 ********************************************/
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
27
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
28 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
29
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
30 #define command "/usr/bin/vagrant"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
31 #define init_command "init"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
32 #define up_command "up"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
33 #define destroy_command "destroy"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
34 #define ssh_command "ssh"
16
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
35 #define box_command "box"
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
36
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
37 #define box_add_command "add"
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
38 #define box_list_command "list"
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
39 #define box_default_url "http://ie.u-ryukyu.ac.jp/vagrant/fedora-19.box"
16
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
40
14
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
41 #define provider_arg "--provider=kvm"
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
42
12
taiki
parents: 11
diff changeset
43 #define NEW(type) ((type*)malloc(sizeof(type)))
taiki
parents: 11
diff changeset
44
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
45 #define VAGRANT_FILE "Vagrantfile"
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
46
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
47 /* Define global variables */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
48
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
49 void
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
50 usage()
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
51 {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
52 printf("Usage: COMMAND [init|up|destroy|ssh]\n");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
53 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
54
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
55 void
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
56 exec_init(int uid, int gid)
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
57 {
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
58 pid_t pid = fork();
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
59 if (pid < 0) {
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
60 perror("fork");
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
61 exit(-1);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
62 } else if (pid == 0) {
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
63 if (execl(command, command, init_command, NULL) < 0) {
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
64 perror("Execl:");
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
65 }
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
66 } else { // grant to edit vagrantfile to user
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
67 sleep(2);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
68 if (chown(VAGRANT_FILE, uid, gid) != 0) {
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
69 printf("chown error.\n");
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
70 exit(1);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
71 }
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
72 char exec[1024];
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
73 strncpy(exec, "/usr/local/bin/change_vagrantfile.py", 1024);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
74 fprintf(stdout, "executing %s\n", exec);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
75 system(exec);
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
76 }
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
77 }
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
78
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
79 void
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
80 exec_box_add(char *box_name)
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
81 {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
82 pid_t pid = fork();
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
83 if (pid < 0) {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
84 perror("fork");
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
85 exit(-1);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
86 } else if (pid == 0) {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
87 if (execl(command, command, box_command, box_add_command, box_name, box_default_url, NULL) < 0) {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
88 perror("Execl:");
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
89 }
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
90 } else {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
91 int status = 0;
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
92 printf("wait...\n");
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
93 if (wait(&status) == -1) {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
94 perror("wait");
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
95 }
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
96 if (!WIFEXITED(status) == -1) {
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
97 perror("wait");
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
98 }
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
99 char exec[1024];
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
100 strncpy(exec, "/usr/local/bin/vagrant_newvm.py -n ", 1024);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
101 strncat(exec, box_name, 1024);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
102 fprintf(stdout, "executing %s\n", exec);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
103 system(exec);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
104 }
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
105 }
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
106
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
107 /* main(int argc, char **argv) - main process loop */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
108
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
109 int main(int argc, char **argv)
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
110 {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
111 int gid;
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
112 int uid;
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
113
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
114 /* Set euid and egid to actual user */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
115
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
116 char *name = getlogin();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
117 uid = getuid();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
118 gid = getgid();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
119 printf("uid %d gid %d name %s\n", uid,gid,name);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
120 setegid(getgid());
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
121 seteuid(getuid());
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
122
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
123 regex_t *pattern = NEW(regex_t);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
124 if (regcomp(pattern, name, 0) != 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
125 exit(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
126 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
127
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
128 /* Confirm user is in GROUP(999) group */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
129
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
130 /*
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
131 if ( gid != 999 ) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
132 printf("User Not Authorized! Exiting...\n");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
133 exit(1);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
134 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
135 */
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
136
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
137 /* Set env valiable */
14
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
138 putenv("VAGRANT_HOME=/root/.vagrant.d/");
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
139 putenv("VAGRANT_DEFAULT_PROVIDER=kvm");
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
140
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
141
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
142 /* Set uid, gid, euid and egid to root */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
143
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
144 setegid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
145 seteuid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
146 setgid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
147 setuid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
148
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
149 /* Check argv for proper arguments and run
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
150 * the corresponding script, if invoked.
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
151 */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
152
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
153 if ( strncmp(argv[1], "init", 4) == 0 ) {
15
f194adc597b6 add source to edit vagrantfile.
taiki
parents: 14
diff changeset
154 exec_init(uid, gid);
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
155 } else if ( strncmp(argv[1], "destroy", 4) == 0 ) {
12
taiki
parents: 11
diff changeset
156 if (execl(command, command, destroy_command, NULL) < 0) {
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
157 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
158 }
16
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
159 } else if ( strncmp(argv[1], "box", 3) == 0 ) {
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
160 if ( strncmp(argv[2], "add", 3) == 0 ) {
17
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
161 char box_name[1024] = "default_box";
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
162 strncpy(box_name, argv[3], 1024);
fb9f3738a8e6 add "box add" and "box list" add python script
taiki
parents: 16
diff changeset
163 exec_box_add(box_name);
16
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
164 } else if (strncmp(argv[2], "list", 4) == 0 ) {
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
165 if (execl(command, command, box_command, box_list_command, NULL) < 0) {
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
166 perror("Execl:");
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
167 }
d8ea207162ca add box command, and box add, box list
taiki
parents: 15
diff changeset
168 }
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
169 } else if ( strncmp(argv[1], "up", 2) == 0 ) {
14
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
170 if (execl(command, command, up_command, provider_arg, NULL) < 0) {
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
171 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
172 }
14
7003cdf42392 can up vagrant machine in user environment (not root)
taiki
parents: 12
diff changeset
173 } else if ( strncmp(argv[1], "ssh", 3) == 0 ) {
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
174 if (execl(command, command, ssh_command, NULL) < 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
175 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
176 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
177 } else {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
178 usage();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
179 exit(1);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
180 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
181 exit(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
182 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
183
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
184 /* end */