File: tunctl-setgroup

package info (click to toggle)
uml-utilities 20070815-1.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 740 kB
  • sloc: ansic: 3,390; perl: 1,277; makefile: 253; sh: 175; exp: 129
file content (103 lines) | stat: -rw-r--r-- 2,720 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Index: tools/tunctl/tunctl.c
===================================================================
--- tools.orig/tunctl/tunctl.c	2002-09-10 18:18:06.000000000 -0400
+++ tools/tunctl/tunctl.c	2007-08-15 11:04:19.000000000 -0400
@@ -9,13 +9,19 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <pwd.h>
+#include <grp.h>
 #include <net/if.h>
 #include <sys/ioctl.h>
 #include <linux/if_tun.h>
 
+/* TUNSETGROUP appeared in 2.6.23 */
+#ifndef TUNSETGROUP
+#define TUNSETGROUP   _IOW('T', 206, int)
+#endif
+
 static void Usage(char *name)
 {
-  fprintf(stderr, "Create: %s [-b] [-u owner] [-t device-name] "
+  fprintf(stderr, "Create: %s [-b] [-u owner] [-g group] [-t device-name] "
 	  "[-f tun-clone-device]\n", name);
   fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n", 
 	  name);
@@ -29,11 +35,13 @@ int main(int argc, char **argv)
 {
   struct ifreq ifr;
   struct passwd *pw;
-  long owner = geteuid();
+  struct group *gr;
+  uid_t owner = -1;
+  gid_t group = -1;
   int tap_fd, opt, delete = 0, brief = 0;
   char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end;
 
-  while((opt = getopt(argc, argv, "bd:f:t:u:")) > 0){
+  while((opt = getopt(argc, argv, "bd:f:t:u:g:")) > 0){
     switch(opt) {
       case 'b':
         brief = 1;
@@ -58,6 +66,20 @@ int main(int argc, char **argv)
 	  Usage(name);
 	}
         break;
+      case 'g':
+	gr = getgrnam(optarg);
+	if(gr != NULL){
+	  group = gr->gr_gid;
+	  break;
+	}
+        group = strtol(optarg, &end, 0);
+	if(*end != '\0'){
+	  fprintf(stderr, "'%s' is neither a groupname nor a numeric group.\n",
+		  optarg);
+	  Usage(name);
+	}
+        break;
+        
       case 't':
         tun = optarg;
         break;
@@ -100,14 +122,34 @@ int main(int argc, char **argv)
       perror("TUNSETPERSIST");
       exit(1);
     }
-    if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
-      perror("TUNSETPERSIST");
-      exit(1);
-    } 
+
+    /* emulate behaviour prior to TUNSETGROUP */
+    if(owner == -1 && group == -1) {
+      owner = geteuid();
+    }
+
+    if(owner != -1) {
+      if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
+      	perror("TUNSETPERSIST");
+      	exit(1);
+      }
+    }
+    if(group != -1) {
+      if(ioctl(tap_fd, TUNSETGROUP, group) < 0){
+      	perror("TUNSETPERSIST");
+      	exit(1);
+      }
+    }
     if(brief)
       printf("%s\n", ifr.ifr_name);
-    else printf("Set '%s' persistent and owned by uid %ld\n", ifr.ifr_name, 
-		owner);
+    else { 
+      printf("Set '%s' persistent and owned by", ifr.ifr_name);
+      if(owner != -1)
+          printf(" uid %d", owner);
+      if(group != -1)
+          printf(" gid %d", group);
+      printf("\n");
+    }
   }
   return(0);
 }