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 struct _auth_obj authfile(const char *tokenid,
89 const char *userid, const char *password,
90 void (*update_nonce)(char *nonce, const int nonsize),
91 const unsigned char *secret, const int secsize,
92 const unsigned char *payload, const int paylsize,
93 struct _auth_chunk (*fetch_key)(const unsigned char *chal,
96 struct _auth_obj ret = {0};
102 const char *wtokenid = "", *wuserid = NULL, *wnonce = NULL;
103 const char *hablob = NULL;
104 unsigned char *ablob = NULL;
110 if ((fnl = path_size(tokenid, userid)) == 0) {
111 ret.err = "authfile path impossible to build";
115 make_path(fn, tokenid, userid);
121 if (fstat(fd, &st)) st.st_size = 2047;
122 if (st.st_size > 2047) st.st_size = 2047;
123 buf = alloca(st.st_size + 1);
124 if (fgets(buf, st.st_size + 1, fp)) {
127 p = &buf[strlen(buf) - 1];
128 while (*p == '\n' || *p == '\r') *p-- = '\0';
129 wtokenid = strtok(buf, ":");
130 wuserid = strtok(NULL, ":");
131 wnonce = strtok(NULL, ":");
132 hablob = strtok(NULL, ":");
134 ret.err = strerror(errno);
138 if (ret.err) return ret;
141 int hlen = strlen(hablob);
142 if (hlen % 32 != 0) {
143 ret.err = "error: auth string has wrong length";
145 strspn(hablob, "0123456789abcdefABCDEF")) {
146 ret.err = "error: auth string not hexadecimal";
151 ablob = alloca(blobsize);
152 for (i = 0; i < blobsize; i++)
153 sscanf(&hablob[i*2], "%2hhx", &ablob[i]);
156 if (ret.err) return ret;
158 nonsize = wnonce ? strlen(wnonce)*2 : 32;
159 if (nonsize < 32) nonsize = 32;
160 newnonce = alloca(nonsize);
161 if (wnonce) strcpy(newnonce, wnonce);
162 else memset(newnonce, 0, nonsize);
163 update_nonce(newnonce, nonsize);
165 ao = authobj(userid?userid:wuserid, password,
166 wnonce, newnonce, secret, secsize,
167 payload, paylsize, ablob, blobsize,
172 if (ao.data) memset(ao.data, 0, ao.datasize);
173 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
174 if (ao.buffer) free(ao.buffer);
178 oldmask = umask(077);
179 if ((fp = fopen(fn, "w"))) {
182 if (fprintf(fp, "%s:%s:%s:", tokenid?tokenid:wtokenid,
183 userid?userid:wuserid, newnonce) < 0) {
184 ret.err = strerror(errno);
185 } else for (i = 0; i < ao.datasize; i++)
186 if (fprintf(fp, "%02x", ao.data[i]) < 0) {
187 ret.err = strerror(errno);
190 if (fclose(fp) < 0) {
191 ret.err = strerror(errno);
194 ret.err = strerror(errno);
196 (void)umask(oldmask);
199 int bufsize = (wuserid?strlen(wuserid)+1:0) + ao.paylsize;
201 if ((ret.buffer = malloc(bufsize)) == NULL) {
202 ret.err = "authfile malloc failed";
204 unsigned char *p = ret.buffer;
206 strcpy((char*)p, wuserid);
208 ret.datasize = strlen(wuserid)+1;
209 p += strlen(wuserid)+1;
212 memcpy(p, ao.payload, ao.paylsize);
214 ret.paylsize = ao.paylsize;
220 if (ao.data) memset(ao.data, 0, ao.datasize);
221 if (ao.payload) memset(ao.payload, 0, ao.paylsize);
222 if (ao.buffer) free(ao.buffer);