| 1 | /* Copyright (C) 2008-2009 Peter Speck |
|---|
| 2 | * |
|---|
| 3 | * This program is free software: you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | This file implements the setuid-root helper tool. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <sys/types.h> |
|---|
| 23 | #include <sys/sysctl.h> |
|---|
| 24 | #include <openssl/md5.h> |
|---|
| 25 | #include <SystemConfiguration/SCDynamicStoreCopySpecific.h> |
|---|
| 26 | #include "GBDebug.h" |
|---|
| 27 | #import "NetworkSettingsUpdater.h" |
|---|
| 28 | |
|---|
| 29 | @interface NetworkSettingsTool : NSObject { |
|---|
| 30 | @private |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | - (id)init; |
|---|
| 34 | - (void)dealloc; |
|---|
| 35 | |
|---|
| 36 | @end |
|---|
| 37 | |
|---|
| 38 | @implementation NetworkSettingsTool |
|---|
| 39 | |
|---|
| 40 | - (id)init |
|---|
| 41 | { |
|---|
| 42 | if (!(self = [super init])) |
|---|
| 43 | return nil; |
|---|
| 44 | return self; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | - (void)dealloc |
|---|
| 48 | { |
|---|
| 49 | [super dealloc]; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | - (void)exitWithError:(NSString*)msg |
|---|
| 53 | { |
|---|
| 54 | NSLog(@"NetworkSettingsTool failed: %@", msg); |
|---|
| 55 | exit(1); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | - (void)runWithArgs:(NSArray*)args |
|---|
| 59 | { |
|---|
| 60 | if (geteuid()) { |
|---|
| 61 | [self exitWithError:[NSString stringWithFormat:@"Not running as root but with uid = %d and euid = %d", |
|---|
| 62 | getuid(), geteuid()]]; |
|---|
| 63 | } |
|---|
| 64 | if (getuid()) { |
|---|
| 65 | if (setuid(0) < 0) |
|---|
| 66 | NSLog(@"Failed setting uid to 0"); |
|---|
| 67 | if (getuid()) { |
|---|
| 68 | [self exitWithError:[NSString stringWithFormat:@"Failed to set uid to 0, is still %d, euid = %d", |
|---|
| 69 | getuid(), geteuid()]]; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | if ([args count] < 2) |
|---|
| 73 | [self exitWithError:@"No arguments given"]; |
|---|
| 74 | int port = [[args objectAtIndex:1] intValue]; |
|---|
| 75 | NSLog(@"port = %d", port); |
|---|
| 76 | NetworkSettingsUpdater* updater = [[[NetworkSettingsUpdater alloc] |
|---|
| 77 | initWithProxyPort:port |
|---|
| 78 | withWantEnable:YES] autorelease]; |
|---|
| 79 | [updater updateSettingsWithCommit:YES]; |
|---|
| 80 | NSLog(@"Network settings synchronized."); |
|---|
| 81 | exit(0); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | @end |
|---|
| 85 | |
|---|
| 86 | int main (int argc, const char * argv[]) |
|---|
| 87 | { |
|---|
| 88 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 89 | NSArray *args = [[NSProcessInfo processInfo] arguments]; |
|---|
| 90 | NetworkSettingsTool* tool = [[[NetworkSettingsTool alloc] init] retain]; |
|---|
| 91 | @try { |
|---|
| 92 | [tool runWithArgs:args]; |
|---|
| 93 | } |
|---|
| 94 | @catch (NSException* ex) { |
|---|
| 95 | NSLog(@"Stops due to exception: '%@' / '%@'", [ex name], [ex reason]); |
|---|
| 96 | exit(1); |
|---|
| 97 | } |
|---|
| 98 | [tool release]; |
|---|
| 99 | [pool release]; |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|