main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. void usage(FILE *fp)
  25. {
  26. fprintf(fp,
  27. "Usage: nmrpflash [OPTIONS...]\n"
  28. "\n"
  29. "Options (-i, and -f or -c are mandatory):\n"
  30. " -a <ipaddr> IP address to assign to target device\n"
  31. " -A <ipaddr> IP address to assign to selected interface\n"
  32. " -B Blind mode (don't wait for response packets)\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 NMRP packets\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. " -S <n> Skip <n> bytes of the firmware file\n"
  46. #ifdef NMRPFLASH_TFTP_TEST
  47. " -U Test TFTP upload\n"
  48. #endif
  49. " -v Be verbose\n"
  50. " -V Print version and exit\n"
  51. " -L List network interfaces\n"
  52. " -h Show this screen\n"
  53. "\n"
  54. "Example: (run as "
  55. #ifndef NMRPFLASH_WINDOWS
  56. "root"
  57. #else
  58. "administrator"
  59. #endif
  60. ")\n\n"
  61. #ifndef NMRPFLASH_WINDOWS
  62. "# nmrpflash -i eth0 -f firmware.bin\n"
  63. #else
  64. "C:\\> nmrpflash.exe -i net0 -f firmware.bin\n"
  65. #endif
  66. "\n"
  67. "When using -c, the environment variables IP, PORT, NETMASK\n"
  68. "and MAC are set to the device IP address, TFTP port, subnet\n"
  69. "mask and MAC address, respectively.\n"
  70. "\n"
  71. "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
  72. "nmrpflash is free software, licensed under the GNU GPLv3.\n"
  73. "Source code at https://github.com/jclehner/nmrpflash\n"
  74. "\n",
  75. NMRPFLASH_VERSION
  76. );
  77. }
  78. #ifdef NMRPFLASH_WINDOWS
  79. void require_admin()
  80. {
  81. SID_IDENTIFIER_AUTHORITY auth = { SECURITY_NT_AUTHORITY };
  82. PSID group = NULL;
  83. BOOL admin, success = AllocateAndInitializeSid(
  84. &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  85. 0, 0, 0, 0, 0, 0, &group
  86. );
  87. if (success) {
  88. success = CheckTokenMembership(NULL, group, &admin);
  89. FreeSid(group);
  90. if (success) {
  91. if (!admin) {
  92. fprintf(stderr, "Error: must be run as administrator\n");
  93. exit(1);
  94. } else {
  95. return;
  96. }
  97. }
  98. }
  99. fprintf(stderr, "Warning: failed to check administrator privileges\n");
  100. }
  101. void show_exit_prompt()
  102. {
  103. DWORD pid;
  104. HWND win = GetConsoleWindow();
  105. if (!win || !GetWindowThreadProcessId(win, &pid)) {
  106. return;
  107. }
  108. if (GetCurrentProcessId() == pid) {
  109. printf("Press any key to exit\n");
  110. getch();
  111. }
  112. }
  113. #else
  114. void require_admin()
  115. {
  116. if (getuid() != 0) {
  117. fprintf(stderr, "Error: must be run as root\n");
  118. exit(1);
  119. }
  120. }
  121. #endif
  122. int main(int argc, char **argv)
  123. {
  124. int c, val, max;
  125. bool list = false, have_dest_mac = false;
  126. struct nmrpd_args args = {
  127. .rx_timeout = 1000,
  128. .ul_timeout = 15 * 60 * 1000,
  129. .tftpcmd = NULL,
  130. .file_local = NULL,
  131. .file_remote = NULL,
  132. .ipaddr_intf = NULL,
  133. .ipaddr = NULL,
  134. .ipmask = "255.255.255.0",
  135. .intf = NULL,
  136. .mac = "ff:ff:ff:ff:ff:ff",
  137. .op = NMRP_UPLOAD_FW,
  138. .port = 69,
  139. .region = NULL,
  140. .blind = false,
  141. .offset = 0,
  142. };
  143. #ifdef NMRPFLASH_WINDOWS
  144. char *newpath = NULL;
  145. char *oldpath = NULL;
  146. char *windir = NULL;
  147. WSADATA wsa;
  148. atexit(&show_exit_prompt);
  149. val = WSAStartup(MAKEWORD(2, 2), &wsa);
  150. if (val != 0) {
  151. win_perror2("WSAStartup", val);
  152. return 1;
  153. }
  154. #ifndef _WIN64
  155. // This dirty hack works around the WOW64 file system redirector[1], which would prevent
  156. // us from calling programs residing in %windir%\System32 when running on a 64bit system
  157. // (since nmrpflash is currently shipped as 32bit only).
  158. //
  159. // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
  160. oldpath = getenv("PATH");
  161. windir = getenv("WINDIR");
  162. if (oldpath && windir) {
  163. newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
  164. sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
  165. SetEnvironmentVariable("PATH", newpath);
  166. free(newpath);
  167. }
  168. #endif
  169. #endif
  170. opterr = 0;
  171. while ((c = getopt(argc, argv, "a:A:Bc:f:F:i:m:M:p:R:S:t:T:hLVvU")) != -1) {
  172. max = 0x7fffffff;
  173. switch (c) {
  174. case 'a':
  175. args.ipaddr = optarg;
  176. break;
  177. case 'A':
  178. args.ipaddr_intf = optarg;
  179. break;
  180. case 'B':
  181. args.blind = true;
  182. break;
  183. case 'c':
  184. args.tftpcmd = optarg;
  185. break;
  186. case 'f':
  187. args.file_local = optarg;
  188. break;
  189. case 'F':
  190. args.file_remote = optarg;
  191. break;
  192. case 'i':
  193. args.intf = optarg;
  194. break;
  195. case 'm':
  196. args.mac = optarg;
  197. have_dest_mac = true;
  198. break;
  199. case 'M':
  200. args.ipmask = optarg;
  201. break;
  202. #ifdef NMRPFLASH_SET_REGION
  203. case 'R':
  204. args.region = optarg;
  205. break;
  206. #endif
  207. case 'p':
  208. case 'S':
  209. case 'T':
  210. case 't':
  211. if (c == 'p') {
  212. max = 0xffff;
  213. }
  214. val = atoi(optarg);
  215. if (val <= 0 || val > max) {
  216. fprintf(stderr, "Invalid numeric value for -%c.\n", c);
  217. return 1;
  218. }
  219. if (c == 'p') {
  220. args.port = val;
  221. } else if (c == 't') {
  222. args.rx_timeout = val;
  223. } else if (c == 'T') {
  224. args.ul_timeout = val * 1000;
  225. } else if (c == 'S') {
  226. args.offset = val;
  227. }
  228. break;
  229. case 'V':
  230. printf("nmrpflash %s\n", NMRPFLASH_VERSION);
  231. val = 0;
  232. goto out;
  233. case 'v':
  234. ++verbosity;
  235. break;
  236. case 'L':
  237. list = true;
  238. break;
  239. case 'h':
  240. usage(stdout);
  241. val = 0;
  242. goto out;
  243. #ifdef NMRPFLASH_TFTP_TEST
  244. case 'U':
  245. if (args.ipaddr && args.file_local) {
  246. val = tftp_put(&args);
  247. goto out;
  248. }
  249. /* fall through */
  250. #endif
  251. default:
  252. usage(stderr);
  253. val = 1;
  254. goto out;
  255. }
  256. }
  257. if (args.ipaddr_intf && !args.ipaddr) {
  258. fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
  259. return 1;
  260. }
  261. if (args.blind && !have_dest_mac) {
  262. fprintf(stderr, "Error: use of -B requires -m <mac>.\n");
  263. return 1;
  264. }
  265. #ifndef NMRPFLASH_FUZZ
  266. if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf)) {
  267. usage(stderr);
  268. return 1;
  269. }
  270. if (!list) {
  271. require_admin();
  272. }
  273. #endif
  274. val = !list ? nmrp_do(&args) : ethsock_list_all();
  275. out:
  276. #ifdef NMRPFLASH_WINDOWS
  277. WSACleanup();
  278. #endif
  279. return val;
  280. }