main.c 7.5 KB

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