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