Several BugFixes (see bug-reports from "accounts")
[vde.git] / vde-2 / src / vde_tunctl.c
blobe9f7a337c5836a7abc2b5dcf9a034f51340a2960
1 /* Copyright 2002 Jeff Dike
2 * Licensed under the GPL
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <pwd.h>
12 #include <grp.h>
13 #include <net/if.h>
14 #include <sys/ioctl.h>
15 #include <linux/if_tun.h>
17 /* TUNSETGROUP appeared in 2.6.23 */
18 #ifndef TUNSETGROUP
19 #define TUNSETGROUP _IOW('T', 206, int)
20 #endif
22 static void Usage(char *name)
24 fprintf(stderr, "Create: %s [-b] [-u owner] [-g group] [-n] [-t device-name] "
25 "[-f tun-clone-device]\n", name);
26 fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
27 name);
28 fprintf(stderr, "The default tun clone device is /dev/net/tun - some systems"
29 " use\n/dev/misc/net/tun instead\n\n");
30 fprintf(stderr, "-b will result in brief output (just the device name)\n");
31 fprintf(stderr, "-n create a tun interface (not needed if the device name prefix is tun\n");
32 exit(1);
35 int main(int argc, char **argv)
37 struct ifreq ifr;
38 struct passwd *pw;
39 struct group *gr;
40 uid_t owner = -1;
41 gid_t group = -1;
42 int tap_fd, opt, delete = 0, brief = 0;
43 int type=IFF_TAP;
45 char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end;
47 while((opt = getopt(argc, argv, "bd:f:t:u:in")) > 0){
48 switch(opt) {
49 case 'b':
50 brief = 1;
51 break;
52 case 'd':
53 delete = 1;
54 tun = optarg;
55 break;
56 case 'f':
57 file = optarg;
58 break;
59 case 'u':
60 pw = getpwnam(optarg);
61 if(pw != NULL){
62 owner = pw->pw_uid;
63 break;
65 owner = strtol(optarg, &end, 0);
66 if(*end != '\0'){
67 fprintf(stderr, "'%s' is neither a username nor a numeric uid.\n",
68 optarg);
69 Usage(name);
71 break;
72 case 'g':
73 gr = getgrnam(optarg);
74 if(gr != NULL){
75 group = gr->gr_gid;
76 break;
78 group = strtol(optarg, &end, 0);
79 if(*end != '\0'){
80 fprintf(stderr, "'%s' is neither a groupname nor a numeric group.\n",
81 optarg);
82 Usage(name);
84 break;
85 case 't':
86 tun = optarg;
87 break;
88 case 'n':
89 type = IFF_TUN;
90 break;
91 case 'h':
92 default:
93 Usage(name);
97 argv += optind;
98 argc -= optind;
100 if(argc > 0)
101 Usage(name);
103 if((tap_fd = open(file, O_RDWR)) < 0){
104 fprintf(stderr, "Failed to open '%s' : ", file);
105 perror("");
106 exit(1);
109 memset(&ifr, 0, sizeof(ifr));
111 if (strncmp(tun,"tun",3)==0) type=IFF_TUN;
112 ifr.ifr_flags = type | IFF_NO_PI;
113 strncpy(ifr.ifr_name, tun, sizeof(ifr.ifr_name) - 1);
114 if(ioctl(tap_fd, TUNSETIFF, (void *) &ifr) < 0){
115 perror("TUNSETIFF");
116 exit(1);
119 if(delete){
120 if(ioctl(tap_fd, TUNSETPERSIST, 0) < 0){
121 perror("TUNSETPERSIST");
122 exit(1);
124 printf("Set '%s' nonpersistent\n", ifr.ifr_name);
126 else {
127 /* emulate behaviour prior to TUNSETGROUP */
128 if(owner == -1 && group == -1) {
129 owner = geteuid();
132 if(owner != -1) {
133 if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
134 perror("TUNSETOWNER");
135 exit(1);
138 if(group != -1) {
139 if(ioctl(tap_fd, TUNSETGROUP, group) < 0){
140 perror("TUNSETGROUP");
141 exit(1);
145 if(ioctl(tap_fd, TUNSETPERSIST, 1) < 0){
146 perror("TUNSETPERSIST");
147 exit(1);
149 if(brief)
150 printf("%s\n", ifr.ifr_name);
151 else {
152 printf("Set '%s' persistent and owned by", ifr.ifr_name);
153 if(owner != -1)
154 printf(" uid %d", owner);
155 if(group != -1)
156 printf(" gid %d", group);
157 printf("\n");
160 return(0);