1 /*
2  * idevicename.c
3  * Simple utility to get or set the device name
4  *
5  * Copyright (c) 2014  Nikias Bassen, All Rights Reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #define TOOL_NAME "idevicename"
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <getopt.h>
33 #ifndef WIN32
34 #include <signal.h>
35 #endif
36 
37 #include <libimobiledevice/libimobiledevice.h>
38 #include <libimobiledevice/lockdown.h>
39 
print_usage(void)40 static void print_usage(void)
41 {
42 	printf("Usage: idevicename [OPTIONS] [NAME]\n");
43 	printf("\n");
44 	printf("Display the device name or set it to NAME if specified.\n");
45 	printf("\n");
46 	printf("OPTIONS:\n");
47 	printf("  -u, --udid UDID\ttarget specific device by UDID\n");
48 	printf("  -n, --network\t\tconnect to network device\n");
49 	printf("  -d, --debug\t\tenable communication debugging\n");
50 	printf("  -h, --help\t\tprint usage information\n");
51 	printf("  -v, --version\t\tprint version information\n");
52 	printf("\n");
53 	printf("Homepage:    <" PACKAGE_URL ">\n");
54 	printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n");
55 }
56 
main(int argc,char ** argv)57 int main(int argc, char** argv)
58 {
59 	int c = 0;
60 	const struct option longopts[] = {
61 		{ "udid",    required_argument, NULL, 'u' },
62 		{ "network", no_argument,       NULL, 'n' },
63 		{ "debug",   no_argument,       NULL, 'd' },
64 		{ "help",    no_argument,       NULL, 'h' },
65 		{ "version", no_argument,       NULL, 'v' },
66 		{ NULL, 0, NULL, 0}
67 	};
68 	int res = -1;
69 	const char* udid = NULL;
70 	int use_network = 0;
71 
72 #ifndef WIN32
73 	signal(SIGPIPE, SIG_IGN);
74 #endif
75 
76 	while ((c = getopt_long(argc, argv, "du:hnv", longopts, NULL)) != -1) {
77 		switch (c) {
78 		case 'u':
79 			if (!*optarg) {
80 				fprintf(stderr, "ERROR: UDID must not be empty!\n");
81 				print_usage();
82 				exit(2);
83 			}
84 			udid = optarg;
85 			break;
86 		case 'n':
87 			use_network = 1;
88 			break;
89 		case 'h':
90 			print_usage();
91 			return 0;
92 		case 'd':
93 			idevice_set_debug_level(1);
94 			break;
95 		case 'v':
96 			printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
97 			return 0;
98 		default:
99 			print_usage();
100 			return 2;
101 		}
102 	}
103 
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc > 1) {
108 		print_usage();
109 		return -1;
110 	}
111 
112 	idevice_t device = NULL;
113 	if (idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX) != IDEVICE_E_SUCCESS) {
114 		if (udid) {
115 			fprintf(stderr, "ERROR: No device found with udid %s.\n", udid);
116 		} else {
117 			fprintf(stderr, "ERROR: No device found.\n");
118 		}
119 		return -1;
120 	}
121 
122 	lockdownd_client_t lockdown = NULL;
123 	lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, TOOL_NAME);
124 	if (lerr != LOCKDOWN_E_SUCCESS) {
125 		idevice_free(device);
126 		fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", lerr);
127 		return -1;
128 	}
129 
130 	if (argc == 0) {
131 		// getting device name
132 		char* name = NULL;
133 		lerr = lockdownd_get_device_name(lockdown, &name);
134 		if (name) {
135 			printf("%s\n", name);
136 			free(name);
137 			res = 0;
138 		} else {
139 			fprintf(stderr, "ERROR: Could not get device name, lockdown error %d\n", lerr);
140 		}
141 	} else {
142 		// setting device name
143 		lerr = lockdownd_set_value(lockdown, NULL, "DeviceName", plist_new_string(argv[0]));
144 		if (lerr == LOCKDOWN_E_SUCCESS) {
145 			printf("device name set to '%s'\n", argv[0]);
146 			res = 0;
147 		} else {
148 			fprintf(stderr, "ERROR: Could not set device name, lockdown error %d\n", lerr);
149 		}
150 	}
151 
152 	lockdownd_client_free(lockdown);
153 	idevice_free(device);
154 
155 	return res;
156 }
157