1/*
2
3	Tomato Firmware
4	Copyright (C) 2006-2009 Jonathan Zarate
5
6*/
7
8#include "rc.h"
9
10#include <sys/mount.h>
11#include <sys/stat.h>
12
13
14void start_cifs(void)
15{
16	xstart("mount-cifs", "-m");
17}
18
19void stop_cifs(void)
20{
21	killall("mount-cifs", SIGTERM);
22	eval("mount-cifs", "-u");
23}
24
25int mount_cifs_main(int argc, char *argv[])
26{
27	char s[512];
28	char opt[512];
29	char mpath[32];
30	int i, j;
31	int try;
32	int first;
33	char *on, *unc, *user, *pass, *dom, *exec, *servern, *sec, *custom;
34	int done[3];
35
36	if (argc == 2) {
37		if (strcmp(argv[1], "-m") == 0) {
38			done[1] = 0;
39			done[2] = 0;
40			first = 1;
41			try = 0;
42			while (1) {
43				for (i = 1; i <= 2; ++i) {
44					if (done[i]) continue;
45
46					done[i] = 2;
47
48					sprintf(s, "cifs%d", i);
49					strlcpy(s, nvram_safe_get(s), sizeof(s));
50					if ((vstrsep(s, "<", &on, &unc, &user, &pass, &dom, &exec, &servern, &sec) != 8) || (*on != '1')) continue;
51					custom = nvram_safe_get("cifs_opts");
52
53					done[i] = 0;
54
55					if (first) {
56						notice_set("cifs", "Mounting...");
57						modprobe("cifs");
58						first = 0;
59					}
60
61					sprintf(mpath, "/cifs%d", i);
62					umount(mpath);
63# ifndef RTCONFIG_BCMARM
64					j = sprintf(opt, "sep=<unc=%s", unc);
65					if (*user) j += sprintf(opt + j, "<user=%s", user);
66					if (*pass) j += sprintf(opt + j, "<pass=%s", pass);
67					if (*dom) j += sprintf(opt + j, "<dom=%s", dom);
68					if (*servern) j += sprintf(opt + j, "<servern=%s", servern);
69					if (*sec) j += sprintf(opt + j, "<sec=%s", sec);
70					if (*custom) j += sprintf(opt + j, "<%s", custom);
71
72					if (mount("-", mpath, "cifs", MS_NOATIME|MS_NODIRATIME, opt) != 0) continue;
73# else
74					j = sprintf(opt, "nounix,noserverino");
75					if (*user) j += sprintf(opt + j, ",username=%s", user);
76					if (*pass) j += sprintf(opt + j, ",password=%s", pass);
77
78					if (mount(unc, mpath, "cifs", MS_NOATIME|MS_NODIRATIME, opt) != 0) continue;
79# endif
80					done[i] = 1;
81					if (try > 12) try = 12;	// 1 min
82
83					if (*exec) {
84						chdir(mpath);
85						system(exec);
86					}
87					run_userfile(mpath, ".asusrouter", NULL, 3);
88				}
89				if ((done[1]) && (done[2])) {
90					notice_set("cifs", "");
91					return 0;
92				}
93
94				sleep(5 * ++try);
95				if (try == 24) {	// 2 min
96					sprintf(s, "Error mounting CIFS #%s. Still trying... ", (done[1] == done[2]) ? "1 and #2" : ((done[1] == 0) ? "1" : "2"));
97					notice_set("cifs", s);
98				}
99				else if (try > 180) {	// 15 mins
100					try = 180;
101				}
102			}
103
104			return 1;
105		}
106		if (strcmp(argv[1], "-u") == 0) {
107			for (i = 1; i <= 2; ++i) {
108				sprintf(mpath, "/cifs%d", i);
109				umount(mpath);
110			}
111			modprobe_r("cifs");
112			notice_set("cifs", "");
113			return 0;
114		}
115	}
116
117	usage_exit(argv[0], "-m|-u");
118	return 1;
119}
120