password.c 1.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 *	BIRD -- Password handling
 *
 *	Copyright 1999 Pavel Machek <pavel@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "nest/bird.h"
#include "nest/password.h"
Martin Mareš's avatar
Martin Mareš committed
11
#include "lib/string.h"
12 13

struct password_item *last_password_item = NULL;
14 15 16 17 18 19 20 21 22 23 24 25 26 27

static int
password_goodness(struct password_item *i)
{
  if (i->from > now)
    return 0;
  if (i->to < now)
    return 0;
  if (i->passive < now)
    return 1;
  return 2;
}

struct password_item *
28
get_best_password(struct password_item *head, int flags UNUSED)
29 30 31 32 33 34 35 36 37 38
{
  int good = -1;
  struct password_item *best = NULL;

  while (head) {
    int cur = password_goodness(head);
    if (cur > good) {
      good = cur;
      best = head;
    }
39
    head=head->next;
40 41 42
  }
  return best;
}
43 44 45 46 47 48 49 50 51 52 53 54

void
password_strncpy(char *to, char *from, int len)
{
  int i;
  for (i=0; i<len; i++) {
    *to++ = *from;
    if (*from)
      from++;
  }
}

Pavel Machek's avatar
Pavel Machek committed
55 56 57
int
password_same(struct password_item *old, struct password_item *new)
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  for(;;)
    {
      if (old == new)
	return 1;
      if (!old || !new)
	return 0;
      if (old->from    != new->from    ||
	  old->to      != new->to      ||
	  old->passive != new->passive ||
	  old->id      != new->id      ||
	  strcmp(old->password, new->password))
	return 0;
      old = old->next;
      new = new->next;
    }
Pavel Machek's avatar
Pavel Machek committed
73
}