Debian Codegraph @

ipapython/kernel_keyring.py

raw
1 # Authors: Rob Crittenden <[email protected]>
2 #
3 # Copyright (C) 2012 Red Hat
4 # see file 'COPYING' for use and warranty information
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 from __future__ import absolute_import
21
22 import os
23
24 from ipapython.ipautil import run
25 from ipaplatform.paths import paths
26 from ipaplatform.tasks import tasks
27
28 # NOTE: Absolute path not required for keyctl since we reset the environment
29 # in ipautil.run.
30
31 # Use the session keyring so the same user can have a different principal
32 # in different shells. This was explicitly chosen over @us because then
33 # it is not possible to use KRB5CCNAME to have a different user principal.
34 # The same session would always be used and the first principal would
35 # always win.
36 KEYRING = '@s'
37 KEYTYPE = 'user'
38
39
40 def dump_keys():
41 """
42 Dump all keys
43 """
44 result = run([paths.KEYCTL, 'list', KEYRING], raiseonerr=False,
45 capture_output=True)
46 return result.output
47
48
49 def get_real_key(key):
50 """
51 One cannot request a key based on the description it was created with
52 so find the one we're looking for.
53 """
54 assert isinstance(key, str)
55 result = run([paths.KEYCTL, 'search', KEYRING, KEYTYPE, key],
56 raiseonerr=False, capture_output=True)
57 if result.returncode:
58 raise ValueError('key %s not found' % key)
59 return result.raw_output.rstrip()
60
61
62 def get_persistent_key(key):
63 """
64 Fetches the value of a persistent key from storage, trimming trailing
65 any tailing whitespace.
66
67 Assert when key is not a string-type.
68 """
69 assert isinstance(key, str)
70 result = run([paths.KEYCTL, 'get_persistent', KEYRING, key],
71 raiseonerr=False, capture_output=True)
72 if result.returncode:
73 raise ValueError('persistent key %s not found' % key)
74 return result.raw_output.rstrip()
75
76
77 def is_persistent_keyring_supported(check_container=True):
78 """Returns True if the kernel persistent keyring is supported.
79
80 If check_container is True and a containerized environment is detected,
81 return False. There is no support for keyring namespace isolation yet.
82 """
83 if check_container and tasks.detect_container() is not None:
84 return False
85 uid = os.geteuid()
86 try:
87 get_persistent_key(str(uid))
88 except ValueError:
89 return False
90
91 return True
92
93
94 def has_key(key):
95 """
96 Returns True/False whether the key exists in the keyring.
97 """
98 assert isinstance(key, str)
99 try:
100 get_real_key(key)
101 return True
102 except ValueError:
103 return False
104
105
106 def read_key(key):
107 """
108 Read the keyring and return the value for key.
109
110 Use pipe instead of print here to ensure we always get the raw data.
111 """
112 assert isinstance(key, str)
113 real_key = get_real_key(key)
114 result = run([paths.KEYCTL, 'pipe', real_key], raiseonerr=False,
115 capture_output=True)
116 if result.returncode:
117 raise ValueError('keyctl pipe failed: %s' % result.error_log)
118
119 return result.raw_output
120
121
122 def update_key(key, value):
123 """
124 Update the keyring data. If they key doesn't exist it is created.
125 """
126 assert isinstance(key, str)
127 assert isinstance(value, bytes)
128 if has_key(key):
129 real_key = get_real_key(key)
130 result = run([paths.KEYCTL, 'pupdate', real_key], stdin=value,
131 raiseonerr=False)
132 if result.returncode:
133 raise ValueError('keyctl pupdate failed: %s' % result.error_log)
134 else:
135 add_key(key, value)
136
137
138 def add_key(key, value):
139 """
140 Add a key to the kernel keyring.
141 """
142 assert isinstance(key, str)
143 assert isinstance(value, bytes)
144 if has_key(key):
145 raise ValueError('key %s already exists' % key)
146 result = run([paths.KEYCTL, 'padd', KEYTYPE, key, KEYRING],
147 stdin=value, raiseonerr=False)
148 if result.returncode:
149 raise ValueError('keyctl padd failed: %s' % result.error_log)
150
151
152 def del_key(key):
153 """
154 Remove a key from the keyring
155 """
156 assert isinstance(key, str)
157 real_key = get_real_key(key)
158 result = run([paths.KEYCTL, 'unlink', real_key, KEYRING],
159 raiseonerr=False)
160 if result.returncode:
161 raise ValueError('keyctl unlink failed: %s' % result.error_log)