Advertisement
NovaYoshi

useradd.py

Sep 8th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Written by Nedko Arnaudov
  4.  
  5. import sys
  6. import os, time, sha, codecs
  7. from random import randint
  8. from getpass import getpass
  9.  
  10. # You need to change this
  11. moin_instance_dir="/usr/local/share/moin"
  12.  
  13. datetime_fmt = '%Y-%m-%d %H:%M:%S'
  14.  
  15. def encodePassword(pwd, charset='utf-8'):
  16.     """ Encode a cleartext password
  17.  
  18.    Compatible to Apache htpasswd SHA encoding.
  19.  
  20.    When using different encoding than 'utf-8', the encoding might fail
  21.    and raise UnicodeError.
  22.  
  23.    @param pwd: the cleartext password, (unicode)
  24.    @param charset: charset used to encode password, used only for
  25.        compatibility with old passwords generated on moin-1.2.
  26.    @rtype: string
  27.    @return: the password in apache htpasswd compatible SHA-encoding,
  28.        or None
  29.    """
  30.     import base64
  31.  
  32.     # Might raise UnicodeError, but we can't do anything about it here,
  33.     # so let the caller handle it.
  34.     pwd = pwd.encode(charset)
  35.  
  36.     pwd = sha.new(pwd).digest()
  37.     pwd = '{SHA}' + base64.encodestring(pwd).rstrip()
  38.     return pwd
  39.  
  40. class User:
  41.     def __init__(self, username, password, email):
  42.         self.aliasname=""
  43.         self.css_url=""
  44.         self.date_fmt=""
  45.         self.datetime_fmt=""
  46.         self.disabled="0"
  47.         self.edit_on_doubleclick="0"
  48.         self.edit_rows="20"
  49.         self.editor_default="text"
  50.         self.editor_ui="freechoice"
  51.         self.language=""
  52.         self.last_saved = str(time.time())
  53.         self.mailto_author="0"
  54.         self.quicklinks=""
  55.         self.remember_last_visit="0"
  56.         self.remember_me="1"
  57.         self.show_fancy_diff="1"
  58.         self.show_nonexist_qm="0"
  59.         self.show_page_trail="1"
  60.         self.show_toolbar="1"
  61.         self.show_topbottom="0"
  62.         self.subscribed_pages=""
  63.         self.theme_name="modern"
  64.         self.tz_offset="0"
  65.         self.want_trivial="0"
  66.         self.wikiname_add_spaces="0"
  67.  
  68.         self.id = "%s.%d" % (str(time.time()), randint(0,65535))
  69.  
  70.         self.name=username
  71.         self.enc_password=encodePassword(password)
  72.         self.email=email
  73.  
  74.     def save(self):
  75.         filename = "%s/data/user/%s" % (moin_instance_dir, self.id)
  76.         data = codecs.open(filename, "w")
  77.         data.write("# Data saved '%s' for id '%s'\n" % (
  78.             time.strftime(datetime_fmt, time.localtime(time.time())),
  79.             self.id))
  80.  
  81.         attrs = vars(self).items()
  82.         attrs.sort()
  83.         for key, value in attrs:
  84.             line = u"%s=%s\n" % (key, unicode(value))
  85.             data.write(line)
  86.         data.close()
  87.  
  88.         os.chmod(filename, 0660)
  89.  
  90. print "MoinMoin user creator"
  91. print "====================="
  92. print
  93.  
  94. if len(sys.argv) != 3:
  95.     print "Usage: moin_useradd.py <username> <email>"
  96.     sys.exit(0)
  97.  
  98. username = sys.argv[1]
  99. email = sys.argv[2]
  100.  
  101. print "username: %s" % username
  102. print "email: %s" % email
  103.  
  104. password = getpass("password: ")
  105. password_again = getpass("password (again): ")
  106.  
  107. if password != password_again:
  108.     print "Passwords does not match"
  109.     sys.exit(0)
  110.  
  111. user = User(username, password, email)
  112. user.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement