]> www.average.org Git - pam_pcsc_cr.git/blobdiff - authfile.c
path= and injectauth params
[pam_pcsc_cr.git] / authfile.c
index abf31fb8f91ff665823e67395fe9eb26043a078c..f6a7fea4057d7df1fc90a2f847762d2981c4c7bd 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <pwd.h>
+#include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,9 +27,9 @@
  * string.
  */
 
-static char *template = "~/.pam_cr/auth";
+static const char *template = "~/.pam_cr/auth";
 
-void authfile_template(char *str)
+void authfile_template(const char *str)
 {
        template = str;
 }
@@ -36,7 +37,7 @@ void authfile_template(char *str)
 static int path_size(const char *tokenid, const char *userid)
 {
        const char *usub;
-       char *p, *q;
+       const char *p, *q;
        struct passwd *pw;
 
        if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
@@ -57,7 +58,8 @@ static void
 make_path(char * const path, const char *tokenid, const char *userid)
 {
        const char *usub;
-       char *p, *q;
+       const char *p;
+       char *q;
        struct passwd *pw;
 
        path[0] = '\0';
@@ -109,7 +111,7 @@ struct _auth_obj authfile(const char *tokenid,
        struct _auth_obj ret = {0};
        mode_t oldmask;
        FILE *fp = NULL;
-       char *fn;
+       char *fn, *nfn;
        int fnl;
        char *buf = NULL;
        struct {
@@ -130,6 +132,8 @@ struct _auth_obj authfile(const char *tokenid,
        }
        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) {
                struct stat st;
@@ -187,7 +191,7 @@ struct _auth_obj authfile(const char *tokenid,
        }
 
        oldmask = umask(077);
-       if ((fp = fopen(fn, "w"))) {
+       if ((fp = fopen(nfn, "w"))) {
                int i;
 
                if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:w.tokenid,
@@ -205,9 +209,14 @@ struct _auth_obj authfile(const char *tokenid,
                ret.err = strerror(errno);
        }
        (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;
+               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";
@@ -221,8 +230,9 @@ struct _auth_obj authfile(const char *tokenid,
                                }
                                if (ao.payload) {
                                        memcpy(p, ao.payload, ao.paylsize);
+                                       p[ao.paylsize] = '\0';
                                        ret.payload = p;
-                                       ret.paylsize = ao.paylsize;
+                                       ret.paylsize = ao.paylsize+1;
                                }
                        }
                }