File: t_pw.c

package info (click to toggle)
libsrp-dev 1.1-1
  • links: PTS
  • area: main
  • in suites: potato, slink
  • size: 316 kB
  • ctags: 269
  • sloc: ansic: 2,627; sh: 274; makefile: 53
file content (352 lines) | stat: -rw-r--r-- 8,648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (c) 1997 Stanford University
 *
 * The use of this software for revenue-generating purposes may require a
 * license from the owners of the underlying intellectual property.
 * Specifically, the SRP protocol may not be used for revenue-generating
 * purposes without a license.
 *
 * Within that constraint, permission to use, copy, modify, and distribute
 * this software and its documentation for any purpose is hereby granted
 * without fee, provided that the above copyright notices and this permission
 * notice appear in all copies of the software and related documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <pwd.h>
#include <sys/stat.h>

#include "t_defines.h"
#include "t_pwd.h"
#include "t_read.h"
#include "t_sha.h"

struct t_pw *
t_openpw(fp)
     FILE * fp;
{
  struct t_pw * tpw;
  char close_flag = 0;

  if(fp == NULL) {
    if((fp = fopen(DEFAULT_PASSWD, "r")) == NULL)
      return NULL;
    close_flag = 1;
  }
  else
    close_flag = 0;

  if((tpw = malloc(sizeof(struct t_pw))) == NULL)
    return NULL;
  tpw->instream = fp;
  tpw->close_on_exit = close_flag;

  return tpw;
}

struct t_pw *
t_openpwbyname(pwname)
     t_const char * pwname;
{
  FILE * fp;
  struct t_pw * t;

  if(pwname == NULL)
    return t_openpw(NULL);

  if((fp = fopen(pwname, "r")) == NULL)
    return NULL;

  t = t_openpw(fp);
  t->close_on_exit = 1;
  return t;
}

void
t_closepw(tpw)
     struct t_pw * tpw;
{
  if(tpw->close_on_exit)
    fclose(tpw->instream);
  free(tpw);
}

void
t_rewindpw(tpw)
     struct t_pw * tpw;
{
  rewind(tpw->instream);
}

struct t_pwent *
t_getpwent(tpw)
     struct t_pw * tpw;
{
  char indexbuf[16];
  char passbuf[MAXB64PARAMLEN];
  char saltstr[MAXB64SALTLEN];

  while(1) {
    if(t_nextfield(tpw->instream, tpw->userbuf, MAXUSERLEN) > 0 &&
       t_nextfield(tpw->instream, passbuf, MAXB64PARAMLEN) > 0 &&
       (tpw->pebuf.password.len = t_fromb64(tpw->pwbuf, passbuf)) > 0 &&
       t_nextfield(tpw->instream, saltstr, MAXB64SALTLEN) > 0 &&
       (tpw->pebuf.salt.len = t_fromb64(tpw->saltbuf, saltstr)) > 0 &&
       t_nextfield(tpw->instream, indexbuf, 16) > 0 &&
       (tpw->pebuf.index = atoi(indexbuf)) > 0) {
      tpw->pebuf.name = tpw->userbuf;
      tpw->pebuf.password.data = tpw->pwbuf;
      tpw->pebuf.salt.data = tpw->saltbuf;
      t_nextline(tpw->instream);
      return &tpw->pebuf;
    }
    else if(t_nextline(tpw->instream) < 0)
      return NULL;
  }
}

struct t_pwent *
t_getpwbyname(tpw, user)
     struct t_pw * tpw;
     t_const char * user;
{
  char indexbuf[16];
  char passbuf[MAXB64PARAMLEN];
  char saltstr[MAXB64SALTLEN];
  char username[MAXUSERLEN];

  t_rewindpw(tpw);

  while(t_nextfield(tpw->instream, username, MAXUSERLEN) > 0) {
    if(strcmp(user, username) == 0)
      if(t_nextfield(tpw->instream, passbuf, MAXB64PARAMLEN) > 0 &&
	 (tpw->pebuf.password.len = t_fromb64(tpw->pwbuf, passbuf)) > 0 &&
	 t_nextfield(tpw->instream, saltstr, MAXB64SALTLEN) > 0 &&
	 (tpw->pebuf.salt.len = t_fromb64(tpw->saltbuf, saltstr)) > 0 &&
	 t_nextfield(tpw->instream, indexbuf, 16) > 0 &&
	 (tpw->pebuf.index = atoi(indexbuf)) > 0) {
	strcpy(tpw->userbuf, username);
	tpw->pebuf.name = tpw->userbuf;
	tpw->pebuf.password.data = tpw->pwbuf;
	tpw->pebuf.salt.data = tpw->saltbuf;
	t_nextline(tpw->instream);
	return &tpw->pebuf;
      }
    if(t_nextline(tpw->instream) < 0)
      return NULL;
  }
  return NULL;
}

struct t_pwent *
t_makepwent(tpw, user, pass, salt, confent)
     struct t_pw * tpw;
     t_const char * user;
     t_const char * pass;
     t_const struct t_num * salt;
     t_const struct t_confent * confent;
{
  BigInteger x, v, n, g;
  char hexbuf[MAXHEXPARAMLEN];
  unsigned char dig[SHA_DIGESTSIZE];
  SHA1_CTX ctxt;

  tpw->pebuf.name = tpw->userbuf;
  tpw->pebuf.password.data = tpw->pwbuf;
  tpw->pebuf.salt.data = tpw->saltbuf;

  strncpy(tpw->pebuf.name, user, MAXUSERLEN);
  tpw->pebuf.index = confent->index;

  if(salt) {
    tpw->pebuf.salt.len = salt->len;
    memcpy(tpw->pebuf.salt.data, salt->data, salt->len);
  }
  else {
    memset(dig, 0, SALTLEN);		/* salt is 80 bits */
    tpw->pebuf.salt.len = SALTLEN;
    do {
      t_random(tpw->pebuf.salt.data, SALTLEN);
    } while(memcmp(tpw->pebuf.salt.data, dig, SALTLEN) == 0);
    if(tpw->pebuf.salt.data[0] == 0)
      tpw->pebuf.salt.data[0] = 0xff;
  }

  n = BigIntegerFromBytes(confent->modulus.data, confent->modulus.len);
  g = BigIntegerFromBytes(confent->generator.data, confent->generator.len);
  v = BigIntegerFromInt(0);

  SHA1Init(&ctxt);
  SHA1Update(&ctxt, user, strlen(user));
  SHA1Update(&ctxt, ":", 1);
  SHA1Update(&ctxt, pass, strlen(pass));
  SHA1Final(dig, &ctxt);

  SHA1Init(&ctxt);
  SHA1Update(&ctxt, tpw->pebuf.salt.data, tpw->pebuf.salt.len);
  SHA1Update(&ctxt, dig, sizeof(dig));
  SHA1Final(dig, &ctxt);

  /* x = H(s, H(u, ':', p)) */
  x = BigIntegerFromBytes(dig, sizeof(dig));

  BigIntegerModExp(v, g, x, n);
  tpw->pebuf.password.len = BigIntegerToBytes(v, tpw->pebuf.password.data);

  BigIntegerFree(v);
  BigIntegerFree(x);
  BigIntegerFree(g);
  BigIntegerFree(n);

  return &tpw->pebuf;
}

void
t_putpwent(ent, fp)
     t_const struct t_pwent * ent;
     FILE * fp;
{
  char strbuf[MAXB64PARAMLEN];
  char saltbuf[MAXB64SALTLEN];

  fprintf(fp, "%s:%s:%s:%d\n", ent->name,
	  t_tob64(strbuf, ent->password.data, ent->password.len),
	  t_tob64(saltbuf, ent->salt.data, ent->salt.len), ent->index);
}

static int
t_pwcopy(pwdest, pwsrc, diff)
     FILE * pwdest;
     FILE * pwsrc;
     struct t_pwent * diff;
{
  struct t_pw * src;
  struct t_pwent * ent;

  if((src = t_openpw(pwsrc)) == NULL)
    return -1;

  while((ent = t_getpwent(src)) != NULL)
    if(diff && strcmp(diff->name, ent->name) == 0) {
      t_putpwent(diff, pwdest);
      diff = NULL;
    }
    else
      t_putpwent(ent, pwdest);

  if(diff)
    t_putpwent(diff, pwdest);
}

int
t_changepw(pwname, diff)
     t_const char * pwname;
     t_const struct t_pwent * diff;
{
  char bakfile[256], bakfile2[256];
  struct stat st;
  FILE * passfp;
  FILE * bakfp;

  if(pwname == NULL)
    pwname = DEFAULT_PASSWD;
  sprintf(bakfile, "%s.bak", pwname);
  sprintf(bakfile2, "%s.sav", pwname);

  if((passfp = fopen(pwname, "r")) == NULL || fstat(fileno(passfp), &st) < 0)
    return -1;

  if((bakfp = fopen(bakfile2, "w")) == NULL &&
    (unlink(bakfile2) < 0 || (bakfp = fopen(bakfile2, "w")) == NULL))
    return -1;

  fchmod(fileno(bakfp), st.st_mode & 0777);

  t_pwcopy(bakfp, passfp, diff);

  fclose(bakfp);
  fclose(passfp);

  unlink(bakfile);
  link(pwname, bakfile);
  unlink(pwname);
  link(bakfile2, pwname);
  unlink(bakfile2);

  return 0;
}

int
t_verifypw(user, pass)
     t_const char * user;
     t_const char * pass;
{
  struct t_pw * tpw;
  struct t_pwent * ent;
  struct t_conf * tconf;
  struct t_confent * tcent;
  struct passwd * syspwent;
  struct t_pw temp_pw;
  struct t_pwent * testent;
  char pathname[256];
  int rval;

  tpw = t_openpw(NULL);
  if(tpw == NULL || (ent = t_getpwbyname(tpw, user)) == NULL) {
    if(tpw != NULL)
      t_closepw(tpw);

#ifdef USE_HOMEDIR
    syspwent = getpwnam(user);		/* Find user's homedir */
    if(syspwent == NULL)
      return -1;
    sprintf(pathname, "%s/.tpasswd", syspwent->pw_dir);
    tpw = t_openpwbyname(pathname);
    if(tpw == NULL)
      return -1;
    
    strcat(pathname, ".conf");		/* Look up entries */
    if((ent = t_getpwbyname(tpw, user)) == NULL ||
       (tconf = t_openconfbyname(pathname)) == NULL) {
      t_closepw(tpw);
      return -1;
    }
#else
    return -1;
#endif
  }
  else {
    if((tconf = t_openconf(NULL)) == NULL) {
      t_closepw(tpw);
      return -1;
    }
  }

  if((tcent = t_getconfbyindex(tconf, ent->index)) == NULL) {
    t_closepw(tpw);
    t_closeconf(tconf);
    return -1;
  }

  testent = t_makepwent(&temp_pw, user, pass, &ent->salt, tcent);

  if(ent->password.len == testent->password.len &&
     memcmp(ent->password.data, testent->password.data, ent->password.len) == 0)
    rval = 1;
  else
    rval = 0;

  t_closepw(tpw);
  t_closeconf(tconf);
  return rval;
}