1174
1181
log.debug("Removing file %s", path)
1175
1182
os.remove(path)
1184
def add_license_key(self, trans, pkg_name, json_token, server_name):
1185
"""Add a license key data to the given package.
1188
trans -- the coresponding transaction
1189
pkg_name -- the name of the corresponding package
1190
json_token -- the oauth token as json
1191
server_name -- the server to use (ubuntu-production, ubuntu-staging)
1193
# set transaction state to downloading
1194
trans.status = STATUS_DOWNLOADING
1196
license_key, license_key_path = \
1197
self.plugins["get_license_key"][0](trans.uid, pkg_name,
1198
json_token, server_name)
1199
except Exception, error:
1200
raise TransactionFailed(ERROR_LICENSE_KEY_DOWNLOAD_FAILED,
1202
# ensure stuff is good
1203
if not license_key_path or not license_key:
1204
raise TransactionFailed(ERROR_LICENSE_KEY_DOWNLOAD_FAILED,
1205
_("The license key is empty"))
1207
# add license key if we have one
1208
self._add_license_key_to_system(pkg_name, license_key, license_key_path)
1210
def _add_license_key_to_system(self, pkg_name, license_key, license_key_path):
1212
license_key_path = os.path.join(apt_pkg.config.find_dir("Dir"),
1213
license_key_path.lstrip("/"))
1215
# Check content of the key
1216
if (license_key.strip().startswith("#!") or
1217
license_key.startswith("\x7fELF")):
1218
raise TransactionFailed(ERROR_LICENSE_KEY_INSTALL_FAILED,
1219
_("The license key is not allowed to "
1220
"contain executable code."))
1221
# Check the path of the license
1222
license_key_path = os.path.normpath(license_key_path)
1223
license_key_path_rootdir = os.path.join(apt_pkg.config["Dir"],
1224
self.LICENSE_KEY_ROOTDIR.lstrip("/"),
1226
if not license_key_path.startswith(license_key_path_rootdir):
1227
raise TransactionFailed(ERROR_LICENSE_KEY_INSTALL_FAILED,
1228
_("The license key path %s is invalid"),
1230
if os.path.lexists(license_key_path):
1231
raise TransactionFaild(ERROR_LICENSE_KEY_INSTALL_FAILED,
1232
_("The license key already exists: %s"),
1235
if os.path.realpath(license_key_path) != license_key_path:
1236
raise TransactionFailed(ERROR_LICENSE_KEY_INSTALL_FAILED,
1237
_("The location of the license key is "
1238
"unsecure since it contains symbolic "
1239
"links. The path %s maps to %s"),
1241
os.path.realpath(licese_key_path))
1242
# Check if the directory already exists
1243
if not os.path.isdir(os.path.dirname(license_key_path)):
1244
raise TransactionFailed(ERROR_LICENSE_KEY_INSTALL_FAILED,
1245
_("The directory where to install the key "
1246
"to doesn't exist yet: %s"),
1249
log.info("Writing license key to '%s'" % license_key_path)
1250
old_umask = os.umask(18)
1252
with open(license_key_path, "w") as license_file:
1253
license_file.write(license_key)
1255
raise TransactionFailed(ERROR_LICENSE_KEY_INSTALL_FAILED,
1256
_("Failed to write key file to: %s"),
1177
1261
def _get_broken_details(self, trans, now=True):
1178
1262
"""Return a message which provides debugging information about
1179
1263
broken packages.