17 * Template string may contain zero or one '~' and zero or one '?'.
18 * '~' at the beginning of the template string is substituted with
19 * the home directory of the userid. In any other position it is
20 * substituted with the userid itself. '?' is substituted with the
21 * tokenid. There is no way to make the resulting path contain '~'
22 * or '?'. If there is more than one '~' or '?', or if the '~' is
23 * at the beginning but userid does not resolve via getpwnam, or
24 * the character to substitute is present but the argument is NULL,
25 * NULL is returned. Otherwise, malloc()'ed area containg the path
29 static char *template = "~/.pam_cr/auth";
31 void authfile_template(char *str)
36 static int path_size(const char *tokenid, const char *userid)
42 if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
43 if ((q = strchr(template, '?')) != strrchr(template, '?')) return 0;
44 if (p && !userid) return 0;
45 if (q && !tokenid) return 0;
47 pw = getpwnam(userid);
53 return strlen(template) + p?strlen(usub):0 + q?strlen(tokenid):0 + 1;
57 make_path(char * const path, const char *tokenid, const char *userid)
64 if (template[0] == '~') {
65 pw = getpwnam(userid);
72 for (p = template; *p; p++) switch (*p) {
88 int parse(char * const buf, const int argc, const char *argv[const])
93 for (i = 0, p = buf; *p; p = q+1, i++) {
94 for (q = p; *q && *q != ':' && *q != '\r' && *q != '\n'; q++) ;
96 if (*p && i < argc) argv[i] = p;
101 struct _auth_obj authfile(const char *tokenid,
102 const char *userid, const char *password,
103 void (*update_nonce)(char *nonce, const int nonsize),
104 const unsigned char *secret, const int secsize,
105 const unsigned char *payload, const int paylsize,
106 struct _auth_chunk (*fetch_key)(const unsigned char *chal,
109 struct _auth_obj ret = {0};
120 } w = {"", NULL, NULL, NULL};
121 unsigned char *ablob = NULL;
127 if ((fnl = path_size(tokenid, userid)) == 0) {
128 ret.err = "authfile path impossible to build";
132 make_path(fn, tokenid, userid);
138 if (fstat(fd, &st)) st.st_size = 2047;
139 if (st.st_size > 2047) st.st_size = 2047;
140 buf = alloca(st.st_size + 1);
141 if (!fgets(buf, st.st_size + 1, fp)) {
142 ret.err = strerror(errno);
143 } else if (parse(buf, sizeof(w)/sizeof(char*),
144 (const char ** const)&w)){
145 ret.err = "error: unparseable auth file";
149 if (ret.err) return ret;
152 int hlen = strlen(w.hablob);
153 if (hlen % 32 != 0) {
154 ret.err = "error: auth string has wrong length";
156 strspn(w.hablob, "0123456789abcdefABCDEF")) {
157 ret.err = "error: auth string not hexadecimal";
162 ablob = alloca(blobsize);
163 for (i = 0; i < blobsize; i++)
164 sscanf(&w.hablob[i*2], "%2hhx", &ablob[i]);
167 if (ret.err) return ret;
169 nonsize = w.nonce ? strlen(w.nonce)*2 : 32;
170 if (nonsize < 32) nonsize = 32;
171 newnonce = alloca(nonsize);
172 if (w.nonce) strcpy(newnonce, w.nonce);
173 else memset(newnonce, 0, nonsize);
174 update_nonce(newnonce, nonsize);
176 ao = authobj(userid?userid:w.userid, password,
177 w.nonce, newnonce, secret, secsize,
178 payload, paylsize, ablob, blobsize,
183 if (ao.data) memset(ao.data, 0, ao.datasize);
184 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
185 if (ao.buffer) free(ao.buffer);
189 oldmask = umask(077);
190 if ((fp = fopen(fn, "w"))) {
193 if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:w.tokenid,
194 userid?userid:w.userid, newnonce) < 0) {
195 ret.err = strerror(errno);
196 } else for (i = 0; i < ao.datasize; i++)
197 if (fprintf(fp, "%02x", ao.data[i]) < 0) {
198 ret.err = strerror(errno);
201 if (fclose(fp) < 0) {
202 ret.err = strerror(errno);
205 ret.err = strerror(errno);
207 (void)umask(oldmask);
210 int bufsize = (w.userid?strlen(w.userid)+1:0) + ao.paylsize;
212 if ((ret.buffer = malloc(bufsize)) == NULL) {
213 ret.err = "authfile malloc failed";
215 unsigned char *p = ret.buffer;
217 strcpy((char*)p, w.userid);
219 ret.datasize = strlen(w.userid)+1;
220 p += strlen(w.userid)+1;
223 memcpy(p, ao.payload, ao.paylsize);
225 ret.paylsize = ao.paylsize;
231 if (ao.data) memset(ao.data, 0, ao.datasize);
232 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
233 if (ao.buffer) free(ao.buffer);