2014-09-19 21:32:50 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#import iniparse as ip
|
|
|
|
#import ConfigParser as cp
|
|
|
|
#smb_conf = ip.INIConfig(open('smb.conf'))
|
|
|
|
# Note - ConfigParser or Iniparser cannot parse smb.conf because of leading spaces in key values
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class SmbConf(object):
|
|
|
|
def __init__(self, in_filename):
|
|
|
|
self.conf = ''
|
|
|
|
with open(in_filename) as in_file:
|
|
|
|
self.conf = in_file.read()
|
|
|
|
|
|
|
|
def add_after_global_section(self, what):
|
|
|
|
self.conf = self.conf.replace('[global]', '[global]' + '\n' + what)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def replace_or_add_global_option(self, option, value):
|
|
|
|
re_ = re.compile('(?m)^\s*%(option)s\s*=' % vars())
|
|
|
|
expression = '%(option)s=%(value)s' % vars()
|
|
|
|
if re_.findall(self.conf):
|
|
|
|
self.conf = re.sub('(?m)^\s*%(option)s\s*=[^$]*' % vars(),
|
|
|
|
expression, self.conf)
|
|
|
|
else:
|
|
|
|
self.add_after_global_section(expression)
|
|
|
|
|
|
|
|
def samba_home_user_patch(self):
|
|
|
|
#https://wiki.archlinux.org/index.php/Samba#Creating_usershare_path
|
|
|
|
self.replace_or_add_global_option('usershare path', '/var/lib/samba/usershare')
|
|
|
|
self.replace_or_add_global_option('usershare max shares', '100')
|
|
|
|
self.replace_or_add_global_option('usershare allow guests', 'yes')
|
|
|
|
self.replace_or_add_global_option('usershare owner only', 'false')
|
2014-09-21 17:47:13 +04:00
|
|
|
#self.replace_or_add_global_option('force user', 'nobody')
|
|
|
|
self.replace_or_add_global_option('guest account', 'nobody')
|
|
|
|
|
2014-09-19 21:32:50 +04:00
|
|
|
#essential for anonymous access from windows PC
|
2014-09-21 17:47:13 +04:00
|
|
|
self.replace_or_add_global_option('map to guest', 'bad user')
|
2014-10-06 18:49:21 +04:00
|
|
|
self.replace_or_add_global_option('workgroup', 'WORKGROUP')
|
2014-09-19 21:32:50 +04:00
|
|
|
|
|
|
|
def output2file(self, out_filename):
|
|
|
|
with open(out_filename, 'w') as f:
|
|
|
|
f.write(self.conf)
|
|
|
|
|
|
|
|
|
|
|
|
in_filename = sys.argv[1]
|
|
|
|
out_filename = in_filename
|
|
|
|
|
|
|
|
if len(sys.argv)>2:
|
|
|
|
out_filename = sys.argv[2]
|
|
|
|
|
|
|
|
smb = SmbConf(in_filename)
|
|
|
|
smb.samba_home_user_patch()
|
|
|
|
smb.output2file(out_filename)
|