/
services.c
19373 lines (17302 loc) · 473 KB
/
services.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Miscellaneous services
*
* Copyright (C) 2009, Broadcom Corporation. All Rights Reserved.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $Id: services.c,v 1.100 2010/03/04 09:39:18 Exp $
*/
#include <rc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <bcmnvram.h>
#include <shutils.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <sys/reboot.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <net/ethernet.h>
#ifdef RTCONFIG_TOR
#include <pwd.h>
#endif
#include <shared.h>
#include "flash_mtd.h"
#ifdef RTCONFIG_PERMISSION_MANAGEMENT
#include <PMS_DBAPIs.h>
#endif
#ifndef RTCONFIG_DUALWAN
#include <rtstate.h> /* for get_wanunit_by_type inline function */
#endif
#ifdef RTCONFIG_RALINK
#include <ralink.h>
#endif
#ifdef RTCONFIG_QCA
#include <qca.h>
#endif
#ifdef RTCONFIG_DSL
#include <dsl-upg.h>
#endif
#ifdef RTCONFIG_USB
#include <disk_io_tools.h> //mkdir_if_none()
#ifdef RTCONFIG_USB_SMS_MODEM
#include "libsmspdu.h"
#endif
#else
#ifdef RTCONFIG_MDNS
int mkdir_if_none(const char *path)
{
DIR *dp;
char cmd[PATH_MAX];
if (!(dp = opendir(path))) {
memset(cmd, 0, PATH_MAX);
sprintf(cmd, "mkdir -m 0777 -p '%s'", (char *)path);
system(cmd);
return 1;
}
closedir(dp);
return 0;
}
#endif
#endif /* RTCONFIG_USB */
#ifdef RTCONFIG_QTN
#include "web-qtn.h"
#endif
#ifdef RTCONFIG_HTTPS
#include <errno.h>
#include <getopt.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#endif
#ifdef RTCONFIG_IPSEC
#include "rc_ipsec.h"
#define DBG(args) _dprintf args
#endif
#if defined(RTCONFIG_NOTIFICATION_CENTER)
#include <libnt.h>
#include <nt_eInfo.h>
#endif
#if defined(RTCONFIG_QCA_PLC_UTILS) || defined(RTCONFIG_QCA_PLC2)
#include <plc_utils.h>
#endif
#if defined(RTCONFIG_BT_CONN)
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#endif /* RTCONFIG_BT_CONN */
#if defined(RTCONFIG_LP5523)
#include <lp5523led.h>
#endif
#ifdef RTCONFIG_CFGSYNC
#include <cfg_lib.h>
#include <cfg_event.h>
#endif
#ifdef RTCONFIG_AMAS
#include <amas-utils.h>
#include <amas_lib.h>
int init_x_Setting = -1;
#endif
extern char *crypt __P((const char *, const char *)); //should be defined in unistd.h with _XOPEN_SOURCE defined
#define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
void chilli_localUser(void);
void stop_hour_monitor_service();
void start_hour_monitor_service();
/* The g_reboot global variable is used to skip several unnecessary delay
* and redundant steps during reboot / restore to default procedure.
*/
int g_reboot = 0;
int g_upgrade = 0;
// Pop an alarm to recheck pids in 500 msec.
static const struct itimerval pop_tv = { {0,0}, {0, 500 * 1000} };
// Pop an alarm to reap zombies.
static const struct itimerval zombie_tv = { {0,0}, {307, 0} };
// -----------------------------------------------------------------------------
static const char dmhosts[] = "/etc/hosts.dnsmasq";
static const char dmresolv[] = "/tmp/resolv.conf";
static const char dmservers[] = "/tmp/resolv.dnsmasq";
#define INADYNCONF "/etc/inadyn.conf"
#ifdef RTCONFIG_TOAD
static void start_toads(void);
static void stop_toads(void);
#endif
#ifdef RTCONFIG_CROND
void stop_cron(void);
void start_cron(void);
#endif
void start_wlcscan(void);
void stop_wlcscan(void);
#ifndef MS_MOVE
#define MS_MOVE 8192
#endif
#ifndef MNT_DETACH
#define MNT_DETACH 0x00000002
#endif
#ifdef BCMDBG
#include <assert.h>
#else
#define assert(a)
#endif
#define logs(s) syslog(LOG_NOTICE, s)
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_JFFSV1) || defined(RTCONFIG_BRCM_NAND_JFFS2)
int jffs2_fail;
#endif
void
sanity_logs()
{
//#if defined(RTAC56U) || defined(RTAC56S)
// logmessage("ATE", "valid user mode(%d)", !nvram_get_int(ATE_BRCM_FACTORY_MODE_STR()));
//#endif
#if defined(RTCONFIG_JFFS2) || defined(RTCONFIG_JFFSV1) || defined(RTCONFIG_BRCM_NAND_JFFS2)
logmessage("jffs2", "valid logs(%d)", !jffs2_fail);
#endif
}
#if 0
static char
*make_var(char *prefix, int index, char *name)
{
static char buf[100];
assert(prefix);
assert(name);
if (index)
snprintf(buf, sizeof(buf), "%s%d%s", prefix, index, name);
else
snprintf(buf, sizeof(buf), "%s%s", prefix, name);
return buf;
}
static int is_wet(int idx, int unit, int subunit, void *param)
{
return nvram_match(wl_nvname("mode", unit, subunit), "wet");
}
#endif
#define TMP_ROOTFS_MNT_POINT "/sysroot"
/* Build temporarily rootfilesystem
* @newroot: Mount point of new root filesystem.
* @return:
* 0: success
* -1: mount tmp filesystem fail
* -2: copy files fail
* -3: make directory fail
*
* WARNING
* ==========================================================================
* YOU HAVE TO HANDLE THIS FUNCTION VERY CAREFUL. IF YOU MISS FILE(S) THAT
* ARE NEED BY init PROCESS, e.g. /usr/lib/libnvram.so,
* KERNEL REBOOTS SYSTEM IN 3 SECONDS. ERROR MESSAGE IS SHOWN BELOW:
* ==========================================================================
* Image successfully flashed
* Kernel panic - not syncing: Attempted to kill init!
* Rebooting in 3 seconds..
* /sbin/init: can't load library 'libnvram.so'
*/
#if defined(RTCONFIG_TEMPROOTFS)
static int remove_tail_char(char *str, char c)
{
char *p;
if (!str || *str == '\0' || c == '\0')
return -1;
for (p = str + strlen(str); p >= str && *p == c; p--)
*p = '\0';
if (p == str)
return -2;
return 0;
}
/*
* @param: extra parameter of cp command
* @dir: base directory
* @files: files, e.g., "fileA fileB fileC"
* @newroot: new root directory
* @return:
* 0: success
*
* 1. cp("", "/bin", "fileA fileB fileC", "/sysroot") equals to below commands:
* a. mkdir -p /sysroot/bin
* b. cp -a /bin/fileA /bin/fileB /bin/fileC /sysroot/bin
*
* 2. cp("L", "/usr/bin", "" or NULL, "/sysroot") equals to below commands:
* a. if [ -e "/sysroot/usr/bin" ] ; then rmdir "/sysroot/usr/bin" ; fi
* b. mkdir -p `dirname /sysroot/usr/bin`
* c. cp -aL /usr/bin /sysroot
*/
static int __cp(const char *param, const char *dir, const char *files, const char *newroot)
{
struct stat st;
const char *sep = "/";
int i, l, len = 0, len2 = 0, mode = 0; /* copy files and sub-directory */
char cmd[2048], *f, *p, *token, *ptr1;
char d[PATH_MAX], dest_dir[PATH_MAX];
char str1[] = "cp -afXXXXXXXXXXXXXYYY";
const char delim[] = " ";
if (!dir || !newroot || *newroot == '\0')
return -1;
if (!files || *files == '\0')
mode = 1; /* copy a directory recursive */
if (!param)
param = "";
sprintf(str1, "cp -af%s", param);
if (dir && *dir == '/')
sep = "";
sprintf(dest_dir, "%s", newroot);
if (stat(dest_dir, &st) || !S_ISDIR(st.st_mode))
return -2;
switch (mode) {
case 0: /* copy files and sub-directory */
if (!(f = strdup(files)))
return -3;
if (*dir != '\0') {
sprintf(dest_dir, "%s%s%s", newroot, sep, dir);
if (!d_exists(dest_dir))
eval("mkdir", "-p", dest_dir);
else if (!S_ISDIR(st.st_mode)) {
_dprintf("%s exist and is not a directory!\n", dest_dir);
return -4;
}
}
strcpy(cmd, str1);
len = strlen(cmd);
p = cmd + len;
len2 = strlen(dest_dir) + 2; /* leading space + tail NULL */
len += len2;
for (i = l = 0, token = strtok_r(f, delim, &ptr1);
token != NULL;
token = strtok_r(NULL, delim, &ptr1), p += l, len += l)
{
sprintf(d, "%s/%s", dir, token);
/* don't check existence if '?' or '*' exist */
if (!strchr(d, '?') && !strchr(d, '*') && stat(d, &st) < 0) {
_dprintf("%s: %s not exist, skip!\n", __func__, d);
l = 0;
continue;
}
l = strlen(d) + 1;
if ((len + l) < sizeof(cmd)) {
strcat(p, " ");
strcat(p, d);
++i;
continue;
}
/* cmd buffer is not enough, flush */
strcat(p, " ");
strcat(p, dest_dir);
system(cmd);
strcpy(cmd, str1);
len = strlen(cmd);
p = cmd + len;
len += len2;
strcat(p, " ");
strcat(p, d);
i = 1;
}
if (i > 0) {
strcat(p, " ");
strcat(p, dest_dir);
system(cmd);
}
free(f);
break;
case 1: /* copy a directory recursive */
/* If /newroot/bin exist and is directory, rmdir /newroot/bin.
* If not, /bin would be copied to newroot/bin/bin.
*/
if (*dir == '\0')
return -10;
sprintf(dest_dir, "%s%s%s", newroot, sep, dir);
if (d_exists(dest_dir)) {
_dprintf("%s exist, remove it\n", dest_dir);
if (rmdir(dest_dir)) {
_dprintf("rmdir %s (%s)\n", dest_dir, strerror(errno));
return -11;
}
}
/* remove tail '/' */
strcpy(d, dest_dir);
remove_tail_char(d, '/');
/* make sure parent directory of destination directory exist */
p = strrchr(d, '/');
if (p && p != d) {
*p = '\0';
remove_tail_char(d, '/');
if (!d_exists(d))
eval("mkdir", "-p", d);
}
sprintf(cmd, "%s %s %s", str1, dir, dest_dir);
system(cmd);
break;
default:
_dprintf("%s: mode %d is not defined!\n", __func__, mode);
return -4;
}
return 0;
}
/* Build a temporary rootfilesystem.
*
* If you add new binary to temp. rootfilesystem, check whether it needs another library!
* For example, iwpriv needs libiw.so.29, libgcc_s.so.1, and libc.so.0:
* $ mipsel-linux-objdump -x bin/iwpriv | grep NEED
* NEEDED libiw.so.29
* NEEDED libgcc_s.so.1
* NEEDED libc.so.0
* VERNEED 0x004008d4
* VERNEEDNUM 0x00000001
*/
static int build_temp_rootfs(const char *newroot)
{
int i, r;
struct stat st;
char d1[PATH_MAX], d2[PATH_MAX];
struct utsname u;
const char *mdir[] = { "/proc", "/tmp", "/sys", "/usr", "/var", "/var/lock" };
const char *bin = "ash busybox cat cp dd df echo grep iwpriv kill ls ps mkdir mount nvram ping sh tar umount uname rm";
const char *sbin = "init rc hotplug2 insmod lsmod modprobe reboot rmmod rtkswitch"
#if defined(RTCONFIG_BWDPI)
" bwdpi* rsasign_sig_check"
#endif
;
const char *lib = "librt*.so* libnsl* libdl* libm* ld-* libiw* libgcc* libpthread* libdisk* libc*"
#if defined(RTCONFIG_LIBASUSLOG)
" libasuslog.so"
#endif
;
const char *usrbin = "killall"
#if defined(RTCONFIG_WIFI_QCN5024_QCN5054)
" cnssdaemon"
#endif
;
const char *usrsbin __attribute__((unused)) = "httpd"
#if defined(RTCONFIG_HTTPS)
" httpds"
#endif
#ifdef RTCONFIG_BCMARM
" nvram"
#endif
#if defined(RTCONFIG_BWDPI)
" shn_ctrl bwdpi_sqlite dcd wred AiProtectionMonitor hwinfo TrafficAnalyzer WebHistory"
#endif
;
const char *usrlib = "libnvram.so libshared.so libcrypto.so* libbcm* libjson*"
#if defined(RTCONFIG_BWDPI)
" libbwdpi.so libbwdpi_sql.so"
#endif
#ifdef RTCONFIG_USB_SMS_MODEM
" libsmspdu.so"
#endif
#if defined(RTCONFIG_HTTPS) || defined(RTCONFIG_PUSH_EMAIL) || defined(RTCONFIG_FRS_FEEDBACK)
" libssl* libmssl*"
#if defined(RTCONFIG_FRS_FEEDBACK)
" libcurl* libxml2*"
#endif
#endif
#if defined(RTCONFIG_NOTIFICATION_CENTER) || defined(RTCONFIG_BWDPI) || \
defined(RTCONFIG_TRAFFIC_LIMITER) || defined(RTCONFIG_MEDIA_SERVER) || \
defined(RTCONFIG_FREERADIUS) || defined(RTCONFIG_CAPTIVE_PORTAL) || \
defined(RTCONFIG_WEBDAV)
" libsqlite*"
#endif
#if defined(RTCONFIG_PERMISSION_MANAGEMENT)
" libsqlcipher.so* libpms_sql.so"
#endif
#if defined(RTCONFIG_FBWIFI)
" libfbwifi.so"
#endif
#ifdef RTCONFIG_CFGSYNC
" libcfgmnt.so"
#endif
#if defined(RTCONFIG_PROTECTION_SERVER)
" libptcsrv.so"
#endif
#if defined(RTCONFIG_LETSENCRYPT)
" libletsencrypt.so"
#endif
#if defined(RTCONFIG_AMAS)
" libamas-utils.so liblldpctl.so* libjansson.so*"
#endif
#if defined(RTCONFIG_WIFI_QCN5024_QCN5054)
" libqmi_cci.so libqmi_common_so.so"
#endif
#if defined(RTCONFIG_CONNDIAG)
" libconn_diag.so"
#endif /* RTCONFIG_CONNDIAG */
;
const char *kmod = "find /lib/modules -name '*.ko'|"
"grep '\\("
#if defined(RTCONFIG_BLINK_LED)
"bled\\|" /* bled.ko */
"usbcore\\|" /* usbcore.ko */
#if LINUX_KERNEL_VERSION >= KERNEL_VERSION(3,2,0)
"usb-common\\|" /* usb-common.ko, kernel 3.2 or above */
#endif
#endif
"nvram_linux\\)'"; /* nvram_linux.ko */
const char *modules = "find /lib/modules -name 'modules.dep'";
if (!newroot || *newroot == '\0')
newroot = TMP_ROOTFS_MNT_POINT;
if ((r = mount("tmpfs", newroot, "tmpfs", MS_NOATIME, "")) != 0)
return -1;
_dprintf("Build temp rootfs\n");
__cp("", "/dev", "", newroot);
if (!uname(&u)) {
snprintf(d1, sizeof(d1), "/lib/modules/%s", u.release);
__cp("", d1, "modules.dep", newroot);
}
__cp("", "/bin", bin, newroot);
__cp("", "/sbin", sbin, newroot);
__cp("", "/lib", lib, newroot);
__cp("", "/lib", "libcrypt*", newroot);
__cp("", "/usr/bin", usrbin, newroot);
#ifdef RTCONFIG_BCMARM
__cp("", "/usr/sbin", usrsbin, newroot);
#endif
__cp("", "/usr/lib", usrlib, newroot);
__cp("L", "/etc", "", newroot); /* don't creat symbolic link (/tmp/etc/foo) that will be broken soon */
#if defined(RTCONFIG_QCA)
__cp("", "/sbin", "wlanconfig", newroot);
__cp("", "/usr/lib", "libnl-tiny.so", newroot);
#if !defined(RTCONFIG_SWITCH_QCA8075_QCA8337_PHY_AQR107_AR8035_QCA8033) && \
!defined(RTCONFIG_SWITCH_QCA8075_PHY_AQR107) && \
!defined(RTCONFIG_SOC_IPQ60XX) && \
!defined(RTCONFIG_SWITCH_RTL8370M_PHY_QCA8033_X2) && \
!defined(RTCONFIG_SWITCH_RTL8370MB_PHY_QCA8033_X2)
__cp("", "/usr/sbin", "swconfig", newroot);
#endif
#if defined(RTCONFIG_SOC_IPQ8074) || \
defined(RTCONFIG_SOC_IPQ40XX)
__cp("", "/usr/sbin", "ssdk_sh", newroot);
#endif
#if defined(RTCONFIG_WIFI_QCA9990_QCA9990) || \
defined(RTCONFIG_WIFI_QCA9994_QCA9994) || \
defined(RTCONFIG_WIFI_QCN5024_QCN5054)
__cp("", "/usr/sbin", "thermald", newroot);
#endif
#if defined(RTCONFIG_CFG80211)
__cp("", "/usr/sbin", "cfg80211*", newroot);
__cp("R", "/lib/wifi", "*.xml", newroot);
#endif
#endif
#if defined(RTCONFIG_PPTPD)
if (nvram_match("pptpd_enable", "1")) {
__cp("", "/usr/sbin", "pptpd bcrelay", newroot);
}
#endif
#if defined(RTCONFIG_OPENVPN)
__cp("", "/usr/lib", "libvpn.so", newroot);
if (nvram_match("VPNServer_enable", "1")) {
__cp("", "/usr/sbin", "openvpn", newroot);
__cp("", "/usr/lib", "libz.so* liblzo2.so* liblz4.so*", newroot);
}
#endif
#if defined(RTCONFIG_IPSEC)
if (nvram_get_int("ipsec_server_enable") || nvram_get_int("ipsec_client_enable")
#ifdef RTCONFIG_INSTANT_GUARD
|| nvram_get_int("ipsec_ig_enable")
#endif
) {
__cp("", "/usr/sbin", "charon-cmd ipsec pki", newroot);
__cp("", "/usr/lib/ipsec", "starter charon", newroot);
__cp("R", "/usr/lib/ipsec", "", newroot);
}
#endif
/* copy mandatory kernel modules */
snprintf(d2, sizeof(d2), "tar cvf - `%s` `%s` | tar xf - -C %s", kmod, modules, newroot);
system(d2);
/* make directory, if not exist */
for (i = 0; i < ARRAY_SIZE(mdir); ++i) {
sprintf(d1, "%s/%s", newroot, mdir[i]);
if (stat(d1, &st) && (r = mkdir(d1, 0755) == -1))
return -3;
}
return 0;
}
/* Switch rootfilesystem to newroot.
* @newroot: Mount point of new root filesystem.
* @return:
* -1: Not init process.
* -2: chdir to newroot fail
* -3: Newroot is not a mount point
* -4: Move mount point to / fail
* -5: exec new init process fail
*/
static int switch_root(const char *newroot)
{
int r;
dev_t rdev;
struct stat st;
char *const argv[] = { "/sbin/init", "reboot", NULL };
if (!newroot || *newroot == '\0')
newroot = TMP_ROOTFS_MNT_POINT;
if (getpid() != 1) {
_dprintf("%s: PID != 1\n", __func__);
return -1;
}
if (chdir(newroot))
return -2;
stat("/", &st);
rdev = st.st_dev;
stat(".", &st);
if (rdev == st.st_dev)
return -3;
/* emulate switch_root command */
if ((r = mount(".", "/", NULL, MS_MOVE, NULL)) != 0)
return -4;
chroot(".");
chdir("/");
/* WARNING:
* If new rootfilesystem lacks libraries that are need by init process,
* kernel reboots system in 3 seconds.
*/
if ((r = execv(argv[0], argv)))
return -5;
/* NEVER REACH HERE */
return 0;
}
#else /* !RTCONFIG_TEMPROOTFS */
static inline int build_temp_rootfs(const char *newroot) { return -999; }
static inline int switch_root(const char *newroot) { return -999; }
#endif /* RTCONFIG_TEMPROOTFS */
#ifdef RTCONFIG_PERMISSION_MANAGEMENT
void PMS_Init_Database()
{
if (f_exists(PMS_DB_FILE)) return;
char para[256];
// create all tables
if (PMS_CreateAllTables() != 0)
{
printf("%s : create table error!\n", __FUNCTION__);
return;
}
// setup default account from nvram
PMS_ACCOUNT_INFO_T *tmp1 = NULL;
if ((tmp1 = PMS_list_Account_new(5, "1", nvram_safe_get("http_username"), nvram_safe_get("http_passwd"), "", "")) != NULL)
{
//printf("%s : %d>%s>%s>%s>%s\n", __FUNCTION__, tmp1->active, tmp1->name, tmp1->passwd, tmp1->desc, tmp1->email);
PMS_ActionAccountInfo(PMS_ACTION_UPDATE, (void *)tmp1, 0);
PMS_list_ACCOUNT_free(tmp1);
}
// setup default account group : Adminstrator / FrontDesk / default
PMS_ACCOUNT_GROUP_INFO_T *tmp2 = NULL;
if ((tmp2 = PMS_list_AccountGroup_new(3, "1", "Administrator", "")) != NULL)
{
PMS_ActionAccountInfo(PMS_ACTION_UPDATE, (void *)tmp2, 1);
PMS_list_ACCOUNT_GROUP_free(tmp2);
}
/* remove in 2016/11/21 due to FrontDesk function isn't ready, after function done, please rollback this */
#if 0
if ((tmp2 = PMS_list_AccountGroup_new(3, "0", "FrontDesk", "")) != NULL)
{
PMS_ActionAccountInfo(PMS_ACTION_UPDATE, (void *)tmp2, 1);
PMS_list_ACCOUNT_GROUP_free(tmp2);
}
#endif
if ((tmp2 = PMS_list_AccountGroup_new(3, "1", "default", "")) != NULL)
{
PMS_ActionAccountInfo(PMS_ACTION_UPDATE, (void *)tmp2, 1);
PMS_list_ACCOUNT_GROUP_free(tmp2);
}
// setup account / group matching table from account
snprintf(para, sizeof(para), "%s>Administrator>FrontDesk", nvram_safe_get("http_username"));
PMS_ActAccMatchInfo(PMS_ACTION_UPDATE, 3, para);
// setup default device group : Adminstrator / default
PMS_DEVICE_GROUP_INFO_T *tmp3 = NULL;
if ((tmp3 = PMS_list_DeviceGroup_new(3, "1", "Administrator", "")) != NULL)
{
PMS_ActionDeviceInfo(PMS_ACTION_UPDATE, (void *)tmp3, 1);
PMS_list_DEVICE_GROUP_free(tmp3);
}
if ((tmp3 = PMS_list_DeviceGroup_new(3, "1", "default", "")) != NULL)
{
PMS_ActionDeviceInfo(PMS_ACTION_UPDATE, (void *)tmp3, 1);
PMS_list_DEVICE_GROUP_free(tmp3);
}
}
#endif
void setup_passwd(void)
{
#ifdef RTCONFIG_PERMISSION_MANAGEMENT
PMS_Init_Database();
#endif
create_passwd();
}
static void
nvram_fsafe_get(const char *nvram, char *nvram_buf, size_t len)
{
int try = 3;
do {
if(try != 3) // dont usleep the first time
{
dbg("[%s:(%d)]:get empty nvram(%s)\n", __FUNCTION__, __LINE__, nvram);
usleep(500);
}
strlcpy(nvram_buf, nvram_safe_get(nvram), len);
try--;
} while(strlen(nvram_buf) == 0 && try > 0);
}
void create_passwd(void)
{
char s[512];
char *p;
char salt[32];
FILE *f;
mode_t m;
char http_user[128] = {0};
#ifdef RTCONFIG_NVRAM_ENCRYPT
char dec_passwd[64];
#endif
char passwd_buf[128] = {0};
#ifdef RTCONFIG_SAMBASRV //!!TB
char *smbd_user;
create_custom_passwd();
#endif
#ifdef RTCONFIG_OPENVPN
mkdir_if_none("/etc/pam.d");
f_write_string("/etc/pam.d/openvpn",
"auth required pam_unix.so\n",
0, 0644);
create_ovpn_passwd();
#endif
strcpy(salt, "$1$");
f_read("/dev/urandom", s, 6);
base64_encode((unsigned char *) s, salt + 3, 6);
salt[3 + 8] = 0;
p = salt;
while (*p) {
if (*p == '+') *p = '.';
++p;
}
if (((p = nvram_get("http_passwd")) == NULL) || (*p == 0)){
p = "admin";
}
#ifdef RTCONFIG_NVRAM_ENCRYPT
else{
memset(dec_passwd, 0, sizeof(dec_passwd));
nvram_fsafe_get("http_passwd", passwd_buf, sizeof(passwd_buf));
pw_dec(passwd_buf, dec_passwd, sizeof(dec_passwd));
p = dec_passwd;
}
#endif
//if (((http_user = nvram_get("http_username")) == NULL) || (*http_user == 0)) http_user = "admin";
nvram_fsafe_get("http_username", http_user, sizeof(http_user));
#ifdef RTCONFIG_SAMBASRV //!!TB
if (((smbd_user = nvram_get("smbd_user")) == NULL) || (*smbd_user == 0) || !strcmp(smbd_user, "root"))
smbd_user = "nas";
#endif
m = umask(0777);
if ((f = fopen("/etc/shadow", "w")) != NULL) {
p = crypt(p, salt);
fprintf(f,
"%s:%s::0:99999:7:0::\n"
#ifdef RTCONFIG_TOR
"tor:*::0:99999:7:0::\n"
#endif
#ifdef RTCONFIG_SAMBASRV //!!TB
"%s:*::0:99999:7:0::\n"
#endif
"nobody:*::0:99999:7:0::\n"
, http_user, p
#ifdef RTCONFIG_SAMBASRV //!!TB
, smbd_user
#endif
);
fappend(f, "/etc/shadow.custom");
#ifdef RTCONFIG_OPENVPN
fappend(f, "/etc/shadow.openvpn");
#endif
#ifdef RTCONFIG_COOVACHILLI
fappend(f, "/etc/shadow.chilli");
fappend(f, "/etc/shadow.chilli-cp");
#endif
append_custom_config("shadow", f);
fclose(f);
run_postconf("shadow", "/etc/shadow");
}
umask(m);
chmod("/etc/shadow", 0600);
sprintf(s,
"%s:x:0:0:%s:/root:/bin/sh\n"
#ifdef RTCONFIG_SAMBASRV //!!TB
"%s:x:100:100:nas:/dev/null:/dev/null\n"
#endif //!!TB
"nobody:x:65534:65534:nobody:/dev/null:/dev/null\n"
#ifdef RTCONFIG_TOR
"tor:x:65533:65533:tor:/dev/null:/dev/null\n"
#endif
, http_user, http_user
#ifdef RTCONFIG_SAMBASRV //!!TB
, smbd_user
#endif //!!TB
);
f_write_string("/etc/passwd", s, 0, 0644);
fappend_file("/etc/passwd", "/etc/passwd.custom");
fappend_file("/etc/passwd", "/jffs/configs/passwd.add");
run_postconf("passwd","/etc/passwd");
#ifdef RTCONFIG_OPENVPN
// TODO: figure out if libvpn does anything special here?
// append_ovpn_accnt("/etc/passwd", "/etc/passwd.openvpn");
fappend_file("/etc/passwd", "/etc/passwd.openvpn");
#endif
sprintf(s,
"root:*:0:\n"
#ifdef RTCONFIG_SAMBASRV //!!TB
"nas:*:100:\n"
#endif
#ifdef RTCONFIG_OPENVPN
"openvpn:x:200:\n" /* OpenVPN GID */
#endif
"nobody:*:65534:\n"
#ifdef RTCONFIG_TOR
"tor:*:65533:\n"
#endif
);
f_write_string("/etc/gshadow", s, 0, 0644);
fappend_file("/etc/gshadow", "/etc/gshadow.custom");
fappend_file("/etc/gshadow", "/jffs/configs/gshadow.add");
run_postconf("gshadow","/etc/gshadow");
f_write_string("/etc/group",
"root:x:0:\n"
#ifdef RTCONFIG_SAMBASRV //!!TB
"nas:x:100:\n"
#endif
#ifdef RTCONFIG_OPENVPN
"openvpn:x:200:\n" /* OpenVPN GID */
#endif
"nobody:x:65534:\n"
#ifdef RTCONFIG_TOR
"tor:x:65533:\n"
#endif
,0 , 0644);
fappend_file("/etc/group", "/etc/group.custom");
fappend_file("/etc/group", "/jffs/configs/group.add");
run_postconf("group","/etc/group");
}
void get_dhcp_pool(char **dhcp_start, char **dhcp_end, char *buffer)
{
if (dhcp_start == NULL || dhcp_end == NULL || buffer == NULL)
return;
if ((repeater_mode()
#if defined(RTCONFIG_BCMWL6) && defined(RTCONFIG_PROXYSTA)
|| psr_mode() || mediabridge_mode()
#elif defined(RTCONFIG_REALTEK) || defined(RTCONFIG_QCA)
|| mediabridge_mode()
#endif
#ifdef RTCONFIG_DPSTA
|| (dpsta_mode() && nvram_get_int("re_mode") == 0)
#endif
) && nvram_get_int("wlc_state") != WLC_STATE_CONNECTED) {
if(nvram_match("lan_proto", "static")) {
unsigned int lan_ipaddr, lan_netmask;
char *p = buffer;
struct in_addr lan1, lan2;
unsigned offset;
lan_ipaddr = ntohl(inet_addr("192.168.1.1"));
lan_netmask = ntohl(inet_addr("255.255.255.0"));
// cprintf("#### lan_ipaddr(%08x) lan_netmask(%08x)\n", lan_ipaddr, lan_netmask);
//start
if (((lan_ipaddr & lan_netmask) | 1) == lan_ipaddr)
offset = 2;
else
offset = 1;
lan1.s_addr = htonl((lan_ipaddr & lan_netmask) | offset);
*dhcp_start = p;
p += sprintf(p, "%s", inet_ntoa(lan1));
p += 1;
//end
if (((lan_ipaddr & lan_netmask) | 254) == lan_ipaddr)
offset = 253;
else
offset = 254;
lan2.s_addr = htonl((lan_ipaddr & lan_netmask) | offset);
*dhcp_end = p;
p += sprintf(p, "%s", inet_ntoa(lan2));
p += 1;
// cprintf("#### dhcp_start(%s) dhcp_end(%s)\n", *dhcp_start, *dhcp_end);
} else {
*dhcp_start = nvram_default_get("dhcp_start");
*dhcp_end = nvram_default_get("dhcp_end");
}
}
else
{
*dhcp_start = nvram_safe_get("dhcp_start");
*dhcp_end = nvram_safe_get("dhcp_end");
}
}
#if 0
int get_dhcpd_lmax()
{
unsigned int lstart, lend, lip;
int dhlease_size, invalid_ipnum, except_lanip;
char *dhcp_start, *dhcp_end, *lan_netmask, *lan_ipaddr;
char buffer[64];
get_dhcp_pool(&dhcp_start, &dhcp_end, buffer);
#ifdef RTCONFIG_WIRELESSREPEATER
if(sw_mode() == SW_MODE_REPEATER && nvram_get_int("wlc_state") != WLC_STATE_CONNECTED){
lan_netmask = nvram_default_get("lan_netmask");
lan_ipaddr = nvram_default_get("lan_ipaddr");
}
else
#endif
{
lan_netmask = nvram_safe_get("lan_netmask");
lan_ipaddr = nvram_safe_get("lan_ipaddr");
}
lstart = htonl(inet_addr(dhcp_start)) & ~htonl(inet_addr(lan_netmask));
lend = htonl(inet_addr(dhcp_end)) & ~htonl(inet_addr(lan_netmask));
lip = htonl(inet_addr(lan_ipaddr)) & ~htonl(inet_addr(lan_netmask));
dhlease_size = lend - lstart + 1;
invalid_ipnum = dhlease_size / 256 * 2;
except_lanip = (lip >= lstart && lip <= lend)? 1:0;
dhlease_size -= invalid_ipnum + except_lanip;
return dhlease_size;
}
#endif
#ifdef RTCONFIG_CONNTRACK
void stop_pctime_service()
{
killall("pctime", SIGTERM);
}
void start_pctime_service()
{
char *cmd[] = {"pctime", NULL};
int pid;
if (!is_router_mode())
return;
if (!pids("pctime")) {
_eval(cmd, NULL, 0, &pid);
}
}
#endif