VBoxUSBHelper.cpp revision 59190ecd61435d19ba3515b876272aee7bd12298
/** $Id$ */
/** @file
* VBoxUSBHelper, setuid binary wrapper for dynamic driver alias updating.
*/
/*
* Copyright (C) 2008-2009 Oracle Corporation
*
* Oracle Corporation confidential
* All rights reserved
*/
/*
* I think it is currently a security risk using VBoxUSBHelper. By that I mean
* fashion without requiring root privileges (well setuid).
*
* Later maybe we can change this to hardcode aliases to prefix "usb" and then
* hardcode the driver as "ugen".
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <libdevice.h>
#include <errno.h>
#include <syslog.h>
typedef enum AliasOp
{
ADD_ALIAS = 12,
} AliasOp;
/**
* Print usage to stdout.
*/
{
/* @todo Eventually remove the usage syntax display & only display the copyright and warning. */
#if 0
fprintf(stderr, "Warning!! This program is internally used by VirtualBox and not meant to be used directly.\n");
#endif
exit(-1);
}
/**
* Return code legend:
* 0 - success.
* -1 - no root permission
* -2 - insufficient arguments
* -3 - abormal termination of udpate_drv (not likely)
* -4 - update_drv failed (mostly trying to add already existing alias)
*/
{
char szAlias[256];
/* Store current user ID and raise privilege. */
setuid(0);
/* For adding alias add the driver once to make sure we can update alias, no error checking needed. */
{
char szAddDrvCmd[512];
}
/* Update the alias. */
char *pszArgs[6];
int rc = 0;
pszArgs[0] = "/usr/sbin/update_drv";
if (processID < 0)
{
/* Bad. fork() failed! */
rc = -2;
}
if (processID == 0)
{
/* Child process. */
syslog(LOG_ERR, "VBoxUSBHelper: %s %s %s %s %s\n", pszArgs[0], pszArgs[1], pszArgs[2], pszArgs[3], pszArgs[4]);
_exit(1);
}
/* Parent process. */
int result;
;
rc = -3;
rc = -4;
/* Restore user ID. */
return rc;
}
/**
* Return code legend:
* 0 - success.
* -1 - no root permission
* -2 - invalid USB device path
* -3 - failed to acquire devctl_handle
* -4 - failed to reset
*/
{
/* Convert the di_node device path into a devctl compatible Ap-Id. */
int rc = 0;
if (pszPort)
{
pszPort++;
if (pszDir)
{
pszDir[0] = '\0';
/* Acquire devctl handle. */
if (pDevHandle)
{
/* Prepare to configure. */
/* Reset the device */
if (!rc)
{
if (fHardReset)
if (!rc)
{
if (fHardReset)
if (!rc)
{
if (rc)
}
else
}
else
}
else
if (rc)
rc = -4;
/* Free-up the name-value list and the obtained handle. */
}
else
{
rc = -3;
}
}
else
{
rc = -1;
}
}
else
{
rc = -1;
}
return rc;
}
/**
* returns error code from UpdateDrv. See UpdateDrv error codes.
*/
{
/* Check root permissions. */
if (geteuid() != 0)
{
return -1;
}
if (argc < 3)
{
return -2;
}
else
{
return -2;
}
/* Return status from update_drv */
}