From c61fa9d901f369e787b1fce71faa595abf8b9153 Mon Sep 17 00:00:00 2001 From: "William F. Jolitz" Date: Wed, 17 Apr 1991 16:02:43 -0800 Subject: [PATCH] 386BSD 0.1 development Work on file usr/src/usr.bin/chpass/chpass.1 Work on file usr/src/usr.bin/chpass/chpass.c Work on file usr/src/usr.bin/chpass/pathnames.h Work on file usr/src/usr.bin/chpass/chpass.h Work on file usr/src/usr.bin/chpass/field.c Work on file usr/src/usr.bin/chpass/edit.c Work on file usr/src/usr.bin/chpass/util.c Work on file usr/src/usr.bin/chpass/table.c Work on file usr/src/usr.bin/cksum/cksum.c Work on file usr/src/usr.bin/cksum/crc.c Work on file usr/src/usr.bin/cksum/extern.h Work on file usr/src/usr.bin/cksum/sum1.c Work on file usr/src/usr.bin/cksum/print.c Work on file usr/src/usr.bin/cksum/sum2.c Co-Authored-By: Lynne Greer Jolitz Synthesized-from: 386BSD-0.1 --- usr/src/usr.bin/chpass/chpass.1 | 230 ++++++++++++++++++++++++++ usr/src/usr.bin/chpass/chpass.c | 185 +++++++++++++++++++++ usr/src/usr.bin/chpass/chpass.h | 50 ++++++ usr/src/usr.bin/chpass/edit.c | 201 ++++++++++++++++++++++ usr/src/usr.bin/chpass/field.c | 256 +++++++++++++++++++++++++++++ usr/src/usr.bin/chpass/pathnames.h | 39 +++++ usr/src/usr.bin/chpass/table.c | 63 +++++++ usr/src/usr.bin/chpass/util.c | 140 ++++++++++++++++ usr/src/usr.bin/cksum/cksum.c | 120 ++++++++++++++ usr/src/usr.bin/cksum/crc.c | 132 +++++++++++++++ usr/src/usr.bin/cksum/extern.h | 45 +++++ usr/src/usr.bin/cksum/print.c | 64 ++++++++ usr/src/usr.bin/cksum/sum1.c | 68 ++++++++ usr/src/usr.bin/cksum/sum2.c | 70 ++++++++ 14 files changed, 1663 insertions(+) create mode 100644 usr/src/usr.bin/chpass/chpass.1 create mode 100644 usr/src/usr.bin/chpass/chpass.c create mode 100644 usr/src/usr.bin/chpass/chpass.h create mode 100644 usr/src/usr.bin/chpass/edit.c create mode 100644 usr/src/usr.bin/chpass/field.c create mode 100644 usr/src/usr.bin/chpass/pathnames.h create mode 100644 usr/src/usr.bin/chpass/table.c create mode 100644 usr/src/usr.bin/chpass/util.c create mode 100644 usr/src/usr.bin/cksum/cksum.c create mode 100644 usr/src/usr.bin/cksum/crc.c create mode 100644 usr/src/usr.bin/cksum/extern.h create mode 100644 usr/src/usr.bin/cksum/print.c create mode 100644 usr/src/usr.bin/cksum/sum1.c create mode 100644 usr/src/usr.bin/cksum/sum2.c diff --git a/usr/src/usr.bin/chpass/chpass.1 b/usr/src/usr.bin/chpass/chpass.1 new file mode 100644 index 0000000000..23ee47d93c --- /dev/null +++ b/usr/src/usr.bin/chpass/chpass.1 @@ -0,0 +1,230 @@ +.\" Copyright (c) 1988, 1990 The Regents of the University of California. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)chpass.1 5.11 (Berkeley) 3/14/91 +.\" +.Dd March 14, 1991 +.Dt CHPASS 1 +.Os +.Sh NAME +.Nm chpass +.Nd add or change user database information +.Sh SYNOPSIS +chpass +.Op Fl a Ar list +.Op Fl s Ar shell +.Op user +.Sh DESCRIPTION +.Nm Chpass +allows editing of the user database information associated +with +.Ar user +or, by default, the current user. +The information is formatted and supplied to an editor for changes. +.Pp +Only the information that the user is allowed to change is displayed. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl a +The super-user is allowed to directly supply a user database +entry, in the format specified by +.Xr passwd 5 , +as an argument. +This argument must be a colon (``:'') separated list of all the +user database fields, although they may be empty. +.It Fl s +The +.Fl s +option attempts to change the user's shell to +.Ar newsh . +.El +.Pp +Possible display items are as follows: +.Pp +.Bl -tag -width "Home Directory:" -compact -offset indent +.It Login: +user's login name +.It Password: +user's encrypted password +.It Uid: +user's id +.It Gid: +user's login group id +.It Change: +password change time +.It Expire: +account expiration time +.It Class: +user's general classification +.It Home Directory: +user's home directory +.It Shell: +user's login shell +.It Full Name: +user's real name +.It Location: +user's normal location +.It Home Phone: +user's home phone +.It Office Phone: +user's office phone +.El +.Pp +The +.Ar login +field is the user name used to access the computer account. +.Pp +The +.Ar password +field contains the encrypted form of the user's password. +.Pp +The +.Ar uid +field is the number associated with the +.Ar login +field. +Both of these fields should be unique across the system (and often +across a group of systems) as they control file access. +.Pp +While it is possible to have multiple entries with identical login names +and/or identical user id's, it is usually a mistake to do so. Routines +that manipulate these files will often return only one of the multiple +entries, and that one by random selection. +.Pp +The +.Ar group +field is the group that the user will be placed in at login. +Since this system supports multiple groups (see +.Xr groups 1 ) +this field currently has little special meaning. +This field may be filled in with either a number or a group name (see +.Xr group 5 ) . +.Pp +The +.Ar change +field is the date by which the password must be changed. +.Pp +The +.Ar expire +field is the date on which the account expires. +.Pp +Both the +.Ar change +and +.Ar expire +fields should be entered in the form ``month day year'' where +.Ar month +is the month name (the first three characters are sufficient), +.Ar day +is the day of the month, and +.Ar year +is the year. +.Pp +The +.Ar class +field is currently unused. In the near future it will be a key to +a +.Xr termcap 5 +style database of user attributes. +.Pp +The user's +.Ar home directory +is the full UNIX path name where the user +will be placed at login. +.Pp +The +.Ar shell +field is the command interpreter the user prefers. +If the +.Ar shell +field is empty, the Bourne shell, +.Pa /bin/sh , +is assumed. +When altering a login shell, and not the super-user, the user +may not change from a non-standard shell or to a non-standard +shell. +Non-standard is defined as a shell not found in +.Pa /etc/shells . +.Pp +The last four fields are for storing the user's +.Ar full name , office location , +and +.Ar home +and +.Ar work telephone +numbers. +.Pp +Once the information has been verified, +.Nm chpass +uses +.Xr pwd_mkdb 8 +to update the user database. +.Sh ENVIRONMENT +The +.Xr vi 1 +editor will be used unless the environment variable EDITOR is set to +an alternate editor. +When the editor terminates, the information is re-read and used to +update the user database itself. +Only the user, or the super-user, may edit the information associated +with the user. +.Sh FILES +.Bl -tag -width /etc/master.passwd -compact +.It Pa /etc/master.passwd +The user database +.It Pa /etc/passwd +A Version 7 format password file +.It Pa /etc/chpass.XXXXXX +Temporary copy of the password file +.It Pa /etc/shells +The list of approved shells +.El +.Sh SEE ALSO +.Xr login 1 , +.Xr finger 1 , +.Xr passwd 1 , +.Xr getusershell 3 , +.Xr passwd 5 , +.Xr pwd_mkdb 8 , +.Xr vipw 8 +.Rs +.%A Robert Morris +.%A Ken Thompson +.%T "UNIX Password security" +.Re +.Sh BUGS +User information should (and eventually will) be stored elsewhere. +.Sh HISTORY +The +.Nm +command appeared in +.Bx 4.3 Reno . diff --git a/usr/src/usr.bin/chpass/chpass.c b/usr/src/usr.bin/chpass/chpass.c new file mode 100644 index 0000000000..4cf13e2032 --- /dev/null +++ b/usr/src/usr.bin/chpass/chpass.c @@ -0,0 +1,185 @@ +/*- + * Copyright (c) 1988 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +char copyright[] = +"@(#) Copyright (c) 1988 The Regents of the University of California.\n\ + All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)chpass.c 5.17 (Berkeley) 3/3/91"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "chpass.h" +#include "pathnames.h" + +char *progname = "chpass"; +char *tempname; +uid_t uid; + +main(argc, argv) + int argc; + char **argv; +{ + extern int optind; + extern char *optarg; + register enum { NEWSH, LOADENTRY, EDITENTRY } op; + register struct passwd *pw; + struct passwd lpw; + int ch, pfd, tfd; + char *arg; + + op = EDITENTRY; + while ((ch = getopt(argc, argv, "a:s:")) != EOF) + switch(ch) { + case 'a': + op = LOADENTRY; + arg = optarg; + break; + case 's': + op = NEWSH; + arg = optarg; + break; + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + uid = getuid(); + + if (op == EDITENTRY || op == NEWSH) + switch(argc) { + case 0: + if (!(pw = getpwuid(uid))) { + (void)fprintf(stderr, + "chpass: unknown user: uid %u\n", uid); + exit(1); + } + break; + case 1: + if (!(pw = getpwnam(*argv))) { + (void)fprintf(stderr, + "chpass: unknown user %s.\n", *argv); + exit(1); + } + if (uid && uid != pw->pw_uid) + baduser(); + break; + default: + usage(); + } + + if (op == NEWSH) { + /* protect p_shell -- it thinks NULL is /bin/sh */ + if (!arg[0]) + usage(); + if (p_shell(arg, pw, (ENTRY *)NULL)) + pw_error((char *)NULL, 0, 1); + } + + if (op == LOADENTRY) { + if (uid) + baduser(); + pw = &lpw; + if (!pw_scan(arg, pw)) + exit(1); + } + + /* + * The temporary file/file descriptor usage is a little tricky here. + * 1: We start off with two fd's, one for the master password + * file (used to lock everything), and one for a temporary file. + * 2: Display() gets an fp for the temporary file, and copies the + * user's information into it. It then gives the temporary file + * to the user and closes the fp, closing the underlying fd. + * 3: The user edits the temporary file some number of times. + * 4: Verify() gets an fp for the temporary file, and verifies the + * contents. It can't use an fp derived from the step #2 fd, + * because the user's editor may have created a new instance of + * the file. Once the file is verified, its contents are stored + * in a password structure. The verify routine closes the fp, + * closing the underlying fd. + * 5: Delete the temporary file. + * 6: Get a new temporary file/fd. Pw_copy() gets an fp for it + * file and copies the master password file into it, replacing + * the user record with a new one. We can't use the first + * temporary file for this because it was owned by the user. + * Pw_copy() closes its fp, flushing the data and closing the + * underlying file descriptor. We can't close the master + * password fp, or we'd lose the lock. + * 7: Call pw_mkdb() (which renames the temporary file) and exit. + * The exit closes the master passwd fp/fd. + */ + pw_init(); + pfd = pw_lock(); + tfd = pw_tmp(); + + if (op == EDITENTRY) { + display(tfd, pw); + edit(pw); + (void)unlink(tempname); + tfd = pw_tmp(); + } + + pw_copy(pfd, tfd, pw); + + if (!pw_mkdb()) + pw_error((char *)NULL, 0, 1); + exit(0); +} + +baduser() +{ + (void)fprintf(stderr, "chpass: %s\n", strerror(EACCES)); + exit(1); +} + +usage() +{ + (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n"); + exit(1); +} diff --git a/usr/src/usr.bin/chpass/chpass.h b/usr/src/usr.bin/chpass/chpass.h new file mode 100644 index 0000000000..1b8bf264cf --- /dev/null +++ b/usr/src/usr.bin/chpass/chpass.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1988 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)chpass.h 5.3 (Berkeley) 2/12/91 + */ + +typedef struct _entry { + char *prompt; + int (*func)(), restricted, len; + char *except, *save; +} ENTRY; + +/* Field numbers. */ +#define E_BPHONE 8 +#define E_HPHONE 9 +#define E_LOCATE 10 +#define E_NAME 7 +#define E_SHELL 12 + +extern ENTRY list[]; +extern uid_t uid; diff --git a/usr/src/usr.bin/chpass/edit.c b/usr/src/usr.bin/chpass/edit.c new file mode 100644 index 0000000000..3308ea65e0 --- /dev/null +++ b/usr/src/usr.bin/chpass/edit.c @@ -0,0 +1,201 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)edit.c 5.2 (Berkeley) 3/3/91"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "chpass.h" + +extern char *tempname; + +void +edit(pw) + struct passwd *pw; +{ + struct stat begin, end; + + for (;;) { + if (stat(tempname, &begin)) + pw_error(tempname, 1, 1); + pw_edit(1); + if (stat(tempname, &end)) + pw_error(tempname, 1, 1); + if (begin.st_mtime == end.st_mtime) { + (void)fprintf(stderr, "chpass: no changes made\n"); + pw_error((char *)NULL, 0, 0); + } + if (verify(pw)) + break; + pw_prompt(); + } +} + +/* + * display -- + * print out the file for the user to edit; strange side-effect: + * set conditional flag if the user gets to edit the shell. + */ +display(fd, pw) + int fd; + struct passwd *pw; +{ + register char *p; + FILE *fp; + char *bp, *ok_shell(), *ttoa(); + + if (!(fp = fdopen(fd, "w"))) + pw_error(tempname, 1, 1); + + (void)fprintf(fp, + "#Changing user database information for %s.\n", pw->pw_name); + if (!uid) { + (void)fprintf(fp, "Login: %s\n", pw->pw_name); + (void)fprintf(fp, "Password: %s\n", pw->pw_passwd); + (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); + (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); + (void)fprintf(fp, "Change [month day year]: %s\n", + ttoa(pw->pw_change)); + (void)fprintf(fp, "Expire [month day year]: %s\n", + ttoa(pw->pw_expire)); + (void)fprintf(fp, "Class: %s\n", pw->pw_class); + (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir); + (void)fprintf(fp, "Shell: %s\n", + *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); + } + /* Only admin can change "restricted" shells. */ + else if (ok_shell(pw->pw_shell)) + /* + * Make shell a restricted field. Ugly with a + * necklace, but there's not much else to do. + */ + (void)fprintf(fp, "Shell: %s\n", + *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); + else + list[E_SHELL].restricted = 1; + bp = pw->pw_gecos; + p = strsep(&bp, ","); + (void)fprintf(fp, "Full Name: %s\n", p ? p : ""); + p = strsep(&bp, ","); + (void)fprintf(fp, "Location: %s\n", p ? p : ""); + p = strsep(&bp, ","); + (void)fprintf(fp, "Office Phone: %s\n", p ? p : ""); + p = strsep(&bp, ","); + (void)fprintf(fp, "Home Phone: %s\n", p ? p : ""); + + (void)fchown(fd, getuid(), getgid()); + (void)fclose(fp); +} + +verify(pw) + struct passwd *pw; +{ + register ENTRY *ep; + register char *p; + FILE *fp; + int len; + char buf[LINE_MAX]; + + if (!(fp = fopen(tempname, "r"))) + pw_error(tempname, 1, 1); + while (fgets(buf, sizeof(buf), fp)) { + if (!buf[0] || buf[0] == '#') + continue; + if (!(p = index(buf, '\n'))) { + (void)fprintf(stderr, "chpass: line too long.\n"); + goto bad; + } + *p = '\0'; + for (ep = list;; ++ep) { + if (!ep->prompt) { + (void)fprintf(stderr, + "chpass: unrecognized field.\n"); + goto bad; + } + if (!strncasecmp(buf, ep->prompt, ep->len)) { + if (ep->restricted && uid) { + (void)fprintf(stderr, + "chpass: you may not change the %s field.\n", + ep->prompt); + goto bad; + } + if (!(p = index(buf, ':'))) { + (void)fprintf(stderr, + "chpass: line corrupted.\n"); + goto bad; + } + while (isspace(*++p)); + if (ep->except && strpbrk(p, ep->except)) { + (void)fprintf(stderr, + "chpass: illegal character in the \"%s\" field.\n", + ep->prompt); + goto bad; + } + if ((ep->func)(p, pw, ep)) { +bad: (void)fclose(fp); + return(0); + } + break; + } + } + } + (void)fclose(fp); + + /* Build the gecos field. */ + len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) + + strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4; + if (!(p = malloc(len))) { + (void)fprintf(stderr, "chpass: %s\n", strerror(errno)); + exit(1); + } + (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save, + list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save); + + if (snprintf(buf, sizeof(buf), + "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s", + pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, + pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir, + pw->pw_shell) >= sizeof(buf)) { + (void)fprintf(stderr, "chpass: entries too long\n"); + return(0); + } + return(pw_scan(buf, pw)); +} diff --git a/usr/src/usr.bin/chpass/field.c b/usr/src/usr.bin/chpass/field.c new file mode 100644 index 0000000000..1ca1dbfd17 --- /dev/null +++ b/usr/src/usr.bin/chpass/field.c @@ -0,0 +1,256 @@ +/* + * Copyright (c) 1988 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)field.c 5.13 (Berkeley) 2/12/91"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include "chpass.h" +#include "pathnames.h" + +/* ARGSUSED */ +p_login(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!*p) { + (void)fprintf(stderr, "chpass: empty login field.\n"); + return(1); + } + if (*p == '-') { + (void)fprintf(stderr, + "chpass: login names may not begin with a hyphen.\n"); + return(1); + } + if (!(pw->pw_name = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save entry.\n"); + return(1); + } + if (index(p, '.')) + (void)fprintf(stderr, + "chpass: \'.\' is dangerous in a login name.\n"); + for (; *p; ++p) + if (isupper(*p)) { + (void)fprintf(stderr, + "chpass: upper-case letters are dangerous in a login name.\n"); + break; + } + return(0); +} + +/* ARGSUSED */ +p_passwd(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!*p) + pw->pw_passwd = ""; /* "NOLOGIN"; */ + else if (!(pw->pw_passwd = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save password entry.\n"); + return(1); + } + + return(0); +} + +/* ARGSUSED */ +p_uid(p, pw, ep) + register char *p; + struct passwd *pw; + ENTRY *ep; +{ + int id; + + if (!*p) { + (void)fprintf(stderr, "chpass: empty uid field.\n"); + return(1); + } + if (!isdigit(*p)) { + (void)fprintf(stderr, "chpass: illegal uid.\n"); + return(1); + } + id = atoi(p); + if ((u_int)id > USHRT_MAX) { + (void)fprintf(stderr, "chpass: %d > max uid value (%d).\n", + id, USHRT_MAX); + return(1); + } + pw->pw_uid = id; + return(0); +} + +/* ARGSUSED */ +p_gid(p, pw, ep) + register char *p; + struct passwd *pw; + ENTRY *ep; +{ + struct group *gr; + int id; + + if (!*p) { + (void)fprintf(stderr, "chpass: empty gid field.\n"); + return(1); + } + if (!isdigit(*p)) { + if (!(gr = getgrnam(p))) { + (void)fprintf(stderr, + "chpass: unknown group %s.\n", p); + return(1); + } + pw->pw_gid = gr->gr_gid; + return(0); + } + id = atoi(p); + if ((u_int)id > USHRT_MAX) { + (void)fprintf(stderr, "chpass: %d > max gid value (%d).\n", + id, USHRT_MAX); + return(1); + } + pw->pw_gid = id; + return(0); +} + +/* ARGSUSED */ +p_class(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!*p) + pw->pw_class = ""; + else if (!(pw->pw_class = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save entry.\n"); + return(1); + } + + return(0); +} + +/* ARGSUSED */ +p_change(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!atot(p, &pw->pw_change)) + return(0); + (void)fprintf(stderr, "chpass: illegal date for change field.\n"); + return(1); +} + +/* ARGSUSED */ +p_expire(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!atot(p, &pw->pw_expire)) + return(0); + (void)fprintf(stderr, "chpass: illegal date for expire field.\n"); + return(1); +} + +/* ARGSUSED */ +p_gecos(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!*p) + ep->save = ""; + else if (!(ep->save = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save entry.\n"); + return(1); + } + return(0); +} + +/* ARGSUSED */ +p_hdir(p, pw, ep) + char *p; + struct passwd *pw; + ENTRY *ep; +{ + if (!*p) { + (void)fprintf(stderr, "chpass: empty home directory field.\n"); + return(1); + } + if (!(pw->pw_dir = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save entry.\n"); + return(1); + } + return(0); +} + +/* ARGSUSED */ +p_shell(p, pw, ep) + register char *p; + struct passwd *pw; + ENTRY *ep; +{ + char *t, *ok_shell(); + + if (!*p) { + pw->pw_shell = _PATH_BSHELL; + return(0); + } + /* only admin can change from or to "restricted" shells */ + if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) { + (void)fprintf(stderr, + "chpass: %s: current shell non-standard.\n", pw->pw_shell); + return(1); + } + if (!(t = ok_shell(p))) { + if (uid) { + (void)fprintf(stderr, + "chpass: %s: non-standard shell.\n", p); + return(1); + } + } + else + p = t; + if (!(pw->pw_shell = strdup(p))) { + (void)fprintf(stderr, "chpass: can't save entry.\n"); + return(1); + } + return(0); +} diff --git a/usr/src/usr.bin/chpass/pathnames.h b/usr/src/usr.bin/chpass/pathnames.h new file mode 100644 index 0000000000..f9d1cfc5f7 --- /dev/null +++ b/usr/src/usr.bin/chpass/pathnames.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)pathnames.h 5.4 (Berkeley) 2/10/91 + */ + +#include + +#undef _PATH_TMP +#define _PATH_TMP "/tmp/chpass.XXXXXX" diff --git a/usr/src/usr.bin/chpass/table.c b/usr/src/usr.bin/chpass/table.c new file mode 100644 index 0000000000..6b734f6255 --- /dev/null +++ b/usr/src/usr.bin/chpass/table.c @@ -0,0 +1,63 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)table.c 5.1 (Berkeley) 2/12/91"; +#endif /* not lint */ + +#include +#include +#include "chpass.h" + +char e1[] = ": "; +char e2[] = ":,"; + +int p_change(), p_class(), p_expire(), p_gecos(), p_gid(), p_hdir(); +int p_login(), p_passwd(), p_shell(), p_uid(); + +ENTRY list[] = { + { "login", p_login, 1, 5, e1, }, + { "password", p_passwd, 1, 8, e1, }, + { "uid", p_uid, 1, 3, e1, }, + { "gid", p_gid, 1, 3, e1, }, + { "class", p_class, 1, 5, e1, }, + { "change", p_change, 1, 6, NULL, }, + { "expire", p_expire, 1, 6, NULL, }, + { "full name", p_gecos, 0, 9, e2, }, + { "office phone", p_gecos, 0, 12, e2, }, + { "home phone", p_gecos, 0, 10, e2, }, + { "location", p_gecos, 0, 8, e2, }, + { "home directory", p_hdir, 1, 14, e1, }, + { "shell", p_shell, 0, 5, e1, }, + { NULL, 0, }, +}; diff --git a/usr/src/usr.bin/chpass/util.c b/usr/src/usr.bin/chpass/util.c new file mode 100644 index 0000000000..b3db7c9ede --- /dev/null +++ b/usr/src/usr.bin/chpass/util.c @@ -0,0 +1,140 @@ +/*- + * Copyright (c) 1988 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)util.c 5.14 (Berkeley) 2/12/91"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include "chpass.h" +#include "pathnames.h" + +static int dmsize[] = + { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +static char *months[] = + { "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", + "December", NULL }; +char * +ttoa(tval) + time_t tval; +{ + struct tm *tp; + static char tbuf[50]; + + if (tval) { + tp = localtime(&tval); + (void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon], + tp->tm_mday, tp->tm_year); + } + else + *tbuf = '\0'; + return(tbuf); +} + +atot(p, store) + char *p; + time_t *store; +{ + register char *t, **mp; + static struct tm *lt; + time_t tval, time(); + int day, month, year; + + if (!*p) { + *store = 0; + return(0); + } + if (!lt) { + unsetenv("TZ"); + (void)time(&tval); + lt = localtime(&tval); + } + if (!(t = strtok(p, " \t"))) + goto bad; + for (mp = months;; ++mp) { + if (!*mp) + goto bad; + if (!strncasecmp(*mp, t, 3)) { + month = mp - months + 1; + break; + } + } + if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) + goto bad; + day = atoi(t); + if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t)) + goto bad; + year = atoi(t); + if (day < 1 || day > 31 || month < 1 || month > 12 || !year) + goto bad; + if (year < 100) + year += TM_YEAR_BASE; + if (year <= EPOCH_YEAR) +bad: return(1); + tval = isleap(year) && month > 2; + for (--year; year >= EPOCH_YEAR; --year) + tval += isleap(year) ? + DAYSPERLYEAR : DAYSPERNYEAR; + while (--month) + tval += dmsize[month]; + tval += day; + tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN; + tval -= lt->tm_gmtoff; + *store = tval; + return(0); +} + +char * +ok_shell(name) + register char *name; +{ + register char *p, *sh; + char *getusershell(); + + setusershell(); + while (sh = getusershell()) { + if (!strcmp(name, sh)) + return(name); + /* allow just shell name, but use "real" path */ + if ((p = rindex(sh, '/')) && !strcmp(name, p + 1)) + return(sh); + } + return(NULL); +} diff --git a/usr/src/usr.bin/cksum/cksum.c b/usr/src/usr.bin/cksum/cksum.c new file mode 100644 index 0000000000..c189d935c0 --- /dev/null +++ b/usr/src/usr.bin/cksum/cksum.c @@ -0,0 +1,120 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * James W. Williams of the University of Maryland. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +char copyright[] = +"@(#) Copyright (c) 1991 The Regents of the University of California.\n\ + All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)cksum.c 5.3 (Berkeley) 4/4/91"; +#endif /* not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "extern.h" + +main(argc, argv) + int argc; + char **argv; +{ + extern int optind; + u_long len, val; + register int ch, fd, rval; + char *fn; + int (*cfncn) __P((int, unsigned long *, unsigned long *)); + void (*pfncn) __P((char *, unsigned long, unsigned long)); + + cfncn = crc; + pfncn = pcrc; + while ((ch = getopt(argc, argv, "o:")) != EOF) + switch(ch) { + case 'o': + if (*optarg == '1') { + cfncn = csum1; + pfncn = psum1; + } else if (*optarg == '2') { + cfncn = csum2; + pfncn = psum2; + } else { + (void)fprintf(stderr, + "cksum: illegal argument to -o option\n"); + usage(); + } + break; + case '?': + default: + usage(); + } + argc -= optind; + argv += optind; + + fd = STDIN_FILENO; + fn = "stdin"; + rval = 0; + do { + if (*argv) { + fn = *argv++; + if ((fd = open(fn, O_RDONLY, 0)) < 0) { + (void)fprintf(stderr, + "cksum: %s: %s\n", fn, strerror(errno)); + rval = 1; + continue; + } + } + if (cfncn(fd, &val, &len)) { + (void)fprintf(stderr, + "cksum: %s: %s\n", fn, strerror(errno)); + rval = 1; + } else + pfncn(fn, val, len); + (void)close(fd); + } while (*argv); + exit(rval); +} + +usage() +{ + (void)fprintf(stderr, "usage: cksum [-o 1 | 2] [file ...]\n"); + exit(1); +} diff --git a/usr/src/usr.bin/cksum/crc.c b/usr/src/usr.bin/cksum/crc.c new file mode 100644 index 0000000000..3662eba949 --- /dev/null +++ b/usr/src/usr.bin/cksum/crc.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * James W. Williams of the University of Maryland. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)crc.c 5.2 (Berkeley) 4/4/91"; +#endif /* not lint */ + +#include +#include + +u_long crctab[] = { + 0x7fffffff, + 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, + 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, + 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, + 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, + 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, + 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, + 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, + 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, + 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, + 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, + 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, + 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, + 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, + 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, + 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, + 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, + 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, + 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, + 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, + 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, + 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, + 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, + 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, + 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, + 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, + 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, + 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, + 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, + 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, + 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, + 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, + 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, + 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, + 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, + 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, + 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, + 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, + 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, + 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, + 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, + 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, + 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +/* + * crc -- + * Compute a POSIX.2 checksum. This routine has been broken out since + * it is anticipated that other programs will use it. It takes a file + * descriptor to read from and locations to store the crc and the number + * of bytes read. It returns 0 on success and 1 on failure. Errno is + * set on failure. + */ +crc(fd, cval, clen) + register int fd; + u_long *cval, *clen; +{ + register int i, nr, step; + register u_char *p; + register u_long crc, total; + u_char buf[8192]; + + crc = step = total = 0; + while ((nr = read(fd, buf, sizeof(buf))) > 0) + for (total += nr, p = buf; nr--; ++p) { + if (!(i = crc >> 24 ^ *p)) { + i = step++; + if (step >= sizeof(crctab)/sizeof(crctab[0])) + step = 0; + } + crc = (crc << 8) ^ crctab[i]; + } + if (nr < 0) + return(1); + + *cval = crc & 0xffffffff; /* Mask to 32 bits. */ + *clen = total; + return(0); +} diff --git a/usr/src/usr.bin/cksum/extern.h b/usr/src/usr.bin/cksum/extern.h new file mode 100644 index 0000000000..4555fe032e --- /dev/null +++ b/usr/src/usr.bin/cksum/extern.h @@ -0,0 +1,45 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)extern.h 5.1 (Berkeley) 4/4/91 + */ + +#include + +__BEGIN_DECLS +int crc __P((int, unsigned long *, unsigned long *)); +void pcrc __P((char *, unsigned long, unsigned long)); +void psum1 __P((char *, unsigned long, unsigned long)); +void psum2 __P((char *, unsigned long, unsigned long)); +int csum1 __P((int, unsigned long *, unsigned long *)); +int csum2 __P((int, unsigned long *, unsigned long *)); +__END_DECLS diff --git a/usr/src/usr.bin/cksum/print.c b/usr/src/usr.bin/cksum/print.c new file mode 100644 index 0000000000..62779d1ee7 --- /dev/null +++ b/usr/src/usr.bin/cksum/print.c @@ -0,0 +1,64 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)print.c 5.1 (Berkeley) 4/4/91"; +#endif /* not lint */ + +#include +#include +#include "extern.h" + +void +pcrc(fn, val, len) + char *fn; + u_long val, len; +{ + (void)printf("%lu %lu %s\n", val, len, fn); +} + +void +psum1(fn, val, len) + char *fn; + u_long val, len; +{ + (void)printf("%lu %lu %s\n", val, (len + 1023) / 1024, fn); +} + +void +psum2(fn, val, len) + char *fn; + u_long val, len; +{ + (void)printf("%lu %lu %s\n", val, (len + 511) / 512, fn); +} diff --git a/usr/src/usr.bin/cksum/sum1.c b/usr/src/usr.bin/cksum/sum1.c new file mode 100644 index 0000000000..6bf9cc3417 --- /dev/null +++ b/usr/src/usr.bin/cksum/sum1.c @@ -0,0 +1,68 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)sum1.c 5.1 (Berkeley) 4/4/91"; +#endif /* not lint */ + +#include +#include + +csum1(fd, cval, clen) + register int fd; + u_long *cval, *clen; +{ + register u_long total; + register int nr; + register u_int crc; + register u_char *p; + u_char buf[8192]; + + /* + * 16-bit checksum, rotating right before each addition; + * overflow is discarded. + */ + crc = total = 0; + while ((nr = read(fd, buf, sizeof(buf))) > 0) + for (total += nr, p = buf; nr--; ++p) { + if (crc & 1) + crc |= 0x10000; + crc = ((crc >> 1) + *p) & 0xffff; + } + if (nr < 0) + return(1); + + *cval = crc; + *clen = total; + return(0); +} diff --git a/usr/src/usr.bin/cksum/sum2.c b/usr/src/usr.bin/cksum/sum2.c new file mode 100644 index 0000000000..730359abf1 --- /dev/null +++ b/usr/src/usr.bin/cksum/sum2.c @@ -0,0 +1,70 @@ +/*- + * Copyright (c) 1991 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char sccsid[] = "@(#)sum2.c 5.1 (Berkeley) 4/4/91"; +#endif /* not lint */ + +#include +#include + +csum2(fd, cval, clen) + register int fd; + u_long *cval, *clen; +{ + register u_long crc, total; + register int nr; + register u_char *p; + u_char buf[8192]; + + /* + * Draft 8 POSIX 1003.2: + * + * s = sum of all bytes + * r = s % 2^16 + (s % 2^32) / 2^16 + * crc = (r % 2^16) + r / 2^16 + */ + crc = total = 0; + while ((nr = read(fd, buf, sizeof(buf))) > 0) + for (total += nr, p = buf; nr--; ++p) + crc += *p; + if (nr < 0) + return(1); + + crc = (crc & 0xffff) + (crc >> 16); + crc = (crc & 0xffff) + (crc >> 16); + + *cval = crc; + *clen = total; + return(0); +} -- 2.20.1