diff options
Diffstat (limited to 'tools/idevicedate.c')
-rw-r--r-- | tools/idevicedate.c | 265 |
1 files changed, 169 insertions, 96 deletions
diff --git a/tools/idevicedate.c b/tools/idevicedate.c index e4e0a33..d05f63e 100644 --- a/tools/idevicedate.c +++ b/tools/idevicedate.c @@ -1,6 +1,6 @@ /* * idevicedate.c - * Simple utility to get and set the clock on an iDevice + * Simple utility to get and set the clock on a device * * Copyright (c) 2011 Martin Szulecki All Rights Reserved. * @@ -8,176 +8,249 @@ * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library 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 * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#define TOOL_NAME "idevicedate" + #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <time.h> #if HAVE_LANGINFO_CODESET -# include <langinfo.h> +#include <langinfo.h> +#endif +#ifndef WIN32 +#include <signal.h> #endif #include <libimobiledevice/libimobiledevice.h> #include <libimobiledevice/lockdown.h> #ifdef _DATE_FMT -# define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT) +#define DATE_FMT_LANGINFO nl_langinfo (_DATE_FMT) +#else +#ifdef WIN32 +#define DATE_FMT_LANGINFO "%a %b %#d %H:%M:%S %Z %Y" #else -# define DATE_FMT_LANGINFO() "" +#define DATE_FMT_LANGINFO "%a %b %e %H:%M:%S %Z %Y" +#endif #endif -static void print_usage(int argc, char **argv) +static void print_usage(int argc, char **argv, int is_error) { - char *name = NULL; - - name = strrchr(argv[0], '/'); - printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); - printf("Display the current date or set it on an iDevice.\n\n"); - printf(" -d, --debug\t\tenable communication debugging\n"); - printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); - printf(" -s, --set TIMESTAMP\tset UTC time described by TIMESTAMP\n"); - printf(" -c, --sync\t\tset time of device to current system time\n"); - printf(" -h, --help\t\tprints usage information\n"); - printf("\n"); + char *name = strrchr(argv[0], '/'); + fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); + fprintf(is_error ? stderr : stdout, + "\n" + "Display the current date or set it on a device.\n" + "\n" + "NOTE: Setting the time on iOS 6 and later is only supported\n" + " in the setup wizard screens before device activation.\n" + "\n" + "OPTIONS:\n" + " -u, --udid UDID target specific device by UDID\n" + " -n, --network connect to network device\n" + " -s, --set TIMESTAMP set UTC time described by TIMESTAMP\n" + " -c, --sync set time of device to current system time\n" + " -d, --debug enable communication debugging\n" + " -h, --help prints usage information\n" + " -v, --version prints version information\n" + "\n" + "Homepage: <" PACKAGE_URL ">\n" + "Bug Reports: <" PACKAGE_BUGREPORT ">\n" + ); } int main(int argc, char *argv[]) { lockdownd_client_t client = NULL; - idevice_t phone = NULL; + lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; + idevice_t device = NULL; idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; - int i; - char uuid[41]; + const char* udid = NULL; + int use_network = 0; time_t setdate = 0; plist_t node = NULL; - uuid[0] = 0; + int node_type = -1; uint64_t datetime = 0; time_t rawtime; struct tm * tmp; - char const *format = NULL; char buffer[80]; + int result = 0; + + int c = 0; + const struct option longopts[] = { + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, + { "udid", required_argument, NULL, 'u' }, + { "network", no_argument, NULL, 'n' }, + { "version", no_argument, NULL, 'v' }, + { "set", required_argument, NULL, 's' }, + { "sync", no_argument, NULL, 'c' }, + { NULL, 0, NULL, 0} + }; +#ifndef WIN32 + signal(SIGPIPE, SIG_IGN); +#endif /* parse cmdline args */ - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + while ((c = getopt_long(argc, argv, "dhu:nvs:c", longopts, NULL)) != -1) { + switch (c) { + case 'd': idevice_set_debug_level(1); - continue; - } - else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { - i++; - if (!argv[i] || (strlen(argv[i]) != 40)) { - print_usage(argc, argv); - return 0; + break; + case 'u': + if (!*optarg) { + fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); + print_usage(argc, argv, 1); + return 2; } - strcpy(uuid, argv[i]); - continue; - } - else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--set")) { - i++; - if (!argv[i] || (strlen(argv[i]) <= 1)) { - print_usage(argc, argv); - return 0; + udid = optarg; + break; + case 'n': + use_network = 1; + break; + case 'h': + print_usage(argc, argv, 0); + return 0; + case 'v': + printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); + return 0; + case 's': + if (!*optarg) { + fprintf(stderr, "ERROR: set argument must not be empty!\n"); + print_usage(argc, argv, 1); + return 2; } - setdate = atoi(argv[i]); + setdate = atoi(optarg); if (setdate == 0) { - printf("ERROR: Invalid timestamp value.\n"); - print_usage(argc, argv); + fprintf(stderr, "ERROR: Invalid timestamp value.\n"); + print_usage(argc, argv, 1); return 0; } - continue; - } - else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--sync")) { - i++; + break; + case 'c': /* get current time */ setdate = time(NULL); /* convert it to local time which sets timezone/daylight variables */ tmp = localtime(&setdate); /* recalculate to make it UTC */ setdate = mktime(tmp); - continue; - } - else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { - print_usage(argc, argv); - return 0; - } - else { - print_usage(argc, argv); - return 0; + break; + default: + print_usage(argc, argv, 1); + return 2; } } + argc -= optind; + argv += optind; - /* determine a date format */ - if (!format) { - format = DATE_FMT_LANGINFO (); - if (!*format) { - format = "%a %b %e %H:%M:%S %Z %Y"; + ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); + if (ret != IDEVICE_E_SUCCESS) { + if (udid) { + printf("No device found with udid %s.\n", udid); + } else { + printf("No device found.\n"); } + return -1; } - if (uuid[0] != 0) { - ret = idevice_new(&phone, uuid); - if (ret != IDEVICE_E_SUCCESS) { - printf("No device found with uuid %s, is it plugged in?\n", uuid); - return -1; - } + if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) { + fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret); + result = -1; + goto cleanup; } - else - { - ret = idevice_new(&phone, NULL); - if (ret != IDEVICE_E_SUCCESS) { - printf("No device found, is it plugged in?\n"); - return -1; - } + + if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) != LOCKDOWN_E_SUCCESS) { + fprintf(stderr, "ERROR: Unable to retrieve 'TimeIntervalSince1970' node from device.\n"); + result = -1; + goto cleanup; } - if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicedate")) { - idevice_free(phone); - return -1; + if (node == NULL) { + fprintf(stderr, "ERROR: Empty node for 'TimeIntervalSince1970' received.\n"); + result = -1; + goto cleanup; } + node_type = plist_get_node_type(node); + /* get or set? */ if (setdate == 0) { /* get time value from device */ - if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) == LOCKDOWN_E_SUCCESS) { - if (node) { + switch (node_type) { + case PLIST_UINT: plist_get_uint_val(node, &datetime); - plist_free(node); - node = NULL; + break; + case PLIST_REAL: + { + double rv = 0; + plist_get_real_val(node, &rv); + datetime = rv; + } + break; + default: + fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n"); + break; + } + plist_free(node); + node = NULL; - /* date/time calculations */ - rawtime = (time_t)datetime; - tmp = localtime(&rawtime); + /* date/time calculations */ + rawtime = (time_t)datetime; + tmp = localtime(&rawtime); - /* finally we format and print the current date */ - strftime(buffer, 80, format, tmp); - puts(buffer); - } - } + /* finally we format and print the current date */ + strftime(buffer, 80, DATE_FMT_LANGINFO, tmp); + puts(buffer); } else { datetime = setdate; - if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", plist_new_uint(datetime)) == LOCKDOWN_E_SUCCESS) { + plist_free(node); + node = NULL; + + switch (node_type) { + case PLIST_UINT: + node = plist_new_uint(datetime); + break; + case PLIST_REAL: + node = plist_new_real((double)datetime); + break; + default: + fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n"); + break; + } + + if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", node) == LOCKDOWN_E_SUCCESS) { tmp = localtime(&setdate); - strftime(buffer, 80, format, tmp); + strftime(buffer, 80, DATE_FMT_LANGINFO, tmp); puts(buffer); } else { printf("ERROR: Failed to set date on device.\n"); } + node = NULL; } - lockdownd_client_free(client); - idevice_free(phone); +cleanup: + if (client) + lockdownd_client_free(client); - return 0; -} + if (device) + idevice_free(device); + return result; +} |