Merge from vendor branch LESS:
[dragonfly.git] / crypto / heimdal / appl / rcp / util.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)util.c   8.2 (Berkeley) 4/2/94
34  * $FreeBSD: src/crypto/heimdal/appl/rcp/util.c,v 1.1.1.1.2.3 2002/09/20 10:50:16 nectar Exp $
35  * $DragonFly: src/crypto/heimdal/appl/rcp/Attic/util.c,v 1.2 2003/06/17 04:24:35 dillon Exp $
36  */
37
38 #if 0
39 #endif
40
41 #include "rcp_locl.h"
42
43 RCSID("$Id: util.c,v 1.6 2001/09/04 14:35:58 assar Exp $");
44
45 char *
46 colon(cp)
47         char *cp;
48 {
49         if (*cp == ':')         /* Leading colon is part of file name. */
50                 return (0);
51
52         for (; *cp; ++cp) {
53                 if (*cp == ':')
54                         return (cp);
55                 if (*cp == '/')
56                         return (0);
57         }
58         return (0);
59 }
60
61 void
62 verifydir(cp)
63         char *cp;
64 {
65         struct stat stb;
66
67         if (!stat(cp, &stb)) {
68                 if (S_ISDIR(stb.st_mode))
69                         return;
70                 errno = ENOTDIR;
71         }
72         run_err("%s: %s", cp, strerror(errno));
73         exit(1);
74 }
75
76 int
77 okname(cp0)
78         char *cp0;
79 {
80         int c;
81         char *cp;
82
83         cp = cp0;
84         do {
85                 c = *cp;
86                 if (c & 0200)
87                         goto bad;
88                 if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-')
89                         goto bad;
90         } while (*++cp);
91         return (1);
92
93 bad:    warnx("%s: invalid user name", cp0);
94         return (0);
95 }
96
97 int
98 susystem(s, userid)
99         int userid;
100         char *s;
101 {
102         void (*istat)(int), (*qstat)(int);
103         int status;
104         pid_t pid;
105
106         pid = fork();
107         switch (pid) {
108         case -1:
109                 return (127);
110
111         case 0:
112                 (void)setuid(userid);
113                 execl(_PATH_BSHELL, "sh", "-c", s, NULL);
114                 _exit(127);
115         }
116         istat = signal(SIGINT, SIG_IGN);
117         qstat = signal(SIGQUIT, SIG_IGN);
118         if (waitpid(pid, &status, 0) < 0)
119                 status = -1;
120         (void)signal(SIGINT, istat);
121         (void)signal(SIGQUIT, qstat);
122         return (status);
123 }
124
125 #ifndef roundup
126 #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
127 #endif
128
129 BUF *
130 allocbuf(bp, fd, blksize)
131         BUF *bp;
132         int fd, blksize;
133 {
134         struct stat stb;
135         size_t size;
136         char *p;
137
138         if (fstat(fd, &stb) < 0) {
139                 run_err("fstat: %s", strerror(errno));
140                 return (0);
141         }
142         size = roundup(stb.st_blksize, blksize);
143         if (size == 0)
144                 size = blksize;
145         if (bp->cnt >= size)
146                 return (bp);
147         if ((p = realloc(bp->buf, size)) == NULL) {
148                 if (bp->buf)
149                         free(bp->buf);
150                 bp->buf = NULL;
151                 bp->cnt = 0;
152                 run_err("%s", strerror(errno));
153                 return (0);
154         }
155         memset(p, 0, size);
156         bp->buf = p;
157         bp->cnt = size;
158         return (bp);
159 }
160
161 void
162 lostconn(signo)
163         int signo;
164 {
165         if (!iamremote)
166                 warnx("lost connection");
167         exit(1);
168 }