18 * Template string may contain zero or one '~' and zero or one '?'.
19 * '~' at the beginning of the template string is substituted with
20 * the home directory of the userid. In any other position it is
21 * substituted with the userid itself. '?' is substituted with the
22 * tokenid. There is no way to make the resulting path contain '~'
23 * or '?'. If there is more than one '~' or '?', or if the '~' is
24 * at the beginning but userid does not resolve via getpwnam, or
25 * the character to substitute is present but the argument is NULL,
26 * NULL is returned. Otherwise, malloc()'ed area containg the path
30 static char *template = "~/.pam_cr/auth";
32 void authfile_template(char *str)
37 static int path_size(const char *tokenid, const char *userid)
43 if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
44 if ((q = strchr(template, '?')) != strrchr(template, '?')) return 0;
45 if (p && !userid) return 0;
46 if (q && !tokenid) return 0;
48 pw = getpwnam(userid);
54 return strlen(template) + p?strlen(usub):0 + q?strlen(tokenid):0 + 1;
58 make_path(char * const path, const char *tokenid, const char *userid)
65 if (template[0] == '~') {
66 pw = getpwnam(userid);
73 for (p = template; *p; p++) switch (*p) {
89 int parse(char * const buf, const int argc, const char *argv[const])
94 for (i = 0, p = buf; *p; p = q+1, i++) {
95 for (q = p; *q && *q != ':' && *q != '\r' && *q != '\n'; q++) ;
97 if (*p && i < argc) argv[i] = p;
102 struct _auth_obj authfile(const char *tokenid,
103 const char *userid, const char *password,
104 void (*update_nonce)(char *nonce, const int nonsize),
105 const unsigned char *secret, const int secsize,
106 const unsigned char *payload, const int paylsize,
107 struct _auth_chunk (*fetch_key)(const unsigned char *chal,
110 struct _auth_obj ret = {0};
121 } w = {"", NULL, NULL, NULL};
122 unsigned char *ablob = NULL;
128 if ((fnl = path_size(tokenid, userid)) == 0) {
129 ret.err = "authfile path impossible to build";
133 make_path(fn, tokenid, userid);
134 nfn = alloca(fnl+32);
135 snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
141 if (fstat(fd, &st)) st.st_size = 2047;
142 if (st.st_size > 2047) st.st_size = 2047;
143 buf = alloca(st.st_size + 1);
144 if (!fgets(buf, st.st_size + 1, fp)) {
145 ret.err = strerror(errno);
146 } else if (parse(buf, sizeof(w)/sizeof(char*),
147 (const char ** const)&w)){
148 ret.err = "error: unparseable auth file";
152 if (ret.err) return ret;
155 int hlen = strlen(w.hablob);
156 if (hlen % 32 != 0) {
157 ret.err = "error: auth string has wrong length";
159 strspn(w.hablob, "0123456789abcdefABCDEF")) {
160 ret.err = "error: auth string not hexadecimal";
165 ablob = alloca(blobsize);
166 for (i = 0; i < blobsize; i++)
167 sscanf(&w.hablob[i*2], "%2hhx", &ablob[i]);
170 if (ret.err) return ret;
172 nonsize = w.nonce ? strlen(w.nonce)*2 : 32;
173 if (nonsize < 32) nonsize = 32;
174 newnonce = alloca(nonsize);
175 if (w.nonce) strcpy(newnonce, w.nonce);
176 else memset(newnonce, 0, nonsize);
177 update_nonce(newnonce, nonsize);
179 ao = authobj(userid?userid:w.userid, password,
180 w.nonce, newnonce, secret, secsize,
181 payload, paylsize, ablob, blobsize,
186 if (ao.data) memset(ao.data, 0, ao.datasize);
187 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
188 if (ao.buffer) free(ao.buffer);
192 oldmask = umask(077);
193 if ((fp = fopen(nfn, "w"))) {
196 if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:w.tokenid,
197 userid?userid:w.userid, newnonce) < 0) {
198 ret.err = strerror(errno);
199 } else for (i = 0; i < ao.datasize; i++)
200 if (fprintf(fp, "%02x", ao.data[i]) < 0) {
201 ret.err = strerror(errno);
204 if (fclose(fp) < 0) {
205 ret.err = strerror(errno);
208 ret.err = strerror(errno);
210 (void)umask(oldmask);
212 unlink(nfn); /* may not exist but no matter */
213 } else if (rename(nfn, fn)) {
214 ret.err = strerror(errno);
218 int bufsize = (w.userid?strlen(w.userid)+1:0) + ao.paylsize + 1;
220 if ((ret.buffer = malloc(bufsize)) == NULL) {
221 ret.err = "authfile malloc failed";
223 unsigned char *p = ret.buffer;
225 strcpy((char*)p, w.userid);
227 ret.datasize = strlen(w.userid)+1;
228 p += strlen(w.userid)+1;
231 memcpy(p, ao.payload, ao.paylsize);
232 p[ao.paylsize] = '\0';
234 ret.paylsize = ao.paylsize+1;
240 if (ao.data) memset(ao.data, 0, ao.datasize);
241 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
242 if (ao.buffer) free(ao.buffer);