| 1 |
/* |
| 2 |
* idevicedate.c |
| 3 |
* Simple utility to get and set the clock on a device |
| 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 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#define TOOL_NAME "idevicedate" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <getopt.h> |
| 32 |
#include <time.h> |
| 33 |
#if HAVE_LANGINFO_CODESET |
| 34 |
#include <langinfo.h> |
| 35 |
#endif |
| 36 |
#ifndef _WIN32 |
| 37 |
#include <signal.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <libimobiledevice/libimobiledevice.h> |
| 41 |
#include <libimobiledevice/lockdown.h> |
| 42 |
|
| 43 |
#ifdef _DATE_FMT |
| 44 |
#define DATE_FMT_LANGINFO nl_langinfo (_DATE_FMT) |
| 45 |
#else |
| 46 |
#ifdef _WIN32 |
| 47 |
#define DATE_FMT_LANGINFO "%a %b %#d %H:%M:%S %Z %Y" |
| 48 |
#else |
| 49 |
#define DATE_FMT_LANGINFO "%a %b %e %H:%M:%S %Z %Y" |
| 50 |
#endif |
| 51 |
#endif |
| 52 |
|
| 53 |
static void print_usage(int argc, char **argv, int is_error) |
| 54 |
{ |
| 55 |
char *name = strrchr(argv[0], '/'); |
| 56 |
fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); |
| 57 |
fprintf(is_error ? stderr : stdout, |
| 58 |
"\n" |
| 59 |
"Display the current date or set it on a device.\n" |
| 60 |
"\n" |
| 61 |
"NOTE: Setting the time on iOS 6 and later is only supported\n" |
| 62 |
" in the setup wizard screens before device activation.\n" |
| 63 |
"\n" |
| 64 |
"OPTIONS:\n" |
| 65 |
" -u, --udid UDID target specific device by UDID\n" |
| 66 |
" -n, --network connect to network device\n" |
| 67 |
" -s, --set TIMESTAMP set UTC time described by TIMESTAMP\n" |
| 68 |
" -c, --sync set time of device to current system time\n" |
| 69 |
" -d, --debug enable communication debugging\n" |
| 70 |
" -h, --help prints usage information\n" |
| 71 |
" -v, --version prints version information\n" |
| 72 |
"\n" |
| 73 |
"Homepage: <" PACKAGE_URL ">\n" |
| 74 |
"Bug Reports: <" PACKAGE_BUGREPORT ">\n" |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
int main(int argc, char *argv[]) |
| 79 |
{ |
| 80 |
lockdownd_client_t client = NULL; |
| 81 |
lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR; |
| 82 |
idevice_t device = NULL; |
| 83 |
idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; |
| 84 |
const char* udid = NULL; |
| 85 |
int use_network = 0; |
| 86 |
time_t setdate = 0; |
| 87 |
plist_t node = NULL; |
| 88 |
int node_type = -1; |
| 89 |
uint64_t datetime = 0; |
| 90 |
time_t rawtime; |
| 91 |
struct tm * tmp; |
| 92 |
char buffer[80]; |
| 93 |
int result = 0; |
| 94 |
|
| 95 |
int c = 0; |
| 96 |
const struct option longopts[] = { |
| 97 |
{ "debug", no_argument, NULL, 'd' }, |
| 98 |
{ "help", no_argument, NULL, 'h' }, |
| 99 |
{ "udid", required_argument, NULL, 'u' }, |
| 100 |
{ "network", no_argument, NULL, 'n' }, |
| 101 |
{ "version", no_argument, NULL, 'v' }, |
| 102 |
{ "set", required_argument, NULL, 's' }, |
| 103 |
{ "sync", no_argument, NULL, 'c' }, |
| 104 |
{ NULL, 0, NULL, 0} |
| 105 |
}; |
| 106 |
|
| 107 |
#ifndef _WIN32 |
| 108 |
signal(SIGPIPE, SIG_IGN); |
| 109 |
#endif |
| 110 |
/* parse cmdline args */ |
| 111 |
while ((c = getopt_long(argc, argv, "dhu:nvs:c", longopts, NULL)) != -1) { |
| 112 |
switch (c) { |
| 113 |
case 'd': |
| 114 |
idevice_set_debug_level(1); |
| 115 |
break; |
| 116 |
case 'u': |
| 117 |
if (!*optarg) { |
| 118 |
fprintf(stderr, "ERROR: UDID argument must not be empty!\n"); |
| 119 |
print_usage(argc, argv, 1); |
| 120 |
return 2; |
| 121 |
} |
| 122 |
udid = optarg; |
| 123 |
break; |
| 124 |
case 'n': |
| 125 |
use_network = 1; |
| 126 |
break; |
| 127 |
case 'h': |
| 128 |
print_usage(argc, argv, 0); |
| 129 |
return 0; |
| 130 |
case 'v': |
| 131 |
printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 132 |
return 0; |
| 133 |
case 's': |
| 134 |
if (!*optarg) { |
| 135 |
fprintf(stderr, "ERROR: set argument must not be empty!\n"); |
| 136 |
print_usage(argc, argv, 1); |
| 137 |
return 2; |
| 138 |
} |
| 139 |
setdate = atoi(optarg); |
| 140 |
if (setdate == 0) { |
| 141 |
fprintf(stderr, "ERROR: Invalid timestamp value.\n"); |
| 142 |
print_usage(argc, argv, 1); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
break; |
| 146 |
case 'c': |
| 147 |
/* get current time */ |
| 148 |
setdate = time(NULL); |
| 149 |
/* convert it to local time which sets timezone/daylight variables */ |
| 150 |
tmp = localtime(&setdate); |
| 151 |
/* recalculate to make it UTC */ |
| 152 |
setdate = mktime(tmp); |
| 153 |
break; |
| 154 |
default: |
| 155 |
print_usage(argc, argv, 1); |
| 156 |
return 2; |
| 157 |
} |
| 158 |
} |
| 159 |
argc -= optind; |
| 160 |
argv += optind; |
| 161 |
|
| 162 |
ret = idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX); |
| 163 |
if (ret != IDEVICE_E_SUCCESS) { |
| 164 |
if (udid) { |
| 165 |
printf("No device found with udid %s.\n", udid); |
| 166 |
} else { |
| 167 |
printf("No device found.\n"); |
| 168 |
} |
| 169 |
return -1; |
| 170 |
} |
| 171 |
|
| 172 |
if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &client, TOOL_NAME))) { |
| 173 |
fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", ldret); |
| 174 |
result = -1; |
| 175 |
goto cleanup; |
| 176 |
} |
| 177 |
|
| 178 |
if(lockdownd_get_value(client, NULL, "TimeIntervalSince1970", &node) != LOCKDOWN_E_SUCCESS) { |
| 179 |
fprintf(stderr, "ERROR: Unable to retrieve 'TimeIntervalSince1970' node from device.\n"); |
| 180 |
result = -1; |
| 181 |
goto cleanup; |
| 182 |
} |
| 183 |
|
| 184 |
if (node == NULL) { |
| 185 |
fprintf(stderr, "ERROR: Empty node for 'TimeIntervalSince1970' received.\n"); |
| 186 |
result = -1; |
| 187 |
goto cleanup; |
| 188 |
} |
| 189 |
|
| 190 |
node_type = plist_get_node_type(node); |
| 191 |
|
| 192 |
/* get or set? */ |
| 193 |
if (setdate == 0) { |
| 194 |
/* get time value from device */ |
| 195 |
switch (node_type) { |
| 196 |
case PLIST_UINT: |
| 197 |
plist_get_uint_val(node, &datetime); |
| 198 |
break; |
| 199 |
case PLIST_REAL: |
| 200 |
{ |
| 201 |
double rv = 0; |
| 202 |
plist_get_real_val(node, &rv); |
| 203 |
datetime = rv; |
| 204 |
} |
| 205 |
break; |
| 206 |
default: |
| 207 |
fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n"); |
| 208 |
break; |
| 209 |
} |
| 210 |
plist_free(node); |
| 211 |
node = NULL; |
| 212 |
|
| 213 |
/* date/time calculations */ |
| 214 |
rawtime = (time_t)datetime; |
| 215 |
tmp = localtime(&rawtime); |
| 216 |
|
| 217 |
/* finally we format and print the current date */ |
| 218 |
strftime(buffer, 80, DATE_FMT_LANGINFO, tmp); |
| 219 |
puts(buffer); |
| 220 |
} else { |
| 221 |
datetime = setdate; |
| 222 |
|
| 223 |
plist_free(node); |
| 224 |
node = NULL; |
| 225 |
|
| 226 |
switch (node_type) { |
| 227 |
case PLIST_UINT: |
| 228 |
node = plist_new_uint(datetime); |
| 229 |
break; |
| 230 |
case PLIST_REAL: |
| 231 |
node = plist_new_real((double)datetime); |
| 232 |
break; |
| 233 |
default: |
| 234 |
fprintf(stderr, "ERROR: Unexpected node type for 'TimeIntervalSince1970'\n"); |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
if(lockdownd_set_value(client, NULL, "TimeIntervalSince1970", node) == LOCKDOWN_E_SUCCESS) { |
| 239 |
tmp = localtime(&setdate); |
| 240 |
strftime(buffer, 80, DATE_FMT_LANGINFO, tmp); |
| 241 |
puts(buffer); |
| 242 |
} else { |
| 243 |
printf("ERROR: Failed to set date on device.\n"); |
| 244 |
} |
| 245 |
node = NULL; |
| 246 |
} |
| 247 |
|
| 248 |
cleanup: |
| 249 |
if (client) |
| 250 |
lockdownd_client_free(client); |
| 251 |
|
| 252 |
if (device) |
| 253 |
idevice_free(device); |
| 254 |
|
| 255 |
return result; |
| 256 |
} |