1# <20180510.1546.06>
2
3# This command attempts to remove any old temp files
4
5proc clearOldTmp {} {
6  global glob env
7  set mes {}
8  if {[file exists $glob(conf_dir)/tmp]} {
9    # still around, can we prove it is stale?
10    set files [glob -directory $glob(conf_dir)/tmp/ * .*]
11    set rm {}
12    set bt 0
13    set btl {}
14    frputs files
15    if {$files != {}} {
16      set rm $files
17      # Lets see if we can come up with an idea of boot time
18      if {! $::MSW} {
19	if {[file exists /proc]} {
20	  # This is close enough and quite fast if it is Linux
21	  lappend btt [file mtime /proc]
22	}
23	if {[catch {exec uptime} utout] == 0 &&\
24		      [regexp {.* up ([0-9]+) days.*} $utout duh days]} {
25	  # this should work for most other *nixs (86400 seconds in a day)
26	  lappend btt [expr {[clock seconds] - ($days * 86400)}]
27	}
28      } else {
29	if {[catch {twapi::get_system_uptime} ti] == 0 && $ti > 60} {
30	  lappend btl [expr {$bt - $ti}]
31	} elseif {[catch {exec wmic os get lastbootuptime} btout] == 0} {
32	  set btout [regsub -all {[^0-9]*|\..*$} $btout {}]
33	  lappend btl [clock scan $btout -format {%Y%m%d%H%M%S}]
34	  frputs btl
35	} elseif {[file exists $env(SystemDrive)/TEMP]} {
36	  # This seems to be useful. I think it is reset at boot time
37	  # and nobody uses it (Windows/TEMP is used instead)
38	  lappend btl [file mtime  $env(SystemDrive)/TEMP]
39	  frputs btl
40	}
41      }
42      foreach btt $btl {
43	frputs "[clock format $btt] "
44	if {$btt < $bt} {
45	  set bt $btt
46	}
47      }
48      # If we are here we have a reasonable boot time
49      # Well, maybe not, but then it is 0
50      set rm {}
51      set mes {}
52      foreach file $files {
53	if {[file mtime $file] < $bt} {
54	  if {[catch {file delete -force $file} ms]} {
55	    lappend mes $ms
56	  }
57	} else {
58	  lappend rm $file
59	}
60      }
61    }
62    if {$rm == {}} {
63      frputs
64      if {[catch {file delete -force $glob(conf_dir)/tmp} ms]} {
65	lappend mes $ms
66      }
67    }
68  }
69  if {$mes != {}} {
70    lappend ::mess "Error trying to remove old $glob(conf_dir)/tmp:" {*}$mes
71  }
72}
73
74
75# The purpose of this code is to update the config file of 17.04.01.19
76# to the new format. The changes are:
77#
78# 1.) the VFS package for adb (and thus its URL) changes from
79#     'aftp' to 'adb'. This may appear in the 'Hotlist'
80#
81# 2.) the 'ftp' log in list (names & passwords) are moved from
82#     config(ftp,login) to config(vfs,login). In addition, the
83#     name of the target is now a full URL.
84#
85# This code moves 'ftp' logins to the new list and format insofar
86# as it can. If a particular login in the 'ftp' list can not be
87# figured out, it is left there with a note to the user each time
88# filerunner runs to do the conversion manually.
89#
90proc update17.04.19 {} {
91  global config
92  # First fix up any "aftp" entries in the hotlist
93  # We are going to cheat here and treat the hotlist as a string
94  #
95  set test $config(hotlist)
96  set config(hotlist) [regsub -all {aftp://} $config(hotlist) {adb://}]
97  set configMod [expr {$test != $config(hotlist)}]
98
99  # Gosh, that was easy!
100
101  proc getHotListEnt {hlist} {
102    upvar url url
103
104    foreach ent $hlist {
105      lassign $ent name value
106      if {$value == {}} {
107	set value $name
108      }
109      if {[string index $name 0] != "-"} {
110	if {[string match *://* $value]} {
111	  if {![IsVFS $value]} {continue}
112	  lappend url $VFStok
113	  set url [lsort -unique $url]
114	}
115	continue
116      }
117      getHotListEnt $value
118    }
119  }
120  set url {}
121  getHotListEnt $config(hotlist)
122  # Lets try to enumerate the CIFS systems.
123  # This should put all we know about into the passwordLocker
124  set openPassword [findUNC::enumCIFS]
125
126  # now the logins
127  set suggestion {}
128  set result {}
129  if {[info exists config(ftp,login)]} {
130    foreach ent $config(ftp,login) {
131      set left [lassign $ent add namePass]
132      lassign $namePass name Pass
133      set can ${name}@$add
134      lassign [split $add .] possProt
135      # set passWords(${name}@$add) $Pass
136
137      # Lets see how well we do just guessing...
138      # lassign [split $can @] name addr
139      lassign [split $add :] addr2 port
140      set found 0
141      foreach ent2 $url {
142	IsVFS $ent2
143	lassign [split $VFSadd @] ename eadd
144	if {$eadd == {}} {
145	  set eadd $ename
146	  set ename {}
147	}
148	if {$add == $eadd && ($ename == {} || $ename == $name)} {
149	  if {$port != 3721 || $VFSpro == "adb" || [llength $Pass] > 4} {
150	    incr found
151	    lappend result  [list ${VFSpro}://$can $Pass {*}$left]
152	  }
153	}
154      }
155      # Here we dig a bit deeper...
156      # We may double up here, but we will fix that with sort
157      if {$possProt in {ftp sftp adb rsync}} {
158	lappend result [list ${possProt}://$can $Pass {*}$left]
159	incr found
160      }
161      set yes 0
162      foreach pat {usb ser* emu* pro* con*} {
163	if {[string match $pat $name]} {
164	  set yes 1
165	  break
166	}
167      }
168      if {($yes || $port == 3721) && [llength $Pass] <= 4} {
169	# This looks like an adb connection...
170	lappend result [list "adb://$can" $Pass {*}$left]
171	incr found
172      } elseif {$name == "ftp"} {
173	# public ftp
174	lappend result [list  "ftp://$can"  $Pass {*}$left]
175	incr found
176      } elseif {$found == 0} {
177	# Unknown, put it in the revised ftn,login
178	lappend suggestion $ent
179      }
180    }
181    set result [lsort -unique $result]
182    set lenRemain [llength $suggestion]
183    set orCount [expr {[llength $config(ftp,login)] - $lenRemain}]
184    set mess0 "Filerunner has converted $orCount of your ftp login(s) to [llength $result]\
185VFS login(s).
186Please check the result for correctness by"
187
188    set messRm "Filerunner could not convert $lenRemain ftp login(s).
189These remain in config(ftp,login).
190Please either manually convert these
191or eliminate them by"
192
193    set messEd "editing the Configuration:
194\"Configuration -> Edit Configuration\"
195section \"VFS\",\"Vfs Login\" and \"Ftp Login\""
196
197    set messDim "\n\nThis message is also at the beginning of the
198\"Log\" which may be viewed by clicking
199\"File -> View Log...\".\n
200Click this window to dismess it."
201
202    switch [expr {[llength $result] > 0 | (($lenRemain > 0) * 2)}] {
203      0 {set mes {}}
204      1 {set mes "$mess0 $messEd $messDim"}
205      2 {set mes "$messRm $messEd $messDim"}
206      3 {set mes "$mess0 $messEd \n\n$messRm $messEd $messDim"}
207    }
208    set test [lnorm $config(ftp,login)]
209    if {[info exist config(vfs,login)]} {
210      set loginTmp $config(vfs,login)
211    }
212    if {[info exists loginTmp]} {
213      set loginTmp [lsort -unique [concat $loginTmp $result]]
214    } elseif {$result != {}} {
215      set loginTmp  [lsort -unique $result]
216    }
217    set config(ftp,login) $suggestion
218    set configMod [expr {$configMod || $test != [lnorm $config(ftp,login)]}]
219    # SaveConfig no longer saves loginTmp
220    # so it is really just a local tmp
221  }
222  # Ok, step two, move all of these to the passwordLocker.
223  # Be mindfull of their might already be entries there.
224  set test $config(passwordLocker)
225  if {[info exists loginTmp]} {
226    foreach ent $loginTmp {
227      if {$ent ni $config(passwordLocker)} {
228	lappend config(passwordLocker) $ent
229      }
230    }
231  }
232  # Now lets grab the passwords from the rsync table...
233  set new {}
234  set dPwd {}
235  foreach ent $config(rsync,table) {
236    lassign $ent add du duh duhh pwd
237    set try {}
238    if {$add == {} && $pwd == {}} {continue}
239    # what sort of thing is it?
240    # Use the default pwd if this entry is nil
241    if {$pwd == {}} {set pwd $dPwd}
242    if {[IsVFS $add]} {
243      set try $add
244    } elseif {[string range $add 0 1] == {//}} {
245      set try "cifs:$add"
246      # lappend new [list "cifs:$add" $pwd]
247    } elseif {[string match *:* $add]} {
248      # Lets assume this is "nfs" until we know better
249      set try "nfs://$add"
250    } elseif {$add == {}} {
251      # this is the defaults. Save the password
252      set dPwd $pwd
253    }
254    # Make sure we have a username in the entry
255    if {[IsVFS $try]} {
256      lassign  [split $VFSadd @] user ladd
257      if {$ladd != {}} {
258	set try $VFSpro://$::env(USER)@$ladd/$VFSpath
259      }
260    }
261
262    if {$try != {}} {
263      if {[set indx [lsearch -exact -index 0 $config(passwordLocker) $try]] == -1} {
264	lappend new [list $try $pwd]
265      } else {
266	# make sure there was a pw
267	set ext [lassign [lindex $config(passwordLocker) $indx] ad pw]
268	frputs ad pw ext pwd
269	if {$pw == {} && $pwd != {}} {
270	  set config(passwordLocker) [lreplace $config(passwordLocker) $indx $indx \
271					  [list $ad $pwd {*}$ext]]
272	}
273      }
274    }
275  }
276
277  set configMod [expr {$configMod || $config(passwordLocker) != $test}]
278
279  if {[info exist openPassword] && $configMod} {
280    unset openPassword
281    append mes "\nFound open passwords in one or more of the files:\
282             \n\"/etc/fstab\", \"/etc/mtab\" and \"/etc/auto.misc\"\
283             \n\nJust saying"
284  }
285  if {$configMod} {
286    SaveConfig
287  }
288  append mes [clearOldTmp]
289  # lh $result
290  # lh [lsort -unique [concat [lnorm $loginTmp] $result]]
291  return $mes
292}
293