Debian Codegraph @

tools/idevicenotificationproxy.c

raw
1 /*
2 * idevicenotificationproxy.c
3 * Simple client for the notification_proxy service
4 *
5 * Copyright (c) 2018-2024 Nikias Bassen, All Rights Reserved.
6 * Copyright (c) 2009-2015 Martin Szulecki, All Rights Reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #define TOOL_NAME "idevicenotificationproxy"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33 #include <errno.h>
34 #include <signal.h>
35
36 #ifdef _WIN32
37 #include <windows.h>
38 #define sleep(x) Sleep(x*1000)
39 #else
40 #include <unistd.h>
41 #endif
42
43 #include <libimobiledevice/libimobiledevice.h>
44 #include <libimobiledevice/lockdown.h>
45 #include <libimobiledevice/notification_proxy.h>
46
47 enum cmd_mode {
48 CMD_NONE = 0,
49 CMD_OBSERVE,
50 CMD_POST
51 };
52
53 static int quit_flag = 0;
54
55 /**
56 * signal handler function for cleaning up properly
57 */
58 static void clean_exit(int sig)
59 {
60 fprintf(stderr, "Exiting...\n");
61 quit_flag++;
62 }
63
64 static void print_usage(int argc, char **argv, int is_error)
65 {
66 char *name = strrchr(argv[0], '/');
67 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] COMMAND\n", (name ? name + 1: argv[0]));
68 fprintf(is_error ? stderr : stdout,
69 "\n"
70 "Post or observe notifications on a device.\n"
71 "\n"
72 "Where COMMAND is one of:\n"
73 " post ID [...] post notification IDs to device and exit\n"
74 " observe ID [...] observe notification IDs in foreground until CTRL+C\n"
75 " or signal is received\n"
76 "\n"
77 "The following OPTIONS are accepted:\n"
78 " -u, --udid UDID target specific device by UDID\n"
79 " -i, --insecure use insecure notification proxy (non-paired device)\n"
80 " -n, --network connect to network device\n"
81 " -d, --debug enable communication debugging\n"
82 " -h, --help prints usage information\n"
83 " -v, --version prints version information\n"
84 "\n"
85 "Homepage: <" PACKAGE_URL ">\n"
86 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
87 );
88 }
89
90 static void notify_cb(const char *notification, void *user_data)
91 {
92 printf("> %s\n", notification);
93 }
94
95 int main(int argc, char *argv[])
96 {
97 lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
98 lockdownd_service_descriptor_t service = NULL;
99 lockdownd_client_t client = NULL;
100 idevice_t device = NULL;
101 np_client_t gnp = NULL;
102
103 int result = -1;
104 int i = 0;
105 const char* udid = NULL;
106 int use_network = 0;
107 int insecure = 0;
108 int cmd = CMD_NONE;
109 char* cmd_arg = NULL;
110
111 int count = 0;
112 char **nspec = NULL;
113 char **nspectmp = NULL;
114
115 int c = 0;
116 const struct option longopts[] = {
117 { "debug", no_argument, NULL, 'd' },
118 { "help", no_argument, NULL, 'h' },
119 { "udid", required_argument, NULL, 'u' },
120 { "insecure", no_argument, NULL, 'i' },
121 { "network", no_argument, NULL, 'n' },
122 { "version", no_argument, NULL, 'v' },
123 { NULL, 0, NULL, 0}
124 };
125
126 signal(SIGINT, clean_exit);
127 signal(SIGTERM, clean_exit);
128 #ifndef _WIN32
129 signal(SIGQUIT, clean_exit);
130 signal(SIGPIPE, SIG_IGN);
131 #endif
132
133 /* parse cmdline args */
134 while ((c = getopt_long(argc, argv, "dhu:inv", longopts, NULL)) != -1) {
135 switch (c) {
136 case 'd':
137 idevice_set_debug_level(1);
138 break;
139 case 'u':
140 if (!*optarg) {
141 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
142 print_usage(argc, argv, 1);
143 return 2;
144 }
145 udid = optarg;
146 break;
147 case 'n':
148 use_network = 1;
149 break;
150 case 'i':
151 insecure = 1;
152 break;
153 case 'h':
154 print_usage(argc, argv, 0);
155 return 0;
156 case 'v':
157 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
158 return 0;
159 default:
160 print_usage(argc, argv, 1);
161 return 2;
162 }
163 }
164 argc -= optind;
165 argv += optind;
166
167 if (!argv[i]) {
168 fprintf(stderr, "ERROR: Missing command\n");
169 print_usage(argc+optind, argv-optind, 1);
170 return 2;
171 }
172
173 if (!strcmp(argv[i], "post")) {
174 cmd = CMD_POST;
175 } else if (!strcmp(argv[i], "observe")) {
176 cmd = CMD_OBSERVE;
177 }
178
179 if (cmd == CMD_POST || cmd == CMD_OBSERVE) {
180 i++;
181 if (!argv[i]) {
182 fprintf(stderr, "ERROR: Please supply a valid notification identifier.\n");
183 print_usage(argc+optind, argv-optind, 1);
184 return 2;
185 }
186
187 count = 0;
188 nspec = malloc(sizeof(char*) * (count+1));
189
190 while(1) {
191 if (argv[i] && (strlen(argv[i]) >= 2) && (strncmp(argv[i], "-", 1) != 0)) {
192 nspectmp = realloc(nspec, sizeof(char*) * (count+1));
193 nspectmp[count] = strdup(argv[i]);
194 nspec = nspectmp;
195 count = count+1;
196 i++;
197 } else {
198 i--;
199 break;
200 }
201 }
202
203 nspectmp = realloc(nspec, sizeof(char*) * (count+1));
204 nspectmp[count] = NULL;
205 nspec = nspectmp;
206 }
207
208 /* verify options */
209 if (cmd == CMD_NONE) {
210 fprintf(stderr, "ERROR: Unsupported command '%s'\n", argv[0]);
211 print_usage(argc+optind, argv-optind, 1);
212 return 2;
213 }
214
215 if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) {
216 if (udid) {
217 printf("No device found with udid %s.\n", udid);
218 } else {
219 printf("No device found.\n");
220 }
221 goto cleanup;
222 }
223
224 if (insecure) {
225 ret = lockdownd_client_new(device, &client, TOOL_NAME);
226 } else {
227 ret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME);
228 }
229 if (LOCKDOWN_E_SUCCESS != ret) {
230 fprintf(stderr, "ERROR: Could not connect to lockdownd: %s (%d)\n", lockdownd_strerror(ret), ret);
231 goto cleanup;
232 }
233
234 ret = lockdownd_start_service(client, (insecure) ? "com.apple.mobile.insecure_notification_proxy" : NP_SERVICE_NAME, &service);
235
236 lockdownd_client_free(client);
237
238 if ((ret == LOCKDOWN_E_SUCCESS) && (service->port > 0)) {
239 if (np_client_new(device, service, &gnp) != NP_E_SUCCESS) {
240 printf("Could not connect to notification_proxy!\n");
241 result = -1;
242 } else {
243 np_set_notify_callback(gnp, notify_cb, NULL);
244
245 switch (cmd) {
246 case CMD_POST:
247 i = 0;
248 while(nspec[i] != NULL && i < (count+1)) {
249 printf("< posting \"%s\"\n", nspec[i]);
250 np_post_notification(gnp, nspec[i]);
251 i++;
252 }
253 break;
254 case CMD_OBSERVE:
255 default:
256 i = 0;
257 while(nspec[i] != NULL && i < (count+1)) {
258 printf("! observing \"%s\"\n", nspec[i]);
259 np_observe_notification(gnp, nspec[i]);
260 i++;
261 }
262
263 /* just sleep and wait for notifications */
264 while (!quit_flag) {
265 sleep(1);
266 }
267
268 break;
269 }
270
271 result = EXIT_SUCCESS;
272
273 if (gnp) {
274 np_client_free(gnp);
275 gnp = NULL;
276 }
277 }
278 } else {
279 printf("ERROR: Could not start service %s: %s\n", NP_SERVICE_NAME, lockdownd_strerror(ret));
280 }
281
282 if (service) {
283 lockdownd_service_descriptor_free(service);
284 service = NULL;
285 }
286
287 cleanup:
288 if (nspec) {
289 i = 0;
290 while(nspec[i] != NULL && i < (count+1)) {
291 free(nspec[i]);
292 i++;
293 }
294 free(nspec);
295 }
296
297 if (cmd_arg) {
298 free(cmd_arg);
299 }
300
301 if (device)
302 idevice_free(device);
303
304 return result;
305 }