Ensure that NMRP packets are at least 64 bytes
[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 #define NMRPFLASH_SET_REGION
27
28 int verbosity = 0;
29
30 void usage(FILE *fp)
31 {
32         fprintf(fp,
33                         "Usage: nmrpflash [OPTIONS...]\n"
34                         "\n"
35                         "Options (-i, -f and/or -c are mandatory):\n"
36                         " -a <ipaddr>     IP address to assign to target device\n"
37                         " -A <ipaddr>     IP address to assign to seleted interface\n"
38                         " -c <command>    Command to run before (or instead of) TFTP upload\n"
39                         " -f <firmware>   Firmware file\n"
40                         " -F <filename>   Remote filename to use during TFTP upload\n"
41                         " -i <interface>  Network interface directly connected to device\n"
42                         " -m <mac>        MAC address of target device (xx:xx:xx:xx:xx:xx)\n"
43                         " -M <netmask>    Subnet mask to assign to target device\n"
44                         " -t <timeout>    Timeout (in milliseconds) for regular messages\n"
45                         " -T <timeout>    Time (seconds) to wait after successfull TFTP upload\n"
46                         " -p <port>       Port to use for TFTP upload\n"
47 #ifdef NMRPFLASH_SET_REGION
48                         " -R <region>     Set device region (NA, WW, GR, PR, RU, BZ, IN, KO, JP)\n"
49 #endif
50 #ifdef NMRPFLASH_TFTP_TEST
51                         " -U              Test TFTP upload\n"
52 #endif
53                         " -v              Be verbose\n"
54                         " -V              Print version and exit\n"
55                         " -L              List network interfaces\n"
56                         " -h              Show this screen\n"
57                         "\n"
58                         "Example: (run as "
59 #ifndef NMRPFLASH_WINDOWS
60                         "root"
61 #else
62                         "administrator"
63 #endif
64                         ")\n\n"
65 #ifndef NMRPFLASH_WINDOWS
66                         "# nmrpflash -i eth0 -f firmware.bin\n"
67 #else
68                         "C:\\> nmrpflash.exe -i net0 -f firmware.bin\n"
69 #endif
70                         "\n"
71                         "When using -c, the environment variables IP, PORT, NETMASK\n"
72                         "and MAC are set to the device IP address, TFTP port, subnet\n"
73                         "mask and MAC address, respectively.\n"
74                         "\n"
75                         "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
76                         "nmrpflash is free software, licensed under the GNU GPLv3.\n"
77                         "Source code at https://github.com/jclehner/nmrpflash\n"
78                         "\n",
79                         NMRPFLASH_VERSION
80           );
81 }
82
83 #ifdef NMRPFLASH_WINDOWS
84 void require_admin()
85 {
86         SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
87         PSID group = NULL;
88         BOOL admin, success = AllocateAndInitializeSid(
89                 &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
90                 0, 0, 0, 0, 0, 0, &group
91         );
92
93         if (success) {
94                 success = CheckTokenMembership(NULL, group, &admin);
95                 FreeSid(group);
96                 if (success) {
97                         if (!admin) {
98                                 fprintf(stderr, "Error: must be run as administrator\n");
99                                 exit(1);
100                         } else {
101                                 return;
102                         }
103                 }
104         }
105
106         fprintf(stderr, "Warning: failed to check administrator privileges\n");
107 }
108
109 void show_exit_prompt()
110 {
111         DWORD pid;
112         HWND win = GetConsoleWindow();
113         if (!win || !GetWindowThreadProcessId(win, &pid)) {
114                 return;
115         }
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
132 int main(int argc, char **argv)
133 {
134         int c, val, max;
135         int list = 0;
136         struct nmrpd_args args = {
137                 .rx_timeout = 200,
138                 .ul_timeout = 5 * 60 * 1000,
139                 .tftpcmd = NULL,
140                 .file_local = NULL,
141                 .file_remote = NULL,
142                 .ipaddr_intf = NULL,
143                 .ipaddr = NULL,
144                 .ipmask = "255.255.255.0",
145                 .intf = NULL,
146                 .mac = "ff:ff:ff:ff:ff:ff",
147                 .op = NMRP_UPLOAD_FW,
148                 .port = 69,
149                 .region = NULL,
150         };
151 #ifdef NMRPFLASH_WINDOWS
152         char *newpath = NULL;
153         char *oldpath = NULL;
154         char *windir = NULL;
155         WSADATA wsa;
156
157         atexit(&show_exit_prompt);
158
159         val = WSAStartup(MAKEWORD(2, 2), &wsa);
160         if (val != 0) {
161                 win_perror2("WSAStartup", val);
162                 return 1;
163         }
164
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
173         oldpath = getenv("PATH");
174         windir = getenv("WINDIR");
175         if (oldpath && windir) {
176                 newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
177                 sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
178                 SetEnvironmentVariable("PATH", newpath);
179                 free(newpath);
180         }
181 #endif
182 #endif
183
184         opterr = 0;
185
186         while ((c = getopt(argc, argv, "a:A:c:f:F:i:m:M:p:R:t:T:hLVvU")) != -1) {
187                 max = 0x7fffffff;
188                 switch (c) {
189                         case 'a':
190                                 args.ipaddr = optarg;
191                                 break;
192                         case 'A':
193                                 args.ipaddr_intf = optarg;
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                                 break;
210                         case 'M':
211                                 args.ipmask = optarg;
212                                 break;
213 #ifdef NMRPFLASH_SET_REGION
214                         case 'R':
215                                 args.region = optarg;
216                                 break;
217 #endif
218                         case 'p':
219                         case 'T':
220                         case 't':
221                                 if (c == 'p') {
222                                         max = 0xffff;
223                                 }
224
225                                 val = atoi(optarg);
226                                 if (val <= 0 || val > max) {
227                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
228                                         return 1;
229                                 }
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                                 }
238
239                                 break;
240                         case 'V':
241                                 printf("nmrpflash %s\n", NMRPFLASH_VERSION);
242                                 val = 0;
243                                 goto out;
244                         case 'v':
245                                 ++verbosity;
246                                 break;
247                         case 'L':
248                                 list = 1;
249                                 break;
250                                 goto out;
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
270         if (args.ipaddr_intf && !args.ipaddr) {
271                 fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
272                 return 1;
273         }
274
275 #ifndef NMRPFLASH_FUZZ
276         if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf /*|| !args.ipaddr*/)) {
277                 usage(stderr);
278                 return 1;
279         }
280
281         require_admin();
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 }