]> www.average.org Git - pam_pcsc_cr.git/blobdiff - authfile.c
mark down README
[pam_pcsc_cr.git] / authfile.c
index 8c023d1e8e3a8a0ddd422082d96c5288e69dabec..325c29d7a4e66ea96bd79e1efdc47f25b42dc7f1 100644 (file)
@@ -1,3 +1,26 @@
+/*
+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
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -27,9 +50,9 @@
  * string.
  */
 
  * 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;
 }
 {
        template = str;
 }
@@ -37,7 +60,7 @@ void authfile_template(char *str)
 static int path_size(const char *tokenid, const char *userid)
 {
        const char *usub;
 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;
        struct passwd *pw;
 
        if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
@@ -51,14 +74,15 @@ static int path_size(const char *tokenid, const char *userid)
        } else {
                usub = userid;
        }
        } else {
                usub = userid;
        }
-       return strlen(template) + p?strlen(usub):0 + q?strlen(tokenid):0 + 1;
+       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;
 }
 
 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';
        struct passwd *pw;
 
        path[0] = '\0';
@@ -112,6 +136,7 @@ struct _auth_obj authfile(const char *tokenid,
        FILE *fp = NULL;
        char *fn, *nfn;
        int fnl;
        FILE *fp = NULL;
        char *fn, *nfn;
        int fnl;
+       struct stat st = {0};
        char *buf = NULL;
        struct {
                const char *tokenid;
        char *buf = NULL;
        struct {
                const char *tokenid;
@@ -135,10 +160,7 @@ struct _auth_obj authfile(const char *tokenid,
        snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
        fp = fopen(fn, "r");
        if (fp) {
        snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
        fp = fopen(fn, "r");
        if (fp) {
-               struct stat st;
-               int fd = fileno(fp);
-
-               if (fstat(fd, &st)) st.st_size = 2047;
+               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)) {
                if (st.st_size > 2047) st.st_size = 2047;
                buf = alloca(st.st_size + 1);
                if (!fgets(buf, st.st_size + 1, fp)) {
@@ -201,6 +223,9 @@ struct _auth_obj authfile(const char *tokenid,
                        ret.err = strerror(errno);
                }
                fprintf(fp, "\n");
                        ret.err = strerror(errno);
                }
                fprintf(fp, "\n");
+               if (st.st_uid || st.st_gid) {
+                       (void)fchown(fileno(fp), st.st_uid, st.st_gid);
+               }
                if (fclose(fp) < 0) {
                        ret.err = strerror(errno);
                }
                if (fclose(fp) < 0) {
                        ret.err = strerror(errno);
                }
@@ -215,7 +240,7 @@ struct _auth_obj authfile(const char *tokenid,
        }
 
        if (!ret.err) {
        }
 
        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";
                if (bufsize) {
                        if ((ret.buffer = malloc(bufsize)) == NULL) {
                                ret.err = "authfile malloc failed";
@@ -229,8 +254,9 @@ struct _auth_obj authfile(const char *tokenid,
                                }
                                if (ao.payload) {
                                        memcpy(p, ao.payload, ao.paylsize);
                                }
                                if (ao.payload) {
                                        memcpy(p, ao.payload, ao.paylsize);
+                                       p[ao.paylsize] = '\0';
                                        ret.payload = p;
                                        ret.payload = p;
-                                       ret.paylsize = ao.paylsize;
+                                       ret.paylsize = ao.paylsize+1;
                                }
                        }
                }
                                }
                        }
                }