summaryrefslogtreecommitdiffstats
path: root/squid-model.lua
blob: 3b55d60183ba0b4a2c7b4a03975692fa549c332c (plain)
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
-- acf model for squid
-- Copyright(c) 2007 A. Brodmann - Licensed under terms of GPL2
module (..., package.seeall)

require "format"

squidconf = "/etc/squid/squid.conf"
squidtempl = "/etc/squid/squid.conf.template"

--- the tokenizer functions - must be dislocated into a library later
tokenizer = {}

tokenizer.new = function( str, delim )
	local token = {}
	token.value = str;
	token.delim = delim;
	token.pos   = 1
	return token
end
               
tokenizer.pos = function( value, substr, pos )
	local retval = pos
	local done = false
	while not done and retval <= #value do
		if string.sub( value, retval, retval ) == substr then
			done = true
		else
			retval = retval + 1
		end
	end
	return retval
end
                                                                  
tokenizer.next = function( token )
	if token.pos > #token.value then
		return token, nil
	end
	
	local strpos = tokenizer.pos( token.value, token.delim, token.pos )
	retval = string.sub(token.value, token.pos, strpos-1)
	if retval == token.delim then
		retval = ""
		token.pos = token.pos + 1
	else
		token.pos = strpos + 1
	end
 
	return token, retval
end
---

get_status = function()

	local retval = "stopped"

	local ptr = io.popen( "/bin/pidof squid" )
	local pid = ptr:read( "*a" )
	ptr:close()
	if pid ~= nil then
		if #pid > 1 then
			retval = "running"
		end
	end
	
	return retval
end

get_squid_version = function()

	local retval = ""
	
	local ptr = io.popen( "/usr/sbin/squid -v" )
	retval = ptr:read( "*l" )
	ptr:close()
	if retval == nil then
		retval = "Error - can't retrieve squid version"
	end
	
	return retval
end

get_status_winbindd = function()

	local retval = "stopped"

	local ptr = io.popen( "/bin/pidof winbindd" )
	local pid = ptr:read( "*a" )
	ptr:close()
	if pid ~= nil then
		if #pid > 1 then
			retval = "running"
		end
	end
	
	return retval
end

service_control = function( control )

	local retval = ""
	
	local ptr = io.popen( "/etc/init.d/squid " .. control, "r" )
	if ptr ~= nil then
		local retmsg = ptr:read( "*a" )
		ptr:close()
		if retmsg ~= nil then
			retval = retmsg
		else
			retval = "service_control(): Failed to read output from initscript!\n"
		end
	else
		retval = "service_control(): Failed to start/stop/restart service!\n"
	end

	return retval
end

service_control_winbindd = function( control )

	local retval = ""
	
	local ptr = io.popen( "/etc/init.d/winbindd " .. control, "r" )
	if ptr ~= nil then
		local retmsg = ptr:read( "*a" )
		ptr:close()
		if retmsg ~= nil then
			retval = retmsg
		else
			retval = "service_control(): Failed to read output from initscript!\n"
		end
	else
		retval = "service_control(): Failed to start/stop/restart service!\n"
	end

	return retval
end

get_winbindd_config = function()

	local error = ""
	local config = { domain = { value="", type="text", label="domain" },
	                 dcname = { value="", type="text", label="DC NetBIOS name" },
	                 dcipaddr = { value="", type="text", label="DC IP Address" },
	                 interfaces = { value="", type="text", label="interfaces" },
	                 loglevel = { value="", type="text", label="0" },
	                 account = { value="", type="text", label="account" },
	                 password = { value="", type="text", label="password" }
	               }
	               
	local ptr = io.open( "/etc/samba/smb.conf" )
	if ptr ~= nil then
	
	else
		
	end

end

get_adv_config = function()

	local retval = ""
	
	local ptr = io.open( squidconf, "r" )
	if ptr ~= nil then
		local retcfg = ptr:read( "*a" )
		ptr:close()
		if retcfg == nil then
			retval = "\n\n    Error: Failed to read " .. squidconf .. "!\n\n"
		else
			retval = retcfg
		end
	end

	return retval
end

get_digest_userlist = function()

	local retval = ""
	
	local ptr = io.open( "/etc/squid/users.list" )
	if ptr ~= nil then
		local retcfg = ptr:read( "*a" )
		ptr:close()
		if retcfg == nil then
			retval = "\n\n   Error: Failed to read user/password list!\n\n"
		else
			retval = retcfg
		end
	end
	
	return retval
end

get_saccess = function()

	local config = {}
	
	config.s_ip = get_file_contents( "/etc/squid/anoniplist" )
	config.s_browser = get_file_contents( "/etc/squid/anonbrowserlist" )
	config.s_domain = get_file_contents( "/etc/squid/anondomainlist" )
	
	return config
end

update_saccess = function( config )

	write_file_contents( "/etc/squid/anoniplist", config.s_ip )
	write_file_contents( "/etc/squid/anonbrowserlist", config.s_browser )
	write_file_contents( "/etc/squid/anondomainlist", config.s_domain )
	
	return
end

write_file_contents = function( name, contents )

	local ptr = io.open( name, "wb+" )
	if ptr ~= nil then
		ptr:write( format.dostounix( contents ) )
		ptr:close()
	end

	return
end

get_file_contents = function( name )

	local retval = ""
	
	local ptr = io.open( name )
	if ptr ~= nil then
		retval = ptr:read( "*a" )
		ptr:close()
		if retval == nil then
			retval = ""
		end
	end
	
	return retval
end

update_adv_config = function( config )

	local retval = "Successfully updated " .. squidconf .. "!"

	local ptr = io.open( squidconf, "wb+" )
	if ptr ~= nil then
		ptr:write( format.dostounix( config ) )
		ptr:close()
	else
		retval = "update_config(): Error, failed to open " .. squidconf .. "!\n"
	end

	return retval
end

update_digest_userlist = function( config )

	local retval = "Successfully updated user list!"
	
	local ptr = io.open( "/etc/squid/users.list", "wb+" )
	if ptr ~= nil then
		ptr:write( format.dostounix( config ) )
		ptr:close()
	else
		retval = "Update Digest User List: Error, failed to open /etc/squid/users.list!\n"
	end
end

get_conf_tag = function( tag, gat )

	local retval = ""
	local error = ""
	local found = false
	local done = false
	
	local fptr = io.open( squidconf, "r" )
	if fptr == nil then
		return "", "Failed to open squid config file!"
	end
	
	while not found and not done do
		local line = fptr:read( "*l" )
		if line == nil then
			done = true
		else 
			if string.sub( line, 1, 22 ) == tag then
				found = true
			end
		end
	end
	
	if done then
		fptr:close()
		return "", "TAG not found in squid config!"
	end
	
	found = false
	done = false
	
	while not found and not done do
		local line = fptr:read( "*l" )
		if line == nil then
			done = true
		elseif string.sub( line, 1, 22 ) == gat then
			found = true
		else
			retval = retval .. "\n" .. line
		end
	end
	
	if done then
		fptr:close()
		return "", "End TAG (GAT) not found in squid config! '" .. gat .. "'"
	end
	
	fptr:close()
	
	return retval, error
end

get_basic_config = function()

	local error = ""
	local config = { proxyip = { value="empty", type="text", label="Proxy IP" },
	                 proxyport = { value="", type="text", label="Proxy Port" },
	                 filterip = { value="", type="text", label="Filter IP" },
	                 filterport = { value="", type="text", label="Filter Port" },
	                 filterregex = { value="", type="text", label="FilterRegex" },
	                 safeports = { value="", type="text", label="Safe_ports" },
	                 sslports = { value="", type="text", label="SSL_ports" },
	                 accesslog = { value="", type="select", label="Access Logs", option={ "yes", "no" } },
	                 diskcache = { value="", type="select", label="Disk Cache Parameters", option={ "yes", "no" } },
	                 authmethod = { value="", type="text", label="Authentication Method" }
	               }
	               
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0001", "### ACF-SQUID-GAT-0001" )
	if #error > 0 then
		return config, error
	end

	local cfg1tok = ""
	local cfg2tok = ""
	local cfg3tok = ""

	--- get proxyip, proxyport
	cfg1tok = tokenizer.new( cfg1, "\n" )
	local done = false
	while not done do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			return nil, "Corrupt squid.conf! Missing 'http_port' statement!"
		else
			if string.sub( str1, 1, 10 ) == "http_port " then
				local str2 = ""
				local ipport = ""
				local ip = ""
				local port = ""
				cfg2tok = tokenizer.new( str1, " " )
				cfg2tok, ipport = tokenizer.next( cfg2tok )
				cfg2tok, ipport = tokenizer.next( cfg2tok )
				if ipport == nil then
					return config, "Corrupt squid.conf! Missing parameter #1 for 'http_port' statement!"
				end
				cfg3tok = tokenizer.new( ipport, ":" )
				cfg3tok, ip = tokenizer.next( cfg3tok )
				cfg3tok, port = tokenizer.next( cfg3tok )
				if port == nil then
					port = ip
					ip = ""
				end
				
				config.proxyip.value = ip
				config.proxyport.value = port
				done = true
			end
		end
	end
	cfg1tok = nil
	cfg2tok = nil
	cfg3tok = nil
	
	--- get filterip, filterport
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0007", "### ACF-SQUID-GAT-0007" )
	if #error > 0 then
		return config, error
	end
	
	cfg1tok = tokenizer.new( cfg1, "\n" )
	done = false
	while not done do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			return nil, "Corrupt squid.conf! Missing 'cache_peer' statement!"
		else
			if string.sub( str1, 1, 11 ) == "cache_peer " then
				local str2 = ""
				local tmp = ""
				local ip = ""
				local port = ""
				cfg2tok = tokenizer.new( str1, " " )
				cfg2tok, tmp = tokenizer.next( cfg2tok )
				cfg2tok, ip = tokenizer.next( cfg2tok )
				cfg2tok, tmp = tokenizer.next( cfg2tok )
				cfg2tok, port = tokenizer.next( cfg2tok )
				if ip == nil or port == nil then
					return config, "Corrupt squid.conf! Missing parameters #2 and/or #4 for 'cache_peer' statement!"
				end
				config.filterip.value = ip
				config.filterport.value = port
				done = true
			end
		end
	end
	cfg1tok = nil
	cfg2tok = nil
	cfg3tok = nil
	
	--- get diskcache
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0002", "### ACF-SQUID-GAT-0002" )
	if #error > 0 then
		return config, error
	end
	
	cfg1tok = tokenizer.new( cfg1, "\n" )
	done = false
	while not done do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			return nil, "Corrupt squid.conf! Missing 'cache_dir' statement!"
		else
			if string.sub( str1, 1, 15 ) == "cache_dir diskd" then
				config.diskcache.value = "yes"
				done = true
			elseif string.sub( str1, 1, 14 ) == "cache_dir null" then
				config.diskcache.value = "no"
				done = true
			end
		
		end
	end
	cfg1tok = nil
	
	--- get cache_access log
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0003", "### ACF-SQUID-GAT-0003" )
	if #error > 0 then
		return config, error
	end
	
	cfg1tok = tokenizer.new( cfg1, "\n" )
	done = false
	local str1 = ""
	while str1 ~= nil do
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 ~= nil then
			if string.sub( str1, 1, 16 ) == "cache_access_log" then
				config.accesslog.value = "yes"
			elseif string.sub( str1, 1, 17 ) == "#cache_access_log" or
					string.sub( str1, 1, 18 ) == "# cache_access_log" then
				config.accesslog.value = "no"
			end
		end
	end
	if config.accesslog.value == "" then
		return nil, "Corrupt squid.conf! Missing 'cache_access_log' statement!"
	end
	cfg1tok = nil
	
	--- authentication method
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0004", "### ACF-SQUID-GAT-0004" )
	if #error > 0 then
		return config, error
	end
	
	cfg1tok = tokenizer.new( cfg1, "\n" )
	local done = false
	local auth = ""
	while not done do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			done = true
		else
			if string.sub( str1, 1, 18 ) == "auth_param digest " then
				if string.match( auth, "D" ) == nil then
					auth = auth .. "D"
				end
			elseif string.sub( str1, 1, 16 ) == "auth_param ntlm " then
				if string.match( auth, "N" ) == nil then
					auth = auth .. "N"
				end
			elseif string.sub( str1, 1, 17 ) == "auth_param basic " then
				if string.match( auth, "B" ) == nil then
					auth = auth .. "B"
				end
			end
		end
	end
	config.authmethod.value = auth
	cfg1tok = nil
	
	--- get filterregex
	local cfg1, error = get_conf_tag( "### ACF-SQUID-TAG-0005", "### ACF-SQUID-GAT-0005" )
	if #error > 0 then
		return config, error
	end
	
	cfg1tok = tokenizer.new( cfg1, "\n" )
	done = false
	while not done do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			return nil, "Corrupt squid.conf! Missing 'acl ContentFilter urlpath_regex -i' statement!"
		else
			if string.sub( str1, 1, 35 ) == "acl ContentFilter urlpath_regex -i " then
				config.filterregex.value = string.sub( str1, 36 )
				done = true
			end
		end
	end
	cfg1tok = nil
	cfg2tok = nil
	cfg3tok = nil
	
	--- get SSL_ports, Safe_ports
	local done1 = false
	local done2 = false
	cfg1tok = tokenizer.new( cfg1, "\n" )
	while not done1 or not done2 do
		local str1 = ""
		cfg1tok, str1 = tokenizer.next( cfg1tok )
		if str1 == nil then
			return nil, "Corrupt squid.conf! Missing 'acl SSL_ports/Safe_ports' statement!"
		else
			if string.sub( str1, 1, 19 ) == "acl SSL_ports port " then
				config.sslports.value = string.sub( str1, 20 )
				done1 = true
			elseif string.sub( str1, 1, 20 ) == "acl Safe_ports port " then
				config.safeports.value = string.sub( str1, 21 )
				done2 = true	
			end
		end
	end
	cfg1tok = nil
	
	return config, error
end

config_preblock_copy = function( fromfile, tofile, tag )

	local done = false
	local line = ""
	while not done do
		line = fromfile:read( "*l" )
		tofile:write( line .. "\n" )
		if string.sub( line, 1, #tag ) == tag then
			done = true
		end
	end
end

config_postblock_copy = function( fromfile, tofile )

	local done = false
	local line = ""
	while not done do
		line = fromfile:read( "*l" )
		if line ~= nil then
			tofile:write( line .. "\n" )
		else
			done = true
		end
	end
end

update_basic_config = function( config )

	local error = ""
	
	--- put proxyip, proxyport
	local tmpfilename = os.tmpname()
	local tmpfile = io.open( tmpfilename, "w+" )
	local cfgfile = io.open( squidconf, "r" )
	local done = false
	
	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0001" )
	
	local don2 = false
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 22 ) == "### ACF-SQUID-GAT-0001" then
			done = true
		end
		if don2 then
			tmpfile:write( line .. "\n" )
		else
			if string.sub( line, 1, 10 ) == "http_port " then
				don2 = true
				tmpfile:write( "http_port " .. config.proxyip.value .. ":" .. config.proxyport.value .. "\n" )
			else
				tmpfile:write( line .. "\n" )
			end
		end
	end

	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )
	
	
	--- put filterip, filterport
	tmpfilename = os.tmpname()
	tmpfile = io.open( tmpfilename, "w+" )
	cfgfile = io.open( squidconf, "r" )
	done = false

	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0007" )

	local don2 = false
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 22 ) == "### ACF-SQUID-GAT-0007" then
			done = true
		end
		if don2 then
			tmpfile:write( line .. "\n" )
		else
			if string.sub( line, 1, 11 ) == "cache_peer " then
				don2 = true
				local lap = 1
				local cfg2tok = tokenizer.new( line, " " )
				local tmpval = ""
				local newstr = ""
				local tmparr = {}
				cfg2tok, tmpval = tokenizer.next( cfg2tok )
				while tmpval ~= nil do
					tmparr[lap] = tmpval
					if lap == 2 then
						newstr = newstr .. config.filterip.value .. " "
					elseif lap == 4 then
						newstr = newstr .. config.filterport.value .. " "
					else 
						newstr = newstr .. tmpval .. " "
					end
					lap = lap + 1
					cfg2tok, tmpval = tokenizer.next( cfg2tok )
				end
			
				tmpfile:write( newstr .. "\n" )
			else
				tmpfile:write( line .. "\n" )
			end
		end
	end
	
	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )
	
	
	--- disk cache parameters
	tmpfilename = os.tmpname()
	tmpfile = io.open( tmpfilename, "w+" )
	cfgfile = io.open( squidconf, "r" )
	done = false
	
	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0002" )
	
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 7 ) == "### ACF" then
			done = true
			tmpfile:write( line .. "\n" )
		else 
			if config.diskcache.value == "yes" then
				if string.sub( line, 1, 16 ) == "#cache_dir diskd" then
					tmpfile:write( string.sub( line, 2 ) .. "\n" )
				elseif string.sub( line, 1, 14 ) == "cache_dir null" then
					tmpfile:write( "#" .. line .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			else
				if string.sub( line, 1, 15 ) == "cache_dir diskd" then
					tmpfile:write( "#" .. line .. "\n" )
				elseif string.sub( line, 1, 15 ) == "#cache_dir null" then
					tmpfile:write( string.sub( line, 2 ) .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			end
		end
	end
	
	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )
	
	
	--- cache access log parameters
	tmpfilename = os.tmpname()
	tmpfile = io.open( tmpfilename, "w+" )
	cfgfile = io.open( squidconf, "r" )
	done = false
	
	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0003" )
	
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 7 ) == "### ACF" then
			done = true
			tmpfile:write( line .. "\n" )
		else 
			if config.accesslog.value == "yes" then
				if string.sub( line, 1, 17 ) == "#cache_access_log" then
					tmpfile:write( string.sub( line, 2 ) .. "\n" )
				elseif string.sub( line, 1, 18 ) == "# cache_access_log" then
					tmpfile:write( string.sub( line, 3 ) .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			else
				if string.sub( line, 1, 16 ) == "cache_access_log" then
					tmpfile:write( "#" .. line .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			end
			
		end
	end
	
	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )
	
	
	--- cache access log parameters
	tmpfilename = os.tmpname()
	tmpfile = io.open( tmpfilename, "w+" )
	cfgfile = io.open( squidconf, "r" )
	done = false
	
	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0005" )
	
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 7 ) == "### ACF" then
			done = true
			tmpfile:write( line .. "\n" )
		else 
			if string.sub( line, 1, 18 ) == "acl SSL_ports port" then
				tmpfile:write( "acl SSL_ports port " .. config.sslports.value .. "\n" )
			elseif string.sub( line, 1, 19 ) == "acl Safe_ports port" then
				tmpfile:write( "acl Safe_ports port " .. config.safeports.value .. "\n" )
			elseif string.sub( line, 1, 34 ) == "acl ContentFilter urlpath_regex -i" then
				tmpfile:write( "acl ContentFilter urlpath_regex -i " .. config.filterregex.value .. "\n" )
			else
				tmpfile:write( line .. "\n" )
			end
		end
	end
	
	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )
	
		
	return error
end

upd_authmethod = function( method )

	local tmpfilename = os.tmpname()
	local tmpfile = io.open( tmpfilename, "w+" )
	local cfgfile = io.open( squidconf, "r" )
	local error = ""
	local line = ""
	local done = false
	
	config_preblock_copy( cfgfile, tmpfile, "### ACF-SQUID-TAG-0004" )
	
	while not done do
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 7 ) == "### ACF" then
			done = true
			tmpfile:write( line .. "\n" )
		else
			if string.sub( line, 1, 17 ) == "auth_param digest" then
				if string.find( method, "D" ) ~= nil then
					tmpfile:write( line .. "\n" )
				else
					tmpfile:write( "#" .. line .. "\n" )
				end
			elseif string.sub( line, 1, 18 ) == "#auth_param digest" then
				if string.find( method, "D" ) ~= nil then
					tmpfile:write( string.sub( line, 2 ) .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			elseif string.sub( line, 1, 15 ) == "auth_param ntlm" then
				if string.find( method, "N" ) ~= nil then
					tmpfile:write( line .. "\n" )
				else
					tmpfile:write( "#" .. line .. "\n" )
				end
			elseif string.sub( line, 1, 16 ) == "#auth_param ntlm" then
				if string.find( method, "N" ) ~= nil then
					tmpfile:write( string.sub( line, 2 ) .. "\n" )
				else
					tmpfile:write( line .. "\n" )
				end
			else
				tmpfile:write( line .. "\n" )
			end
		end
	end
	
	config_postblock_copy( cfgfile, tmpfile )
	
	tmpfile:close()
	cfgfile:close()
	os.rename( tmpfilename, squidconf )

	return error
end

dependancy_ok = function()

	local retval = false
	local cfgfile = io.open( squidconf )
	local line = ""
	
	if cfgfile ~= nil then
		line = cfgfile:read( "*l" )
		if string.sub( line, 1, 19 ) == "### ACF-SQUID-MAGIC" then
			retval = true
		end
	end
	
	return retval
end

create_cfg_from_template = function()

	local from = io.open( squidtempl )
	local to = io.open( squidconf, "wb+" )
	local line = ""
	
	while line ~= nil do
		line = from:read( "*l" )
		if line ~= nil then
			to:write( line .. "\n" )
		end
	end
	
	from:close()
	to:close()
	
	return
end