xref: /freebsd/crypto/heimdal/appl/rcp/util.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*-
25e9cd1aeSAssar Westerlund  * Copyright (c) 1992, 1993
35e9cd1aeSAssar Westerlund  *	The Regents of the University of California.  All rights reserved.
45e9cd1aeSAssar Westerlund  *
55e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
65e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
75e9cd1aeSAssar Westerlund  * are met:
85e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
95e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
105e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
125e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
135e9cd1aeSAssar Westerlund  * 3. All advertising materials mentioning features or use of this software
145e9cd1aeSAssar Westerlund  *    must display the following acknowledgement:
155e9cd1aeSAssar Westerlund  *	This product includes software developed by the University of
165e9cd1aeSAssar Westerlund  *	California, Berkeley and its contributors.
175e9cd1aeSAssar Westerlund  * 4. Neither the name of the University nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund #if 0
355e9cd1aeSAssar Westerlund #ifndef lint
365e9cd1aeSAssar Westerlund #if 0
375e9cd1aeSAssar Westerlund static char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94";
385e9cd1aeSAssar Westerlund #endif
395e9cd1aeSAssar Westerlund static const char rcsid[] =
405e9cd1aeSAssar Westerlund   "$FreeBSD$";
415e9cd1aeSAssar Westerlund #endif /* not lint */
425e9cd1aeSAssar Westerlund #endif
435e9cd1aeSAssar Westerlund 
445e9cd1aeSAssar Westerlund #include "rcp_locl.h"
455e9cd1aeSAssar Westerlund 
46*ae771770SStanislav Sedov RCSID("$Id$");
475e9cd1aeSAssar Westerlund 
485e9cd1aeSAssar Westerlund char *
colon(cp)495e9cd1aeSAssar Westerlund colon(cp)
505e9cd1aeSAssar Westerlund 	char *cp;
515e9cd1aeSAssar Westerlund {
525e9cd1aeSAssar Westerlund 	if (*cp == ':')		/* Leading colon is part of file name. */
535e9cd1aeSAssar Westerlund 		return (0);
545e9cd1aeSAssar Westerlund 
555e9cd1aeSAssar Westerlund 	for (; *cp; ++cp) {
565e9cd1aeSAssar Westerlund 		if (*cp == ':')
575e9cd1aeSAssar Westerlund 			return (cp);
585e9cd1aeSAssar Westerlund 		if (*cp == '/')
595e9cd1aeSAssar Westerlund 			return (0);
605e9cd1aeSAssar Westerlund 	}
615e9cd1aeSAssar Westerlund 	return (0);
625e9cd1aeSAssar Westerlund }
635e9cd1aeSAssar Westerlund 
64*ae771770SStanislav Sedov char *
unbracket(char * cp)65*ae771770SStanislav Sedov unbracket(char *cp)
66*ae771770SStanislav Sedov {
67*ae771770SStanislav Sedov 	char *ep;
68*ae771770SStanislav Sedov 
69*ae771770SStanislav Sedov 	if (*cp == '[') {
70*ae771770SStanislav Sedov 		ep = cp + (strlen(cp) - 1);
71*ae771770SStanislav Sedov 		if (*ep == ']') {
72*ae771770SStanislav Sedov 			*ep = '\0';
73*ae771770SStanislav Sedov 			++cp;
74*ae771770SStanislav Sedov 		}
75*ae771770SStanislav Sedov 	}
76*ae771770SStanislav Sedov 	return (cp);
77*ae771770SStanislav Sedov }
78*ae771770SStanislav Sedov 
795e9cd1aeSAssar Westerlund void
verifydir(cp)805e9cd1aeSAssar Westerlund verifydir(cp)
815e9cd1aeSAssar Westerlund 	char *cp;
825e9cd1aeSAssar Westerlund {
835e9cd1aeSAssar Westerlund 	struct stat stb;
845e9cd1aeSAssar Westerlund 
855e9cd1aeSAssar Westerlund 	if (!stat(cp, &stb)) {
865e9cd1aeSAssar Westerlund 		if (S_ISDIR(stb.st_mode))
875e9cd1aeSAssar Westerlund 			return;
885e9cd1aeSAssar Westerlund 		errno = ENOTDIR;
895e9cd1aeSAssar Westerlund 	}
905e9cd1aeSAssar Westerlund 	run_err("%s: %s", cp, strerror(errno));
915e9cd1aeSAssar Westerlund 	exit(1);
925e9cd1aeSAssar Westerlund }
935e9cd1aeSAssar Westerlund 
945e9cd1aeSAssar Westerlund int
okname(cp0)955e9cd1aeSAssar Westerlund okname(cp0)
965e9cd1aeSAssar Westerlund 	char *cp0;
975e9cd1aeSAssar Westerlund {
985e9cd1aeSAssar Westerlund 	int c;
99c19800e8SDoug Rabson 	unsigned char *cp;
1005e9cd1aeSAssar Westerlund 
101c19800e8SDoug Rabson 	cp = (unsigned char *)cp0;
1025e9cd1aeSAssar Westerlund 	do {
1035e9cd1aeSAssar Westerlund 		c = *cp;
1045e9cd1aeSAssar Westerlund 		if (c & 0200)
1055e9cd1aeSAssar Westerlund 			goto bad;
1065e9cd1aeSAssar Westerlund 		if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
1075e9cd1aeSAssar Westerlund 			goto bad;
1085e9cd1aeSAssar Westerlund 	} while (*++cp);
1095e9cd1aeSAssar Westerlund 	return (1);
1105e9cd1aeSAssar Westerlund 
1115e9cd1aeSAssar Westerlund bad:	warnx("%s: invalid user name", cp0);
1125e9cd1aeSAssar Westerlund 	return (0);
1135e9cd1aeSAssar Westerlund }
1145e9cd1aeSAssar Westerlund 
1155e9cd1aeSAssar Westerlund int
susystem(s)116*ae771770SStanislav Sedov susystem(s)
1175e9cd1aeSAssar Westerlund 	char *s;
1185e9cd1aeSAssar Westerlund {
1195e9cd1aeSAssar Westerlund 	void (*istat)(int), (*qstat)(int);
1205e9cd1aeSAssar Westerlund 	int status;
1215e9cd1aeSAssar Westerlund 	pid_t pid;
1225e9cd1aeSAssar Westerlund 
1235e9cd1aeSAssar Westerlund 	pid = fork();
1245e9cd1aeSAssar Westerlund 	switch (pid) {
1255e9cd1aeSAssar Westerlund 	case -1:
1265e9cd1aeSAssar Westerlund 		return (127);
1275e9cd1aeSAssar Westerlund 
1285e9cd1aeSAssar Westerlund 	case 0:
1295e9cd1aeSAssar Westerlund 		execl(_PATH_BSHELL, "sh", "-c", s, NULL);
1305e9cd1aeSAssar Westerlund 		_exit(127);
1315e9cd1aeSAssar Westerlund 	}
1325e9cd1aeSAssar Westerlund 	istat = signal(SIGINT, SIG_IGN);
1335e9cd1aeSAssar Westerlund 	qstat = signal(SIGQUIT, SIG_IGN);
1345e9cd1aeSAssar Westerlund 	if (waitpid(pid, &status, 0) < 0)
1355e9cd1aeSAssar Westerlund 		status = -1;
1365e9cd1aeSAssar Westerlund 	(void)signal(SIGINT, istat);
1375e9cd1aeSAssar Westerlund 	(void)signal(SIGQUIT, qstat);
1385e9cd1aeSAssar Westerlund 	return (status);
1395e9cd1aeSAssar Westerlund }
1405e9cd1aeSAssar Westerlund 
1415e9cd1aeSAssar Westerlund #ifndef roundup
1425e9cd1aeSAssar Westerlund #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
1435e9cd1aeSAssar Westerlund #endif
1445e9cd1aeSAssar Westerlund 
1455e9cd1aeSAssar Westerlund BUF *
allocbuf(bp,fd,blksize)1465e9cd1aeSAssar Westerlund allocbuf(bp, fd, blksize)
1475e9cd1aeSAssar Westerlund 	BUF *bp;
1485e9cd1aeSAssar Westerlund 	int fd, blksize;
1495e9cd1aeSAssar Westerlund {
1505e9cd1aeSAssar Westerlund 	struct stat stb;
1515e9cd1aeSAssar Westerlund 	size_t size;
1524137ff4cSJacques Vidrine 	char *p;
1535e9cd1aeSAssar Westerlund 
1545e9cd1aeSAssar Westerlund 	if (fstat(fd, &stb) < 0) {
1555e9cd1aeSAssar Westerlund 		run_err("fstat: %s", strerror(errno));
1565e9cd1aeSAssar Westerlund 		return (0);
1575e9cd1aeSAssar Westerlund 	}
1585e9cd1aeSAssar Westerlund 	size = roundup(stb.st_blksize, blksize);
1595e9cd1aeSAssar Westerlund 	if (size == 0)
1605e9cd1aeSAssar Westerlund 		size = blksize;
1615e9cd1aeSAssar Westerlund 	if (bp->cnt >= size)
1625e9cd1aeSAssar Westerlund 		return (bp);
1634137ff4cSJacques Vidrine 	if ((p = realloc(bp->buf, size)) == NULL) {
1644137ff4cSJacques Vidrine 		if (bp->buf)
1654137ff4cSJacques Vidrine 			free(bp->buf);
1664137ff4cSJacques Vidrine 		bp->buf = NULL;
1675e9cd1aeSAssar Westerlund 		bp->cnt = 0;
1685e9cd1aeSAssar Westerlund 		run_err("%s", strerror(errno));
1695e9cd1aeSAssar Westerlund 		return (0);
1705e9cd1aeSAssar Westerlund 	}
1714137ff4cSJacques Vidrine 	memset(p, 0, size);
1724137ff4cSJacques Vidrine 	bp->buf = p;
1735e9cd1aeSAssar Westerlund 	bp->cnt = size;
1745e9cd1aeSAssar Westerlund 	return (bp);
1755e9cd1aeSAssar Westerlund }
1765e9cd1aeSAssar Westerlund 
1775e9cd1aeSAssar Westerlund void
lostconn(signo)1785e9cd1aeSAssar Westerlund lostconn(signo)
1795e9cd1aeSAssar Westerlund 	int signo;
1805e9cd1aeSAssar Westerlund {
1815e9cd1aeSAssar Westerlund 	if (!iamremote)
1825e9cd1aeSAssar Westerlund 		warnx("lost connection");
1835e9cd1aeSAssar Westerlund 	exit(1);
1845e9cd1aeSAssar Westerlund }
185