]> www.average.org Git - pam_pcsc_cr.git/blob - authfile.c
f7daa60eec5f0c3de4b8596b3bbf481a610f7c89
[pam_pcsc_cr.git] / authfile.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <pwd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <alloca.h>
13 #include "authobj.h"
14 #include "authfile.h"
15
16 /*
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
26  * string.
27  */
28
29 static char *template = "~/.pam_cr/auth";
30
31 void authfile_template(char *str)
32 {
33         template = str;
34 }
35
36 static int path_size(const char *tokenid, const char *userid)
37 {
38         const char *usub;
39         char *p, *q;
40         struct passwd *pw;
41
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;
46         if (p == template) {
47                 pw = getpwnam(userid);
48                 if (!pw) return 0;
49                 usub = pw->pw_dir;
50         } else {
51                 usub = userid;
52         }
53         return strlen(template) + p?strlen(usub):0 + q?strlen(tokenid):0 + 1;
54 }
55
56 static void
57 make_path(char * const path, const char *tokenid, const char *userid)
58 {
59         const char *usub;
60         char *p, *q;
61         struct passwd *pw;
62
63         path[0] = '\0';
64         if (template[0] == '~') {
65                 pw = getpwnam(userid);
66                 if (!pw) return;
67                 usub = pw->pw_dir;
68         } else {
69                 usub = userid;
70         }
71         q = path;
72         for (p = template; *p; p++) switch (*p) {
73         case '~':
74                 strcpy(q, usub);
75                 while (*q) q++;
76                 break;
77         case '?':
78                 strcpy(q, tokenid);
79                 while (*q) q++;
80                 break;
81         default:
82                 *q++ = *p;
83                 break;
84         }
85         *q = '\0';
86 }
87
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,
94                                                 const int csize))
95 {
96         struct _auth_obj ret = {0};
97         mode_t oldmask;
98         FILE *fp = NULL;
99         char *fn;
100         int fnl;
101         char *buf = NULL;
102         const char *wtokenid = "", *wuserid = NULL, *wnonce = NULL;
103         const char *hablob = NULL;
104         unsigned char *ablob = NULL;
105         int blobsize = 0;
106         char *newnonce;
107         int nonsize;
108         struct _auth_obj ao;
109
110         if ((fnl = path_size(tokenid, userid)) == 0) {
111                 ret.err = "authfile path impossible to build";
112                 return ret;
113         }
114         fn = alloca(fnl);
115         make_path(fn, tokenid, userid);
116         fp = fopen(fn, "r");
117         if (fp) {
118                 struct stat st;
119                 int fd = fileno(fp);
120
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)) {
125                         char *p;
126
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, ":");
133                 } else {
134                         ret.err = strerror(errno);
135                 }
136                 fclose(fp);
137         }
138         if (ret.err) return ret;
139
140         if (hablob) {
141                 int hlen = strlen(hablob);
142                 if (hlen % 32 != 0) {
143                         ret.err = "error: auth string has wrong length";
144                 } else if (hlen !=
145                                 strspn(hablob, "0123456789abcdefABCDEF")) {
146                         ret.err = "error: auth string not hexadecimal";
147                 } else {
148                         int i;
149
150                         blobsize = hlen/2;
151                         ablob = alloca(blobsize);
152                         for (i = 0; i < blobsize; i++)
153                                 sscanf(&hablob[i*2], "%2hhx", &ablob[i]);
154                 }
155         }
156         if (ret.err) return ret;
157
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);
164
165         ao = authobj(userid?userid:wuserid, password,
166                         wnonce, newnonce, secret, secsize,
167                         payload, paylsize, ablob, blobsize,
168                         fetch_key);
169
170         if (ao.err) {
171                 ret.err = ao.err;
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);
175                 return ret;
176         }
177
178         oldmask = umask(077);
179         if ((fp = fopen(fn, "w"))) {
180                 int i;
181
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);
188                 }
189                 fprintf(fp, "\n");
190                 if (fclose(fp) < 0) {
191                         ret.err = strerror(errno);
192                 }
193         } else {
194                 ret.err = strerror(errno);
195         }
196         (void)umask(oldmask);
197
198         if (!ret.err) {
199                 int bufsize = (wuserid?strlen(wuserid)+1:0) + ao.paylsize;
200                 if (bufsize) {
201                         if ((ret.buffer = malloc(bufsize)) == NULL) {
202                                 ret.err = "authfile malloc failed";
203                         } else {
204                                 unsigned char *p = ret.buffer;
205                                 if (wuserid) {
206                                         strcpy((char*)p, wuserid);
207                                         ret.data = p;
208                                         ret.datasize = strlen(wuserid)+1;
209                                         p += strlen(wuserid)+1;
210                                 }
211                                 if (ao.payload) {
212                                         memcpy(p, ao.payload, ao.paylsize);
213                                         ret.payload = p;
214                                         ret.paylsize = ao.paylsize;
215                                 }
216                         }
217                 }
218         }
219
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);
223         return ret;
224 }