]> www.average.org Git - pam_pcsc_cr.git/blobdiff - authfile.c
use base64 instead of hex
[pam_pcsc_cr.git] / authfile.c
index f1ac1509379c1ba15ee4250f89b4ca1113189fbd..bb2d593deefa60a01c331d2adc1557d5e6820e19 100644 (file)
+/*
+Copyright (c) 2013 Eugene Crosser
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+    1. The origin of this software must not be misrepresented; you must
+    not claim that you wrote the original software. If you use this
+    software in a product, an acknowledgment in the product documentation
+    would be appreciated but is not required.
+
+    2. Altered source versions must be plainly marked as such, and must
+    not be misrepresented as being the original software.
+
+    3. This notice may not be removed or altered from any source
+    distribution.
+*/
+
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <pwd.h>
+#include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
 #include <alloca.h>
+#include "base64.h"
 #include "authobj.h"
 #include "authfile.h"
-#include "pcsc_cr.h"
 
-#define OBJSIZE 256
+/*
+ * Template string may contain zero or one '~' and zero or one '?'.
+ * '~' at the beginning of the template string is substituted with
+ * the home directory of the userid. In any other position it is
+ * substituted with the userid itself. '?' is substituted with the
+ * tokenid. There is no way to make the resulting path contain '~'
+ * or '?'. If there is more than one '~' or '?', or if the '~' is
+ * at the beginning but userid does not resolve via getpwnam, or
+ * the character to substitute is present but the argument is NULL,
+ * NULL is returned. Otherwise, malloc()'ed area containg the path
+ * string.
+ */
+
+static const char *template = "~/.pam_cr/auth";
+
+void authfile_template(const char *str)
+{
+       template = str;
+}
+
+static int path_size(const char *tokenid, const char *userid)
+{
+       const char *usub;
+       const char *p, *q;
+       struct passwd *pw;
+
+       if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
+       if ((q = strchr(template, '?')) != strrchr(template, '?')) return 0;
+       if (p && !userid) return 0;
+       if (q && !tokenid) return 0;
+       if (p == template) {
+               pw = getpwnam(userid);
+               if (!pw) return 0;
+               usub = pw->pw_dir;
+       } else {
+               usub = userid;
+       }
+       return strlen(template)+(p?strlen(usub):0)+(q?strlen(tokenid):0)+1;
+}
+
+static void
+make_path(char * const path, const char *tokenid, const char *userid)
+{
+       const char *usub;
+       const char *p;
+       char *q;
+       struct passwd *pw;
+
+       path[0] = '\0';
+       if (template[0] == '~') {
+               pw = getpwnam(userid);
+               if (!pw) return;
+               usub = pw->pw_dir;
+       } else {
+               usub = userid;
+       }
+       q = path;
+       for (p = template; *p; p++) switch (*p) {
+       case '~':
+               strcpy(q, usub);
+               while (*q) q++;
+               break;
+       case '?':
+               strcpy(q, tokenid);
+               while (*q) q++;
+               break;
+       default:
+               *q++ = *p;
+               break;
+       }
+       *q = '\0';
+}
 
-int update_authfile(const char *fn, const char *tokenid, const char *id,
-                const char *password, const char *nonce,
-                const unsigned char *secret, const int secsize,
-                const unsigned char *payload, const int paysize)
+int parse(char * const buf, const int argc, const char *argv[const])
 {
-       FILE *fp;
-       int rc;
+       char *p, *q;
        int i;
-       unsigned char key[20];
-       int keysize = sizeof(key);
-       unsigned char *mysecret = secret;
-       int mysecsize = secsize;
-       unsigned char *myload = payload;
-       int myloadsize = paysize;
-       unsigned char *authobj = NULL;
-       int authsize = 0;
+
+       for (i = 0, p = buf; *p; p = q+1, i++) {
+               for (q = p; *q && *q != ':' && *q != '\r' && *q != '\n'; q++) ;
+               *q = '\0';
+               if (*p && i < argc) argv[i] = p;
+       }
+       return i != argc;
+}
+
+struct _auth_obj authfile(const char *tokenid,
+               const char *userid, const char *password,
+               void (*update_nonce)(char *nonce, const int nonsize),
+               const unsigned char *secret, const int secsize,
+               const unsigned char *payload, const int paylsize,
+               struct _auth_chunk (*fetch_key)(const unsigned char *chal,
+                                               const int csize))
+{
+       struct _auth_obj ret = {0};
+       mode_t oldmask;
+       FILE *fp = NULL;
+       char *fn, *nfn;
+       int fnl;
+       struct stat st = {0};
        char *buf = NULL;
-       char *mytokenid = NULL;
-       char *myid = NULL;
-       char *mynonce = NULL;
-       char *hauthobj = NULL;
-       unsigned char *oldauthobj = NULL;
-       int oldauthsize;
-
-       if ((fp = fopen(fn, "r"))) {
-               struct stat st;
-               int fd = fileno(fp);
-
-               if (!fstat(fd, &st)) {
-                       eprint("fstat \"%s\" (fd %d) error: %s",
-                               fn, fd, strerror(errno));
-                       st.st_size = 2047;
-               }
+       struct {
+               const char *tokenid;
+               const char *userid;
+               const char *nonce;
+               const char *hablob;
+       } w = {"", NULL, NULL, NULL};
+       unsigned char *ablob = NULL;
+       int blobsize = 0;
+       char *newnonce;
+       int nonsize;
+       struct _auth_obj ao;
+
+       if ((fnl = path_size(tokenid, userid)) == 0) {
+               ret.err = "authfile path impossible to build";
+               return ret;
+       }
+       fn = alloca(fnl);
+       make_path(fn, tokenid, userid);
+       nfn = alloca(fnl+32);
+       snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
+       fp = fopen(fn, "r");
+       if (fp) {
+               if (fstat(fileno(fp), &st)) st.st_size = 2047;
                if (st.st_size > 2047) st.st_size = 2047;
                buf = alloca(st.st_size + 1);
-               if (fgets(buf, st.st_size + 1, fp)) {
-                       char *p;
-
-                       p = &buf[strlen(buf) - 1];
-                       while (*p == '\n' || *p == '\r') *p-- = '\0';
-                       mytokenid = strtok(buf, ":");
-                       myid = strtok(NULL, ":");
-                       mynonce = strtok(NULL, ":");
-                       hauthobj = strtok(NULL, ":");
-               } else {
-                       eprint("error reading from %s: %s",
-                               fn, strerror(errno));
+               if (!fgets(buf, st.st_size + 1, fp)) {
+                       ret.err = strerror(errno);
+               } else if (parse(buf, sizeof(w)/sizeof(char*),
+                                       (const char ** const)&w)){
+                       ret.err = "error: unparseable auth file";
                }
                fclose(fp);
        }
-       if (hauthobj) {
-               int hlen;
-
-               hlen = strlen(hauthobj);
-               if (hlen % 32 != 0) {
-                       eprint("error: auth string has wrong length");
-               } else if (hlen !=
-                               strspn(hauthobj, "0123456789abcdefABCDEF")) {
-                       eprint("error: auth string not hexadecimal");
-               } else {
-                       oldauthsize = hlen/2;
-                       oldauthobj = alloca(oldauthsize);
-                       for (i = 0; i < oldauthsize; i++)
-                               sscanf(&hauthobj[i*2], "%2hhx", &oldauthobj[i]);
-               }
+       if (ret.err) return ret;
+
+       if (w.hablob) {
+               blobsize = strlen(w.hablob)*3/4;
+               ablob = alloca(blobsize);
+               if (b64_decode(w.hablob, ablob, &blobsize))
+                       ret.err = "error: undecodeable auth string";
        }
+       if (ret.err) return ret;
 
-       if (!secret) {
-               unsigned char chal[64];
-               int csize = sizeof(chal);
-               long rc;
+       nonsize = w.nonce ? strlen(w.nonce)*2 : 32;
+       if (nonsize < 32) nonsize = 32;
+       newnonce = alloca(nonsize);
+       if (w.nonce) strcpy(newnonce, w.nonce);
+       else memset(newnonce, 0, nonsize);
+       update_nonce(newnonce, nonsize);
 
-               if (!oldauthobj || !password) {
-                       eprint("if no secret given, old auth file must"
-                               " be present and password must be given");
-                       return -1;
-               }
-               rc = make_challenge(myid, password, mynonce, chal, &csize);
-               if (rc) {
-                       eprint("cannot make challenge");
-                       return -1;
-               }
-               rc = pcsc_cr(chal, csize, key, &keysize);
-               if (rc) {
-                       eprint("error querying token: %s", pcsc_errstr(rc));
-                       return -1;
-               }
-               mysecsize = oldauthsize;
-               mysecret = alloca(mysecsize);
-               myloadsize  = oldauthsize;
-               myload = alloca(myloadsize);
-               rc = parse_authobj(key, keysize, oldauthobj, oldauthsize,
-                       mysecret, &mysecsize, myload, &myloadsize);
-               if (rc) {
-                       eprint("cannot parse old authobj: %d", rc);
-                       return -1;
-               }
-       }
-       if (tokenid) mytokenid = tokenid;
-       if (id) myid = id;
-       if (nonce) mynonce = nonce;
-       else {
-               unsigned int prev = atoi(mynonce);
-               mynonce = alloca(16);
-               sprintf(mynonce, "%d", prev + 1);
-       }
+       ao = authobj(userid?userid:w.userid, password,
+                       w.nonce, newnonce, secret, secsize,
+                       payload, paylsize, ablob, blobsize,
+                       fetch_key);
 
-       authsize = ((mysecsize + myloadsize + 16 + 4 * sizeof(short) - 1) /
-                       16 + 1) * 16;
-       authobj = alloca(authsize);
-       rc = make_authobj(myid, password, mynonce, mysecret, mysecsize,
-                       myload, myloadsize, authobj, &authsize);
-       if (rc) {
-               eprint("make_authobj error %d", rc);
-               return -1;
+       if (ao.err) {
+               ret.err = ao.err;
+               if (ao.data) memset(ao.data, 0, ao.datasize);
+               if (ao.payload) memset(ao.payload, 0, ao.paylsize);
+               if (ao.buffer) free(ao.buffer);
+               return ret;
        }
 
-       if ((fp = fopen(fn, "w"))) {
-               if (fprintf(fp, "%s:%s:%s:", mytokenid, myid, mynonce) < 0) {
-                       eprint("cannot write to \"%s\": %s",
-                               fn, strerror(errno));
-                       return -1;
+       oldmask = umask(077);
+       if ((fp = fopen(nfn, "w"))) {
+               int bsize = ((ao.datasize-1)/3+1)*4+1;
+               char *b64 = alloca(bsize);
+
+               if (b64_encode(ao.data, ao.datasize, b64, &bsize)) {
+                       ret.err = "error: could not encode auth string";
+               } else if (fprintf(fp, "%s:%s:%s:%s\n",
+                               tokenid?tokenid:w.tokenid,
+                               userid?userid:w.userid, newnonce, b64) < 0) {
+                       ret.err = strerror(errno);
                }
-               for (i = 0; i < authsize; i++)
-                   if (fprintf(fp, "%02x", authobj[i]) < 0) {
-                       eprint("cannot write to \"%s\": %s",
-                               fn, strerror(errno));
-                       return -1;
+               if (st.st_uid || st.st_gid) {
+                       if (fchown(fileno(fp), st.st_uid, st.st_gid)) /*ign*/;
                }
-               fprintf(fp, "\n");
                if (fclose(fp) < 0) {
-                       eprint("cannot close \"%s\": %s",
-                               fn, strerror(errno));
-                       return -1;
+                       ret.err = strerror(errno);
                }
        } else {
-               eprint("cannot open \"%s\": %s",
-                       fn, strerror(errno));
-               return -1;
+               ret.err = strerror(errno);
        }
-       return 0;
+       (void)umask(oldmask);
+       if (ret.err) {
+               unlink(nfn); /* may not exist but no matter */
+       } else if (rename(nfn, fn)) {
+               ret.err = strerror(errno);
+       }
+
+       if (!ret.err) {
+               int bufsize = (w.userid?strlen(w.userid)+1:0) + ao.paylsize + 1;
+               if (bufsize) {
+                       if ((ret.buffer = malloc(bufsize)) == NULL) {
+                               ret.err = "authfile malloc failed";
+                       } else {
+                               unsigned char *p = ret.buffer;
+                               if (w.userid) {
+                                       strcpy((char*)p, w.userid);
+                                       ret.data = p;
+                                       ret.datasize = strlen(w.userid)+1;
+                                       p += strlen(w.userid)+1;
+                               }
+                               if (ao.payload) {
+                                       memcpy(p, ao.payload, ao.paylsize);
+                                       p[ao.paylsize] = '\0';
+                                       ret.payload = p;
+                                       ret.paylsize = ao.paylsize+1;
+                               }
+                       }
+               }
+       }
+
+       if (ao.data) memset(ao.data, 0, ao.datasize);
+       if (ao.payload) memset(ao.payload, 0, ao.paylsize);
+       if (ao.buffer) free(ao.buffer);
+       return ret;
 }