#!/usr/bin/env python
# Copyright (c) 2007, Secure64 Software Corporation
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 
#
#
#	encrypt a passwd (very simplistic for now, using GPG or such-like
#       key someday)
#
#

#-- imports
import os
import os.path
import sys
import getpass
import sys

if os.path.exists('../bind2nsd/Config.py'):
   sys.path.append('../bind2nsd')
   from Config import *
else:
   from bind2nsd.Config import *

if os.path.exists('../pyDes-1.2'):
   sys.path.append('../pyDes-1.2')
import pyDes

#-- globals
conf = Config()

#-- useful functions
def usage():
   print 's64-mkpw %s, copyright(c) 2007, Secure64 Software Corporation' \
               % (conf.getValue('version'))
   print
   print 'usage: s64-mkpw'
   return

def mkprintable(val):
   cstr = ''
   for ii in range(0, len(val)):
      c = val[ii]
      cstr += '%d ' % (ord(c))
   return cstr


#-- main ---------------------------------------------------------------
def main():
   if len(sys.argv) > 1:
      usage()
      sys.exit(1)

   print 's64-mkpw %s, copyright(c) 2007, Secure64 Software Corporation' \
               % (conf.getValue('version'))
   syspw = getpass.getpass('sysconfig password: ')
   dnspw = getpass.getpass('dnsconfig password: ')

   fd = open(conf.getValue('password_file'), 'w+')

   obj = pyDes.triple_des('aBcDeFgHiJkLmNoP', pyDes.ECB)
   fd.write(mkprintable(obj.encrypt(syspw, '#')) + '\n')
   fd.write(mkprintable(obj.encrypt(dnspw, '#')) + '\n')
   fd.close()
   print '=> stored password in "%s"' % (conf.getValue('password_file'))

   return

if __name__ == '__main__':
   main()

