-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathshadow.c
113 lines (89 loc) · 1.78 KB
/
shadow.c
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
/*
* SPDX-FileCopyrightText: 1989 - 1994, Julianne Frances Haugh
* SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
* SPDX-FileCopyrightText: 2003 - 2005, Tomasz Kłoczko
* SPDX-FileCopyrightText: 2009 , Nicolas François
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
/* Newer versions of Linux libc already have shadow support. */
#ifndef HAVE_GETSPNAM
#ident "$Id$"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "atoi/a2i/a2s.h"
#include "atoi/str2i/str2u.h"
#include "defines.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"
static FILE *shadow;
#define FIELDS 9
#define OFIELDS 5
/*
* setspent - initialize access to shadow text and DBM files
*/
void setspent (void)
{
if (NULL != shadow) {
rewind (shadow);
}else {
shadow = fopen (SHADOW_FILE, "re");
}
}
/*
* endspent - terminate access to shadow text and DBM files
*/
void endspent (void)
{
if (NULL != shadow) {
(void) fclose (shadow);
}
shadow = NULL;
}
/*
* fgetspent - get an entry from a /etc/shadow formatted stream
*/
struct spwd *fgetspent (FILE * fp)
{
char buf[BUFSIZ];
if (NULL == fp) {
return (0);
}
if (fgets (buf, sizeof buf, fp) != NULL)
{
stpsep(buf, "\n");
return sgetspent(buf);
}
return 0;
}
/*
* getspent - get a (struct spwd *) from the current shadow file
*/
struct spwd *getspent (void)
{
if (NULL == shadow) {
setspent ();
}
return (fgetspent (shadow));
}
/*
* getspnam - get a shadow entry by name
*/
struct spwd *getspnam (const char *name)
{
struct spwd *sp;
setspent ();
while ((sp = getspent ()) != NULL) {
if (streq(name, sp->sp_namp)) {
break;
}
}
endspent ();
return (sp);
}
#else
extern int ISO_C_forbids_an_empty_translation_unit;
#endif