Пример #1
0
def main():
    configPresent = sshdb.configCheck()
    if configPresent != 0:
        sys.exit(
            "No configfile found, please use \
        sshdbconfig.py to create a default config"
        )
    # parse config und connect
    connectingString = sshdb.parseConfig()
    if connectingString[0] == 1:
        sys.exit("Please edit your configfile")
    if connectingString[0] == "postgresql":
        conn = sshdb.connectpsql(connectingString[1])
    if connectingString[0] == "mysql":
        import MySQLdb

        conn = MySQLdb.connect(read_default_file="~/sshkeydb.conf")

    cursor = conn.cursor()
    listall = "select realname, role, keypath, checksum, active from users where role = '%s'" % myRole
    cursor.execute(listall)
    myresults = cursor.fetchall()
    for i in myresults:
        print "Realname: %s" % i[0]
        print "role: %s" % i[1]
        print "keypath: %s" % i[2]
        print "checksum: %s" % i[3]
        print "Active: %s" % i[4]
        print "--------------------------------------------------------------------------"
Пример #2
0
def main():
    configPresent = sshdb.configCheck()
    if configPresent != 0:
        sys.exit("No configfile found, please use \
        sshdbconfig.py to create a default config")
    # parse config und connect
    connectingString = sshdb.parseConfig()
    if connectingString[0] == 1:
        sys.exit("Please edit your configfile")
    if connectingString[0] == 'postgresql':
        conn = sshdb.connectpsql(connectingString[1])
    if connectingString[0] == 'mysql':
        import MySQLdb
        conn = MySQLdb.connect(read_default_file="~/sshkeydb.conf")

    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('--role', '-r', dest='',
        help='Role of the user (default: admin)', default='')
        parser.add_argument('--realname', '-R', dest='realname',
        help='Name of the key owner', default='')
        parser.add_argument('--revoke', dest='revoke',
        help='Revoke (boolean (true/false)', default='false')
        parser.add_argument('--listkey', '-l', dest='listing',
        help='listkeys', default='')
        args = parser.parse_args()
        theRole = args.role
        theName = args.realname
        theRevoke = args.revoke
        theListing = args.listing
    except:
        sys.exit("Wrong commandline arguments")

    try:
        if TheListing == None:
            print "moo"
        else:
            print TheListing
    except:
        print "the f**k"
Пример #3
0
def main():
    configPresent = sshdb.configCheck()
    if configPresent != 0:
        sys.exit("No configfile found, please use \
        sshdbconfig.py to create a default config")
    # parse config und connect
    connectingString = sshdb.parseConfig()
    if connectingString[0] == 1:
        sys.exit("Please edit your configfile")
    if connectingString[0] == 'postgresql':
        conn = sshdb.connectpsql(connectingString[1])
    if connectingString[0] == 'mysql':
        import MySQLdb
        conn = MySQLdb.connect(read_default_file="~/sshkeydb.conf")

    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('--role', '-r', dest='role',
        help='Role of the user (default: admin)', default='admin')
        parser.add_argument('--realname', '-R', dest='realname',
        help='Name of the key owner', default='')
        args = parser.parse_args()
        theRole = args.role
        theName = args.realname
    except:
        sys.exit("Wrong commandline arguments")

    try:
        keyList = sshdb.genList()
        cursor = conn.cursor()
        if theName == '':
            getByRole = \
            "select role, realname, keyfile, keypath, checksum from \
            users where role = '%s';" % theRole
            cursor.execute(getByRole)
            myresults = cursor.fetchall()
        else:
            getByName = \
            "select role, realname, keyfile, keypath, checksum from \
            users where realname = '%s';" % theName
            cursor.execute(getByName)
            myresults = cursor.fetchall()
            """
            Dirty hack with the exception ValueError need a better solution
            """
        for key in myresults:
            getKey = key[2]
            """ checking if the key exists in the authorized_keys """
            try:
                keyList.index(key[2])
                print "Key exists in authorized_keys: %s" % key[3]
            except ValueError:
                keychecksum = hashlib.sha256(getKey).hexdigest()
                if key[4] == keychecksum:
                    print "Checksum ok for %s" % key[3]
                else:
                    print "Checksum is different"
                    sys.exit("Aborting")
                auth = open(os.path.expanduser('~/.ssh/authorized_keys'), 'a')
                auth.write(key[2])
                auth.close()
            except:
                print "please check if the file ~/.ssh/authorized_keys \
                is present"
    except:
        print "Database Error"
    conn.close()
    sys.exit()
Пример #4
0
def main():
    configPresent = sshdb.configCheck()
    if configPresent != 0:
        sys.exit(
        "No configfile found, please use sshdbconfig.py \
        to create a default config")

    # parse config und connect
    connectingString = sshdb.parseConfig()
    whichdb = connectingString[0]
    if whichdb == 1:
        sys.exit("Please edit your configfile")

    if whichdb == 'postgresql':
        conn = sshdb.connectpsql(connectingString[1])

    if whichdb == 'mysql':
        conn = MySQLdb.connect(read_default_file="~/sshkeydb.conf")

    # Searching for the ssh key using the default keys

    try:
        keyExists = os.path.exists(os.path.expanduser('~/.ssh/id_rsa.pub'))
        if keyExists == True:
            keypath = os.path.expanduser('~/.ssh/id_rsa.pub')
    except:
        keyExists = os.path.exists(os.path.expanduser('~/.ssh/id_dsa.pub'))
        if keyExists == True:
            keypath = os.path.expanduser('~/.ssh/id_rsa.pub')
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('--key', '-k', dest='keytossh',
        help='Path to the Key (default: ~/.ssh/id_rsa.pub)', default=keypath)
        parser.add_argument('--role', '-r', dest='role',
        help='Role of the user (default: admin)', default='admin')
        parser.add_argument('--realname', '-R', dest='realname',
        help='Name of the key owner', default='')
        args = parser.parse_args()
        keypath = args.keytossh
        theRole = args.role
        theName = args.realname
    except:
        sys.exit("Wrong commandline arguments")

    checkKey = sshdb.isPublicKey(keypath)

    if checkKey == 0:
        print "Key is OK"
    else:
        sys.exit("Key is no publickey. I am stopping here")

    #Check if the key exists, make a checksum and read it
    hashSHA256 = sshdb.makeSHA256Hash(keypath)
    if hashSHA256[0] == 1:
        sys.stdout.write("Key not found in %s" % keypath)
    users = ({"key": hashSHA256[1], "role": theRole, "keySum": hashSHA256[0], \
    "path": keypath, "realname": theName})
    theQuery = sshdb.myQuery(users)
    queryReturnCode = sshdb.insertQuery(conn, theQuery)
    if queryReturnCode == 0:
        sys.stdout.write("Commited key successful in the database\n")
    elif queryReturnCode == 42:
        sys.stdout.write("SSH-key exists in the database\n")
    conn.close()
    sys.exit("Goodbye")