1 #!/usr/bin/python 2 # 3 # CDDL HEADER START 4 # 5 # The contents of this file are subject to the terms of the 6 # Common Development and Distribution License (the "License"). 7 # You may not use this file except in compliance with the License. 8 # 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 # or http://www.opensolaris.org/os/licensing. 11 # See the License for the specific language governing permissions 12 # and limitations under the License. 13 # 14 # When distributing Covered Code, include this CDDL HEADER in each 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 # If applicable, add the following below this CDDL HEADER, with the 17 # fields enclosed by brackets "[]" replaced with your own identifying 18 # information: Portions Copyright [yyyy] [name of copyright owner] 19 # 20 # CDDL HEADER END 21 # 22 23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 # Use is subject to license terms. 25 26 import privileges 27 import rbac 28 import os 29 import sys 30 import tempfile 31 32 # privileges tests 33 34 def test_setppriv(): 35 amchild = os.fork() 36 if amchild == 0: 37 if privileges.setppriv(privileges.PRIV_OFF, privileges.PRIV_EFFECTIVE, 38 ['proc_fork']): 39 try: 40 os.fork() 41 sys.exit(1) 42 except OSError, e: 43 sys.exit(0) 44 45 child = os.wait() 46 if child[1] is not 0: 47 print "setppriv. Bad exit status from pid %i\n" % child[0] 48 return False 49 return True 50 51 def test_getppriv(): 52 if 'proc_fork' in privileges.getppriv(privileges.PRIV_LIMIT): 53 return True 54 print "getppriv or PRIV_PROC_FORK not in PRIV_LIMIT.\n" 55 return False 56 57 def test_priv_ineffect(): 58 if privileges.priv_ineffect('proc_fork'): 59 return True 60 print "priv_ineffect or PRIV_PROC_FORK not in effect\n" 61 return False 62 63 # authattr tests 64 65 def test_chkauthattr(): 66 try: 67 a = rbac.authattr() 68 except Exception, e: 69 print "Could not instantiate authattr object: %s\n" % e 70 return False 71 try: 72 res = a.chkauthattr('solaris.*', 'root') 73 except Exception, e: 74 print "chkauthattr failed: %s\n" % e 75 return False 76 if not res: 77 print "chkauthattr failed or \'root\' lacks \'solaris.*\'\n" 78 return False 79 return True 80 81 def test_getauthattr(): 82 try: 83 a = rbac.authattr() 84 except Exception, e: 85 print "Could not instantiate authattr object: %s\n" % e 86 return False 87 try: 88 res = a.getauthattr() 89 except Exception, e: 90 print "getauthattr failed: %s\n" % e 91 return False 92 if not 'name' in res.keys(): 93 print "getauthattr failed\n" 94 return False 95 return True 96 97 def test_getauthnam(): 98 try: 99 a = rbac.authattr() 100 except Exception, e: 101 print "Could not instantiate authattr object: %s\n" % e 102 return False 103 try: 104 res = a.getauthnam('solaris.') 105 except Exception, e: 106 print "getauthnam failed: %s\n" % e 107 return False 108 if not res: 109 print "getauthnam failed or \'solaris.\' not in auth_attr(4)\n" 110 return False 111 return True 112 113 def test_authattr_iter(): 114 try: 115 a = rbac.authattr() 116 except Exception, e: 117 print "Could not instantiate authattr object: %s\n" % e 118 return False 119 res = a.next() 120 if not 'name' in res.keys() or type(a) != type(a.__iter__()): 121 print "authattr object is not an iterable\n" 122 return False 123 return True 124 125 # execattr tests 126 127 def test_getexecattr(): 128 try: 129 a = rbac.execattr() 130 except Exception, e: 131 print "Could not instantiate execattr object: %s\n" % e 132 return False 133 try: 134 res = a.getexecattr() 135 except Exception, e: 136 print "getexecattr failed: %s\n" % e 137 return False 138 if not 'name' in res.keys(): 139 print "getexecattr failed\n" 140 return False 141 return True 142 143 def test_getexecuser(): 144 try: 145 a = rbac.execattr() 146 except Exception, e: 147 print "Could not instantiate execattr object: %s\n" % e 148 return False 149 try: 150 res = a.getexecuser("root", "act", "*;*;*;*;*") 151 except Exception, e: 152 print "getexecuser failed: %s\n" % e 153 return False 154 if not res: 155 print "getexecuser failed or \'root\' not assigned to \'act\', " \ 156 "\'*;*;*;*;*\' \n" 157 return False 158 return True 159 160 161 def test_getexecprof(): 162 try: 163 a = rbac.execattr() 164 except Exception, e: 165 print "Could not instantiate execattr object: %s\n" % e 166 return False 167 try: 168 res = a.getexecprof("All", "cmd", "*") 169 except Exception, e: 170 print "getexecprof failed: %s\n" % e 171 return False 172 if not res: 173 print "getexecprof failed or \'All\' not granted \'cmd\' : \'*\'\n" 174 return False 175 return True 176 177 def test_execattr_iter(): 178 try: 179 a = rbac.execattr() 180 except Exception, e: 181 print "Could not instantiate execattr object: %s\n" % e 182 return False 183 res = a.next() 184 if not 'name' in res.keys() or type(a) != type(a.__iter__()): 185 print "execattr object is not an iterable\n" 186 return False 187 return True 188 189 # userattr tests 190 191 def test_getuserattr(): 192 try: 193 a = rbac.userattr() 194 except Exception, e: 195 print "Could not instantiate userattr object: %s\n" % e 196 return False 197 try: 198 res = a.getuserattr() 199 except Exception, e: 200 print "getuserattr failed: %s\n" % e 201 return False 202 if not 'name' in res.keys(): 203 print "getuserattr failed\n" 204 return False 205 return True 206 207 def test_fgetuserattr(): 208 temp = tempfile.NamedTemporaryFile() 209 temp.write("user::::profiles=Software Installation;roles=foo;"\ 210 "auths=solaris.foo.bar") 211 temp.seek(0) 212 try: 213 a = rbac.userattr() 214 except Exception, e: 215 print "Could not instantiate userattr object: %s\n" % e 216 return False 217 try: 218 res = a.fgetuserattr(temp.name) 219 temp.close() 220 except Exception, e: 221 print "fgetuserattr failed: %s\n" % e 222 temp.close() 223 return False 224 if not 'name' in res.keys(): 225 print "fgetuserattr failed\n" 226 return False 227 return True 228 229 def test_getuseruid(): 230 try: 231 a = rbac.userattr() 232 except Exception, e: 233 print "Could not instantiate userattr object: %s\n" % e 234 return False 235 try: 236 res = a.getuseruid(0) 237 except Exception, e: 238 print "getusernam failed: %s\n" % e 239 return False 240 if not 'name' in res: 241 print "getusernam failed or no uid 0\n" 242 return False 243 return True 244 245 def test_getusernam(): 246 try: 247 a = rbac.userattr() 248 except Exception, e: 249 print "Could not instantiate userattr object: %s\n" % e 250 return False 251 try: 252 res = a.getusernam('root') 253 except Exception, e: 254 print "getusernam failed: %s\n" % e 255 return False 256 if not 'name' in res: 257 print "getusernam failed or no \'root\' user\n" 258 return False 259 return True 260 261 def test_userattr_iter(): 262 try: 263 a = rbac.userattr() 264 except Exception, e: 265 print "Could not instantiate userattr object: %s\n" % e 266 return False 267 res = a.next() 268 if not 'name' in res.keys() or type(a) != type(a.__iter__()): 269 print "userattr object is not an iterable\n" 270 return False 271 return True 272 273 if not test_setppriv() or not test_getppriv() or not test_priv_ineffect(): 274 print "*** Failures detected in privileges module\n" 275 sys.exit(1) 276 277 if not test_getauthattr() or not test_chkauthattr() or not test_getauthnam() \ 278 or not test_authattr_iter: 279 print "*** Failures detected in rbac.authattr\n" 280 sys.exit(1) 281 282 if not test_getexecattr() or not test_getexecuser() or not test_getexecprof() \ 283 or not test_execattr_iter(): 284 print "*** Failures detected in rbac.execattr\n" 285 sys.exit(1) 286 287 if not test_getuserattr() or not test_fgetuserattr() or not test_getusernam()\ 288 or not test_getuseruid() or not test_userattr_iter(): 289 print "*** Failures detected in rbac.userattr\n" 290 sys.exit(1) 291