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