cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
system.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # @cond LICENSE
4 #
5 # Copyright [2010-2013] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @endcond LICENSE
20 ##
21 # @package src.common.tools.system
22 # @author Maciej Nabozny <mn@mnabozny.pl>
23 #
24 
25 import os
26 import pwd, grp
27 
28 def check_my_uid(uid):
29  if os.getuid() != uid:
30  raise Exception("Uid is not %d" % uid)
31 
32 def check_my_gid(gid):
33  if os.getgid() != gid:
34  raise Exception("Gid is not %d" % gid)
35 
36 def check_my_login(login):
37  try:
38  pwlogin = pwd.getpwnam(login)
39  except:
40  raise Exception("Login %s does not exist" % login)
41 
42  check_my_uid(pwlogin.pw_uid)
43 
44 def check_my_group(group):
45  try:
46  grpgroup = grp.getgrnam(group)
47  except:
48  raise Exception("Group %s does not exists" % group)
49 
50  check_my_gid(grpgroup.g_gid)
51 
52 def set_cc1():
53  uid_cc1 = pwd.getpwnam('cc1').pw_uid
54  gid_cc1 = grp.getgrnam('cc1').gr_gid
55  gid_kvm = grp.getgrnam('kvm').gr_gid
56  gid_libvirt = grp.getgrnam('libvirt').gr_gid
57 
58  if check_my_uid(0):
59  os.environ['HOME'] = '/var/lib/cc1/'
60 
61  os.setgroups([gid_cc1, gid_kvm, gid_libvirt])
62  os.setregid(gid_cc1, gid_cc1)
63  os.setreuid(uid_cc1, uid_cc1)
64  elif os.getuid() != uid_cc1:
65  raise Exception("Not you are not root")
66