VirtualBox

Changeset 6866 in vbox for trunk


Ignore:
Timestamp:
Feb 8, 2008 2:19:33 PM (17 years ago)
Author:
vboxsync
Message:

updated tunctl

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Installer/linux/tunctl.c

    r4050 r6866  
    1010#include <unistd.h>
    1111#include <pwd.h>
     12#include <grp.h>
    1213#include <net/if.h>
    1314#include <sys/ioctl.h>
    1415#include <linux/if_tun.h>
    1516
     17/* TUNSETGROUP appeared in 2.6.23 */
     18#ifndef TUNSETGROUP
     19#define TUNSETGROUP   _IOW('T', 206, int)
     20#endif
     21
    1622static void Usage(char *name)
    1723{
    18   fprintf(stderr, "Create: %s [-b] [-u owner] [-t device-name] "
     24  fprintf(stderr, "Create: %s [-b] [-u owner] [-g group] [-t device-name] "
    1925          "[-f tun-clone-device]\n", name);
    20   fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n", 
     26  fprintf(stderr, "Delete: %s -d device-name [-f tun-clone-device]\n\n",
    2127          name);
    2228  fprintf(stderr, "The default tun clone device is /dev/net/tun - some systems"
     
    3036  struct ifreq ifr;
    3137  struct passwd *pw;
    32   long owner = geteuid();
     38  struct group *gr;
     39  uid_t owner = -1;
     40  gid_t group = -1;
    3341  int tap_fd, opt, delete = 0, brief = 0;
    3442  char *tun = "", *file = "/dev/net/tun", *name = argv[0], *end;
    3543
    36   while((opt = getopt(argc, argv, "bd:f:t:u:")) > 0){
     44  while((opt = getopt(argc, argv, "bd:f:t:u:g:")) > 0){
    3745    switch(opt) {
    3846      case 'b':
     
    5967        }
    6068        break;
     69      case 'g':
     70        gr = getgrnam(optarg);
     71        if(gr != NULL){
     72          group = gr->gr_gid;
     73          break;
     74        }
     75        group = strtol(optarg, &end, 0);
     76        if(*end != '\0'){
     77          fprintf(stderr, "'%s' is neither a groupname nor a numeric group.\n",
     78                  optarg);
     79          Usage(name);
     80        }
     81        break;
     82
    6183      case 't':
    6284        tun = optarg;
     
    91113  if(delete){
    92114    if(ioctl(tap_fd, TUNSETPERSIST, 0) < 0){
    93       perror("TUNSETPERSIST");
     115      perror("disabling TUNSETPERSIST");
    94116      exit(1);
    95     }   
     117    }
    96118    printf("Set '%s' nonpersistent\n", ifr.ifr_name);
    97119  }
    98120  else {
     121    /* emulate behaviour prior to TUNSETGROUP */
     122    if(owner == -1 && group == -1) {
     123      owner = geteuid();
     124    }
     125
     126    if(owner != -1) {
     127      if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
     128        perror("TUNSETOWNER");
     129        exit(1);
     130      }
     131    }
     132    if(group != -1) {
     133      if(ioctl(tap_fd, TUNSETGROUP, group) < 0){
     134        perror("TUNSETGROUP");
     135        exit(1);
     136      }
     137    }
     138
    99139    if(ioctl(tap_fd, TUNSETPERSIST, 1) < 0){
    100       perror("TUNSETPERSIST");
     140      perror("enabling TUNSETPERSIST");
    101141      exit(1);
    102142    }
    103     if(ioctl(tap_fd, TUNSETOWNER, owner) < 0){
    104       perror("TUNSETPERSIST");
    105       exit(1);
    106     }
     143
    107144    if(brief)
    108145      printf("%s\n", ifr.ifr_name);
    109     else printf("Set '%s' persistent and owned by uid %ld\n", ifr.ifr_name,
    110                 owner);
     146    else {
     147      printf("Set '%s' persistent and owned by", ifr.ifr_name);
     148      if(owner != -1)
     149          printf(" uid %d", owner);
     150      if(group != -1)
     151          printf(" gid %d", group);
     152      printf("\n");
     153    }
    111154  }
    112155  return(0);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette