summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarl MacMillan <kmacmill@redhat.com>2007-10-08 11:17:15 -0400
committerKarl MacMillan <kmacmill@redhat.com>2007-10-08 11:17:15 -0400
commitcfaa18a1cf879ff15abc1dfc338e3ce1bb0cb227 (patch)
tree56cda0019dc65d29faedf1de1461628f376160f5
parentd5fedb5f978d7aaa31db9c177286872c6244c12a (diff)
downloadfreeipa-cfaa18a1cf879ff15abc1dfc338e3ce1bb0cb227.tar.gz
freeipa-cfaa18a1cf879ff15abc1dfc338e3ce1bb0cb227.tar.xz
freeipa-cfaa18a1cf879ff15abc1dfc338e3ce1bb0cb227.zip
On 10/4/07, Rob Crittenden <rcritten@redhat.com> wrote:
> William Jon McCann wrote: > > Hi, > > > > After playing with the install (repeatedly) I ended up with a lot of > > duplicate values in: > > /etc/sysconfig/dirsrv > > /etc/sysconfig/ipa-kpasswd > > > > Here is a patch that should fix this. It modifies the file "in-place" > > and removes lines that matching the key (or commented key) and then > > appends the new key=value. > > > > Jon > > Cool, I've wanted to fix this for a while (and recently aborted a switch > from open with "a" to "w"). > > What happens if the file doesn't exist yet? Do we need to wrap the > fileinput loop in either a try/except or just look to see if the file > exists first (my vote)? > > Something like: > > def update_key_val_in_file(filename, key, val): > if os.path.exists(filename): > pattern = "^[\s#]*%s\s*=" % re.escape(key) > p = re.compile(pattern) > for line in fileinput.input(filename, inplace=1): > if not p.search(line): > sys.stdout.write(line) > fileinput.close() > f = open(filename, "a") > f.write("%s=%s\n" % (key, val)) > f.close() Good point. In genera,l I prefer doing a try because it is a little less racy but in this case it doesn't make a difference. Updated patch attached. Thanks, Jon
-rw-r--r--ipa-server/ipaserver/krbinstance.py23
1 files changed, 17 insertions, 6 deletions
diff --git a/ipa-server/ipaserver/krbinstance.py b/ipa-server/ipaserver/krbinstance.py
index 4eaa6f41..be70b035 100644
--- a/ipa-server/ipaserver/krbinstance.py
+++ b/ipa-server/ipaserver/krbinstance.py
@@ -23,6 +23,9 @@ import string
import tempfile
import shutil
import logging
+import fileinput
+import re
+import sys
from random import Random
from time import gmtime
import os
@@ -48,6 +51,18 @@ def ldap_mod(fd, dn, pwd):
args = ["/usr/bin/ldapmodify", "-h", "127.0.0.1", "-xv", "-D", dn, "-w", pwd, "-f", fd.name]
run(args)
+def update_key_val_in_file(filename, key, val):
+ if os.path.exists(filename):
+ pattern = "^[\s#]*%s\s*=" % re.escape(key)
+ p = re.compile(pattern)
+ for line in fileinput.input(filename, inplace=1):
+ if not p.search(line):
+ sys.stdout.write(line)
+ fileinput.close()
+ f = open(filename, "a")
+ f.write("%s=%s\n" % (key, val))
+ f.close()
+
class KrbInstance:
def __init__(self):
self.ds_user = None
@@ -207,9 +222,7 @@ class KrbInstance:
print "Error timed out waiting for kadmin to finish operations\n"
os.exit()
- cfg_fd = open("/etc/sysconfig/dirsrv", "a")
- cfg_fd.write("export KRB5_KTNAME=/etc/dirsrv/ds.keytab\n")
- cfg_fd.close()
+ update_key_val_in_file("/etc/sysconfig/dirsrv", "export KRB5_KTNAME", "/etc/dirsrv/ds.keytab")
pent = pwd.getpwnam(self.ds_user)
os.chown("/etc/dirsrv/ds.keytab", pent.pw_uid, pent.pw_gid)
@@ -237,9 +250,7 @@ class KrbInstance:
print "Error timed out waiting for kadmin to finish operations\n"
os.exit()
- cfg_fd = open("/etc/sysconfig/ipa-kpasswd", "a")
- cfg_fd.write("export KRB5_KTNAME=/var/kerberos/krb5kdc/kpasswd.keytab\n")
- cfg_fd.close()
+ update_key_val_in_file("/etc/sysconfig/ipa-kpasswd", "export KRB5_KTNAME", "/var/kerberos/krb5kdc/kpasswd.keytab")
pent = pwd.getpwnam(self.ds_user)
os.chown("/var/kerberos/krb5kdc/kpasswd.keytab", pent.pw_uid, pent.pw_gid)