]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/helper/SCHelper_client.c
configd-293.4.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / helper / SCHelper_client.c
1 /*
2 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <fcntl.h>
25 #include <paths.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <SystemConfiguration/SCPrivate.h>
39
40 #include "SCHelper_client.h"
41 #include "helper_comm.h"
42
43
44 #define HELPER "SCHelper"
45 #define HELPER_LEN (sizeof(HELPER) - 1)
46
47 #define SUFFIX_SYM "~sym"
48 #define SUFFIX_SYM_LEN (sizeof(SUFFIX_SYM) - 1)
49
50
51 __private_extern__
52 int
53 _SCHelperOpen(CFDataRef authorizationData)
54 {
55 Boolean ok;
56 int sock;
57 struct sockaddr_un sun;
58 uint32_t status = 0;
59 static int yes = 1;
60
61 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
62 perror("_SCHelperOpen socket() failed");
63 return -1;
64 }
65
66 sun.sun_family = AF_UNIX;
67 strlcpy(sun.sun_path, "/var/run/SCHelper", sizeof(sun.sun_path));
68 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
69 perror("_SCHelperOpen connect() failed");
70 close(sock);
71 return -1;
72 }
73
74 if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (const void *)&yes, sizeof(yes)) == -1) {
75 perror("_SCHelperOpen setsockopt() failed");
76 close(sock);
77 return -1;
78 }
79
80 ok = _SCHelperExec(sock, SCHELPER_MSG_AUTH, authorizationData, &status, NULL);
81 if (!ok) {
82 SCLog(TRUE, LOG_INFO, CFSTR("_SCHelperOpen: could not send authorization"));
83 close(sock);
84 return -1;
85 }
86
87 ok = (status == 0);
88 if (!ok) {
89 SCLog(TRUE, LOG_INFO, CFSTR("could not start \"" HELPER "\", status = %u"), status);
90 close(sock);
91 return -1;
92 }
93
94 return sock;
95 }
96
97
98 __private_extern__
99 void
100 _SCHelperClose(int helper)
101 {
102 if (!_SCHelperExec(helper, SCHELPER_MSG_EXIT, NULL, NULL, NULL)) {
103 SCLog(TRUE, LOG_INFO, CFSTR("_SCHelperOpen: could not send exit request"));
104 }
105
106 (void)close(helper);
107 return;
108 }