source: trunk/guitools/shared/smbtree.vrs

Last change on this file was 1055, checked in by Herwig Bauernfeind, 8 years ago

Unify debug formatting

File size: 32.1 KB
Line 
1/* Samba REXX Routines to handle smbtree output */
2
3/*
4Copyright (C) 2007-2017 Herwig Bauernfeind for bww bitwise works GmbH.
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20
21/*:VRX */
22_RefreshTree:
23 say time()' _RefreshTree() started'
24 FirstRun = 0
25 call _StatusBarWrapper "Refreshing network"
26 ok = SysFileDelete(samba.!msg)
27 parse var debuglevel .'='level
28 if level = 0 then debuglevel = ' --debuglevel=1'
29
30 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' | UserCred = '--user=%%' then UserCred = '-N'
31 if ShowHidden = 'SHOWHIDDEN' | ShowHidden = '' then ShowHidden = 0
32 if BroadCast = 1 then BroadCast = '-b'; else BroadCast = ''
33
34 say " UserCred="UserCred
35 samba.!serverlist = TempDir'smbtree.srvlst'
36
37 /* smbtree -b = Use broadcast instead of using the master browser
38 smbtree -D = List only domains (workgroups) of tree
39 smbtree -S = List domains(workgroups) and servers of */
40 say ' detach 'samba.!smbtreeexe' 'BroadCast' -S 'UserCred' 'debuglevel' 2>'samba.!error' 1>'samba.!serverlist
41 address cmd 'detach 'samba.!smbtreeexe' 'BroadCast' -S 'UserCred' 'debuglevel' 2>'samba.!error' 1>'samba.!serverlist
42
43 if level = 0 then debuglevel = ' --debuglevel=0'
44
45 /* strip username from caption */
46 UserContext = VRGet("CN_SMBTREE","Caption")
47 UserContext = strip(DelWord(UserContext,words(UserContext)))
48
49 if UserCred = '-N' then ok = VRSet("CN_SMBTREE","Caption",UserContext" Guest")
50 else do
51 parse var UserCred '--user='username'%'.
52 ok = VRSet("CN_SMBTREE","Caption",UserContext" "username)
53 end
54
55 if BroadCast = '-b' then BroadCast = 1; else BroadCast = 0
56 if UserCred = '-N' then UserCred = ''
57
58 ok = VRMethod("CN_smbtree", "RemoveRecord", "ALL")
59
60 RefreshMode = "TREE"
61 ok = VRSet("CN_smbtree","Enabled", 0)
62 ok = VRset("TM_RefreshTreeDisplay","Enabled",1)
63
64 say time()' _RefreshTree() done'
65return
66
67/*:VRX */
68_RefreshTreeDisplay:
69 say time()' _RefreshTreeDisplay() started'
70
71 errstat = stream(samba.!error,'c','open read')
72 if errstat = "READY:" then do
73 if file2stem(samba.!error,"treeError.") > 1 then do
74 call _StatusBarWrapper treeError.2
75 end
76 end
77
78 stat = stream(samba.!serverlist,'c','open read')
79 if stat <> "READY:" then do
80 call _StatusBarWrapper '+.'
81 return
82 end
83
84 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' | UserCred = '--user=%%' then UserCred = '-N'
85 if ShowHidden = 'SHOWHIDDEN' | ShowHidden = '' then ShowHidden = 0
86 if BroadCast = 1 then BroadCast = '-b'; else BroadCast = ''
87
88 if UserCred = '-N' then ok = VRSet("CN_SMBTREE","Caption",Usercontext" Guest")
89 else do
90 parse var UserCred '--user='username'%'.
91 ok = VRSet("CN_SMBTREE","Caption", Usercontext" "username)
92 end
93
94 ok = VRset("TM_RefreshTreeDisplay","Enabled",0)
95 ok = VRSet("CN_smbtree", 'Enabled', 0 )
96
97 ok = VRSet( "CN_smbtree", "Painting", 0 )
98 ok = VRMethod("CN_smbtree", "RemoveRecord", "ALL")
99
100 drop smbtree.
101
102 ok = file2stem(samba.!serverlist,"smbtreeline.")
103 if smbtreeline.0 = 0 then do
104 ok = VRSet("TM_Throbber","Enabled", 0)
105 ok = VRSet("Pict_Throbber","Visible", 0)
106 end
107
108
109 do sl = 1 to smbtreeline.0
110 Header = c2x(left(smbtreeline.sl,3))
111 select
112 when Header = "09095C" then nop /* share - obsolete, we only list domains and servers here */
113 when Header = "095C5C" then do /* machine */
114 smbtreeline.sl = strip(smbtreeline.sl,,'09'x)
115 parse var smbtreeline.sl '\\'machine '0909'x comment
116 machine = strip(machine)
117 comment = strip(comment)
118 if VRGet("CN_smbtree","View") = "IconTree" then parent = smbtree.!workgroup; else parent = ""
119 /* We create any machine as sleeping initially */
120 smbtree.!machine = _AddSleepingMachine(machine,comment,parent)
121
122 ok = VRSet( "CN_smbtree", "Painting", 1 )
123 ok = VRSet( "CN_smbtree", "Painting", 0 )
124
125 if VRGet("CN_smbtree","View") <> "Detail" then do
126 /* Tree view */
127 call _RefreshShares
128 end
129 else do /* Fill records for details view */
130 ok = VRSet("Main", 'Pointer', 'Wait' )
131 /* Get NMBLookup Status for machine */
132 NMBStatus = _GetMachineNMBSTatus(machine)
133 parse var NMBStatus IPStr'|'MAC '|' Roles; drop NMBStatus
134 if pos("PDC",Roles) > 0 then ok = VRMethod( "CN_smbtree", "SetRecordAttr", smbtree.!machine, "Icon","#63:PMWP.DLL")
135 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, IPFH, IPStr,MBFH, Roles, MacFH,MAC)
136
137 /* FIXME: Possibly obsolete */
138 parse var IPStr MachineIP ',' .
139 if strip(MachineIP) = "" then MachineIP = machine
140
141 ok = VRSet("Main", 'Pointer', '<default>' )
142
143 /* Find out OS, version */
144 say ' 'samba.!smbclientexe' -L "'machine'" 'UserCred' 'debuglevel' 2>'samba.!msg' 1>NUL'
145 address cmd samba.!smbclientexe' -L "'machine'" 'UserCred' 'debuglevel' 2>'samba.!msg' 1>NUL'
146 smbline = linein(samba.!msg)
147 ok = stream(samba.!msg,'c','close')
148 parse var smbline "Domain=["WorkGroup"] OS=["OS"] Server=["Version"]"Rest
149 ok = VRMethod( "CN_smbtree", "SetFieldData", smbtree.!machine, OSFH, OS, VersionFH, Version, WorkgroupFH, WorkGroup)
150 end
151 end
152 when smbtreeline.sl = "" then nop /* skip empty lines */
153 otherwise do /* possible new workgroup */
154 say ' Possible workgroup: "'smbtreeline.sl'"'
155 ThrowMsg = 0
156 /* Do some checks to be sure */
157 select
158 when pos("RECEIVING",translate(smbtreeline.sl)) > 0 then ThrowMsg = 1
159 when pos("NT_STATUS",translate(smbtreeline.sl)) > 0 then ThrowMsg = 1
160 when pos(" ", smbtreeline.sl) > 0 then ThrowMsg = 1
161 when pos("=", smbtreeline.sl) > 0 then ThrowMsg = 1
162 when pos(":", smbtreeline.sl) > 0 then ThrowMsg = 1
163 when pos("%", smbtreeline.sl) > 0 then ThrowMsg = 1
164 otherwise do /* it is really a new workgroup */
165 if VRGet("CN_smbtree","View") = "IconTree" then do
166 say "Checks passed, "smbtreeline.sl" is a workgroup!"
167 smbtree.!workgroup = _AddWorkGroup(smbtreeline.sl)
168 CurWG = smbtreeline.sl
169 end
170 end
171 end
172 if ThrowMsg = 1 then do
173 ThrowMsg = 0
174 Msg.Type = "W"
175 Msg.Text = smbtreeline.sl
176 call _ShowMsg
177 end
178 end
179 end
180 end
181
182 ok = VRSet( "CN_smbtree", "Painting", 1 )
183
184/* ok = VRSet("Main", 'Pointer', '<default>' ) */
185 ok = VRSet("CN_smbtree","Enabled", 1)
186/* ok = VRSet("TM_Throbber","Enabled", 0)
187 ok = VRSet("Pict_Throbber","Visible", 0) */
188 say time()' _RefreshTreeDisplay() done'
189return
190
191/*:VRX _RefreshShares */
192_RefreshShares:
193 say time()' _RefreshShares() started'
194
195 smbmachine = TempDir||"smbmachine."||machine
196 MaxSmbClient = 32 /* Do not run more than MaxSmbClient instances of smbclient.exe at the same time */
197
198 Defer = 1
199 do while Defer = 1
200 SmbCltCount = 0
201 ok = PRProcessList(proc)
202
203 do I = 1 to proc.0
204 CurProc = VRParseFileName(proc.i.name,'NE')
205 if CurProc = "SMBCLIENT.EXE" then SmbCltCount = SmbCltCount + 1
206 end
207 say ' 'SmbCltCount' instance(s) of 'samba.!smbclientexe' is/are running.'
208 if SmbCltCount >= MaxSmbClient then do
209 say " Waiting until at least "SmbCltCount-MaxSmbClient+1" instance(s) of smbclient.exe terminate(s)."
210 ok = SysSleep(1)
211 end
212 else Defer = 0
213 end
214
215 if UserCred = 'USERCRED' | UserCred = '' | UserCred = '--user=%' | UserCred = '--user=%%' then UserCred = '-N'
216
217 /* We have to remove the double % for smbclient.exe - not entirely clear why */
218 OldUserCred = ""
219 if pos('%%',UserCred) > 0 & pos("4OS2", value("COMSPEC",,"OS2ENVIRONMENT")) = 0 then do
220 OldUserCred = UserCred
221 parse var UserCred '--user='username'%%'password
222 UserCred = '--user='username'%'password
223 say " Strip double %%!!!"
224 end
225
226 say ' detach 'samba.!smbclientexe' -L "'machine'" 'UserCred' 'debuglevel' 2>'smbmachine' 1>&2'
227 address cmd 'detach 'samba.!smbclientexe' -L "'machine'" 'UserCred' 'debuglevel' 2>'smbmachine' 1>&2'
228
229 if UserCred = '-N' then ok = VRSet("CN_SMBTREE","Caption",Usercontext" Guest")
230 else do
231 parse var UserCred '--user='username'%'.
232 ok = VRSet("CN_SMBTREE","Caption",Usercontext" "username)
233 end
234
235 if OldUserCred <> "" then do
236 UserCred = OldUserCred
237 OldUserCred = ""
238 say " Restore double %%!!!"
239 end
240
241 if UserCred = '-N' then UserCred = ''
242
243 RefreshMode = "SHARE"
244
245 ok = VRSet("CN_smbtree","Enabled", 0)
246 ok = VRset("TM_RefreshTreeDisplay","Enabled",1)
247 say time()' _RefreshShares() done'
248return
249
250/*:VRX _AddSharesDisplay
251*/
252_AddSharesDisplay: /* New get shares code - uses smbclient output and is much faster */
253 say time()' _AddSharesDisplay() started'
254
255 ok = SysFileTree(Tempdir||'smbmachine.*',smbmachine.,'FO')
256 if smbmachine.0 = 0 then do /* we are done, no more files around, cleanup, disable Timer and exit */
257 RefreshMode = ""
258 ok = VRset("TM_RefreshTreeDisplay","Enabled", 0)
259 ok = VRSet("TM_RefreshTreeDisplay","Delay", 1000)
260 ok = VRSet("CN_smbtree","Enabled", 1)
261 if DoLMHosts = 1 then do
262 call _LMHostsRead