Don't require user to specify -a anymore
[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 -a 192.168.1.254 -f firmware.bin\n"
67 #else
68                         "C:\\> nmrpflash.exe -i net0 -a 192.168.1.254 -f firmware.bin\n"
69 #endif
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
79 #ifdef NMRPFLASH_WINDOWS
80 void require_admin()
81 {
82         SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
83         PSID adminGroup = NULL;
84         BOOL success = AllocateAndInitializeSid(
85                 &auth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
86                 0, 0, 0, 0, 0, 0, &adminGroup
87         );
88
89         if (success) {
90                 if (CheckTokenMembership(NULL, adminGroup, &success)) {
91                         if (!success) {
92                                 fprintf(stderr, "Error: must be run as administrator\n");
93                                 exit(1);
94                         } else {
95                                 return;
96                         }
97                 }
98                 FreeSid(adminGroup);
99         }
100
101         fprintf(stderr, "Warning: failed to check administrator privileges\n");
102 }
103 #else
104 void require_admin()
105 {
106         if (getuid() != 0) {
107                 fprintf(stderr, "Error: must be run as root\n");
108                 exit(1);
109         }
110 }
111 #endif
112
113 int main(int argc, char **argv)
114 {
115         int c, val, max;
116         int list = 0;
117         struct nmrpd_args args = {
118                 .rx_timeout = 200,
119                 .ul_timeout = 5 * 60 * 1000,
120                 .tftpcmd = NULL,
121                 .file_local = NULL,
122                 .file_remote = NULL,
123                 .ipaddr_intf = NULL,
124                 .ipaddr = NULL,
125                 .ipmask = "255.255.255.0",
126                 .intf = NULL,
127                 .mac = "ff:ff:ff:ff:ff:ff",
128                 .op = NMRP_UPLOAD_FW,
129                 .port = 69,
130                 .region = NULL,
131         };
132 #ifdef NMRPFLASH_WINDOWS
133         char *newpath = NULL;
134         char *oldpath = NULL;
135         char *windir = NULL;
136         WSADATA wsa;
137
138         val = WSAStartup(MAKEWORD(2, 2), &wsa);
139         if (val != 0) {
140                 win_perror2("WSAStartup", val);
141                 return 1;
142         }
143
144
145 #ifndef _WIN64
146         // This dirty hack works around the WOW64 file system redirector[1], which would prevent
147         // us from calling programs residing in %windir%\System32 when running on a 64bit system
148         // (since nmrpflash is currently shipped as 32bit only).
149         //
150         // [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
151
152         oldpath = getenv("PATH");
153         windir = getenv("WINDIR");
154         if (oldpath && windir) {
155                 newpath = malloc(strlen(oldpath) + strlen(windir) + 32);
156                 sprintf(newpath, "%s;%s\\Sysnative", oldpath, windir);
157                 SetEnvironmentVariable("PATH", newpath);
158                 free(newpath);
159         }
160 #endif
161 #endif
162
163         opterr = 0;
164
165         while ((c = getopt(argc, argv, "a:A:c:f:F:i:m:M:p:R:t:T:hLVvU")) != -1) {
166                 max = 0x7fffffff;
167                 switch (c) {
168                         case 'a':
169                                 args.ipaddr = optarg;
170                                 break;
171                         case 'A':
172                                 args.ipaddr_intf = optarg;
173                                 break;
174                         case 'c':
175                                 args.tftpcmd = optarg;
176                                 break;
177                         case 'f':
178                                 args.file_local = optarg;
179                                 break;
180                         case 'F':
181                                 args.file_remote = optarg;
182                                 break;
183                         case 'i':
184                                 args.intf = optarg;
185                                 break;
186                         case 'm':
187                                 args.mac = optarg;
188                                 break;
189                         case 'M':
190                                 args.ipmask = optarg;
191                                 break;
192 #ifdef NMRPFLASH_SET_REGION
193                         case 'R':
194                                 args.region = optarg;
195                                 break;
196 #endif
197                         case 'p':
198                         case 'T':
199                         case 't':
200                                 if (c == 'p') {
201                                         max = 0xffff;
202                                 }
203
204                                 val = atoi(optarg);
205                                 if (val <= 0 || val > max) {
206                                         fprintf(stderr, "Invalid numeric value for -%c.\n", c);
207                                         return 1;
208                                 }
209
210                                 if (c == 'p') {
211                                         args.port = val;
212                                 } else if (c == 't') {
213                                         args.rx_timeout = val;
214                                 } else if (c == 'T') {
215                                         args.ul_timeout = val * 1000;
216                                 }
217
218                                 break;
219                         case 'V':
220                                 printf("nmrpflash %s\n", NMRPFLASH_VERSION);
221                                 val = 0;
222                                 goto out;
223                         case 'v':
224                                 ++verbosity;
225                                 break;
226                         case 'L':
227                                 list = 1;
228                                 break;
229                                 goto out;
230                         case 'h':
231                                 usage(stdout);
232                                 val = 0;
233                                 goto out;
234 #ifdef NMRPFLASH_TFTP_TEST
235                         case 'U':
236                                 if (args.ipaddr && args.file_local) {
237                                         val = tftp_put(&args);
238                                         goto out;
239                                 }
240                                 /* fall through */
241 #endif
242                         default:
243                                 usage(stderr);
244                                 val = 1;
245                                 goto out;
246                 }
247         }
248
249         if (args.ipaddr_intf && !args.ipaddr) {
250                 fprintf(stderr, "Error: cannot use -A <ipaddr> without using -a <ipaddr>.\n");
251                 return 1;
252         }
253
254         if (!list && ((!args.file_local && !args.tftpcmd) || !args.intf /*|| !args.ipaddr*/)) {
255                 usage(stderr);
256                 return 1;
257         }
258
259         require_admin();
260         val = !list ? nmrp_do(&args) : ethsock_list_all();
261
262 out:
263 #ifdef NMRPFLASH_WINDOWS
264         WSACleanup();
265 #endif
266         return val;
267 }