Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

import fileinput 

from subprocess import CalledProcessError, check_call, check_output 

 

from charmhelpers.core import hookenv 

from charmhelpers.core.host import pwgen, service 

from configobj import ConfigObj 

 

 

class SambaHelper: 

def __init__(self): 

self.charm_config = hookenv.config() 

self.config_file = "/etc/samba/smb.conf" 

self.smb_config = ConfigObj(self.config_file) 

self.users = [] 

 

def restart_samba(self): 

service("restart", "smbd") 

service("restart", "nmbd") 

 

def reload_samba(self): 

service("reload", "smbd") 

service("reload", "nmbd") 

 

def reload_config(self): 

self.smb_config = ConfigObj(self.config_file) 

 

def save_config(self): 

hookenv.log("Writing config file {}".format(self.config_file), "DEBUG") 

self.smb_config.write() 

self.reload_samba() 

 

def clean_example_config(self): 

for line in fileinput.input(self.config_file, inplace=True): 

if line.startswith("#") or line.startswith(";"): 

continue 

if "\n" == line: 

continue 

print(line, end="") 

 

def ensure_users(self, users): 

self.update_users() 

# add users here, call add user helper as needed 

userlist = users.split(",") 

if userlist and len(userlist) > 0: 

for user in userlist: 

# check if user is in local password database, create 

# if not already there 

if user not in self.users: 

self.add_user(user) 

# if so, lets add them to smbpasswd 

# check smbpasswd list for users that are not in provided config 

for smbuser in self.users: 

if smbuser not in userlist: 

# we found an orphan, set phasers to delete 

self.remove_user(smbuser) 

 

def update_users(self): 

try: 

sambatool = check_output(["samba-tool", "user", "list"]) 

except CalledProcessError: 

hookenv.log("Error getting users", "ERROR") 

return False 

else: 

self.users = sambatool.split("\n") 

 

def add_user(self, user): 

try: 

sambatool = check_output(["samba-tool", "user", "getpassword", user]) 

except CalledProcessError: 

hookenv.log( 

"Error getting password for user {} in smbpasswd".format(user), "ERROR" 

) 

return False 

else: 

return sambatool 

 

def get_password(self, user): 

try: 

sambatool = check_output(["samba-tool", "user", "getpassword", user]) 

except CalledProcessError: 

hookenv.log( 

"Error getting password for user {} in smbpasswd".format(user), "ERROR" 

) 

return False 

else: 

return sambatool 

 

def set_password(self, user, password): 

# if password is empty or false, auto-generate 

if not password: 

password = pwgen() 

# run samba-tool 

try: 

check_call( 

[ 

"samba-tool", 

"user", 

"setpassword", 

user, 

"--newpassword={}".format(password), 

] 

) 

except CalledProcessError: 

hookenv.log( 

"Error setting password for user {} in smbpasswd".format(user), "ERROR" 

) 

return False 

else: 

return True 

 

def update_config(self): 

hookenv.log("Updating configuration", "DEBUG") 

if self.charm_config["smb-users"]: 

hookenv.log( 

"Processing users: {}".format(self.charm_config["smb-users"]), "DEBUG" 

) 

self.ensure_users(self.charm_config["smb-users"]) 

if self.charm_config["smb-shares"]: 

hookenv.log( 

"Processing shares: {}".format(self.charm_config["smb-shares"]), "DEBUG" 

) 

for entry in self.charm_config["smb-shares"].split(","): 

if ":" in entry: 

share, path = entry.split(":") 

self.smb_config[share] = {} 

self.smb_config[share]["path"] = path 

 

if self.charm_config["smb-browsable"]: 

self.smb_config[share]["browsable"] = "yes" 

else: 

self.smb_config[share]["browsable"] = "no" 

 

if self.charm_config["smb-guest"]: 

self.smb_config[share]["guest ok"] = "yes" 

else: 

self.smb_config[share]["guest ok"] = "no" 

 

if self.charm_config["smb-read-only"]: 

self.smb_config[share]["read only"] = "yes" 

else: 

self.smb_config[share]["read only"] = "no" 

 

if self.charm_config["smb-force-mask"]: 

self.smb_config[share]["force create mode"] = self.charm_config[ 

"smb-force-mask" 

] 

 

if self.charm_config["smb-force-dir-mask"]: 

self.smb_config[share][ 

"force directory mode" 

] = self.charm_config["smb-force-dir-mask"] 

 

if self.charm_config["smb-force-user"]: 

self.smb_config[share]["force user"] = self.charm_config[ 

"smb-force-user" 

] 

 

if self.charm_config["smb-force-group"]: 

self.smb_config[share]["force group"] = self.charm_config[ 

"smb-force-group" 

] 

 

if self.charm_config["smb-write-list"]: 

self.smb_config[share]["write list"] = self.charm_config[ 

"smb-write-list" 

] 

 

self.smb_config[share]["create mask"] = self.charm_config[ 

"smb-create-mask" 

] 

self.smb_config[share]["directory mask"] = self.charm_config[ 

"smb-dir-mask" 

] 

 

if self.charm_config["smb-custom"]: 

for num, share in enumerate(self.charm_config["smb-custom"].split(";")): 

share_name = "custom-" + str(num) 

self.smb_config[share_name] = {} 

for setting in share.split(","): 

key, value = setting.split("=") 

self.smb_config[share_name][key] = value 

locking_config = "no" 

if self.charm_config["smb-strict-locking"].lower() == "yes": 

locking_config = "yes" 

elif self.charm_config["smb-strict-locking"].lower() == "auto": 

locking_config = "Auto" 

self.smb_config["global"]["strict locking"] = locking_config 

for section in self.smb_config.keys(): 

sections = ["global"] 

if self.charm_config["smb-shares"]: 

for entry in self.charm_config["smb-shares"].split(","): 

share, path = entry.split(":") 

sections.append(share) 

if self.charm_config["smb-custom"]: 

sections.extend( 

[ 

"custom-{}".format(i) 

for i, dummy in enumerate( 

self.charm_config["smb-custom"].split(";") 

) 

] 

) 

if section not in sections: 

del self.smb_config[section]