Changeset 7657:f3dd54249626


Ignore:
Timestamp:
11/11/07 17:00:08 (6 years ago)
Author:
Yi Qiang <yqiang@…>
Branch:
default
Message:

Refactored setup code a little bit. Bail out if ssh-keygen is not installed when trying to generate keys for the user.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/dsage/scripts/dsage_setup.py

    r6720 r7657  
    5656    return config 
    5757 
     58def add_default_user(): 
     59    """ 
     60    Adds the default user. 
     61     
     62    """ 
     63     
     64    from twisted.conch.ssh import keys 
     65    import base64 
     66    from getpass import getuser 
     67     
     68    username = getuser() 
     69    pubkey_file = os.path.join(DSAGE_DIR, 'dsage_key.pub') 
     70    clientdb = ClientDatabase() 
     71    pubkey = base64.encodestring( 
     72                    keys.getPublicKeyString(filename=pubkey_file).strip()) 
     73    if clientdb.get_user(username) is None: 
     74        clientdb.add_user(username, pubkey) 
     75        print 'Added user %s.\n' % (username) 
     76    else: 
     77        user, key = clientdb.get_user_and_key(username) 
     78        if key != pubkey: 
     79            clientdb.del_user(username) 
     80            clientdb.add_user(username, pubkey) 
     81            print "User %s's pubkey changed, setting to new one." % (username) 
     82        else: 
     83            print 'User %s already exists.' % (username) 
     84 
    5885def setup_client(testing=False): 
    5986    check_dsage_dir() 
     
    6289        cmd = ["ssh-keygen", "-q", "-trsa", "-P ''", "-f%s" % key_file] 
    6390        return 
    64  
     91     
     92    if not cmd_exists('ssh-keygen'): 
     93        print DELIMITER 
     94        print "Could NOT find ssh-keygen." 
     95        print "Aborting." 
     96        return 
     97         
    6598    print DELIMITER 
    6699    print "Generating public/private key pair for authentication..." 
     
    68101    print "Just hit enter when prompted for a passphrase" 
    69102    print DELIMITER 
     103     
    70104    cmd = ["ssh-keygen", "-q", "-trsa", "-f%s" % key_file]     
    71105    ld = os.environ['LD_LIBRARY_PATH'] 
     
    75109    finally: 
    76110        os.environ['LD_LIBRARY_PATH'] = ld 
     111         
    77112    print "\n" 
    78113    print "Client configuration finished.\n" 
     
    131166    f.write(s) 
    132167    f.close() 
     168     
    133169    # Disable certificate generation -- not used right now anyways 
    134170    privkey_file = os.path.join(DSAGE_DIR, 'cacert.pem') 
    135171    pubkey_file = os.path.join(DSAGE_DIR, 'pubcert.pem') 
     172     
    136173    print DELIMITER 
    137174    print "Generating SSL certificate for server..." 
     175     
    138176    if os.uname()[0] != 'Darwin' and cmd_exists('openssl'): 
    139177        # We use openssl by default if it exists, since it is *vastly* 
     
    157195    subprocess.call(cmd, shell=True) 
    158196    print DELIMITER 
     197     
     198    # Set read only permissions on cert 
    159199    os.chmod(os.path.join(DSAGE_DIR, 'cacert.pem'), 0600) 
    160200     
    161     # conf_file = os.path.join(DSAGE_DIR, 'server.conf') 
    162     # config.write(open(conf_file, 'w')) 
    163              
    164201    # add default user 
    165     from twisted.conch.ssh import keys 
    166     import base64 
    167  
    168     # c = ConfigParser.ConfigParser() 
    169     # c.read(os.path.join(DSAGE_DIR, 'client.conf')) 
    170     from getpass import getuser 
    171     username = getuser() 
    172     pubkey_file = os.path.join(DSAGE_DIR, 'dsage_key.pub') 
    173     clientdb = ClientDatabase() 
    174     pubkey = base64.encodestring( 
    175                     keys.getPublicKeyString(filename=pubkey_file).strip()) 
    176     if clientdb.get_user(username) is None: 
    177         clientdb.add_user(username, pubkey) 
    178         print 'Added user %s.\n' % (username) 
    179     else: 
    180         user, key = clientdb.get_user_and_key(username) 
    181         if key != pubkey: 
    182             clientdb.del_user(username) 
    183             clientdb.add_user(username, pubkey) 
    184             print "User %s's pubkey changed, setting to new one." % (username) 
    185         else: 
    186             print 'User %s already exists.' % (username) 
     202    add_default_user() 
    187203             
    188204    print "Server configuration finished.\n\n" 
Note: See TracChangeset for help on using the changeset viewer.