122347Spst/* writerec.c: The __opiewriterec() library function.
222347Spst
329964Sache%%% copyright-cmetz-96
492906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
592906SmarkmThe Inner Net License Version 3 applies to this software.
622347SpstYou should have received a copy of the license with this software. If
722347Spstyou didn't get a copy, you may request one from <license@inner.net>.
822347Spst
922347Spst	History:
1022347Spst
1192906Smarkm	Modified by cmetz for OPIE 2.4. Check that seed and sequence number are
1292906Smarkm		valid.
1329964Sache	Modified by cmetz for OPIE 2.31. Removed active attack protection
1429964Sache		support. Fixed passwd bug.
1522347Spst	Created by cmetz for OPIE 2.3 from passwd.c.
16148506Sache
17148506Sache$FreeBSD$
1822347Spst*/
1922347Spst#include "opie_cfg.h"
2022347Spst
2122347Spst#include <stdio.h>
2222347Spst#if TM_IN_SYS_TIME
2322347Spst#include <sys/time.h>
2422347Spst#else /* TM_IN_SYS_TIME */
2522347Spst#include <time.h>
2622347Spst#endif /* TM_IN_SYS_TIME */
2722347Spst#include <sys/types.h>
2822347Spst#if HAVE_UNISTD_H
2922347Spst#include <unistd.h>
3022347Spst#endif /* HAVE_UNISTD_H */
3122347Spst#if HAVE_STRING_H
3222347Spst#include <string.h>
3322347Spst#endif /* HAVE_STRING_H */
3422347Spst#if HAVE_STDLIB_H
3522347Spst#include <stdlib.h>
3622347Spst#endif /* HAVE_STDLIB_H */
3792906Smarkm#include <ctype.h>
3822347Spst#include "opie.h"
3922347Spst
4022347Spstchar *__opienone = "****************";
4122347Spst
4222347Spstint __opiewriterec FUNCTION((opie), struct opie *opie)
4322347Spst{
4422347Spst  char buf[17], buf2[64];
4522347Spst  time_t now;
4622347Spst  FILE *f, *f2 = NULL;
4722347Spst  int i = 0;
4892906Smarkm  char *c;
4922347Spst
5022347Spst  time(&now);
5122347Spst  if (strftime(buf2, sizeof(buf2), " %b %d,%Y %T", localtime(&now)) < 1)
5222347Spst    return -1;
5322347Spst
5422347Spst  if (!(opie->opie_flags & __OPIE_FLAGS_READ)) {
5522347Spst    struct opie opie2;
5622347Spst    i = opielookup(&opie2, opie->opie_principal);
5729964Sache    opie->opie_flags = opie2.opie_flags;
5829964Sache    opie->opie_recstart = opie2.opie_recstart;
5922347Spst  }
6092906Smarkm
6192906Smarkm  for (c = opie->opie_seed; *c; c++)
6292906Smarkm    if (!isalnum(*c))
6392906Smarkm      return -1;
6492906Smarkm
6592906Smarkm  if ((opie->opie_n < 0) || (opie->opie_n > 9999))
6692906Smarkm      return -1;
6792906Smarkm
6822347Spst  switch(i) {
6922347Spst  case 0:
70148506Sache    if (!(f = __opieopen(KEY_FILE, 1, 0600)))
7122347Spst      return -1;
7222347Spst    if (fseek(f, opie->opie_recstart, SEEK_SET))
7322347Spst      return -1;
7422347Spst    break;
7522347Spst  case 1:
76148506Sache    if (!(f = __opieopen(KEY_FILE, 2, 0600)))
7722347Spst      return -1;
7822347Spst    break;
7922347Spst  default:
8022347Spst    return -1;
8122347Spst  }
8222347Spst
8322347Spst  if (fprintf(f, "%s %04d %-16s %s %-21s\n", opie->opie_principal, opie->opie_n, opie->opie_seed, opie->opie_val ? opie->opie_val : __opienone, buf2) < 1)
8422347Spst    return -1;
8522347Spst
8622347Spst  fclose(f);
8722347Spst
8822347Spst  return 0;
8922347Spst}
90