]> www.average.org Git - pam_pcsc_cr.git/blobdiff - authfile.c
configure.ac: remove sanitizer by default
[pam_pcsc_cr.git] / authfile.c
index bb2d593deefa60a01c331d2adc1557d5e6820e19..17354b8cb490adcbda42f05bf851423945d9db2f 100644 (file)
@@ -42,13 +42,11 @@ freely, subject to the following restrictions:
  * 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.
+ * substituted with the userid itself. There is no way to make the
+ * resulting path contain '~'. If there is more than one '~', or if
+ * the '~' is at the beginning but userid does not resolve via
+ * getpwnam, or '~' is present but the argument is NULL, path_size
+ * returns 0, and make_path returns 1.
  */
 
 static const char *template = "~/.pam_cr/auth";
@@ -58,50 +56,34 @@ void authfile_template(const char *str)
        template = str;
 }
 
-static int path_size(const char *tokenid, const char *userid)
+/*
+  I know using these two functions and alloca() in between it ugly, but
+  I like the alternatives even less. =ec
+*/
+
+static int path_size(const struct passwd *pw)
 {
-       const char *usub;
-       const char *p, *q;
-       struct passwd *pw;
+       const char *p;
 
        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;
+       if (p && !pw) return 0;
+       if (p == template) return strlen(template)+strlen(pw->pw_dir)+1;
+       else return strlen(template)+strlen(pw->pw_name)+1;
 }
 
-static void
-make_path(char * const path, const char *tokenid, const char *userid)
+static int
+make_path(char * const path, const struct passwd *pw)
 {
-       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);
+               if (!pw) return 1;
+               if (p == template) strcpy(q, pw->pw_dir);
+               else strcpy(q, pw->pw_name);
                while (*q) q++;
                break;
        default:
@@ -109,6 +91,7 @@ make_path(char * const path, const char *tokenid, const char *userid)
                break;
        }
        *q = '\0';
+       return 0;
 }
 
 int parse(char * const buf, const int argc, const char *argv[const])
@@ -124,15 +107,15 @@ int parse(char * const buf, const int argc, const char *argv[const])
        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_obj authfile(const char *userid, const char *password,
+               void (*update_nonce)(char *nonce, const size_t nonsize),
+               const unsigned char *secret, const size_t secsize,
+               const unsigned char *payload, const size_t paylsize,
                struct _auth_chunk (*fetch_key)(const unsigned char *chal,
-                                               const int csize))
+                                               const size_t csize))
 {
        struct _auth_obj ret = {0};
+       const struct passwd *pw = NULL;
        mode_t oldmask;
        FILE *fp = NULL;
        char *fn, *nfn;
@@ -140,23 +123,26 @@ struct _auth_obj authfile(const char *tokenid,
        struct stat st = {0};
        char *buf = NULL;
        struct {
-               const char *tokenid;
                const char *userid;
                const char *nonce;
                const char *hablob;
-       } w = {"", NULL, NULL, NULL};
+       } 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";
+       if (userid) pw = getpwnam(userid);
+       if ((fnl = path_size(pw)) == 0) {
+               ret.err = "authfile path_size failed";
                return ret;
        }
        fn = alloca(fnl);
-       make_path(fn, tokenid, userid);
+       if (make_path(fn, pw)) {
+               ret.err = "authfile make_path failed";
+               return ret;
+       }
        nfn = alloca(fnl+32);
        snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
        fp = fopen(fn, "r");
@@ -209,13 +195,12 @@ struct _auth_obj authfile(const char *tokenid,
 
                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,
+               } else if (fprintf(fp, "%s:%s:%s\n",
                                userid?userid:w.userid, newnonce, b64) < 0) {
                        ret.err = strerror(errno);
                }
                if (st.st_uid || st.st_gid) {
-                       if (fchown(fileno(fp), st.st_uid, st.st_gid)) /*ign*/;
+                       if (fchown(fileno(fp), st.st_uid, st.st_gid)) {/*ign*/;}
                }
                if (fclose(fp) < 0) {
                        ret.err = strerror(errno);