cfgfiles.py revision 289
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# NOTE: This module is inherently posix specific. Care is taken in the modules
# that use this module to not use it on other operating systems.
import os
import fcntl
import re
import tempfile
import sys
import time
import datetime
""" Solaris configuration file editor... make it easier to
modify Solaris line-oriented configuration files from actions """
l.sort()
else:
return "CfgFile(%s):%s:%s:%s" % \
def getcolumnnames(self):
return self.column_names
raise RuntimeError, "line %s in %s has %d columns" % \
return cols
def getfilelines(self):
""" given self, return list of lines to be printed.
default impl preserves orignal + insertion order"""
return [l[1] for l in lines]
lineno = 1
linecnt = 1;
linecnt += 1
else:
# for k, v in dic.iteritems():
# self.max_lengths[k] = max(len(v), self.max_lengths[k])
else:
if val:
return val[1]
else:
return {}
def getdefaultvalues(self):
""" returns dictionary of default string values - ignores
other types """
for i in self.default_values
""" update existing record, using orig values if missing
in template"""
""" set value of record in file, replacing any previous def.
for any missing info, use defaults. Will insert new value """
# bring in any missing values as defaults if not None
raise RuntimeError, \
"Required attribute %s is missing" % field
else:
if orig:
else:
""" print out values in file format """
[
]))
if not self.needswriting:
return
for l in self.getfilelines():
print >>file, l
class PasswordFile(CfgFile):
"""Manage the passwd and shadow together. Note that
self.password_file = \
":",
{"username" : (1, None),
"password" : (2, "x"),
"gid" : (4, None),
"gcos-field" : (5, "& User"),
"home-dir" : (6, "/"),
"login-shell": (7, "")
},
self.shadow_file = \
":",
{"username" : (1, None),
"password" : (2, "*LK*"),
"min" : (4, ""),
"max" : (5, ""),
"warn" : (6, ""),
"inactive" : (7, ""),
"expire" : (8, ""),
"flag" : (9, "")
},
if lock:
""" merge dbs... do passwd file first to get right passwd value"""
return c
if "password" in copy:
# ignore attempts to set passwd for passwd file
if "password" in copy:
def getnextuid(self):
"""returns next free system (<=99) uid"""
uids=[]
for i in range(100):
if i not in uids:
return i
raise RuntimeError, "No free system uids"
def getcolumnnames(self):
return names
def getdefaultvalues(self):
return a
def unlockfile(self):
""" manage the group file"""
":",
{"groupname" : (1, None),
"password" : (2, ""),
"user-list" : (4, "")
},
def getnextgid(self):
"""returns next free system (<=99) gid"""
gids=[]
for i in range(100):
if i not in gids:
return i
raise RuntimeError, "No free system gids"
""""add named user to group; does not check if user exists"""
if not group:
""" remove named user from group """
if not group:
raise RuntimeError, "User %s not in group %s" % (
""" return list of additional groups user belongs to """
return sorted([
t[1]["groupname"]
])
for g in removals:
for g in additions:
class FtpusersFile(CfgFile):
""" If a username is present in this file, it denies that user
the ability to use ftp"""
" ",
{"username" : (1, None)
},
"username")
""" returns true if user is allowed to use FTP - ie is NOT in file"""
""" add specified user to file, removing ability to use ftp"""
""" remove specified user from file """
class UserattrFile(CfgFile):
""" manage the userattr file """
":",
{"username" : (1, None),
"qualifier" : (2, ""),
"reserved1" : (3, ""),
"reserved2" : (4, ""),
"attributes" : (5, "")
},
"username")
""" return tokenized line, with attribute column a dictionary
w/ lists for values"""
raise RuntimeError, "line %s in %s has %d columns rather than %s" % \
d = {}
for attr in attributes:
cols[4] = d
return cols
""" print out string; replace attribute dictionary with proper
string and use base class to convert entire record to a string """
attrdict = c["attributes"]
[
])
c["attributes"] = str
if __name__ == "__main__":
if 0:
print a
attrdict = a["attributes"]
print a
attrdict = a["attributes"]
p.discard("Wombat")
pw.unlockfile()