xref: /freebsd/include/pwd.h (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef _PWD_H_
38 #define	_PWD_H_
39 
40 #include <sys/cdefs.h>
41 #include <sys/_types.h>
42 
43 #ifndef _GID_T_DECLARED
44 typedef	__gid_t		gid_t;
45 #define	_GID_T_DECLARED
46 #endif
47 
48 #ifndef _TIME_T_DECLARED
49 typedef	__time_t	time_t;
50 #define	_TIME_T_DECLARED
51 #endif
52 
53 #ifndef _UID_T_DECLARED
54 typedef	__uid_t		uid_t;
55 #define	_UID_T_DECLARED
56 #endif
57 
58 #ifndef _SIZE_T_DECLARED
59 typedef __size_t	size_t;
60 #define _SIZE_T_DECLARED
61 #endif
62 
63 #define _PATH_PWD		"/etc"
64 #define	_PATH_PASSWD		"/etc/passwd"
65 #define	_PASSWD			"passwd"
66 #define	_PATH_MASTERPASSWD	"/etc/master.passwd"
67 #define	_MASTERPASSWD		"master.passwd"
68 
69 #define	_PATH_MP_DB		"/etc/pwd.db"
70 #define	_MP_DB			"pwd.db"
71 #define	_PATH_SMP_DB		"/etc/spwd.db"
72 #define	_SMP_DB			"spwd.db"
73 
74 #define	_PATH_PWD_MKDB		"/usr/sbin/pwd_mkdb"
75 
76 /* Historically, the keys in _PATH_MP_DB/_PATH_SMP_DB had the format
77  * `1 octet tag | key', where the tag is one of the _PW_KEY* values
78  * listed below.  These values happen to be ASCII digits.  Starting
79  * with FreeBSD 5.1, the tag is now still a single octet, but the
80  * upper 4 bits are interpreted as a version.  Pre-FreeBSD 5.1 format
81  * entries are version `3' -- this conveniently results in the same
82  * key values as before.  The new, architecture-independent entries
83  * are version `4'.
84  * As it happens, some applications read the database directly.
85  * (Bad app, no cookie!)  Thus, we leave the _PW_KEY* symbols at their
86  * old pre-FreeBSD 5.1 values so these apps still work.  Consequently
87  * we have to muck around a bit more to get the correct, versioned
88  * tag, and that is what the _PW_VERSIONED macro is about.
89  */
90 
91 #define _PW_VERSION_MASK	'\xF0'
92 #define _PW_VERSIONED(x, v)	((unsigned char)(((x) & 0xCF) | ((v)<<4)))
93 
94 #define	_PW_KEYBYNAME		'\x31'	/* stored by name */
95 #define	_PW_KEYBYNUM		'\x32'	/* stored by entry in the "file" */
96 #define	_PW_KEYBYUID		'\x33'	/* stored by uid */
97 #define _PW_KEYYPENABLED	'\x34'	/* YP is enabled */
98 #define	_PW_KEYYPBYNUM		'\x35'	/* special +@netgroup entries */
99 
100 /* The database also contains a key to indicate the format version of
101  * the entries therein.  There may be other, older versioned entries
102  * as well.
103  */
104 #define _PWD_VERSION_KEY	"\xFF" "VERSION"
105 #define _PWD_CURRENT_VERSION	'\x04'
106 
107 #define	_PASSWORD_EFMT1		'_'	/* extended encryption format */
108 
109 #define	_PASSWORD_LEN		128	/* max length, not counting NULL */
110 
111 struct passwd {
112 	char	*pw_name;		/* user name */
113 	char	*pw_passwd;		/* encrypted password */
114 	uid_t	pw_uid;			/* user uid */
115 	gid_t	pw_gid;			/* user gid */
116 	time_t	pw_change;		/* password change time */
117 	char	*pw_class;		/* user access class */
118 	char	*pw_gecos;		/* Honeywell login info */
119 	char	*pw_dir;		/* home directory */
120 	char	*pw_shell;		/* default shell */
121 	time_t	pw_expire;		/* account expiration */
122 	int	pw_fields;		/* internal: fields filled in */
123 };
124 
125 /* Mapping from fields to bits for pw_fields. */
126 #define _PWF(x)		(1 << x)
127 #define _PWF_NAME	_PWF(0)
128 #define _PWF_PASSWD	_PWF(1)
129 #define _PWF_UID	_PWF(2)
130 #define _PWF_GID	_PWF(3)
131 #define _PWF_CHANGE	_PWF(4)
132 #define _PWF_CLASS	_PWF(5)
133 #define _PWF_GECOS	_PWF(6)
134 #define _PWF_DIR	_PWF(7)
135 #define _PWF_SHELL	_PWF(8)
136 #define _PWF_EXPIRE	_PWF(9)
137 
138 /* XXX These flags are bogus.  With nsswitch, there are many
139  * possible sources and they cannot be represented in a small integer.
140  */
141 #define _PWF_SOURCE	0x3000
142 #define _PWF_FILES	0x1000
143 #define _PWF_NIS	0x2000
144 #define _PWF_HESIOD	0x3000
145 
146 __BEGIN_DECLS
147 struct passwd	*getpwnam(const char *);
148 struct passwd	*getpwuid(uid_t);
149 
150 #if __XSI_VISIBLE >= 500
151 void		 endpwent(void);
152 struct passwd	*getpwent(void);
153 void		 setpwent(void);
154 #endif
155 
156 #if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE >= 500
157 int		 getpwnam_r(const char *, struct passwd *, char *, size_t,
158 		    struct passwd **);
159 int		 getpwuid_r(uid_t, struct passwd *, char *, size_t,
160 		    struct passwd **);
161 #endif
162 
163 #if __BSD_VISIBLE
164 int		 getpwent_r(struct passwd *, char *, size_t, struct passwd **);
165 int		 setpassent(int);
166 const char	*user_from_uid(uid_t, int);
167 int		 uid_from_user(const char *, uid_t *);
168 int		 pwcache_userdb(int (*)(int), void (*)(void),
169 		    struct passwd * (*)(const char *),
170 		    struct passwd * (*)(uid_t));
171 #endif
172 __END_DECLS
173 
174 #endif /* !_PWD_H_ */
175