1
2
3
4
5
6
7
8
9
10 import os
11 import sys
12 import dbus
13
14 from up2date_client import up2dateUtils
15 from up2date_client import up2dateErrors
16 from up2date_client import rhnserver
17 from up2date_client import pkgUtils
18 from up2date_client import up2dateLog
19 from up2date_client import rhnreg_constants
20 from up2date_client import hardware
21 from up2date_client.rhnPackageInfo import convertPackagesFromHashToList
22 from up2date_client.pkgplatform import getPlatform
23 from rhn.i18n import ustr, sstr
24 from rhn.tb import raise_with_tb
25
26 try:
27 import urlparse
28 import xmlrpclib
29 from types import ListType, TupleType, StringType, UnicodeType, DictType, DictionaryType
30 except ImportError:
31 import urllib.parse as urlparse
32 import xmlrpc.client as xmlrpclib
33 ListType = list
34 TupleType = tuple
35 StringType = bytes
36 UnicodeType = str
37 DictType = dict
38 DictionaryType = dict
39 long = int
40
41 try:
42 from virtualization import support
43 except ImportError:
44 support = None
45
46 import gettext
47 t = gettext.translation('rhn-client-tools', fallback=True)
48
49 if not hasattr(t, 'ugettext'):
50 t.ugettext = t.gettext
51 _ = t.ugettext
52
53
54
55 SYSID_DIR = "/etc/sysconfig/rhn"
56 REMIND_FILE = "%s/rhn_register_remind" % SYSID_DIR
57
58 HW_CODE_FILE = "%s/hw-activation-code" % SYSID_DIR
59 RHSM_FILE = "/etc/pki/consumer/cert.pem"
60
61 from up2date_client import config
62 cfg = config.initUp2dateConfig()
63 log = up2dateLog.initLog()
64
65
67
68 if os.access("/usr/sbin/rhnsd", os.R_OK|os.X_OK):
69
70 systemd_system_unitdir = "/usr/lib/systemd/system"
71 systemd_systemctl = "/usr/bin/systemctl"
72 if not os.access(systemd_systemctl, os.R_OK|os.X_OK):
73 if os.access("/bin/systemctl", os.R_OK|os.X_OK):
74 systemd_systemctl = "/bin/systemctl"
75 systemd_system_unitdir = "/lib/systemd/system"
76 if os.access("%s/rhnsd.service" % systemd_system_unitdir, os.R_OK):
77
78 if os.access(systemd_systemctl, os.R_OK|os.X_OK):
79 os.system("%s enable rhnsd > /dev/null" % systemd_systemctl);
80 os.system("%s start rhnsd > /dev/null" % systemd_systemctl);
81 else:
82 print(_("Warning: unable to enable rhnsd with systemd"))
83 else:
84
85 if os.access("/sbin/chkconfig", os.R_OK|os.X_OK):
86 os.system("/sbin/chkconfig rhnsd on > /dev/null");
87 else:
88 print(_("Warning: unable to enable rhnsd with chkconfig"))
89
90 service_path = "/sbin/service"
91 if not os.access(service_path, os.R_OK|os.X_OK):
92 if os.access("/usr/sbin/service", os.R_OK|os.X_OK):
93 service_path = "/usr/sbin/service"
94
95 rc = os.system("%s rhnsd status > /dev/null" % service_path)
96 if rc:
97 os.system("%s rhnsd start > /dev/null" % service_path)
98
100 configFile = cfg["oemInfoFile"] or "/etc/sysconfig/rhn/oeminfo"
101
102 if not os.access(configFile, os.R_OK):
103 return {}
104
105 fd = open(configFile, "r")
106 L = fd.readlines()
107
108 info = {}
109 for i in L:
110 i = i.strip()
111 if i == "":
112 continue
113 try:
114 (key, value) = i.split(':')
115 except ValueError:
116 raise_with_tb(up2dateErrors.OemInfoFileError(i))
117
118 info[key] = value.strip()
119
120 return info
121
123 """ Returns true if system is registred using subscription manager """
124 if os.access(RHSM_FILE, os.R_OK):
125 statinfo = os.stat(RHSM_FILE)
126 return statinfo.st_size > 0
127 else:
128 return False
129
131 return os.access(cfg['systemIdPath'], os.R_OK)
132
139
143
145 """ Write a file to disk that is not readable by other users. """
146 dir_name = os.path.dirname(secure_file)
147 if not os.access(dir_name, os.W_OK):
148 return False
149
150 if os.access(secure_file, os.F_OK):
151
152 try:
153 os.rename(secure_file, secure_file + '.save')
154 except:
155 return False
156
157 fd = os.open(secure_file, os.O_WRONLY | os.O_CREAT, int('0600', 8))
158 fd_file = os.fdopen(fd, 'w')
159 try:
160 fd_file.write(sstr(file_contents))
161 finally:
162 fd_file.close()
163
164 return True
165
177
181
183 """
184 This function returns the UUID and virtualization type of this system, if
185 it is a guest. Otherwise, it returns None. To figure this out, we'll
186 use a number of heuristics (list in order of precedence):
187
188 1. Check /proc/xen/xsd_port. If exists, we know the system is a
189 host; exit.
190 2. Check SMBIOS. If vendor='Xen' and UUID is non-zero, we know the
191 system is a fully-virt guest; exit.
192 3. Check /sys/hypervisor/uuid. If exists and is non-zero, we know
193 the system is a para-virt guest; exit.
194 4. If non of the above checks worked; we know we have a
195 non-xen-enabled system; exit.
196 """
197
198
199
200 try:
201 if os.path.exists("/proc/xen/xsd_port"):
202
203
204
205
206 (uuid, virt_type) = get_fully_virt_info()
207 return (uuid, virt_type)
208 except IOError:
209
210 pass
211
212
213 (uuid, virt_type) = get_fully_virt_info()
214 if uuid is not None:
215 return (uuid, virt_type)
216
217
218 (uuid, virt_type) = get_para_virt_info()
219 if uuid is not None:
220 return (uuid, virt_type)
221
222
223
224 return (None, None)
225
227 """
228 This function checks /sys/hypervisor/uuid to see if the system is a
229 para-virt guest. It returns a (uuid, virt_type) tuple.
230 """
231 try:
232 uuid_file = open('/sys/hypervisor/uuid', 'r')
233 uuid = uuid_file.read()
234 uuid_file.close()
235 uuid = uuid.lower().replace('-', '').rstrip("\r\n")
236 virt_type = "para"
237 return (uuid, virt_type)
238 except IOError:
239
240 pass
241
242 return (None, None)
243
245 """
246 This function looks in the SMBIOS area to determine if this is a
247 fully-virt guest. It returns a (uuid, virt_type) tuple.
248 """
249 vendor = hardware.dmi_vendor()
250 uuid = hardware.dmi_system_uuid()
251 if vendor.lower() == "xen":
252 uuid = uuid.lower().replace('-', '')
253 virt_type = "fully"
254 return (uuid, virt_type)
255 else:
256 return (None, None)
257
259 uuid = eval('0x%s' % uuid)
260 return long(uuid) == long(0)
261
266
267
272
276
277
279 - def __init__(self, systemId, channels, failedChannels, systemSlots,
280 failedSystemSlots, universalActivationKey, rawDict=None):
281
282 self._systemId = systemId
283 self._channels = channels
284 self._failedChannels = failedChannels
285 self._systemSlots = systemSlots
286 self._failedSystemSlots = failedSystemSlots
287 if len(universalActivationKey) > 0:
288 self._universalActivationKey = universalActivationKey
289 else:
290 self._universalActivationKey = None
291 self.rawDict = rawDict
292
294 return self._systemId
295
297 return self._channels
298
300 return self._failedChannels
301
303 return self._systemSlots
304
307
310
312 """Returns None if no universal activation key was used."""
313 return self._universalActivationKey
314
316 """Returns True if the system was subscribed to at least one channel
317 and was given any type of system slot so it will get updates. In other
318 words, returns True if the system will be getting at least basic
319 updates.
320
321 """
322
323
324 return len(self._channels) > 0 and len(self._systemSlots) > 0
325
331
339
340
341 -def registerSystem(username = None, password = None,
342 profileName = None,
343 token = None, other = None):
369
371 try:
372 bus = dbus.SystemBus()
373 validity_obj = bus.ProxyObjectClass(bus, 'com.redhat.SubscriptionManager',
374 '/EntitlementStatus', introspect=False)
375 validity_iface = dbus.Interface(validity_obj,
376 dbus_interface='com.redhat.SubscriptionManager.EntitlementStatus')
377 except dbus.DBusException:
378
379
380 return
381
382 try:
383 validity_iface.check_status()
384 except dbus.DBusException:
385
386
387
388 pass
389
390
412
413
414
415
416 -def registerSystem2(username = None, password = None,
417 profileName = None, packages = None,
418 activationKey = None, other = {}):
419 """Uses the new xmlrpcs to register a system. Returns a dict instead of just
420 system id.
421
422 The main differences between this and registerSystem and that this doesn't
423 do activation and does child channel subscriptions if possible. See the
424 documentation for the xmlrpc handlers in backend for more detail.
425
426 If nothing is going to be in other, it can be {} or None.
427
428 New in RHEL 5.
429
430 """
431 if other is None:
432 other = {}
433
434 if activationKey:
435 assert username is None
436 assert password is None
437 assert activationKey is not None
438 else:
439 assert username is not None
440 assert password is not None
441 assert activationKey is None
442 for key in other.keys():
443 assert key in ['registration_number',
444 'org_id',
445 'virt_uuid',
446 'virt_type',
447 'channel']
448
449 if cfg['supportsSMBIOS']:
450 other["smbios"] = _encode_characters(hardware.get_smbios())
451
452 s = rhnserver.RhnServer()
453
454 if activationKey:
455 info = s.registration.new_system_activation_key(profileName,
456 up2dateUtils.getOSRelease(),
457 up2dateUtils.getVersion(),
458 up2dateUtils.getArch(),
459 activationKey,
460 other)
461 else:
462 info = s.registration.new_system_user_pass(profileName,
463 up2dateUtils.getOSRelease(),
464 up2dateUtils.getVersion(),
465 up2dateUtils.getArch(),
466 username,
467 password,
468 other)
469 log.log_debug("Returned:\n%s" % info)
470 result = RegistrationResult(info['system_id'],
471 info['channels'], info['failed_channels'],
472 info['system_slots'], info['failed_system_slots'],
473 info['universal_activation_key'],
474 rawDict=info)
475 return result
476
478 return cfg["supportsEUS"]
479
481 def remove_ip6addr(x):
482 if x['class'] == 'NETINFO' and 'ip6addr' in x:
483 del x['ip6addr']
484 return x
485 s = rhnserver.RhnServer()
486 if not s.capabilities.hasCapability('ipv6', 1):
487 hardwareList = [remove_ip6addr(i) for i in hardwareList]
488 s.registration.add_hw_profile(systemId, _encode_characters(hardwareList))
489
496
500
504
506 """Raises up2dateErrors.InvalidProtocolError if the server url has a
507 protocol specified and it's not http or https.
508
509 """
510 protocol, host, path, parameters, query, fragmentIdentifier = urlparse.urlparse(server)
511 if protocol is None or protocol == '':
512 server = 'https://' + server
513
514
515 protocol, host, path, parameters, query, fragmentIdentifier = urlparse.urlparse(server)
516 if protocol not in ['https', 'http']:
517 raise up2dateErrors.InvalidProtocolError("You specified an invalid "
518 "protocol. Only https and "
519 "http are allowed.")
520 if path is None or path == '' or path == '/':
521 path = '/XMLRPC'
522 server = urlparse.urlunparse((protocol, host, path, parameters, query,
523 fragmentIdentifier))
524
525 return server
526
528 """Returns 'hosted' if the url points to a known hosted server. Otherwise
529 returns 'satellite'.
530 """
531 return 'satellite'
532
533
535 ACTIVATED_NOW = 0
536 ALREADY_USED = 1
537
538 - def __init__(self, status, registrationNumber, channels={}, systemSlots={}):
539 """channels and systemSlots are dicts where the key/value pairs are
540 label (string) / quantity (int).
541
542 """
543 self._status = status
544
545 self._regNum = registrationNumber
546 self._channels = channels
547 self._systemSlots = systemSlots
548
551
554
556 """Returns a dict- the key/value pairs are label/quantity."""
557 return self._channels
558
560 """Returns a dict- the key/value pairs are label/quantity."""
561 return self._systemSlots
562
564 """ All the data we gathered from dmi, bios, gudev are in utf-8,
565 we need to convert characters beyond ord(127) - e.g \xae to unicode.
566 """
567 result=[]
568 for item in args:
569 item_type = type(item)
570 if item_type == StringType:
571 item = ustr(item)
572 elif item_type == TupleType:
573 item = tuple(_encode_characters(i) for i in item)
574 elif item_type == ListType:
575 item = [_encode_characters(i) for i in item]
576 elif item_type == DictType or item_type == DictionaryType:
577 item = dict([(_encode_characters(name, val)) for name, val in item.items()])
578
579 result.append(item)
580 if len(result) == 1:
581 return result[0]
582 else:
583 return tuple(result)
584
612
645
646
648 if os.access("/usr/sbin/rhn_check", os.R_OK|os.X_OK):
649 from subprocess import Popen, PIPE
650 p = Popen(["/usr/sbin/rhn_check"], stdin=PIPE, stdout=PIPE, \
651 stderr=PIPE)
652 map(lambda x:log.log_me(x), p.stdout.readlines() + \
653 p.stderr.readlines())
654 else:
655 log.log_me("Warning: unable to run rhn_check")
656
657 if getPlatform() == 'deb':
659 """On Debian no extra action for plugin is needed"""
660 return 1, 0
661 else:
662 from up2date_client.pmPlugin import pluginEnable
663