X-Git-Url: http://www.average.org/gitweb/?p=pam_pcsc_cr.git;a=blobdiff_plain;f=authfile.c;h=bb2d593deefa60a01c331d2adc1557d5e6820e19;hp=abf31fb8f91ff665823e67395fe9eb26043a078c;hb=2b714d273b08b945ef677e0fbbf2c8d3d4437980;hpb=54f191d82623f7bc8468322a0aa044002584827d diff --git a/authfile.c b/authfile.c index abf31fb..bb2d593 100644 --- a/authfile.c +++ b/authfile.c @@ -1,15 +1,40 @@ +/* +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 #include #include +#include #include #include #include #include #include #include +#include "base64.h" #include "authobj.h" #include "authfile.h" @@ -26,9 +51,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 +61,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; @@ -50,14 +75,15 @@ static int path_size(const char *tokenid, const char *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; - char *p, *q; + const char *p; + char *q; struct passwd *pw; path[0] = '\0'; @@ -109,8 +135,9 @@ 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; + struct stat st = {0}; char *buf = NULL; struct { const char *tokenid; @@ -130,12 +157,11 @@ 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; - 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)) { @@ -149,20 +175,10 @@ struct _auth_obj authfile(const char *tokenid, if (ret.err) return ret; if (w.hablob) { - int hlen = strlen(w.hablob); - if (hlen % 32 != 0) { - ret.err = "error: auth string has wrong length"; - } else if (hlen != - strspn(w.hablob, "0123456789abcdefABCDEF")) { - ret.err = "error: auth string not hexadecimal"; - } else { - int i; - - blobsize = hlen/2; - ablob = alloca(blobsize); - for (i = 0; i < blobsize; i++) - sscanf(&w.hablob[i*2], "%2hhx", &ablob[i]); - } + 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; @@ -187,17 +203,20 @@ struct _auth_obj authfile(const char *tokenid, } oldmask = umask(077); - if ((fp = fopen(fn, "w"))) { - int i; + if ((fp = fopen(nfn, "w"))) { + int bsize = ((ao.datasize-1)/3+1)*4+1; + char *b64 = alloca(bsize); - if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:w.tokenid, - userid?userid:w.userid, newnonce) < 0) { - ret.err = strerror(errno); - } else for (i = 0; i < ao.datasize; i++) - if (fprintf(fp, "%02x", ao.data[i]) < 0) { + 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); } - fprintf(fp, "\n"); + if (st.st_uid || st.st_gid) { + if (fchown(fileno(fp), st.st_uid, st.st_gid)) /*ign*/; + } if (fclose(fp) < 0) { ret.err = strerror(errno); } @@ -205,9 +224,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 +245,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; } } }