b10-cmdctl-usermgr.py.in 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!@PYTHON@
  2. # Copyright (C) 2010 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. '''
  17. This file implements user management program. The user name and
  18. its password is appended to csv file.
  19. '''
  20. import random
  21. from hashlib import sha1
  22. import csv
  23. import getpass
  24. import getopt
  25. import sys
  26. import isc.utils.process
  27. isc.utils.process.rename()
  28. VERSION_NUMBER = 'bind10'
  29. DEFAULT_FILE = 'cmdctl-accounts.csv'
  30. def gen_password_hash(password):
  31. salt = "".join(chr(random.randint(33, 127)) for x in range(64))
  32. saltedpwd = sha1((password + salt).encode()).hexdigest()
  33. return salt, saltedpwd
  34. def username_exist(name, filename):
  35. # The file may doesn't exist.
  36. exist = False
  37. csvfile = None
  38. try:
  39. csvfile = open(filename)
  40. reader = csv.reader(csvfile)
  41. for row in reader:
  42. if name == row[0]:
  43. exist = True
  44. break
  45. except Exception:
  46. pass
  47. if csvfile:
  48. csvfile.close()
  49. return exist
  50. def save_userinfo(username, pw, salt, filename):
  51. csvfile = open(filename, 'a')
  52. writer = csv.writer(csvfile)
  53. writer.writerow([username, pw, salt])
  54. csvfile.close()
  55. print("\n create new account successfully! \n")
  56. def usage():
  57. print('''Usage: usermgr [options]
  58. -h, --help \t Show this help message and exit
  59. -f, --file \t Specify the file to append user name and password
  60. -v, --version\t Get version number
  61. ''')
  62. def main():
  63. filename = DEFAULT_FILE
  64. try:
  65. opts, args = getopt.getopt(sys.argv[1:], 'f:hv',
  66. ['file=', 'help', 'version'])
  67. except getopt.GetoptError as err:
  68. print(err)
  69. usage()
  70. sys.exit(2)
  71. for op, param in opts:
  72. if op in ('-h', '--help'):
  73. usage()
  74. sys.exit()
  75. elif op in ('-v', '--version'):
  76. print(VERSION_NUMBER)
  77. sys.exit()
  78. elif op in ('-f', "--file"):
  79. filename = param
  80. else:
  81. assert False, 'unknown option'
  82. usage()
  83. try:
  84. while True :
  85. name = input("Desired Login Name:")
  86. if name == '':
  87. print("error, user name can't be empty")
  88. continue
  89. if username_exist(name, filename):
  90. print("user name already exists!")
  91. continue
  92. while True:
  93. pwd1 = getpass.getpass("Choose a password:")
  94. pwd2 = getpass.getpass("Re-enter password:")
  95. if pwd1 != pwd2:
  96. print("password is not same, please input again")
  97. else:
  98. break;
  99. salt, pw = gen_password_hash(pwd1)
  100. save_userinfo(name, pw, salt, filename)
  101. inputdata = input('continue to create new account by input \'y\' or \'Y\':')
  102. if inputdata not in ['y', 'Y']:
  103. break
  104. except KeyboardInterrupt:
  105. pass
  106. if __name__ == '__main__':
  107. main()