main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <unistd.h>
  20. #include <getopt.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "nmrpd.h"
  24. int verbosity = 0;
  25. void usage(FILE *fp)
  26. {
  27. fprintf(fp,
  28. "Usage: nmrpflash [OPTIONS...]\n"
  29. "\n"
  30. "Options (-i, -f and/or -c are mandatory):\n"
  31. " -a <ipaddr> IP address to assign to target device\n"
  32. " -A <ipaddr> IP address to assign to seleted interface\n"
  33. " -c <command> Command to run before (or instead of) TFTP upload\n"
  34. " -f <firmware> Firmware file\n"
  35. " -F <filename> Remote filename to use during TFTP upload\n"
  36. " -i <interface> Network interface directly connected to device\n"
  37. " -m <mac> MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
  38. " -M <netmask> Subnet mask to assign to target device\n"
  39. " -t <timeout> Timeout (in milliseconds) for regular messages\n"
  40. " -T <timeout> Time (seconds) to wait after successfull TFTP upload\n"
  41. " -p <port> Port to use for TFTP upload\n"
  42. #ifdef NMRPFLASH_SET_REGION
  43. " -R <region> Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n"
  44. #endif
  45. #ifdef NMRPFLASH_TFTP_TEST
  46. " -U Test TFTP upload\n"
  47. #endif
  48. " -v Be verbose\n"
  49. " -V Print version and exit\n"
  50. " -L List network interfaces\n"
  51. " -h Show this screen\n"
  52. "\n"
  53. "Example: (run as "
  54. #ifndef NMRPFLASH_WINDOWS
  55. "root"
  56. #else
  57. "administrator"
  58. #endif
  59. ")\n\n"
  60. #ifndef NMRPFLASH_WINDOWS
  61. "# nmrpflash -i eth0 -f firmware.bin\n"
  62. #else
  63. "C:\\> nmrpflash.exe -i net0 -f firmware.bin\n"
  64. #endif
  65. "\n"
  66. "When using -c, the environment variables IP, PORT, NETMASK\n"
  67. "and MAC are set to the device IP address, TFTP port, subnet\n"
  68. "mask and MAC address, respectively.\n"
  69. "\n"
  70. "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
  71. "nmrpflash is free software, licensed under the GNU GPLv3.\n"
  72. "Source code at https://github.com/jclehner/nmrpflash\n"
  73. "\n",
  74. NMRPFLASH_VERSION
  75. );
  76. }
  77. #ifdef NMRPFLASH_WINDOWS
  78. void require_admin()
  79. {
  80. SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
  81. PSID group = NULL;
  82. BOOL admin, success = AllocateAndInitializeSid(
  83. &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  84. 0, 0, 0, 0, 0, 0, &group
  85. );
  86. if (success) {
  87. success = CheckTokenMembership(NULL, group, &admin);
  88. FreeSid(group);
  89. if (success) {
  90. if (!admin) {
  91. fprintf(stderr, "Error: must be run as administrator\n");
  92. exit(1);
  93. } else {
  94. return;
  95. }
  96. }
  97. }
  98. fprintf(stderr, "Warning: failed to check administrator privileges\n");
  99. }
  100. void show_exit_prompt()
  101. {
  102. DWORD pid;
  103. HWND win = GetConsoleWindow();
  104. if (!win || !GetWindowThreadProcessId(win, &pid)) {
  105. return;
  106. }
  107. if (GetCurrentProcessId() == pid) {
  108. printf("Press any key to exit\n");
  109. getch();
  110. }
  111. }
  112. #else
  113. void require_admin()
  114. {
  115. if (getuid() != 0) {
  116. fprintf(stderr, "Error: must be run as root\n");
  117. exit(1);
  118. }
  119. }
  120. #endif
  121. int main(int argc, char **argv)
  122. {
  123. int c, val, max;
  124. int list = 0;
  125. struct nmrpd_args args = {
  126. .rx_timeout = 200,
  127. .ul_timeout = 5 * 60 * 1000,
  128. .tftpcmd = NULL,
  129. .file_local = NULL,
  130. .file_remote = NULL,
  131. .ipaddr_intf = NULL,
  132. .ipaddr = NULL,
  133. .ipmask = "255.255.255.0",
  134. .intf = NULL,
  135. .mac = "ff:ff:ff:ff:ff:ff",
  136. .op = NMRP_UPLOAD_FW,
  137. .port = 69,
  138. .region = NULL,
  139. };
  140. #ifdef NMRPFLASH_WINDOWS
  141. char *newpath = NULL;
  142. char *oldpath = NULL;
  143. char *windir = NULL;
  144. WSADATA wsa;
  145. atexit(&show_exit_prompt);
  146. val = WSAStartup(MAKEWORD(2, 2), &wsa);
  147. if (val != 0) {
  148. win_perror2("WSAStartup", val);
  149. return 1;
  150. }
  151. #ifndef _WIN64
  152. // This dirty hack works around the WOW64 file system redirector[1], which would prevent
  153. // us from calling programs residing in %windir%\System32 when running on a 64bit system
  154. // (since nmrpflash is currently shipped as 32bit only).
  155. //
  156. // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
  157. oldpath = getenv("PATH");
  158. windir = getenv("WINDIR");
  159. if (oldpath && windir) {
  160. newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
  161. sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
  162. SetEnvironmentVariable("PATH", newpath);
  163. free(newpath);
  164. }
  165. #endif
  166. #endif
  167. opterr = 0;
  168. while ((c = getopt(argc, argv, "a:A:c:f:F:i:m:M:p:R:t:T:hLVvU")) != -1) {
  169. max = 0x7fffffff;
  170. switch (c) {
  171. case 'a':
  172. args.ipaddr = optarg;
  173. break;
  174. case 'A':
  175. args.ipaddr_intf = optarg;
  176. break;
  177. case 'c':
  178. args.tftpcmd = optarg;
  179. break;
  180. case 'f':
  181. args.file_local = optarg;
  182. break;
  183. case 'F':
  184. args.file_remote = optarg;
  185. break;
  186. case 'i':
  187. args.intf = optarg;
  188. break;
  189. case 'm':
  190. args.mac = optarg;
  191. break;
  192. case 'M':
  193. args.ipmask = optarg;
  194. break;
  195. #ifdef NMRPFLASH_SET_REGION
  196. case 'R':
  197. args.region = optarg;
  198. break;
  199. #endif
  200. case 'p':
  201. case 'T':
  202. case 't':
  203. if (c == 'p') {
  204. max = 0xffff;
  205. }
  206. val = atoi(optarg);
  207. if (val <= 0 || val > max) {
  208. fprintf(stderr, "Invalid numeric value for -%c.\n", c);
  209. return 1;
  210. }
  211. if (c == 'p') {
  212. args.port = val;
  213. } else if (c == 't') {
  214. args.rx_timeout = val;
  215. } else if (c == 'T') {
  216. args.ul_timeout = val * 1000;
  217. }
  218. break;
  219. case 'V':
  220. printf("nmrpflash %s\n", NMRPFLASH_VERSION);
  221. val = 0;
  222. goto out;
  223. case 'v':
  224. ++verbosity;
  225. break;
  226. case 'L':
  227. list = 1;
  228. break;
  229. case 'h':
  230. usage(stdout);
  231. val = 0;
  232. goto out;
  233. #ifdef NMRPFLASH_TFTP_TEST
  234. case 'U':
  235. if (args.ipaddr && args.file_local) {
  236. val = tftp_put(&args);
  237. goto out;
  238. }
  239. /* fall through */
  240. #endif
  241. default:
  242. usage(stderr);
  243. val = 1;
  244. goto out;
  245. }
  246. }
  247. if (args.ipaddr_intf && !args.ipaddr) {
  248. fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
  249. return 1;
  250. }
  251. #ifndef NMRPFLASH_FUZZ
  252. if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf)) {
  253. usage(stderr);
  254. return 1;
  255. }
  256. if (!list) {
  257. require_admin();
  258. }
  259. #endif
  260. val = !list ? nmrp_do(&args) : ethsock_list_all();
  261. out:
  262. #ifdef NMRPFLASH_WINDOWS
  263. WSACleanup();
  264. #endif
  265. return val;
  266. }