NetBSD Problem Report #1143
From gnats Tue Jun 20 05:02:48 1995
Received: from pain.lcs.mit.edu (pain.lcs.mit.edu [128.52.46.239]) by sun-lamp.pc.cs.cmu.edu (8.6.11/8.6.10) with ESMTP id FAA25605 for <gnats-bugs@sun-lamp.cs.berkeley.edu>; Tue, 20 Jun 1995 05:02:41 -0400
Message-Id: <199506120446.VAA04494@gandalf.sigmasoft.com>
Date: Sun, 11 Jun 1995 21:46:18 -0700
From: Thorsten Lockert <tholo@SigmaSoft.COM>
Reply-To: tholo@SigmaSoft.COM
To: gnats-bugs@NetBSD.ORG
Subject: ncheck(8) implementation
X-Send-Pr-Version: 3.2
>Number: 1143
>Category: bin
>Synopsis: ncheck(8) implementation
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: bin-bug-people
>State: closed
>Class: change-request
>Submitter-Id: net
>Arrival-Date: Tue Jun 20 05:05:17 +0000 1995
>Closed-Date: Wed Jul 12 04:48:28 +0000 1995
>Last-Modified:
>Originator: Thorsten Lockert
>Release: June 8, 1995
>Organization:
SigmaSoft, Th. Lockert
>Environment:
System: NetBSD gandalf.sigmasoft.com 1.0A NetBSD 1.0A (GANDALF) #1: Sun May 7 21:49:27 PDT 1995 tholo@gandalf.sigmasoft.com:/usr/src/sys/arch/i386/compile/GANDALF i386
>Description:
There are no ncheck(8) implementation on NetBSD. ncheck(8)
makes finding suid and sgid files somewhat faster than using
find to do the job.
However, ncheck(8) is mostly obsolete these days...
>How-To-Repeat:
Try to execute ncheck. :)
>Fix:
Install this in src/sbin/ncheck or src/usr.sbin/ncheck:
## This is a shell archive. Save it in a file, remove anything before
## this line, and then unpack it by entering "sh file". Note, it may
## create directories; files and directories will be owned by you and
## have default permissions.
##
## This archive contains:
##
## Makefile
## ncheck.8
## ncheck.c
##
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
X# $NetBSD$
X
XPROG= ncheck
XMAN= ncheck.8
X
X.include <bsd.prog.mk>
END-of-Makefile
echo x - ncheck.8
sed 's/^X//' >ncheck.8 << 'END-of-ncheck.8'
X.\" $NetBSD$
X.\"
X.\" Copyright (c) 1995 SigmaSoft, Th. Lockert
X.\" All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\" notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\" notice, this list of conditions and the following disclaimer in the
X.\" documentation and/or other materials provided with the distribution.
X.\" 3. All advertising materials mentioning features or use of this software
X.\" must display the following acknowledgement:
X.\" This product includes software developed by SigmaSoft, Th. Lockert,
X.\" for the NetBSD Project.
X.\" 4. The name of the author may not be used to endorse or promote products
X.\" derived from this software without specific prior written permission
X.\"
X.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
X.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
X.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
X.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
X.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
X.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
X.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X.\"
X.Dd June 11, 1995
X.Dt NCHECK 8
X.Os NetBSD 1.0a
X.Sh NAME
X.Nm ncheck
X.Nd generate names from inode-numbers
X.Sh SYNOPSIS
X.Nm ncheck
X.Op Fl i Ar numbers
X.Op Fl asv
X.Ar filesystem
X.Sh DESCRIPTION
X.Nm ncheck
Xgenerates a list of filenames and inode numbers for the given
Xfile system. Names of directories are followed by a `.'
X.Pp
XThe report is not sorted.
X.Pp
XThe options to
X.Nm ncheck
Xare as follows:
X.Bl -tag -width indent
X.It Fl i Ar numbers
XReport only those files whose inode numbers follows.
X.It Fl a
XPrint the file names `.' and `..', which are ordinarily skipped.
X.It Fl s
XReport only special files and files with set-user-ID or set-group-ID
Xset. This is meant to find hidden violations of security policies.
X.It Fl v
XGive more verbose information on inodes
X.Sh SEE ALSO
X.Xr sort 1 ,
X.Xr fsck 8
X.Sh HISTORY
XThe
X.Nm ncheck
Xcommand was designed to be similar in functionality to the corresponding
Xcommand in
X.Tn "SunOS 4.1.3" .
X.Sh AUTHOR
X.Bl -tag
XThorsten Lockert <tholo@sigmasoft.com>
X.El
END-of-ncheck.8
echo x - ncheck.c
sed 's/^X//' >ncheck.c << 'END-of-ncheck.c'
X/* $NetBSD$ */
X
X/*-
X * Copyright (c) 1995 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X * notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X * notice, this list of conditions and the following disclaimer in the
X * documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X * must display the following acknowledgement:
X * This product includes software developed by SigmaSoft, Th. Lockert,
X * for the NetBSD Project.
X * 4. The name of the author may not be used to endorse or promote products
X * derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
X * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
X * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
X * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
X * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X */
X
X#ifndef lint
Xstatic char rcsid[] = "$NetBSD$";
X#endif /* not lint */
X
X#include <sys/param.h>
X#include <sys/time.h>
X#include <sys/stat.h>
X#include <ufs/ffs/fs.h>
X#include <ufs/ufs/dir.h>
X#include <ufs/ufs/dinode.h>
X
X#include <stdio.h>
X#include <stdlib.h>
X#include <fcntl.h>
X#include <string.h>
X#include <ctype.h>
X#include <unistd.h>
X#include <errno.h>
X#include <err.h>
X
X#define MAXINOPB (MAXBSIZE / sizeof(struct dinode))
X
Xchar *disk; /* name of the disk file */
Xint diskfd; /* disk file descriptor */
Xstruct fs *sblock; /* the file system super block */
Xchar sblock_buf[MAXBSIZE];
Xlong dev_bsize; /* block size of underlying disk device */
Xint dev_bshift; /* log2(dev_bsize) */
Xino_t *ilist; /* list of inodes to check */
Xint ninodes; /* number of inodes in list */
Xint sflag; /* only suid and special files */
Xint aflag; /* print the . and .. entries too */
Xint mflag; /* verbose output */
X
Xstruct icache_s {
X ino_t ino;
X struct dinode di;
X} *icache;
Xint nicache;
X
Xvoid addinode __P((ino_t inum));
Xstruct dinode *getino __P((ino_t inum));
Xvoid findinodes __P((ino_t));
Xvoid bread __P((daddr_t, char *, int));
Xvoid usage __P((void));
Xvoid scanonedir __P((ino_t, const char *));
Xvoid dirindir __P((ino_t, daddr_t, int, long *, const char *));
Xvoid searchdir __P((ino_t, daddr_t, long, long, const char *));
Xint matchino __P((const void *, const void *));
Xint matchcache __P((const void *, const void *));
Xvoid cacheino __P((ino_t, struct dinode *));
Xstruct dinode *cached __P((ino_t));
Xint main __P((int, char *[]));
X
X/*
X * Check to see if the indicated inodes are the same
X */
Xint
Xmatchino(key, val)
X const void *key, *val;
X{
X ino_t k = *(ino_t *)key;
X ino_t v = *(ino_t *)val;
X
X if (k < v)
X return -1;
X else if (k > v)
X return 0;
X return 0;
X}
X
X/*
X * Check if the indicated inode match the entry in the cache
X */
Xint matchcache(key, val)
X const void *key, *val;
X{
X ino_t ino = *(ino_t *)key;
X struct icache_s *ic = (struct icache_s *)val;
X
X if (ino < ic->ino)
X return -1;
X else if (ino > ic->ino)
X return 1;
X return 0;
X}
X
X/*
X * Add an inode to the cached entries
X */
Xvoid
Xcacheino(ino, ip)
X ino_t ino;
X struct dinode *ip;
X{
X if (nicache)
X icache = realloc(icache, (nicache + 1) * sizeof(struct icache_s));
X else
X icache = malloc(sizeof(struct icache_s));
X icache[nicache].ino = ino;
X icache[nicache++].di = *ip;
X}
X
X/*
X * Get a cached inode
X */
Xstruct dinode *
Xcached(ino)
X ino_t ino;
X{
X struct icache_s *ic;
X
X ic = (struct icache_s *)bsearch(&ino, icache, nicache, sizeof(struct icache_s), matchcache);
X return ic ? &ic->di : NULL;
X}
X
X/*
X * Walk the inode list for a filesystem to find all allocated inodes
X * Remember inodes we want to give information about and cache all
X * inodes pointing to directories
X */
Xvoid
Xfindinodes(maxino)
X ino_t maxino;
X{
X register ino_t ino;
X register struct dinode *dp;
X mode_t mode;
X
X for (ino = ROOTINO; ino < maxino; ino++) {
X dp = getino(ino);
X mode = dp->di_mode & IFMT;
X if (!mode)
X continue;
X if (mode == IFDIR)
X cacheino(ino, dp);
X if (sflag &&
X (((dp->di_mode & (ISGID | ISUID)) == 0) &&
X ((mode == IFREG) || (mode == IFDIR) || (mode == IFLNK))))
X continue;
X addinode(ino);
X }
X}
X
X/*
X * Get a specified inode from disk. Attempt to minimize reads to once
X * per cylinder group
X */
Xstruct dinode *
Xgetino(inum)
X ino_t inum;
X{
X static struct dinode *itab = NULL;
X static daddr_t iblk = -1;
X struct dinode *ip;
X
X if (inum < ROOTINO || inum >= sblock->fs_ncg * sblock->fs_ipg)
X return NULL;
X if ((ip = cached(inum)) != NULL)
X return ip;
X if ((inum / sblock->fs_ipg) != iblk || itab == NULL) {
X iblk = inum / sblock->fs_ipg;
X if (itab == NULL &&
X (itab = calloc(sizeof(struct dinode), sblock->fs_ipg)) == NULL)
X errx(1, "no memory for inodes");
X bread(fsbtodb(sblock, cgimin(sblock, iblk)), (char *)itab,
X sblock->fs_ipg * sizeof(struct dinode));
X }
X return &itab[inum % sblock->fs_ipg];
X}
X
X/*
X * Read a chunk of data from the disk. Try to recover from hard errors by
X * reading in sector sized pieces. Error recovery is attempted at most
X * BREADEMAX times before seeking consent from the operator to continue.
X */
Xint breaderrors = 0;
X#define BREADEMAX 32
X
Xvoid
Xbread(blkno, buf, size)
X daddr_t blkno;
X char *buf;
X int size;
X{
X int cnt, i;
X
Xloop:
X if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
X warnx("bread: lseek fails\n");
X if ((cnt = read(diskfd, buf, size)) == size)
X return;
X if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
X /*
X * Trying to read the final fragment.
X *
X * NB - dump only works in TP_BSIZE blocks, hence
X * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
X * It should be smarter about not actually trying to
X * read more than it can get, but for the time being
X * we punt and scale back the read only when it gets
X * us into trouble. (mkm 9/25/83)
X */
X size -= dev_bsize;
X goto loop;
X }
X if (cnt == -1)
X warnx("read error from %s: %s: [block %d]: count=%d\n",
X disk, strerror(errno), blkno, size);
X else
X warnx("short read error from %s: [block %d]: count=%d, got=%d\n",
X disk, blkno, size, cnt);
X if (++breaderrors > BREADEMAX)
X errx(1, "More than %d block read errors from %s\n", BREADEMAX, disk);
X /*
X * Zero buffer, then try to read each sector of buffer separately.
X */
X memset(buf, 0, size);
X for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
X if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
X warnx("bread: lseek2 fails!\n");
X if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
X continue;
X if (cnt == -1) {
X warnx("read error from %s: %s: [sector %d]: count=%d\n",
X disk, strerror(errno), blkno, dev_bsize);
X continue;
X }
X warnx("short read error from %s: [sector %d]: count=%d, got=%d\n",
X disk, blkno, dev_bsize, cnt);
X }
X}
X
X/*
X * Add an inode to the in-memory list of inodes to dump
X */
Xvoid
Xaddinode(ino)
X ino_t ino;
X{
X if (ninodes)
X ilist = realloc(ilist, sizeof(ino_t) * (ninodes + 1));
X else
X ilist = malloc(sizeof(ino_t));
X if (ilist == NULL)
X errx(4, "not enough memory to allocate tables");
X ilist[ninodes] = ino;
X ninodes++;
X}
X
X/*
X * Scan the directory pointer at by ino
X */
Xvoid
Xscanonedir(ino, path)
X ino_t ino;
X const char *path;
X{
X struct dinode *dp;
X long filesize;
X int i;
X
X if ((dp = cached(ino)) == NULL)
X return;
X filesize = dp->di_size;
X for (i = 0; filesize > 0 && i < NDADDR; i++) {
X if (dp->di_db[i])
X searchdir(ino, dp->di_db[i], dblksize(sblock, dp, i), filesize, path);
X filesize -= sblock->fs_bsize;
X }
X for (i = 0; filesize > 0 && i < NIADDR; i++) {
X if (dp->di_ib[i])
X dirindir(ino, dp->di_ib[i], i, &filesize, path);
X }
X}
X
X/*
X * Read indirect blocks, and pass the data blocks to be searched
X * as directories. Quit as soon as any entry is found that will
X * require the directory to be dumped.
X */
Xvoid
Xdirindir(ino, blkno, ind_level, filesize, path)
X ino_t ino;
X daddr_t blkno;
X int ind_level;
X long *filesize;
X const char *path;
X{
X daddr_t idblk[MAXBSIZE / sizeof(daddr_t)];
X int i;
X
X bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
X if (ind_level <= 0) {
X for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
X blkno = idblk[i];
X if (blkno)
X searchdir(ino, blkno, sblock->fs_bsize, *filesize, path);
X }
X return;
X }
X ind_level--;
X for (i = 0; *filesize > 0 && NINDIR(sblock); i++) {
X blkno = idblk[i];
X if (blkno)
X dirindir(ino, blkno, ind_level, filesize, path);
X }
X}
X
X/*
X * Scan a disk block containing directory information looking to see if
X * any of the entries are on the inode list and to see if the directory
X * contains any subdirectories. Display entries for marked inodes.
X * Pass inodes pointing to directories back to scanonedir().
X */
Xvoid
Xsearchdir(ino, blkno, size, filesize, path)
X ino_t ino;
X daddr_t blkno;
X long size;
X long filesize;
X const char *path;
X{
X char dblk[MAXBSIZE];
X struct direct *dp;
X struct dinode *di;
X mode_t mode;
X char *npath;
X long loc;
X
X bread(fsbtodb(sblock, blkno), dblk, (int)size);
X if (filesize < size)
X size = filesize;
X for (loc = 0; loc < size;) {
X dp = (struct direct *)(dblk + loc);
X if (dp->d_reclen == 0) {
X warnx("corrupted directory, inode %lu", ino);
X break;
X }
X loc += dp->d_reclen;
X if (!dp->d_ino)
X continue;
X if (dp->d_name[0] == '.') {
X if (!aflag && (dp->d_name[1] == '\0' ||
X (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
X continue;
X }
X di = getino(dp->d_ino);
X mode = di->di_mode & IFMT;
X if (bsearch(&dp->d_ino, ilist, ninodes, sizeof(*ilist), matchino)) {
X if (mflag)
X printf("mode %-6o uid %-5lu gid %-5lu ino ", di->di_mode, di->di_uid, di->di_gid);
X printf("%-7lu %s/%s%s\n", dp->d_ino, path, dp->d_name, mode == IFDIR ? "/." : "");
X if (dp->d_name[0] == '.') {
X if (dp->d_name[1] == '\0' ||
X (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
X continue;
X }
X }
X if (mode == IFDIR) {
X npath = malloc(strlen(path) + strlen(dp->d_name) + 2);
X strcpy(npath, path);
X strcat(npath, "/");
X strcat(npath, dp->d_name);
X scanonedir(dp->d_ino, npath);
X free(npath);
X }
X }
X}
X
Xvoid
Xusage()
X{
X fprintf(stderr, "Usage: ncheck [-i numbers] [-as] filesystem\n");
X exit(3);
X}
X
Xint
Xmain(argc, argv)
X int argc;
X char *argv[];
X{
X int c, iflag = 0;
X ino_t ino;
X
X while ((c = getopt(argc, argv, "ai:ms")) != EOF)
X switch (c) {
X case 'a':
X aflag++;
X break;
X case 'i':
X iflag++;
X addinode(strtoul(optarg, NULL, 10));
X while (optind < argc && (ino = strtoul(argv[optind], NULL, 10)) != 0) {
X addinode(ino);
X optind++;
X }
X break;
X case 'm':
X mflag++;
X break;
X case 's':
X sflag++;
X break;
X case '?':
X exit(2);
X }
X if (optind != argc - 1)
X usage();
X disk = argv[optind];
X
X if ((diskfd = open(disk, O_RDONLY)) < 0)
X err(1, "cannot open %s", disk);
X sblock = (struct fs *)sblock_buf;
X bread(SBOFF, (char *)sblock, SBSIZE);
X if (sblock->fs_magic != FS_MAGIC)
X errx(1, "not a file system");
X dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
X dev_bshift = ffs(dev_bsize) - 1;
X if (dev_bsize != (1 << dev_bshift))
X errx(2, "blocksize (%d) not a power of 2", dev_bsize);
X if (!iflag)
X findinodes(sblock->fs_ipg * sblock->fs_ncg);
X printf("%s:\n", disk);
X scanonedir(ROOTINO, "");
X close(diskfd);
X return 0;
X}
END-of-ncheck.c
exit
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed
State-Changed-By: cgd
State-Changed-When: Wed Jul 12 00:48:28 EDT 1995
State-Changed-Why:
I encouraged Thorsten to submit this, and almost committed it. However,
it's a Bad Idea, at least relatively.
ncheck has the annoying property that it is inherently file system specific.
local file systems other than the FFS (including cd9660 and LFS) have
the ability to support devices and set-id binaries. given that, ncheck
cannot be used in place of 'find' until all local file systems have
ncheck equivalents. were that to be done, 'ncheck' would have to be
converted to somehow invoke the appropriate per-filesystem programs, etc.
As noted in the PR, ncheck is mostly useless these days. indeed, it's only
real use is to find devices and set-id files. given that, i see little point
in requiring all N supported local file systems to have an ncheck-equivalent,
and little reason to encourage its use.
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.