Commit Graph

101 Commits

Author SHA1 Message Date
Alan Somers
9ad1d357e0 fix integer underflow in getgrnam_r and getpwnam_r
Sometimes nscd(8) will return a 1-byte buffer for a nonexistent entry. This
triggered an integer underflow in grp_unmarshal_func, causing getgrnam_r to
return ERANGE instead of 0.

Fix the user's buffer size check, and add a correct check for a too-small
nscd buffer.

PR:		248932
Event:		September 2020 Bugathon
Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	Axcient
Differential Revision: https://reviews.freebsd.org/D26204
2020-09-19 19:08:27 +00:00
Brooks Davis
6671366a55 Fix -Wvoid-pointer-to-enum-cast warnings.
This pattern is used in callbacks with void * data arguments and seems
both relatively uncommon and relatively harmless.  Silence the warning
by casting through uintptr_t.

This warning is on by default in Clang 11.

Reviewed by:	arichardson
Obtained from:	CheriBSD (partial)
MFC after:	1 week
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D24425
2020-04-15 18:15:58 +00:00
Ian Lepore
34e9190d82 Make pw_scan(3) more compatible with getpwent(3) et. al. when processing
data from /etc/passwd rather than /etc/master.passwd.

The libc getpwent(3) and related functions automatically read master.passwd
when run by root, or passwd when run by a non-root user.  When run by non-
root, getpwent() copes with the missing data by setting the corresponding
fields in the passwd struct to known values (zeroes for numbers, or a
pointer to an empty string for literals).  When libutil's pw_scan(3) was
used to parse a line without the root-accessible data, it was leaving
garbage in the corresponding fields.

These changes rename the static pw_init() function used by getpwent() and
friends to __pw_initpwd(), and move it into pw_scan.c so that common init
code can be shared between libc and libutil.  pw_scan(3) now calls
__pw_initpwd() before __pw_scan(), just like the getpwent() family does, so
that reading an arbitrary passwd file in either format and parsing it with
pw_scan(3) returns the same results as getpwent(3) would.

This also adds a new pw_initpwd(3) function to libutil, so that code which
creates passwd structs from scratch in some manner that doesn't involve
pw_scan() can initialize the struct to the values expected by lots of
existing code, which doesn't expect to encounter NULL pointers or garbage
values in some fields.
2018-07-26 18:34:38 +00:00
Pedro F. Giffuni
d915a14ef0 libc: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-25 17:12:48 +00:00
Pedro F. Giffuni
bf51882a09 libc: make some more use of the nitems() macro.
We have an nitems() macro in the <sys/param.h> header that is
convenient to re-use as it makes things easier to read.
Given that it is available already without adding additional
headers and other parts of libc already use it, extend a bit
more its use.
2016-04-16 17:52:00 +00:00
Enji Cooper
3e973f9b6d Fix compiling with gcc [4.2.1] after r287797 when MK_HESOID == no and
MK_NIS == no by converting `i` back to an int, and instead cast the loop
comparison to `int`

The loop comparison is iterating the len(ns_dtab)-1, because
the last element is the sentinel tuple { NULL, NULL, NULL, }, so when
both HESOID and NIS are off, len(ns_dtab)-1 == 1 - 1 == 0, and the loop
is skipped because the expression is tautologically false

While here, convert `(sizeof(x) / sizeof(x[0]))` to `nitems(x)`

Tested with: clang 3.7.0, gcc 4.2.1, and gcc 4.9.4 [*] with MK_NIS={no,yes}
             and by running bash -lc 'id -u && id -g && id'

* gcc 4.9.4 needs another patch in order for the compile to succeed
  with -Werror with lib/libc/gen/getgrent.c

Reported by: jhibbits
2015-10-25 07:42:56 +00:00
Craig Rodrigues
e263ec16a9 Use unsigned variables in a few places.
Eliminates gcc 4.9 warnings.
2015-09-14 18:59:01 +00:00
Pedro F. Giffuni
b3d078e666 compat_passwd(): yet another uninitialized access to stayopen.
CID:	1018731
2015-03-06 22:22:57 +00:00
Pedro F. Giffuni
a69a20fa13 Fix small memleaks in nis_passwd() and nis_group().
These only occur upon error.

Code Review:	https://reviews.freebsd.org/D1849
Reviewed by:	delphij

CID:	1016715
CID:	1016717
2015-02-20 01:02:32 +00:00
Pedro F. Giffuni
09cc2762fa More tidy-ups on uninitialized scalar variable
As a followup to r278363, there is one more case where
stayopen can be accessed uninitialized, but even after
swapping arguments, access is possible in some other
cases so prevent it completely by initializing stayopen.

CID:	1018729
CID:	1018732
2015-02-15 16:50:21 +00:00
Pedro F. Giffuni
eb77fbdcec Protect uninitialized scalar variable from being accessed
In a couple of cases a variable "stayopen" can be checked
unitialized. This is of no danger as the complementary
condition is false but prevent the access by switching
the checks.

CID:	1018729
CID:	1018732
2015-02-07 19:51:34 +00:00
Hajimu UMEMOTO
06a99fe36f - Extend the nsswitch to support Services, Protocols and Rpc
databases.
- Make nsswitch support caching.

Submitted by:	Michael Bushkov <bushman__at__rsu.ru>
Sponsored by:	Google Summer of Code 2005
2006-04-28 12:03:38 +00:00
Tim Kientzle
5edb850fbe POSIX prohibits any library function from setting errno to 0.
Correct my previous commit and add a comment to the manpage
indicating that the user must set errno to 0 if they wish to
distinguish "no such user" from "error".

Pointed out by: Jacques Vidrine (nectar@)
2004-05-17 18:27:05 +00:00
Tim Kientzle
b4cc8687ad If getpwent/getpwuid/getpwnam return NULL, they must also set errno. 2004-05-17 06:17:59 +00:00
Jonathan Chen
dbb4b1c83d Fix a bug that could result in getpw*() incorrectly returning NULL when NIS
adjunct maps are used.  One symtom of this bug is sshd saying:
	login_get_lastlog: Cannot find account for uid X
when logging in.  The problem here is caused by an incorrect reuse of the rv
variable when previous values are needed later.
2004-04-21 21:15:08 +00:00
Mark Murray
16fc3635f7 Make NULL a (void*)0 whereever possible, and fix the warnings(-Werror)
that this provokes. "Wherever possible" means "In the kernel OR NOT
C++" (implying C).

There are places where (void *) pointers are not valid, such as for
function pointers, but in the special case of (void *)0, agreement
settles on it being OK.

Most of the fixes were NULL where an integer zero was needed; many
of the fixes were NULL where ascii <nul> ('\0') was needed, and a
few were just "other".

Tested on: i386 sparc64
2004-03-05 08:10:19 +00:00
Jacques Vidrine
3beb62d3d6 Kill whitespace at end of lines. 2004-01-12 13:33:24 +00:00
Jacques Vidrine
2734a36f06 Fix a bug that could result in a null pointer dereference in
getpwent(3) or getpwuid(3) when using NIS adjunct maps.  The bug was
present in the internal `nis_passwd' function.  The lookup in the
adjunct map used the name passed into `nis_passwd', however no name
was of course supplied by getpwent or getpwuid.  Correctly use the
name from the `struct pwd' that was found instead.

PR:		bin/59962
Submitted by:	Gabriel Gomez <ggomez@fing.edu.uy>
2004-01-12 13:29:54 +00:00
Bill Paul
0287aa1cee Revert to using yp_order() to probe for master.paswd.by* maps and
don't probe the server at all for passwd.by* maps. This fixes
interoperability with the Services For UNIX NIS server (which is
really a front end to Captive^WActiveDirectory). This server
incorrectly returns success for all YPPROC_MASTER requests,
even for maps that don't exist, which makes it impossible to
(ab)use it to probe for the existence of the master.passwd.by*
maps.

This is a little kludgey, but basically restores the original
behavior of getpwent.c as it is in -stable, and works around both
the lack of YPPROC_ORDER on NIS+ servers as well as the broken
YPPROC_MASTER on Services For UNIX servers.
2003-07-18 23:51:15 +00:00
John W. De Boskey
788940b418 fix NIS+ YP compat mode
PR:		bin/52792
Submitted by:	TOMITA Yoshinori <yoshint@flab.fujitsu.co.jp>
2003-06-27 03:37:44 +00:00
Jacques Vidrine
d05090827f Back out the `hiding' of strlcpy and strlcat. Several people
vocally objected to this safety belt.
2003-05-01 19:03:14 +00:00
Jacques Vidrine
5723e501ab `Hide' strlcpy and strlcat (using the namespace.h / __weak_reference
technique) so that we don't wind up calling into an application's
version if the application defines them.

Inspired by:	qpopper's interfering and buggy version of strlcpy
2003-04-29 21:13:50 +00:00
Jacques Vidrine
c14d379de1 When using `compat' mode, be sure to re-dispatch setpwent, endpwent,
setgrent, and endgrent also.  (The previous NSS implementation used to
simply twiddle the internal data of the various modules directly.)

A symptom (group list set incorrectly in sshd) was
Reported by:	Glenn Johnson <gjohnson@srrc.ars.usda.gov>

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-25 18:25:19 +00:00
Jacques Vidrine
e2527daf1a In compat mode, we `redispatch' the lookup. It is probably a good
idea to re-initialize `struct passwd', because e.g. pw_class might
get set by one module, but not by another.  Add another call to the
internal pwd_init function to accomplish this.

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-24 23:56:58 +00:00
Jacques Vidrine
d3fc864f93 Don't try to access the NIS `master' maps unless we have superuser
privileges.  To do so may cause the NIS server to log spurious and
annoying `access denied' messages.

Reported by:	Philip Paeps <philip@paeps.cx>
Sponsored by:	DARPA, Network Associates Laboratories
2003-04-21 13:57:58 +00:00
Jacques Vidrine
171614bfd6 Follow-up to revision 1.74: Using the result buffer to store our empty
string was an incredibly dumb idea (of course it will be changed by an
NSS module on success!).  Use a static empty string instead.

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 22:07:30 +00:00
Jacques Vidrine
a9ceaa9dc8 Follow-up to revision 1.73: set _PWF_FILES when `compat' source is used
but user is found in local file.

Reported by:	Shizuka Kudo <shizukakudo_99@yahoo.com>
Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 21:13:35 +00:00
Jacques Vidrine
4c3c0fecae Don't use `memset' to initialize a struct passwd. A module
may not fill in all fields, and in the case of string fields, this could
cause trouble for applications.  (The only likely example is `pw_class',
because this field is not used by all modules in all cases.)

Move initialization of struct passwd from module-specific code to the
dispatch code.

The problem of a NULL pw_class was
Noticed by:	Philip Paeps <philip@paeps.cx>
                    and the c^Htrusty ssh(1) command.
Déjà vu by:	getpwent.c revision 1.56

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 17:27:05 +00:00
Jacques Vidrine
71cc8f0764 Correctly set _PWF_FILES in pw_fields when appropriate.
(_PWF_NIS and _PWF_HESIOD were already being set.)

Reported by:	Shizuka Kudo <shizukakudo_99@yahoo.com>
Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 16:24:25 +00:00
Jacques Vidrine
b4603f3dd4 Revert the definitions of _PW_KEY* to their previous values. There is
at least one consumer outside of libc and pwd_mkdb.
Adjust the versioning in libc and pwd_mkdb accordingly.

named was the application affected, and that fact was first
Reported by:	Zherdev Anatoly <tolyar@mx.ru>

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 14:11:17 +00:00
Jacques Vidrine
1b467db206 The default if nsswitch.conf(5) is not present is supposed to be the
hated `compat' source, not `files'.

Reported by:	Philip Paeps <philip@paeps.cx>
Sponsored by:	DARPA, Network Associates Laboratories
2003-04-18 01:00:37 +00:00
Jacques Vidrine
05f98035ee = Implement thread-safe versions of the getpwent(3) and getgrent(3)
family of functions using the new nsdispatch(3) core.  Remove
  arbitrary size limits when using the thread-safe versions.

= Re-implement the traditional getpwent(3)/getgrent(3) functions on
  top of the thread-safe versions.

= Update the on-disk format of the hashed version of the passwd(5)
  databases to allow for versioned entries.  The legacy version is
  `3'.  (Don't ask.)

= Add support for version `4' entries in the passwd(5) database.
  Entries in this format are identical to version 3 entries except
  that all integers are stored as 32-bit integers in network byte
  order (big endian).

= pwd_mkdb is updated to generate both version 3 and version 4
  entries.

Sponsored by:	DARPA, Network Associates Laboratories
2003-04-17 14:15:26 +00:00
Jacques Vidrine
2bbd7cf820 Eliminate 19 warnings in libc (at level WARNS=2) of the
`implicit declaration of function' variety.
2003-02-27 13:40:01 +00:00
Jacques Vidrine
e0554a531f Eliminate 61 warnings emitted at WARNS=2 (leaving 53 to go).
Only warnings that could be fixed without changing the generated object
code and without restructuring the source code have been handled.

Reviewed by:	/sbin/md5
2003-02-16 17:29:11 +00:00
Dag-Erling Smørgrav
719137a64d Fix bug that causes passwd and friends to fail when the user has a '+' in
their passwd file for NIS because _PWF_SOURCE is not set.

Submitted by:	amigus (perforce change 10969)
2002-05-07 23:26:00 +00:00
Dag-Erling Smørgrav
00d9b6b8d6 Missed a spot in previous commit.
Sponsored by:	DARPA, NAI Labs
2002-04-15 00:21:25 +00:00
Dag-Erling Smørgrav
e479b601dd (ab)use unused bits in the pw_fields member of struct passwd to record
the source of the data contained in the structure.

Sponsored by:	DARPA, NAI Labs
2002-04-14 22:24:55 +00:00
David E. O'Brien
b231cb3945 * Remove __P and convert to ANSI prototypes.
* Remove 'register'.  (some functions had 7+ register functions...)
* Fix SCM ID's.
2002-02-01 01:32:19 +00:00
Bruce Evans
56bcbf00e2 Fixed world breakage due to missing include of <sys/cdefs.h> in previous
commit.

Fixed related style bugs:
basename.c: misplaced '#if 0'
dirname.c: misplaced '#if 0'
getgrent.c: missing '#if 0', and tab lossage in vendor id (the previous
            commit fixed the complete corruption of the vendor id but
	    lost a tab)
getpwent.c: missing '#if 0'
2002-01-31 12:58:36 +00:00
David E. O'Brien
135b57f906 Fix FreeBSD IDs. 2002-01-30 21:36:57 +00:00
Peter Wemm
9213df7cda Explicitly use int32_t for on-disk records for pw_change and pw_expire,
since that is what we use now and this insulates us from any time_t
tweaks here.  We can define a record format that uses 64 bit times if/when
we need to.
2001-10-27 02:13:41 +00:00
David Malone
75328eca8d If the username we are trying to look up in the db files won't fit into
the buffer then act like it doesn't exist. The buffer is always big enough
for any valid username.

PR:		27860
Reviewed by:	nectar
2001-06-18 16:09:24 +00:00
Daniel Eischen
d201fe46e3 Remove _THREAD_SAFE and make libc thread-safe by default by
adding (weak definitions to) stubs for some of the pthread
functions.  If the threads library is linked in, the real
pthread functions will pulled in.

Use the following convention for system calls wrapped by the
threads library:
	__sys_foo - actual system call
	_foo - weak definition to __sys_foo
	foo - weak definition to __sys_foo

Change all libc uses of system calls wrapped by the threads
library from foo to _foo.  In order to define the prototypes
for _foo(), we introduce namespace.h and un-namespace.h
(suggested by bde).  All files that need to reference these
system calls, should include namespace.h before any standard
includes, then include un-namespace.h after the standard
includes and before any local includes.  <db.h> is an exception
and shouldn't be included in between namespace.h and
un-namespace.h  namespace.h will define foo to _foo, and
un-namespace.h will undefine foo.

Try to eliminate some of the recursive calls to MT-safe
functions in libc/stdio in preparation for adding a mutex
to FILE.  We have recursive mutexes, but would like to avoid
using them if possible.

Remove uneeded includes of <errno.h> from a few files.

Add $FreeBSD$ to a few files in order to pass commitprep.

Approved by:	-arch
2001-01-24 13:01:12 +00:00
Jacques Vidrine
0634de0c1f Fix bug introduced in previous commit: users obtained via compat mode
had uid, gid set to 0 if not otherwise specified!

Submitted by:	eivind
2000-11-13 21:22:50 +00:00
Jacques Vidrine
61a33b7ca5 Fix passwd entry `prototypes' in compat mode. I broke this in revision
1.55 when importing nsswitch from NetBSD.

Reported by:	Naoki Kobayashi <shibata@geo.titech.ac.jp>
2000-11-10 19:11:14 +00:00
Jacques Vidrine
580ef74769 Explicitly initialize _pw_passwd. 2000-10-27 18:27:07 +00:00
Jacques Vidrine
248aee623c Add nsswitch support. By creating an /etc/nsswitch.conf file, you can
configure FreeBSD so that various databases such as passwd and group can be
looked up using flat files, NIS, or Hesiod.

= Hesiod has been added to libc (see hesiod(3)).

= A library routine for parsing nsswitch.conf and invoking callback
  functions as specified has been added to libc (see nsdispatch(3)).

= The following C library functions have been modified to use nsdispatch:
    . getgrent, getgrnam, getgrgid
    . getpwent, getpwnam, getpwuid
    . getusershell
    . getaddrinfo
    . gethostbyname, gethostbyname2, gethostbyaddr
    . getnetbyname, getnetbyaddr
    . getipnodebyname, getipnodebyaddr, getnodebyname, getnodebyaddr

= host.conf has been removed from src/etc.  rc.network has been modified
  to warn that host.conf is no longer used at boot time.  In addition, if
  there is a host.conf but no nsswitch.conf, the latter is created at boot
  time from the former.

Obtained from:	NetBSD
2000-09-06 18:16:48 +00:00
Kris Kennaway
8f6d480067 sprintf -> snprintf paranoia 2000-08-04 10:23:42 +00:00
Andrey A. Chernov
0200080b60 YP/NIS code: remove unnecessary endgrent() calls which can cause fail on
next try over chroot (descriptor closed). getgrnam() used already handles
endgrent() properly and honors _gr_stayopen. Automatically call
setgroupent(1) when _pw_stayopen is set (for YP/NIS code).
1999-10-16 12:31:49 +00:00
Andrey A. Chernov
8b7690ea77 Fix longstanding bug "unused stayopen" introduced in rev1.11
PR:		14201
1999-10-16 11:50:37 +00:00