/*
* utils.c - various utility functions used in pppd.
*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, provided that the above copyright
* notice appears in all copies.
*
* SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
*
* Copyright (c) 1999 The Australian National University.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the Australian National University. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <netdb.h>
#include <utmp.h>
#include <pwd.h>
#include <sys/resource.h>
#ifdef SVR4
#endif
#include "pppd.h"
#endif
#if defined(SUNOS4)
extern char *strerror();
#endif
/* Don't log to stdout until we're sure it's ok to do so. */
static void vslp_printer __P((void *, const char *, ...));
void (*) (void *, const char *, ...), void *));
struct buffer_info {
char *ptr;
int len;
};
/*
* strllen - like strlen, but doesn't run past end of input.
*/
const char *str;
{
if (*str++ == '\0')
break;
return (ret);
}
/*
* slprintf - format a message into a buffer. Like sprintf except we
* also specify the length of the output buffer, and we handle %m
* (error message), %v (visible string), %q (quoted string), %t
* (current time), %I (IP address), %P (PPP packet), and %B (sequence
* of bytes) formats. Doesn't do floating-point formats. Returns the
* number of chars put into buf.
*/
int
{
int n;
#if defined(__STDC__)
#else
char *buf;
int buflen;
const char *fmt;
#endif
return (n);
}
/*
* Print to file or, if argument is NULL, to syslog at debug level.
*/
int
{
int n;
#if defined(__STDC__)
#else
const char *fmt;
#endif
*nlp = '\0';
}
}
} else {
}
return (n);
}
/*
* vslprintf - like slprintf, takes a va_list instead of a list of args.
*/
int
char *buf;
int buflen;
const char *fmt;
{
int c, n, longs;
#ifdef SOL2
#else
unsigned long val;
long sval;
#endif
const char *f, *str;
unsigned char *p;
time_t t;
--buflen;
while (buflen > 0) {
for (f = fmt; *f != '%' && *f != 0; ++f)
;
if (f > fmt) {
fmt = f;
}
if (*fmt == 0)
break;
c = *++fmt;
width = 0;
prec = -1;
fillch = ' ';
if (c == '0') {
fillch = '0';
c = *++fmt;
}
if (c == '*') {
c = *++fmt;
} else {
while (isdigit(c)) {
c = *++fmt;
}
}
if (c == '.') {
c = *++fmt;
if (c == '*') {
c = *++fmt;
} else {
prec = 0;
while (isdigit(c)) {
c = *++fmt;
}
}
}
longs = 0;
if (c == 'l') {
longs++;
c = *++fmt;
if (c == 'l') {
longs++;
c = *++fmt;
}
}
str = 0;
base = 0;
neg = 0;
val = 0;
++fmt;
switch (c) {
case 'u':
#ifdef SOL2
if (longs >= 2)
else
#endif
if (longs > 0)
else
base = 10;
break;
case 'd':
#ifdef SOL2
if (longs >= 2)
else
#endif
if (longs > 0)
else
if (sval < 0) {
neg = 1;
} else
base = 10;
break;
case 'o':
#ifdef SOL2
if (longs >= 2)
else
#endif
if (longs > 0)
else
base = 8;
break;
case 'x':
case 'X':
#ifdef SOL2
if (longs >= 2)
else
#endif
if (longs > 0)
else
base = 16;
break;
case 'p':
base = 16;
neg = 2;
break;
case 's':
break;
case 'c':
num[1] = 0;
break;
case 'm':
break;
case 'I':
break;
case 't':
(void) time(&t);
break;
case 'v': /* "visible" string */
case 'q': /* quoted string */
quoted = c == 'q';
n = prec;
} else {
n = strlen((char *)p);
n = prec;
}
while (n > 0 && buflen > 0) {
c = *p++;
--n;
if (!quoted && c >= 0x80) {
(void) OUTCHAR('M');
(void) OUTCHAR('-');
c -= 0x80;
}
(void) OUTCHAR('\\');
if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
if (quoted) {
(void) OUTCHAR('\\');
switch (c) {
default:
(void) OUTCHAR('x');
}
} else {
if (c == '\t')
(void) OUTCHAR(c);
else {
(void) OUTCHAR('^');
(void) OUTCHAR(c ^ 0x40);
}
}
} else
(void) OUTCHAR(c);
}
continue;
case 'P': /* print PPP packet */
continue;
case 'B':
n = width;
/* For safety's sake */
if (n > 2000)
n = 2000;
while (--n >= 0) {
c = *p++;
if (fillch == ' ')
(void) OUTCHAR(' ');
}
(void) OUTCHAR('.');
(void) OUTCHAR('.');
(void) OUTCHAR('.');
}
continue;
default:
*buf++ = '%';
if (c != '%')
--fmt; /* so %z outputs %z etc. */
--buflen;
continue;
}
if (base != 0) {
*--mstr = 0;
break;
}
switch (neg) {
case 1:
*--mstr = '-';
break;
case 2:
*--mstr = 'x';
*--mstr = '0';
break;
}
} else {
}
if (width > 0) {
buflen -= n;
for (; n > 0; --n)
}
}
}
*buf = 0;
}
/*
* vslp_printer - used in processing a %P format
*/
static void
{
int n;
#if defined(__STDC__)
#else
void *arg;
const char *fmt;
#endif
}
/*
* log_packet - format a packet and log it.
*/
static char *linep;
void
u_char *p;
int len;
const char *prefix;
int level;
{
}
/*
* format_packet - make a readable representation of a packet,
* calling `printer(arg, format, ...)' to output it.
*/
static void
u_char *p;
int len;
void *arg;
{
int i, n;
p += 2;
len -= PPP_HDRLEN;
break;
p += n;
len -= n;
} else {
break;
len = 0;
} else
}
}
}
static void
{
int n;
#if defined(__STDC__)
#else
void *arg;
const char *fmt;
#endif
}
linep += n;
}
/*
* print_string - print a readable representation of a string using
* printer.
*/
void
char *p;
int len;
void *arg;
{
int c;
c = *p++;
if (isprint(c)) {
if (c == '\\' || c == '"')
} else {
switch (c) {
case '\n':
break;
case '\r':
break;
case '\t':
break;
default:
}
}
}
}
/*
* logit - does the hard work for fatal et al.
*/
static void
int level;
const char *fmt;
{
int n;
(!early_log || log_to_specific_fd)) {
buf[n++] = '\n';
log_to_fd = -1;
}
}
/*
* fatal - log an error message and die horribly.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* error - log an error message.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* warn - log a warning message.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* notice - log a notice-level message.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* info - log an informational message.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* dbglog - log a debug message.
*/
void
{
#if defined(__STDC__)
#else
const char *fmt;
#endif
}
/*
* Code names for regular PPP messages. Used by LCP and most NCPs,
* not used by authentication protocols.
*/
const char *
{
static const char *codelist[] = {
"Vendor-Extension", "Configure-Request", "Configure-Ack",
"Configure-Nak", "Configure-Reject", "Terminate-Request",
"Terminate-Ack", "Code-Reject", "Protocol-Reject",
"Echo-Request", "Echo-Reply", "Discard-Request",
"Identification", "Time-Remaining",
"Reset-Request", "Reset-Ack"
};
static const char *shortcode[] = {
"VendExt", "ConfReq", "ConfAck",
"ConfNak", "ConfRej", "TermReq",
"TermAck", "CodeRej", "ProtRej",
"EchoReq", "EchoRep", "DiscReq",
"Ident", "TimeRem",
"ResetReq", "ResetAck"
};
if (shortflag)
else
return ((const char *)msgbuf);
}
}
/* Procedures for locking the serial device using a lock file. */
#ifndef LOCK_DIR
#ifdef _linux_
#else
#ifdef SVR4
#else
#endif
#endif
#endif /* LOCK_DIR */
/*
* lock - create a lock file for the named device
*/
int
char *dev;
{
#ifdef LOCKLIB
int result;
if (result == 0) {
return (0);
}
if (result > 0)
else
return (-1);
#else /* LOCKLIB */
char lock_buffer[12];
#ifdef SVR4
return (-1);
}
return (-1);
}
#else
char *p;
dev = p + 1;
#endif
break;
}
/* Read the lock file to find out who has the device locked. */
if (fd < 0) {
continue;
break;
}
#ifndef LOCK_BINARY
#else
#endif /* LOCK_BINARY */
fd = -1;
if (n <= 0) {
break;
}
/* See if the process still exists. */
#ifndef LOCK_BINARY
lock_buffer[n] = 0;
#endif /* LOCK_BINARY */
return (1); /* somebody else locked it for us */
if (pid == 0
continue;
}
} else
break;
}
if (fd < 0) {
lock_file[0] = 0;
return (-1);
}
#ifndef LOCK_BINARY
#else
#endif
return (0);
#endif
}
/*
* relock - called to update our lockfile when we are about to detach,
* thus changing our pid (we fork, the child carries on, and the parent dies).
* Note that this is called by the parent, with pid equal to the pid
* of the child. This avoids a potential race which would exist if
* we had the child rewrite the lockfile (the parent might die first,
* and another process could think the lock was stale if it checked
* between when the parent died and the child rewrote the lockfile).
*/
int
int pid;
{
#ifdef LOCKLIB
/* XXX is there a way to do this? */
return (-1);
#else /* LOCKLIB */
int fd;
char lock_buffer[12];
if (lock_file[0] == 0)
return (-1);
if (fd < 0) {
lock_file[0] = 0;
return (-1);
}
#ifndef LOCK_BINARY
#else
#endif /* LOCK_BINARY */
return (0);
#endif /* LOCKLIB */
}
/*
* unlock - remove our lockfile
*/
void
unlock()
{
if (lock_file[0]) {
#ifdef LOCKLIB
#else
#endif
lock_file[0] = 0;
}
}
const char *
{
const char *cp;
return (cp);
#else
extern char *sys_siglist[];
extern int sys_nsig;
return (sys_siglist[signum]);
#endif
return ("??");
}