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# Copyright (C) Citrix Systems Inc. 

2# 

3# This program is free software; you can redistribute it and/or modify 

4# it under the terms of the GNU Lesser General Public License as published 

5# by the Free Software Foundation; version 2.1 only. 

6# 

7# This program is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU Lesser General Public License for more details. 

11# 

12# You should have received a copy of the GNU Lesser General Public License 

13# along with this program; if not, write to the Free Software Foundation, Inc., 

14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

15# 

16# cifutils: Extract credentials from SR (e.g ISOSR, SMBSR) dconf 

17 

18import util 

19import xs_errors 

20 

21 

22class CIFSException(Exception): 

23 def __init__(self, errstr): 

24 self.errstr = errstr 

25 

26 

27def getDconfPasswordKey(prefix=""): 

28 key_password = prefix + 'password' 

29 key_secret = prefix + 'password_secret' 

30 return key_password, key_secret 

31 

32 

33def containsPassword(dconf, prefix=""): 

34 key_password, key_secret = getDconfPasswordKey(prefix) 

35 if(key_password in dconf): 

36 util.SMlog("dconf contains password") 

37 if(key_secret in dconf): 

38 util.SMlog("dconf contains secret") 

39 

40 return ((key_password in dconf) or (key_secret in dconf)) 

41 

42 

43def containsCredentials(dconf, prefix=""): 

44 if('username' in dconf): 

45 util.SMlog("dconf contains username") 

46 return ((('username' in dconf)) and (containsPassword(dconf, prefix))) 

47 

48 

49def splitDomainAndUsername(uname): 

50 

51 username = None 

52 domain = None 

53 dom_username = uname.split('\\') 

54 

55 if len(dom_username) == 1: 

56 domain = None 

57 username = dom_username[0] 

58 elif len(dom_username) == 2: 

59 domain = dom_username[0] 

60 username = dom_username[1] 

61 else: 

62 raise CIFSException("A maximum of 2 tokens are expected " 

63 "(<domain>\<username>). {} were given." 

64 .format(len(dom_username))) 

65 return username, domain 

66 

67 

68def getCIFCredentials(dconf, session, prefix=""): 

69 credentials = None 

70 domain = None 

71 if (containsCredentials(dconf, prefix)): 

72 

73 username, domain = splitDomainAndUsername(dconf['username']) 

74 credentials = {} 

75 credentials["USER"] = util.to_plain_string(username) 

76 util.SMlog("CIFS user = {user}".format(user=credentials["USER"])) 

77 

78 key_password, key_secret = getDconfPasswordKey(prefix) 

79 if key_secret in dconf: 

80 password = util.get_secret(session, dconf[key_secret]) 

81 if password is not None: 81 ↛ 88line 81 didn't jump to line 88, because the condition on line 81 was never false

82 util.SMlog("Obtained CIFS password via secret") 

83 else: 

84 password = dconf[key_password] 

85 if password is not None: 85 ↛ 88line 85 didn't jump to line 88, because the condition on line 85 was never false

86 util.SMlog("Obtained CIFS password") 

87 

88 credentials["PASSWD"] = util.to_plain_string(password) 

89 if credentials["PASSWD"] is not None: 89 ↛ 92line 89 didn't jump to line 92, because the condition on line 89 was never false

90 util.SMlog("Obtained CIFS plain text password") 

91 

92 domain = util.to_plain_string(domain) 

93 else: 

94 util.SMlog("NOTE: No CIFS credentials found in dconf") 

95 

96 return credentials, domain