1/* -*- C -*- */ 2/* 3 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H�gskolan 4 * (Royal Institute of Technology, Stockholm, Sweden). 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of the Institute nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35/* $Id: roken.h.in,v 1.133 1999/12/30 02:22:54 assar Exp $ */ 36 37#include <stdio.h> 38#include <stdlib.h> 39#include <stdarg.h> 40#include <string.h> 41#include <signal.h> 42#ifdef HAVE_SYS_PARAM_H 43#include <sys/param.h> 44#endif 45#ifdef HAVE_SYS_TYPES_H 46#include <sys/types.h> 47#endif 48#ifdef HAVE_UNISTD_H 49#include <unistd.h> 50#endif 51#ifdef HAVE_SYS_SOCKET_H 52#include <sys/socket.h> 53#endif 54#ifdef HAVE_SYS_UIO_H 55#include <sys/uio.h> 56#endif 57#ifdef HAVE_GRP_H 58#include <grp.h> 59#endif 60#ifdef HAVE_SYS_STAT_H 61#include <sys/stat.h> 62#endif 63#ifdef HAVE_NETINET_IN_H 64#include <netinet/in.h> 65#endif 66#ifdef HAVE_NETINET_IN6_H 67#include <netinet/in6.h> 68#endif 69#ifdef HAVE_NETINET6_IN6_H 70#include <netinet6/in6.h> 71#endif 72#ifdef HAVE_ARPA_INET_H 73#include <arpa/inet.h> 74#endif 75#ifdef HAVE_NETDB_H 76#include <netdb.h> 77#endif 78#ifdef HAVE_SYSLOG_H 79#include <syslog.h> 80#endif 81#ifdef HAVE_WINSOCK_H 82#include <winsock.h> 83#endif 84#ifdef HAVE_FCNTL_H 85#include <fcntl.h> 86#endif 87#ifdef HAVE_ERRNO_H 88#include <errno.h> 89#endif 90#ifdef HAVE_ERR_H 91#include <err.h> 92#endif 93#ifdef HAVE_TERMIOS_H 94#include <termios.h> 95#endif 96#if defined(HAVE_SYS_IOCTL_H) && SunOS != 40 97#include <sys/ioctl.h> 98#endif 99#ifdef TIME_WITH_SYS_TIME 100#include <sys/time.h> 101#include <time.h> 102#elif defined(HAVE_SYS_TIME_H) 103#include <sys/time.h> 104#else 105#include <time.h> 106#endif 107 108#ifdef HAVE_PATHS_H 109#include <paths.h> 110#endif 111 112#ifdef __cplusplus 113extern "C" { 114#endif 115 116#ifndef ROKEN_LIB_FUNCTION 117#if defined(__BORLANDC__) 118#define ROKEN_LIB_FUNCTION /* not-ready-definition-yet */ 119#elif defined(_MSC_VER) 120#define ROKEN_LIB_FUNCTION /* not-ready-definition-yet2 */ 121#else 122#define ROKEN_LIB_FUNCTION 123#endif 124#endif 125 126#include <roken-common.h> 127 128#if !defined(HAVE_SETSID) && defined(HAVE__SETSID) 129#define setsid _setsid 130#endif 131 132#ifndef HAVE_PUTENV 133int putenv(const char *string); 134#endif 135 136#if !defined(HAVE_SETENV) || defined(NEED_SETENV_PROTO) 137int setenv(const char *var, const char *val, int rewrite); 138#endif 139 140#if !defined(HAVE_UNSETENV) || defined(NEED_UNSETENV_PROTO) 141void unsetenv(const char *name); 142#endif 143 144#if !defined(HAVE_GETUSERSHELL) || defined(NEED_GETUSERSHELL_PROTO) 145char *getusershell(void); 146void endusershell(void); 147#endif 148 149#if !defined(HAVE_SNPRINTF) || defined(NEED_SNPRINTF_PROTO) 150int snprintf (char *str, size_t sz, const char *format, ...) 151 __attribute__ ((format (printf, 3, 4))); 152#endif 153 154#if !defined(HAVE_VSNPRINTF) || defined(NEED_VSNPRINTF_PROTO) 155int vsnprintf (char *str, size_t sz, const char *format, va_list ap) 156 __attribute__((format (printf, 3, 0))); 157#endif 158 159#if !defined(HAVE_ASPRINTF) || defined(NEED_ASPRINTF_PROTO) 160int asprintf (char **ret, const char *format, ...) 161 __attribute__ ((format (printf, 2, 3))); 162#endif 163 164#if !defined(HAVE_VASPRINTF) || defined(NEED_VASPRINTF_PROTO) 165int vasprintf (char **ret, const char *format, va_list ap) 166 __attribute__((format (printf, 2, 0))); 167#endif 168 169#if !defined(HAVE_ASNPRINTF) || defined(NEED_ASNPRINTF_PROTO) 170int asnprintf (char **ret, size_t max_sz, const char *format, ...) 171 __attribute__ ((format (printf, 3, 4))); 172#endif 173 174#if !defined(HAVE_VASNPRINTF) || defined(NEED_VASNPRINTF_PROTO) 175int vasnprintf (char **ret, size_t max_sz, const char *format, va_list ap) 176 __attribute__((format (printf, 3, 0))); 177#endif 178 179#ifndef HAVE_STRDUP 180char * strdup(const char *old); 181#endif 182 183#ifndef HAVE_STRNDUP 184char * strndup(const char *old, size_t sz); 185#endif 186 187#ifndef HAVE_STRLWR 188char * strlwr(char *); 189#endif 190 191#ifndef HAVE_STRNLEN 192size_t strnlen(const char*, size_t); 193#endif 194 195#if !defined(HAVE_STRSEP) || defined(NEED_STRSEP_PROTO) 196char *strsep(char**, const char*); 197#endif 198 199#ifndef HAVE_STRCASECMP 200int strcasecmp(const char *s1, const char *s2); 201#endif 202 203#ifdef NEED_FCLOSE_PROTO 204int fclose(FILE *); 205#endif 206 207#ifdef NEED_STRTOK_R_PROTO 208char *strtok_r(char *s1, const char *s2, char **lasts); 209#endif 210 211#ifndef HAVE_STRUPR 212char * strupr(char *); 213#endif 214 215#ifndef HAVE_STRLCPY 216size_t strlcpy (char *dst, const char *src, size_t dst_sz); 217#endif 218 219#ifndef HAVE_STRLCAT 220size_t strlcat (char *dst, const char *src, size_t dst_sz); 221#endif 222 223#ifndef HAVE_GETDTABLESIZE 224int getdtablesize(void); 225#endif 226 227#if !defined(HAVE_STRERROR) && !defined(strerror) 228char *strerror(int eno); 229#endif 230 231#if !defined(HAVE_HSTRERROR) || defined(NEED_HSTRERROR_PROTO) 232/* This causes a fatal error under Psoriasis */ 233#if !(defined(SunOS) && (SunOS >= 50)) 234const char *hstrerror(int herr); 235#endif 236#endif 237 238#ifndef HAVE_H_ERRNO_DECLARATION 239extern int h_errno; 240#endif 241 242#if !defined(HAVE_INET_ATON) || defined(NEED_INET_ATON_PROTO) 243int inet_aton(const char *cp, struct in_addr *adr); 244#endif 245 246#ifndef HAVE_INET_NTOP 247const char * 248inet_ntop(int af, const void *src, char *dst, size_t size); 249#endif 250 251#ifndef HAVE_INET_PTON 252int 253inet_pton(int af, const char *src, void *dst); 254#endif 255 256#if !defined(HAVE_GETCWD) 257char* getcwd(char *path, size_t size); 258#endif 259 260#ifdef HAVE_PWD_H 261#include <pwd.h> 262struct passwd *k_getpwnam (const char *user); 263struct passwd *k_getpwuid (uid_t uid); 264#endif 265 266const char *get_default_username (void); 267 268#ifndef HAVE_SETEUID 269int seteuid(uid_t euid); 270#endif 271 272#ifndef HAVE_SETEGID 273int setegid(gid_t egid); 274#endif 275 276#ifndef HAVE_LSTAT 277int lstat(const char *path, struct stat *buf); 278#endif 279 280#if !defined(HAVE_MKSTEMP) || defined(NEED_MKSTEMP_PROTO) 281int mkstemp(char *); 282#endif 283 284#ifndef HAVE_CGETENT 285int cgetent(char **buf, char **db_array, const char *name); 286int cgetstr(char *buf, const char *cap, char **str); 287#endif 288 289#ifndef HAVE_INITGROUPS 290int initgroups(const char *name, gid_t basegid); 291#endif 292 293#ifndef HAVE_FCHOWN 294int fchown(int fd, uid_t owner, gid_t group); 295#endif 296 297#ifndef HAVE_DAEMON 298int daemon(int nochdir, int noclose); 299#endif 300 301#ifndef HAVE_INNETGR 302int innetgr(const char *netgroup, const char *machine, 303 const char *user, const char *domain); 304#endif 305 306#ifndef HAVE_CHOWN 307int chown(const char *path, uid_t owner, gid_t group); 308#endif 309 310#ifndef HAVE_RCMD 311int rcmd(char **ahost, unsigned short inport, const char *locuser, 312 const char *remuser, const char *cmd, int *fd2p); 313#endif 314 315#if !defined(HAVE_INNETGR) || defined(NEED_INNETGR_PROTO) 316int innetgr(const char*, const char*, const char*, const char*); 317#endif 318 319#ifndef HAVE_IRUSEROK 320int iruserok(unsigned raddr, int superuser, const char *ruser, 321 const char *luser); 322#endif 323 324#if !defined(HAVE_GETHOSTNAME) || defined(NEED_GETHOSTNAME_PROTO) 325int gethostname(char *name, int namelen); 326#endif 327 328#ifndef HAVE_WRITEV 329ssize_t 330writev(int d, const struct iovec *iov, int iovcnt); 331#endif 332 333#ifndef HAVE_READV 334ssize_t 335readv(int d, const struct iovec *iov, int iovcnt); 336#endif 337 338#ifndef HAVE_MKSTEMP 339int 340mkstemp(char *template); 341#endif 342 343#ifndef HAVE_FLOCK 344#ifndef LOCK_SH 345#define LOCK_SH 1 /* Shared lock */ 346#endif 347#ifndef LOCK_EX 348#define LOCK_EX 2 /* Exclusive lock */ 349#endif 350#ifndef LOCK_NB 351#define LOCK_NB 4 /* Don't block when locking */ 352#endif 353#ifndef LOCK_UN 354#define LOCK_UN 8 /* Unlock */ 355#endif 356 357int flock(int fd, int operation); 358#endif /* HAVE_FLOCK */ 359 360time_t tm2time (struct tm tm, int local); 361 362int unix_verify_user(char *user, char *password); 363 364void mini_inetd (int port); 365 366int roken_concat (char *s, size_t len, ...); 367 368size_t roken_mconcat (char **s, size_t max_len, ...); 369 370int roken_vconcat (char *s, size_t len, va_list args); 371 372size_t roken_vmconcat (char **s, size_t max_len, va_list args); 373 374ssize_t net_write (int fd, const void *buf, size_t nbytes); 375 376ssize_t net_read (int fd, void *buf, size_t nbytes); 377 378int issuid(void); 379 380#ifndef HAVE_STRUCT_WINSIZE 381struct winsize { 382 unsigned short ws_row, ws_col; 383 unsigned short ws_xpixel, ws_ypixel; 384}; 385#endif 386 387int get_window_size(int fd, struct winsize *); 388 389#ifndef HAVE_VSYSLOG 390void vsyslog(int pri, const char *fmt, va_list ap); 391#endif 392 393#ifndef HAVE_OPTARG_DECLARATION 394extern char *optarg; 395#endif 396#ifndef HAVE_OPTIND_DECLARATION 397extern int optind; 398#endif 399#ifndef HAVE_OPTERR_DECLARATION 400extern int opterr; 401#endif 402 403#ifndef HAVE___PROGNAME_DECLARATION 404extern const char *__progname; 405#endif 406 407#ifndef HAVE_ENVIRON_DECLARATION 408extern char **environ; 409#endif 410 411#ifndef HAVE_GETIPNODEBYNAME 412struct hostent * 413getipnodebyname (const char *name, int af, int flags, int *error_num); 414#endif 415 416#ifndef HAVE_GETIPNODEBYADDR 417struct hostent * 418getipnodebyaddr (const void *src, size_t len, int af, int *error_num); 419#endif 420 421#ifndef HAVE_FREEHOSTENT 422void 423freehostent (struct hostent *h); 424#endif 425 426#ifndef HAVE_COPYHOSTENT 427struct hostent * 428copyhostent (const struct hostent *h); 429#endif 430 431#ifndef HAVE_SOCKLEN_T 432typedef int socklen_t; 433#endif 434 435#ifndef HAVE_STRUCT_SOCKADDR_STORAGE 436 437#ifndef HAVE_SA_FAMILY_T 438typedef unsigned short sa_family_t; 439#endif 440 441#ifdef HAVE_IPV6 442#define _SS_MAXSIZE sizeof(struct sockaddr_in6) 443#else 444#define _SS_MAXSIZE sizeof(struct sockaddr_in) 445#endif 446 447#define _SS_ALIGNSIZE sizeof(unsigned long) 448 449#if HAVE_STRUCT_SOCKADDR_SA_LEN 450 451typedef unsigned char roken_sa_family_t; 452 453#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (roken_sa_family_t) - sizeof(unsigned char)) 454#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + sizeof(unsigned char) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 455 456struct sockaddr_storage { 457 unsigned char ss_len; 458 roken_sa_family_t ss_family; 459 char __ss_pad1[_SS_PAD1SIZE]; 460 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 461}; 462 463#else /* !HAVE_STRUCT_SOCKADDR_SA_LEN */ 464 465typedef unsigned short roken_sa_family_t; 466 467#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof (roken_sa_family_t)) 468#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof (roken_sa_family_t) + _SS_PAD1SIZE + _SS_ALIGNSIZE)) 469 470struct sockaddr_storage { 471 roken_sa_family_t ss_family; 472 char __ss_pad1[_SS_PAD1SIZE]; 473 unsigned long __ss_align[_SS_PAD2SIZE / sizeof(unsigned long) + 1]; 474}; 475 476#endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ 477 478#endif /* HAVE_STRUCT_SOCKADDR_STORAGE */ 479 480#ifndef HAVE_STRUCT_ADDRINFO 481struct addrinfo { 482 int ai_flags; 483 int ai_family; 484 int ai_socktype; 485 int ai_protocol; 486 size_t ai_addrlen; 487 char *ai_canonname; 488 struct sockaddr *ai_addr; 489 struct addrinfo *ai_next; 490}; 491#endif 492 493#ifndef HAVE_GETADDRINFO 494int 495getaddrinfo(const char *nodename, 496 const char *servname, 497 const struct addrinfo *hints, 498 struct addrinfo **res); 499#endif 500 501#ifndef HAVE_GETNAMEINFO 502int getnameinfo(const struct sockaddr *sa, socklen_t salen, 503 char *host, size_t hostlen, 504 char *serv, size_t servlen, 505 int flags); 506#endif 507 508#ifndef HAVE_FREEADDRINFO 509void 510freeaddrinfo(struct addrinfo *ai); 511#endif 512 513#ifndef HAVE_GAI_STRERROR 514char * 515gai_strerror(int ecode); 516#endif 517 518int 519getnameinfo_verified(const struct sockaddr *sa, socklen_t salen, 520 char *host, size_t hostlen, 521 char *serv, size_t servlen, 522 int flags); 523 524#ifndef HAVE_STRFTIME 525size_t 526strftime (char *buf, size_t maxsize, const char *format, 527 const struct tm *tm); 528#endif 529 530#ifndef HAVE_STRPTIME 531char * 532strptime (const char *buf, const char *format, struct tm *timeptr); 533#endif 534 535/* 536 * kludges and such 537 */ 538 539#if 1 540int roken_gethostby_setup(const char*, const char*); 541struct hostent* roken_gethostbyname(const char*); 542struct hostent* roken_gethostbyaddr(const void*, size_t, int); 543#else 544#ifdef GETHOSTBYNAME_PROTO_COMPATIBLE 545#define roken_gethostbyname(x) gethostbyname(x) 546#else 547#define roken_gethostbyname(x) gethostbyname((char *)x) 548#endif 549 550#ifdef GETHOSTBYADDR_PROTO_COMPATIBLE 551#define roken_gethostbyaddr(a, l, t) gethostbyaddr(a, l, t) 552#else 553#define roken_gethostbyaddr(a, l, t) gethostbyaddr((char *)a, l, t) 554#endif 555#endif 556 557#ifdef GETSERVBYNAME_PROTO_COMPATIBLE 558#define roken_getservbyname(x,y) getservbyname(x,y) 559#else 560#define roken_getservbyname(x,y) getservbyname((char *)x, (char *)y) 561#endif 562 563#ifdef OPENLOG_PROTO_COMPATIBLE 564#define roken_openlog(a,b,c) openlog(a,b,c) 565#else 566#define roken_openlog(a,b,c) openlog((char *)a,b,c) 567#endif 568 569void set_progname(char *argv0); 570 571#ifdef __cplusplus 572} 573#endif 574