Remove unused msg_dump arg
[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, NETMASK and MAC are\n"
72                         "set to the device IP address, subnet mask and MAC address.\n"
73                         "\n"
74                         "nmrpflash %s, Copyright (C) 2016 Joseph C. Lehner\n"
75                         "nmrpflash is free software, licensed under the GNU GPLv3.\n"
76                         "Source code at https://github.com/jclehner/nmrpflash\n"
77                         "\n",
78                         NMRPFLASH_VERSION
79           );
80 }
81
82 #ifdef NMRPFLASH_WINDOWS
83 void require_admin()
84 {
85         SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
86         PSID adminGroup = NULL;
87         BOOL success = AllocateAndInitializeSid(
88                 &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
89                 0, 0, 0, 0, 0, 0, &adminGroup
90         );
91
92         if (success) {
93                 if (CheckTokenMembership(NULL, adminGroup, &success)) {
94                         if (!success) {
95                                 fprintf(stderr, "Error: must be run as administrator\n");
96                                 exit(1);
97                         } else {
98                                 return;
99                         }
100                 }
101                 FreeSid(adminGroup);
102         }
103
104         fprintf(stderr, "Warning: failed to check administrator privileges\n");
105 }
106 #else
107 void require_admin()
108 {
109         if (getuid() != 0) {
110                 fprintf(stderr, "Error: must be run as root\n");
111                 exit(1);
112         }
113 }
114 #endif
115
116 int main(int argc, char **argv)
117 {
118         int c, val, max;
119         int list = 0;
120         struct nmrpd_args args = {
121                 .rx_timeout = 200,
122                 .ul_timeout = 5 * 60 * 1000,
123                 .tftpcmd = NULL,
124                 .file_local = NULL,
125                 .file_remote = NULL,
126                 .ipaddr_intf = NULL,
127                 .ipaddr = NULL,
128                 .ipmask = "255.255.255.0",
129                 .intf = NULL,
130                 .mac = "ff:ff:ff:ff:ff:ff",
131                 .op = NMRP_UPLOAD_FW,
132                 .port = 69,
133                 .region = NULL,
134         };
135 #ifdef NMRPFLASH_WINDOWS
136         char *newpath = NULL;
137         char *oldpath = NULL;
138         char *windir = NULL;
139         WSADATA wsa;
140
141         val = WSAStartup(MAKEWORD(2, 2), &wsa);
142         if (val != 0) {
143                 win_perror2("WSAStartup", val);
144                 return 1;
145         }
146
147
148 #ifndef _WIN64
149         // This dirty hack works around the WOW64 file system redirector[1], which would prevent
150         // us from calling programs residing in %windir%\System32 when running on a 64bit system
151         // (since nmrpflash is currently shipped as 32bit only).
152         //
153         // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
154
155         oldpath = getenv("PATH");
156         windir = getenv("WINDIR");
157         if (oldpath && windir) {
158                 newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
159                 sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
160                 SetEnvironmentVariable("PATH", newpath);
161                 free(newpath);
162         }
163 #endif
164 #endif
165
166         opterr = 0;
167
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
207                                 val = atoi(optarg);
208                                 if (val <= 0 || val > max) {
209                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
210                                         return 1;
211                                 }
212
213                                 if (c == 'p') {
214                                         args.port = val;
215                                 } else if (c == 't') {
216                                         args.rx_timeout = val;
217                                 } else if (c == 'T') {
218                                         args.ul_timeout = val * 1000;
219                                 }
220
221                                 break;
222                         case 'V':
223                                 printf("nmrpflash %s\n", NMRPFLASH_VERSION);
224                                 val = 0;
225                                 goto out;
226                         case 'v':
227                                 ++verbosity;
228                                 break;
229                         case 'L':
230                                 list = 1;
231                                 break;
232                                 goto out;
233                         case 'h':
234                                 usage(stdout);
235                                 val = 0;
236                                 goto out;
237 #ifdef NMRPFLASH_TFTP_TEST
238                         case 'U':
239                                 if (args.ipaddr && args.file_local) {
240                                         val = tftp_put(&args);
241                                         goto out;
242                                 }
243                                 /* fall through */
244 #endif
245                         default:
246                                 usage(stderr);
247                                 val = 1;
248                                 goto out;
249                 }
250         }
251
252         if (args.ipaddr_intf && !args.ipaddr) {
253                 fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
254                 return 1;
255         }
256
257 #ifndef NMRPFLASH_FUZZ
258         if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf /*|| !args.ipaddr*/)) {
259                 usage(stderr);
260                 return 1;
261         }
262
263         require_admin();
264 #endif
265         val = !list ? nmrp_do(&args) : ethsock_list_all();
266
267 out:
268 #ifdef NMRPFLASH_WINDOWS
269         WSACleanup();
270 #endif
271         return val;
272 }