5981107bf628d0f7a374d262a3d25f912b127e1f
* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)util.c 8.19 (Berkeley) %G%";
** STRIPQUOTES -- Strip quotes & quote bits from a string.
** Runs through a string and strips off unquoted quote
** characters and quote bits. This is done in place.
** s -- the string to strip.
** XALLOC -- Allocate memory and bitch wildly on failure.
** THIS IS A CLUDGE. This should be made to give a proper
** error -- but after all, what can we do?
** sz -- size of area to allocate.
** pointer to data region.
p
= malloc((unsigned) sz
);
syserr("Out of memory!!");
/* exit(EX_UNAVAILABLE); */
** COPYPLIST -- copy list of pointers.
** This routine is the equivalent of newstr for lists of
** list -- list of pointers to copy.
** Must be NULL terminated.
** copycont -- if TRUE, copy the contents of the vector
** (which must be a string) also.
copyplist(list
, copycont
)
for (vp
= list
; *vp
!= NULL
; vp
++)
newvp
= (char **) xalloc((int) (vp
- list
) * sizeof *vp
);
bcopy((char *) list
, (char *) newvp
, (int) (vp
- list
) * sizeof *vp
);
for (vp
= newvp
; *vp
!= NULL
; vp
++)
** COPYQUEUE -- copy address queue.
** This routine is the equivalent of newstr for address queues
** addresses marked with QDONTSEND aren't copied
** addr -- list of address structures to copy.
register ADDRESS
*newaddr
;
register ADDRESS
**tail
= &ret
;
if (!bitset(QDONTSEND
, addr
->q_flags
))
newaddr
= (ADDRESS
*) xalloc(sizeof(ADDRESS
));
STRUCTCOPY(*addr
, *newaddr
);
** PRINTAV -- print argument vector.
** av -- argument vector.
printf("\n\t%08x=", *av
);
** LOWER -- turn letter into lower case.
** c -- character to turn into lower case.
return((isascii(c
) && isupper(c
)) ? tolower(c
) : c
);
** XPUTS -- put string doing control escapes.
register struct metamac
*mp
;
extern struct metamac MetaMacros
[];
while ((c
= (*s
++ & 0377)) != '\0')
if (c
== MATCHREPL
|| c
== MACROEXPAND
)
for (mp
= MetaMacros
; mp
->metaname
!= '\0'; mp
++)
if ((mp
->metaval
& 0377) == c
)
printf("$%c", mp
->metaname
);
if (mp
->metaname
!= '\0')
/* wasn't a meta-macro -- find another way to print it */
(void) putchar(c
^ 0100);
** MAKELOWER -- Translate a line into lower case
** p -- the string to translate. If NULL, return is
** String pointed to by p is translated to lower case.
for (; (c
= *p
) != '\0'; p
++)
if (isascii(c
) && isupper(c
))
** FULLNAME -- extract full name from a passwd file entry.
** pw -- password entry to start from.
** buf -- buffer to store result in.
** TRUE -- if the resulting message should be a MIME format.
** FALSE -- if MIME is not necessary.
/* values for should_quote */
register unsigned char *gecos
;
const unsigned char *login
;
register unsigned char *bp
= buf
;
int should_quote
= NO_QUOTE
;
register char *p
= pw
->pw_gecos
;
/* make sure specials, SPACE and CTLs are quoted within " " */
for (p
= gecos
; *p
&& *p
!= ',' && *p
!= ';' && *p
!= '%'; p
++)
should_quote
= SHOULD_MIME
;
should_quote
= SHOULD_QUOTE
;
if (should_quote
== SHOULD_MIME
)
strcpy (bp
, "=?iso-8859-1?Q?");
for (p
= gecos
; *p
&& *p
!= ',' && *p
!= ';' && *p
!= '%'; p
++)
(void) strcpy(bp
, login
);
else if (*p
< 040 || *p
>= 200 ||
strchr("_?()<>@:\\\".[]", *p
) != NULL
)
*bp
++ = "0123456789ABCDEF"[(*p
>> 4) & 0xf];
*bp
++ = "0123456789ABCDEF"[*p
& 0xf];
for (p
= gecos
; *p
&& *p
!= ',' && *p
!= ';' && *p
!= '%'; p
++)
(void) strcpy(bp
, login
);
return should_quote
== SHOULD_MIME
;
** SAFEFILE -- return true if a file exists and is safe for a user.
** fn -- filename to check.
** uid -- user id to compare against.
** gid -- group id to compare against.
** uname -- user name to compare against (used for group
** SF_MUSTOWN -- "uid" must own this file.
** SF_NOSLINK -- file cannot be a symbolic link.
** mode -- mode bits that must match.
** 0 if fn exists, is owned by uid, and matches mode.
** An errno otherwise. The actual errno is cleared.
# define S_IXOTH (S_IEXEC >> 6)
# define S_IXGRP (S_IEXEC >> 3)
# define S_IXUSR (S_IEXEC)
safefile(fn
, uid
, gid
, uname
, flags
, mode
)
register struct group
*gr
= NULL
;
printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n",
fn
, uid
, gid
, flags
, mode
);
for (p
= fn
; (p
= strchr(++p
, '/')) != NULL
; *p
= '/')
if (stat(fn
, &stbuf
) < 0)
if (stbuf
.st_uid
== uid
&& bitset(S_IXUSR
, stbuf
.st_mode
))
if (stbuf
.st_gid
== gid
&& bitset(S_IXGRP
, stbuf
.st_mode
))
((gr
!= NULL
&& gr
->gr_gid
== stbuf
.st_gid
) ||
(gr
= getgrgid(stbuf
.st_gid
)) != NULL
))
for (gp
= gr
->gr_mem
; *gp
!= NULL
; gp
++)
if (strcmp(*gp
, uname
) == 0)
if (*gp
!= NULL
&& bitset(S_IXGRP
, stbuf
.st_mode
))
if (!bitset(S_IXOTH
, stbuf
.st_mode
))
printf("\t[dir %s] %s\n", fn
, errstring(ret
));
if ((bitset(SF_NOSLINK
, flags
) ? lstat(fn
, &stbuf
)
if (stat(fn
, &stbuf
) < 0)
printf("\t%s\n", errstring(ret
));
if (bitset(SF_NOSLINK
, flags
) && S_ISLNK(stbuf
.st_mode
))
printf("\t[mode %o]\tEPERM\n");
else if (stbuf
.st_uid
!= uid
)
else if (uname
!= NULL
&&
((gr
!= NULL
&& gr
->gr_gid
== stbuf
.st_gid
) ||
(gr
= getgrgid(stbuf
.st_gid
)) != NULL
))
for (gp
= gr
->gr_mem
; *gp
!= NULL
; gp
++)
if (strcmp(*gp
, uname
) == 0)
printf("\t[uid %d, stat %o, mode %o] ",
stbuf
.st_uid
, stbuf
.st_mode
, mode
);
if ((stbuf
.st_uid
== uid
|| stbuf
.st_uid
== 0 ||
!bitset(SF_MUSTOWN
, flags
)) &&
(stbuf
.st_mode
& mode
) == mode
)
** FIXCRLF -- fix <CR><LF> in line.
** Looks for the <CR><LF> combination and turns it into the
** UNIX canonical <NL> character. It only takes one line,
** i.e., it is assumed that the first <NL> found is the end
** line -- the line to fix.
** stripnl -- if true, strip the newline also.
** line is changed in place.
if (p
> line
&& p
[-1] == '\r')
** DFOPEN -- determined file open
** This routine has the semantics of fopen, except that it will
** keep trying a few times to make this happen. The idea is that
** on very loaded systems, we may run out of resources (inodes,
** whatever), so this tries to get around it.
# define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
O_ACCMODE
, O_RDONLY
, "r",
O_ACCMODE
|O_APPEND
, O_WRONLY
, "w",
O_ACCMODE
|O_APPEND
, O_WRONLY
|O_APPEND
, "a",
O_APPEND
, O_APPEND
, "a+",
dfopen(filename
, omode
, cmode
)
register struct omodes
*om
;
for (om
= OpenModes
; om
->mask
!= 0; om
++)
if ((omode
& om
->mask
) == om
->mode
)
for (tries
= 0; tries
< 10; tries
++)
sleep((unsigned) (10 * tries
));
fd
= open(filename
, omode
, cmode
);
if (errno
!= ENFILE
&& errno
!= EINTR
)
if (fd
>= 0 && fstat(fd
, &st
) >= 0 && S_ISREG(st
.st_mode
))
/* lock the file to avoid accidental conflicts */
if ((omode
& O_ACCMODE
) != O_RDONLY
)
(void) lockfile(fd
, filename
, NULL
, locktype
);
return fdopen(fd
, om
->farg
);
** PUTLINE -- put a line like fputs obeying SMTP conventions
** This routine always guarantees outputing a newline (or CRLF,
** as appropriate) at the end of the string.
** fp -- file to put it onto.
** m -- the mailer used to control output.
/* strip out 0200 bits -- these can look like TELNET protocol */
if (bitnset(M_7BITS
, m
->m_flags
))
for (p
= l
; (svchar
= *p
) != '\0'; ++p
)
if (bitset(0200, svchar
))
/* find the end of the line */
if (TrafficLogFile
!= NULL
)
fprintf(TrafficLogFile
, "%05d >>> ", getpid());
/* check for line overflow */
while (m
->m_linelimit
> 0 && (p
- l
) > m
->m_linelimit
)
register char *q
= &l
[m
->m_linelimit
- 1];
if (l
[0] == '.' && bitnset(M_XDOT
, m
->m_flags
))
if (TrafficLogFile
!= NULL
)
(void) putc('.', TrafficLogFile
);
if (TrafficLogFile
!= NULL
)
fprintf(TrafficLogFile
, "%s!\n%05d >>> ",
if (l
[0] == '.' && bitnset(M_XDOT
, m
->m_flags
))
if (TrafficLogFile
!= NULL
)
(void) putc('.', TrafficLogFile
);
if (TrafficLogFile
!= NULL
)
fprintf(TrafficLogFile
, "%.*s\n", p
- l
, l
);
** XUNLINK -- unlink a file, doing logging as appropriate.
** f -- name of file to unlink.
syslog(LOG_DEBUG
, "%s: unlink %s", CurEnv
->e_id
, f
);
if (i
< 0 && LogLevel
> 97)
syslog(LOG_DEBUG
, "%s: unlink-fail %d", f
, errno
);
** XFCLOSE -- close a file, doing logging as appropriate.
** fp -- file pointer for the file to close
** a, b -- miscellaneous crud to print for debugging
printf("xfclose(%x) %s %s\n", fp
, a
, b
);
syserr("xfclose(%s %s): fd = 1", a
, b
);
if (fclose(fp
) < 0 && tTd(53, 99))
printf("xfclose FAILURE: %s\n", errstring(errno
));
** SFGETS -- "safe" fgets -- times out and ignores random interrupts.
** buf -- place to put the input line.
** fp -- file to read from.
** timeout -- the timeout before error occurs.
** during -- what we are trying to read (for error messages).
** NULL on error (including timeout). This will also leave
** buf containing a null string.
static jmp_buf CtxReadTimeout
;
static int readtimeout();
sfgets(buf
, siz
, fp
, timeout
, during
)
register EVENT
*ev
= NULL
;
if (setjmp(CtxReadTimeout
) != 0)
"timeout waiting for input from %s during %s\n",
CurHostName
? CurHostName
: "local", during
);
usrerr("451 timeout waiting for input during %s",
ev
= setevent(timeout
, readtimeout
, 0);
while (p
== NULL
&& !feof(fp
) && !ferror(fp
))
/* clear the event if it has not sprung */
/* clean up the books and exit */
if (TrafficLogFile
!= NULL
)
fprintf(TrafficLogFile
, "%05d <<< [EOF]\n", getpid());
if (TrafficLogFile
!= NULL
)
fprintf(TrafficLogFile
, "%05d <<< %s", getpid(), buf
);
for (p
= buf
; *p
!= '\0'; p
++)
longjmp(CtxReadTimeout
, 1);
** FGETFOLDED -- like fgets, but know about folded lines.
** buf -- place to put result.
** f -- file to read from.
** input line(s) on success, NULL on error or EOF.
** This will normally be buf -- unless the line is too
** long, when it will be xalloc()ed.
** buf gets lines from f, with continuation lines (lines
** with leading white space) appended. CRLF's are mapped
** into single newlines. Any trailing NL is stripped.
while ((i
= getc(f
)) != EOF
)
if (i
!= ' ' && i
!= '\t')
** CURTIME -- return current time.
** ATOBOOL -- convert a string representation to boolean.
** s -- string to convert. Takes "tTyY" as true,
** A boolean representation of the string.
if (s
== NULL
|| *s
== '\0' || strchr("tTyY", *s
) != NULL
)
** ATOOCT -- convert a string representation to octal.
** s -- string to convert.
** An integer representing the string interpreted as an
while (*s
>= '0' && *s
<= '7')
i
= (i
<< 3) | (*s
++ - '0');
** WAITFOR -- wait for a particular process id.
** pid -- process id to wait for.
** -1 if pid never shows up.
} while ((i
>= 0 || errno
== EINTR
) && i
!= pid
);
** BITINTERSECT -- tell if two bitmaps intersect
** a, b -- the bitmaps in question
** TRUE if they have a non-null intersection
for (i
= BITMAPBYTES
/ sizeof (int); --i
>= 0; )
** BITZEROP -- tell if a bitmap is all zero
** map -- the bit map to check
** TRUE if map is all zero.
** FALSE if there are any bits set in map.
for (i
= BITMAPBYTES
/ sizeof (int); --i
>= 0; )
** STRCONTAINEDIN -- tell if one string is contained in another
** a -- possible substring.
** b -- possible superstring.
** TRUE if a is contained in b.
if (isascii(c
) && isupper(c
))
if (*b
!= c
&& isascii(*b
) && isupper(*b
) && tolower(*b
) != c
)
if (strncasecmp(a
, b
, la
) == 0)
** CHECKFD012 -- check low numbered file descriptors
** File descriptors 0, 1, and 2 should be open at all times.
** This routine verifies that, and fixes it if not true.
** where -- a tag printed if the assertion failed
if (fstat(i
, &stbuf
) < 0 && errno
!= EOPNOTSUPP
)
syserr("%s: fd %d not open", where
, i
);
fd
= open("/dev/null", i
== 0 ? O_RDONLY
: O_WRONLY
, 0666);
** PRINTOPENFDS -- print the open file descriptors (for debugging)
** logit -- if set, send output to syslog; otherwise
for (fd
= 0; fd
< DtableSize
; fd
++)
dumpfd(fd
, FALSE
, logit
);
** DUMPFD -- dump a file descriptor
** fd -- the file descriptor to dump.
** printclosed -- if set, print a notification even if
** it is closed; otherwise print nothing.
** logit -- if set, send output to syslog instead of stdout.
dumpfd(fd
, printclosed
, logit
)
register struct hostent
*hp
;
if (printclosed
|| errno
!= EBADF
)
sprintf(p
, "CANNOT STAT (%s)", errstring(errno
));
slen
= fcntl(fd
, F_GETFL
, NULL
);
sprintf(p
, "fl=0x%x, ", slen
);
sprintf(p
, "mode=%o: ", st
.st_mode
);
switch (st
.st_mode
& S_IFMT
)
if (getsockname(fd
, (struct sockaddr
*) &sin
, &slen
) < 0)
hp
= gethostbyaddr((char *) &sin
.sin_addr
, slen
, AF_INET
);
sprintf(p
, "%s/%d", hp
== NULL
? inet_ntoa(sin
.sin_addr
)
: hp
->h_name
, ntohs(sin
.sin_port
));
if (getpeername(fd
, (struct sockaddr
*) &sin
, &slen
) < 0)
hp
= gethostbyaddr((char *) &sin
.sin_addr
, slen
, AF_INET
);
sprintf(p
, "%s/%d", hp
== NULL
? inet_ntoa(sin
.sin_addr
)
: hp
->h_name
, ntohs(sin
.sin_port
));
sprintf(p
, "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld",
major(st
.st_dev
), minor(st
.st_dev
), st
.st_ino
,
st
.st_nlink
, st
.st_uid
, st
.st_gid
, st
.st_size
);
syslog(LOG_DEBUG
, "%s", buf
);