2013-05-01 10:54:20 +00:00
|
|
|
/*
|
|
|
|
* runtool.m - Run an external program as root for networking
|
|
|
|
* Copyright (C) 2010, Daniel Sumorok
|
|
|
|
*
|
|
|
|
* Basilisk II (C) 1997-2008 Christian Bauer
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-05-01 00:46:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
|
2013-05-25 14:48:15 +00:00
|
|
|
FILE * run_tool(const char *if_name, const char *tool_name);
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-25 14:48:15 +00:00
|
|
|
FILE * run_tool(const char *if_name, const char *tool_name)
|
2013-05-05 00:37:29 +00:00
|
|
|
{
|
2013-05-05 00:36:11 +00:00
|
|
|
OSStatus auth_status;
|
2013-05-25 14:48:15 +00:00
|
|
|
FILE *fp = NULL;
|
2013-05-29 23:42:59 +00:00
|
|
|
char *args[] = {NULL, NULL, NULL};
|
2013-05-25 14:48:15 +00:00
|
|
|
char path_buffer[256];
|
2013-05-05 00:36:11 +00:00
|
|
|
AuthorizationFlags auth_flags;
|
|
|
|
AuthorizationRef auth_ref;
|
|
|
|
AuthorizationItem auth_items[1];
|
|
|
|
AuthorizationRights auth_rights;
|
2013-05-25 14:48:15 +00:00
|
|
|
CFBundleRef bundle_ref;
|
|
|
|
CFURLRef url_ref;
|
|
|
|
CFStringRef path_str;
|
|
|
|
CFStringRef tool_name_str;
|
2013-05-26 02:01:21 +00:00
|
|
|
char c;
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-25 14:48:15 +00:00
|
|
|
bundle_ref = CFBundleGetMainBundle();
|
|
|
|
if(bundle_ref == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tool_name_str = CFStringCreateWithCString(NULL, tool_name,
|
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
|
|
|
|
url_ref = CFBundleCopyResourceURL(bundle_ref, tool_name_str,
|
|
|
|
NULL, NULL);
|
2013-05-26 02:01:21 +00:00
|
|
|
CFRelease(tool_name_str);
|
2013-05-25 14:48:15 +00:00
|
|
|
|
|
|
|
if(url_ref == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
path_str = CFURLCopyFileSystemPath(url_ref, kCFURLPOSIXPathStyle);
|
|
|
|
CFRelease(url_ref);
|
|
|
|
|
|
|
|
if(path_str == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-25 14:48:15 +00:00
|
|
|
if(!CFStringGetCString(path_str, path_buffer, sizeof(path_buffer),
|
|
|
|
kCFStringEncodingUTF8)) {
|
|
|
|
CFRelease(path_str);
|
2013-05-01 00:46:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-05-25 14:48:15 +00:00
|
|
|
CFRelease(path_str);
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-25 14:48:15 +00:00
|
|
|
args[0] = (char *)tool_name;
|
|
|
|
args[1] = (char *)if_name;
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
auth_flags = kAuthorizationFlagExtendRights |
|
2013-05-01 00:46:31 +00:00
|
|
|
kAuthorizationFlagInteractionAllowed |
|
|
|
|
kAuthorizationFlagPreAuthorize;
|
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
auth_items[0].name = "system.privilege.admin";
|
|
|
|
auth_items[0].valueLength = 0;
|
|
|
|
auth_items[0].value = NULL;
|
|
|
|
auth_items[0].flags = 0;
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
auth_rights.count = sizeof (auth_items) / sizeof (auth_items[0]);
|
|
|
|
auth_rights.items = auth_items;
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
auth_status = AuthorizationCreate(&auth_rights,
|
|
|
|
kAuthorizationEmptyEnvironment,
|
|
|
|
auth_flags,
|
|
|
|
&auth_ref);
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
if (auth_status != errAuthorizationSuccess) {
|
2013-05-25 14:48:15 +00:00
|
|
|
fprintf(stderr, "%s: AuthorizationCreate() failed.\n",
|
|
|
|
__func__);
|
2013-05-01 00:46:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
auth_status = AuthorizationExecuteWithPrivileges(auth_ref,
|
2013-05-25 14:48:15 +00:00
|
|
|
path_buffer,
|
2013-05-05 00:36:11 +00:00
|
|
|
kAuthorizationFlagDefaults,
|
|
|
|
args + 1,
|
|
|
|
&fp);
|
2013-05-01 00:46:31 +00:00
|
|
|
|
2013-05-05 00:36:11 +00:00
|
|
|
if (auth_status != errAuthorizationSuccess) {
|
2013-05-25 14:48:15 +00:00
|
|
|
fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n",
|
|
|
|
__func__);
|
2013-05-01 00:46:31 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-26 02:01:21 +00:00
|
|
|
if(fread(&c, 1, 1, fp) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-01 00:46:31 +00:00
|
|
|
return fp;
|
|
|
|
}
|