[mod.sources] chuni.c - change a users default universe

sources-request@panda.UUCP (03/01/86)

Mod.sources:  Volume 4, Issue 3
Submitted by: panda!talcott!seismo!munnari!aaec.oz!frank (Frank Crawford)


----------------- CUT HERE ------------------- CUT HERE ---------------------
#!/bin/sh
# This is a shell archive, extract with sh (not csh)
# It contains :	README
#		Makefile
#		chuni.1
#		chuni.c
#
echo 'Start of chuni.shar, part 01 of 01:'
echo 'x - README'
sed 's/^X//' > README << '/'
X	Here is a program to let users change their default login universe.
XIt's Pyramid specific and is used in a fashion similar to ``chsh''.
X
X	This program was written very early on and as much as anything was
Xreally to play with file locking and with universes.  It has been used on
Xour Pyramid for nearly 18 months and hasn't been changed in over 12 months.
X
X	The major problem with it is that it relies on the Universe name
Xbeing only three characters long.  If this ever changes (and it probably
Xwill) then the program will have to be rewritten.
X
X	To use it just compile and install it setuid root.  See the
Xaccompanying Makefile.
X
X	Any bugs or problems let me know.
X
X						Frank Crawford
X
X------
XMail:	Australian Atomic Energy Commission,
X	Private Mailbag,
X	Sutherland,
X	N.S.W.	2232
X	Australia.
XPhone:	+61 2 543 3094
X
XACSnet:	frank@aaec.OZ
XCSNET:	frank@aaec.OZ
XUUCP:	{seismo,mcvax,ukc,prlb2,ubc-vision}!munnari!aaec.OZ!frank
XARPA/
XInternet: frank%aaec.OZ@seismo.CSS.GOV
/
echo 'x - Makefile'
sed 's/^X//' > Makefile << '/'
X#	Makefile for chuni
XPROGNAME = chuni
X
X# programs
XCC = cc
X
X# flags
XCFLAGS = -O
XLDFLAGS =
X
X# directories
XBIN = /usr/local/bin
X
X#files
XSRCS = chuni.c
X
Xall:	$(PROGNAME)
X
X$(PROGNAME):	$(SRCS)
X	$(CC) $(CFLAGS) $(LDFLAGS) -o $(PROGNAME) $(SRCS)
X
Xinstall:	$(PROGNAME)
X	ucb install -o root -m u+srwx,og+rx $(PROGNAME) $(BIN)
X
Xclean:
X	rm -f core *.o $(PROGNAME)
X
Xdelta:
X	sccs delta `sccs tell`
X
Xprint:
X	@print Makefile $(SRCS)
/
echo 'x - chuni.1'
sed 's/^X//' > chuni.1 << '/'
X.TH CHUNI 1 local
X.SH NAME
Xchuni \- change default login universe
X.SH SYNOPSIS
X.B chuni
Xname
X[
Xuniverse
X]
X.SH DESCRIPTION
X.I Chuni
Xis a command similar to
X.IR  chsh (1) ,
Xexcept that it is used to change the login universe field in the universe
Xfile rather than the shell field in the password file.
XIf no
X.I universe
Xis specified,
Xthen the current login universe is printed.
XOtherwise, only
X.IR att
Xor
X.I ucb
Xcan be specified as the universe.
X.PP
XOnly the Superuser can change the login universe of someone else.
X.PP
XAn example use of this command would be
X.PP
X.DT
X	chuni bill ucb
X.SH FILES
X/etc/u_universe
X.SH "SEE ALSO"
Xchsh(1), universe(1) 
X.SH AUTHOR
XFrank Crawford
X.\"	@(#)chuni.1	8/31/84
X.SH BUGS
X.PP
XIf Pyramid ever add a universe name that is not 3 characters long then
Xthis will have to be rewritten.
/
echo 'x - chuni.c'
sed 's/^X//' > chuni.c << '/'
X/* This program is designed to change the default login universe
X * of a user.  It requires to run setuid and handles checking of
X * user permissions (hopefully).
X *
X *    Only a user or root can change a the login universe
X *					F. Crawford - 31 Jul 84
X *	SCCS @(#)chuni.c	1.3	9/26/84
X */
X#include <stdio.h>
X#include <pwd.h>
X#include <universe.h>
X#include <sys/file.h>
X
X#define UNIVERSE	"/etc/u_universe"	/* Universe file */
X#define NOUNIV	(sizeof(univ_name) / sizeof(char *))
X						/* Number of known universes */
X#define UCB	2				/* Universe number of UCB */
X
Xchar *getlogin();
X
Xvoid release (file)
X    FILE *file;
X    {
X    flock(fileno(file), LOCK_UN);
X    fclose(file);
X    }
X
Xint main(argc, argv)
X    int argc;
X    char **argv;
X    {
X    register FILE *univ;
X    register char *loginid;
X    register int i;
X    char buf[20], name[20];
X    int len;
X    struct passwd *pwdent;
X
X    setuniverse(UCB);
X    if (--argc < 1)
X	{
X	fprintf(stderr, "Usage: %s user [universe]\n", *argv);
X	exit(1);
X	}
X    else
X	++argv;
X    if (!(loginid = getlogin()))
X	{
X	if (!(pwdent = getpwuid(getuid())))
X	    {
X	    fprintf(stderr, "You don't exist\n");
X	    exit(1);
X	    }
X	loginid = pwdent->pw_name;
X	}
X    if (!(univ = fopen(UNIVERSE, "r+")))
X	{
X	perror(UNIVERSE);
X	exit(1);
X	}
X    setbuf(univ, NULL);
X    if (flock(fileno(univ), LOCK_SH | LOCK_NB) < 0)
X	{
X	fprintf(stderr, "File is busy\n");
X	fclose(univ);
X	exit(1);
X	}
X    strcpy(name, *argv);
X    strcat(name, ":");
X    len = strlen(name);
X    while (fgets(buf, sizeof(buf), univ) && strncmp(name, buf, len))
X	;
X    if (strncmp(name, buf, len))
X	{
X	fprintf(stderr, "Name not found\n");
X	release(univ);
X	exit(1);
X	}
X    if (argc == 1)
X	{
X	printf("%s", &buf[len]);
X	release(univ);
X	exit(0);
X	}
X    else
X	{
X	if (getuid() && strcmp(*argv, loginid))
X	    fprintf(stderr, "Permission denied\n");
X	else
X	    {
X	    for (i = 0; i < NOUNIV; i++)
X		if (!strcmp(argv[1], univ_name[i]))
X		    {
X		    flock(fileno(univ), LOCK_EX);
X		    fseek(univ, (long) (-strlen(buf)), 1);
X		    fprintf(univ, "%s:%s\n", argv[0], argv[1]);
X		    release(univ);
X		    exit(0);
X		    }
X	    fprintf(stderr, "Unknown universe\n");
X	    }
X	}
X    release(univ);
X    exit(1);
X    }
/
echo 'Part 01 of chuni.shar complete.'
exit