- FILE *fp;
- int rc;
- int i;
- unsigned char key[20];
- int keysize = sizeof(key);
- unsigned char mysecret[20];
- int mysecsize = sizeof(mysecret);
- unsigned char myload[256];
- int myloadsize = sizeof(myload);
- unsigned char *authobj = alloca(OBJSIZE);
- int authsize = OBJSIZE;
- char buf[512];
- char *oldtokenid = NULL, *oldid = NULL, *oldnonce = NULL,
- *hauthobj = NULL;
- unsigned char *oldauthobj = NULL;
- int oldauthsize;
-
- if ((fp = fopen(fn, "r"))) {
- if (fgets(buf, sizeof(buf), fp)) {
- oldtokenid = strtok(buf, ":\r\n");
- oldid = strtok(NULL, ":\r\n");
- oldnonce = strtok(NULL, ":\r\n");
- hauthobj = strtok(NULL, ":\r\n");
+ const char *usub;
+ char *path;
+ char *p, *q;
+ struct passwd *pw;
+
+ if ((p = strchr(template, '~')) != strrchr(template, '~')) return NULL;
+ if ((q = strchr(template, '?')) != strrchr(template, '?')) return NULL;
+ if (p && !userid) return NULL;
+ if (q && !tokenid) return NULL;
+ if (p == template) {
+ pw = getpwnam(userid);
+ if (!pw) return NULL;
+ usub = pw->pw_dir;
+ } else {
+ usub = userid;
+ }
+ path = malloc(strlen(template) + p?strlen(usub):0 +
+ q?strlen(tokenid):0 + 1);
+ if (!path) return NULL;
+ q = path;
+ for (p = template; *p; p++) switch (*p) {
+ case '~':
+ strcpy(q, usub);
+ while (*q) q++;
+ break;
+ case '?':
+ strcpy(q, tokenid);
+ while (*q) q++;
+ break;
+ default:
+ *q++ = *p;
+ break;
+ }
+ *q = '\0';
+ return path;
+}
+
+struct _auth_obj authfile(const char *tokenid,
+ const char *userid, const char *password,
+ void (*update_nonce)(char *nonce, const int nonsize),
+ const unsigned char *secret, const int secsize,
+ const unsigned char *payload, const int paylsize,
+ struct _auth_chunk (*fetch_key)(const unsigned char *chal,
+ const int csize))
+{
+ struct _auth_obj ret = {0};
+ FILE *fp = NULL;
+ char *fn;
+ char *buf = NULL;
+ const char *wtokenid = NULL, *wuserid = NULL, *wnonce = NULL;
+ const char *hablob = NULL;
+ unsigned char *ablob = NULL;
+ int blobsize = 0;
+ char *newnonce;
+ int nonsize;
+ struct _auth_obj ao;
+
+ if ((fn = make_path(tokenid, userid)) == NULL) {
+ ret.err = "authfile path impossible to build";
+ return ret;
+ }
+ fp = fopen(fn, "r");
+ free(fn);
+ if (fp) {
+ struct stat st;
+ int fd = fileno(fp);
+
+ if (fstat(fd, &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)) {
+ char *p;
+
+ p = &buf[strlen(buf) - 1];
+ while (*p == '\n' || *p == '\r') *p-- = '\0';
+ wtokenid = strtok(buf, ":");
+ wuserid = strtok(NULL, ":");
+ wnonce = strtok(NULL, ":");
+ hablob = strtok(NULL, ":");