1#!/usr/bin/env python3 2# Unix SMB/CIFS implementation. 3# Tests for smbcquotas 4# Copyright (C) Noel Power 2017 5 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 3 of the License, or 9# (at your option) any later version. 10 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19import sys 20import traceback 21import logging 22import os 23 24USER_QUOTAS = 1 25USER_DEFAULT_QUOTAS = 2 26GROUP_QUOTAS = 3 27GROUP_DEFAULT_QUOTAS = 4 28 29#Quota model 30 31class Quota: 32 def __init__(self): 33 self.flags = 0 34 self.quotatype = USER_DEFAULT_QUOTAS 35 self.uid = 0 36 self.usedblocks = 0 37 self.softlimit = 0 38 self.hardlimit = 0 39 self.hardlimit = 0 40 self.usedinodes = 0 41 self.slimitinodes = 0 42 self.hlimitinodes = 0 43 44def quota_to_str(item): 45 result = str(item.flags) + " " + str(item.usedblocks) + " " + str(item.softlimit) + " " + str(item.hardlimit) + " " + str(item.usedinodes) + " " + str(item.slimitinodes) + " " + str(item.hlimitinodes) 46 return result 47 48def quota_to_db_str(item): 49 result = item.uid + " " + str(item.usedblocks) + " " + str(item.softlimit) + " " + str(item.hardlimit) + " " + str(item.usedinodes) + " " + str(item.slimitinodes) + " " + str(item.hlimitinodes) 50 return result 51 52def load_quotas(input_file): 53 fileContents = open(input_file,"r") 54 lineno = 0 55 quotas = [] 56 for line in fileContents: 57 if line.strip().startswith("#"): 58 continue 59 content = line.strip().split() 60 quota = Quota() 61 if len(content) < 7: 62 logging.debug("ignoring line %d, doesn't have enough fields\n"%lineno) 63 else: 64 quota.flags = 2 65 quota.uid = content[0] 66 quota.usedblocks = content[1] 67 quota.softlimit = content[2] 68 quota.hardlimit = content[3] 69 quota.usedinodes = content[4] 70 quota.slimitinodes = content[5] 71 quota.hlimitinodes = content[6] 72 quotas.append(quota) 73 74 fileContents.close() 75 return quotas 76 77def set_quotas(quota_list, output_file): 78 filecontents = open(output_file,"w+") 79 if filecontents == None: 80 return False; 81 lines = "" 82 for quota in quota_list: 83 next_line = quota_to_db_str(quota) 84 if next_line: 85 lines = lines + next_line + "\n" 86 filecontents.write(lines) 87 filecontents.close() 88 return True 89 90def get_quotas(uid, quota_list): 91 logging.debug("in get_quotas\n") 92 for quota in quota_list: 93 if quota.uid == uid: 94 return quota 95 return None 96 97def main(): 98 logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG) 99 logging.debug("system args passed are %s\n"% str(sys.argv)) 100 quota_file_dir = os.path.dirname(sys.argv[0]); 101 quota_file_db = os.path.join(quota_file_dir,"quotas.db") 102 logging.debug("quota db is located %s\n", quota_file_db) 103 quota_list = load_quotas(quota_file_db) 104 logging.debug("quotas loaded have %s entries\n", len(quota_list)) 105 result = None 106 if len(sys.argv) == 4: 107 # Get Quota 108 directory = sys.argv[1] 109 if sys.argv[2] == "1": 110 query_type = USER_QUOTAS 111 elif sys.argv[2] == "2": 112 query_type = USER_DEFAULT_QUOTAS 113 elif sys.argv[2] == "3": 114 query_type = GROUP_QUOTAS 115 elif sys.argv[2] == "4": 116 query_type = GROUP_DEFAULT_QUOTAS 117 uid = sys.argv[3] 118 quota = get_quotas(uid, quota_list) 119 if quota is None: 120 logging.debug("no result for uid %s"%uid) 121 else: 122 result = quota_to_str(quota) 123 logging.debug("got result for uid %s\n"%uid); 124 if result is None: 125 result = "0 0 0 0 0 0 0" 126 logging.debug("for uid %s returning quotas %s\n"%(uid,result)) 127 print("%s"%result) 128 elif len(sys.argv) > 8: 129 # Set Quota 130 quota = Quota() 131 directory = sys.argv[1] 132 quota.query_type = sys.argv[2] 133 quota.uid = sys.argv[3] 134 quota.flags = sys.argv[4] 135 quota.softlimit = sys.argv[5] 136 quota.hardlimit = sys.argv[6] 137 quota.slimitinodes = sys.argv[7] 138 quota.hlimitinodes = sys.argv[8] 139 found = get_quotas(quota.uid, quota_list) 140 if found: 141 found.query_type = quota.query_type 142 found.uid = quota.uid 143 found.flags = quota.flags 144 found.softlimit = quota.softlimit 145 found.hardlimit = quota.hardlimit 146 found.slimitinodes = quota.slimitinodes 147 found.hlimitinodes = quota.hlimitinodes 148 else: 149 quota_list.append(quota) 150 if set_quotas(quota_list,quota_file_db): 151 print ("%s\n"%quota_to_str(quota_list[-1])) 152 return 153if __name__ == '__main__': 154 main() 155