chiark / gitweb /
catacomb/pwsafe.py, pwsafe: Dispatching for multiple backends.
[catacomb-python] / key.c
diff --git a/key.c b/key.c
index f9acd3c8a07c038846c916f7be3160f6b50244f0..2af00192e71f890f23a1457371c87681a3e4ece1 100644 (file)
--- a/key.c
+++ b/key.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Key files and data
  *
  * (c) 2005 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -106,7 +104,7 @@ static PyMethodDef keyexc_pymethods[] = {
 
 static void keyexc_raise(int err)
 {
-  PyObject *arg = Py_BuildValue("(i)", err);
+  PyObject *arg = Py_BuildValue("(is)", err, key_strerror(err));
   if (arg) PyErr_SetObject(keyexc, arg);
   Py_XDECREF(arg);
 }
@@ -270,7 +268,7 @@ static int convflags(PyObject *x, void *p)
   rc = 1;
   goto end;
 tyerr:
-  TYERR("expected flag string or flag/mask pair");
+  TYERR("expected flag string or integer bitfield");
 end:
   return (rc);
 }
@@ -336,7 +334,7 @@ static void keydata_pydealloc(PyObject *me)
 static PyObject *kdmeth_matchp(PyObject *me, PyObject *arg)
 {
   key_filter f;
-  
+
   if (!PyArg_ParseTuple(arg, "O&:matchp", convfilter, &f))
     return (0);
   return (getbool(KEY_MATCH(KEYDATA_KD(me), &f)));
@@ -350,6 +348,21 @@ static PyObject *kdmeth_split(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
+static PyObject *kdmeth_copy(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  key_filter f = { 0, 0 };
+  static char *kwlist[] = { "filter", 0 };
+  key_data *kd;
+
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:copy", kwlist,
+                                  convfilter, &f))
+    return (0);
+  if ((kd = key_copydata(KEYDATA_KD(me), &f)) == 0)
+    RETURN_NONE;
+  else
+    return (keydata_pywrap(kd));
+}
+
 static PyObject *kdmeth_write(PyObject *me, PyObject *arg, PyObject *kw)
 {
   key_filter f = { 0, 0 };
@@ -388,7 +401,7 @@ static PyObject *kdmeth_plock(PyObject *me, PyObject *arg)
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s:plock", &tag))
     goto end;
   if ((err = key_plock(&kd, KEYDATA_KD(me), tag)) != 0)
@@ -404,7 +417,7 @@ static PyObject *kdmeth_lock(PyObject *me, PyObject *arg)
   int n;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s#:lock", &p, &n))
     goto end;
   key_lock(&kd, KEYDATA_KD(me), p, n);
@@ -440,7 +453,7 @@ static PyObject *meth__KeyData_decode(PyObject *me, PyObject *arg)
     goto end;
   if ((kd = key_decode(p, n)) == 0)
     KEYERR(KERR_MALFORMED);
-  rc =  keydata_pywrap(kd);
+  rc = keydata_pywrap(kd);
 end:
   return (rc);
 }
@@ -454,6 +467,7 @@ static PyMethodDef keydata_pymethods[] = {
   METH (split,                 "KD.split()")
   KWMETH(write,                        "KD.write(filter = <any>) -> STRING")
   KWMETH(encode,               "KD.encode(filter = <any>) -> BYTES")
+  KWMETH(copy,                 "KD.encode(filter = <any>) -> KD")
   METH (plock,                 "KD.plock(TAG) -> ENCRYPTED-KD")
   METH (lock,                  "KD.lock(KEY) -> ENCRYPTED-KD")
 #undef METHNAME
@@ -635,7 +649,7 @@ static PyObject *kdemeth_punlock(PyObject *me, PyObject *arg)
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s:punlock", &tag))
     goto end;
   if ((err = key_punlock(&kd, KEYDATA_KD(me), tag)) != 0)
@@ -652,7 +666,7 @@ static PyObject *kdemeth_unlock(PyObject *me, PyObject *arg)
   int err;
   PyObject *rc = 0;
   key_data *kd;
-  
+
   if (!PyArg_ParseTuple(arg, "s#:unlock", &p, &n))
     goto end;
   if ((err = key_unlock(&kd, KEYDATA_KD(me), p, n)) != 0)
@@ -1097,6 +1111,7 @@ static int keydatastruct_pystore(PyObject *me,
 
   if ((tag = PyString_AsString(key)) == 0)
     goto end;
+  key_split(&KEYDATA_KD(me));
   if (value) {
     if (!KEYDATA_PYCHECK(value))
       TYERR("expected KeyData value");
@@ -1370,16 +1385,16 @@ static PyObject *key_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   PyObject *kfobj;
   uint32 id;
   char *type;
-  long exptime = KEXP_FOREVER;
+  unsigned long exptime = KEXP_FOREVER;
   static char *kwlist[] = { "keyfile", "id", "type", "exptime", 0 };
   key *k;
   int err;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&sl:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&s|O&:new", kwlist,
                                   keyfile_pytype, &kfobj, convu32, &id,
-                                  &type, &exptime))
+                                  &type, convulong, &exptime))
     goto end;
-  if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) == 0)
+  if ((err = key_new(KEYFILE_KF(kfobj), id, type, exptime, &k)) != 0)
     KEYERR(err);
   return (key_dowrap(ty, kfobj, k));
 end:
@@ -1395,7 +1410,7 @@ static void key_pydealloc(PyObject *me)
 static PyObject *kmeth_delete(PyObject *me, PyObject *arg)
 {
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, ":delete")) goto end;
   if ((err = key_delete(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
   RETURN_ME;
@@ -1406,7 +1421,7 @@ end:
 static PyObject *kmeth_expire(PyObject *me, PyObject *arg)
 {
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, ":expire")) goto end;
   if ((err = key_expire(KEY_KF(me), KEY_K(me))) != 0) KEYERR(err);
   RETURN_ME;
@@ -1418,7 +1433,7 @@ static PyObject *kmeth_used(PyObject *me, PyObject *arg)
 {
   long t;
   int err;
-  
+
   if (!PyArg_ParseTuple(arg, "l:used", &t)) goto end;
   if ((err = key_used(KEY_KF(me), KEY_K(me), t)) != 0) KEYERR(err);
   RETURN_ME;
@@ -1909,7 +1924,7 @@ static PyObject *keyfile_pylookup(PyObject *me, PyObject *key)
   }
 end:
   return (rc);
-}  
+}
 
 static PyObject *kfmeth_newkey(PyObject *me, PyObject *arg, PyObject *kw)
 {