1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2024 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20 21from .base_rule import BaseRule 22 23class UserGroupModuleRule(BaseRule): 24 RULE_NAME = "NO-User-Group_In-Init" 25 26 def __init__(self, mgr, args): 27 super().__init__(mgr, args) 28 29 def __check__(self): 30 return self.check_user_group() 31 32 def check_user_group(self): 33 passed = True 34 passwd_parser = self.get_mgr().get_parser_by_name('user_group') 35 group_id = passwd_parser[0] 36 passwd_id = passwd_parser[1] 37 38 repeat_name = [] 39 repeat_uid = [] 40 for uid in passwd_id._uid_list: 41 if passwd_id._uid_list.count(uid) > 1: 42 repeat_uid.append(uid) 43 44 for name in passwd_id._name_list: 45 if passwd_id._name_list.count(name) > 1: 46 repeat_name.append(name) 47 if len(repeat_name): 48 passed = False 49 self.error("repeat uid name list: %s" % repeat_name) 50 51 if len(repeat_uid): 52 passed = False 53 self.error("repeat uid value list: %s" % repeat_uid) 54 55 if (self._check_gid_in_passwd(passwd_id._passwd) == -1): 56 passed = False 57 self.error("%s has different passwd and group values" % value["uid"]) 58 59 def _check_gid_in_passwd(self, passwd): 60 is_passed = True 61 for key, value in passwd.items(): 62 if value["passwdId"] == value["groupId"]: 63 pass 64 else: 65 is_passed = False 66 self.error("%s has different passwd and group values" % value["name"]) 67 return is_passed 68