<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
<link rel="self" type="application/atom+xml" href="https://forum.yubico.com/feed.php?f=3&amp;t=646" />

<title>Yubico Forum</title>
<subtitle>...visit our web-store at</subtitle>
<link href="https://forum.yubico.com/index.php" />
<updated>2011-03-06T07:33:53+01:00</updated>

<author><name><![CDATA[Yubico Forum]]></name></author>
<id>https://forum.yubico.com/feed.php?f=3&amp;t=646</id>
<entry>
<author><name><![CDATA[Redhatter]]></name></author>
<updated>2011-03-06T07:33:53+01:00</updated>
<published>2011-03-06T07:33:53+01:00</published>
<id>https://forum.yubico.com/viewtopic.php?t=646&amp;p=2601#p2601</id>
<link href="https://forum.yubico.com/viewtopic.php?t=646&amp;p=2601#p2601"/>
<title type="html"><![CDATA[python-yubico-client not recognised]]></title>

<content type="html" xml:base="https://forum.yubico.com/viewtopic.php?t=646&amp;p=2601#p2601"><![CDATA[
Hi all...<br /><br />I've been experimenting with PAM authentication and the Yubikey, in particular, I've been tinkering with using the HMAC-SHA1 mode of the key.  Ultimately I'd like to integrate this into the advanced-yubico-pam module.<br /><br />As a first step, I've managed to get the YubiKey authenticating PAM using HMAC-SHA1, below is the code that I have written for this:<br /><div class="codetitle"><b>Code:</b></div><div class="codecontent">#!/usr/bin/python<br /><br />import binascii<br />import hashlib<br />import hmac<br />import logging<br />import os<br />import random<br /><br />import yubico.yubikey<br />import yubico.yubico_util<br />import yubico.yubico_exception<br /><br /># Setup logging<br />logging.basicConfig(  filename = '/tmp/nistcr-pam.log', \<br />                      filemode = 'a', \<br />                      level = logging.CRITICAL, \<br />                      format = '%(asctime)s %(levelname)-8s %(message)s', \<br />                      datefmt = '%d.%m.%Y %H:%M:%S')<br />log = logging.getLogger('nistcr-pam')<br /><br />def pam_sm_authenticate(pamh, flags, argv):<br />  try:<br />    user = pamh.get_user(None)<br />  except pamh.exception, e:<br />    return e.pam_result<br />  if user == None:<br />    log.info('No user')<br />    return pamh.PAM_AUTH_ERR<br /><br />  # Look for and initialize the YubiKey<br />  try:<br />    YK = yubico.yubikey.find_key(debug=False)<br />    log.debug(&quot;Version : %s &quot; % YK.version())<br />    log.debug(&quot;Serial  : %i&quot; % YK.serial())<br />  except yubico.yubico_exception.YubicoError as inst:<br />    log.error(&quot;Yubikey Error: %s&quot; % inst.reason)<br />    return pamh.PAM_AUTH_ERR<br /><br />  # Attempt to pick up the user's key<br />  fn = os.path.join(os.path.expanduser('~'+user),'.yubikey')<br />  log.debug('Yubikey Configuration in ' + fn)<br />  try:<br />    fs = os.stat(fn)<br />  except OSError:<br />    log.info('Configuration file not accessible')<br />    return pamh.PAM_AUTH_ERR<br />  <br />  # Check for sane permissions<br />  if (fs.st_mode &amp; 07177):<br />    log.error('File permissions not safe: {0:04o}'.format(fs.st_mode))<br />    return pamh.PAM_AUTH_ERR<br />  <br />  # Open the file<br />  try:<br />    fp = open(fn, 'r')<br />  except OSError:<br />    log.error('Failed to open configuration file')<br />    return pamh.PAM_AUTH_ERR<br /><br />  # Read the key<br />  key_hex = fp.readline()&#91;:40&#93;<br />  key = binascii.a2b_hex(key_hex)<br />  <br />  # Generate challenge<br />  challenge = binascii.a2b_hex(hex(random.getrandbits(64*8))&#91;2:-1&#93;)&#91;0:63&#93;<br />  challenge_pad = challenge.ljust(64,chr(0))<br />  log.debug('Challenge: ' + repr(challenge) + ' len:' + str(len(challenge)))<br /><br />  # Create HMAC and generate expected response<br />  h = hmac.HMAC(key, challenge, hashlib.sha1)<br />  expected = h.digest()<br />  log.debug('Expecting: ' + repr(expected) + ' len:' + str(len(expected)))<br /><br />  # Ask the YubiKey<br />  try:<br />    response = YK.challenge_response(challenge_pad, slot=2)<br />  except yubico.yubico_exception.YubicoError as inst:<br />    log.error(&quot;Yubikey Error: %s&quot; % inst.reason)<br />    return pamh.PAM_AUTH_ERR<br />  log.debug('Received: ' + repr(response) + ' len:' + str(len(response)))<br /><br />  if (response == expected):<br />    return pamh.PAM_SUCCESS<br />  else:<br />    return pamh.PAM_AUTH_ERR<br /><br />def pam_sm_setcred(pamh, flags, argv):<br />  return pamh.PAM_SUCCESS<br /><br />def pam_sm_acct_mgmt(pamh, flags, argv):<br />  return pamh.PAM_SUCCESS<br /><br />def pam_sm_open_session(pamh, flags, argv):<br />  return pamh.PAM_SUCCESS<br /><br />def pam_sm_close_session(pamh, flags, argv):<br />  return pamh.PAM_SUCCESS<br /><br />def pam_sm_chauthtok(pamh, flags, argv):<br />  return pamh.PAM_SUCCESS</div><br /><br />Very crude at this point.  My next step was to try and get the advanced-yubico-pam-module installed and configured in its present state.  So I clone the repository and install it in the usual manner.  Some details of my machine and the revisions being used:<br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent">sh-4.1$ python --version<br />Python 2.6.6<br />sh-4.1$ uname -a<br />Linux zhouman 2.6.35.7-lm2f-nb #2 Wed Oct 13 00:42:58 EST 2010 mips64 ICT Loongson-2 V0.3 FPU V0.1 lemote-yeeloong-2f-8.9inches GNU/Linux<br />sh-4.1$ git describe<br />fatal: No annotated tags can describe 'b38e555356315616880a92f90c11e99b6deab85c'.<br />However, there were unannotated tags: try --tags.<br />sh-4.1$ git remote show origin<br />* remote origin<br />  Fetch URL: git://github.com/Kami/python-yubico-client.git<br />  Push  URL: git://github.com/Kami/python-yubico-client.git<br />  HEAD branch: master<br />  Remote branch:<br />    master tracked<br />  Local branch configured for 'git pull':<br />    master merges with remote master<br />  Local ref configured for 'git push':<br />    master pushes to master (up to date)</div><br /><br />Distribution is Gentoo Linux/MIPS 10.0 based on the latest O32 userland stage3.<br /><br />Installation of a Python module appeared to go fine…<br /><div class="codetitle"><b>Code:</b></div><div class="codecontent">sh-4.1$ python setup.py build<br />running build<br />running build_py<br />creating build<br />creating build/lib<br />creating build/lib/yubico<br />copying yubico/yubico.py -&gt; build/lib/yubico<br />copying yubico/yubico_exceptions.py -&gt; build/lib/yubico<br />copying yubico/modhex.py -&gt; build/lib/yubico<br />copying yubico/httplib_ssl.py -&gt; build/lib/yubico<br />copying yubico/__init__.py -&gt; build/lib/yubico<br />copying yubico/otp.py -&gt; build/lib/yubico<br />sh-4.1$ sudo python setup.py install<br />running install<br />running build<br />running build_py<br />running install_lib<br />copying build/lib/yubico/yubico.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />copying build/lib/yubico/yubico_exceptions.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />copying build/lib/yubico/modhex.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />copying build/lib/yubico/httplib_ssl.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />copying build/lib/yubico/__init__.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />copying build/lib/yubico/otp.py -&gt; /usr/lib/python2.6/site-packages/yubico<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/yubico.py to yubico.pyc<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/yubico_exceptions.py to yubico_exceptions.pyc<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/modhex.py to modhex.pyc<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/httplib_ssl.py to httplib_ssl.pyc<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/__init__.py to __init__.pyc<br />byte-compiling /usr/lib/python2.6/site-packages/yubico/otp.py to otp.pyc<br />running install_egg_info<br />Removing /usr/lib/python2.6/site-packages/yubico-1.5.dev-py2.6.egg-info<br />Writing /usr/lib/python2.6/site-packages/yubico-1.5.dev-py2.6.egg-info</div><br /><br />Great, now let's test it.<br /><div class="codetitle"><b>Code:</b></div><div class="codecontent">sh-4.1$ cd demo/<br />sh-4.1$ python example.py <br />Traceback (most recent call last):<br />  File &quot;example.py&quot;, line 2, in &lt;module&gt;<br />    from yubico import yubico<br />ImportError: cannot import name yubico</div><br /><br />Errm… didn't I just install that?  Any ideas?<p>Statistics: Posted by <a href="https://forum.yubico.com/memberlist.php?mode=viewprofile&amp;u=1526">Redhatter</a> — Sun Mar 06, 2011 7:33 am</p><hr />
]]></content>
</entry>
</feed>