diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/Makefile.am | 7 | ||||
| -rw-r--r-- | tools/idevicedate.c | 201 |
2 files changed, 207 insertions, 1 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am index f274084..0a47fdc 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am | |||
| @@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include | |||
| 3 | AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) | 3 | AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) |
| 4 | AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) | 4 | AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) |
| 5 | 5 | ||
| 6 | bin_PROGRAMS = idevice_id ideviceinfo idevicepair idevicesyslog idevicebackup ideviceimagemounter idevicescreenshot ideviceenterrecovery | 6 | bin_PROGRAMS = idevice_id ideviceinfo idevicepair idevicesyslog idevicebackup ideviceimagemounter idevicescreenshot ideviceenterrecovery idevicedate |
| 7 | 7 | ||
| 8 | ideviceinfo_SOURCES = ideviceinfo.c | 8 | ideviceinfo_SOURCES = ideviceinfo.c |
| 9 | ideviceinfo_CFLAGS = $(AM_CFLAGS) | 9 | ideviceinfo_CFLAGS = $(AM_CFLAGS) |
| @@ -44,3 +44,8 @@ ideviceenterrecovery_SOURCES = ideviceenterrecovery.c | |||
| 44 | ideviceenterrecovery_CFLAGS = $(AM_CFLAGS) | 44 | ideviceenterrecovery_CFLAGS = $(AM_CFLAGS) |
| 45 | ideviceenterrecovery_LDFLAGS = $(AM_LDFLAGS) | 45 | ideviceenterrecovery_LDFLAGS = $(AM_LDFLAGS) |
| 46 | ideviceenterrecovery_LDADD = ../src/libimobiledevice.la | 46 | ideviceenterrecovery_LDADD = ../src/libimobiledevice.la |
| 47 | |||
| 48 | idevicedate_SOURCES = idevicedate.c | ||
| 49 | idevicedate_CFLAGS = $(AM_CFLAGS) | ||
| 50 | idevicedate_LDFLAGS = $(AM_LDFLAGS) | ||
| 51 | idevicedate_LDADD = ../src/libimobiledevice.la | ||
diff --git a/tools/idevicedate.c b/tools/idevicedate.c new file mode 100644 index 0000000..b3e7334 --- /dev/null +++ b/tools/idevicedate.c | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /* | ||
| 2 | * idevicedate.c | ||
| 3 | * Simple utility to get and set the clock on an iDevice | ||
| 4 | * | ||
| 5 | * Copyright (c) 2011 Martin Szulecki 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 | #include <stdio.h> | ||
| 23 | #include <stdlib.h> | ||
| 24 | #include <string.h> | ||
| 25 | #include <time.h> | ||
| 26 | #if HAVE_LANGINFO_CODESET | ||
| 27 | # include <langinfo.h> | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #include <libimobiledevice/libimobiledevice.h> | ||
| 31 | #include <libimobiledevice/lockdown.h> | ||
| 32 | |||
| 33 | #ifdef _DATE_FMT | ||
| 34 | # define DATE_FMT_LANGINFO() nl_langinfo (_DATE_FMT) | ||
| 35 | #else | ||
| 36 | # define DATE_FMT_LANGINFO() "" | ||
| 37 | #endif | ||
| 38 | |||
| 39 | static void print_usage(int argc, char **argv) | ||
| 40 | { | ||
| 41 | char *name = NULL; | ||
| 42 | |||
| 43 | name = strrchr(argv[0], '/'); | ||
| 44 | printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); | ||
| 45 | printf("Display the current date or set it on an iDevice.\n\n"); | ||
| 46 | printf(" -d, --debug\t\tenable communication debugging\n"); | ||
| 47 | printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); | ||
| 48 | printf(" -s, --set TIMESTAMP\tset UTC time described by TIMESTAMP\n"); | ||
| 49 | printf(" -c, --sync\t\tset time of device to current system time\n"); | ||
| 50 | printf(" -h, --help\t\tprints usage information\n"); | ||
| 51 | printf("\n"); | ||
| 52 | } | ||
| 53 | |||
| 54 | int main(int argc, char *argv[]) | ||
| 55 | { | ||
| 56 | lockdownd_client_t client = NULL; | ||
| 57 | idevice_t phone = NULL; | ||
| 58 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | ||
| 59 | int i; | ||
| 60 | char uuid[41]; | ||
| 61 | time_t setdate = 0; | ||
| 62 | plist_t node = NULL; | ||
| 63 | uuid[0] = 0; | ||
| 64 | uint64_t datetime = 0; | ||
| 65 | time_t rawtime; | ||
| 66 | double offset_from_utc = 0.0; | ||
| 67 | struct tm * tmp; | ||
| 68 | char const *format = NULL; | ||
| 69 | char buffer[80]; | ||
| 70 | int tzshift = 0; | ||
| 71 | |||
| 72 | /* parse cmdline args */ | ||
| 73 | for (i = 1; i < argc; i++) { | ||
| 74 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | ||
| 75 | idevice_set_debug_level(1); | ||
| 76 | continue; | ||
| 77 | } | ||
| 78 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { | ||
| 79 | i++; | ||
| 80 | if (!argv[i] || (strlen(argv[i]) != 40)) { | ||
| 81 | print_usage(argc, argv); | ||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | strcpy(uuid, argv[i]); | ||
| 85 | continue; | ||
| 86 | } | ||
| 87 | else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--set")) { | ||
| 88 | i++; | ||
| 89 | if (!argv[i] || (strlen(argv[i]) <= 1)) { | ||
| 90 | print_usage(argc, argv); | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | setdate = atoi(argv[i]); | ||
| 94 | if (setdate == 0) { | ||
| 95 | printf("ERROR: Invalid timestamp value.\n"); | ||
| 96 | print_usage(argc, argv); | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | tzshift = 1; | ||
| 100 | continue; | ||
| 101 | } | ||
| 102 | else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--sync")) { | ||
| 103 | i++; | ||
| 104 | /* get current time */ | ||
| 105 | setdate = time(NULL); | ||
| 106 | /* convert it to local time which sets timezone/daylight variables */ | ||
| 107 | tmp = localtime(&setdate); | ||
| 108 | /* recalculate to make it UTC */ | ||
| 109 | setdate = mktime(tmp) - timezone - (daylight ? 3600 : 0 ); | ||
| 110 | tzshift = 0; | ||
| 111 | continue; | ||
| 112 | } | ||
| 113 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | ||
| 114 | print_usage(argc, argv); | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | print_usage(argc, argv); | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | /* determine a date format */ | ||
| 124 | if (!format) { | ||
| 125 | format = DATE_FMT_LANGINFO (); | ||
| 126 | if (!*format) { | ||
| 127 | format = "%a %b %e %H:%M:%S %Z %Y"; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | if (uuid[0] != 0) { | ||
| 132 | ret = idevice_new(&phone, uuid); | ||
| 133 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 134 | printf("No device found with uuid %s, is it plugged in?\n", uuid); | ||
| 135 | return -1; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | else | ||
| 139 | { | ||
| 140 | ret = idevice_new(&phone, NULL); | ||
| 141 | if (ret != IDEVICE_E_SUCCESS) { | ||
| 142 | printf("No device found, is it plugged in?\n"); | ||
| 143 | return -1; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicedate")) { | ||
| 148 | idevice_free(phone); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* read timezone offset of device, needed for conversions */ | ||
| 153 | if (lockdownd_get_value(client, NULL, "TimeZoneOffsetFromUTC", &node) == LOCKDOWN_E_SUCCESS) { | ||
| 154 | if (node) { | ||
| 155 | plist_get_real_val(node, &offset_from_utc); | ||
| 156 | plist_free(node); | ||
| 157 | node = NULL; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | /* get or set? */ | ||
| 162 | if (setdate == 0) { | ||
| 163 | /* get time value from device */ | ||
| 164 | if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) == LOCKDOWN_E_SUCCESS) { | ||
| 165 | if (node) { | ||
| 166 | plist_get_uint_val(node, &datetime); | ||
| 167 | plist_free(node); | ||
| 168 | node = NULL; | ||
| 169 | |||
| 170 | /* date/time calculations */ | ||
| 171 | rawtime = (time_t)datetime; | ||
| 172 | tmp = localtime(&rawtime); | ||
| 173 | tmp->tm_gmtoff = offset_from_utc; | ||
| 174 | |||
| 175 | /* finally we format and print the current date */ | ||
| 176 | strftime(buffer, 80, format, tmp); | ||
| 177 | puts(buffer); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } else { | ||
| 181 | if (tzshift) { | ||
| 182 | /* if we had provided a timestamp and have to adjust according to the device's timezone */ | ||
| 183 | setdate = setdate - offset_from_utc; | ||
| 184 | } | ||
| 185 | datetime = setdate; | ||
| 186 | |||
| 187 | if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", plist_new_uint(datetime)) == LOCKDOWN_E_SUCCESS) { | ||
| 188 | tmp = localtime(&setdate); | ||
| 189 | strftime(buffer, 80, format, tmp); | ||
| 190 | puts(buffer); | ||
| 191 | } else { | ||
| 192 | printf("ERROR: Failed to set date on device.\n"); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | lockdownd_client_free(client); | ||
| 197 | idevice_free(phone); | ||
| 198 | |||
| 199 | return 0; | ||
| 200 | } | ||
| 201 | |||
