cfgfiles.py revision 2602
#
# 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
#
#
#
# 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 datetime
import errno
import os
import re
import stat
import sys
import tempfile
import time
""" 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
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:
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:
lineno = 0
""" 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"),
"uid" : (3, None),
"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, "")
},
"etc/.pwd.lock"))
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=[]
if t[1]:
for i in range(100):
return i
raise RuntimeError, "No free system uids"
def getcolumnnames(self):
return names
def getdefaultvalues(self):
return a
""" manage the group file"""
":",
{"groupname" : (1, None),
"password" : (2, ""),
"gid" : (3, None),
"user-list" : (4, "")
},
def getnextgid(self):
"""returns next free system (<=99) gid"""
gids=[]
if t[1]:
for i in range(100):
return i
raise RuntimeError, "No free system gids"
""""add named user to group; does not check if user exists"""
# If the group isn't in the database, we'll add the user to the group,
# but unless the group is being added in the same transaction, the group
# won't have a gid, and essentially be inert on the system.
if not group:
group = {
"groupname": groupname,
"user-list": ""
}
""" 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 """
"""Add or remove 'username' from the file to turn off or on the user's
ability to ftp into the system. If 'value' is True, allow the user, and
if False, deny the user. If the value matches what is already encoded,
don't do anything.
"""
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"""
return cols
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