Ejemplo n.º 1
0
int
uid_name(char *name, uid_t *uid)
{
	struct passwd *pw;
	UIDC *ptr;
	int namelen;

	/*
	 * return -1 for mangled names
	 */
	if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
		return(-1);
	if ((usrtb == NULL) && (usrtb_start() < 0))
		return(-1);

	/*
	 * look up in hash table, if found and valid return the uid,
	 * if found and invalid, return a -1
	 */
	ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
	if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
		if (ptr->valid == INVALID)
			return(-1);
		*uid = ptr->uid;
		return(0);
	}

	if (!pwopn) {
		setpassent(1);
		++pwopn;
	}

	if (ptr == NULL)
		ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
		  (UIDC *)malloc(sizeof(UIDC));

	/*
	 * no match, look it up, if no match store it as an invalid entry,
	 * or store the matching uid
	 */
	if (ptr == NULL) {
		if ((pw = getpwnam(name)) == NULL)
			return(-1);
		*uid = pw->pw_uid;
		return(0);
	}
	(void)strlcpy(ptr->name, name, sizeof(ptr->name));
	if ((pw = getpwnam(name)) == NULL) {
		ptr->valid = INVALID;
		return(-1);
	}
	ptr->valid = VALID;
	*uid = ptr->uid = pw->pw_uid;
	return(0);
}
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	int hash, n, pos;

	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next)
			if ((void *)s->socket == xf->xf_data)
				break;
		if (s == NULL)
			continue;
		if (!check_ports(s))
			continue;
		s->shown = 1;
		pos = 0;
		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
			pos += xprintf("%lu ", (u_long)xf->xf_uid);
		else
			pos += xprintf("%s ", pwd->pw_name);
		while (pos < 9)
			pos += xprintf(" ");
		pos += xprintf("%.10s", getprocname(xf->xf_pid));
		while (pos < 20)
			pos += xprintf(" ");
		pos += xprintf("%lu ", (u_long)xf->xf_pid);
		while (pos < 26)
			pos += xprintf(" ");
		pos += xprintf("%d ", xf->xf_fd);
		displaysock(s, pos);
	}
	if (opt_j >= 0)
		return;
	for (hash = 0; hash < HASHSIZE; hash++) {
		for (s = sockhash[hash]; s != NULL; s = s->next) {
			if (s->shown)
				continue;
			if (!check_ports(s))
				continue;
			pos = 0;
			pos += xprintf("%-8s %-10s %-5s %-2s ",
			    "?", "?", "?", "?");
			displaysock(s, pos);
		}
	}
}
Ejemplo n.º 3
0
static int
pwd_setpassent(const nvlist_t *limits, const nvlist_t *nvlin, nvlist_t *nvlout)
{
	int stayopen;

	if (!nvlist_exists_bool(nvlin, "stayopen"))
		return (EINVAL);

	stayopen = nvlist_get_bool(nvlin, "stayopen") ? 1 : 0;

	return (setpassent(stayopen) == 0 ? EFAULT : 0);
}
Ejemplo n.º 4
0
static int
passwd_fill_test_data(struct passwd_test_data *td)
{
	struct passwd *pwd;

	setpassent(1);
	while ((pwd = getpwent()) != NULL) {
		if (passwd_test_correctness(pwd, NULL) == 0)
			TEST_DATA_APPEND(passwd, td, pwd);
		else
			return (-1);
	}
	endpwent();

	return (0);
}
Ejemplo n.º 5
0
/*
 * passwd
 */
static int
passwd(int argc, char *argv[])
{
	struct passwd	*pw;
	unsigned long	id;
	int		i, rv;

	assert(argc > 1);
	assert(argv != NULL);

#define PASSWDPRINT	printf("%s:%s:%u:%u:%s:%s:%s\n", \
			    pw->pw_name, pw->pw_passwd, pw->pw_uid, \
			    pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell)

	setpassent(1);
	rv = RV_OK;
	if (argc == 2) {
		while ((pw = getpwent()) != NULL)
			PASSWDPRINT;
	} else {
		for (i = 2; i < argc; i++) {
			if (parsenum(argv[i], &id))
				pw = getpwuid((uid_t)id);
			else
				pw = getpwnam(argv[i]);
			if (pw != NULL)
				PASSWDPRINT;
			else {
				rv = RV_NOTFOUND;
				break;
			}
		}
	}
	endpwent();
	return rv;
}
Ejemplo n.º 6
0
fsnode *
read_mtree(const char *fname, fsnode *node)
{
	struct mtree_fileinfo *fi;
	FILE *fp;
	int c, error;

	/* We do not yet support nesting... */
	assert(node == NULL);

	if (strcmp(fname, "-") == 0)
		fp = stdin;
	else {
		fp = fopen(fname, "r");
		if (fp == NULL)
			err(1, "Can't open `%s'", fname);
	}

	error = mtree_file_push(fname, fp);
	if (error)
		goto out;

	bzero(&mtree_global, sizeof(mtree_global));
	bzero(&mtree_global_inode, sizeof(mtree_global_inode));
	mtree_global.inode = &mtree_global_inode;
	mtree_global_inode.nlink = 1;
	mtree_global_inode.st.st_nlink = 1;
	mtree_global_inode.st.st_atime = mtree_global_inode.st.st_ctime =
	    mtree_global_inode.st.st_mtime = time(NULL);
	errors = warnings = 0;

	setgroupent(1);
	setpassent(1);

	mtree_root = node;
	mtree_current = node;
	do {
		/* Start of a new line... */
		fi = SLIST_FIRST(&mtree_fileinfo);
		fi->line++;

		error = skip_over(fp, " \t");
		if (error)
			break;

		c = getc(fp);
		if (c == EOF) {
			error = ferror(fp) ? errno : -1;
			break;
		}

		switch (c) {
		case '\n':		/* empty line */
			error = 0;
			break;
		case '#':		/* comment -- skip to end of line. */
			error = skip_to(fp, "\n");
			if (!error)
				(void)getc(fp);
			break;
		case '/':		/* special commands */
			error = read_mtree_command(fp);
			break;
		default:		/* specification */
			ungetc(c, fp);
			error = read_mtree_spec(fp);
			break;
		}
	} while (!error);

	endpwent();
	endgrent();

	if (error <= 0 && (errors || warnings)) {
		warnx("%u error(s) and %u warning(s) in mtree manifest",
		    errors, warnings);
		if (errors)
			exit(1);
	}

 out:
	if (error > 0)
		errc(1, error, "Error reading mtree file");

	if (fp != stdin)
		fclose(fp);

	if (mtree_root != NULL)
		return (mtree_root);

	/* Handle empty specifications. */
	node = create_node(".", S_IFDIR, NULL, &mtree_global);
	node->first = node;
	return (node);
}
Ejemplo n.º 7
0
static char *_get_pw_info(pool *p, const char *u, time_t *lstchg, time_t *min,
    time_t *max, time_t *warn, time_t *inact, time_t *expire) {
  char *cpw = NULL;
#if defined(HAVE_GETPRPWENT) || defined(COMSEC)
  struct pr_passwd *prpw;
#endif
#if !defined(HAVE_GETPRPWENT) || defined(COMSEC)
  struct passwd *pw;
#endif

 /* Some platforms (i.e. BSD) provide "transparent" shadowing, which
  * requires that we are root in order to have the password member
  * filled in.
  */

  PRIVS_ROOT
#if !defined(HAVE_GETPRPWENT) || defined(COMSEC)
# ifdef COMSEC
  if (!iscomsec()) {
# endif /* COMSEC */
  endpwent();
#if defined(BSDI3) || defined(BSDI4)
  /* endpwent() seems to be buggy on BSDI3.1 (is this true for 4.0?)
   * setpassent(0) _seems_ to do the same thing, however this conflicts
   * with the man page documented behavior.  Argh, why do all the bsds
   * have to be different in this area (except OpenBSD, grin).
   */
  setpassent(0);
#else /* BSDI3 || BSDI4 */
  setpwent();
#endif /* BSDI3 || BSDI4 */

  pw = getpwnam(u);
  if (pw) {
    cpw = pstrdup(p, pw->pw_passwd);

    if (lstchg)
      *lstchg = (time_t) -1;

    if (min)
      *min = (time_t) -1;

    if (max)
      *max = (time_t) -1;

    if (warn)
      *warn = (time_t) -1;

    if (inact)
      *inact = (time_t) -1;

    if (expire)
      *expire = (time_t) -1;
  }

  endpwent();
#ifdef COMSEC
  } else {
#endif /* COMSEC */
#endif /* !HAVE_GETPRWENT or COMSEC */

#if defined(HAVE_GETPRPWENT) || defined(COMSEC)
  endprpwent();
  setprpwent();

  prpw = getprpwnam((char *) u);

  if (prpw) {
    cpw = pstrdup(p, prpw->ufld.fd_encrypt);

    if (lstchg)
      *lstchg = (time_t) -1;

    if (min)
      *min = prpw->ufld.fd_min;

    if (max)
      *max = (time_t) -1;

    if (warn)
      *warn = (time_t) -1;

    if (inact)
      *inact = (time_t) -1;

    if (expire)
      *expire = prpw->ufld.fd_expire;
  }

  endprpwent();
#ifdef COMSEC
  }
#endif /* COMSEC */
#endif /* HAVE_GETPRPWENT or COMSEC */

  PRIVS_RELINQUISH
#if defined(BSDI3) || defined(BSDI4)
  setpassent(1);
#endif
  return cpw;
}
Ejemplo n.º 8
0
int
main(int argc, char **argv)
{
	int ch;

	/* Allow user's locale settings to affect character output. */
	setlocale(LC_CTYPE, "");

	/*
	 * Reset back to the C locale, unless we are using a known
	 * single-byte 8-bit locale.
	 */
	if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8))
		setlocale(LC_CTYPE, "C");

	oflag = 1;		/* default to old "office" behavior */

	while ((ch = getopt(argc, argv, "lmpshog8")) != -1)
		switch(ch) {
		case 'l':
			lflag = 1;		/* long format */
			break;
		case 'm':
			mflag = 1;		/* force exact match of names */
			break;
		case 'p':
			pplan = 1;		/* don't show .plan/.project */
			break;
		case 's':
			sflag = 1;		/* short format */
			break;
		case 'h':
			oflag = 0;		/* remote host info */
			break;
		case 'o':
			oflag = 1;		/* office info */
			break;
		case 'g':
			gflag = 1;		/* no gecos info, besides name */
			break;
		case '8':
			eightflag = 1;		/* 8-bit pass-through */
			break;
		case '?':
		default:
			(void)fprintf(stderr,
			    "usage: finger [-lmpshog8] [login ...]\n");
			exit(1);
		}
	argc -= optind;
	argv += optind;

	(void)time(&now);
	setpassent(1);
	entries = getutentries(NULL, &ehead);
	if (argc == 0) {
		/*
		 * Assign explicit "small" format if no names given and -l
		 * not selected.  Force the -s BEFORE we get names so proper
		 * screening will be done.
		 */
		if (!lflag)
			sflag = 1;	/* if -l not explicit, force -s */
		loginlist();
		if (entries == 0)
			(void)printf("No one logged on.\n");
	} else {
		userlist(argc, argv);
		/*
		 * Assign explicit "large" format if names given and -s not
		 * explicitly stated.  Force the -l AFTER we get names so any
		 * remote finger attempts specified won't be mishandled.
		 */
		if (!sflag)
			lflag = 1;	/* if -s not explicit, force -l */
	}
	if (entries) {
		if (lflag)
			lflag_print();
		else
			sflag_print();
	}
	return (0);
}
Ejemplo n.º 9
0
int
main(int argc, char **argv)
{
	int ch;
	int i;
#ifdef HAVE_CAP_ENTER
	int retval;
	pid_t childpid, pid;
#endif
	FILE *fp;

	while ((ch = getopt(argc, argv, "d:lnprsx")) != -1) {
		switch(ch) {
		case 'd':
			del = optarg;
			break;

		case 'l':
			oneline = 1;
			break;

		case 'n':
			oflags |= AU_OFLAG_NORESOLVE;
			break;

		case 'p':
			partial = 1;
			break;

		case 'r':
			if (oflags & AU_OFLAG_SHORT)
				usage();	/* Exclusive from shortfrm. */
			oflags |= AU_OFLAG_RAW;
			break;

		case 's':
			if (oflags & AU_OFLAG_RAW)
				usage();	/* Exclusive from raw. */
			oflags |= AU_OFLAG_SHORT;
			break;

		case 'x':
			oflags |= AU_OFLAG_XML;
			break;

		case '?':
		default:
			usage();
		}
	}

#ifdef HAVE_CAP_ENTER
	/*
	 * Prime group, password, and audit-event files to be opened before we
	 * enter capability mode.
	 */
	(void)getgrgid(0);
	(void)setgroupent(1);
	(void)getpwuid(0);
	(void)setpassent(1);
	(void)getauevent();
#endif

	if (oflags & AU_OFLAG_XML)
		au_print_xml_header(stdout);

	/* For each of the files passed as arguments dump the contents. */
	if (optind == argc) {
#ifdef HAVE_CAP_ENTER
		retval = cap_enter();
		if (retval != 0 && errno != ENOSYS)
			err(EXIT_FAILURE, "cap_enter");
#endif
		print_tokens(stdin);
		return (1);
	}
	for (i = optind; i < argc; i++) {
		fp = fopen(argv[i], "r");
		if (fp == NULL) {
			perror(argv[i]);
			continue;
		}

		/*
		 * If operating with sandboxing, create a sandbox process for
		 * each trail file we operate on.  This avoids the need to do
		 * fancy things with file descriptors, etc, when iterating on
		 * a list of arguments.
		 */
#ifdef HAVE_CAP_ENTER
		childpid = fork();
		if (childpid == 0) {
			/* Child. */
			retval = cap_enter();
			if (retval != 0 && errno != ENOSYS)
				err(EXIT_FAILURE, "cap_enter");
			if (print_tokens(fp) == -1)
				perror(argv[i]);
			exit(0);
		}

		/* Parent.  Await child termination. */
		while ((pid = waitpid(childpid, NULL, 0)) != childpid);
#else
		if (print_tokens(fp) == -1)
			perror(argv[i]);
#endif
		fclose(fp);
	}

	if (oflags & AU_OFLAG_XML)
		au_print_xml_footer(stdout);

	return (0);
}
Ejemplo n.º 10
0
Archivo: pax.c Proyecto: MirBSD/mircpio
int
main(int argc, char **argv)
{
	const char *tmpdir;
	size_t tdlen;

	/* may not be a constant, thus initialising early */
	listf = stderr;

	now = time(NULL);

	/*
	 * Keep a reference to cwd, so we can always come back home.
	 */
	cwdfd = binopen2(BO_CLEXEC, ".", O_RDONLY);
	if (cwdfd < 0) {
		syswarn(1, errno, "Cannot open current working directory.");
		return(exit_val);
	}

	/*
	 * Where should we put temporary files?
	 */
	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
		tmpdir = _PATH_TMP;
	tdlen = strlen(tmpdir);
	while (tdlen > 0 && tmpdir[tdlen - 1] == '/')
		tdlen--;
	tempfile = malloc(tdlen + 1 + sizeof(_TFILE_BASE));
	if (tempfile == NULL) {
		paxwarn(1, "%s for %s", "Out of memory",
		    "temp file name");
		return (exit_val);
	}
	if (tdlen)
		memcpy(tempfile, tmpdir, tdlen);
	tempbase = tempfile + tdlen;
	*tempbase++ = '/';

#if HAVE_SETPGENT
	/*
	 * keep passwd and group files open for faster lookups.
	 */
	setpassent(1);
	setgroupent(1);
#endif

	/*
	 * parse options, determine operational mode, general init
	 */
	options(argc, argv);
	if ((gen_init() < 0) || (tty_init() < 0))
		return(exit_val);

#if HAVE_PLEDGE
	/*
	 * pmode needs to restore setugid bits when extracting or copying,
	 * so can't pledge at all then.
	 */
	if (pmode == 0 || (act != EXTRACT && act != COPY)) {
		if (pledge("stdio rpath wpath cpath fattr dpath getpw proc exec tape",
		    NULL) == -1)
			err(1, "pledge");

		/* Copy mode, or no gzip -- don't need to fork/exec. */
		if (compress_program == NULL || act == COPY) {
			if (pledge("stdio rpath wpath cpath fattr dpath getpw tape",
			    NULL) == -1)
				err(1, "pledge");
		}
	}
#endif

	/* make list fd independent and line-buffered */
	if ((listfd = dup(fileno(listf))) < 0 ||
	    !(listf = fdopen(listfd, "wb"))) {
		syswarn(1, errno, "Cannot open list file descriptor");
		return (exit_val);
	}
	if (fcntl(listfd, F_SETFD, FD_CLOEXEC) == -1)
		syswarn(0, errno, "%s on list file descriptor",
		    "Failed to set the close-on-exec flag");
	setlinebuf(listf);

	/*
	 * select a primary operation mode
	 */
	switch (act) {
	case EXTRACT:
		extract();
		break;
	case ARCHIVE:
		archive();
		break;
	case APPND:
		if (compress_program != NULL)
			errx(1, "cannot compress while appending");
		append();
		break;
	case COPY:
		copy();
		break;
	default:
		/* for ar_io.c etc. */
		act = LIST;
		/* FALLTHROUGH */
	case LIST:
		list();
		break;
	}
	return(exit_val);
}
Ejemplo n.º 11
0
const char *
name_uid(uid_t uid, int frc)
{
	struct passwd *pw;
	UIDC *ptr;

	if ((uidtb == NULL) && (uidtb_start() < 0))
		return("");

	/*
	 * see if we have this uid cached
	 */
	ptr = uidtb[uid % UID_SZ];
	if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
		/*
		 * have an entry for this uid
		 */
		if (frc || (ptr->valid == VALID))
			return(ptr->name);
		return("");
	}

	/*
	 * No entry for this uid, we will add it
	 */
	if (!pwopn) {
		setpassent(1);
		++pwopn;
	}
	if (ptr == NULL)
		ptr = uidtb[uid % UID_SZ] = (UIDC *)malloc(sizeof(UIDC));

	if ((pw = getpwuid(uid)) == NULL) {
		/*
		 * no match for this uid in the local password file
		 * a string that is the uid in numeric format
		 */
		if (ptr == NULL)
			return("");
		ptr->uid = uid;
		ptr->valid = INVALID;
#		ifdef NET2_STAT
		(void)snprintf(ptr->name, sizeof(ptr->name), "%u", uid);
#		else
		(void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
			       (unsigned long)uid);
#		endif
		if (frc == 0)
			return("");
	} else {
		/*
		 * there is an entry for this uid in the password file
		 */
		if (ptr == NULL)
			return(pw->pw_name);
		ptr->uid = uid;
		(void)strncpy(ptr->name, pw->pw_name, UNMLEN - 1);
		ptr->name[UNMLEN-1] = '\0';
		ptr->valid = VALID;
	}
	return(ptr->name);
}
Ejemplo n.º 12
0
int
__posix_getpwnam_r(const char *login,  struct passwd *pwptr,
		char *buf, size_t buflen, struct passwd **result) {
#else
int
getpwnam_r(const char *login,  struct passwd *pwptr,
		char *buf, size_t buflen, struct passwd **result) {
#endif
	struct passwd *pw = getpwnam(login);
	int res;

	if (pw == NULL) {
		*result = NULL;
		return (0);
	}

	res = copy_passwd(pw, pwptr, buf, buflen);
	*result = res ? NULL : pwptr;
	return (res);
}

#ifdef POSIX_GETPWNAM_R
struct passwd *
getpwnam_r(const char *login,  struct passwd *pwptr, char *buf, int buflen) {
	struct passwd *pw = getpwnam(login);
	int res;

	if (pw == NULL)
		return (NULL);

	res = copy_passwd(pw, pwptr, buf, buflen);
	return (res ? NULL : pwptr);
}
#endif

/* POSIX 1003.1c */
#ifdef POSIX_GETPWUID_R
int
__posix_getpwuid_r(uid_t uid, struct passwd *pwptr,
		char *buf, size_t buflen, struct passwd **result) {
#else
int
getpwuid_r(uid_t uid, struct passwd *pwptr,
		char *buf, size_t buflen, struct passwd **result) {
#endif
	struct passwd *pw = getpwuid(uid);
	int res;

	if (pw == NULL) {
		*result = NULL;
		return (0);
	}

	res = copy_passwd(pw, pwptr, buf, buflen);
	*result = res ? NULL : pwptr;
	return (res);
}

#ifdef POSIX_GETPWUID_R
struct passwd *
getpwuid_r(uid_t uid,  struct passwd *pwptr, char *buf, int buflen) {
	struct passwd *pw = getpwuid(uid);
	int res;

	if (pw == NULL)
		return (NULL);

	res = copy_passwd(pw, pwptr, buf, buflen);
	return (res ? NULL : pwptr);
}
#endif

/*
 *	These assume a single context is in operation per thread.
 *	If this is not the case we will need to call irs directly
 *	rather than through the base functions.
 */

PASS_R_RETURN
getpwent_r(struct passwd *pwptr, PASS_R_ARGS) {
	struct passwd *pw = getpwent();
	int res;

	if (pw == NULL)
		return (PASS_R_BAD);

	res = copy_passwd(pw, pwptr, buf, buflen);
	return (res ? PASS_R_BAD : PASS_R_OK);
}

PASS_R_SET_RETURN
#ifdef PASS_R_ENT_ARGS
setpassent_r(int stayopen, PASS_R_ENT_ARGS)
#else
setpassent_r(int stayopen)
#endif
{

	setpassent(stayopen);
#ifdef PASS_R_SET_RESULT
	return (PASS_R_SET_RESULT);
#endif
}
Ejemplo n.º 13
0
main()
	{
	register int i;
	register struct passwd *pw;
	struct	itimerval it;
	u_char	c, xxx;
	int	len, tosslen;
	uid_t	uid;

	signal(SIGPIPE, SIG_DFL);
	for	(i = getdtablesize(); --i > 2; )
		close(i);
/*
 * Need a timer running while we disassociate from the control terminal
 * in case of a modem line which has lost carrier.
*/
	timerclear(&it.it_interval);
	it.it_value.tv_sec = 5;
	it.it_value.tv_usec = 0;
	signal(SIGALRM, timeout);
	setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);
	if	(setjmp(env) == 0)
		{
		i = open("/dev/tty", 0);
		if	(i >= 0)
			{
			ioctl(i, TIOCNOTTY, NULL);
			close(i);
			}
		}
/*
 * Now start a timer with one minute refresh.  In the signal service
 * routine, check the parent process id to see if this process has
 * been orphaned and if so exit.  This is primarily aimed at removing
 * the 'ctimed' process left behind by 'sendmail's multi-fork startup
 * but may prove useful in preventing accumulation of 'ctimed' processes
 * in other circumstances as well.  Normally this process is short
 * lived.
*/
	it.it_interval.tv_sec = 60;
	it.it_interval.tv_usec = 0;
	it.it_value.tv_sec = 60;
	it.it_value.tv_usec = 0;
	signal(SIGALRM, checkppid);
	setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL);

	while	(read(fileno(stdin), &c, 1) == 1)
		{
		switch	(c)
			{
			case	CTIME:
				l = 0L;
				getb(fileno(stdin), &l, sizeof l);
				cp = ctime(&l);
				write(fileno(stdout), cp, 26);
				break;
			case	ASCTIME:
				getb(fileno(stdin), &tmtmp, sizeof tmtmp);
				cp = asctime(&tmtmp);
				write(fileno(stdout), cp, 26);
				break;
			case	TZSET:
				(void) tzset();
				break;
			case	LOCALTIME:
				l = 0L;
				getb(fileno(stdin), &l, sizeof l);
				tp = localtime(&l);
				write(fileno(stdout), tp, sizeof (*tp));
				strcpy(junk, tp->tm_zone);
				junk[24] = '\0';
				write(fileno(stdout), junk, 24);
				break;
			case	GMTIME:
				l = 0L;
				getb(fileno(stdin), &l, sizeof l);
				tp = gmtime(&l);
				write(fileno(stdout), tp, sizeof (*tp));
				strcpy(junk, tp->tm_zone);
				junk[24] = '\0';
				write(fileno(stdout), junk, 24);
				break;
			case	OFFTIME:
				getb(fileno(stdin), &l, sizeof l);
				getb(fileno(stdin), &off, sizeof off);
#ifdef	__bsdi__
				l += off;
				tp = localtime(&l);
#else
				tp = offtime(&l, off);
#endif
				write(fileno(stdout), tp, sizeof (*tp));
				break;
			case	GETPWENT:
				pw = getpwent();
				do_pw(pw);
				break;
			case	GETPWNAM:
				getb(fileno(stdin), &len, sizeof (int));
				if	(len > UT_NAMESIZE)
					{
					tosslen = len - UT_NAMESIZE;
					len = UT_NAMESIZE;
					}
				else
					tosslen = 0;
				getb(fileno(stdin), junk, len);
				for	(;tosslen; tosslen--)
					getb(fileno(stdin), &xxx, 1);
				junk[len] = '\0';
				pw = getpwnam(junk);
				do_pw(pw);
				break;
			case	GETPWUID:
				getb(fileno(stdin), &uid, sizeof (uid_t));
				pw = getpwuid(uid);
				do_pw(pw);
				break;
			case	SETPASSENT:
				getb(fileno(stdin), &len, sizeof (int));
				if	(setpassent(len))
					len = 1;
				else
					len = 0;
				write(fileno(stdout), &len, sizeof (int));
				break;
			case	ENDPWENT:
				endpwent();
				break;
			default:
				abort("switch");
			}
		}
	}
Ejemplo n.º 14
0
void
setpwent(void)
{
	(void) setpassent(0);
}
Ejemplo n.º 15
0
int
main(int argc, char **argv)
{
	int envargc, argcnt;
	char *envargv[3];
	struct passwd *pw;
	static char myname[] = "finger";

	if (getuid() == 0 || geteuid() == 0) {
		invoker_root = 1;
		if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
			if (setgid(pw->pw_gid) != 0)
				err(1, "setgid()");
			if (setuid(pw->pw_uid) != 0)
				err(1, "setuid()");
		} else {
			if (setgid(UNPRIV_UGID) != 0)
				err(1, "setgid()");
			if (setuid(UNPRIV_UGID) != 0)
				err(1, "setuid()");
		}
	}

	(void) setlocale(LC_ALL, "");

				/* remove this line to get remote host */
	oflag = 1;		/* default to old "office" behavior */

	/*
	 * Process environment variables followed by command line arguments.
	 */
	if ((envargv[1] = getenv("FINGER"))) {
		envargc = 2;
		envargv[0] = myname;
		envargv[2] = NULL;
		(void) option(envargc, envargv);
	}

	argcnt = option(argc, argv);
	argc -= argcnt;
	argv += argcnt;

	(void)time(&now);
	setpassent(1);
	if (!*argv) {
		/*
		 * Assign explicit "small" format if no names given and -l
		 * not selected.  Force the -s BEFORE we get names so proper
		 * screening will be done.
		 */
		if (!lflag)
			sflag = 1;	/* if -l not explicit, force -s */
		loginlist();
		if (entries == 0)
			(void)printf("No one logged on.\n");
	} else {
		userlist(argc, argv);
		/*
		 * Assign explicit "large" format if names given and -s not
		 * explicitly stated.  Force the -l AFTER we get names so any
		 * remote finger attempts specified won't be mishandled.
		 */
		if (!sflag)
			lflag = 1;	/* if -s not explicit, force -l */
	}
	if (entries) {
		if (lflag)
			lflag_print();
		else
			sflag_print();
	}
	return (0);
}
Ejemplo n.º 16
0
static void
display(void)
{
	struct passwd *pwd;
	struct xfile *xf;
	struct sock *s;
	void *p;
	int hash, n, pos;

	printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
	    "USER", "COMMAND", "PID", "FD", "PROTO",
	    "LOCAL ADDRESS", "FOREIGN ADDRESS");
	setpassent(1);
	for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
		if (xf->xf_data == NULL)
			continue;
		hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
		for (s = sockhash[hash]; s != NULL; s = s->next)
			if ((void *)s->socket == xf->xf_data)
				break;
		if (s == NULL)
			continue;
		if (!check_ports(s))
			continue;
		pos = 0;
		if ((pwd = getpwuid(xf->xf_uid)) == NULL)
			pos += xprintf("%lu ", (u_long)xf->xf_uid);
		else
			pos += xprintf("%s ", pwd->pw_name);
		while (pos < 9)
			pos += xprintf(" ");
		pos += xprintf("%.10s", getprocname(xf->xf_pid));
		while (pos < 20)
			pos += xprintf(" ");
		pos += xprintf("%lu ", (u_long)xf->xf_pid);
		while (pos < 26)
			pos += xprintf(" ");
		pos += xprintf("%d ", xf->xf_fd);
		while (pos < 29)
			pos += xprintf(" ");
		pos += xprintf("%s", s->protoname);
		if (s->vflag & INP_IPV4)
			pos += xprintf("4 ");
		if (s->vflag & INP_IPV6)
			pos += xprintf("6 ");
		while (pos < 36)
			pos += xprintf(" ");
		switch (s->family) {
		case AF_INET:
		case AF_INET6:
			pos += printaddr(s->family, &s->laddr);
			if (s->family == AF_INET6 && pos >= 58)
				pos += xprintf(" ");
			while (pos < 58)
				pos += xprintf(" ");
			pos += printaddr(s->family, &s->faddr);
			break;
		case AF_UNIX:
			/* server */
			if (s->laddr.ss_len > 0) {
				pos += printaddr(s->family, &s->laddr);
				break;
			}
			/* client */
			p = *(void **)&s->faddr;
			if (p == NULL) {
				pos += xprintf("(not connected)");
				break;
			}
			pos += xprintf("-> ");
			for (hash = 0; hash < HASHSIZE; ++hash) {
				for (s = sockhash[hash]; s != NULL; s = s->next)
					if (s->pcb == p)
						break;
				if (s != NULL)
					break;
			}
			if (s == NULL || s->laddr.ss_len == 0)
				pos += xprintf("??");
			else
				pos += printaddr(s->family, &s->laddr);
			break;
		default:
			abort();
		}
		xprintf("\n");
	}
}
Ejemplo n.º 17
0
int
main(int argc, char *argv[])
{
	const char *logfile =
#if defined(SUPPORT_UTMPX)
	    _PATH_LASTLOGX;
#elif defined(SUPPORT_UTMP)
	    _PATH_LASTLOG;
#else
	#error "either SUPPORT_UTMP or SUPPORT_UTMPX must be defined"
#endif
	int	ch;
	size_t	len;

	while ((ch = getopt(argc, argv, "f:H:L:nN:rt")) != -1) {
		switch (ch) {
		case 'H':
			hostlen = atoi(optarg);
			break;
		case 'f':
			logfile = optarg;
			break;
		case 'L':
			linelen = atoi(optarg);
			break;
		case 'n':
			numeric++;
			break;
		case 'N':
			namelen = atoi(optarg);
			break;
		case 'r':
			sortlog |= SORT_REVERSE;
			break;
		case 't':
			sortlog |= SORT_TIME;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	len = strlen(logfile);

	setpassent(1);	/* Keep passwd file pointers open */

#if defined(SUPPORT_UTMPX)
	if (len > 0 && logfile[len - 1] == 'x')
		dolastlogx(logfile, argc, argv);
	else
#endif
#if defined(SUPPORT_UTMP)
		dolastlog(logfile, argc, argv);
#endif

	setpassent(0);	/* Close passwd file pointers */

	if (outstack && DOSORT(sortlog))
		sortoutput(outstack);

	return 0;
}