Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/lookup_shadow.c
blob: 36558063925e00abde10b8d6df60f025684ea7a3 (plain)
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
/*
   lookup_shadow.c - simple lookup code for shadow entries

   Copyright (C) 2013 Arthur de Jong

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

#ifndef HAVE_SHADOW_H
/* dummy implementation that does nothing for FreeBSD */
int main(int argc,char *argv[])
{
  fprintf(stderr, "%s: shadow lookups unsupported\n", argv[0]);
  return 1;
}
#else /* HAVE_SHADOW_H */

#include <shadow.h>

static void print_shadow(struct spwd *result)
{
  printf("%s:%s:", result->sp_namp, result->sp_pwdp);
  if (result->sp_lstchg >= 0)
    printf("%d", (int)result->sp_lstchg);
  printf(":");
  if (result->sp_min >= 0)
    printf("%d", (int)result->sp_min);
  printf(":");
  if (result->sp_max >= 0)
    printf("%d", (int)result->sp_max);
  printf(":");
  if (result->sp_warn >= 0)
    printf("%d", (int)result->sp_warn);
  printf(":");
  if (result->sp_inact >= 0)
    printf("%d", (int)result->sp_inact);
  printf(":");
  if (result->sp_expire >= 0)
    printf("%d", (int)result->sp_expire);
  printf(":");
  if (result->sp_flag >= 0)
    printf("%x", (int)result->sp_flag);
  printf("\n");
}

/* the main program... */
int main(int argc,char *argv[])
{
  struct spwd *result;
  /* check arguments */
  if ((argc != 1) && (argc != 2))
  {
    fprintf(stderr, "Usage: %s [USERNAME]\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  /* start lookup */
  if (argc == 2)
  {
    /* get entry by name */
    errno = 0;
    result = getspnam(argv[1]);
    if (result == NULL)
      exit(EXIT_FAILURE);
    print_shadow(result);
  }
  else /* argc == 1 */
  {
    /* get all entries */
    setspent();
    while ((result = getspent()) != NULL)
      print_shadow(result);
    endspent();
  }
  return 0;
}

#endif /* HAVE_SHADOW_H */