annotate ie-vagrant.c @ 12:4e34b48bf7c7

fix
author taiki
date Mon, 11 Nov 2013 09:36:21 -1000
parents 422041acef4c
children 7003cdf42392
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>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
8 #include <unistd.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
9 #include <signal.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
10 #include <string.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
12 #include <sys/types.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
13 #include <regex.h>
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
14
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
15 /********************************************
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
16 * Vagrant Wrapper - Secure Yourself *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
17 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
18 * 2007 - Mike Golvach - eggi@comcast.net *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
19 * 2013 - Shinji KONO kono@ie.u-rykyu.ac.jp *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
20 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
21 * Usage: COMMAND [init|up|destroy|ssh] *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
22 * *
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
23 ********************************************/
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
24
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
25 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
26
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
27 #define command "/usr/bin/vagrant"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
28 #define init_command "init"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
29 #define up_command "up"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
30 #define destroy_command "destroy"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
31 #define ssh_command "ssh"
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
32
12
taiki
parents: 11
diff changeset
33 #define NEW(type) ((type*)malloc(sizeof(type)))
taiki
parents: 11
diff changeset
34
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
35 /* Define global variables */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
36
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
37 void
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
38 usage()
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
39 {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
40 printf("Usage: COMMAND [init|up|destroy|ssh]\n");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
41 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
42
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
43 /* main(int argc, char **argv) - main process loop */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
44
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
45 int main(int argc, char **argv)
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
46 {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
47 int gid;
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
48 int uid;
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
49
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
50 /* Set euid and egid to actual user */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
51
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
52 char *name = getlogin();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
53 uid = getuid();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
54 gid = getgid();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
55 printf("uid %d gid %d name %s\n", uid,gid,name);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
56 setegid(getgid());
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
57 seteuid(getuid());
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
58
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
59 regex_t *pattern = NEW(regex_t);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
60 if (regcomp(pattern, name, 0) != 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
61 exit(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
62 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
63
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
64 /* Confirm user is in GROUP(999) group */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
65
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
66 /*
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
67 if ( gid != 999 ) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
68 printf("User Not Authorized! Exiting...\n");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
69 exit(1);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
70 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
71 */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
72
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
73 /* Set uid, gid, euid and egid to root */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
74
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
75 setegid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
76 seteuid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
77 setgid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
78 setuid(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
79
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
80 /* Check argv for proper arguments and run
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
81 * the corresponding script, if invoked.
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
82 */
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
83
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
84 if ( strncmp(argv[1], "init", 4) == 0 ) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
85 if (execl(command, command, init_command, NULL) < 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
86 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
87 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
88 } else if ( strncmp(argv[1], "destroy", 4) == 0 ) {
12
taiki
parents: 11
diff changeset
89 if (execl(command, command, destroy_command, NULL) < 0) {
11
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
90 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
91 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
92 } else if ( strncmp(argv[1], "up", 2) == 0 ) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
93 if (execl(command, command, up_command, NULL) < 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
94 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
95 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
96 } else if ( strncmp(argv[1], "ssh", 6) == 0 ) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
97 if (execl(command, command, ssh_command, NULL) < 0) {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
98 perror("Execl:");
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
99 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
100 } else {
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
101 usage();
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
102 exit(1);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
103 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
104 exit(0);
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
105 }
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
106
422041acef4c add ie-vagrant
taiki
parents:
diff changeset
107 /* end */