summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/crypt/Makefile.inc12
-rw-r--r--src/lib/libc/crypt/arc4random.383
-rw-r--r--src/lib/libc/crypt/arc4random.c176
-rw-r--r--src/lib/libc/crypt/bcrypt.c362
-rw-r--r--src/lib/libc/crypt/blowfish.3104
-rw-r--r--src/lib/libc/crypt/blowfish.c774
-rw-r--r--src/lib/libc/crypt/cast.c779
-rw-r--r--src/lib/libc/crypt/crypt.3298
-rw-r--r--src/lib/libc/crypt/crypt.c714
-rw-r--r--src/lib/libc/crypt/md5crypt.c157
-rw-r--r--src/lib/libc/crypt/morecrypt.c628
-rw-r--r--src/lib/libc/include/namespace.h2
-rw-r--r--src/lib/libc/net/Makefile.inc40
-rw-r--r--src/lib/libc/net/base64.c317
-rw-r--r--src/lib/libc/net/byteorder.3115
-rw-r--r--src/lib/libc/net/ethers.325
-rw-r--r--src/lib/libc/net/ethers.c200
-rw-r--r--src/lib/libc/net/gethostbyname.380
-rw-r--r--src/lib/libc/net/gethostnamadr.c799
-rw-r--r--src/lib/libc/net/getnetbyaddr.c14
-rw-r--r--src/lib/libc/net/getnetbyname.c16
-rw-r--r--src/lib/libc/net/getnetent.337
-rw-r--r--src/lib/libc/net/getnetent.c34
-rw-r--r--src/lib/libc/net/getnetnamadr.c382
-rw-r--r--src/lib/libc/net/getproto.c10
-rw-r--r--src/lib/libc/net/getprotoent.34
-rw-r--r--src/lib/libc/net/getprotoent.c26
-rw-r--r--src/lib/libc/net/getprotoname.c10
-rw-r--r--src/lib/libc/net/getservbyname.c10
-rw-r--r--src/lib/libc/net/getservbyport.c10
-rw-r--r--src/lib/libc/net/getservent.36
-rw-r--r--src/lib/libc/net/getservent.c28
-rw-r--r--src/lib/libc/net/herror.c38
-rw-r--r--src/lib/libc/net/htonl.c18
-rw-r--r--src/lib/libc/net/htons.c16
-rw-r--r--src/lib/libc/net/inet.3182
-rw-r--r--src/lib/libc/net/inet_addr.c85
-rw-r--r--src/lib/libc/net/inet_lnaof.c12
-rw-r--r--src/lib/libc/net/inet_makeaddr.c12
-rw-r--r--src/lib/libc/net/inet_net.3149
-rw-r--r--src/lib/libc/net/inet_net_ntop.c139
-rw-r--r--src/lib/libc/net/inet_net_pton.c207
-rw-r--r--src/lib/libc/net/inet_neta.c83
-rw-r--r--src/lib/libc/net/inet_netof.c12
-rw-r--r--src/lib/libc/net/inet_network.c25
-rw-r--r--src/lib/libc/net/inet_ntoa.c8
-rw-r--r--src/lib/libc/net/inet_ntop.c194
-rw-r--r--src/lib/libc/net/inet_pton.c220
-rw-r--r--src/lib/libc/net/ipx.3126
-rw-r--r--src/lib/libc/net/ipx_addr.c229
-rw-r--r--src/lib/libc/net/ipx_ntoa.c58
-rw-r--r--src/lib/libc/net/iso_addr.34
-rw-r--r--src/lib/libc/net/iso_addr.c8
-rw-r--r--src/lib/libc/net/link_addr.3 (renamed from src/lib/libc/net/linkaddr.3)4
-rw-r--r--src/lib/libc/net/linkaddr.c8
-rw-r--r--src/lib/libc/net/ns.34
-rw-r--r--src/lib/libc/net/ns_addr.c15
-rw-r--r--src/lib/libc/net/ns_ntoa.c17
-rw-r--r--src/lib/libc/net/nsap_addr.c109
-rw-r--r--src/lib/libc/net/ntohl.c18
-rw-r--r--src/lib/libc/net/ntohs.c16
-rw-r--r--src/lib/libc/net/rcmd.339
-rw-r--r--src/lib/libc/net/rcmd.c268
-rw-r--r--src/lib/libc/net/rcmdsh.3 (renamed from src/lib/libc/stdlib/realloc.3)134
-rw-r--r--src/lib/libc/net/rcmdsh.c124
-rw-r--r--src/lib/libc/net/recv.c8
-rw-r--r--src/lib/libc/net/res_comp.c224
-rw-r--r--src/lib/libc/net/res_data.c117
-rw-r--r--src/lib/libc/net/res_debug.c1443
-rw-r--r--src/lib/libc/net/res_init.c290
-rw-r--r--src/lib/libc/net/res_mkquery.c173
-rw-r--r--src/lib/libc/net/res_query.c190
-rw-r--r--src/lib/libc/net/res_random.c233
-rw-r--r--src/lib/libc/net/res_send.c738
-rw-r--r--src/lib/libc/net/resolver.311
-rw-r--r--src/lib/libc/net/send.c8
-rw-r--r--src/lib/libc/net/sethostent.c14
-rw-r--r--src/lib/libc/stdlib/Makefile.inc33
-rw-r--r--src/lib/libc/stdlib/_rand48.c4
-rw-r--r--src/lib/libc/stdlib/a64l.3122
-rw-r--r--src/lib/libc/stdlib/a64l.c28
-rw-r--r--src/lib/libc/stdlib/abort.39
-rw-r--r--src/lib/libc/stdlib/abort.c16
-rw-r--r--src/lib/libc/stdlib/abs.33
-rw-r--r--src/lib/libc/stdlib/abs.c3
-rw-r--r--src/lib/libc/stdlib/alloca.36
-rw-r--r--src/lib/libc/stdlib/atexit.33
-rw-r--r--src/lib/libc/stdlib/atexit.c3
-rw-r--r--src/lib/libc/stdlib/atexit.h3
-rw-r--r--src/lib/libc/stdlib/atof.33
-rw-r--r--src/lib/libc/stdlib/atof.c3
-rw-r--r--src/lib/libc/stdlib/atoi.310
-rw-r--r--src/lib/libc/stdlib/atoi.c3
-rw-r--r--src/lib/libc/stdlib/atol.33
-rw-r--r--src/lib/libc/stdlib/atol.c3
-rw-r--r--src/lib/libc/stdlib/bsearch.313
-rw-r--r--src/lib/libc/stdlib/bsearch.c3
-rw-r--r--src/lib/libc/stdlib/calloc.35
-rw-r--r--src/lib/libc/stdlib/calloc.c6
-rw-r--r--src/lib/libc/stdlib/cfree.c49
-rw-r--r--src/lib/libc/stdlib/div.33
-rw-r--r--src/lib/libc/stdlib/div.c3
-rw-r--r--src/lib/libc/stdlib/drand48.c4
-rw-r--r--src/lib/libc/stdlib/erand48.c4
-rw-r--r--src/lib/libc/stdlib/exit.33
-rw-r--r--src/lib/libc/stdlib/exit.c3
-rw-r--r--src/lib/libc/stdlib/free.382
-rw-r--r--src/lib/libc/stdlib/getenv.313
-rw-r--r--src/lib/libc/stdlib/getenv.c65
-rw-r--r--src/lib/libc/stdlib/getopt.319
-rw-r--r--src/lib/libc/stdlib/getopt.c3
-rw-r--r--src/lib/libc/stdlib/getsubopt.3148
-rw-r--r--src/lib/libc/stdlib/getsubopt.c106
-rw-r--r--src/lib/libc/stdlib/heapsort.c3
-rw-r--r--src/lib/libc/stdlib/jrand48.c4
-rw-r--r--src/lib/libc/stdlib/l64a.c15
-rw-r--r--src/lib/libc/stdlib/labs.33
-rw-r--r--src/lib/libc/stdlib/labs.c3
-rw-r--r--src/lib/libc/stdlib/lcong48.c4
-rw-r--r--src/lib/libc/stdlib/ldiv.33
-rw-r--r--src/lib/libc/stdlib/ldiv.c3
-rw-r--r--src/lib/libc/stdlib/lrand48.c4
-rw-r--r--src/lib/libc/stdlib/malloc.3297
-rw-r--r--src/lib/libc/stdlib/malloc.c1529
-rw-r--r--src/lib/libc/stdlib/memory.33
-rw-r--r--src/lib/libc/stdlib/merge.c7
-rw-r--r--src/lib/libc/stdlib/mrand48.c4
-rw-r--r--src/lib/libc/stdlib/multibyte.c3
-rw-r--r--src/lib/libc/stdlib/nrand48.c4
-rw-r--r--src/lib/libc/stdlib/putenv.c19
-rw-r--r--src/lib/libc/stdlib/qabs.33
-rw-r--r--src/lib/libc/stdlib/qabs.c3
-rw-r--r--src/lib/libc/stdlib/qdiv.33
-rw-r--r--src/lib/libc/stdlib/qdiv.c3
-rw-r--r--src/lib/libc/stdlib/qsort.33
-rw-r--r--src/lib/libc/stdlib/qsort.c34
-rw-r--r--src/lib/libc/stdlib/radixsort.310
-rw-r--r--src/lib/libc/stdlib/radixsort.c7
-rw-r--r--src/lib/libc/stdlib/rand.38
-rw-r--r--src/lib/libc/stdlib/rand.c3
-rw-r--r--src/lib/libc/stdlib/rand48.33
-rw-r--r--src/lib/libc/stdlib/rand48.h2
-rw-r--r--src/lib/libc/stdlib/random.326
-rw-r--r--src/lib/libc/stdlib/random.c48
-rw-r--r--src/lib/libc/stdlib/realpath.35
-rw-r--r--src/lib/libc/stdlib/realpath.c10
-rw-r--r--src/lib/libc/stdlib/seed48.c4
-rw-r--r--src/lib/libc/stdlib/setenv.c8
-rw-r--r--src/lib/libc/stdlib/srand48.c4
-rw-r--r--src/lib/libc/stdlib/strtod.33
-rw-r--r--src/lib/libc/stdlib/strtod.c236
-rw-r--r--src/lib/libc/stdlib/strtol.37
-rw-r--r--src/lib/libc/stdlib/strtol.c70
-rw-r--r--src/lib/libc/stdlib/strtoq.c60
-rw-r--r--src/lib/libc/stdlib/strtoul.33
-rw-r--r--src/lib/libc/stdlib/strtoul.c44
-rw-r--r--src/lib/libc/stdlib/strtouq.c36
-rw-r--r--src/lib/libc/stdlib/system.311
-rw-r--r--src/lib/libc/stdlib/system.c7
-rw-r--r--src/lib/libc/stdlib/tfind.c41
-rw-r--r--src/lib/libc/stdlib/tsearch.3116
-rw-r--r--src/lib/libc/stdlib/tsearch.c126
-rw-r--r--src/lib/libc/string/Makefile.inc30
-rw-r--r--src/lib/libc/string/__strerror.c35
-rw-r--r--src/lib/libc/string/__strsignal.c32
-rw-r--r--src/lib/libc/string/bcmp.33
-rw-r--r--src/lib/libc/string/bcmp.c8
-rw-r--r--src/lib/libc/string/bcopy.33
-rw-r--r--src/lib/libc/string/bcopy.c3
-rw-r--r--src/lib/libc/string/bm.33
-rw-r--r--src/lib/libc/string/bm.c7
-rw-r--r--src/lib/libc/string/bstring.33
-rw-r--r--src/lib/libc/string/bzero.33
-rw-r--r--src/lib/libc/string/bzero.c7
-rw-r--r--src/lib/libc/string/ffs.33
-rw-r--r--src/lib/libc/string/ffs.c7
-rw-r--r--src/lib/libc/string/index.39
-rw-r--r--src/lib/libc/string/index.c3
-rw-r--r--src/lib/libc/string/memccpy.316
-rw-r--r--src/lib/libc/string/memccpy.c13
-rw-r--r--src/lib/libc/string/memchr.33
-rw-r--r--src/lib/libc/string/memchr.c3
-rw-r--r--src/lib/libc/string/memcmp.35
-rw-r--r--src/lib/libc/string/memcmp.c3
-rw-r--r--src/lib/libc/string/memcpy.33
-rw-r--r--src/lib/libc/string/memmove.33
-rw-r--r--src/lib/libc/string/memset.39
-rw-r--r--src/lib/libc/string/memset.c3
-rw-r--r--src/lib/libc/string/rindex.35
-rw-r--r--src/lib/libc/string/rindex.c3
-rw-r--r--src/lib/libc/string/strcasecmp.315
-rw-r--r--src/lib/libc/string/strcasecmp.c17
-rw-r--r--src/lib/libc/string/strcat.310
-rw-r--r--src/lib/libc/string/strcat.c9
-rw-r--r--src/lib/libc/string/strchr.35
-rw-r--r--src/lib/libc/string/strcmp.33
-rw-r--r--src/lib/libc/string/strcmp.c7
-rw-r--r--src/lib/libc/string/strcoll.33
-rw-r--r--src/lib/libc/string/strcoll.c3
-rw-r--r--src/lib/libc/string/strcpy.33
-rw-r--r--src/lib/libc/string/strcpy.c9
-rw-r--r--src/lib/libc/string/strcspn.33
-rw-r--r--src/lib/libc/string/strcspn.c3
-rw-r--r--src/lib/libc/string/strdup.321
-rw-r--r--src/lib/libc/string/strdup.c26
-rw-r--r--src/lib/libc/string/strerror.33
-rw-r--r--src/lib/libc/string/strerror.c3
-rw-r--r--src/lib/libc/string/strftime.3202
-rw-r--r--src/lib/libc/string/strftime.c317
-rw-r--r--src/lib/libc/string/string.35
-rw-r--r--src/lib/libc/string/strlcat.c71
-rw-r--r--src/lib/libc/string/strlcpy.3140
-rw-r--r--src/lib/libc/string/strlcpy.c63
-rw-r--r--src/lib/libc/string/strlen.33
-rw-r--r--src/lib/libc/string/strlen.c7
-rw-r--r--src/lib/libc/string/strmode.333
-rw-r--r--src/lib/libc/string/strmode.c8
-rw-r--r--src/lib/libc/string/strncat.c3
-rw-r--r--src/lib/libc/string/strncmp.c7
-rw-r--r--src/lib/libc/string/strncpy.c3
-rw-r--r--src/lib/libc/string/strpbrk.33
-rw-r--r--src/lib/libc/string/strpbrk.c3
-rw-r--r--src/lib/libc/string/strrchr.33
-rw-r--r--src/lib/libc/string/strsep.378
-rw-r--r--src/lib/libc/string/strsep.c22
-rw-r--r--src/lib/libc/string/strsignal.33
-rw-r--r--src/lib/libc/string/strsignal.c3
-rw-r--r--src/lib/libc/string/strspn.35
-rw-r--r--src/lib/libc/string/strspn.c3
-rw-r--r--src/lib/libc/string/strstr.33
-rw-r--r--src/lib/libc/string/strstr.c3
-rw-r--r--src/lib/libc/string/strtok.36
-rw-r--r--src/lib/libc/string/strtok.c3
-rw-r--r--src/lib/libc/string/strxfrm.33
-rw-r--r--src/lib/libc/string/strxfrm.c3
-rw-r--r--src/lib/libc/string/swab.35
-rw-r--r--src/lib/libc/string/swab.c5
-rw-r--r--src/lib/libcrypto/Makefile.ssl161
-rw-r--r--src/lib/libcrypto/asn1/Makefile.ssl120
-rw-r--r--src/lib/libcrypto/asn1/f.c80
-rw-r--r--src/lib/libcrypto/asn1/x_cinf.c197
-rw-r--r--src/lib/libcrypto/bf/Makefile.ssl107
-rw-r--r--src/lib/libcrypto/bio/Makefile.ssl92
-rw-r--r--src/lib/libcrypto/bn/Makefile.ssl133
-rw-r--r--src/lib/libcrypto/buffer/Makefile.ssl84
-rw-r--r--src/lib/libcrypto/cast/Makefile.ssl109
-rw-r--r--src/lib/libcrypto/conf/Makefile.ssl85
-rw-r--r--src/lib/libcrypto/des/FILES96
-rw-r--r--src/lib/libcrypto/des/Makefile.ssl140
-rw-r--r--src/lib/libcrypto/dh/Makefile.ssl84
-rw-r--r--src/lib/libcrypto/dsa/Makefile.ssl84
-rw-r--r--src/lib/libcrypto/err/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/evp/Makefile.ssl111
-rw-r--r--src/lib/libcrypto/hmac/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/idea/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/lhash/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/md2/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/md5/Makefile.ssl104
-rw-r--r--src/lib/libcrypto/mdc2/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/objects/Makefile.ssl87
-rw-r--r--src/lib/libcrypto/pem/Makefile.ssl96
-rw-r--r--src/lib/libcrypto/pkcs7/Makefile.ssl86
-rw-r--r--src/lib/libcrypto/rand/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/rc2/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/rc4/Makefile.ssl108
-rw-r--r--src/lib/libcrypto/rc5/Makefile.ssl107
-rw-r--r--src/lib/libcrypto/rc5/asm/rc5-586.pl109
-rw-r--r--src/lib/libcrypto/ripemd/Makefile.ssl104
-rw-r--r--src/lib/libcrypto/rsa/Makefile.ssl86
-rw-r--r--src/lib/libcrypto/sha/Makefile.ssl103
-rw-r--r--src/lib/libcrypto/stack/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/txt_db/Makefile.ssl80
-rw-r--r--src/lib/libcrypto/x509/Makefile.ssl96
-rw-r--r--src/lib/libssl/src/Makefile.ssl331
-rw-r--r--src/lib/libssl/src/apps/Makefile.ssl144
-rw-r--r--src/lib/libssl/src/apps/der_chop305
-rw-r--r--src/lib/libssl/src/certs/ICE-CA.pem59
-rw-r--r--src/lib/libssl/src/certs/ICE-root.pem48
-rw-r--r--src/lib/libssl/src/certs/ICE-user.pem63
-rw-r--r--src/lib/libssl/src/certs/ICE.crl9
-rw-r--r--src/lib/libssl/src/certs/ca-cert.pem31
-rw-r--r--src/lib/libssl/src/certs/dsa-ca.pem43
-rw-r--r--src/lib/libssl/src/certs/dsa-pca.pem49
-rw-r--r--src/lib/libssl/src/certs/factory.pem15
-rw-r--r--src/lib/libssl/src/certs/nortelCA.pem16
-rw-r--r--src/lib/libssl/src/certs/pca-cert.pem31
-rw-r--r--src/lib/libssl/src/certs/rsa-cca.pem19
-rw-r--r--src/lib/libssl/src/certs/timCA.pem16
-rw-r--r--src/lib/libssl/src/certs/tjhCA.pem15
-rw-r--r--src/lib/libssl/src/certs/vsign2.pem31
-rw-r--r--src/lib/libssl/src/crypto/Makefile.ssl161
-rw-r--r--src/lib/libssl/src/crypto/asn1/Makefile.ssl120
-rw-r--r--src/lib/libssl/src/crypto/asn1/f.c80
-rw-r--r--src/lib/libssl/src/crypto/asn1/x_cinf.c197
-rw-r--r--src/lib/libssl/src/crypto/bf/Makefile.ssl107
-rw-r--r--src/lib/libssl/src/crypto/bio/Makefile.ssl92
-rw-r--r--src/lib/libssl/src/crypto/bn/Makefile.ssl133
-rw-r--r--src/lib/libssl/src/crypto/buffer/Makefile.ssl84
-rw-r--r--src/lib/libssl/src/crypto/cast/Makefile.ssl109
-rw-r--r--src/lib/libssl/src/crypto/conf/Makefile.ssl85
-rw-r--r--src/lib/libssl/src/crypto/des/FILES96
-rw-r--r--src/lib/libssl/src/crypto/des/Makefile.ssl140
-rw-r--r--src/lib/libssl/src/crypto/dh/Makefile.ssl84
-rw-r--r--src/lib/libssl/src/crypto/dsa/Makefile.ssl84
-rw-r--r--src/lib/libssl/src/crypto/err/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/evp/Makefile.ssl111
-rw-r--r--src/lib/libssl/src/crypto/hmac/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/idea/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/lhash/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/md2/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/md5/Makefile.ssl104
-rw-r--r--src/lib/libssl/src/crypto/mdc2/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/objects/Makefile.ssl87
-rw-r--r--src/lib/libssl/src/crypto/pem/Makefile.ssl96
-rw-r--r--src/lib/libssl/src/crypto/pkcs7/Makefile.ssl86
-rw-r--r--src/lib/libssl/src/crypto/rand/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/rc2/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/rc4/Makefile.ssl108
-rw-r--r--src/lib/libssl/src/crypto/rc5/Makefile.ssl107
-rw-r--r--src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl109
-rw-r--r--src/lib/libssl/src/crypto/ripemd/Makefile.ssl104
-rw-r--r--src/lib/libssl/src/crypto/rsa/Makefile.ssl86
-rw-r--r--src/lib/libssl/src/crypto/sha/Makefile.ssl103
-rw-r--r--src/lib/libssl/src/crypto/stack/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/txt_db/Makefile.ssl80
-rw-r--r--src/lib/libssl/src/crypto/x509/Makefile.ssl96
-rw-r--r--src/lib/libssl/src/ssl/Makefile.ssl100
-rw-r--r--src/lib/libssl/src/test/Makefile.ssl294
-rw-r--r--src/lib/libssl/src/tools/Makefile.ssl54
-rw-r--r--src/lib/libssl/test/Makefile.ssl294
-rw-r--r--src/regress/lib/libc/db/Makefile4
-rw-r--r--src/regress/lib/libc/db/README36
-rw-r--r--src/regress/lib/libc/db/dbtest.c231
-rw-r--r--src/regress/lib/libc/db/run.test35
-rw-r--r--src/regress/lib/libc/ieeefp/Makefile6
-rw-r--r--src/regress/lib/libc/ieeefp/except/except.c1
-rw-r--r--src/regress/lib/libc/regex/debug.c2
-rw-r--r--src/regress/lib/libc/regex/main.c11
-rw-r--r--src/regress/lib/libc/setjmp/jmptest.c3
-rw-r--r--src/usr.bin/nc/Makefile7
-rw-r--r--src/usr.bin/nc/README946
-rw-r--r--src/usr.bin/nc/data/Makefile10
-rw-r--r--src/usr.bin/nc/data/README9
-rw-r--r--src/usr.bin/nc/data/data.c274
-rw-r--r--src/usr.bin/nc/data/dns-any.d36
-rw-r--r--src/usr.bin/nc/data/nfs-0.d59
-rw-r--r--src/usr.bin/nc/data/pm.d8
-rw-r--r--src/usr.bin/nc/data/pmap-dump.d60
-rw-r--r--src/usr.bin/nc/data/pmap-mnt.d78
-rw-r--r--src/usr.bin/nc/data/rip.d52
-rw-r--r--src/usr.bin/nc/data/rservice.c68
-rw-r--r--src/usr.bin/nc/data/showmount.d63
-rw-r--r--src/usr.bin/nc/data/xor.c92
-rw-r--r--src/usr.bin/nc/generic.h377
-rw-r--r--src/usr.bin/nc/nc.1215
-rw-r--r--src/usr.bin/nc/netcat.blurb61
-rw-r--r--src/usr.bin/nc/netcat.c1670
-rw-r--r--src/usr.bin/nc/scripts/README5
-rw-r--r--src/usr.bin/nc/scripts/alta33
-rw-r--r--src/usr.bin/nc/scripts/bsh29
-rw-r--r--src/usr.bin/nc/scripts/dist.sh23
-rw-r--r--src/usr.bin/nc/scripts/irc79
-rw-r--r--src/usr.bin/nc/scripts/iscan35
-rw-r--r--src/usr.bin/nc/scripts/ncp46
-rw-r--r--src/usr.bin/nc/scripts/probe50
-rw-r--r--src/usr.bin/nc/scripts/web148
-rw-r--r--src/usr.bin/nc/scripts/webproxy138
-rw-r--r--src/usr.bin/nc/scripts/webrelay44
-rw-r--r--src/usr.bin/nc/scripts/websearch77
369 files changed, 28066 insertions, 3474 deletions
diff --git a/src/lib/libc/crypt/Makefile.inc b/src/lib/libc/crypt/Makefile.inc
new file mode 100644
index 0000000000..9d96d657db
--- /dev/null
+++ b/src/lib/libc/crypt/Makefile.inc
@@ -0,0 +1,12 @@
1# $OpenBSD: Makefile.inc,v 1.10 1998/07/21 22:23:20 provos Exp $
2
3.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/crypt ${.CURDIR}/crypt
4
5SRCS+= cast.c crypt.c morecrypt.c md5crypt.c arc4random.c blowfish.c
6SRCS+= bcrypt.c
7
8MAN+= crypt.3 blowfish.3 arc4random.3
9MLINKS+=crypt.3 encrypt.3 crypt.3 setkey.3 crypt.3 des_cipher.3
10MLINKS+=crypt.3 des_setkey.3 blowfish.3 blf_key.3 blowfish.3 blf_enc.3
11MLINKS+=blowfish.3 blf_dec.3
12MLINKS+=arc4random.3 arc4random_stir.3 arc4random.3 arc4random_addrandom.3
diff --git a/src/lib/libc/crypt/arc4random.3 b/src/lib/libc/crypt/arc4random.3
new file mode 100644
index 0000000000..741965c5ac
--- /dev/null
+++ b/src/lib/libc/crypt/arc4random.3
@@ -0,0 +1,83 @@
1.\" $OpenBSD: arc4random.3,v 1.4 1998/09/07 16:44:34 aaron Exp $
2.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
3.\" 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 Niels Provos.
16.\" 4. The name of the author may not be used to endorse or promote products
17.\" derived from this software without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29.\"
30.\" Manual page, using -mandoc macros
31.\"
32.Dd April 15, 1997
33.Dt ARC4RANDOM 3
34.Os "OpenBSD 2.0"
35.Sh NAME
36.Nm arc4random,
37.Nm arc4random_stir,
38.Nm arc4random_addrandom
39.Nd arc4 random number generator.
40.Sh SYNOPSIS
41.Fd #include <stdlib.h>
42.Ft u_int32_t
43.Fn arc4random "void"
44.Ft void
45.Fn arc4random_stir "void"
46.Ft void
47.Fn arc4random_addrandom "u_char *dat" "int datlen"
48.Sh DESCRIPTION
49The
50.Fn arc4random
51function uses the key stream generator employed by the
52arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes
53can be in about
54.if t 2\u\s71700\s10\d
55.if n (2**1700)
56states.
57.Pp
58The
59.Fn arc4random_stir
60function reads data from
61.Pa /dev/arandom
62and uses it to permutate the S-Boxes via
63.Fn arc4random_addrandom .
64.Pp
65There is no need to call
66.Fn arc4random_stir
67before using
68.Fn arc4random ,
69since
70.Fn arc4random
71automatically initalizes itself.
72.Sh SEE ALSO
73.Xr rand48 3 ,
74.Xr rand 3 ,
75.Xr random 3
76.Sh HISTORY
77.Pa RC4
78has been designed by RSA Data Security, Inc. It was posted anonymously
79to the USENET and was confirmed to be equivalent by several sources who
80had access to the original cipher. Since
81.Pa RC4
82used to be a trade secret, the cipher is now referred to as
83.Pa ARC4 .
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c
new file mode 100644
index 0000000000..5279c21518
--- /dev/null
+++ b/src/lib/libc/crypt/arc4random.c
@@ -0,0 +1,176 @@
1/* $OpenBSD: arc4random.c,v 1.3 1998/03/22 19:01:16 niklas Exp $ */
2
3/*
4 * Arc4 random number generator for OpenBSD.
5 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
6 *
7 * Modification and redistribution in source and binary forms is
8 * permitted provided that due credit is given to the author and the
9 * OpenBSD project (for instance by leaving this copyright notice
10 * intact).
11 */
12
13/*
14 * This code is derived from section 17.1 of Applied Cryptography,
15 * second edition, which describes a stream cipher allegedly
16 * compatible with RSA Labs "RC4" cipher (the actual description of
17 * which is a trade secret). The same algorithm is used as a stream
18 * cipher called "arcfour" in Tatu Ylonen's ssh package.
19 *
20 * Here the stream cipher has been modified always to include the time
21 * when initializing the state. That makes it impossible to
22 * regenerate the same random sequence twice, so this can't be used
23 * for encryption, but will generate good random numbers.
24 *
25 * RC4 is a registered trademark of RSA Laboratories.
26 */
27
28#include <fcntl.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <sys/types.h>
32#include <sys/time.h>
33
34#ifdef __GNUC__
35#define inline __inline
36#else /* !__GNUC__ */
37#define inline
38#endif /* !__GNUC__ */
39
40struct arc4_stream {
41 u_int8_t i;
42 u_int8_t j;
43 u_int8_t s[256];
44};
45
46int rs_initialized;
47static struct arc4_stream rs;
48
49static inline void
50arc4_init(as)
51 struct arc4_stream *as;
52{
53 int n;
54
55 for (n = 0; n < 256; n++)
56 as->s[n] = n;
57 as->i = 0;
58 as->j = 0;
59}
60
61static inline void
62arc4_addrandom(as, dat, datlen)
63 struct arc4_stream *as;
64 u_char *dat;
65 int datlen;
66{
67 int n;
68 u_int8_t si;
69
70 as->i--;
71 for (n = 0; n < 256; n++) {
72 as->i = (as->i + 1);
73 si = as->s[as->i];
74 as->j = (as->j + si + dat[n % datlen]);
75 as->s[as->i] = as->s[as->j];
76 as->s[as->j] = si;
77 }
78}
79
80static void
81arc4_stir(as)
82 struct arc4_stream *as;
83{
84 int fd;
85 struct {
86 struct timeval tv;
87 u_int8_t rnd[128 - sizeof(struct timeval)];
88 } rdat;
89
90 gettimeofday(&rdat.tv, NULL);
91 fd = open("/dev/arandom", O_RDONLY);
92 if (fd >= 0) {
93 read(fd, rdat.rnd, sizeof(rdat.rnd));
94 close(fd);
95 }
96 /* fd < 0? Ah, what the heck. We'll just take whatever was on the
97 * stack... */
98
99 arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
100}
101
102static inline u_int8_t
103arc4_getbyte(as)
104 struct arc4_stream *as;
105{
106 u_int8_t si, sj;
107
108 as->i = (as->i + 1);
109 si = as->s[as->i];
110 as->j = (as->j + si);
111 sj = as->s[as->j];
112 as->s[as->i] = sj;
113 as->s[as->j] = si;
114 return (as->s[(si + sj) & 0xff]);
115}
116
117static inline u_int32_t
118arc4_getword(as)
119 struct arc4_stream *as;
120{
121 u_int32_t val;
122 val = arc4_getbyte(as) << 24;
123 val |= arc4_getbyte(as) << 16;
124 val |= arc4_getbyte(as) << 8;
125 val |= arc4_getbyte(as);
126 return val;
127}
128
129void
130arc4random_stir()
131{
132 if (!rs_initialized) {
133 arc4_init(&rs);
134 rs_initialized = 1;
135 }
136 arc4_stir(&rs);
137}
138
139void
140arc4random_addrandom(dat, datlen)
141 u_char *dat;
142 int datlen;
143{
144 if (!rs_initialized)
145 arc4random_stir();
146 arc4_addrandom(&rs, dat, datlen);
147}
148
149u_int32_t
150arc4random()
151{
152 if (!rs_initialized)
153 arc4random_stir();
154 return arc4_getword(&rs);
155}
156
157#if 0
158/*-------- Test code for i386 --------*/
159#include <stdio.h>
160#include <machine/pctr.h>
161int
162main(int argc, char **argv)
163{
164 const int iter = 1000000;
165 int i;
166 pctrval v;
167
168 v = rdtsc();
169 for (i = 0; i < iter; i++)
170 arc4random();
171 v = rdtsc() - v;
172 v /= iter;
173
174 printf("%qd cycles\n", v);
175}
176#endif
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
new file mode 100644
index 0000000000..1b121fb28f
--- /dev/null
+++ b/src/lib/libc/crypt/bcrypt.c
@@ -0,0 +1,362 @@
1/* $OpenBSD: bcrypt.c,v 1.12 1998/08/10 18:33:07 provos Exp $ */
2
3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Niels Provos.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/* This password hashing algorithm was designed by David Mazieres
34 * <dm@lcs.mit.edu> and works as follows:
35 *
36 * 1. state := InitState ()
37 * 2. state := ExpandKey (state, salt, password) 3.
38 * REPEAT rounds:
39 * state := ExpandKey (state, 0, salt)
40 * state := ExpandKey(state, 0, password)
41 * 4. ctext := "OrpheanBeholderScryDoubt"
42 * 5. REPEAT 64:
43 * ctext := Encrypt_ECB (state, ctext);
44 * 6. RETURN Concatenate (salt, ctext);
45 *
46 */
47
48#if 0
49#include <stdio.h>
50#endif
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <sys/types.h>
55#include <string.h>
56#include <pwd.h>
57#include <blf.h>
58
59/* This implementation is adaptable to current computing power.
60 * You can have up to 2^31 rounds which should be enough for some
61 * time to come.
62 */
63
64#define BCRYPT_VERSION '2'
65#define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
66#define BCRYPT_BLOCKS 6 /* Ciphertext blocks */
67#define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */
68
69char *bcrypt_gensalt __P((u_int8_t));
70
71static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t));
72static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t));
73static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *));
74
75static char encrypted[_PASSWORD_LEN];
76static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
77static char error[] = ":";
78
79const static u_int8_t Base64Code[] =
80"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
81
82const static u_int8_t index_64[128] =
83{
84 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
85 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
88 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
89 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
90 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
91 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
92 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
93 255, 255, 255, 255, 255, 255, 28, 29, 30,
94 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
95 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
96 51, 52, 53, 255, 255, 255, 255, 255
97};
98#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
99
100#ifdef __STDC__
101static void
102decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
103#else
104static void
105decode_base64(buffer, len, data)
106 u_int8_t *buffer;
107 u_int16_t len;
108 u_int8_t *data;
109#endif
110{
111 u_int8_t *bp = buffer;
112 u_int8_t *p = data;
113 u_int8_t c1, c2, c3, c4;
114 while (bp < buffer + len) {
115 c1 = CHAR64(*p);
116 c2 = CHAR64(*(p + 1));
117
118 /* Invalid data */
119 if (c1 == 255 || c2 == 255)
120 break;
121
122 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
123 if (bp >= buffer + len)
124 break;
125
126 c3 = CHAR64(*(p + 2));
127 if (c3 == 255)
128 break;
129
130 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
131 if (bp >= buffer + len)
132 break;
133
134 c4 = CHAR64(*(p + 3));
135 if (c4 == 255)
136 break;
137 *bp++ = ((c3 & 0x03) << 6) | c4;
138
139 p += 4;
140 }
141}
142
143#ifdef __STDC__
144static void
145encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
146#else
147static void
148encode_salt(salt, csalt, clen, logr)
149 char *salt;
150 u_int8_t *csalt;
151 u_int16_t clen;
152 u_int8_t logr;
153#endif
154{
155 salt[0] = '$';
156 salt[1] = BCRYPT_VERSION;
157 salt[2] = 'a';
158 salt[3] = '$';
159
160 snprintf(salt + 4, 4, "%2.2u$", logr);
161
162 encode_base64((u_int8_t *) salt + 7, csalt, clen);
163}
164/* Generates a salt for this version of crypt.
165 Since versions may change. Keeping this here
166 seems sensible.
167 */
168
169#ifdef __STDC__
170char *
171bcrypt_gensalt(u_int8_t log_rounds)
172#else
173char *
174bcrypt_gensalt(log_rounds)
175 u_int8_t log_rounds;
176#endif
177{
178 u_int8_t csalt[BCRYPT_MAXSALT];
179 u_int16_t i;
180 u_int32_t seed = 0;
181
182 for (i = 0; i < BCRYPT_MAXSALT; i++) {
183 if (i % 4 == 0)
184 seed = arc4random();
185 csalt[i] = seed & 0xff;
186 seed = seed >> 8;
187 }
188
189 if (log_rounds < 4)
190 log_rounds = 4;
191
192 encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
193 return gsalt;
194}
195/* We handle $Vers$log2(NumRounds)$salt+passwd$
196 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
197
198char *
199bcrypt(key, salt)
200 const char *key;
201 const char *salt;
202{
203 blf_ctx state;
204 u_int32_t rounds, i, k;
205 u_int16_t j;
206 u_int8_t key_len, salt_len, logr, minor;
207 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
208 u_int8_t csalt[BCRYPT_MAXSALT];
209 u_int32_t cdata[BCRYPT_BLOCKS];
210
211 /* Discard "$" identifier */
212 salt++;
213
214 if (*salt > BCRYPT_VERSION) {
215 /* How do I handle errors ? Return ':' */
216 return error;
217 }
218
219 /* Check for minor versions */
220 if (salt[1] != '$') {
221 switch (salt[1]) {
222 case 'a':
223 /* 'ab' should not yield the same as 'abab' */
224 minor = salt[1];
225 salt++;
226 break;
227 default:
228 return error;
229 }
230 } else
231 minor = 0;
232
233 /* Discard version + "$" identifier */
234 salt += 2;
235
236 if (salt[2] != '$')
237 /* Out of sync with passwd entry */
238 return error;
239
240 /* Computer power doesnt increase linear, 2^x should be fine */
241 if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS)
242 return error;
243
244 /* Discard num rounds + "$" identifier */
245 salt += 3;
246
247 /* We dont want the base64 salt but the raw data */
248 decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
249 salt_len = BCRYPT_MAXSALT;
250 key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
251
252 /* Setting up S-Boxes and Subkeys */
253 Blowfish_initstate(&state);
254 Blowfish_expandstate(&state, csalt, salt_len,
255 (u_int8_t *) key, key_len);
256 for (k = 0; k < rounds; k++) {
257 Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
258 Blowfish_expand0state(&state, csalt, salt_len);
259 }
260
261 /* This can be precomputed later */
262 j = 0;
263 for (i = 0; i < BCRYPT_BLOCKS; i++)
264 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
265
266 /* Now do the encryption */
267 for (k = 0; k < 64; k++)
268 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
269
270 for (i = 0; i < BCRYPT_BLOCKS; i++) {
271 ciphertext[4 * i + 3] = cdata[i] & 0xff;
272 cdata[i] = cdata[i] >> 8;
273 ciphertext[4 * i + 2] = cdata[i] & 0xff;
274 cdata[i] = cdata[i] >> 8;
275 ciphertext[4 * i + 1] = cdata[i] & 0xff;
276 cdata[i] = cdata[i] >> 8;
277 ciphertext[4 * i + 0] = cdata[i] & 0xff;
278 }
279
280
281 i = 0;
282 encrypted[i++] = '$';
283 encrypted[i++] = BCRYPT_VERSION;
284 if (minor)
285 encrypted[i++] = minor;
286 encrypted[i++] = '$';
287
288 snprintf(encrypted + i, 4, "%2.2u$", logr);
289
290 encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
291 encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
292 4 * BCRYPT_BLOCKS - 1);
293 return encrypted;
294}
295
296#ifdef __STDC__
297static void
298encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
299#else
300static void
301encode_base64(buffer, data, len)
302 u_int8_t *buffer;
303 u_int8_t *data;
304 u_int16_t len;
305#endif
306{
307 u_int8_t *bp = buffer;
308 u_int8_t *p = data;
309 u_int8_t c1, c2;
310 while (p < data + len) {
311 c1 = *p++;
312 *bp++ = Base64Code[(c1 >> 2)];
313 c1 = (c1 & 0x03) << 4;
314 if (p >= data + len) {
315 *bp++ = Base64Code[c1];
316 break;
317 }
318 c2 = *p++;
319 c1 |= (c2 >> 4) & 0x0f;
320 *bp++ = Base64Code[c1];
321 c1 = (c2 & 0x0f) << 2;
322 if (p >= data + len) {
323 *bp++ = Base64Code[c1];
324 break;
325 }
326 c2 = *p++;
327 c1 |= (c2 >> 6) & 0x03;
328 *bp++ = Base64Code[c1];
329 *bp++ = Base64Code[c2 & 0x3f];
330 }
331 *bp = '\0';
332}
333#if 0
334void
335main()
336{
337 char blubber[73];
338 char salt[100];
339 char *p;
340 salt[0] = '$';
341 salt[1] = BCRYPT_VERSION;
342 salt[2] = '$';
343
344 snprintf(salt + 3, 4, "%2.2u$", 5);
345
346 printf("24 bytes of salt: ");
347 fgets(salt + 6, 94, stdin);
348 salt[99] = 0;
349 printf("72 bytes of password: ");
350 fpurge(stdin);
351 fgets(blubber, 73, stdin);
352 blubber[72] = 0;
353
354 p = crypt(blubber, salt);
355 printf("Passwd entry: %s\n\n", p);
356
357 p = bcrypt_gensalt(5);
358 printf("Generated salt: %s\n", p);
359 p = crypt(blubber, p);
360 printf("Passwd entry: %s\n", p);
361}
362#endif
diff --git a/src/lib/libc/crypt/blowfish.3 b/src/lib/libc/crypt/blowfish.3
new file mode 100644
index 0000000000..02a1ef8738
--- /dev/null
+++ b/src/lib/libc/crypt/blowfish.3
@@ -0,0 +1,104 @@
1.\" $OpenBSD: blowfish.3,v 1.2 1998/08/10 18:40:58 provos Exp $
2.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
3.\" 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 Niels Provos.
16.\" 4. The name of the author may not be used to endorse or promote products
17.\" derived from this software without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29.\"
30.\" Manual page, using -mandoc macros
31.\"
32.Dd February 13, 1997
33.Dt BLOWFISH 3
34.Os "OpenBSD 2.0"
35.Sh NAME
36.Nm blf_key,
37.Nm blf_enc,
38.Nm blf_dec
39.Nd Blowfish encryption
40.Sh SYNOPSIS
41.Fd #include <blf.h>
42.Ft void
43.Fn blf_key "blf_ctx *state" "const u_int8_t *key" "u_int16_t keylen"
44.Ft void
45.Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen"
46.Ft void
47.Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen"
48.Ft void
49.Fn blf_ecb_encrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen"
50.Ft void
51.Fn blf_ecb_decrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen"
52.Ft void
53.Fn blf_cbc_encrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen"
54.Ft void
55.Fn blf_cbc_decrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen"
56.Sh DESCRIPTION
57.Pa Blowfish
58is a fast unpatented block cipher designed by Bruce Schneier.
59It basically consists of a 16 times iterated Feistel network.
60The block size is 64 bit and the key size is maximal 448 bit.
61.Pp
62The
63.Fn blf_key
64function initializes the 4 8bit S-boxes and the 18 Subkeys with
65the hexadecimal digits of Pi. The key is used for further randomization.
66The first argument to
67.Fn blf_enc
68is the initalized state derived from
69.Fn blf_key .
70The stream of 32-bit words is encrypted in Electronic Codebook
71Mode (ECB) and
72.Pa datalen
73must be even.
74.Fn blf_dec
75is used for decrypting Blowfish encrypted blocks.
76.Pp
77The functions
78.Fn blf_ecb_encrypt
79and
80.Fn blf_ecb_decrypt
81are used for encrypting and decrypting octet streams in ECB mode.
82The functions
83.Fn blf_cbc_encrypt
84and
85.Fn blf_cbc_decrypt
86are used for encrypting and decrypting octet streams in
87Cipherblock Chaining Mode (CBC).
88.Pp
89The functions
90.Fn Blowfish_initstate ,
91.Fn Blowfish_expand0state ,
92.Fn Blowfish_expandstate ,
93.Fn Blowfish_encipher
94and
95.Fn Blowfish_decipher
96are used for customization of the
97.Pa Blowfish
98cipher, e.g. for the blowfish password hashing function.
99.Sh SEE ALSO
100.Xr crypt 3 ,
101.Xr passwd 1 ,
102.Xr passwd 5
103.Sh AUTHOR
104Niels Provos <provos@physnet.uni-hamburg.de>
diff --git a/src/lib/libc/crypt/blowfish.c b/src/lib/libc/crypt/blowfish.c
new file mode 100644
index 0000000000..6cddbc64b3
--- /dev/null
+++ b/src/lib/libc/crypt/blowfish.c
@@ -0,0 +1,774 @@
1/* $OpenBSD: blowfish.c,v 1.12 1998/08/30 22:35:39 niklas Exp $ */
2/*
3 * Blowfish block cipher for OpenBSD
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * All rights reserved.
6 *
7 * Implementation advice by David Mazieres <dm@lcs.mit.edu>.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Niels Provos.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * This code is derived from section 14.3 and the given source
37 * in section V of Applied Cryptography, second edition.
38 * Blowfish is an unpatented fast block cipher designed by
39 * Bruce Schneier.
40 */
41
42#if 0
43#include <stdio.h> /* used for debugging */
44#include <string.h>
45#endif
46
47#include <sys/types.h>
48#include <blf.h>
49
50#undef inline
51#ifdef __GNUC__
52#define inline __inline
53#else /* !__GNUC__ */
54#define inline
55#endif /* !__GNUC__ */
56
57/* Function for Feistel Networks */
58
59#define F(bc, x) ((((bc)->S[0][((x) & 0xFF000000) >> 24] \
60 + (bc)->S[1][((x) &0xFF0000 ) >> 16]) \
61 ^ (bc)->S[2][((x) & 0xFF00) >> 8]) \
62 + (bc)->S[3][(x) & 0x00FF])
63
64#define BLFRND(bc,i,j,n) (i ^= F(bc,j) ^ (bc)->P[n])
65
66void
67Blowfish_encipher(c, xl, xr)
68 blf_ctx *c;
69 u_int32_t *xl;
70 u_int32_t *xr;
71{
72 u_int32_t Xl;
73 u_int32_t Xr;
74
75 Xl = *xl;
76 Xr = *xr;
77
78 Xl ^= c->P[0];
79 BLFRND(c, Xr, Xl, 1); BLFRND(c, Xl, Xr, 2);
80 BLFRND(c, Xr, Xl, 3); BLFRND(c, Xl, Xr, 4);
81 BLFRND(c, Xr, Xl, 5); BLFRND(c, Xl, Xr, 6);
82 BLFRND(c, Xr, Xl, 7); BLFRND(c, Xl, Xr, 8);
83 BLFRND(c, Xr, Xl, 9); BLFRND(c, Xl, Xr, 10);
84 BLFRND(c, Xr, Xl, 11); BLFRND(c, Xl, Xr, 12);
85 BLFRND(c, Xr, Xl, 13); BLFRND(c, Xl, Xr, 14);
86 BLFRND(c, Xr, Xl, 15); BLFRND(c, Xl, Xr, 16);
87
88 *xl = Xr ^ c->P[17];
89 *xr = Xl;
90}
91
92void
93Blowfish_decipher(c, xl, xr)
94 blf_ctx *c;
95 u_int32_t *xl;
96 u_int32_t *xr;
97{
98 u_int32_t Xl;
99 u_int32_t Xr;
100
101 Xl = *xl;
102 Xr = *xr;
103
104 Xl ^= c->P[17];
105 BLFRND(c, Xr, Xl, 16); BLFRND(c, Xl, Xr, 15);
106 BLFRND(c, Xr, Xl, 14); BLFRND(c, Xl, Xr, 13);
107 BLFRND(c, Xr, Xl, 12); BLFRND(c, Xl, Xr, 11);
108 BLFRND(c, Xr, Xl, 10); BLFRND(c, Xl, Xr, 9);
109 BLFRND(c, Xr, Xl, 8); BLFRND(c, Xl, Xr, 7);
110 BLFRND(c, Xr, Xl, 6); BLFRND(c, Xl, Xr, 5);
111 BLFRND(c, Xr, Xl, 4); BLFRND(c, Xl, Xr, 3);
112 BLFRND(c, Xr, Xl, 2); BLFRND(c, Xl, Xr, 1);
113
114 *xl = Xr ^ c->P[0];
115 *xr = Xl;
116}
117
118void
119Blowfish_initstate(c)
120 blf_ctx *c;
121{
122
123/* P-box and S-box tables initialized with digits of Pi */
124
125 const blf_ctx initstate =
126
127 { {
128 {
129 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
130 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
131 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
132 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
133 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee,
134 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
135 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef,
136 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
137 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
138 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
139 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce,
140 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
141 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e,
142 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
143 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
144 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
145 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88,
146 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
147 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e,
148 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
149 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
150 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
151 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88,
152 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
153 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6,
154 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
155 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
156 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
157 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba,
158 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
159 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f,
160 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
161 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
162 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
163 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279,
164 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
165 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab,
166 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
167 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
168 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
169 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0,
170 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
171 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790,
172 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
173 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
174 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
175 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7,
176 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
177 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad,
178 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
179 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
180 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
181 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477,
182 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
183 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49,
184 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
185 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
186 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
187 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41,
188 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
189 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400,
190 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
191 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
192 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a},
193 {
194 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623,
195 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
196 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
197 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
198 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
199 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
200 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e,
201 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
202 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
203 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
204 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff,
205 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
206 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701,
207 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
208 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
209 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
210 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf,
211 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
212 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e,
213 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
214 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
215 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
216 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
217 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
218 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b,
219 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
220 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
221 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
222 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f,
223 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
224 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4,
225 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
226 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
227 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
228 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802,
229 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
230 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510,
231 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
232 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
233 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
234 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
235 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
236 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8,
237 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
238 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
239 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
240 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128,
241 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
242 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0,
243 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
244 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
245 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
246 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3,
247 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
248 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00,
249 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
250 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
251 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
252 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
253 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
254 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9,
255 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
256 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
257 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7},
258 {
259 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934,
260 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
261 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
262 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
263 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45,
264 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
265 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a,
266 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
267 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
268 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
269 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42,
270 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
271 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
272 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
273 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
274 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
275 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33,
276 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
277 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3,
278 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
279 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
280 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
281 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b,
282 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
283 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922,
284 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
285 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
286 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
287 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37,
288 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
289 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
290 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
291 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
292 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
293 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d,
294 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
295 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350,
296 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
297 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
298 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
299 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d,
300 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
301 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f,
302 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
303 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
304 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
305 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2,
306 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
307 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
308 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
309 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
310 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
311 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52,
312 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
313 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5,
314 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
315 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
316 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
317 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24,
318 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
319 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4,
320 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
321 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
322 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0},
323 {
324 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b,
325 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
326 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
327 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
328 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8,
329 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
330 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304,
331 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
332 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
333 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
334 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9,
335 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
336 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593,
337 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
338 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
339 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
340 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b,
341 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
342 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c,
343 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
344 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
345 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
346 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb,
347 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
348 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991,
349 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
350 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
351 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
352 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae,
353 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
354 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5,
355 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
356 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
357 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
358 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84,
359 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
360 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8,
361 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
362 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
363 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
364 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38,
365 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
366 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c,
367 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
368 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
369 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
370 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964,
371 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
372 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8,
373 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
374 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
375 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
376 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02,
377 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
378 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614,
379 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
380 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
381 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
382 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0,
383 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
384 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e,
385 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
386 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
387 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6}
388 },
389 {
390 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
391 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
392 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
393 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
394 0x9216d5d9, 0x8979fb1b
395 } };
396
397 *c = initstate;
398
399}
400
401#ifdef __STDC__
402u_int32_t
403Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, u_int16_t *current)
404#else
405u_int32_t
406Blowfish_stream2word(data, databytes, current)
407 const u_int8_t *data;
408 u_int16_t databytes;
409 u_int16_t *current;
410#endif
411{
412 u_int8_t i;
413 u_int16_t j;
414 u_int32_t temp;
415
416 temp = 0x00000000;
417 j = *current;
418
419 for (i = 0; i < 4; i++, j++) {
420 if (j >= databytes)
421 j = 0;
422 temp = (temp << 8) | data[j];
423 }
424
425 *current = j;
426 return temp;
427}
428
429#if __STDC__
430void
431Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)
432#else
433void
434Blowfish_expand0state(c, key, keybytes)
435 blf_ctx *c;
436 const u_int8_t *key;
437 u_int16_t keybytes;
438#endif
439{
440 u_int16_t i;
441 u_int16_t j;
442 u_int16_t k;
443 u_int32_t temp;
444 u_int32_t datal;
445 u_int32_t datar;
446
447 j = 0;
448 for (i = 0; i < BLF_N + 2; i++) {
449 /* Extract 4 int8 to 1 int32 from keystream */
450 temp = Blowfish_stream2word(key, keybytes, &j);
451 c->P[i] = c->P[i] ^ temp;
452 }
453
454 j = 0;
455 datal = 0x00000000;
456 datar = 0x00000000;
457 for (i = 0; i < BLF_N + 2; i += 2) {
458 Blowfish_encipher(c, &datal, &datar);
459
460 c->P[i] = datal;
461 c->P[i + 1] = datar;
462 }
463
464 for (i = 0; i < 4; i++) {
465 for (k = 0; k < 256; k += 2) {
466 Blowfish_encipher(c, &datal, &datar);
467
468 c->S[i][k] = datal;
469 c->S[i][k + 1] = datar;
470 }
471 }
472}
473
474
475#if __STDC__
476void
477Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes,
478 const u_int8_t *key, u_int16_t keybytes)
479#else
480void
481Blowfish_expandstate(c, data, databytes, key, keybytes)
482 blf_ctx *c;
483 const u_int8_t *data;
484 u_int16_t databytes;
485 const u_int8_t *key;
486 u_int16_t keybytes;
487#endif
488{
489 u_int16_t i;
490 u_int16_t j;
491 u_int16_t k;
492 u_int32_t temp;
493 u_int32_t datal;
494 u_int32_t datar;
495
496 j = 0;
497 for (i = 0; i < BLF_N + 2; i++) {
498 /* Extract 4 int8 to 1 int32 from keystream */
499 temp = Blowfish_stream2word(key, keybytes, &j);
500 c->P[i] = c->P[i] ^ temp;
501 }
502
503 j = 0;
504 datal = 0x00000000;
505 datar = 0x00000000;
506 for (i = 0; i < BLF_N + 2; i += 2) {
507 datal ^= Blowfish_stream2word(data, databytes, &j);
508 datar ^= Blowfish_stream2word(data, databytes, &j);
509 Blowfish_encipher(c, &datal, &datar);
510
511 c->P[i] = datal;
512 c->P[i + 1] = datar;
513 }
514
515 for (i = 0; i < 4; i++) {
516 for (k = 0; k < 256; k += 2) {
517 datal ^= Blowfish_stream2word(data, databytes, &j);
518 datar ^= Blowfish_stream2word(data, databytes, &j);
519 Blowfish_encipher(c, &datal, &datar);
520
521 c->S[i][k] = datal;
522 c->S[i][k + 1] = datar;
523 }
524 }
525
526}
527
528#if __STDC__
529void
530blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len)
531#else
532void
533blf_key(c, k, len)
534 blf_ctx *c;
535 const u_int8_t *k;
536 u_int16_t len;
537#endif
538{
539 /* Initalize S-boxes and subkeys with Pi */
540 Blowfish_initstate(c);
541
542 /* Transform S-boxes and subkeys with key */
543 Blowfish_expand0state(c, k, len);
544}
545
546#if __STDC__
547void
548blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
549#else
550void
551blf_enc(c, data, blocks)
552 blf_ctx *c;
553 u_int32_t *data;
554 u_int16_t blocks;
555#endif
556{
557 u_int32_t *d;
558 u_int16_t i;
559
560 d = data;
561 for (i = 0; i < blocks; i++) {
562 Blowfish_encipher(c, d, d + 1);
563 d += 2;
564 }
565}
566
567#if __STDC__
568void
569blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
570#else
571void
572blf_dec(c, data, blocks)
573 blf_ctx *c;
574 u_int32_t *data;
575 u_int16_t blocks;
576#endif
577{
578 u_int32_t *d;
579 u_int16_t i;
580
581 d = data;
582 for (i = 0; i < blocks; i++) {
583 Blowfish_decipher(c, d, d + 1);
584 d += 2;
585 }
586}
587
588#if __STDC__
589void
590blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
591#else
592void
593blf_ecb_encrypt(c, data, len)
594 blf_ctx *c;
595 u_int8_t *data;
596 u_int32_t len;
597#endif
598{
599 u_int32_t l, r;
600 u_int32_t i;
601
602 for (i = 0; i < len; i += 8) {
603 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
604 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
605 Blowfish_encipher(c, &l, &r);
606 data[0] = l >> 24 & 0xff;
607 data[1] = l >> 16 & 0xff;
608 data[2] = l >> 8 & 0xff;
609 data[3] = l & 0xff;
610 data[4] = r >> 24 & 0xff;
611 data[5] = r >> 16 & 0xff;
612 data[6] = r >> 8 & 0xff;
613 data[7] = r & 0xff;
614 data += 8;
615 }
616}
617
618#if __STDC__
619void
620blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
621#else
622void
623blf_ecb_decrypt(c, data, len)
624 blf_ctx *c;
625 u_int8_t *data;
626 u_int32_t len;
627#endif
628{
629 u_int32_t l, r;
630 u_int32_t i;
631
632 for (i = 0; i < len; i += 8) {
633 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
634 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
635 Blowfish_decipher(c, &l, &r);
636 data[0] = l >> 24 & 0xff;
637 data[1] = l >> 16 & 0xff;
638 data[2] = l >> 8 & 0xff;
639 data[3] = l & 0xff;
640 data[4] = r >> 24 & 0xff;
641 data[5] = r >> 16 & 0xff;
642 data[6] = r >> 8 & 0xff;
643 data[7] = r & 0xff;
644 data += 8;
645 }
646}
647
648#if __STDC__
649void
650blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
651#else
652void
653blf_cbc_encrypt(c, iv, data, len)
654 blf_ctx *c;
655 u_int8_t *iv;
656 u_int8_t *data;
657 u_int32_t len;
658#endif
659{
660 u_int32_t l, r;
661 u_int32_t i, j;
662
663 for (i = 0; i < len; i += 8) {
664 for (j = 0; j < 8; j++)
665 data[j] ^= iv[j];
666 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
667 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
668 Blowfish_encipher(c, &l, &r);
669 data[0] = l >> 24 & 0xff;
670 data[1] = l >> 16 & 0xff;
671 data[2] = l >> 8 & 0xff;
672 data[3] = l & 0xff;
673 data[4] = r >> 24 & 0xff;
674 data[5] = r >> 16 & 0xff;
675 data[6] = r >> 8 & 0xff;
676 data[7] = r & 0xff;
677 iv = data;
678 data += 8;
679 }
680}
681
682#if __STDC__
683void
684blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
685#else
686void
687blf_cbc_decrypt(c, iva, data, len)
688 blf_ctx *c;
689 u_int8_t *iva;
690 u_int8_t *data;
691 u_int32_t len;
692#endif
693{
694 u_int32_t l, r;
695 u_int8_t *iv;
696 u_int32_t i, j;
697
698 iv = data + len - 16;
699 data = data + len - 8;
700 for (i = len - 8; i >= 8; i -= 8) {
701 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
702 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
703 Blowfish_decipher(c, &l, &r);
704 data[0] = l >> 24 & 0xff;
705 data[1] = l >> 16 & 0xff;
706 data[2] = l >> 8 & 0xff;
707 data[3] = l & 0xff;
708 data[4] = r >> 24 & 0xff;
709 data[5] = r >> 16 & 0xff;
710 data[6] = r >> 8 & 0xff;
711 data[7] = r & 0xff;
712 for (j = 0; j < 8; j++)
713 data[j] ^= iv[j];
714 iv = data;
715 data -= 8;
716 }
717 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
718 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
719 Blowfish_decipher(c, &l, &r);
720 data[0] = l >> 24 & 0xff;
721 data[1] = l >> 16 & 0xff;
722 data[2] = l >> 8 & 0xff;
723 data[3] = l & 0xff;
724 data[4] = r >> 24 & 0xff;
725 data[5] = r >> 16 & 0xff;
726 data[6] = r >> 8 & 0xff;
727 data[7] = r & 0xff;
728 for (j = 0; j < 8; j++)
729 data[j] ^= iva[j];
730}
731
732#if 0
733void
734report(u_int32_t data[], u_int16_t len)
735{
736 u_int16_t i;
737 for (i = 0; i < len; i += 2)
738 printf("Block %0hd: %08lx %08lx.\n",
739 i / 2, data[i], data[i + 1]);
740}
741void
742main(void)
743{
744
745 blf_ctx c;
746 char key[] = "AAAAA";
747 char key2[] = "abcdefghijklmnopqrstuvwxyz";
748
749 u_int32_t data[10];
750 u_int32_t data2[] =
751 {0x424c4f57l, 0x46495348l};
752
753 u_int16_t i;
754
755 /* First test */
756 for (i = 0; i < 10; i++)
757 data[i] = i;
758
759 blf_key(&c, (u_int8_t *) key, 5);
760 blf_enc(&c, data, 5);
761 blf_dec(&c, data, 1);
762 blf_dec(&c, data + 2, 4);
763 printf("Should read as 0 - 9.\n");
764 report(data, 10);
765
766 /* Second test */
767 blf_key(&c, (u_int8_t *) key2, strlen(key2));
768 blf_enc(&c, data2, 1);
769 printf("\nShould read as: 0x324ed0fe 0xf413a203.\n");
770 report(data2, 2);
771 blf_dec(&c, data2, 1);
772 report(data2, 2);
773}
774#endif
diff --git a/src/lib/libc/crypt/cast.c b/src/lib/libc/crypt/cast.c
new file mode 100644
index 0000000000..264138f03e
--- /dev/null
+++ b/src/lib/libc/crypt/cast.c
@@ -0,0 +1,779 @@
1/* $OpenBSD: cast.c,v 1.2 1998/07/21 22:42:03 provos Exp $ */
2/*
3 * CAST-128 in C
4 * Written by Steve Reid <sreid@sea-to-sky.net>
5 * 100% Public Domain - no warranty
6 * Released 1997.10.11
7 */
8
9#include <sys/types.h>
10
11#include <cast.h>
12
13/* CAST S-Boxes */
14
15static const u_int32_t cast_sbox1[256] = {
16 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A,
17 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949,
18 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675,
19 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E,
20 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2,
21 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D,
22 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F,
23 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0,
24 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE,
25 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7,
26 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F,
27 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935,
28 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D,
29 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D,
30 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165,
31 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50,
32 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272,
33 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE,
34 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D,
35 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3,
36 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A,
37 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167,
38 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F,
39 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291,
40 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9,
41 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779,
42 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6,
43 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2,
44 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9,
45 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511,
46 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E,
47 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D,
48 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E,
49 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5,
50 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82,
51 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324,
52 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC,
53 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C,
54 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F,
55 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC,
56 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491,
57 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D,
58 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE,
59 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96,
60 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A,
61 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A,
62 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79,
63 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D,
64 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779,
65 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD,
66 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755,
67 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6,
68 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB,
69 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9,
70 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0,
71 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872,
72 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79,
73 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C,
74 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298,
75 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E,
76 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571,
77 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9,
78 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D,
79 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF
80};
81
82static const u_int32_t cast_sbox2[256] = {
83 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380,
84 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651,
85 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA,
86 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3,
87 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909,
88 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB,
89 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B,
90 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806,
91 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4,
92 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B,
93 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F,
94 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359,
95 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21,
96 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B,
97 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D,
98 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C,
99 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F,
100 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34,
101 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D,
102 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB,
103 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4,
104 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD,
105 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801,
106 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860,
107 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755,
108 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B,
109 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709,
110 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304,
111 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B,
112 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B,
113 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C,
114 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF,
115 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9,
116 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C,
117 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3,
118 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13,
119 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9,
120 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F,
121 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB,
122 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6,
123 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4,
124 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6,
125 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43,
126 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58,
127 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8,
128 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906,
129 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171,
130 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D,
131 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89,
132 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6,
133 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B,
134 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4,
135 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB,
136 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6,
137 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E,
138 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F,
139 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA,
140 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249,
141 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA,
142 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA,
143 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD,
144 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9,
145 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF,
146 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1
147};
148
149static const u_int32_t cast_sbox3[256] = {
150 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907,
151 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90,
152 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE,
153 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5,
154 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E,
155 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E,
156 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC,
157 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240,
158 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E,
159 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5,
160 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F,
161 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B,
162 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99,
163 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71,
164 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F,
165 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04,
166 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380,
167 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82,
168 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8,
169 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15,
170 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504,
171 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2,
172 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6,
173 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176,
174 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E,
175 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148,
176 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D,
177 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC,
178 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1,
179 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341,
180 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C,
181 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E,
182 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15,
183 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51,
184 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4,
185 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F,
186 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B,
187 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A,
188 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392,
189 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B,
190 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231,
191 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B,
192 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889,
193 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5,
194 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67,
195 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45,
196 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49,
197 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536,
198 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D,
199 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC,
200 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D,
201 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0,
202 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E,
203 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69,
204 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767,
205 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2,
206 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE,
207 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49,
208 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24,
209 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D,
210 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0,
211 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A,
212 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5,
213 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783
214};
215
216static const u_int32_t cast_sbox4[256] = {
217 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298,
218 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1,
219 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120,
220 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF,
221 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220,
222 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15,
223 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE,
224 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121,
225 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701,
226 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25,
227 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B,
228 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5,
229 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93,
230 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB,
231 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746,
232 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5,
233 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9,
234 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D,
235 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB,
236 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6,
237 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C,
238 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23,
239 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7,
240 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003,
241 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340,
242 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6,
243 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327,
244 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119,
245 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC,
246 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24,
247 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205,
248 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A,
249 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031,
250 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79,
251 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5,
252 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF,
253 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C,
254 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26,
255 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69,
256 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB,
257 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9,
258 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7,
259 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF,
260 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417,
261 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3,
262 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2,
263 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2,
264 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2,
265 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF,
266 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A,
267 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091,
268 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919,
269 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF,
270 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF,
271 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF,
272 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876,
273 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367,
274 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB,
275 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C,
276 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04,
277 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43,
278 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282,
279 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E,
280 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2
281};
282
283static const u_int32_t cast_sbox5[256] = {
284 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911,
285 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F,
286 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00,
287 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A,
288 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180,
289 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF,
290 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2,
291 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02,
292 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725,
293 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A,
294 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B,
295 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7,
296 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571,
297 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9,
298 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC,
299 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981,
300 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA,
301 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774,
302 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263,
303 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655,
304 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468,
305 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2,
306 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B,
307 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910,
308 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284,
309 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1,
310 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4,
311 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA,
312 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7,
313 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049,
314 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE,
315 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F,
316 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6,
317 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA,
318 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4,
319 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE,
320 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561,
321 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3,
322 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6,
323 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840,
324 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406,
325 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4,
326 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472,
327 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2,
328 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487,
329 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7,
330 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288,
331 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5,
332 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2,
333 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E,
334 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78,
335 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E,
336 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76,
337 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801,
338 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0,
339 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD,
340 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58,
341 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0,
342 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2,
343 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20,
344 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE,
345 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8,
346 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55,
347 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4
348};
349
350static const u_int32_t cast_sbox6[256] = {
351 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C,
352 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC,
353 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9,
354 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138,
355 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E,
356 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367,
357 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866,
358 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98,
359 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C,
360 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072,
361 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD,
362 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3,
363 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53,
364 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD,
365 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D,
366 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8,
367 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF,
368 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9,
369 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807,
370 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54,
371 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A,
372 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387,
373 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563,
374 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC,
375 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0,
376 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF,
377 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE,
378 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF,
379 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0,
380 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F,
381 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2,
382 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289,
383 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853,
384 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950,
385 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA,
386 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F,
387 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9,
388 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B,
389 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751,
390 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE,
391 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358,
392 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13,
393 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397,
394 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976,
395 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459,
396 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0,
397 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4,
398 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891,
399 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F,
400 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA,
401 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB,
402 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC,
403 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2,
404 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084,
405 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB,
406 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25,
407 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B,
408 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121,
409 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B,
410 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5,
411 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855,
412 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD,
413 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454,
414 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F
415};
416
417static const u_int32_t cast_sbox7[256] = {
418 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693,
419 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F,
420 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82,
421 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE,
422 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD,
423 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43,
424 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F,
425 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19,
426 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9,
427 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2,
428 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E,
429 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516,
430 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83,
431 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88,
432 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E,
433 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816,
434 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A,
435 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756,
436 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F,
437 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A,
438 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B,
439 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264,
440 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78,
441 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688,
442 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D,
443 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28,
444 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802,
445 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3,
446 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9,
447 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7,
448 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302,
449 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06,
450 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858,
451 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033,
452 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A,
453 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A,
454 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4,
455 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566,
456 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF,
457 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509,
458 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9,
459 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962,
460 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C,
461 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E,
462 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07,
463 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C,
464 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939,
465 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C,
466 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E,
467 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285,
468 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378,
469 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301,
470 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD,
471 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE,
472 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567,
473 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767,
474 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2,
475 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647,
476 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF,
477 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914,
478 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2,
479 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C,
480 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA,
481 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3
482};
483
484static const u_int32_t cast_sbox8[256] = {
485 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095,
486 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5,
487 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174,
488 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC,
489 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940,
490 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD,
491 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42,
492 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D,
493 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164,
494 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2,
495 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4,
496 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862,
497 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0,
498 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC,
499 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6,
500 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C,
501 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491,
502 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E,
503 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B,
504 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039,
505 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8,
506 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8,
507 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006,
508 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42,
509 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564,
510 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5,
511 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB,
512 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472,
513 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC,
514 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225,
515 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8,
516 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C,
517 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441,
518 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB,
519 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F,
520 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054,
521 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504,
522 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70,
523 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C,
524 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC,
525 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6,
526 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C,
527 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD,
528 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3,
529 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4,
530 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4,
531 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC,
532 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101,
533 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA,
534 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F,
535 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF,
536 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E,
537 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603,
538 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A,
539 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37,
540 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C,
541 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819,
542 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384,
543 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D,
544 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C,
545 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347,
546 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82,
547 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D,
548 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E
549};
550
551/* Macros to access 8-bit bytes out of a 32-bit word */
552#define U8a(x) ( (u_int8_t) (x>>24) )
553#define U8b(x) ( (u_int8_t) ((x>>16)&255) )
554#define U8c(x) ( (u_int8_t) ((x>>8)&255) )
555#define U8d(x) ( (u_int8_t) ((x)&255) )
556
557/* Circular left shift */
558#define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) )
559
560/* CAST-128 uses three different round functions */
561#define F1(l, r, i) \
562 t = ROL(key->xkey[i] + r, key->xkey[i+16]); \
563 l ^= ((cast_sbox1[U8a(t)] ^ cast_sbox2[U8b(t)]) - \
564 cast_sbox3[U8c(t)]) + cast_sbox4[U8d(t)];
565#define F2(l, r, i) \
566 t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); \
567 l ^= ((cast_sbox1[U8a(t)] - cast_sbox2[U8b(t)]) + \
568 cast_sbox3[U8c(t)]) ^ cast_sbox4[U8d(t)];
569#define F3(l, r, i) \
570 t = ROL(key->xkey[i] - r, key->xkey[i+16]); \
571 l ^= ((cast_sbox1[U8a(t)] + cast_sbox2[U8b(t)]) ^ \
572 cast_sbox3[U8c(t)]) - cast_sbox4[U8d(t)];
573
574
575/***** Encryption Function *****/
576
577void cast_encrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock)
578{
579u_int32_t t, l, r;
580
581 /* Get inblock into l,r */
582 l = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) |
583 ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3];
584 r = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) |
585 ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7];
586 /* Do the work */
587 F1(l, r, 0);
588 F2(r, l, 1);
589 F3(l, r, 2);
590 F1(r, l, 3);
591 F2(l, r, 4);
592 F3(r, l, 5);
593 F1(l, r, 6);
594 F2(r, l, 7);
595 F3(l, r, 8);
596 F1(r, l, 9);
597 F2(l, r, 10);
598 F3(r, l, 11);
599 /* Only do full 16 rounds if key length > 80 bits */
600 if (key->rounds > 12) {
601 F1(l, r, 12);
602 F2(r, l, 13);
603 F3(l, r, 14);
604 F1(r, l, 15);
605 }
606 /* Put l,r into outblock */
607 outblock[0] = U8a(r);
608 outblock[1] = U8b(r);
609 outblock[2] = U8c(r);
610 outblock[3] = U8d(r);
611 outblock[4] = U8a(l);
612 outblock[5] = U8b(l);
613 outblock[6] = U8c(l);
614 outblock[7] = U8d(l);
615 /* Wipe clean */
616 t = l = r = 0;
617}
618
619
620/***** Decryption Function *****/
621
622void cast_decrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock)
623{
624u_int32_t t, l, r;
625
626 /* Get inblock into l,r */
627 r = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) |
628 ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3];
629 l = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) |
630 ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7];
631 /* Do the work */
632 /* Only do full 16 rounds if key length > 80 bits */
633 if (key->rounds > 12) {
634 F1(r, l, 15);
635 F3(l, r, 14);
636 F2(r, l, 13);
637 F1(l, r, 12);
638 }
639 F3(r, l, 11);
640 F2(l, r, 10);
641 F1(r, l, 9);
642 F3(l, r, 8);
643 F2(r, l, 7);
644 F1(l, r, 6);
645 F3(r, l, 5);
646 F2(l, r, 4);
647 F1(r, l, 3);
648 F3(l, r, 2);
649 F2(r, l, 1);
650 F1(l, r, 0);
651 /* Put l,r into outblock */
652 outblock[0] = U8a(l);
653 outblock[1] = U8b(l);
654 outblock[2] = U8c(l);
655 outblock[3] = U8d(l);
656 outblock[4] = U8a(r);
657 outblock[5] = U8b(r);
658 outblock[6] = U8c(r);
659 outblock[7] = U8d(r);
660 /* Wipe clean */
661 t = l = r = 0;
662}
663
664
665/***** Key Schedual *****/
666
667void cast_setkey(cast_key* key, u_int8_t* rawkey, int keybytes)
668{
669u_int32_t t[4], z[4], x[4];
670int i;
671
672 /* Set number of rounds to 12 or 16, depending on key length */
673 key->rounds = (keybytes <= 10 ? 12 : 16);
674
675 /* Copy key to workspace x */
676 for (i = 0; i < 4; i++) {
677 x[i] = 0;
678 if ((i*4+0) < keybytes) x[i] = (u_int32_t)rawkey[i*4+0] << 24;
679 if ((i*4+1) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+1] << 16;
680 if ((i*4+2) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+2] << 8;
681 if ((i*4+3) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+3];
682 }
683 /* Generate 32 subkeys, four at a time */
684 for (i = 0; i < 32; i+=4) {
685 switch (i & 4) {
686 case 0:
687 t[0] = z[0] = x[0] ^ cast_sbox5[U8b(x[3])] ^
688 cast_sbox6[U8d(x[3])] ^ cast_sbox7[U8a(x[3])] ^
689 cast_sbox8[U8c(x[3])] ^ cast_sbox7[U8a(x[2])];
690 t[1] = z[1] = x[2] ^ cast_sbox5[U8a(z[0])] ^
691 cast_sbox6[U8c(z[0])] ^ cast_sbox7[U8b(z[0])] ^
692 cast_sbox8[U8d(z[0])] ^ cast_sbox8[U8c(x[2])];
693 t[2] = z[2] = x[3] ^ cast_sbox5[U8d(z[1])] ^
694 cast_sbox6[U8c(z[1])] ^ cast_sbox7[U8b(z[1])] ^
695 cast_sbox8[U8a(z[1])] ^ cast_sbox5[U8b(x[2])];
696 t[3] = z[3] = x[1] ^ cast_sbox5[U8c(z[2])] ^
697 cast_sbox6[U8b(z[2])] ^ cast_sbox7[U8d(z[2])] ^
698 cast_sbox8[U8a(z[2])] ^ cast_sbox6[U8d(x[2])];
699 break;
700 case 4:
701 t[0] = x[0] = z[2] ^ cast_sbox5[U8b(z[1])] ^
702 cast_sbox6[U8d(z[1])] ^ cast_sbox7[U8a(z[1])] ^
703 cast_sbox8[U8c(z[1])] ^ cast_sbox7[U8a(z[0])];
704 t[1] = x[1] = z[0] ^ cast_sbox5[U8a(x[0])] ^
705 cast_sbox6[U8c(x[0])] ^ cast_sbox7[U8b(x[0])] ^
706 cast_sbox8[U8d(x[0])] ^ cast_sbox8[U8c(z[0])];
707 t[2] = x[2] = z[1] ^ cast_sbox5[U8d(x[1])] ^
708 cast_sbox6[U8c(x[1])] ^ cast_sbox7[U8b(x[1])] ^
709 cast_sbox8[U8a(x[1])] ^ cast_sbox5[U8b(z[0])];
710 t[3] = x[3] = z[3] ^ cast_sbox5[U8c(x[2])] ^
711 cast_sbox6[U8b(x[2])] ^ cast_sbox7[U8d(x[2])] ^
712 cast_sbox8[U8a(x[2])] ^ cast_sbox6[U8d(z[0])];
713 break;
714 }
715 switch (i & 12) {
716 case 0:
717 case 12:
718 key->xkey[i+0] = cast_sbox5[U8a(t[2])] ^ cast_sbox6[U8b(t[2])] ^
719 cast_sbox7[U8d(t[1])] ^ cast_sbox8[U8c(t[1])];
720 key->xkey[i+1] = cast_sbox5[U8c(t[2])] ^ cast_sbox6[U8d(t[2])] ^
721 cast_sbox7[U8b(t[1])] ^ cast_sbox8[U8a(t[1])];
722 key->xkey[i+2] = cast_sbox5[U8a(t[3])] ^ cast_sbox6[U8b(t[3])] ^
723 cast_sbox7[U8d(t[0])] ^ cast_sbox8[U8c(t[0])];
724 key->xkey[i+3] = cast_sbox5[U8c(t[3])] ^ cast_sbox6[U8d(t[3])] ^
725 cast_sbox7[U8b(t[0])] ^ cast_sbox8[U8a(t[0])];
726 break;
727 case 4:
728 case 8:
729 key->xkey[i+0] = cast_sbox5[U8d(t[0])] ^ cast_sbox6[U8c(t[0])] ^
730 cast_sbox7[U8a(t[3])] ^ cast_sbox8[U8b(t[3])];
731 key->xkey[i+1] = cast_sbox5[U8b(t[0])] ^ cast_sbox6[U8a(t[0])] ^
732 cast_sbox7[U8c(t[3])] ^ cast_sbox8[U8d(t[3])];
733 key->xkey[i+2] = cast_sbox5[U8d(t[1])] ^ cast_sbox6[U8c(t[1])] ^
734 cast_sbox7[U8a(t[2])] ^ cast_sbox8[U8b(t[2])];
735 key->xkey[i+3] = cast_sbox5[U8b(t[1])] ^ cast_sbox6[U8a(t[1])] ^
736 cast_sbox7[U8c(t[2])] ^ cast_sbox8[U8d(t[2])];
737 break;
738 }
739 switch (i & 12) {
740 case 0:
741 key->xkey[i+0] ^= cast_sbox5[U8c(z[0])];
742 key->xkey[i+1] ^= cast_sbox6[U8c(z[1])];
743 key->xkey[i+2] ^= cast_sbox7[U8b(z[2])];
744 key->xkey[i+3] ^= cast_sbox8[U8a(z[3])];
745 break;
746 case 4:
747 key->xkey[i+0] ^= cast_sbox5[U8a(x[2])];
748 key->xkey[i+1] ^= cast_sbox6[U8b(x[3])];
749 key->xkey[i+2] ^= cast_sbox7[U8d(x[0])];
750 key->xkey[i+3] ^= cast_sbox8[U8d(x[1])];
751 break;
752 case 8:
753 key->xkey[i+0] ^= cast_sbox5[U8b(z[2])];
754 key->xkey[i+1] ^= cast_sbox6[U8a(z[3])];
755 key->xkey[i+2] ^= cast_sbox7[U8c(z[0])];
756 key->xkey[i+3] ^= cast_sbox8[U8c(z[1])];
757 break;
758 case 12:
759 key->xkey[i+0] ^= cast_sbox5[U8d(x[0])];
760 key->xkey[i+1] ^= cast_sbox6[U8d(x[1])];
761 key->xkey[i+2] ^= cast_sbox7[U8a(x[2])];
762 key->xkey[i+3] ^= cast_sbox8[U8b(x[3])];
763 break;
764 }
765 if (i >= 16) {
766 key->xkey[i+0] &= 31;
767 key->xkey[i+1] &= 31;
768 key->xkey[i+2] &= 31;
769 key->xkey[i+3] &= 31;
770 }
771 }
772 /* Wipe clean */
773 for (i = 0; i < 4; i++) {
774 t[i] = x[i] = z[i] = 0;
775 }
776}
777
778/* Made in Canada */
779
diff --git a/src/lib/libc/crypt/crypt.3 b/src/lib/libc/crypt/crypt.3
new file mode 100644
index 0000000000..524645de55
--- /dev/null
+++ b/src/lib/libc/crypt/crypt.3
@@ -0,0 +1,298 @@
1.\" $OpenBSD: crypt.3,v 1.11 1998/02/27 12:17:45 deraadt Exp $
2.\"
3.\" FreeSec: libcrypt
4.\"
5.\" Copyright (c) 1994 David Burren
6.\" All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 4. Neither the name of the author nor the names of other contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" Manual page, using -mandoc macros
33.\"
34.Dd March 9, 1994
35.Dt CRYPT 3
36.Os
37.Sh NAME
38.Nm crypt ,
39.Nm setkey ,
40.Nm encrypt ,
41.Nm des_setkey ,
42.Nm des_cipher
43.Nd DES encryption
44.Sh SYNOPSIS
45.Fd #include <unistd.h>
46.Ft char
47.Fn *crypt "const char *key" "const char *setting"
48.Ft int
49.Fn setkey "char *key"
50.Ft int
51.Fn encrypt "char *block" "int flag"
52.Ft int
53.Fn des_setkey "const char *key"
54.Ft int
55.Fn des_cipher "const char *in" "char *out" "int32_t salt" "int count"
56.Sh DESCRIPTION
57The
58.Fn crypt
59function performs password encryption, based on the
60.Tn NBS
61Data Encryption Standard (DES).
62Additional code has been added to deter key search attempts and to use
63stronger hashing algorithms.
64The first argument to
65.Fn crypt
66is a
67.Dv null Ns -terminated
68string, typically a user's typed password.
69The second is in one of three forms:
70if it begins with an underscore (``_'') then an extended format is used
71in interpreting both the key and the setting, as outlined below. If it begins
72with an string character (``$'') and a number then a different algorithm
73is used depending on the number. At the moment a ``$1'' chooses MD5 hashing
74and a ``$2'' chooses Blowfish hashing, see below for more information.
75.Ss Extended crypt:
76.Pp
77The
78.Ar key
79is divided into groups of 8 characters (the last group is null-padded)
80and the low-order 7 bits of each each character (56 bits per group) are
81used to form the DES key as follows:
82the first group of 56 bits becomes the initial DES key.
83For each additional group, the XOR of the encryption of the current DES
84key with itself and the group bits becomes the next DES key.
85.Pp
86The setting is a 9-character array consisting of an underscore followed
87by 4 bytes of iteration count and 4 bytes of salt.
88These are encoded as printable characters, 6 bits per character,
89least significant character first.
90The values 0 to 63 are encoded as ``./0-9A-Za-z''.
91This allows 24 bits for both
92.Fa count
93and
94.Fa salt .
95.Ss "MD5" crypt:
96.Pp
97For
98.Tn MD5
99crypt the version number,
100.Fa salt
101and the hashed password are separated
102by the ``$'' character. The maximum length of a password is limited by
103the length counter of the MD5 context, which is about
1042**64. A valid MD5 password entry looks like this:
105.Pp
106``$1$caeiHQwX$hsKqOjrFRRN6K32OWkCBf1''.
107.Pp
108The whole MD5 password string is passed as
109.Fa setting
110for interpretation.
111.Ss "Blowfish" crypt:
112.Pp
113The
114.Tn Blowfish
115version of crypt has 128 bits of
116.Fa salt
117in order to make building
118dictionaries of common passwords space consuming. The initial state
119of the
120.Tn Blowfish
121cipher is expanded using the
122.Fa salt
123and the
124.Fa password
125repeating the process a variable number of rounds, which is encoded in
126the password string. The maximum password length is 72. The final Blowfish
127password entry is created by encrypting
128the string ``OrpheanBeholderScryDoubt'' with the
129.Tn Blowfish
130state 64 times.
131.Pp
132The version number, the logarithm of the number of rounds and
133the concatenation of salt and
134hashed password are separated by the ``$'' character. An encoded ``8''
135would specify 256 rounds.
136A valid Blowfish password looks like this:
137.Pp
138``$2a$12$eIAq8PR8sIUnJ1HaohxX2O9x9Qlm2vK97LJ5dsXdmB.eXF42qjchC''.
139.Pp
140The whole Blowfish password string is passed as
141.Fa setting
142for interpretation.
143.Ss "Traditional" crypt:
144.Pp
145The first 8 bytes of the key are null-padded, and the low-order 7 bits of
146each character is used to form the 56-bit
147.Tn DES
148key.
149.Pp
150The setting is a 2-character array of the ASCII-encoded salt.
151Thus only 12 bits of
152.Fa salt
153are used.
154.Fa count
155is set to 25.
156.Ss DES Algorithm:
157.Pp
158The
159.Fa salt
160introduces disorder in the
161.Tn DES
162algorithm in one of 16777216 or 4096 possible ways
163(ie. with 24 or 12 bits: if bit
164.Em i
165of the
166.Ar salt
167is set, then bits
168.Em i
169and
170.Em i+24
171are swapped in the
172.Tn DES
173E-box output).
174.Pp
175The DES key is used to encrypt a 64-bit constant using
176.Ar count
177iterations of
178.Tn DES .
179The value returned is a
180.Dv null Ns -terminated
181string, 20 or 13 bytes (plus null) in length, consisting of the
182.Ar setting
183followed by the encoded 64-bit encryption.
184.Pp
185The functions,
186.Fn encrypt ,
187.Fn setkey ,
188.Fn des_setkey
189and
190.Fn des_cipher
191provide access to the
192.Tn DES
193algorithm itself.
194.Fn setkey
195is passed a 64-byte array of binary values (numeric 0 or 1).
196A 56-bit key is extracted from this array by dividing the
197array into groups of 8, and ignoring the last bit in each group.
198That bit is reserved for a byte parity check by DES, but is ignored
199by these functions.
200.Pp
201The
202.Fa block
203argument to
204.Fn encrypt
205is also a 64-byte array of binary values.
206If the value of
207.Fa flag
208is 0,
209.Fa block
210is encrypted otherwise it is decrypted.
211The result is returned in the original array
212.Fa block
213after using the key specified by
214.Fn setkey
215to process it.
216.Pp
217The argument to
218.Fn des_setkey
219is a character array of length 8.
220The least significant bit (the parity bit) in each character is ignored,
221and the remaining bits are concatenated to form a 56-bit key.
222The function
223.Fn des_cipher
224encrypts (or decrypts if
225.Fa count
226is negative) the 64-bits stored in the 8 characters at
227.Fa in
228using
229.Xr abs 3
230of
231.Fa count
232iterations of
233.Tn DES
234and stores the 64-bit result in the 8 characters at
235.Fa out
236(which may be the same as
237.Fa in
238).
239The
240.Fa salt
241specifies perturbations to the
242.Tn DES
243E-box output as described above.
244.Pp
245The function
246.Fn crypt
247returns a pointer to the encrypted value on success, and NULL on failure.
248The functions
249.Fn setkey ,
250.Fn encrypt ,
251.Fn des_setkey ,
252and
253.Fn des_cipher
254return 0 on success and 1 on failure.
255.Pp
256The
257.Fn crypt ,
258.Fn setkey
259and
260.Fn des_setkey
261functions all manipulate the same key space.
262.Sh SEE ALSO
263.Xr login 1 ,
264.Xr passwd 1 ,
265.Xr blowfish 3 ,
266.Xr getpass 3 ,
267.Xr md5 3 ,
268.Xr passwd 5
269.Sh BUGS
270The
271.Fn crypt
272function returns a pointer to static data, and subsequent calls to
273.Fn crypt
274will modify the same object.
275.Sh HISTORY
276A rotor-based
277.Fn crypt
278function appeared in
279.At v6 .
280The current style
281.Fn crypt
282first appeared in
283.At v7 .
284.Pp
285This library (FreeSec 1.0) was developed outside the United States of America
286as an unencumbered replacement for the U.S.-only libcrypt encryption
287library.
288Programs linked against the
289.Fn crypt
290interface may be exported from the U.S.A. only if they use
291.Fn crypt
292solely for authentication purposes and avoid use of
293the other programmer interfaces listed above. Special care has been taken
294in the library so that programs which only use the
295.Fn crypt
296interface do not pull in the other components.
297.Sh AUTHOR
298David Burren <davidb@werj.com.au>
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c
new file mode 100644
index 0000000000..8fd319a4f3
--- /dev/null
+++ b/src/lib/libc/crypt/crypt.c
@@ -0,0 +1,714 @@
1/* $OpenBSD: crypt.c,v 1.13 1998/03/22 19:01:18 niklas Exp $ */
2
3/*
4 * FreeSec: libcrypt
5 *
6 * Copyright (c) 1994 David Burren
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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 * 4. Neither the name of the author nor the names of other 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 AUTHOR 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 AUTHOR 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 *
34 * This is an original implementation of the DES and the crypt(3) interfaces
35 * by David Burren <davidb@werj.com.au>.
36 *
37 * An excellent reference on the underlying algorithm (and related
38 * algorithms) is:
39 *
40 * B. Schneier, Applied Cryptography: protocols, algorithms,
41 * and source code in C, John Wiley & Sons, 1994.
42 *
43 * Note that in that book's description of DES the lookups for the initial,
44 * pbox, and final permutations are inverted (this has been brought to the
45 * attention of the author). A list of errata for this book has been
46 * posted to the sci.crypt newsgroup by the author and is available for FTP.
47 *
48 * NOTE:
49 * This file has a static version of des_setkey() so that crypt.o exports
50 * only the crypt() interface. This is required to make binaries linked
51 * against crypt.o exportable or re-exportable from the USA.
52 */
53
54#if defined(LIBC_SCCS) && !defined(lint)
55static char rcsid[] = "$OpenBSD: crypt.c,v 1.13 1998/03/22 19:01:18 niklas Exp $";
56#endif /* LIBC_SCCS and not lint */
57
58#include <sys/types.h>
59#include <sys/param.h>
60#include <pwd.h>
61#include <string.h>
62
63#ifdef DEBUG
64# include <stdio.h>
65#endif
66
67static u_char IP[64] = {
68 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
69 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
70 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
71 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
72};
73
74static u_char inv_key_perm[64];
75static u_char u_key_perm[56];
76static u_char key_perm[56] = {
77 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
78 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
79 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
80 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
81};
82
83static u_char key_shifts[16] = {
84 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
85};
86
87static u_char inv_comp_perm[56];
88static u_char comp_perm[48] = {
89 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
90 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
91 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
92 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
93};
94
95/*
96 * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
97 */
98
99static u_char u_sbox[8][64];
100static u_char sbox[8][64] = {
101 {
102 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
103 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
104 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
105 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
106 },
107 {
108 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
109 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
110 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
111 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
112 },
113 {
114 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
115 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
116 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
117 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
118 },
119 {
120 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
121 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
122 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
123 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
124 },
125 {
126 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
127 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
128 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
129 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
130 },
131 {
132 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
133 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
134 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
135 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
136 },
137 {
138 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
139 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
140 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
141 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
142 },
143 {
144 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
145 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
146 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
147 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
148 }
149};
150
151static u_char un_pbox[32];
152static u_char pbox[32] = {
153 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
154 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
155};
156
157static u_int32_t bits32[32] =
158{
159 0x80000000, 0x40000000, 0x20000000, 0x10000000,
160 0x08000000, 0x04000000, 0x02000000, 0x01000000,
161 0x00800000, 0x00400000, 0x00200000, 0x00100000,
162 0x00080000, 0x00040000, 0x00020000, 0x00010000,
163 0x00008000, 0x00004000, 0x00002000, 0x00001000,
164 0x00000800, 0x00000400, 0x00000200, 0x00000100,
165 0x00000080, 0x00000040, 0x00000020, 0x00000010,
166 0x00000008, 0x00000004, 0x00000002, 0x00000001
167};
168
169static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
170
171static u_int32_t saltbits;
172static int32_t old_salt;
173static u_int32_t *bits28, *bits24;
174static u_char init_perm[64], final_perm[64];
175static u_int32_t en_keysl[16], en_keysr[16];
176static u_int32_t de_keysl[16], de_keysr[16];
177static int des_initialised = 0;
178static u_char m_sbox[4][4096];
179static u_int32_t psbox[4][256];
180static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
181static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
182static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
183static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
184static u_int32_t old_rawkey0, old_rawkey1;
185
186static u_char ascii64[] =
187 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
188/* 0000000000111111111122222222223333333333444444444455555555556666 */
189/* 0123456789012345678901234567890123456789012345678901234567890123 */
190
191static __inline int
192ascii_to_bin(ch)
193 char ch;
194{
195 if (ch > 'z')
196 return(0);
197 if (ch >= 'a')
198 return(ch - 'a' + 38);
199 if (ch > 'Z')
200 return(0);
201 if (ch >= 'A')
202 return(ch - 'A' + 12);
203 if (ch > '9')
204 return(0);
205 if (ch >= '.')
206 return(ch - '.');
207 return(0);
208}
209
210static void
211des_init()
212{
213 int i, j, b, k, inbit, obit;
214 u_int32_t *p, *il, *ir, *fl, *fr;
215
216 old_rawkey0 = old_rawkey1 = 0;
217 saltbits = 0;
218 old_salt = 0;
219 bits24 = (bits28 = bits32 + 4) + 4;
220
221 /*
222 * Invert the S-boxes, reordering the input bits.
223 */
224 for (i = 0; i < 8; i++)
225 for (j = 0; j < 64; j++) {
226 b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
227 u_sbox[i][j] = sbox[i][b];
228 }
229
230 /*
231 * Convert the inverted S-boxes into 4 arrays of 8 bits.
232 * Each will handle 12 bits of the S-box input.
233 */
234 for (b = 0; b < 4; b++)
235 for (i = 0; i < 64; i++)
236 for (j = 0; j < 64; j++)
237 m_sbox[b][(i << 6) | j] =
238 (u_sbox[(b << 1)][i] << 4) |
239 u_sbox[(b << 1) + 1][j];
240
241 /*
242 * Set up the initial & final permutations into a useful form, and
243 * initialise the inverted key permutation.
244 */
245 for (i = 0; i < 64; i++) {
246 init_perm[final_perm[i] = IP[i] - 1] = i;
247 inv_key_perm[i] = 255;
248 }
249
250 /*
251 * Invert the key permutation and initialise the inverted key
252 * compression permutation.
253 */
254 for (i = 0; i < 56; i++) {
255 u_key_perm[i] = key_perm[i] - 1;
256 inv_key_perm[key_perm[i] - 1] = i;
257 inv_comp_perm[i] = 255;
258 }
259
260 /*
261 * Invert the key compression permutation.
262 */
263 for (i = 0; i < 48; i++) {
264 inv_comp_perm[comp_perm[i] - 1] = i;
265 }
266
267 /*
268 * Set up the OR-mask arrays for the initial and final permutations,
269 * and for the key initial and compression permutations.
270 */
271 for (k = 0; k < 8; k++) {
272 for (i = 0; i < 256; i++) {
273 *(il = &ip_maskl[k][i]) = 0;
274 *(ir = &ip_maskr[k][i]) = 0;
275 *(fl = &fp_maskl[k][i]) = 0;
276 *(fr = &fp_maskr[k][i]) = 0;
277 for (j = 0; j < 8; j++) {
278 inbit = 8 * k + j;
279 if (i & bits8[j]) {
280 if ((obit = init_perm[inbit]) < 32)
281 *il |= bits32[obit];
282 else
283 *ir |= bits32[obit-32];
284 if ((obit = final_perm[inbit]) < 32)
285 *fl |= bits32[obit];
286 else
287 *fr |= bits32[obit - 32];
288 }
289 }
290 }
291 for (i = 0; i < 128; i++) {
292 *(il = &key_perm_maskl[k][i]) = 0;
293 *(ir = &key_perm_maskr[k][i]) = 0;
294 for (j = 0; j < 7; j++) {
295 inbit = 8 * k + j;
296 if (i & bits8[j + 1]) {
297 if ((obit = inv_key_perm[inbit]) == 255)
298 continue;
299 if (obit < 28)
300 *il |= bits28[obit];
301 else
302 *ir |= bits28[obit - 28];
303 }
304 }
305 *(il = &comp_maskl[k][i]) = 0;
306 *(ir = &comp_maskr[k][i]) = 0;
307 for (j = 0; j < 7; j++) {
308 inbit = 7 * k + j;
309 if (i & bits8[j + 1]) {
310 if ((obit=inv_comp_perm[inbit]) == 255)
311 continue;
312 if (obit < 24)
313 *il |= bits24[obit];
314 else
315 *ir |= bits24[obit - 24];
316 }
317 }
318 }
319 }
320
321 /*
322 * Invert the P-box permutation, and convert into OR-masks for
323 * handling the output of the S-box arrays setup above.
324 */
325 for (i = 0; i < 32; i++)
326 un_pbox[pbox[i] - 1] = i;
327
328 for (b = 0; b < 4; b++)
329 for (i = 0; i < 256; i++) {
330 *(p = &psbox[b][i]) = 0;
331 for (j = 0; j < 8; j++) {
332 if (i & bits8[j])
333 *p |= bits32[un_pbox[8 * b + j]];
334 }
335 }
336
337 des_initialised = 1;
338}
339
340static void
341setup_salt(salt)
342 int32_t salt;
343{
344 u_int32_t obit, saltbit;
345 int i;
346
347 if (salt == old_salt)
348 return;
349 old_salt = salt;
350
351 saltbits = 0;
352 saltbit = 1;
353 obit = 0x800000;
354 for (i = 0; i < 24; i++) {
355 if (salt & saltbit)
356 saltbits |= obit;
357 saltbit <<= 1;
358 obit >>= 1;
359 }
360}
361
362static int
363des_setkey(key)
364 const char *key;
365{
366 u_int32_t k0, k1, rawkey0, rawkey1;
367 int shifts, round;
368
369 if (!des_initialised)
370 des_init();
371
372 rawkey0 = ntohl(*(u_int32_t *) key);
373 rawkey1 = ntohl(*(u_int32_t *) (key + 4));
374
375 if ((rawkey0 | rawkey1)
376 && rawkey0 == old_rawkey0
377 && rawkey1 == old_rawkey1) {
378 /*
379 * Already setup for this key.
380 * This optimisation fails on a zero key (which is weak and
381 * has bad parity anyway) in order to simplify the starting
382 * conditions.
383 */
384 return(0);
385 }
386 old_rawkey0 = rawkey0;
387 old_rawkey1 = rawkey1;
388
389 /*
390 * Do key permutation and split into two 28-bit subkeys.
391 */
392 k0 = key_perm_maskl[0][rawkey0 >> 25]
393 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
394 | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
395 | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
396 | key_perm_maskl[4][rawkey1 >> 25]
397 | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
398 | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
399 | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
400 k1 = key_perm_maskr[0][rawkey0 >> 25]
401 | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
402 | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
403 | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
404 | key_perm_maskr[4][rawkey1 >> 25]
405 | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
406 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
407 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
408 /*
409 * Rotate subkeys and do compression permutation.
410 */
411 shifts = 0;
412 for (round = 0; round < 16; round++) {
413 u_int32_t t0, t1;
414
415 shifts += key_shifts[round];
416
417 t0 = (k0 << shifts) | (k0 >> (28 - shifts));
418 t1 = (k1 << shifts) | (k1 >> (28 - shifts));
419
420 de_keysl[15 - round] =
421 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
422 | comp_maskl[1][(t0 >> 14) & 0x7f]
423 | comp_maskl[2][(t0 >> 7) & 0x7f]
424 | comp_maskl[3][t0 & 0x7f]
425 | comp_maskl[4][(t1 >> 21) & 0x7f]
426 | comp_maskl[5][(t1 >> 14) & 0x7f]
427 | comp_maskl[6][(t1 >> 7) & 0x7f]
428 | comp_maskl[7][t1 & 0x7f];
429
430 de_keysr[15 - round] =
431 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
432 | comp_maskr[1][(t0 >> 14) & 0x7f]
433 | comp_maskr[2][(t0 >> 7) & 0x7f]
434 | comp_maskr[3][t0 & 0x7f]
435 | comp_maskr[4][(t1 >> 21) & 0x7f]
436 | comp_maskr[5][(t1 >> 14) & 0x7f]
437 | comp_maskr[6][(t1 >> 7) & 0x7f]
438 | comp_maskr[7][t1 & 0x7f];
439 }
440 return(0);
441}
442
443static int
444do_des(l_in, r_in, l_out, r_out, count)
445 u_int32_t l_in, r_in, *l_out, *r_out;
446 int count;
447{
448 /*
449 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
450 */
451 u_int32_t l, r, *kl, *kr, *kl1, *kr1;
452 u_int32_t f, r48l, r48r;
453 int round;
454
455 if (count == 0) {
456 return(1);
457 } else if (count > 0) {
458 /*
459 * Encrypting
460 */
461 kl1 = en_keysl;
462 kr1 = en_keysr;
463 } else {
464 /*
465 * Decrypting
466 */
467 count = -count;
468 kl1 = de_keysl;
469 kr1 = de_keysr;
470 }
471
472 /*
473 * Do initial permutation (IP).
474 */
475 l = ip_maskl[0][l_in >> 24]
476 | ip_maskl[1][(l_in >> 16) & 0xff]
477 | ip_maskl[2][(l_in >> 8) & 0xff]
478 | ip_maskl[3][l_in & 0xff]
479 | ip_maskl[4][r_in >> 24]
480 | ip_maskl[5][(r_in >> 16) & 0xff]
481 | ip_maskl[6][(r_in >> 8) & 0xff]
482 | ip_maskl[7][r_in & 0xff];
483 r = ip_maskr[0][l_in >> 24]
484 | ip_maskr[1][(l_in >> 16) & 0xff]
485 | ip_maskr[2][(l_in >> 8) & 0xff]
486 | ip_maskr[3][l_in & 0xff]
487 | ip_maskr[4][r_in >> 24]
488 | ip_maskr[5][(r_in >> 16) & 0xff]
489 | ip_maskr[6][(r_in >> 8) & 0xff]
490 | ip_maskr[7][r_in & 0xff];
491
492 while (count--) {
493 /*
494 * Do each round.
495 */
496 kl = kl1;
497 kr = kr1;
498 round = 16;
499 while (round--) {
500 /*
501 * Expand R to 48 bits (simulate the E-box).
502 */
503 r48l = ((r & 0x00000001) << 23)
504 | ((r & 0xf8000000) >> 9)
505 | ((r & 0x1f800000) >> 11)
506 | ((r & 0x01f80000) >> 13)
507 | ((r & 0x001f8000) >> 15);
508
509 r48r = ((r & 0x0001f800) << 7)
510 | ((r & 0x00001f80) << 5)
511 | ((r & 0x000001f8) << 3)
512 | ((r & 0x0000001f) << 1)
513 | ((r & 0x80000000) >> 31);
514 /*
515 * Do salting for crypt() and friends, and
516 * XOR with the permuted key.
517 */
518 f = (r48l ^ r48r) & saltbits;
519 r48l ^= f ^ *kl++;
520 r48r ^= f ^ *kr++;
521 /*
522 * Do sbox lookups (which shrink it back to 32 bits)
523 * and do the pbox permutation at the same time.
524 */
525 f = psbox[0][m_sbox[0][r48l >> 12]]
526 | psbox[1][m_sbox[1][r48l & 0xfff]]
527 | psbox[2][m_sbox[2][r48r >> 12]]
528 | psbox[3][m_sbox[3][r48r & 0xfff]];
529 /*
530 * Now that we've permuted things, complete f().
531 */
532 f ^= l;
533 l = r;
534 r = f;
535 }
536 r = l;
537 l = f;
538 }
539 /*
540 * Do final permutation (inverse of IP).
541 */
542 *l_out = fp_maskl[0][l >> 24]
543 | fp_maskl[1][(l >> 16) & 0xff]
544 | fp_maskl[2][(l >> 8) & 0xff]
545 | fp_maskl[3][l & 0xff]
546 | fp_maskl[4][r >> 24]
547 | fp_maskl[5][(r >> 16) & 0xff]
548 | fp_maskl[6][(r >> 8) & 0xff]
549 | fp_maskl[7][r & 0xff];
550 *r_out = fp_maskr[0][l >> 24]
551 | fp_maskr[1][(l >> 16) & 0xff]
552 | fp_maskr[2][(l >> 8) & 0xff]
553 | fp_maskr[3][l & 0xff]
554 | fp_maskr[4][r >> 24]
555 | fp_maskr[5][(r >> 16) & 0xff]
556 | fp_maskr[6][(r >> 8) & 0xff]
557 | fp_maskr[7][r & 0xff];
558 return(0);
559}
560
561static int
562des_cipher(in, out, salt, count)
563 const char *in;
564 char *out;
565 int32_t salt;
566 int count;
567{
568 u_int32_t l_out, r_out, rawl, rawr;
569 u_int32_t x[2];
570 int retval;
571
572 if (!des_initialised)
573 des_init();
574
575 setup_salt(salt);
576
577 memcpy(x, in, sizeof x);
578 rawl = ntohl(x[0]);
579 rawr = ntohl(x[1]);
580 retval = do_des(rawl, rawr, &l_out, &r_out, count);
581
582 x[0] = htonl(l_out);
583 x[1] = htonl(r_out);
584 memcpy(out, x, sizeof x);
585 return(retval);
586}
587
588char *
589crypt(key, setting)
590 const char *key;
591 const char *setting;
592{
593 int i;
594 u_int32_t count, salt, l, r0, r1, keybuf[2];
595 u_char *p, *q;
596 static u_char output[21];
597 extern char *md5crypt __P((const char *, const char *));
598 extern char *bcrypt __P((const char *, const char *));
599
600 if (setting[0] == '$') {
601 switch (setting[1]) {
602 case '1':
603 return (md5crypt(key, setting));
604 default:
605 return bcrypt(key, setting);
606 }
607 }
608
609 if (!des_initialised)
610 des_init();
611
612 /*
613 * Copy the key, shifting each character up by one bit
614 * and padding with zeros.
615 */
616 q = (u_char *) keybuf;
617 while ((q - (u_char *) keybuf) < sizeof(keybuf)) {
618 if ((*q++ = *key << 1))
619 key++;
620 }
621 if (des_setkey((u_char *) keybuf))
622 return(NULL);
623
624 if (*setting == _PASSWORD_EFMT1) {
625 /*
626 * "new"-style:
627 * setting - underscore, 4 bytes of count, 4 bytes of salt
628 * key - unlimited characters
629 */
630 for (i = 1, count = 0; i < 5; i++)
631 count |= ascii_to_bin(setting[i]) << (i - 1) * 6;
632
633 for (i = 5, salt = 0; i < 9; i++)
634 salt |= ascii_to_bin(setting[i]) << (i - 5) * 6;
635
636 while (*key) {
637 /*
638 * Encrypt the key with itself.
639 */
640 if (des_cipher((u_char*)keybuf, (u_char*)keybuf, 0, 1))
641 return(NULL);
642 /*
643 * And XOR with the next 8 characters of the key.
644 */
645 q = (u_char *) keybuf;
646 while (((q - (u_char *) keybuf) < sizeof(keybuf)) &&
647 *key)
648 *q++ ^= *key++ << 1;
649
650 if (des_setkey((u_char *) keybuf))
651 return(NULL);
652 }
653 strncpy((char *)output, setting, 9);
654
655 /*
656 * Double check that we weren't given a short setting.
657 * If we were, the above code will probably have created
658 * wierd values for count and salt, but we don't really care.
659 * Just make sure the output string doesn't have an extra
660 * NUL in it.
661 */
662 output[9] = '\0';
663 p = output + strlen((const char *)output);
664 } else {
665 /*
666 * "old"-style:
667 * setting - 2 bytes of salt
668 * key - up to 8 characters
669 */
670 count = 25;
671
672 salt = (ascii_to_bin(setting[1]) << 6)
673 | ascii_to_bin(setting[0]);
674
675 output[0] = setting[0];
676 /*
677 * If the encrypted password that the salt was extracted from
678 * is only 1 character long, the salt will be corrupted. We
679 * need to ensure that the output string doesn't have an extra
680 * NUL in it!
681 */
682 output[1] = setting[1] ? setting[1] : output[0];
683
684 p = output + 2;
685 }
686 setup_salt(salt);
687 /*
688 * Do it.
689 */
690 if (do_des(0, 0, &r0, &r1, count))
691 return(NULL);
692 /*
693 * Now encode the result...
694 */
695 l = (r0 >> 8);
696 *p++ = ascii64[(l >> 18) & 0x3f];
697 *p++ = ascii64[(l >> 12) & 0x3f];
698 *p++ = ascii64[(l >> 6) & 0x3f];
699 *p++ = ascii64[l & 0x3f];
700
701 l = (r0 << 16) | ((r1 >> 16) & 0xffff);
702 *p++ = ascii64[(l >> 18) & 0x3f];
703 *p++ = ascii64[(l >> 12) & 0x3f];
704 *p++ = ascii64[(l >> 6) & 0x3f];
705 *p++ = ascii64[l & 0x3f];
706
707 l = r1 << 2;
708 *p++ = ascii64[(l >> 12) & 0x3f];
709 *p++ = ascii64[(l >> 6) & 0x3f];
710 *p++ = ascii64[l & 0x3f];
711 *p = 0;
712
713 return((char *)output);
714}
diff --git a/src/lib/libc/crypt/md5crypt.c b/src/lib/libc/crypt/md5crypt.c
new file mode 100644
index 0000000000..7ec60f38e0
--- /dev/null
+++ b/src/lib/libc/crypt/md5crypt.c
@@ -0,0 +1,157 @@
1/* $OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp $ */
2
3/*
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
10 *
11 * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $
12 *
13 */
14
15#if defined(LIBC_SCCS) && !defined(lint)
16static char rcsid[] = "$OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp $";
17#endif /* LIBC_SCCS and not lint */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <string.h>
22#include <md5.h>
23#include <string.h>
24
25static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
26 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
27
28static void to64 __P((char *, u_int32_t, int));
29
30static void
31to64(s, v, n)
32 char *s;
33 u_int32_t v;
34 int n;
35{
36 while (--n >= 0) {
37 *s++ = itoa64[v&0x3f];
38 v >>= 6;
39 }
40}
41
42/*
43 * UNIX password
44 *
45 * Use MD5 for what it is best at...
46 */
47
48char *
49md5crypt(pw, salt)
50 register const char *pw;
51 register const char *salt;
52{
53 /*
54 * This string is magic for this algorithm. Having
55 * it this way, we can get get better later on
56 */
57 static unsigned char *magic = (unsigned char *)"$1$";
58
59 static char passwd[120], *p;
60 static const unsigned char *sp,*ep;
61 unsigned char final[16];
62 int sl,pl,i;
63 MD5_CTX ctx,ctx1;
64 u_int32_t l;
65
66 /* Refine the Salt first */
67 sp = (const unsigned char *)salt;
68
69 /* If it starts with the magic string, then skip that */
70 if(!strncmp((const char *)sp,(const char *)magic,strlen((const char *)magic)))
71 sp += strlen((const char *)magic);
72
73 /* It stops at the first '$', max 8 chars */
74 for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
75 continue;
76
77 /* get the length of the true salt */
78 sl = ep - sp;
79
80 MD5Init(&ctx);
81
82 /* The password first, since that is what is most unknown */
83 MD5Update(&ctx,(const unsigned char *)pw,strlen(pw));
84
85 /* Then our magic string */
86 MD5Update(&ctx,magic,strlen((const char *)magic));
87
88 /* Then the raw salt */
89 MD5Update(&ctx,sp,sl);
90
91 /* Then just as many characters of the MD5(pw,salt,pw) */
92 MD5Init(&ctx1);
93 MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw));
94 MD5Update(&ctx1,sp,sl);
95 MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw));
96 MD5Final(final,&ctx1);
97 for(pl = strlen(pw); pl > 0; pl -= 16)
98 MD5Update(&ctx,final,pl>16 ? 16 : pl);
99
100 /* Don't leave anything around in vm they could use. */
101 memset(final,0,sizeof final);
102
103 /* Then something really weird... */
104 for (i = strlen(pw); i ; i >>= 1)
105 if(i&1)
106 MD5Update(&ctx, final, 1);
107 else
108 MD5Update(&ctx, (const unsigned char *)pw, 1);
109
110 /* Now make the output string */
111 strcpy(passwd,(const char *)magic);
112 strncat(passwd,(const char *)sp,sl);
113 strcat(passwd,"$");
114
115 MD5Final(final,&ctx);
116
117 /*
118 * and now, just to make sure things don't run too fast
119 * On a 60 Mhz Pentium this takes 34 msec, so you would
120 * need 30 seconds to build a 1000 entry dictionary...
121 */
122 for(i=0;i<1000;i++) {
123 MD5Init(&ctx1);
124 if(i & 1)
125 MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw));
126 else
127 MD5Update(&ctx1,final,16);
128
129 if(i % 3)
130 MD5Update(&ctx1,sp,sl);
131
132 if(i % 7)
133 MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw));
134
135 if(i & 1)
136 MD5Update(&ctx1,final,16);
137 else
138 MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw));
139 MD5Final(final,&ctx1);
140 }
141
142 p = passwd + strlen(passwd);
143
144 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
145 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
146 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
147 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
148 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
149 l = final[11] ; to64(p,l,2); p += 2;
150 *p = '\0';
151
152 /* Don't leave anything around in vm they could use. */
153 memset(final,0,sizeof final);
154
155 return passwd;
156}
157
diff --git a/src/lib/libc/crypt/morecrypt.c b/src/lib/libc/crypt/morecrypt.c
new file mode 100644
index 0000000000..e9e0ced4c1
--- /dev/null
+++ b/src/lib/libc/crypt/morecrypt.c
@@ -0,0 +1,628 @@
1/* $OpenBSD: morecrypt.c,v 1.9 1998/03/22 19:01:20 niklas Exp $ */
2
3/*
4 * FreeSec: libcrypt
5 *
6 * Copyright (c) 1994 David Burren
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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 * 4. Neither the name of the author nor the names of other 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 AUTHOR 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 AUTHOR 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 *
34 * This is an original implementation of the DES and the crypt(3) interfaces
35 * by David Burren <davidb@werj.com.au>.
36 *
37 * An excellent reference on the underlying algorithm (and related
38 * algorithms) is:
39 *
40 * B. Schneier, Applied Cryptography: protocols, algorithms,
41 * and source code in C, John Wiley & Sons, 1994.
42 *
43 * Note that in that book's description of DES the lookups for the initial,
44 * pbox, and final permutations are inverted (this has been brought to the
45 * attention of the author). A list of errata for this book has been
46 * posted to the sci.crypt newsgroup by the author and is available for FTP.
47 *
48 * NOTE:
49 * This file must copy certain chunks of crypt.c for legal reasons.
50 * crypt.c can only export the interface crypt(), to make binaries
51 * exportable from the USA. Hence, to also have the other crypto interfaces
52 * available we have to copy pieces...
53 */
54
55#if defined(LIBC_SCCS) && !defined(lint)
56static char rcsid[] = "$OpenBSD: morecrypt.c,v 1.9 1998/03/22 19:01:20 niklas Exp $";
57#endif /* LIBC_SCCS and not lint */
58
59#include <sys/types.h>
60#include <sys/param.h>
61#include <pwd.h>
62#include <string.h>
63
64#ifdef DEBUG
65# include <stdio.h>
66#endif
67
68static u_char IP[64] = {
69 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
70 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
71 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
72 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
73};
74
75static u_char inv_key_perm[64];
76static u_char u_key_perm[56];
77static u_char key_perm[56] = {
78 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
79 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
80 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
81 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
82};
83
84static u_char key_shifts[16] = {
85 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
86};
87
88static u_char inv_comp_perm[56];
89static u_char comp_perm[48] = {
90 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
91 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
92 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
93 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
94};
95
96/*
97 * No E box is used, as it's replaced by some ANDs, shifts, and ORs.
98 */
99
100static u_char u_sbox[8][64];
101static u_char sbox[8][64] = {
102 {
103 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
104 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
105 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
106 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
107 },
108 {
109 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
110 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
111 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
112 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
113 },
114 {
115 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
116 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
117 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
118 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
119 },
120 {
121 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
122 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
123 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
124 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
125 },
126 {
127 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
128 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
129 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
130 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
131 },
132 {
133 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
134 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
135 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
136 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
137 },
138 {
139 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
140 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
141 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
142 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
143 },
144 {
145 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
146 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
147 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
148 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
149 }
150};
151
152static u_char un_pbox[32];
153static u_char pbox[32] = {
154 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
155 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
156};
157
158static u_int32_t bits32[32] =
159{
160 0x80000000, 0x40000000, 0x20000000, 0x10000000,
161 0x08000000, 0x04000000, 0x02000000, 0x01000000,
162 0x00800000, 0x00400000, 0x00200000, 0x00100000,
163 0x00080000, 0x00040000, 0x00020000, 0x00010000,
164 0x00008000, 0x00004000, 0x00002000, 0x00001000,
165 0x00000800, 0x00000400, 0x00000200, 0x00000100,
166 0x00000080, 0x00000040, 0x00000020, 0x00000010,
167 0x00000008, 0x00000004, 0x00000002, 0x00000001
168};
169
170static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
171
172static u_int32_t saltbits;
173static int32_t old_salt;
174static u_int32_t *bits28, *bits24;
175static u_char init_perm[64], final_perm[64];
176static u_int32_t en_keysl[16], en_keysr[16];
177static u_int32_t de_keysl[16], de_keysr[16];
178static int des_initialised = 0;
179static u_char m_sbox[4][4096];
180static u_int32_t psbox[4][256];
181static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
182static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
183static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
184static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
185static u_int32_t old_rawkey0, old_rawkey1;
186
187static __inline int
188ascii_to_bin(ch)
189 char ch;
190{
191 if (ch > 'z')
192 return(0);
193 if (ch >= 'a')
194 return(ch - 'a' + 38);
195 if (ch > 'Z')
196 return(0);
197 if (ch >= 'A')
198 return(ch - 'A' + 12);
199 if (ch > '9')
200 return(0);
201 if (ch >= '.')
202 return(ch - '.');
203 return(0);
204}
205
206void
207des_init()
208{
209 int i, j, b, k, inbit, obit;
210 u_int32_t *p, *il, *ir, *fl, *fr;
211
212 old_rawkey0 = old_rawkey1 = 0;
213 saltbits = 0;
214 old_salt = 0;
215 bits24 = (bits28 = bits32 + 4) + 4;
216
217 /*
218 * Invert the S-boxes, reordering the input bits.
219 */
220 for (i = 0; i < 8; i++)
221 for (j = 0; j < 64; j++) {
222 b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
223 u_sbox[i][j] = sbox[i][b];
224 }
225
226 /*
227 * Convert the inverted S-boxes into 4 arrays of 8 bits.
228 * Each will handle 12 bits of the S-box input.
229 */
230 for (b = 0; b < 4; b++)
231 for (i = 0; i < 64; i++)
232 for (j = 0; j < 64; j++)
233 m_sbox[b][(i << 6) | j] =
234 (u_sbox[(b << 1)][i] << 4) |
235 u_sbox[(b << 1) + 1][j];
236
237 /*
238 * Set up the initial & final permutations into a useful form, and
239 * initialise the inverted key permutation.
240 */
241 for (i = 0; i < 64; i++) {
242 init_perm[final_perm[i] = IP[i] - 1] = i;
243 inv_key_perm[i] = 255;
244 }
245
246 /*
247 * Invert the key permutation and initialise the inverted key
248 * compression permutation.
249 */
250 for (i = 0; i < 56; i++) {
251 u_key_perm[i] = key_perm[i] - 1;
252 inv_key_perm[key_perm[i] - 1] = i;
253 inv_comp_perm[i] = 255;
254 }
255
256 /*
257 * Invert the key compression permutation.
258 */
259 for (i = 0; i < 48; i++) {
260 inv_comp_perm[comp_perm[i] - 1] = i;
261 }
262
263 /*
264 * Set up the OR-mask arrays for the initial and final permutations,
265 * and for the key initial and compression permutations.
266 */
267 for (k = 0; k < 8; k++) {
268 for (i = 0; i < 256; i++) {
269 *(il = &ip_maskl[k][i]) = 0;
270 *(ir = &ip_maskr[k][i]) = 0;
271 *(fl = &fp_maskl[k][i]) = 0;
272 *(fr = &fp_maskr[k][i]) = 0;
273 for (j = 0; j < 8; j++) {
274 inbit = 8 * k + j;
275 if (i & bits8[j]) {
276 if ((obit = init_perm[inbit]) < 32)
277 *il |= bits32[obit];
278 else
279 *ir |= bits32[obit-32];
280 if ((obit = final_perm[inbit]) < 32)
281 *fl |= bits32[obit];
282 else
283 *fr |= bits32[obit - 32];
284 }
285 }
286 }
287 for (i = 0; i < 128; i++) {
288 *(il = &key_perm_maskl[k][i]) = 0;
289 *(ir = &key_perm_maskr[k][i]) = 0;
290 for (j = 0; j < 7; j++) {
291 inbit = 8 * k + j;
292 if (i & bits8[j + 1]) {
293 if ((obit = inv_key_perm[inbit]) == 255)
294 continue;
295 if (obit < 28)
296 *il |= bits28[obit];
297 else
298 *ir |= bits28[obit - 28];
299 }
300 }
301 *(il = &comp_maskl[k][i]) = 0;
302 *(ir = &comp_maskr[k][i]) = 0;
303 for (j = 0; j < 7; j++) {
304 inbit = 7 * k + j;
305 if (i & bits8[j + 1]) {
306 if ((obit=inv_comp_perm[inbit]) == 255)
307 continue;
308 if (obit < 24)
309 *il |= bits24[obit];
310 else
311 *ir |= bits24[obit - 24];
312 }
313 }
314 }
315 }
316
317 /*
318 * Invert the P-box permutation, and convert into OR-masks for
319 * handling the output of the S-box arrays setup above.
320 */
321 for (i = 0; i < 32; i++)
322 un_pbox[pbox[i] - 1] = i;
323
324 for (b = 0; b < 4; b++)
325 for (i = 0; i < 256; i++) {
326 *(p = &psbox[b][i]) = 0;
327 for (j = 0; j < 8; j++) {
328 if (i & bits8[j])
329 *p |= bits32[un_pbox[8 * b + j]];
330 }
331 }
332
333 des_initialised = 1;
334}
335
336void
337setup_salt(salt)
338 int32_t salt;
339{
340 u_int32_t obit, saltbit;
341 int i;
342
343 if (salt == old_salt)
344 return;
345 old_salt = salt;
346
347 saltbits = 0;
348 saltbit = 1;
349 obit = 0x800000;
350 for (i = 0; i < 24; i++) {
351 if (salt & saltbit)
352 saltbits |= obit;
353 saltbit <<= 1;
354 obit >>= 1;
355 }
356}
357
358int
359do_des(l_in, r_in, l_out, r_out, count)
360 u_int32_t l_in, r_in, *l_out, *r_out;
361 int count;
362{
363 /*
364 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
365 */
366 u_int32_t l, r, *kl, *kr, *kl1, *kr1;
367 u_int32_t f, r48l, r48r;
368 int round;
369
370 if (count == 0) {
371 return(1);
372 } else if (count > 0) {
373 /*
374 * Encrypting
375 */
376 kl1 = en_keysl;
377 kr1 = en_keysr;
378 } else {
379 /*
380 * Decrypting
381 */
382 count = -count;
383 kl1 = de_keysl;
384 kr1 = de_keysr;
385 }
386
387 /*
388 * Do initial permutation (IP).
389 */
390 l = ip_maskl[0][l_in >> 24]
391 | ip_maskl[1][(l_in >> 16) & 0xff]
392 | ip_maskl[2][(l_in >> 8) & 0xff]
393 | ip_maskl[3][l_in & 0xff]
394 | ip_maskl[4][r_in >> 24]
395 | ip_maskl[5][(r_in >> 16) & 0xff]
396 | ip_maskl[6][(r_in >> 8) & 0xff]
397 | ip_maskl[7][r_in & 0xff];
398 r = ip_maskr[0][l_in >> 24]
399 | ip_maskr[1][(l_in >> 16) & 0xff]
400 | ip_maskr[2][(l_in >> 8) & 0xff]
401 | ip_maskr[3][l_in & 0xff]
402 | ip_maskr[4][r_in >> 24]
403 | ip_maskr[5][(r_in >> 16) & 0xff]
404 | ip_maskr[6][(r_in >> 8) & 0xff]
405 | ip_maskr[7][r_in & 0xff];
406
407 while (count--) {
408 /*
409 * Do each round.
410 */
411 kl = kl1;
412 kr = kr1;
413 round = 16;
414 while (round--) {
415 /*
416 * Expand R to 48 bits (simulate the E-box).
417 */
418 r48l = ((r & 0x00000001) << 23)
419 | ((r & 0xf8000000) >> 9)
420 | ((r & 0x1f800000) >> 11)
421 | ((r & 0x01f80000) >> 13)
422 | ((r & 0x001f8000) >> 15);
423
424 r48r = ((r & 0x0001f800) << 7)
425 | ((r & 0x00001f80) << 5)
426 | ((r & 0x000001f8) << 3)
427 | ((r & 0x0000001f) << 1)
428 | ((r & 0x80000000) >> 31);
429 /*
430 * Do salting for crypt() and friends, and
431 * XOR with the permuted key.
432 */
433 f = (r48l ^ r48r) & saltbits;
434 r48l ^= f ^ *kl++;
435 r48r ^= f ^ *kr++;
436 /*
437 * Do sbox lookups (which shrink it back to 32 bits)
438 * and do the pbox permutation at the same time.
439 */
440 f = psbox[0][m_sbox[0][r48l >> 12]]
441 | psbox[1][m_sbox[1][r48l & 0xfff]]
442 | psbox[2][m_sbox[2][r48r >> 12]]
443 | psbox[3][m_sbox[3][r48r & 0xfff]];
444 /*
445 * Now that we've permuted things, complete f().
446 */
447 f ^= l;
448 l = r;
449 r = f;
450 }
451 r = l;
452 l = f;
453 }
454 /*
455 * Do final permutation (inverse of IP).
456 */
457 *l_out = fp_maskl[0][l >> 24]
458 | fp_maskl[1][(l >> 16) & 0xff]
459 | fp_maskl[2][(l >> 8) & 0xff]
460 | fp_maskl[3][l & 0xff]
461 | fp_maskl[4][r >> 24]
462 | fp_maskl[5][(r >> 16) & 0xff]
463 | fp_maskl[6][(r >> 8) & 0xff]
464 | fp_maskl[7][r & 0xff];
465 *r_out = fp_maskr[0][l >> 24]
466 | fp_maskr[1][(l >> 16) & 0xff]
467 | fp_maskr[2][(l >> 8) & 0xff]
468 | fp_maskr[3][l & 0xff]
469 | fp_maskr[4][r >> 24]
470 | fp_maskr[5][(r >> 16) & 0xff]
471 | fp_maskr[6][(r >> 8) & 0xff]
472 | fp_maskr[7][r & 0xff];
473 return(0);
474}
475
476int
477des_cipher(in, out, salt, count)
478 const char *in;
479 char *out;
480 long salt;
481 int count;
482{
483 u_int32_t l_out, r_out, rawl, rawr;
484 u_int32_t x[2];
485 int retval;
486
487 if (!des_initialised)
488 des_init();
489
490 setup_salt((int32_t)salt);
491
492 memcpy(x, in, sizeof x);
493 rawl = ntohl(x[0]);
494 rawr = ntohl(x[1]);
495 retval = do_des(rawl, rawr, &l_out, &r_out, count);
496
497 x[0] = htonl(l_out);
498 x[1] = htonl(r_out);
499 memcpy(out, x, sizeof x);
500 return(retval);
501}
502
503int
504des_setkey(key)
505 const char *key;
506{
507 u_int32_t k0, k1, rawkey0, rawkey1;
508 int shifts, round;
509
510 if (!des_initialised)
511 des_init();
512
513 rawkey0 = ntohl(*(u_int32_t *) key);
514 rawkey1 = ntohl(*(u_int32_t *) (key + 4));
515
516 if ((rawkey0 | rawkey1)
517 && rawkey0 == old_rawkey0
518 && rawkey1 == old_rawkey1) {
519 /*
520 * Already setup for this key.
521 * This optimisation fails on a zero key (which is weak and
522 * has bad parity anyway) in order to simplify the starting
523 * conditions.
524 */
525 return(0);
526 }
527 old_rawkey0 = rawkey0;
528 old_rawkey1 = rawkey1;
529
530 /*
531 * Do key permutation and split into two 28-bit subkeys.
532 */
533 k0 = key_perm_maskl[0][rawkey0 >> 25]
534 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
535 | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
536 | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
537 | key_perm_maskl[4][rawkey1 >> 25]
538 | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
539 | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
540 | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
541 k1 = key_perm_maskr[0][rawkey0 >> 25]
542 | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
543 | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
544 | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
545 | key_perm_maskr[4][rawkey1 >> 25]
546 | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
547 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
548 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
549 /*
550 * Rotate subkeys and do compression permutation.
551 */
552 shifts = 0;
553 for (round = 0; round < 16; round++) {
554 u_int32_t t0, t1;
555
556 shifts += key_shifts[round];
557
558 t0 = (k0 << shifts) | (k0 >> (28 - shifts));
559 t1 = (k1 << shifts) | (k1 >> (28 - shifts));
560
561 de_keysl[15 - round] =
562 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
563 | comp_maskl[1][(t0 >> 14) & 0x7f]
564 | comp_maskl[2][(t0 >> 7) & 0x7f]
565 | comp_maskl[3][t0 & 0x7f]
566 | comp_maskl[4][(t1 >> 21) & 0x7f]
567 | comp_maskl[5][(t1 >> 14) & 0x7f]
568 | comp_maskl[6][(t1 >> 7) & 0x7f]
569 | comp_maskl[7][t1 & 0x7f];
570
571 de_keysr[15 - round] =
572 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
573 | comp_maskr[1][(t0 >> 14) & 0x7f]
574 | comp_maskr[2][(t0 >> 7) & 0x7f]
575 | comp_maskr[3][t0 & 0x7f]
576 | comp_maskr[4][(t1 >> 21) & 0x7f]
577 | comp_maskr[5][(t1 >> 14) & 0x7f]
578 | comp_maskr[6][(t1 >> 7) & 0x7f]
579 | comp_maskr[7][t1 & 0x7f];
580 }
581 return(0);
582}
583
584int
585setkey(key)
586 const char *key;
587{
588 int i, j;
589 u_int32_t packed_keys[2];
590 u_char *p;
591
592 p = (u_char *) packed_keys;
593
594 for (i = 0; i < 8; i++) {
595 p[i] = 0;
596 for (j = 0; j < 8; j++)
597 if (*key++ & 1)
598 p[i] |= bits8[j];
599 }
600 return(des_setkey(p));
601}
602
603int
604encrypt(block, flag)
605 char *block;
606 int flag;
607{
608 u_int32_t io[2];
609 u_char *p;
610 int i, j, retval;
611
612 if (!des_initialised)
613 des_init();
614
615 setup_salt((int32_t)0);
616 p = (u_char *)block;
617 for (i = 0; i < 2; i++) {
618 io[i] = 0L;
619 for (j = 0; j < 32; j++)
620 if (*p++ & 1)
621 io[i] |= bits32[j];
622 }
623 retval = do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
624 for (i = 0; i < 2; i++)
625 for (j = 0; j < 32; j++)
626 block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0;
627 return(retval);
628}
diff --git a/src/lib/libc/include/namespace.h b/src/lib/libc/include/namespace.h
index 803657e006..4a51f15ddf 100644
--- a/src/lib/libc/include/namespace.h
+++ b/src/lib/libc/include/namespace.h
@@ -1,4 +1,4 @@
1/* $NetBSD: namespace.h,v 1.2 1995/02/27 13:02:12 cgd Exp $ */ 1/* $OpenBSD: namespace.h,v 1.2 1996/08/19 08:28:08 tholo Exp $ */
2 2
3#define catclose _catclose 3#define catclose _catclose
4#define catgets _catgets 4#define catgets _catgets
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc
index 2d220067e4..935a1904c1 100644
--- a/src/lib/libc/net/Makefile.inc
+++ b/src/lib/libc/net/Makefile.inc
@@ -1,16 +1,19 @@
1# $NetBSD: Makefile.inc,v 1.23 1995/03/02 09:09:07 chopps Exp $ 1# $OpenBSD: Makefile.inc,v 1.16 1998/08/29 21:11:40 deraadt Exp $
2# @(#)Makefile.inc 8.2 (Berkeley) 9/5/93
3 2
4# net sources 3# net sources
5.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net 4.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net
6 5
7SRCS+= gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \ 6CFLAGS+=-DRESOLVSORT
8 getproto.c getprotoent.c getprotoname.c getservbyname.c \ 7
8SRCS+= base64.c gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \
9 getnetnamadr.c getproto.c getprotoent.c getprotoname.c getservbyname.c \
9 getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \ 10 getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \
10 inet_makeaddr.c inet_netof.c inet_network.c inet_ntoa.c \ 11 inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \
11 iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c rcmd.c recv.c res_comp.c \ 12 inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c \
12 res_debug.c res_init.c res_mkquery.c res_query.c res_send.c \ 13 inet_pton.c ipx_addr.c ipx_ntoa.c \
13 send.c sethostent.c ethers.c 14 iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c nsap_addr.c rcmd.c recv.c \
15 res_comp.c res_data.c res_debug.c res_init.c res_mkquery.c res_query.c \
16 res_random.c res_send.c send.c sethostent.c ethers.c rcmdsh.c
14 17
15# machine-dependent net sources 18# machine-dependent net sources
16# m-d Makefile.inc must include sources for: 19# m-d Makefile.inc must include sources for:
@@ -18,16 +21,21 @@ SRCS+= gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \
18 21
19.include "${.CURDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" 22.include "${.CURDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc"
20 23
21MAN+= byteorder.3 gethostbyname.3 getnetent.3 getprotoent.3 getservent.3 \ 24MAN+= byteorder.3 ethers.3 gethostbyname.3 getnetent.3 getprotoent.3 \
22 inet.3 linkaddr.3 ns.3 rcmd.3 resolver.3 ethers.3 25 getservent.3 inet.3 inet_net.3 iso_addr.3 link_addr.3 ns.3 ipx.3 \
26 rcmd.3 rcmdsh.3 resolver.3
23 27
24MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ 28MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \
25 byteorder.3 ntohs.3 29 byteorder.3 ntohs.3 byteorder.3 htobe16.3 byteorder.3 htobe32.3 \
30 byteorder.3 betoh16.3 byteorder.3 betoh32.3 byteorder.3 htole16.3 \
31 byteorder.3 htole32.3 byteorder.3 letoh16.3 byteorder.3 letoh32.3 \
32 byteorder.3 swap16.3 byteorder.3 swap32.3
26MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ 33MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \
27 ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 34 ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 ethers.3 ether_addr.3
28MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ 35MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \
29 gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ 36 gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \
30 gethostbyname.3 herror.3 37 gethostbyname.3 herror.3 gethostbyname.3 gethostbyname2.3 \
38 gethostbyname.3 hstrerror.3
31MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ 39MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \
32 getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 40 getnetent.3 getnetbyname.3 getnetent.3 setnetent.3
33MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ 41MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \
@@ -37,8 +45,10 @@ MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \
37MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ 45MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \
38 inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ 46 inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \
39 inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ 47 inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \
40 inet.3 ntoa.3 48 inet.3 ntoa.3 inet.3 inet_ntop.3 inet.3 inet_pton.3
41MLINKS+=linkaddr.3 linkntoa.3 49MLINKS+=iso_addr.3 iso_ntoa.3
50MLINKS+=link_addr.3 link_ntoa.3
51MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3
42MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 52MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3
43MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 53MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3
44MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ 54MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \
diff --git a/src/lib/libc/net/base64.c b/src/lib/libc/net/base64.c
new file mode 100644
index 0000000000..452fe5afcc
--- /dev/null
+++ b/src/lib/libc/net/base64.c
@@ -0,0 +1,317 @@
1/* $OpenBSD: base64.c,v 1.3 1997/11/08 20:46:55 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20/*
21 * Portions Copyright (c) 1995 by International Business Machines, Inc.
22 *
23 * International Business Machines, Inc. (hereinafter called IBM) grants
24 * permission under its copyrights to use, copy, modify, and distribute this
25 * Software with or without fee, provided that the above copyright notice and
26 * all paragraphs of this notice appear in all copies, and that the name of IBM
27 * not be used in connection with the marketing of any product incorporating
28 * the Software or modifications thereof, without specific, written prior
29 * permission.
30 *
31 * To the extent it has a right to do so, IBM grants an immunity from suit
32 * under its patents, if any, for the use, sale or manufacture of products to
33 * the extent that such products are used for performing Domain Name System
34 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
35 * granted for any product per se or for any other function of any product.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
38 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
40 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
41 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
42 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <arpa/inet.h>
50#include <arpa/nameser.h>
51
52#include <ctype.h>
53#include <resolv.h>
54#include <stdio.h>
55
56#include <stdlib.h>
57#include <string.h>
58
59#define Assert(Cond) if (!(Cond)) abort()
60
61static const char Base64[] =
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
63static const char Pad64 = '=';
64
65/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
66 The following encoding technique is taken from RFC 1521 by Borenstein
67 and Freed. It is reproduced here in a slightly edited form for
68 convenience.
69
70 A 65-character subset of US-ASCII is used, enabling 6 bits to be
71 represented per printable character. (The extra 65th character, "=",
72 is used to signify a special processing function.)
73
74 The encoding process represents 24-bit groups of input bits as output
75 strings of 4 encoded characters. Proceeding from left to right, a
76 24-bit input group is formed by concatenating 3 8-bit input groups.
77 These 24 bits are then treated as 4 concatenated 6-bit groups, each
78 of which is translated into a single digit in the base64 alphabet.
79
80 Each 6-bit group is used as an index into an array of 64 printable
81 characters. The character referenced by the index is placed in the
82 output string.
83
84 Table 1: The Base64 Alphabet
85
86 Value Encoding Value Encoding Value Encoding Value Encoding
87 0 A 17 R 34 i 51 z
88 1 B 18 S 35 j 52 0
89 2 C 19 T 36 k 53 1
90 3 D 20 U 37 l 54 2
91 4 E 21 V 38 m 55 3
92 5 F 22 W 39 n 56 4
93 6 G 23 X 40 o 57 5
94 7 H 24 Y 41 p 58 6
95 8 I 25 Z 42 q 59 7
96 9 J 26 a 43 r 60 8
97 10 K 27 b 44 s 61 9
98 11 L 28 c 45 t 62 +
99 12 M 29 d 46 u 63 /
100 13 N 30 e 47 v
101 14 O 31 f 48 w (pad) =
102 15 P 32 g 49 x
103 16 Q 33 h 50 y
104
105 Special processing is performed if fewer than 24 bits are available
106 at the end of the data being encoded. A full encoding quantum is
107 always completed at the end of a quantity. When fewer than 24 input
108 bits are available in an input group, zero bits are added (on the
109 right) to form an integral number of 6-bit groups. Padding at the
110 end of the data is performed using the '=' character.
111
112 Since all base64 input is an integral number of octets, only the
113 -------------------------------------------------
114 following cases can arise:
115
116 (1) the final quantum of encoding input is an integral
117 multiple of 24 bits; here, the final unit of encoded
118 output will be an integral multiple of 4 characters
119 with no "=" padding,
120 (2) the final quantum of encoding input is exactly 8 bits;
121 here, the final unit of encoded output will be two
122 characters followed by two "=" padding characters, or
123 (3) the final quantum of encoding input is exactly 16 bits;
124 here, the final unit of encoded output will be three
125 characters followed by one "=" padding character.
126 */
127
128int
129b64_ntop(src, srclength, target, targsize)
130 u_char const *src;
131 size_t srclength;
132 char *target;
133 size_t targsize;
134{
135 size_t datalength = 0;
136 u_char input[3];
137 u_char output[4];
138 int i;
139
140 while (2 < srclength) {
141 input[0] = *src++;
142 input[1] = *src++;
143 input[2] = *src++;
144 srclength -= 3;
145
146 output[0] = input[0] >> 2;
147 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
148 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
149 output[3] = input[2] & 0x3f;
150 Assert(output[0] < 64);
151 Assert(output[1] < 64);
152 Assert(output[2] < 64);
153 Assert(output[3] < 64);
154
155 if (datalength + 4 > targsize)
156 return (-1);
157 target[datalength++] = Base64[output[0]];
158 target[datalength++] = Base64[output[1]];
159 target[datalength++] = Base64[output[2]];
160 target[datalength++] = Base64[output[3]];
161 }
162
163 /* Now we worry about padding. */
164 if (0 != srclength) {
165 /* Get what's left. */
166 input[0] = input[1] = input[2] = '\0';
167 for (i = 0; i < srclength; i++)
168 input[i] = *src++;
169
170 output[0] = input[0] >> 2;
171 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
172 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
173 Assert(output[0] < 64);
174 Assert(output[1] < 64);
175 Assert(output[2] < 64);
176
177 if (datalength + 4 > targsize)
178 return (-1);
179 target[datalength++] = Base64[output[0]];
180 target[datalength++] = Base64[output[1]];
181 if (srclength == 1)
182 target[datalength++] = Pad64;
183 else
184 target[datalength++] = Base64[output[2]];
185 target[datalength++] = Pad64;
186 }
187 if (datalength >= targsize)
188 return (-1);
189 target[datalength] = '\0'; /* Returned value doesn't count \0. */
190 return (datalength);
191}
192
193/* skips all whitespace anywhere.
194 converts characters, four at a time, starting at (or after)
195 src from base - 64 numbers into three 8 bit bytes in the target area.
196 it returns the number of data bytes stored at the target, or -1 on error.
197 */
198
199int
200b64_pton(src, target, targsize)
201 char const *src;
202 u_char *target;
203 size_t targsize;
204{
205 int tarindex, state, ch;
206 char *pos;
207
208 state = 0;
209 tarindex = 0;
210
211 while ((ch = *src++) != '\0') {
212 if (isspace(ch)) /* Skip whitespace anywhere. */
213 continue;
214
215 if (ch == Pad64)
216 break;
217
218 pos = strchr(Base64, ch);
219 if (pos == 0) /* A non-base64 character. */
220 return (-1);
221
222 switch (state) {
223 case 0:
224 if (target) {
225 if (tarindex >= targsize)
226 return (-1);
227 target[tarindex] = (pos - Base64) << 2;
228 }
229 state = 1;
230 break;
231 case 1:
232 if (target) {
233 if (tarindex + 1 >= targsize)
234 return (-1);
235 target[tarindex] |= (pos - Base64) >> 4;
236 target[tarindex+1] = ((pos - Base64) & 0x0f)
237 << 4 ;
238 }
239 tarindex++;
240 state = 2;
241 break;
242 case 2:
243 if (target) {
244 if (tarindex + 1 >= targsize)
245 return (-1);
246 target[tarindex] |= (pos - Base64) >> 2;
247 target[tarindex+1] = ((pos - Base64) & 0x03)
248 << 6;
249 }
250 tarindex++;
251 state = 3;
252 break;
253 case 3:
254 if (target) {
255 if (tarindex >= targsize)
256 return (-1);
257 target[tarindex] |= (pos - Base64);
258 }
259 tarindex++;
260 state = 0;
261 break;
262 }
263 }
264
265 /*
266 * We are done decoding Base-64 chars. Let's see if we ended
267 * on a byte boundary, and/or with erroneous trailing characters.
268 */
269
270 if (ch == Pad64) { /* We got a pad char. */
271 ch = *src++; /* Skip it, get next. */
272 switch (state) {
273 case 0: /* Invalid = in first position */
274 case 1: /* Invalid = in second position */
275 return (-1);
276
277 case 2: /* Valid, means one byte of info */
278 /* Skip any number of spaces. */
279 for (; ch != '\0'; ch = *src++)
280 if (!isspace(ch))
281 break;
282 /* Make sure there is another trailing = sign. */
283 if (ch != Pad64)
284 return (-1);
285 ch = *src++; /* Skip the = */
286 /* Fall through to "single trailing =" case. */
287 /* FALLTHROUGH */
288
289 case 3: /* Valid, means two bytes of info */
290 /*
291 * We know this char is an =. Is there anything but
292 * whitespace after it?
293 */
294 for (; ch != '\0'; ch = *src++)
295 if (!isspace(ch))
296 return (-1);
297
298 /*
299 * Now make sure for cases 2 and 3 that the "extra"
300 * bits that slopped past the last full byte were
301 * zeros. If we don't check them, they become a
302 * subliminal channel.
303 */
304 if (target && target[tarindex] != 0)
305 return (-1);
306 }
307 } else {
308 /*
309 * We ended by seeing the end of the string. Make sure we
310 * have no partial bytes lying around.
311 */
312 if (state != 0)
313 return (-1);
314 }
315
316 return (tarindex);
317}
diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3
index 701a69f688..f2788b25dc 100644
--- a/src/lib/libc/net/byteorder.3
+++ b/src/lib/libc/net/byteorder.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: byteorder.3,v 1.3 1995/02/25 06:20:27 cgd Exp $ 1.\" $OpenBSD: byteorder.3,v 1.5 1997/11/19 23:30:17 niklas Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)byteorder.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt BYTEORDER 3 35.Dt BYTEORDER 3
38.Os BSD 4.2 36.Os BSD 4.2
@@ -40,26 +38,101 @@
40.Nm htonl , 38.Nm htonl ,
41.Nm htons , 39.Nm htons ,
42.Nm ntohl , 40.Nm ntohl ,
43.Nm ntohs 41.Nm ntohs ,
44.Nd convert values between host and network byte order 42.Nm htobe32 ,
43.Nm htobe16 ,
44.Nm betoh32 ,
45.Nm betoh16 ,
46.Nm htole32 ,
47.Nm htole16 ,
48.Nm letoh32 ,
49.Nm letoh16 ,
50.Nm swap32 ,
51.Nm swap16
52.Nd convert values between different byte orderings
45.Sh SYNOPSIS 53.Sh SYNOPSIS
46.Fd #include <sys/param.h> 54.Fd #include <sys/types.h>
47.Ft u_long 55.Fd #include <machine/endian.h>
48.Fn htonl "u_long hostlong" 56.Ft u_int32_t
49.Ft u_short 57.Fn htonl "u_int32_t host32"
50.Fn htons "u_short hostshort" 58.Ft u_int16_t
51.Ft u_long 59.Fn htons "u_int16_t host16"
52.Fn ntohl "u_long netlong" 60.Ft u_int32_t
53.Ft u_short 61.Fn ntohl "u_int32_t net32"
54.Fn ntohs "u_short netshort" 62.Ft u_int16_t
63.Fn ntohs "u_int16_t net16"
64.Ft u_int32_t
65.Fn htobe32 "u_int32_t host32"
66.Ft u_int16_t
67.Fn htobe16 "u_int16_t host16"
68.Ft u_int32_t
69.Fn betoh32 "u_int32_t big32"
70.Ft u_int16_t
71.Fn betoh16 "u_int16_t big16"
72.Ft u_int32_t
73.Fn htole32 "u_int32_t host32"
74.Ft u_int16_t
75.Fn htole16 "u_int16_t host16"
76.Ft u_int32_t
77.Fn letoh32 "u_int32_t little32"
78.Ft u_int16_t
79.Fn letoh16 "u_int16_t little16"
80.Ft u_int32_t
81.Fn swap32 "u_int32_t val32"
82.Ft u_int16_t
83.Fn swap16 "u_int16_t val16"
55.Sh DESCRIPTION 84.Sh DESCRIPTION
56These routines convert 16 and 32 bit quantities between network 85These routines convert 16 and 32 bit quantities between different
57byte order and host byte order. 86byte orderings. The "swap" functions reverse the byte ordering of
87the given quantity, the others converts either from/to the native
88byte order used by the host to/from either little- or big-endian (a.k.a
89network) order.
90.Pp
91Apart from the "swap" functions, the names can be described by this form:
92{src-order}to{dst-order}{size}.
93Both {src-order} and {dst-order} can take the following forms:
94.Bl -tag -width "be "
95.It Em h
96host order
97.It Em n
98network order (big-endian)
99.It Em be
100big-endian (Most significant byte first)
101.It Em le
102little-endian (Least significant byte first)
103.El
104.Pp
105One of the specified orderings must be "h".
106{Size} will take these forms:
107.Bl -tag -width "32 "
108.It Em l
109long (32-bit, used in conjunction with forms involving "n")
110.It Em s
111short (16-bit, used in conjunction with forms involving "n")
112.It Em 16
11316-bit
114.It Em 32
11532-bit
116.El
117.Pp
118The "swap" functions are of the form: swap{size}.
119.Pp
120Names involving "n" convert quantities between network
121byte order and host byte order. The last letter (s/l) is a mnemonic
122for the traditional names for such quantities, short and long,
123respectively. Today, the C concept of "short"/"long" integers
124need not coincide with this traditional misunderstanding.
58On machines which have a byte order which is the same as the network 125On machines which have a byte order which is the same as the network
59order, routines are defined as null macros. 126order, routines are defined as null macros.
60.Pp 127.Pp
61These routines are most often used in conjunction with Internet 128The functions involving either "be", "le" or "swap" use the numbers
62addresses and ports as returned by 129(16/32) for specifying the bitwidth of the quantities they operate on.
130Currently all supported architectures are either big- or little-endian
131so either the "be" or the "le" variants are implemented as null macros.
132.Pp
133The routines mentioned above which have either {src-order} or {dst-order}
134set to "n" are most often used in
135conjunction with Internet addresses and ports as returned by
63.Xr gethostbyname 3 136.Xr gethostbyname 3
64and 137and
65.Xr getservent 3 . 138.Xr getservent 3 .
@@ -73,6 +146,10 @@ functions appeared in
73.Bx 4.2 . 146.Bx 4.2 .
74.Sh BUGS 147.Sh BUGS
75On the 148On the
76.Tn VAX 149.Tn vax ,
150.Tn alpha ,
151.Tn i386 ,
152and so far
153.Tn mips
77bytes are handled backwards from most everyone else in 154bytes are handled backwards from most everyone else in
78the world. This is not expected to be fixed in the near future. 155the world. This is not expected to be fixed in the near future.
diff --git a/src/lib/libc/net/ethers.3 b/src/lib/libc/net/ethers.3
index 81e6c65935..f5db308115 100644
--- a/src/lib/libc/net/ethers.3
+++ b/src/lib/libc/net/ethers.3
@@ -1,15 +1,17 @@
1.\" $OpenBSD: ethers.3,v 1.9 1998/05/12 09:15:19 deraadt Exp $
1.\" 2.\"
2.\" Written by roland@frob.com. Public domain. 3.\" Written by roland@frob.com. Public domain.
3.\" 4.\"
4.Dd December 16, 1993 5.Dd December 16, 1993
5.Dt ETHERS 3 6.Dt ETHERS 3
6.Os NetBSD 7.Os
7.Sh NAME 8.Sh NAME
9.Nm ether_aton ,
8.Nm ether_ntoa , 10.Nm ether_ntoa ,
9.Nm ether_addr , 11.Nm ether_addr ,
10.Nm ether_ntohost , 12.Nm ether_ntohost ,
11.Nm ether_hostton , 13.Nm ether_hostton ,
12.Nm ether_line , 14.Nm ether_line
13.Nd get ethers entry 15.Nd get ethers entry
14.Sh SYNOPSIS 16.Sh SYNOPSIS
15.Fd #include <netinet/if_ether.h> 17.Fd #include <netinet/if_ether.h>
@@ -17,15 +19,18 @@
17.Fn ether_ntoa "struct ether_addr *e" 19.Fn ether_ntoa "struct ether_addr *e"
18.Ft struct ether_addr * 20.Ft struct ether_addr *
19.Fn ether_aton "char *s" 21.Fn ether_aton "char *s"
22.Ft int
20.Fn ether_ntohost "char *hostname" "struct ether_addr *e" 23.Fn ether_ntohost "char *hostname" "struct ether_addr *e"
24.Ft int
21.Fn ether_hostton "char *hostname" "struct ether_addr *e" 25.Fn ether_hostton "char *hostname" "struct ether_addr *e"
26.Ft int
22.Fn ether_line "char *l" "struct ether_addr *e" "char *hostname" 27.Fn ether_line "char *l" "struct ether_addr *e" "char *hostname"
23.Sh DESCRIPTION 28.Sh DESCRIPTION
24Ethernet addresses are represented by the 29Ethernet addresses are represented by the
25following structure: 30following structure:
26.Bd -literal -offset indent 31.Bd -literal -offset indent
27struct ether_addr { 32struct ether_addr {
28 u_char ether_addr_octet[6]; 33 u_int8_t ether_addr_octet[6];
29}; 34};
30.Ed 35.Ed
31.Pp 36.Pp
@@ -51,12 +56,15 @@ addresses,
51The 56The
52.Fn ether_ntohost 57.Fn ether_ntohost
53function looks up the given Ethernet address and writes the associated 58function looks up the given Ethernet address and writes the associated
54host name into the character buffer passed. 59host name into the character buffer passed. This buffer should be
60.Ev MAXHOSTNAMELEN
61characters in size.
55The 62The
56.Fn ether_hostton 63.Fn ether_hostton
57function looks up the given host name and writes the associated 64function looks up the given host name and writes the associated
58Ethernet address into the structure passed. Both functions return 65Ethernet address into the structure passed. Both functions return
59zero if they find the requested host name or address, and -1 if not. 66zero if they find the requested host name or address, and -1 if not.
67.Pp
60Each call reads 68Each call reads
61.Pa /etc/ethers 69.Pa /etc/ethers
62from the beginning; if a + appears alone on a line in the file, then 70from the beginning; if a + appears alone on a line in the file, then
@@ -76,6 +84,9 @@ function parses a line from the
76file and fills in the passed ``struct ether_addr'' and character 84file and fills in the passed ``struct ether_addr'' and character
77buffer with the Ethernet address and host name on the line. It 85buffer with the Ethernet address and host name on the line. It
78returns zero if the line was successfully parsed and -1 if not. 86returns zero if the line was successfully parsed and -1 if not.
87The character buffer buffer should be
88.Ev MAXHOSTNAMELEN
89characters in size.
79.Sh FILES 90.Sh FILES
80.Bl -tag -width /etc/ethers -compact 91.Bl -tag -width /etc/ethers -compact
81.It Pa /etc/ethers 92.It Pa /etc/ethers
@@ -95,8 +106,4 @@ NetBSD 0.9b.
95.Sh BUGS 106.Sh BUGS
96The data space used by these functions is static; if future use 107The data space used by these functions is static; if future use
97requires the data, it should be copied before any subsequent calls to 108requires the data, it should be copied before any subsequent calls to
98these functions overwrite it. There is no way to restrict how many 109these functions overwrite it.
99character will be written into the host name buffer passed. A very
100long line in
101.Pa /etc/ethers
102could overflow your buffer.
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c
index 0f32b9b71b..9df876b6f4 100644
--- a/src/lib/libc/net/ethers.c
+++ b/src/lib/libc/net/ethers.c
@@ -1,12 +1,42 @@
1/* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ 1/* $OpenBSD: ethers.c,v 1.9 1998/06/21 22:13:44 millert Exp $ */
2 2
3/* 3/*
4 * ethers(3N) a la Sun. 4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
5 * 17 *
6 * Written by Roland McGrath <roland@frob.com> 10/14/93. 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
7 * Public domain. 19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * ethers(3) a la Sun.
32 * Originally Written by Roland McGrath <roland@frob.com> 10/14/93.
33 * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com>
8 */ 34 */
9 35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char rcsid[] = "$OpenBSD: ethers.c,v 1.9 1998/06/21 22:13:44 millert Exp $";
38#endif /* LIBC_SCCS and not lint */
39
10#include <sys/types.h> 40#include <sys/types.h>
11#include <sys/socket.h> 41#include <sys/socket.h>
12#include <net/if.h> 42#include <net/if.h>
@@ -18,22 +48,60 @@
18#include <stdio.h> 48#include <stdio.h>
19#include <stdlib.h> 49#include <stdlib.h>
20#include <string.h> 50#include <string.h>
51#include <ctype.h>
21 52
22#ifndef _PATH_ETHERS 53#ifndef _PATH_ETHERS
23#define _PATH_ETHERS "/etc/ethers" 54#define _PATH_ETHERS "/etc/ethers"
24#endif 55#endif
25 56
57static char * _ether_aton __P((char *, struct ether_addr *));
58
26char * 59char *
27ether_ntoa(e) 60ether_ntoa(e)
28 struct ether_addr *e; 61 struct ether_addr *e;
29{ 62{
30 static char a[] = "xx:xx:xx:xx:xx:xx"; 63 static char a[] = "xx:xx:xx:xx:xx:xx";
31 64
32 sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", 65 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
66 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
67 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
68 errno = EINVAL;
69 return (NULL);
70 }
71
72 (void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
33 e->ether_addr_octet[0], e->ether_addr_octet[1], 73 e->ether_addr_octet[0], e->ether_addr_octet[1],
34 e->ether_addr_octet[2], e->ether_addr_octet[3], 74 e->ether_addr_octet[2], e->ether_addr_octet[3],
35 e->ether_addr_octet[4], e->ether_addr_octet[5]); 75 e->ether_addr_octet[4], e->ether_addr_octet[5]);
36 return a; 76
77 return (a);
78}
79
80static char *
81_ether_aton(s, e)
82 char *s;
83 struct ether_addr *e;
84{
85 int i;
86 long l;
87 char *pp;
88
89 while (isspace(*s))
90 s++;
91
92 /* expect 6 hex octets separated by ':' or space/NUL if last octet */
93 for (i = 0; i < 6; i++) {
94 l = strtol(s, &pp, 16);
95 if (pp == s || l > 0xFF)
96 return (NULL);
97 if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
98 return (NULL);
99 e->ether_addr_octet[i] = (u_char)l;
100 s = pp + 1;
101 }
102
103 /* return character after the octets ala strtol(3) */
104 return (pp);
37} 105}
38 106
39struct ether_addr * 107struct ether_addr *
@@ -41,33 +109,32 @@ ether_aton(s)
41 char *s; 109 char *s;
42{ 110{
43 static struct ether_addr n; 111 static struct ether_addr n;
44 u_int i[6]; 112
45 113 return (_ether_aton(s, &n) ? &n : NULL);
46 if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
47 &i[2], &i[3], &i[4], &i[5]) == 6) {
48 n.ether_addr_octet[0] = (u_char)i[0];
49 n.ether_addr_octet[1] = (u_char)i[1];
50 n.ether_addr_octet[2] = (u_char)i[2];
51 n.ether_addr_octet[3] = (u_char)i[3];
52 n.ether_addr_octet[4] = (u_char)i[4];
53 n.ether_addr_octet[5] = (u_char)i[5];
54 return &n;
55 }
56 return NULL;
57} 114}
58 115
116int
59ether_ntohost(hostname, e) 117ether_ntohost(hostname, e)
60 char *hostname; 118 char *hostname;
61 struct ether_addr *e; 119 struct ether_addr *e;
62{ 120{
63 FILE *f; 121 FILE *f;
64 char buf[BUFSIZ]; 122 char buf[BUFSIZ+1], *p;
123 size_t len;
65 struct ether_addr try; 124 struct ether_addr try;
66
67#ifdef YP 125#ifdef YP
68 char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; 126 char trybuf[sizeof("xx:xx:xx:xx:xx:xx")];
69 int trylen; 127 int trylen;
128#endif
129
130 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
131 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
132 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
133 errno = EINVAL;
134 return (-1);
135 }
70 136
137#ifdef YP
71 sprintf(trybuf, "%x:%x:%x:%x:%x:%x", 138 sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
72 e->ether_addr_octet[0], e->ether_addr_octet[1], 139 e->ether_addr_octet[0], e->ether_addr_octet[1],
73 e->ether_addr_octet[2], e->ether_addr_octet[3], 140 e->ether_addr_octet[2], e->ether_addr_octet[3],
@@ -76,12 +143,19 @@ ether_ntohost(hostname, e)
76#endif 143#endif
77 144
78 f = fopen(_PATH_ETHERS, "r"); 145 f = fopen(_PATH_ETHERS, "r");
79 if (f==NULL) 146 if (f == NULL)
80 return -1; 147 return (-1);
81 while (fgets(buf, sizeof buf, f)) { 148 while ((p = fgetln(f, &len)) != NULL) {
149 if (p[len-1] == '\n')
150 len--;
151 if (len > sizeof(buf) - 2)
152 continue;
153 (void)memcpy(buf, p, len);
154 buf[len] = '\n'; /* code assumes newlines later on */
155 buf[len+1] = '\0';
82#ifdef YP 156#ifdef YP
83 /* A + in the file means try YP now. */ 157 /* A + in the file means try YP now. */
84 if (!strncmp(buf, "+\n", sizeof buf)) { 158 if (!strncmp(buf, "+\n", sizeof(buf))) {
85 char *ypbuf, *ypdom; 159 char *ypbuf, *ypdom;
86 int ypbuflen; 160 int ypbuflen;
87 161
@@ -93,42 +167,51 @@ ether_ntohost(hostname, e)
93 if (ether_line(ypbuf, &try, hostname) == 0) { 167 if (ether_line(ypbuf, &try, hostname) == 0) {
94 free(ypbuf); 168 free(ypbuf);
95 (void)fclose(f); 169 (void)fclose(f);
96 return 0; 170 return (0);
97 } 171 }
98 free(ypbuf); 172 free(ypbuf);
99 continue; 173 continue;
100 } 174 }
101#endif 175#endif
102 if (ether_line(buf, &try, hostname) == 0 && 176 if (ether_line(buf, &try, hostname) == 0 &&
103 bcmp((char *)&try, (char *)e, sizeof try) == 0) { 177 memcmp((void *)&try, (void *)e, sizeof(try)) == 0) {
104 (void)fclose(f); 178 (void)fclose(f);
105 return 0; 179 return (0);
106 } 180 }
107 } 181 }
108 (void)fclose(f); 182 (void)fclose(f);
109 errno = ENOENT; 183 errno = ENOENT;
110 return -1; 184 return (-1);
111} 185}
112 186
187int
113ether_hostton(hostname, e) 188ether_hostton(hostname, e)
114 char *hostname; 189 char *hostname;
115 struct ether_addr *e; 190 struct ether_addr *e;
116{ 191{
117 FILE *f; 192 FILE *f;
118 char buf[BUFSIZ]; 193 char buf[BUFSIZ+1], *p;
119 char try[MAXHOSTNAMELEN]; 194 char try[MAXHOSTNAMELEN];
195 size_t len;
120#ifdef YP 196#ifdef YP
121 int hostlen = strlen(hostname); 197 int hostlen = strlen(hostname);
122#endif 198#endif
123 199
124 f = fopen(_PATH_ETHERS, "r"); 200 f = fopen(_PATH_ETHERS, "r");
125 if (f==NULL) 201 if (f==NULL)
126 return -1; 202 return (-1);
127 203
128 while (fgets(buf, sizeof buf, f)) { 204 while ((p = fgetln(f, &len)) != NULL) {
205 if (p[len-1] == '\n')
206 len--;
207 if (len > sizeof(buf) - 2)
208 continue;
209 memcpy(buf, p, len);
210 buf[len] = '\n'; /* code assumes newlines later on */
211 buf[len+1] = '\0';
129#ifdef YP 212#ifdef YP
130 /* A + in the file means try YP now. */ 213 /* A + in the file means try YP now. */
131 if (!strncmp(buf, "+\n", sizeof buf)) { 214 if (!strncmp(buf, "+\n", sizeof(buf))) {
132 char *ypbuf, *ypdom; 215 char *ypbuf, *ypdom;
133 int ypbuflen; 216 int ypbuflen;
134 217
@@ -140,7 +223,7 @@ ether_hostton(hostname, e)
140 if (ether_line(ypbuf, e, try) == 0) { 223 if (ether_line(ypbuf, e, try) == 0) {
141 free(ypbuf); 224 free(ypbuf);
142 (void)fclose(f); 225 (void)fclose(f);
143 return 0; 226 return (0);
144 } 227 }
145 free(ypbuf); 228 free(ypbuf);
146 continue; 229 continue;
@@ -148,31 +231,40 @@ ether_hostton(hostname, e)
148#endif 231#endif
149 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { 232 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
150 (void)fclose(f); 233 (void)fclose(f);
151 return 0; 234 return (0);
152 } 235 }
153 } 236 }
154 (void)fclose(f); 237 (void)fclose(f);
155 errno = ENOENT; 238 errno = ENOENT;
156 return -1; 239 return (-1);
157} 240}
158 241
159ether_line(l, e, hostname) 242int
160 char *l; 243ether_line(line, e, hostname)
244 char *line;
161 struct ether_addr *e; 245 struct ether_addr *e;
162 char *hostname; 246 char *hostname;
163{ 247{
164 u_int i[6]; 248 char *p;
165 249 size_t n;
166 if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], 250
167 &i[2], &i[3], &i[4], &i[5], hostname) == 7) { 251 /* Parse "xx:xx:xx:xx:xx:xx" */
168 e->ether_addr_octet[0] = (u_char)i[0]; 252 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
169 e->ether_addr_octet[1] = (u_char)i[1]; 253 goto bad;
170 e->ether_addr_octet[2] = (u_char)i[2]; 254
171 e->ether_addr_octet[3] = (u_char)i[3]; 255 /* Now get the hostname */
172 e->ether_addr_octet[4] = (u_char)i[4]; 256 while (isspace(*p))
173 e->ether_addr_octet[5] = (u_char)i[5]; 257 p++;
174 return 0; 258 if (*p == '\0')
175 } 259 goto bad;
260 n = strcspn(p, " \t\n");
261 if (n >= MAXHOSTNAMELEN)
262 goto bad;
263 (void)strncpy(hostname, p, n);
264 hostname[n] = '\0';
265 return (0);
266
267bad:
176 errno = EINVAL; 268 errno = EINVAL;
177 return -1; 269 return (-1);
178} 270}
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3
index bac0368296..37069c0a59 100644
--- a/src/lib/libc/net/gethostbyname.3
+++ b/src/lib/libc/net/gethostbyname.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: gethostbyname.3,v 1.6 1995/02/25 06:20:28 cgd Exp $ 1.\" $OpenBSD: gethostbyname.3,v 1.9 1998/09/07 16:44:35 aaron Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1987, 1991, 1993 3.\" Copyright (c) 1983, 1987, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,17 +31,17 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)gethostbyname.3 8.2 (Berkeley) 4/19/94 34.Dd March 13, 1997
35.\"
36.Dd April 19, 1994
37.Dt GETHOSTBYNAME 3 35.Dt GETHOSTBYNAME 3
38.Os BSD 4.2 36.Os
39.Sh NAME 37.Sh NAME
40.Nm gethostbyname , 38.Nm gethostbyname ,
39.Nm gethostbyname2 ,
41.Nm gethostbyaddr , 40.Nm gethostbyaddr ,
42.Nm gethostent , 41.Nm gethostent ,
43.Nm sethostent , 42.Nm sethostent ,
44.Nm endhostent , 43.Nm endhostent ,
44.Nm hstrerror ,
45.Nm herror 45.Nm herror
46.Nd get network host entry 46.Nd get network host entry
47.Sh SYNOPSIS 47.Sh SYNOPSIS
@@ -50,12 +50,19 @@
50.Ft struct hostent * 50.Ft struct hostent *
51.Fn gethostbyname "const char *name" 51.Fn gethostbyname "const char *name"
52.Ft struct hostent * 52.Ft struct hostent *
53.Fn gethostbyname2 "const char *name" "int af"
54.Ft struct hostent *
53.Fn gethostbyaddr "const char *addr" "int len" "int type" 55.Fn gethostbyaddr "const char *addr" "int len" "int type"
54.Ft struct hostent * 56.Ft struct hostent *
55.Fn gethostent void 57.Fn gethostent void
58.Ft void
56.Fn sethostent "int stayopen" 59.Fn sethostent "int stayopen"
60.Ft void
57.Fn endhostent void 61.Fn endhostent void
58.Fn herror "char *string" 62.Ft void
63.Fn herror "const char *string"
64.Ft const char *
65.Fn hstrerror "int err"
59.Sh DESCRIPTION 66.Sh DESCRIPTION
60The 67The
61.Fn gethostbyname 68.Fn gethostbyname
@@ -92,8 +99,7 @@ Official name of the host.
92.It Fa h_aliases 99.It Fa h_aliases
93A zero terminated array of alternate names for the host. 100A zero terminated array of alternate names for the host.
94.It Fa h_addrtype 101.It Fa h_addrtype
95The type of address being returned; currently always 102The type of address being returned.
96.Dv AF_INET .
97.It Fa h_length 103.It Fa h_length
98The length, in bytes, of the address. 104The length, in bytes, of the address.
99.It Fa h_addr_list 105.It Fa h_addr_list
@@ -103,6 +109,7 @@ Host addresses are returned in network byte order.
103The first address in 109The first address in
104.Fa h_addr_list ; 110.Fa h_addr_list ;
105this is for backward compatibility. 111this is for backward compatibility.
112.El
106.Pp 113.Pp
107When using the nameserver, 114When using the nameserver,
108.Fn gethostbyname 115.Fn gethostbyname
@@ -116,6 +123,14 @@ See
116.Xr hostname 7 123.Xr hostname 7
117for the domain search procedure and the alias file format. 124for the domain search procedure and the alias file format.
118.Pp 125.Pp
126.Fn Gethostbyname2
127is an advanced form of
128.Fn gethostbyname
129which allows lookups in address families other than
130.Dv AF_INET ,
131for example
132.Dv AF_INET6 .
133.Pp
119The 134The
120.Fn sethostent 135.Fn sethostent
121function 136function
@@ -141,13 +156,27 @@ function
141closes the 156closes the
142.Tn TCP 157.Tn TCP
143connection. 158connection.
159.Pp
160The
161.Fn herror
162function prints an error message describing the failure. If its argument
163.Fa string
164is
165.Pf non Dv -NULL ,
166it is prepended to the message string and separated from it by a colon
167and a space. The error message is printed with a trailing newline.
168The contents of the error message is the same as that returned by
169.Fn hstrerror
170with argument
171.Fa h_errno .
144.Sh FILES 172.Sh FILES
145.Bl -tag -width /etc/hosts -compact 173.Bl -tag -width /etc/hosts -compact
146.It Pa /etc/hosts 174.It Pa /etc/hosts
147.El 175.El
148.Sh DIAGNOSTICS 176.Sh DIAGNOSTICS
149Error return status from 177Error return status from
150.Fn gethostbyname 178.Fn gethostbyname ,
179.Fn gethostbyname2 ,
151and 180and
152.Fn gethostbyaddr 181.Fn gethostbyaddr
153is indicated by return of a null pointer. 182is indicated by return of a null pointer.
@@ -155,15 +184,6 @@ The external integer
155.Va h_errno 184.Va h_errno
156may then be checked to see whether this is a temporary failure 185may then be checked to see whether this is a temporary failure
157or an invalid or unknown host. 186or an invalid or unknown host.
158The routine
159.Fn herror
160can be used to print an error message describing the failure.
161If its argument
162.Fa string
163is
164.Pf non Dv -NULL ,
165it is printed, followed by a colon and a space.
166The error message is printed with a trailing newline.
167.Pp 187.Pp
168The variable 188The variable
169.Va h_errno 189.Va h_errno
@@ -197,20 +217,6 @@ for example, a mail-forwarder may be registered for this domain.
197The 217The
198.Fn gethostent 218.Fn gethostent
199function 219function
200is defined, and
201.Fn sethostent
202and
203.Fn endhostent
204are redefined,
205when
206.Xr libc 3
207is built to use only the routines to lookup in
208.Pa /etc/hosts
209and not the name server.
210.Pp
211The
212.Fn gethostent
213function
214reads the next line of 220reads the next line of
215.Pa /etc/hosts , 221.Pa /etc/hosts ,
216opening the file if necessary. 222opening the file if necessary.
@@ -224,7 +230,8 @@ If the
224.Fa stayopen 230.Fa stayopen
225argument is non-zero, 231argument is non-zero,
226the file will not be closed after each call to 232the file will not be closed after each call to
227.Fn gethostbyname 233.Fn gethostbyname ,
234.Fn gethostbyname2 ,
228or 235or
229.Fn gethostbyaddr . 236.Fn gethostbyaddr .
230.Pp 237.Pp
@@ -251,4 +258,9 @@ These functions use static data storage;
251if the data is needed for future use, it should be 258if the data is needed for future use, it should be
252copied before any subsequent calls overwrite it. 259copied before any subsequent calls overwrite it.
253Only the Internet 260Only the Internet
254address format is currently understood. 261address formats are currently understood.
262.Pp
263YP does not support any address families other than
264.Dv AF_INET
265and uses
266the traditional database format.
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c
index ec3f14a900..7321225863 100644
--- a/src/lib/libc/net/gethostnamadr.c
+++ b/src/lib/libc/net/gethostnamadr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $ */
2
3/*- 1/*-
4 * Copyright (c) 1985, 1988, 1993 2 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -54,12 +52,7 @@
54 */ 52 */
55 53
56#if defined(LIBC_SCCS) && !defined(lint) 54#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 55static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.30 1998/03/16 05:06:55 millert Exp $";
58static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel ";
60#else
61static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $";
62#endif
63#endif /* LIBC_SCCS and not lint */ 56#endif /* LIBC_SCCS and not lint */
64 57
65#include <sys/param.h> 58#include <sys/param.h>
@@ -73,12 +66,16 @@ static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycro
73#include <ctype.h> 66#include <ctype.h>
74#include <errno.h> 67#include <errno.h>
75#include <string.h> 68#include <string.h>
69#include <syslog.h>
76#ifdef YP 70#ifdef YP
77#include <rpc/rpc.h> 71#include <rpc/rpc.h>
78#include <rpcsvc/yp_prot.h> 72#include <rpcsvc/yp.h>
79#include <rpcsvc/ypclnt.h> 73#include <rpcsvc/ypclnt.h>
74#include "ypinternal.h"
80#endif 75#endif
81 76
77#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
78
82#define MAXALIASES 35 79#define MAXALIASES 35
83#define MAXADDRS 35 80#define MAXADDRS 35
84 81
@@ -91,10 +88,26 @@ static char *__ypdomain;
91static struct hostent host; 88static struct hostent host;
92static char *host_aliases[MAXALIASES]; 89static char *host_aliases[MAXALIASES];
93static char hostbuf[BUFSIZ+1]; 90static char hostbuf[BUFSIZ+1];
94static struct in_addr host_addr; 91static union {
92 struct in_addr _host_in_addr;
93 u_char _host_addr[16]; /* IPv4 or IPv6 */
94} _host_addr_u;
95#define host_addr _host_addr_u._host_addr
95static FILE *hostf = NULL; 96static FILE *hostf = NULL;
96static int stayopen = 0; 97static int stayopen = 0;
97 98
99static void map_v4v6_address __P((const char *src, char *dst));
100static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len));
101
102#ifdef RESOLVSORT
103static void addrsort __P((char **, int));
104#endif
105
106int _hokchar __P((const char *));
107
108static const char AskedForGot[] =
109 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
110
98#if PACKETSZ > 1024 111#if PACKETSZ > 1024
99#define MAXPACKET PACKETSZ 112#define MAXPACKET PACKETSZ
100#else 113#else
@@ -107,31 +120,81 @@ typedef union {
107} querybuf; 120} querybuf;
108 121
109typedef union { 122typedef union {
110 int32_t al; 123 int32_t al;
111 char ac; 124 char ac;
112} align; 125} align;
113 126
114static int qcomp __P((struct in_addr **, struct in_addr **)); 127static struct hostent *getanswer __P((const querybuf *, int, const char *,
115static struct hostent *getanswer __P((querybuf *, int, int)); 128 int));
116 129
117extern int h_errno; 130extern int h_errno;
118 131
132int
133_hokchar(p)
134 const char *p;
135{
136 char c;
137
138 /*
139 * Many people do not obey RFC 822 and 1035. The valid
140 * characters are a-z, A-Z, 0-9, '-' and . But the others
141 * tested for below can happen, and we must be more permissive
142 * than the resolver until those idiots clean up their act.
143 * We let '/' through, but not '..'
144 */
145 while ((c = *p++)) {
146 if (('a' <= c && c <= 'z') ||
147 ('A' <= c && c <= 'Z') ||
148 ('0' <= c && c <= '9'))
149 continue;
150 if (strchr("-_/", c))
151 continue;
152 if (c == '.' && *p != '.')
153 continue;
154 return 0;
155 }
156 return 1;
157}
158
119static struct hostent * 159static struct hostent *
120getanswer(answer, anslen, iquery) 160getanswer(answer, anslen, qname, qtype)
121 querybuf *answer; 161 const querybuf *answer;
122 int anslen; 162 int anslen;
123 int iquery; 163 const char *qname;
164 int qtype;
124{ 165{
125 register HEADER *hp; 166 register const HEADER *hp;
126 register u_char *cp; 167 register const u_char *cp;
127 register int n; 168 register int n;
128 u_char *eom; 169 const u_char *eom;
129 char *bp, **ap; 170 char *bp, **ap, **hap;
130 int type, class, buflen, ancount, qdcount; 171 int type, class, buflen, ancount, qdcount;
131 int haveanswer, getclass = C_ANY; 172 int haveanswer, had_error;
132 char **hap; 173 int toobig = 0;
174 char tbuf[MAXDNAME];
175 const char *tname;
176 int (*name_ok) __P((const char *));
133 177
178 tname = qname;
179 host.h_name = NULL;
134 eom = answer->buf + anslen; 180 eom = answer->buf + anslen;
181 switch (qtype) {
182 case T_A:
183 case T_AAAA:
184#ifdef USE_RESOLV_NAME_OK
185 name_ok = res_hnok;
186 break;
187#endif
188 case T_PTR:
189#ifdef USE_RESOLV_NAME_OK
190 name_ok = res_dnok;
191#else
192 name_ok = _hokchar;
193#endif
194 break;
195 default:
196 return (NULL);
197 }
135 /* 198 /*
136 * find first satisfactory answer 199 * find first satisfactory answer
137 */ 200 */
@@ -139,31 +202,29 @@ getanswer(answer, anslen, iquery)
139 ancount = ntohs(hp->ancount); 202 ancount = ntohs(hp->ancount);
140 qdcount = ntohs(hp->qdcount); 203 qdcount = ntohs(hp->qdcount);
141 bp = hostbuf; 204 bp = hostbuf;
142 buflen = sizeof(hostbuf); 205 buflen = sizeof hostbuf;
143 cp = answer->buf + sizeof(HEADER); 206 cp = answer->buf + HFIXEDSZ;
144 if (qdcount) { 207 if (qdcount != 1) {
145 if (iquery) { 208 h_errno = NO_RECOVERY;
146 if ((n = dn_expand((u_char *)answer->buf, 209 return (NULL);
147 (u_char *)eom, (u_char *)cp, (u_char *)bp, 210 }
148 buflen)) < 0) { 211 n = dn_expand(answer->buf, eom, cp, bp, buflen);
149 h_errno = NO_RECOVERY; 212 if ((n < 0) || !(*name_ok)(bp)) {
150 return ((struct hostent *) NULL); 213 h_errno = NO_RECOVERY;
151 } 214 return (NULL);
152 cp += n + QFIXEDSZ; 215 }
153 host.h_name = bp; 216 cp += n + QFIXEDSZ;
154 n = strlen(bp) + 1; 217 if (qtype == T_A || qtype == T_AAAA) {
155 bp += n; 218 /* res_send() has already verified that the query name is the
156 buflen -= n; 219 * same as the one we sent; this just gets the expanded name
157 } else 220 * (i.e., with the succeeding search-domain tacked on).
158 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 221 */
159 while (--qdcount > 0) 222 n = strlen(bp) + 1; /* for the \0 */
160 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 223 host.h_name = bp;
161 } else if (iquery) { 224 bp += n;
162 if (hp->aa) 225 buflen -= n;
163 h_errno = HOST_NOT_FOUND; 226 /* The qname can be abbreviated, but h_name is now absolute. */
164 else 227 qname = host.h_name;
165 h_errno = TRY_AGAIN;
166 return ((struct hostent *) NULL);
167 } 228 }
168 ap = host_aliases; 229 ap = host_aliases;
169 *ap = NULL; 230 *ap = NULL;
@@ -172,104 +233,256 @@ getanswer(answer, anslen, iquery)
172 *hap = NULL; 233 *hap = NULL;
173 host.h_addr_list = h_addr_ptrs; 234 host.h_addr_list = h_addr_ptrs;
174 haveanswer = 0; 235 haveanswer = 0;
175 while (--ancount >= 0 && cp < eom) { 236 had_error = 0;
176 if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, 237 while (ancount-- > 0 && cp < eom && !had_error) {
177 (u_char *)cp, (u_char *)bp, buflen)) < 0) 238 n = dn_expand(answer->buf, eom, cp, bp, buflen);
178 break; 239 if ((n < 0) || !(*name_ok)(bp)) {
179 cp += n; 240 had_error++;
241 continue;
242 }
243 cp += n; /* name */
180 type = _getshort(cp); 244 type = _getshort(cp);
181 cp += sizeof(u_int16_t); 245 cp += INT16SZ; /* type */
182 class = _getshort(cp); 246 class = _getshort(cp);
183 cp += sizeof(u_int16_t) + sizeof(u_int32_t); 247 cp += INT16SZ + INT32SZ; /* class, TTL */
184 n = _getshort(cp); 248 n = _getshort(cp);
185 cp += sizeof(u_int16_t); 249 cp += INT16SZ; /* len */
186 if (type == T_CNAME) { 250 if (class != C_IN) {
251 /* XXX - debug? syslog? */
187 cp += n; 252 cp += n;
253 continue; /* XXX - had_error++ ? */
254 }
255 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
188 if (ap >= &host_aliases[MAXALIASES-1]) 256 if (ap >= &host_aliases[MAXALIASES-1])
189 continue; 257 continue;
258 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
259 if ((n < 0) || !(*name_ok)(tbuf)) {
260 had_error++;
261 continue;
262 }
263 cp += n;
264 /* Store alias. */
190 *ap++ = bp; 265 *ap++ = bp;
191 n = strlen(bp) + 1; 266 n = strlen(bp) + 1; /* for the \0 */
267 bp += n;
268 buflen -= n;
269 /* Get canonical name. */
270 n = strlen(tbuf) + 1; /* for the \0 */
271 if (n > buflen) {
272 had_error++;
273 continue;
274 }
275 strcpy(bp, tbuf);
276 host.h_name = bp;
192 bp += n; 277 bp += n;
193 buflen -= n; 278 buflen -= n;
194 continue; 279 continue;
195 } 280 }
196 if (iquery && type == T_PTR) { 281 if (qtype == T_PTR && type == T_CNAME) {
197 if ((n = dn_expand((u_char *)answer->buf, 282 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
198 (u_char *)eom, (u_char *)cp, (u_char *)bp, 283 if ((n < 0) || !res_hnok(tbuf)) {
199 buflen)) < 0) 284 had_error++;
200 break; 285 continue;
286 }
201 cp += n; 287 cp += n;
202 host.h_name = bp; 288 /* Get canonical name. */
203 return(&host); 289 n = strlen(tbuf) + 1; /* for the \0 */
290 if (n > buflen) {
291 had_error++;
292 continue;
293 }
294 strcpy(bp, tbuf);
295 tname = bp;
296 bp += n;
297 buflen -= n;
298 continue;
204 } 299 }
205 if (iquery || type != T_A) { 300 if (type != qtype) {
206#ifdef DEBUG 301 syslog(LOG_NOTICE|LOG_AUTH,
207 if (_res.options & RES_DEBUG) 302 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
208 printf("unexpected answer type %d, size %d\n", 303 qname, p_class(C_IN), p_type(qtype),
209 type, n); 304 p_type(type));
210#endif
211 cp += n; 305 cp += n;
212 continue; 306 continue; /* XXX - had_error++ ? */
213 } 307 }
214 if (haveanswer) { 308 switch (type) {
215 if (n != host.h_length) { 309 case T_PTR:
310 if (strcasecmp(tname, bp) != 0) {
311 syslog(LOG_NOTICE|LOG_AUTH,
312 AskedForGot, qname, bp);
216 cp += n; 313 cp += n;
217 continue; 314 continue; /* XXX - had_error++ ? */
315 }
316 n = dn_expand(answer->buf, eom, cp, bp, buflen);
317 if ((n < 0) || !res_hnok(bp)) {
318 had_error++;
319 break;
320 }
321#if MULTI_PTRS_ARE_ALIASES
322 cp += n;
323 if (!haveanswer)
324 host.h_name = bp;
325 else if (ap < &host_aliases[MAXALIASES-1])
326 *ap++ = bp;
327 else
328 n = -1;
329 if (n != -1) {
330 n = strlen(bp) + 1; /* for the \0 */
331 bp += n;
332 buflen -= n;
218 } 333 }
219 if (class != getclass) { 334 break;
335#else
336 host.h_name = bp;
337 if (_res.options & RES_USE_INET6) {
338 n = strlen(bp) + 1; /* for the \0 */
339 bp += n;
340 buflen -= n;
341 map_v4v6_hostent(&host, &bp, &buflen);
342 }
343 h_errno = NETDB_SUCCESS;
344 return (&host);
345#endif
346 case T_A:
347 case T_AAAA:
348 if (strcasecmp(host.h_name, bp) != 0) {
349 syslog(LOG_NOTICE|LOG_AUTH,
350 AskedForGot, host.h_name, bp);
351 cp += n;
352 continue; /* XXX - had_error++ ? */
353 }
354 if (n != host.h_length) {
220 cp += n; 355 cp += n;
221 continue; 356 continue;
222 } 357 }
223 } else { 358 if (!haveanswer) {
224 host.h_length = n; 359 register int nn;
225 getclass = class; 360
226 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
227 if (!iquery) {
228 host.h_name = bp; 361 host.h_name = bp;
229 bp += strlen(bp) + 1; 362 nn = strlen(bp) + 1; /* for the \0 */
363 bp += nn;
364 buflen -= nn;
230 } 365 }
231 }
232 366
233 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 367 bp += sizeof(align) - ((u_long)bp % sizeof(align));
234 368
235 if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 369 if (bp + n >= &hostbuf[sizeof hostbuf]) {
370#ifdef DEBUG
371 if (_res.options & RES_DEBUG)
372 printf("size (%d) too big\n", n);
373#endif
374 had_error++;
375 continue;
376 }
377 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
378 if (!toobig++)
236#ifdef DEBUG 379#ifdef DEBUG
237 if (_res.options & RES_DEBUG) 380 if (_res.options & RES_DEBUG)
238 printf("size (%d) too big\n", n); 381 printf("Too many addresses (%d)\n", MAXADDRS);
239#endif 382#endif
383 cp += n;
384 continue;
385 }
386 bcopy(cp, *hap++ = bp, n);
387 bp += n;
388 buflen -= n;
389 cp += n;
240 break; 390 break;
241 } 391 }
242 bcopy(cp, *hap++ = bp, n); 392 if (!had_error)
243 bp +=n; 393 haveanswer++;
244 cp += n;
245 haveanswer++;
246 } 394 }
247 if (haveanswer) { 395 if (haveanswer) {
248 *ap = NULL; 396 *ap = NULL;
249 *hap = NULL; 397 *hap = NULL;
250 if (_res.nsort) { 398# if defined(RESOLVSORT)
251 qsort(host.h_addr_list, haveanswer, 399 /*
252 sizeof(struct in_addr), 400 * Note: we sort even if host can take only one address
253 (int (*)__P((const void *, const void *)))qcomp); 401 * in its return structures - should give it the "best"
402 * address in that case, not some random one
403 */
404 if (_res.nsort && haveanswer > 1 && qtype == T_A)
405 addrsort(h_addr_ptrs, haveanswer);
406# endif /*RESOLVSORT*/
407 if (!host.h_name) {
408 n = strlen(qname) + 1; /* for the \0 */
409 if (n > buflen)
410 goto try_again;
411 strcpy(bp, qname);
412 host.h_name = bp;
413 bp += n;
414 buflen -= n;
254 } 415 }
416 if (_res.options & RES_USE_INET6)
417 map_v4v6_hostent(&host, &bp, &buflen);
418 h_errno = NETDB_SUCCESS;
255 return (&host); 419 return (&host);
256 } else {
257 h_errno = TRY_AGAIN;
258 return ((struct hostent *) NULL);
259 } 420 }
421 try_again:
422 h_errno = TRY_AGAIN;
423 return (NULL);
260} 424}
261 425
262struct hostent * 426struct hostent *
263gethostbyname(name) 427gethostbyname(name)
264 const char *name; 428 const char *name;
265{ 429{
430 struct hostent *hp;
431 extern struct hostent *_gethtbyname2();
432
433 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
434 return (_gethtbyname2(name, AF_INET));
435
436 if (_res.options & RES_USE_INET6) {
437 hp = gethostbyname2(name, AF_INET6);
438 if (hp)
439 return (hp);
440 }
441 return (gethostbyname2(name, AF_INET));
442}
443
444struct hostent *
445gethostbyname2(name, af)
446 const char *name;
447 int af;
448{
266 querybuf buf; 449 querybuf buf;
267 register const char *cp; 450 register const char *cp;
268 int n, i; 451 char *bp;
269 extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); 452 int n, size, type, len, i;
453 extern struct hostent *_gethtbyname2(), *_yp_gethtbyname();
270 register struct hostent *hp; 454 register struct hostent *hp;
271 char lookups[MAXDNSLUS]; 455 char lookups[MAXDNSLUS];
272 456
457 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
458 return (_gethtbyname2(name, af));
459
460 switch (af) {
461 case AF_INET:
462 size = INADDRSZ;
463 type = T_A;
464 break;
465 case AF_INET6:
466 size = IN6ADDRSZ;
467 type = T_AAAA;
468 break;
469 default:
470 h_errno = NETDB_INTERNAL;
471 errno = EAFNOSUPPORT;
472 return (NULL);
473 }
474
475 host.h_addrtype = af;
476 host.h_length = size;
477
478 /*
479 * if there aren't any dots, it could be a user-level alias.
480 * this is also done in res_query() since we are not the only
481 * function that looks up host names.
482 */
483 if (!strchr(name, '.') && (cp = __hostalias(name)))
484 name = cp;
485
273 /* 486 /*
274 * disallow names consisting only of digits/dots, unless 487 * disallow names consisting only of digits/dots, unless
275 * they end in a dot. 488 * they end in a dot.
@@ -284,26 +497,59 @@ gethostbyname(name)
284 * Fake up a hostent as if we'd actually 497 * Fake up a hostent as if we'd actually
285 * done a lookup. 498 * done a lookup.
286 */ 499 */
287 if (!inet_aton(name, &host_addr)) { 500 if (inet_pton(af, name, host_addr) <= 0) {
288 h_errno = HOST_NOT_FOUND; 501 h_errno = HOST_NOT_FOUND;
289 return((struct hostent *) NULL); 502 return (NULL);
290 } 503 }
291 host.h_name = (char *)name; 504 strncpy(hostbuf, name, MAXHOSTNAMELEN-1);
505 hostbuf[MAXHOSTNAMELEN-1] = '\0';
506 bp = hostbuf + MAXHOSTNAMELEN;
507 len = sizeof hostbuf - MAXHOSTNAMELEN;
508 host.h_name = hostbuf;
292 host.h_aliases = host_aliases; 509 host.h_aliases = host_aliases;
293 host_aliases[0] = NULL; 510 host_aliases[0] = NULL;
294 host.h_addrtype = AF_INET; 511 h_addr_ptrs[0] = (char *)host_addr;
295 host.h_length = sizeof(u_int32_t);
296 h_addr_ptrs[0] = (char *)&host_addr;
297 h_addr_ptrs[1] = NULL; 512 h_addr_ptrs[1] = NULL;
298 host.h_addr_list = h_addr_ptrs; 513 host.h_addr_list = h_addr_ptrs;
514 if (_res.options & RES_USE_INET6)
515 map_v4v6_hostent(&host, &bp, &len);
516 h_errno = NETDB_SUCCESS;
299 return (&host); 517 return (&host);
300 } 518 }
301 if (!isdigit(*cp) && *cp != '.') 519 if (!isdigit(*cp) && *cp != '.')
302 break; 520 break;
303 } 521 }
304 522 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
305 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 523 name[0] == ':')
306 return (_gethtbyname(name)); 524 for (cp = name;; ++cp) {
525 if (!*cp) {
526 if (*--cp == '.')
527 break;
528 /*
529 * All-IPv6-legal, no dot at the end.
530 * Fake up a hostent as if we'd actually
531 * done a lookup.
532 */
533 if (inet_pton(af, name, host_addr) <= 0) {
534 h_errno = HOST_NOT_FOUND;
535 return (NULL);
536 }
537 strncpy(hostbuf, name, MAXHOSTNAMELEN-1);
538 hostbuf[MAXHOSTNAMELEN-1] = '\0';
539 bp = hostbuf + MAXHOSTNAMELEN;
540 len = sizeof hostbuf - MAXHOSTNAMELEN;
541 host.h_name = hostbuf;
542 host.h_aliases = host_aliases;
543 host_aliases[0] = NULL;
544 h_addr_ptrs[0] = (char *)host_addr;
545 h_addr_ptrs[1] = NULL;
546 host.h_addr_list = h_addr_ptrs;
547 h_errno = NETDB_SUCCESS;
548 return (&host);
549 }
550 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
551 break;
552 }
307 553
308 bcopy(_res.lookups, lookups, sizeof lookups); 554 bcopy(_res.lookups, lookups, sizeof lookups);
309 if (lookups[0] == '\0') 555 if (lookups[0] == '\0')
@@ -314,11 +560,13 @@ gethostbyname(name)
314 switch (lookups[i]) { 560 switch (lookups[i]) {
315#ifdef YP 561#ifdef YP
316 case 'y': 562 case 'y':
317 hp = _yp_gethtbyname(name); 563 /* YP only supports AF_INET. */
564 if (af == AF_INET)
565 hp = _yp_gethtbyname(name);
318 break; 566 break;
319#endif 567#endif
320 case 'b': 568 case 'b':
321 if ((n = res_search(name, C_IN, T_A, buf.buf, 569 if ((n = res_search(name, C_IN, type, buf.buf,
322 sizeof(buf))) < 0) { 570 sizeof(buf))) < 0) {
323#ifdef DEBUG 571#ifdef DEBUG
324 if (_res.options & RES_DEBUG) 572 if (_res.options & RES_DEBUG)
@@ -326,38 +574,79 @@ gethostbyname(name)
326#endif 574#endif
327 break; 575 break;
328 } 576 }
329 hp = getanswer(&buf, n, 0); 577 hp = getanswer(&buf, n, name, type);
330 break; 578 break;
331 case 'f': 579 case 'f':
332 hp = _gethtbyname(name); 580 hp = _gethtbyname2(name, af);
333 break; 581 break;
334 } 582 }
335 } 583 }
584 /* XXX h_errno not correct in all cases... */
336 return (hp); 585 return (hp);
337} 586}
338 587
339struct hostent * 588struct hostent *
340gethostbyaddr(addr, len, type) 589gethostbyaddr(addr, len, af)
341 const char *addr; 590 const char *addr; /* XXX should have been def'd as u_char! */
342 int len, type; 591 int len, af;
343{ 592{
344 int n, i; 593 const u_char *uaddr = (const u_char *)addr;
594 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
595 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
596 int n, size, i;
345 querybuf buf; 597 querybuf buf;
346 register struct hostent *hp; 598 register struct hostent *hp;
347 char qbuf[MAXDNAME]; 599 char qbuf[MAXDNAME+1], *qp;
348 extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); 600 extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr();
349 char lookups[MAXDNSLUS]; 601 char lookups[MAXDNSLUS];
350 602
351 if (type != AF_INET)
352 return ((struct hostent *) NULL);
353 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
354 ((unsigned)addr[3] & 0xff),
355 ((unsigned)addr[2] & 0xff),
356 ((unsigned)addr[1] & 0xff),
357 ((unsigned)addr[0] & 0xff));
358
359 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 603 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
360 return (_gethtbyaddr(addr, len, type)); 604 return (_gethtbyaddr(addr, len, af));
605
606 if (af == AF_INET6 && len == IN6ADDRSZ &&
607 (!bcmp(uaddr, mapped, sizeof mapped) ||
608 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
609 /* Unmap. */
610 addr += sizeof mapped;
611 uaddr += sizeof mapped;
612 af = AF_INET;
613 len = INADDRSZ;
614 }
615 switch (af) {
616 case AF_INET:
617 size = INADDRSZ;
618 break;
619 case AF_INET6:
620 size = IN6ADDRSZ;
621 break;
622 default:
623 errno = EAFNOSUPPORT;
624 h_errno = NETDB_INTERNAL;
625 return (NULL);
626 }
627 if (size != len) {
628 errno = EINVAL;
629 h_errno = NETDB_INTERNAL;
630 return (NULL);
631 }
632 switch (af) {
633 case AF_INET:
634 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
635 (uaddr[3] & 0xff),
636 (uaddr[2] & 0xff),
637 (uaddr[1] & 0xff),
638 (uaddr[0] & 0xff));
639 break;
640 case AF_INET6:
641 qp = qbuf;
642 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
643 qp += sprintf(qp, "%x.%x.",
644 uaddr[n] & 0xf,
645 (uaddr[n] >> 4) & 0xf);
646 }
647 strcpy(qp, "ip6.int");
648 break;
649 }
361 650
362 bcopy(_res.lookups, lookups, sizeof lookups); 651 bcopy(_res.lookups, lookups, sizeof lookups);
363 if (lookups[0] == '\0') 652 if (lookups[0] == '\0')
@@ -368,11 +657,14 @@ gethostbyaddr(addr, len, type)
368 switch (lookups[i]) { 657 switch (lookups[i]) {
369#ifdef YP 658#ifdef YP
370 case 'y': 659 case 'y':
371 hp = _yp_gethtbyaddr(addr, len, type); 660 /* YP only supports AF_INET. */
661 if (af == AF_INET)
662 hp = _yp_gethtbyaddr(addr);
372 break; 663 break;
373#endif 664#endif
374 case 'b': 665 case 'b':
375 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); 666 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf,
667 sizeof buf.buf);
376 if (n < 0) { 668 if (n < 0) {
377#ifdef DEBUG 669#ifdef DEBUG
378 if (_res.options & RES_DEBUG) 670 if (_res.options & RES_DEBUG)
@@ -380,20 +672,27 @@ gethostbyaddr(addr, len, type)
380#endif 672#endif
381 break; 673 break;
382 } 674 }
383 hp = getanswer(&buf, n, 1); 675 if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
384 if (hp == NULL)
385 break; 676 break;
386 hp->h_addrtype = type; 677 hp->h_addrtype = af;
387 hp->h_length = len; 678 hp->h_length = len;
388 h_addr_ptrs[0] = (char *)&host_addr; 679 bcopy(addr, host_addr, len);
389 h_addr_ptrs[1] = (char *)0; 680 h_addr_ptrs[0] = (char *)host_addr;
390 host_addr = *(struct in_addr *)addr; 681 h_addr_ptrs[1] = NULL;
682 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
683 map_v4v6_address((char*)host_addr,
684 (char*)host_addr);
685 hp->h_addrtype = AF_INET6;
686 hp->h_length = IN6ADDRSZ;
687 }
688 h_errno = NETDB_SUCCESS;
391 break; 689 break;
392 case 'f': 690 case 'f':
393 hp = _gethtbyaddr(addr, len, type); 691 hp = _gethtbyaddr(addr, len, af);
394 break; 692 break;
395 } 693 }
396 } 694 }
695 /* XXX h_errno not correct in all cases... */
397 return (hp); 696 return (hp);
398} 697}
399 698
@@ -422,35 +721,57 @@ _gethtent()
422{ 721{
423 char *p; 722 char *p;
424 register char *cp, **q; 723 register char *cp, **q;
724 int af;
725 size_t len;
425 726
426 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) 727 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
728 h_errno = NETDB_INTERNAL;
427 return (NULL); 729 return (NULL);
428again: 730 }
429 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) 731 again:
732 if ((p = fgetln(hostf, &len)) == NULL) {
733 h_errno = HOST_NOT_FOUND;
430 return (NULL); 734 return (NULL);
431 if (*p == '#') 735 }
736 if (p[len-1] == '\n')
737 len--;
738 if (len >= sizeof(hostbuf) || len == 0)
432 goto again; 739 goto again;
433 cp = strpbrk(p, "#\n"); 740 p = memcpy(hostbuf, p, len);
434 if (cp == NULL) 741 hostbuf[len] = '\0';
742 if (*p == '#')
435 goto again; 743 goto again;
436 *cp = '\0'; 744 if ((cp = strchr(p, '#')))
437 cp = strpbrk(p, " \t"); 745 *cp = '\0';
438 if (cp == NULL) 746 if (!(cp = strpbrk(p, " \t")))
439 goto again; 747 goto again;
440 *cp++ = '\0'; 748 *cp++ = '\0';
441 /* THIS STUFF IS INTERNET SPECIFIC */ 749 if ((_res.options & RES_USE_INET6) &&
442 h_addr_ptrs[0] = (char *)&host_addr; 750 inet_pton(AF_INET6, p, host_addr) > 0) {
751 af = AF_INET6;
752 len = IN6ADDRSZ;
753 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
754 if (_res.options & RES_USE_INET6) {
755 map_v4v6_address((char*)host_addr, (char*)host_addr);
756 af = AF_INET6;
757 len = IN6ADDRSZ;
758 } else {
759 af = AF_INET;
760 len = INADDRSZ;
761 }
762 } else {
763 goto again;
764 }
765 h_addr_ptrs[0] = (char *)host_addr;
443 h_addr_ptrs[1] = NULL; 766 h_addr_ptrs[1] = NULL;
444 (void) inet_aton(p, &host_addr);
445 host.h_addr_list = h_addr_ptrs; 767 host.h_addr_list = h_addr_ptrs;
446 host.h_length = sizeof(u_int32_t); 768 host.h_length = len;
447 host.h_addrtype = AF_INET; 769 host.h_addrtype = af;
448 while (*cp == ' ' || *cp == '\t') 770 while (*cp == ' ' || *cp == '\t')
449 cp++; 771 cp++;
450 host.h_name = cp; 772 host.h_name = cp;
451 q = host.h_aliases = host_aliases; 773 q = host.h_aliases = host_aliases;
452 cp = strpbrk(cp, " \t"); 774 if ((cp = strpbrk(cp, " \t")))
453 if (cp != NULL)
454 *cp++ = '\0'; 775 *cp++ = '\0';
455 while (cp && *cp) { 776 while (cp && *cp) {
456 if (*cp == ' ' || *cp == '\t') { 777 if (*cp == ' ' || *cp == '\t') {
@@ -459,70 +780,73 @@ again:
459 } 780 }
460 if (q < &host_aliases[MAXALIASES - 1]) 781 if (q < &host_aliases[MAXALIASES - 1])
461 *q++ = cp; 782 *q++ = cp;
462 cp = strpbrk(cp, " \t"); 783 if ((cp = strpbrk(cp, " \t")))
463 if (cp != NULL)
464 *cp++ = '\0'; 784 *cp++ = '\0';
465 } 785 }
466 *q = NULL; 786 *q = NULL;
787 if (_res.options & RES_USE_INET6) {
788 char *bp = hostbuf;
789 int buflen = sizeof hostbuf;
790
791 map_v4v6_hostent(&host, &bp, &buflen);
792 }
793 h_errno = NETDB_SUCCESS;
467 return (&host); 794 return (&host);
468} 795}
469 796
470struct hostent * 797struct hostent *
471_gethtbyname(name) 798_gethtbyname(name)
472 char *name; 799 const char *name;
800{
801 extern struct hostent *_gethtbyname2();
802 struct hostent *hp;
803
804 if (_res.options & RES_USE_INET6) {
805 hp = _gethtbyname2(name, AF_INET6);
806 if (hp)
807 return (hp);
808 }
809 return (_gethtbyname2(name, AF_INET));
810}
811
812struct hostent *
813_gethtbyname2(name, af)
814 const char *name;
815 int af;
473{ 816{
474 register struct hostent *p; 817 register struct hostent *p;
475 register char **cp; 818 register char **cp;
476 819
477 _sethtent(0); 820 _sethtent(0);
478 while (p = _gethtent()) { 821 while ((p = _gethtent())) {
822 if (p->h_addrtype != af)
823 continue;
479 if (strcasecmp(p->h_name, name) == 0) 824 if (strcasecmp(p->h_name, name) == 0)
480 break; 825 break;
481 for (cp = p->h_aliases; *cp != 0; cp++) 826 for (cp = p->h_aliases; *cp != 0; cp++)
482 if (strcasecmp(*cp, name) == 0) 827 if (strcasecmp(*cp, name) == 0)
483 goto found; 828 goto found;
484 } 829 }
485found: 830 found:
486 _endhtent(); 831 _endhtent();
487 if (p==NULL)
488 h_errno = HOST_NOT_FOUND;
489 return (p); 832 return (p);
490} 833}
491 834
492struct hostent * 835struct hostent *
493_gethtbyaddr(addr, len, type) 836_gethtbyaddr(addr, len, af)
494 const char *addr; 837 const char *addr;
495 int len, type; 838 int len, af;
496{ 839{
497 register struct hostent *p; 840 register struct hostent *p;
498 841
499 _sethtent(0); 842 _sethtent(0);
500 while (p = _gethtent()) 843 while ((p = _gethtent()))
501 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 844 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
502 break; 845 break;
503 _endhtent(); 846 _endhtent();
504 if (p==NULL)
505 h_errno = HOST_NOT_FOUND;
506 return (p); 847 return (p);
507} 848}
508 849
509static int
510qcomp(a1, a2)
511 struct in_addr **a1, **a2;
512{
513 int pos1, pos2;
514
515 for (pos1 = 0; pos1 < _res.nsort; pos1++)
516 if (_res.sort_list[pos1].addr.s_addr ==
517 ((*a1)->s_addr & _res.sort_list[pos1].mask))
518 break;
519 for (pos2 = 0; pos2 < _res.nsort; pos2++)
520 if (_res.sort_list[pos2].addr.s_addr ==
521 ((*a2)->s_addr & _res.sort_list[pos2].mask))
522 break;
523 return pos1 - pos2;
524}
525
526#ifdef YP 850#ifdef YP
527struct hostent * 851struct hostent *
528_yphostent(line) 852_yphostent(line)
@@ -537,7 +861,7 @@ _yphostent(line)
537 861
538 host.h_name = NULL; 862 host.h_name = NULL;
539 host.h_addr_list = h_addr_ptrs; 863 host.h_addr_list = h_addr_ptrs;
540 host.h_length = sizeof(u_int32_t); 864 host.h_length = INADDRSZ;
541 host.h_addrtype = AF_INET; 865 host.h_addrtype = AF_INET;
542 hap = h_addr_ptrs; 866 hap = h_addr_ptrs;
543 buf = host_addrs; 867 buf = host_addrs;
@@ -598,9 +922,8 @@ done:
598} 922}
599 923
600struct hostent * 924struct hostent *
601_yp_gethtbyaddr(addr, len, type) 925_yp_gethtbyaddr(addr)
602 const char *addr; 926 const char *addr;
603 int len, type;
604{ 927{
605 struct hostent *hp = (struct hostent *)NULL; 928 struct hostent *hp = (struct hostent *)NULL;
606 static char *__ypcurrent; 929 static char *__ypcurrent;
@@ -636,6 +959,8 @@ _yp_gethtbyname(name)
636 static char *__ypcurrent; 959 static char *__ypcurrent;
637 int __ypcurrentlen, r; 960 int __ypcurrentlen, r;
638 961
962 if (strlen(name) >= MAXHOSTNAMELEN)
963 return (NULL);
639 if (!__ypdomain) { 964 if (!__ypdomain) {
640 if (_yp_check(&__ypdomain) == 0) 965 if (_yp_check(&__ypdomain) == 0)
641 return (hp); 966 return (hp);
@@ -645,10 +970,110 @@ _yp_gethtbyname(name)
645 __ypcurrent = NULL; 970 __ypcurrent = NULL;
646 r = yp_match(__ypdomain, "hosts.byname", name, 971 r = yp_match(__ypdomain, "hosts.byname", name,
647 strlen(name), &__ypcurrent, &__ypcurrentlen); 972 strlen(name), &__ypcurrent, &__ypcurrentlen);
648 if (r==0) 973 if (r == 0)
649 hp = _yphostent(__ypcurrent); 974 hp = _yphostent(__ypcurrent);
650 if (hp==NULL) 975 if (hp == NULL)
651 h_errno = HOST_NOT_FOUND; 976 h_errno = HOST_NOT_FOUND;
652 return (hp); 977 return (hp);
653} 978}
654#endif 979#endif
980
981static void
982map_v4v6_address(src, dst)
983 const char *src;
984 char *dst;
985{
986 u_char *p = (u_char *)dst;
987 char tmp[INADDRSZ];
988 int i;
989
990 /* Stash a temporary copy so our caller can update in place. */
991 bcopy(src, tmp, INADDRSZ);
992 /* Mark this ipv6 addr as a mapped ipv4. */
993 for (i = 0; i < 10; i++)
994 *p++ = 0x00;
995 *p++ = 0xff;
996 *p++ = 0xff;
997 /* Retrieve the saved copy and we're done. */
998 bcopy(tmp, (void*)p, INADDRSZ);
999}
1000
1001static void
1002map_v4v6_hostent(hp, bpp, lenp)
1003 struct hostent *hp;
1004 char **bpp;
1005 int *lenp;
1006{
1007 char **ap;
1008
1009 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
1010 return;
1011 hp->h_addrtype = AF_INET6;
1012 hp->h_length = IN6ADDRSZ;
1013 for (ap = hp->h_addr_list; *ap; ap++) {
1014 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
1015
1016 if (*lenp < (i + IN6ADDRSZ)) {
1017 /* Out of memory. Truncate address list here. XXX */
1018 *ap = NULL;
1019 return;
1020 }
1021 *bpp += i;
1022 *lenp -= i;
1023 map_v4v6_address(*ap, *bpp);
1024 *ap = *bpp;
1025 *bpp += IN6ADDRSZ;
1026 *lenp -= IN6ADDRSZ;
1027 }
1028}
1029
1030struct hostent *
1031gethostent()
1032{
1033 return (_gethtent());
1034}
1035
1036#ifdef RESOLVSORT
1037static void
1038addrsort(ap, num)
1039 char **ap;
1040 int num;
1041{
1042 int i, j;
1043 char **p;
1044 short aval[MAXADDRS];
1045 int needsort = 0;
1046
1047 p = ap;
1048 for (i = 0; i < num; i++, p++) {
1049 for (j = 0 ; (unsigned)j < _res.nsort; j++)
1050 if (_res.sort_list[j].addr.s_addr ==
1051 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
1052 break;
1053 aval[i] = j;
1054 if (needsort == 0 && i > 0 && j < aval[i-1])
1055 needsort = i;
1056 }
1057 if (!needsort)
1058 return;
1059
1060 while (needsort < num) {
1061 for (j = needsort - 1; j >= 0; j--) {
1062 if (aval[j] > aval[j+1]) {
1063 char *hp;
1064
1065 i = aval[j];
1066 aval[j] = aval[j+1];
1067 aval[j+1] = i;
1068
1069 hp = ap[j];
1070 ap[j] = ap[j+1];
1071 ap[j+1] = hp;
1072
1073 } else
1074 break;
1075 }
1076 needsort++;
1077 }
1078}
1079#endif
diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c
index c193860e36..925d1d5895 100644
--- a/src/lib/libc/net/getnetbyaddr.c
+++ b/src/lib/libc/net/getnetbyaddr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getnetbyaddr.c,v 1.5 1997/07/09 01:08:28 millert Exp $";
38static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -46,14 +40,14 @@ static char rcsid[] = "$NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp
46extern int _net_stayopen; 40extern int _net_stayopen;
47 41
48struct netent * 42struct netent *
49getnetbyaddr(net, type) 43_getnetbyaddr(net, type)
50 register long net; 44 register in_addr_t net;
51 register int type; 45 register int type;
52{ 46{
53 register struct netent *p; 47 register struct netent *p;
54 48
55 setnetent(_net_stayopen); 49 setnetent(_net_stayopen);
56 while (p = getnetent()) 50 while ((p = getnetent()))
57 if (p->n_addrtype == type && p->n_net == net) 51 if (p->n_addrtype == type && p->n_net == net)
58 break; 52 break;
59 if (!_net_stayopen) 53 if (!_net_stayopen)
diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c
index 93a2e1256c..4e39cf6860 100644
--- a/src/lib/libc/net/getnetbyname.c
+++ b/src/lib/libc/net/getnetbyname.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getnetbyname.c,v 1.5 1997/07/09 01:08:29 millert Exp $";
38static char sccsid[] = "@(#)getnetbyname.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -47,18 +41,18 @@ static char rcsid[] = "$NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp
47extern int _net_stayopen; 41extern int _net_stayopen;
48 42
49struct netent * 43struct netent *
50getnetbyname(name) 44_getnetbyname(name)
51 register const char *name; 45 register const char *name;
52{ 46{
53 register struct netent *p; 47 register struct netent *p;
54 register char **cp; 48 register char **cp;
55 49
56 setnetent(_net_stayopen); 50 setnetent(_net_stayopen);
57 while (p = getnetent()) { 51 while ((p = getnetent())) {
58 if (strcmp(p->n_name, name) == 0) 52 if (strcasecmp(p->n_name, name) == 0)
59 break; 53 break;
60 for (cp = p->n_aliases; *cp != 0; cp++) 54 for (cp = p->n_aliases; *cp != 0; cp++)
61 if (strcmp(*cp, name) == 0) 55 if (strcasecmp(*cp, name) == 0)
62 goto found; 56 goto found;
63 } 57 }
64found: 58found:
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3
index d4f0bedbf9..5864b75839 100644
--- a/src/lib/libc/net/getnetent.3
+++ b/src/lib/libc/net/getnetent.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: getnetent.3,v 1.3 1995/02/25 06:20:32 cgd Exp $ 1.\" $OpenBSD: getnetent.3,v 1.5 1998/03/16 05:06:56 millert Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,11 +31,9 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)getnetent.3 8.1 (Berkeley) 6/4/93 34.Dd March 13, 1997
35.\"
36.Dd June 4, 1993
37.Dt GETNETENT 3 35.Dt GETNETENT 3
38.Os BSD 4.2 36.Os
39.Sh NAME 37.Sh NAME
40.Nm getnetent , 38.Nm getnetent ,
41.Nm getnetbyaddr , 39.Nm getnetbyaddr ,
@@ -50,7 +48,7 @@
50.Ft struct netent * 48.Ft struct netent *
51.Fn getnetbyname "char *name" 49.Fn getnetbyname "char *name"
52.Ft struct netent * 50.Ft struct netent *
53.Fn getnetbyaddr "long net" "int type" 51.Fn getnetbyaddr "in_addr_t net" "int type"
54.Fn setnetent "int stayopen" 52.Fn setnetent "int stayopen"
55.Fn endnetent 53.Fn endnetent
56.Sh DESCRIPTION 54.Sh DESCRIPTION
@@ -70,7 +68,7 @@ struct netent {
70 char *n_name; /* official name of net */ 68 char *n_name; /* official name of net */
71 char **n_aliases; /* alias list */ 69 char **n_aliases; /* alias list */
72 int n_addrtype; /* net number type */ 70 int n_addrtype; /* net number type */
73 unsigned long n_net; /* net number */ 71 in_addr_t n_net; /* net number */
74}; 72};
75.Ed 73.Ed
76.Pp 74.Pp
@@ -113,11 +111,10 @@ The
113function 111function
114and 112and
115.Fn getnetbyaddr 113.Fn getnetbyaddr
116sequentially search from the beginning 114search the domain name server if the system is configured to use one.
117of the file until a matching 115If the search fails, or no name server is configured, they sequentially
118net name or 116search from the beginning of the file until a matching net name or
119net address and type is found, 117net address and type is found, or until
120or until
121.Dv EOF 118.Dv EOF
122is encountered. 119is encountered.
123Network numbers are supplied in host order. 120Network numbers are supplied in host order.
@@ -131,7 +128,8 @@ Null pointer
131.Dv EOF 128.Dv EOF
132or error. 129or error.
133.Sh SEE ALSO 130.Sh SEE ALSO
134.Xr networks 5 131.Xr networks 5 ,
132.Xr resolver 3
135.Sh HISTORY 133.Sh HISTORY
136The 134The
137.Fn getnetent , 135.Fn getnetent ,
@@ -143,11 +141,8 @@ and
143functions appeared in 141functions appeared in
144.Bx 4.2 . 142.Bx 4.2 .
145.Sh BUGS 143.Sh BUGS
146The data space used by 144The data space used by these functions is static; if future use
147these functions is static; if future use requires the data, it should be 145requires the data, it should be copied before any subsequent calls
148copied before any subsequent calls to these functions overwrite it. 146to these functions overwrite it. Only Internet network numbers
149Only Internet network 147are currently understood. Expecting network numbers to fit in no
150numbers are currently understood. 148more than 32 bits is naive.
151Expecting network numbers to fit
152in no more than 32 bits is probably
153naive.
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c
index b4e16b8f5d..8f618a1d5e 100644
--- a/src/lib/libc/net/getnetent.c
+++ b/src/lib/libc/net/getnetent.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getnetent.c,v 1.8 1998/03/16 05:06:57 millert Exp $";
38static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
@@ -81,22 +75,27 @@ endnetent()
81struct netent * 75struct netent *
82getnetent() 76getnetent()
83{ 77{
84 char *p; 78 char *p, *cp, **q;
85 register char *cp, **q; 79 size_t len;
86 80
87 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) 81 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
88 return (NULL); 82 return (NULL);
89again: 83again:
90 p = fgets(line, BUFSIZ, netf); 84 if ((p = fgetln(netf, &len)) == NULL)
91 if (p == NULL)
92 return (NULL); 85 return (NULL);
93 if (*p == '#') 86 if (p[len-1] == '\n')
87 len--;
88 if (len >= sizeof(line) || len == 0)
94 goto again; 89 goto again;
95 cp = strpbrk(p, "#\n"); 90 p = memcpy(line, p, len);
96 if (cp == NULL) 91 line[len] = '\0';
92 if (*p == '#')
97 goto again; 93 goto again;
98 *cp = '\0'; 94 if ((cp = strchr(p, '#')) != NULL)
95 *cp = '\0';
99 net.n_name = p; 96 net.n_name = p;
97 if (strlen(net.n_name) >= MAXHOSTNAMELEN-1)
98 net.n_name[MAXHOSTNAMELEN-1] = '\0';
100 cp = strpbrk(p, " \t"); 99 cp = strpbrk(p, " \t");
101 if (cp == NULL) 100 if (cp == NULL)
102 goto again; 101 goto again;
@@ -116,8 +115,11 @@ again:
116 cp++; 115 cp++;
117 continue; 116 continue;
118 } 117 }
119 if (q < &net_aliases[MAXALIASES - 1]) 118 if (q < &net_aliases[MAXALIASES - 1]) {
120 *q++ = cp; 119 *q++ = cp;
120 if (strlen(cp) >= MAXHOSTNAMELEN-1)
121 cp[MAXHOSTNAMELEN-1] = '\0';
122 }
121 cp = strpbrk(cp, " \t"); 123 cp = strpbrk(cp, " \t");
122 if (cp != NULL) 124 if (cp != NULL)
123 *cp++ = '\0'; 125 *cp++ = '\0';
diff --git a/src/lib/libc/net/getnetnamadr.c b/src/lib/libc/net/getnetnamadr.c
new file mode 100644
index 0000000000..de208bbac9
--- /dev/null
+++ b/src/lib/libc/net/getnetnamadr.c
@@ -0,0 +1,382 @@
1/* $OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1997, Jason Downs. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Jason Downs for the
17 * OpenBSD system.
18 * 4. Neither the name(s) of the author(s) nor the name OpenBSD
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 AUTHOR(S) ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * 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/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
35 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
40 */
41/*
42 * Copyright (c) 1983, 1993
43 * The Regents of the University of California. All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 */
73
74#if defined(LIBC_SCCS) && !defined(lint)
75#if 0
76static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
77static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
78static char rcsid[] = "$From: getnetnamadr.c,v 8.7 1996/08/05 08:31:35 vixie Exp $";
79#else
80static char rcsid[] = "$OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $";
81#endif
82#endif /* LIBC_SCCS and not lint */
83
84#include <sys/types.h>
85#include <sys/param.h>
86#include <sys/socket.h>
87#include <netinet/in.h>
88#include <arpa/inet.h>
89#include <arpa/nameser.h>
90
91#include <stdio.h>
92#include <netdb.h>
93#include <resolv.h>
94#include <ctype.h>
95#include <errno.h>
96#include <string.h>
97
98extern int h_errno;
99
100struct netent *_getnetbyaddr __P((in_addr_t net, int type));
101struct netent *_getnetbyname __P((const char *name));
102
103int _hokchar __P((const char *));
104
105#define BYADDR 0
106#define BYNAME 1
107#define MAXALIASES 35
108
109#if PACKETSZ > 1024
110#define MAXPACKET PACKETSZ
111#else
112#define MAXPACKET 1024
113#endif
114
115typedef union {
116 HEADER hdr;
117 u_char buf[MAXPACKET];
118} querybuf;
119
120typedef union {
121 long al;
122 char ac;
123} align;
124
125static struct netent *
126getnetanswer(answer, anslen, net_i)
127 querybuf *answer;
128 int anslen;
129 int net_i;
130{
131
132 register HEADER *hp;
133 register u_char *cp;
134 register int n;
135 u_char *eom;
136 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
137 char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
138 char *in, *st, *pauxt, *bp, **ap;
139 char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
140 static struct netent net_entry;
141 static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1];
142
143 /*
144 * find first satisfactory answer
145 *
146 * answer --> +------------+ ( MESSAGE )
147 * | Header |
148 * +------------+
149 * | Question | the question for the name server
150 * +------------+
151 * | Answer | RRs answering the question
152 * +------------+
153 * | Authority | RRs pointing toward an authority
154 * | Additional | RRs holding additional information
155 * +------------+
156 */
157 eom = answer->buf + anslen;
158 hp = &answer->hdr;
159 ancount = ntohs(hp->ancount); /* #/records in the answer section */
160 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
161 bp = netbuf;
162 buflen = sizeof(netbuf);
163 cp = answer->buf + HFIXEDSZ;
164 if (!qdcount) {
165 if (hp->aa)
166 h_errno = HOST_NOT_FOUND;
167 else
168 h_errno = TRY_AGAIN;
169 return (NULL);
170 }
171 while (qdcount-- > 0)
172 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
173 ap = net_aliases;
174 *ap = NULL;
175 net_entry.n_aliases = net_aliases;
176 haveanswer = 0;
177 while (--ancount >= 0 && cp < eom) {
178 n = dn_expand(answer->buf, eom, cp, bp, buflen);
179#ifdef USE_RESOLV_NAME_OK
180 if ((n < 0) || !res_dnok(bp))
181#else
182 if ((n < 0) || !_hokchar(bp))
183#endif
184 break;
185 cp += n;
186 ans[0] = '\0';
187 (void)strncpy(&ans[0], bp, sizeof ans-1);
188 ans[sizeof ans-1] = '\0';
189 GETSHORT(type, cp);
190 GETSHORT(class, cp);
191 cp += INT32SZ; /* TTL */
192 GETSHORT(n, cp);
193 if (class == C_IN && type == T_PTR) {
194 n = dn_expand(answer->buf, eom, cp, bp, buflen);
195 if ((n < 0) || !res_hnok(bp)) {
196 cp += n;
197 return (NULL);
198 }
199 cp += n;
200 *ap++ = bp;
201 bp += strlen(bp) + 1;
202 net_entry.n_addrtype =
203 (class == C_IN) ? AF_INET : AF_UNSPEC;
204 haveanswer++;
205 }
206 }
207 if (haveanswer) {
208 *ap = NULL;
209 switch (net_i) {
210 case BYADDR:
211 net_entry.n_name = *net_entry.n_aliases;
212 net_entry.n_net = 0L;
213 break;
214 case BYNAME:
215 in = *net_entry.n_aliases;
216 net_entry.n_name = &ans[0];
217 aux2[0] = '\0';
218 for (i = 0; i < 4; i++) {
219 for (st = in, nchar = 0;
220 *st != '.';
221 st++, nchar++)
222 ;
223 if (nchar != 1 || *in != '0' || flag) {
224 flag = 1;
225 (void)strncpy(paux1,
226 (i==0) ? in : in-1,
227 (i==0) ?nchar : nchar+1);
228 paux1[(i==0) ? nchar : nchar+1] = '\0';
229 pauxt = paux2;
230 paux2 = strcat(paux1, paux2);
231 paux1 = pauxt;
232 }
233 in = ++st;
234 }
235 net_entry.n_net = inet_network(paux2);
236 break;
237 }
238 net_entry.n_aliases++;
239 return (&net_entry);
240 }
241 h_errno = TRY_AGAIN;
242 return (NULL);
243}
244
245struct netent *
246getnetbyaddr(net, net_type)
247 register in_addr_t net;
248 register int net_type;
249{
250 unsigned int netbr[4];
251 int nn, anslen;
252 querybuf buf;
253 char qbuf[MAXDNAME];
254 in_addr_t net2;
255 struct netent *net_entry = NULL;
256 char lookups[MAXDNSLUS];
257 int i;
258
259 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
260 return(_getnetbyaddr(net, net_type));
261
262 bcopy(_res.lookups, lookups, sizeof lookups);
263 if (lookups[0] == '\0')
264 strncpy(lookups, "bf", sizeof lookups);
265
266 for (i = 0; i < MAXDNSLUS && lookups[i]; i++) {
267 switch (lookups[i]) {
268#ifdef YP
269 case 'y':
270 /* There is no YP support. */
271 break;
272#endif /* YP */
273 case 'b':
274 if (net_type != AF_INET)
275 break; /* DNS only supports AF_INET? */
276
277 for (nn = 4, net2 = net; net2; net2 >>= 8)
278 netbr[--nn] = net2 & 0xff;
279 switch (nn) {
280 case 3: /* Class A */
281 snprintf(qbuf, sizeof(qbuf),
282 "0.0.0.%u.in-addr.arpa", netbr[3]);
283 break;
284 case 2: /* Class B */
285 snprintf(qbuf, sizeof(qbuf),
286 "0.0.%u.%u.in-addr.arpa",
287 netbr[3], netbr[2]);
288 break;
289 case 1: /* Class C */
290 snprintf(qbuf, sizeof(qbuf),
291 "0.%u.%u.%u.in-addr.arpa",
292 netbr[3], netbr[2], netbr[1]);
293 break;
294 case 0: /* Class D - E */
295 snprintf(qbuf, sizeof(qbuf),
296 "%u.%u.%u.%u.in-addr.arpa",
297 netbr[3], netbr[2], netbr[1], netbr[0]);
298 break;
299 }
300 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf,
301 sizeof(buf));
302 if (anslen < 0) {
303#ifdef DEBUG
304 if (_res.options & RES_DEBUG)
305 printf("res_query failed\n");
306#endif
307 break;
308 }
309 net_entry = getnetanswer(&buf, anslen, BYADDR);
310 if (net_entry != NULL) {
311 unsigned u_net = net; /* maybe net should be unsigned ? */
312
313 /* Strip trailing zeros */
314 while ((u_net & 0xff) == 0 && u_net != 0)
315 u_net >>= 8;
316 net_entry->n_net = u_net;
317 return (net_entry);
318 }
319 break;
320 case 'f':
321 net_entry = _getnetbyaddr(net, net_type);
322 if (net_entry != NULL)
323 return (net_entry);
324 }
325 }
326
327 /* Nothing matched. */
328 return (NULL);
329}
330
331struct netent *
332getnetbyname(net)
333 register const char *net;
334{
335 int anslen;
336 querybuf buf;
337 char qbuf[MAXDNAME];
338 struct netent *net_entry = NULL;
339 char lookups[MAXDNSLUS];
340 int i;
341
342 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
343 return (_getnetbyname(net));
344
345 bcopy(_res.lookups, lookups, sizeof lookups);
346 if (lookups[0] == '\0')
347 strncpy(lookups, "bf", sizeof lookups);
348
349 for (i = 0; i < MAXDNSLUS && lookups[i]; i++) {
350 switch (lookups[i]) {
351#ifdef YP
352 case 'y':
353 /* There is no YP support. */
354 break;
355#endif /* YP */
356 case 'b':
357 strncpy(qbuf, net, sizeof qbuf-1);
358 qbuf[sizeof qbuf-1] = '\0';
359 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf,
360 sizeof(buf));
361 if (anslen < 0) {
362#ifdef DEBUG
363 if (_res.options & RES_DEBUG)
364 printf("res_query failed\n");
365#endif
366 break;
367 }
368 net_entry = getnetanswer(&buf, anslen, BYNAME);
369 if (net_entry != NULL)
370 return (net_entry);
371 break;
372 case 'f':
373 net_entry = _getnetbyname(net);
374 if (net_entry != NULL)
375 return (net_entry);
376 break;
377 }
378 }
379
380 /* Nothing matched. */
381 return (NULL);
382}
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c
index 49c09b0806..474d8d9427 100644
--- a/src/lib/libc/net/getproto.c
+++ b/src/lib/libc/net/getproto.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getproto.c,v 1.3 1997/07/09 01:08:31 millert Exp $";
38static char sccsid[] = "@(#)getproto.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -52,7 +46,7 @@ getprotobynumber(proto)
52 register struct protoent *p; 46 register struct protoent *p;
53 47
54 setprotoent(_proto_stayopen); 48 setprotoent(_proto_stayopen);
55 while (p = getprotoent()) 49 while ((p = getprotoent()))
56 if (p->p_proto == proto) 50 if (p->p_proto == proto)
57 break; 51 break;
58 if (!_proto_stayopen) 52 if (!_proto_stayopen)
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3
index 8d607199ef..f67987954f 100644
--- a/src/lib/libc/net/getprotoent.3
+++ b/src/lib/libc/net/getprotoent.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: getprotoent.3,v 1.3 1995/02/25 06:20:34 cgd Exp $ 1.\" $OpenBSD: getprotoent.3,v 1.2 1996/08/19 08:28:50 tholo Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)getprotoent.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt GETPROTOENT 3 35.Dt GETPROTOENT 3
38.Os BSD 4.2 36.Os BSD 4.2
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c
index 1179b9029b..2bef526e7a 100644
--- a/src/lib/libc/net/getprotoent.c
+++ b/src/lib/libc/net/getprotoent.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.3 1998/03/16 05:06:59 millert Exp $";
38static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
@@ -80,20 +74,24 @@ endprotoent()
80struct protoent * 74struct protoent *
81getprotoent() 75getprotoent()
82{ 76{
83 char *p; 77 char *p, *cp, **q;
84 register char *cp, **q; 78 size_t len;
85 79
86 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) 80 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
87 return (NULL); 81 return (NULL);
88again: 82again:
89 if ((p = fgets(line, BUFSIZ, protof)) == NULL) 83 if ((p = fgetln(protof, &len)) == NULL)
90 return (NULL); 84 return (NULL);
91 if (*p == '#') 85 if (p[len-1] == '\n')
86 len--;
87 if (len >= sizeof(line) || len == 0)
92 goto again; 88 goto again;
93 cp = strpbrk(p, "#\n"); 89 p = memcpy(line, p, len);
94 if (cp == NULL) 90 line[len] = '\0';
91 if (*p == '#')
95 goto again; 92 goto again;
96 *cp = '\0'; 93 if ((cp = strchr(p, '#')) != NULL)
94 *cp = '\0';
97 proto.p_name = p; 95 proto.p_name = p;
98 cp = strpbrk(p, " \t"); 96 cp = strpbrk(p, " \t");
99 if (cp == NULL) 97 if (cp == NULL)
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c
index 4f8cf21c3f..7a4e5fede5 100644
--- a/src/lib/libc/net/getprotoname.c
+++ b/src/lib/libc/net/getprotoname.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getprotoname.c,v 1.3 1997/07/09 01:08:32 millert Exp $";
38static char sccsid[] = "@(#)getprotoname.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -54,7 +48,7 @@ getprotobyname(name)
54 register char **cp; 48 register char **cp;
55 49
56 setprotoent(_proto_stayopen); 50 setprotoent(_proto_stayopen);
57 while (p = getprotoent()) { 51 while ((p = getprotoent())) {
58 if (strcmp(p->p_name, name) == 0) 52 if (strcmp(p->p_name, name) == 0)
59 break; 53 break;
60 for (cp = p->p_aliases; *cp != 0; cp++) 54 for (cp = p->p_aliases; *cp != 0; cp++)
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c
index b4a6311966..25f0e27d06 100644
--- a/src/lib/libc/net/getservbyname.c
+++ b/src/lib/libc/net/getservbyname.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.3 1997/07/09 01:08:34 millert Exp $";
38static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -54,7 +48,7 @@ getservbyname(name, proto)
54 register char **cp; 48 register char **cp;
55 49
56 setservent(_serv_stayopen); 50 setservent(_serv_stayopen);
57 while (p = getservent()) { 51 while ((p = getservent())) {
58 if (strcmp(name, p->s_name) == 0) 52 if (strcmp(name, p->s_name) == 0)
59 goto gotname; 53 goto gotname;
60 for (cp = p->s_aliases; *cp; cp++) 54 for (cp = p->s_aliases; *cp; cp++)
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c
index c34790737b..4b063760d2 100644
--- a/src/lib/libc/net/getservbyport.c
+++ b/src/lib/libc/net/getservbyport.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getservbyport.c,v 1.3 1997/07/09 01:08:35 millert Exp $";
38static char sccsid[] = "@(#)getservbyport.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <netdb.h> 38#include <netdb.h>
@@ -54,7 +48,7 @@ getservbyport(port, proto)
54 register struct servent *p; 48 register struct servent *p;
55 49
56 setservent(_serv_stayopen); 50 setservent(_serv_stayopen);
57 while (p = getservent()) { 51 while ((p = getservent())) {
58 if (p->s_port != port) 52 if (p->s_port != port)
59 continue; 53 continue;
60 if (proto == 0 || strcmp(p->s_proto, proto) == 0) 54 if (proto == 0 || strcmp(p->s_proto, proto) == 0)
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3
index 9e0656be00..d1684c28c4 100644
--- a/src/lib/libc/net/getservent.3
+++ b/src/lib/libc/net/getservent.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: getservent.3,v 1.3 1995/02/25 06:20:38 cgd Exp $ 1.\" $OpenBSD: getservent.3,v 1.3 1996/08/30 02:00:11 millert Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)getservent.3 8.3 (Berkeley) 1/12/94
35.\"
36.Dd January 12, 1994 34.Dd January 12, 1994
37.Dt GETSERVENT 3 35.Dt GETSERVENT 3
38.Os BSD 4.2 36.Os BSD 4.2
@@ -50,7 +48,7 @@
50.Ft struct servent * 48.Ft struct servent *
51.Fn getservbyname "char *name" "char *proto" 49.Fn getservbyname "char *name" "char *proto"
52.Ft struct servent * 50.Ft struct servent *
53.Fn getservbyport "int port" proto 51.Fn getservbyport "int port" "char *proto"
54.Ft void 52.Ft void
55.Fn setservent "int stayopen" 53.Fn setservent "int stayopen"
56.Ft void 54.Ft void
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c
index 316891450e..7d8cb6d8ca 100644
--- a/src/lib/libc/net/getservent.c
+++ b/src/lib/libc/net/getservent.c
@@ -1,5 +1,3 @@
1/* $NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: getservent.c,v 1.4 1998/03/16 05:07:00 millert Exp $";
38static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
@@ -80,20 +74,24 @@ endservent()
80struct servent * 74struct servent *
81getservent() 75getservent()
82{ 76{
83 char *p; 77 char *p, *cp, **q;
84 register char *cp, **q; 78 size_t len;
85 79
86 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) 80 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
87 return (NULL); 81 return (NULL);
88again: 82again:
89 if ((p = fgets(line, BUFSIZ, servf)) == NULL) 83 if ((p = fgetln(servf, &len)) == NULL)
90 return (NULL); 84 return (NULL);
91 if (*p == '#') 85 if (p[len-1] == '\n')
86 len--;
87 if (len >= sizeof(line) || len == 0)
92 goto again; 88 goto again;
93 cp = strpbrk(p, "#\n"); 89 p = memcpy(line, p, len);
94 if (cp == NULL) 90 line[len] = '\0';
91 if (*p == '#')
95 goto again; 92 goto again;
96 *cp = '\0'; 93 if ((cp = strchr(p, '#')) != NULL)
94 *cp = '\0';
97 serv.s_name = p; 95 serv.s_name = p;
98 p = strpbrk(p, " \t"); 96 p = strpbrk(p, " \t");
99 if (p == NULL) 97 if (p == NULL)
@@ -105,7 +103,7 @@ again:
105 if (cp == NULL) 103 if (cp == NULL)
106 goto again; 104 goto again;
107 *cp++ = '\0'; 105 *cp++ = '\0';
108 serv.s_port = htons((u_short)atoi(p)); 106 serv.s_port = htons((in_port_t)atoi(p));
109 serv.s_proto = cp; 107 serv.s_proto = cp;
110 q = serv.s_aliases = serv_aliases; 108 q = serv.s_aliases = serv_aliases;
111 cp = strpbrk(cp, " \t"); 109 cp = strpbrk(cp, " \t");
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c
index 41adbf1055..737bb115a7 100644
--- a/src/lib/libc/net/herror.c
+++ b/src/lib/libc/net/herror.c
@@ -1,9 +1,11 @@
1/* $NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $ */ 1/* $OpenBSD: herror.c,v 1.4 1997/03/13 19:07:28 downsj Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1987, 1993
5 * -
4 * Copyright (c) 1987, 1993 6 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,26 +58,27 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: herror.c,v 4.9.1.1 1993/05/02 23:14:35 vixie Rel "; 61static char rcsid[] = "$From: herror.c,v 8.3 1996/08/05 08:31:35 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: herror.c,v 1.4 1997/03/13 19:07:28 downsj Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
65#include <sys/types.h> 67#include <sys/types.h>
68#include <sys/param.h>
66#include <sys/uio.h> 69#include <sys/uio.h>
67#include <netdb.h> 70#include <netdb.h>
68#include <unistd.h> 71#include <unistd.h>
69#include <string.h> 72#include <string.h>
70 73
71char *h_errlist[] = { 74const char *h_errlist[] = {
72 "Error 0", 75 "Resolver Error 0 (no error)",
73 "Unknown host", /* 1 HOST_NOT_FOUND */ 76 "Unknown host", /* 1 HOST_NOT_FOUND */
74 "Host name lookup failure", /* 2 TRY_AGAIN */ 77 "Host name lookup failure", /* 2 TRY_AGAIN */
75 "Unknown server error", /* 3 NO_RECOVERY */ 78 "Unknown server error", /* 3 NO_RECOVERY */
76 "No address associated with name", /* 4 NO_ADDRESS */ 79 "No address associated with name", /* 4 NO_ADDRESS */
77}; 80};
78int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) }; 81int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
79 82
80extern int h_errno; 83extern int h_errno;
81 84
@@ -98,8 +101,7 @@ herror(s)
98 v->iov_len = 2; 101 v->iov_len = 2;
99 v++; 102 v++;
100 } 103 }
101 v->iov_base = (u_int)h_errno < h_nerr ? 104 v->iov_base = (char *)hstrerror(h_errno);
102 h_errlist[h_errno] : "Unknown error";
103 v->iov_len = strlen(v->iov_base); 105 v->iov_len = strlen(v->iov_base);
104 v++; 106 v++;
105 v->iov_base = "\n"; 107 v->iov_base = "\n";
@@ -107,9 +109,13 @@ herror(s)
107 writev(STDERR_FILENO, iov, (v - iov) + 1); 109 writev(STDERR_FILENO, iov, (v - iov) + 1);
108} 110}
109 111
110char * 112const char *
111hstrerror(err) 113hstrerror(err)
112 int err; 114 int err;
113{ 115{
114 return (u_int)err < h_nerr ? h_errlist[err] : "Unknown resolver error"; 116 if (err < 0)
117 return ("Resolver internal error");
118 else if (err < h_nerr)
119 return (h_errlist[err]);
120 return ("Unknown resolver error");
115} 121}
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c
index ac85cb0fd7..73b7432731 100644
--- a/src/lib/libc/net/htonl.c
+++ b/src/lib/libc/net/htonl.c
@@ -1,29 +1,25 @@
1/* $NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $ */
2
3/* 1/*
4 * Written by J.T. Conklin <jtc@netbsd.org>. 2 * Written by J.T. Conklin <jtc@netbsd.org>.
5 * Public domain. 3 * Public domain.
6 */ 4 */
7 5
8#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $"; 7static char *rcsid = "$OpenBSD: htonl.c,v 1.4 1996/12/12 03:19:55 tholo Exp $";
10#endif 8#endif /* LIBC_SCCS and not lint */
11 9
12#include <sys/types.h> 10#include <sys/types.h>
13#include <machine/endian.h> 11#include <machine/endian.h>
14 12
15#undef htonl 13#undef htonl
16 14
17unsigned long 15u_int32_t
18htonl(x) 16htonl(x)
19 unsigned long x; 17 u_int32_t x;
20{ 18{
21 u_int32_t y = x;
22
23#if BYTE_ORDER == LITTLE_ENDIAN 19#if BYTE_ORDER == LITTLE_ENDIAN
24 u_char *s = (u_char *)&y; 20 u_char *s = (u_char *)&x;
25 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; 21 return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
26#else 22#else
27 return y; 23 return x;
28#endif 24#endif
29} 25}
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c
index b2500a51d1..47cf25952d 100644
--- a/src/lib/libc/net/htons.c
+++ b/src/lib/libc/net/htons.c
@@ -1,26 +1,28 @@
1/* $NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $ */
2
3/* 1/*
4 * Written by J.T. Conklin <jtc@netbsd.org>. 2 * Written by J.T. Conklin <jtc@netbsd.org>.
5 * Public domain. 3 * Public domain.
6 */ 4 */
7 5
8#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $"; 7static char *rcsid = "$OpenBSD: htons.c,v 1.6 1997/07/25 20:30:07 mickey Exp $";
10#endif 8#endif /* LIBC_SCCS and not lint */
11 9
12#include <sys/types.h> 10#include <sys/types.h>
13#include <machine/endian.h> 11#include <machine/endian.h>
14 12
15#undef htons 13#undef htons
16 14
17unsigned short 15u_int16_t
16#ifdef __STDC__
17htons(u_int16_t x)
18#else
18htons(x) 19htons(x)
19 unsigned short x; 20 u_int16_t x;
21#endif
20{ 22{
21#if BYTE_ORDER == LITTLE_ENDIAN 23#if BYTE_ORDER == LITTLE_ENDIAN
22 u_char *s = (u_char *) &x; 24 u_char *s = (u_char *) &x;
23 return s[0] << 8 | s[1]; 25 return (u_int16_t)(s[0] << 8 | s[1]);
24#else 26#else
25 return x; 27 return x;
26#endif 28#endif
diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3
index 49bac97e96..2fb86cd927 100644
--- a/src/lib/libc/net/inet.3
+++ b/src/lib/libc/net/inet.3
@@ -1,4 +1,5 @@
1.\" $NetBSD: inet.3,v 1.4 1995/02/27 09:45:26 chopps Exp $ 1.\" $OpenBSD: inet.3,v 1.4 1997/06/23 04:01:11 millert Exp $
2.\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $
2.\" 3.\"
3.\" Copyright (c) 1983, 1990, 1991, 1993 4.\" Copyright (c) 1983, 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 5.\" The Regents of the University of California. All rights reserved.
@@ -33,36 +34,42 @@
33.\" 34.\"
34.\" @(#)inet.3 8.1 (Berkeley) 6/4/93 35.\" @(#)inet.3 8.1 (Berkeley) 6/4/93
35.\" 36.\"
36.Dd June 4, 1993 37.Dd June 18, 1997
37.Dt INET 3 38.Dt INET 3
38.Os BSD 4.2 39.Os BSD 4.2
39.Sh NAME 40.Sh NAME
40.Nm inet_aton ,
41.Nm inet_addr , 41.Nm inet_addr ,
42.Nm inet_aton ,
43.Nm inet_lnaof ,
44.Nm inet_makeaddr ,
45.Nm inet_netof ,
42.Nm inet_network , 46.Nm inet_network ,
43.Nm inet_ntoa , 47.Nm inet_ntoa ,
44.Nm inet_makeaddr , 48.Nm inet_ntop ,
45.Nm inet_lnaof , 49.Nm inet_pton
46.Nm inet_netof
47.Nd Internet address manipulation routines 50.Nd Internet address manipulation routines
48.Sh SYNOPSIS 51.Sh SYNOPSIS
49.Fd #include <sys/socket.h> 52.Fd #include <sys/socket.h>
50.Fd #include <netinet/in.h> 53.Fd #include <netinet/in.h>
51.Fd #include <arpa/inet.h> 54.Fd #include <arpa/inet.h>
52.Ft int 55.Ft in_addr_t
53.Fn inet_aton "const char *cp" "struct in_addr *pin"
54.Ft unsigned long
55.Fn inet_addr "const char *cp" 56.Fn inet_addr "const char *cp"
56.Ft unsigned long 57.Ft int
58.Fn inet_aton "const char *cp" "struct in_addr *addr"
59.Ft in_addr_t
60.Fn inet_lnaof "struct in_addr in"
61.Ft struct in_addr
62.Fn inet_makeaddr "unsigned long net" "unsigned long lna"
63.Ft in_addr_t
64.Fn inet_netof "struct in_addr in"
65.Ft in_addr_t
57.Fn inet_network "const char *cp" 66.Fn inet_network "const char *cp"
58.Ft char * 67.Ft char *
59.Fn inet_ntoa "struct in_addr in" 68.Fn inet_ntoa "struct in_addr in"
60.Ft struct in_addr 69.Ft const char *
61.Fn inet_makeaddr "int net" "int lna" 70.Fn inet_ntop "int af" "const void *src" "char *dst" "size_t size"
62.Ft unsigned long 71.Ft int
63.Fn inet_lnaof "struct in_addr in" 72.Fn inet_pton "int af" "const char *src" "void *dst"
64.Ft unsigned long
65.Fn inet_netof "struct in_addr in"
66.Sh DESCRIPTION 73.Sh DESCRIPTION
67The routines 74The routines
68.Fn inet_aton , 75.Fn inet_aton ,
@@ -74,6 +81,17 @@ numbers expressed in the Internet standard
74.Ql \&. 81.Ql \&.
75notation. 82notation.
76The 83The
84.Fn inet_pton
85function converts a presentation format address (that is, printable form
86as held in a character string) to network format (usually a
87.Ft struct in_addr
88or some other internal binary representation, in network byte order). It
89returns 1 if the address was valid for the specified address family, or
900 if the address wasn't parseable in the specified address family, or -1
91if some system error occurred (in which case
92.Va errno
93will have been set). This function is presently valid for AF_INET and
94AF_INET6. The
77.Fn inet_aton 95.Fn inet_aton
78routine interprets the specified character string as an Internet address, 96routine interprets the specified character string as an Internet address,
79placing the address into the structure provided. 97placing the address into the structure provided.
@@ -86,6 +104,16 @@ and
86functions return numbers suitable for use 104functions return numbers suitable for use
87as Internet addresses and Internet network 105as Internet addresses and Internet network
88numbers, respectively. 106numbers, respectively.
107.Pp
108The function
109.Fn inet_ntop
110converts an address from network format (usually a
111.Ft struct in_addr
112or some other binary form, in network byte order) to presentation format
113(suitable for external display purposes). It returns NULL if a system
114error occurs (in which case,
115.Va errno
116will have been set), or it returns a pointer to the destination string.
89The routine 117The routine
90.Fn inet_ntoa 118.Fn inet_ntoa
91takes an Internet address and returns an 119takes an Internet address and returns an
@@ -108,7 +136,7 @@ All Internet addresses are returned in network
108order (bytes ordered from left to right). 136order (bytes ordered from left to right).
109All network numbers and local address parts are 137All network numbers and local address parts are
110returned as machine format integer values. 138returned as machine format integer values.
111.Sh INTERNET ADDRESSES 139.Sh INTERNET ADDRESSES (IP VERSION 4)
112Values specified using the 140Values specified using the
113.Ql \&. 141.Ql \&.
114notation take one 142notation take one
@@ -124,15 +152,14 @@ When four parts are specified, each is interpreted
124as a byte of data and assigned, from left to right, 152as a byte of data and assigned, from left to right,
125to the four bytes of an Internet address. Note 153to the four bytes of an Internet address. Note
126that when an Internet address is viewed as a 32-bit 154that when an Internet address is viewed as a 32-bit
127integer quantity on the 155integer quantity on a system that uses little-endian
128.Tn VAX 156byte order (such as the
129the bytes referred to 157.Tn Intel 386, 486
130above appear as 158and
159.Tn Pentium
160processors) the bytes referred to above appear as
131.Dq Li d.c.b.a . 161.Dq Li d.c.b.a .
132That is, 162That is, little-endian bytes are ordered from right to left.
133.Tn VAX
134bytes are
135ordered from right to left.
136.Pp 163.Pp
137When a three part address is specified, the last 164When a three part address is specified, the last
138part is interpreted as a 16-bit quantity and placed 165part is interpreted as a 16-bit quantity and placed
@@ -161,6 +188,68 @@ may be decimal, octal, or hexadecimal, as specified
161in the C language (i.e., a leading 0x or 0X implies 188in the C language (i.e., a leading 0x or 0X implies
162hexadecimal; otherwise, a leading 0 implies octal; 189hexadecimal; otherwise, a leading 0 implies octal;
163otherwise, the number is interpreted as decimal). 190otherwise, the number is interpreted as decimal).
191.Sh INTERNET ADDRESSES (IP VERSION 6)
192The presentation format of an IPv6 address is given in [RFC1884 2.2]:
193.Pp
194There are three conventional forms for representing IPv6 addresses as
195text strings:
196.Bl -enum
197.It
198The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
199hexadecimal values of the eight 16-bit pieces of the address.
200Examples:
201.Bd -literal -offset indent
202FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
2031080:0:0:0:8:800:200C:417A
204.Ed
205.Pp
206Note that it is not necessary to write the leading zeros in an
207individual field, but there must be at least one numeral in
208every field (except for the case described in 2.).
209.It
210Due to the method of allocating certain styles of IPv6
211addresses, it will be common for addresses to contain long
212strings of zero bits. In order to make writing addresses
213.Pp
214containing zero bits easier a special syntax is available to
215compress the zeros. The use of ``::'' indicates multiple groups
216of 16-bits of zeros. The ``::'' can only appear once in an
217address. The ``::'' can also be used to compress the leading
218and/or trailing zeros in an address.
219.Pp
220For example the following addresses:
221.Bd -literal -offset indent
2221080:0:0:0:8:800:200C:417A a unicast address
223FF01:0:0:0:0:0:0:43 a multicast address
2240:0:0:0:0:0:0:1 the loopback address
2250:0:0:0:0:0:0:0 the unspecified addresses
226.Ed
227.Pp
228may be represented as:
229.Bd -literal -offset indent
2301080::8:800:200C:417A a unicast address
231FF01::43 a multicast address
232::1 the loopback address
233:: the unspecified addresses
234.Ed
235.It
236An alternative form that is sometimes more convenient when
237dealing with a mixed environment of IPv4 and IPv6 nodes is
238x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values
239of the six high-order 16-bit pieces of the address, and the 'd's
240are the decimal values of the four low-order 8-bit pieces of the
241address (standard IPv4 representation). Examples:
242.Bd -literal -offset indent
2430:0:0:0:0:0:13.1.68.3
2440:0:0:0:0:FFFF:129.144.52.38
245.Ed
246.Pp
247or in compressed form:
248.Bd -literal -offset indent
249::13.1.68.3
250::FFFF:129.144.52.38
251.Ed
252.El
164.Sh DIAGNOSTICS 253.Sh DIAGNOSTICS
165The constant 254The constant
166.Dv INADDR_NONE 255.Dv INADDR_NONE
@@ -170,14 +259,44 @@ and
170.Fn inet_network 259.Fn inet_network
171for malformed requests. 260for malformed requests.
172.Sh SEE ALSO 261.Sh SEE ALSO
262.Xr byteorder 3 ,
173.Xr gethostbyname 3 , 263.Xr gethostbyname 3 ,
174.Xr getnetent 3 , 264.Xr getnetent 3 ,
265.Xr inet_net 3 ,
175.Xr hosts 5 , 266.Xr hosts 5 ,
176.Xr networks 5 , 267.Xr networks 5
268.Sh STANDARDS
269The
270.Nm inet_ntop
271and
272.Nm inet_pton
273functions conforms to the IETF IPng BSD API and address formatting
274specifications. Note that
275.Nm inet_pton
276does not accept 1-, 2-, or 3-part dotted addresses; all four parts
277must be specified. This is a narrower input set than that accepted by
278.Nm inet_aton .
177.Sh HISTORY 279.Sh HISTORY
178These 280The
179functions appeared in 281.Nm inet_addr ,
282.Nm inet_network ,
283.Nm inet_makeaddr ,
284.Nm inet_lnaof
285and
286.Nm inet_netof
287functions appeared in
180.Bx 4.2 . 288.Bx 4.2 .
289The
290.Nm inet_aton
291and
292.Nm inet_ntoa
293functions appeared in
294.Bx 4.3 .
295The
296.Nm inet_pton
297and
298.Nm inet_ntop
299functions appeared in BIND 4.9.4.
181.Sh BUGS 300.Sh BUGS
182The value 301The value
183.Dv INADDR_NONE 302.Dv INADDR_NONE
@@ -187,11 +306,14 @@ cannot return that value without indicating failure.
187The newer 306The newer
188.Fn inet_aton 307.Fn inet_aton
189function does not share this problem. 308function does not share this problem.
309.Pp
190The problem of host byte ordering versus network byte ordering is 310The problem of host byte ordering versus network byte ordering is
191confusing. 311confusing.
312.Pp
192The string returned by 313The string returned by
193.Fn inet_ntoa 314.Fn inet_ntoa
194resides in a static memory area. 315resides in a static memory area.
195.Pp 316.Pp
196Inet_addr should return a 317.Fn inet_addr
197.Fa struct in_addr . 318should return a
319.Fa "struct in_addr" .
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c
index b5b9d8302f..5e4dcdafb2 100644
--- a/src/lib/libc/net/inet_addr.c
+++ b/src/lib/libc/net/inet_addr.c
@@ -1,9 +1,11 @@
1/* $NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $ */ 1/* $OpenBSD: inet_addr.c,v 1.5 1997/04/05 21:13:10 millert Exp $ */
2 2
3/* 3/*
4 * ++Copyright++ 1983, 1990, 1993
5 * -
4 * Copyright (c) 1983, 1990, 1993 6 * Copyright (c) 1983, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -31,16 +33,38 @@
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE. 35 * SUCH DAMAGE.
36 * -
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 * -
55 * --Copyright--
34 */ 56 */
35 57
36#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 59#if 0
38static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 60static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
61static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $";
39#else 62#else
40static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.5 1997/04/05 21:13:10 millert Exp $";
41#endif 64#endif
42#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
43 66
67#include <sys/types.h>
44#include <sys/param.h> 68#include <sys/param.h>
45#include <netinet/in.h> 69#include <netinet/in.h>
46#include <arpa/inet.h> 70#include <arpa/inet.h>
@@ -50,7 +74,7 @@ static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"
50 * Ascii internet address interpretation routine. 74 * Ascii internet address interpretation routine.
51 * The value returned is in network order. 75 * The value returned is in network order.
52 */ 76 */
53u_long 77in_addr_t
54inet_addr(cp) 78inet_addr(cp)
55 register const char *cp; 79 register const char *cp;
56{ 80{
@@ -73,56 +97,58 @@ inet_aton(cp, addr)
73 register const char *cp; 97 register const char *cp;
74 struct in_addr *addr; 98 struct in_addr *addr;
75{ 99{
76 register u_long val; 100 register in_addr_t val;
77 register int base, n; 101 register int base, n;
78 register char c; 102 register char c;
79 u_int parts[4]; 103 u_int parts[4];
80 register u_int *pp = parts; 104 register u_int *pp = parts;
81 105
106 c = *cp;
82 for (;;) { 107 for (;;) {
83 /* 108 /*
84 * Collect number up to ``.''. 109 * Collect number up to ``.''.
85 * Values are specified as for C: 110 * Values are specified as for C:
86 * 0x=hex, 0=octal, other=decimal. 111 * 0x=hex, 0=octal, isdigit=decimal.
87 */ 112 */
113 if (!isdigit(c))
114 return (0);
88 val = 0; base = 10; 115 val = 0; base = 10;
89 if (*cp == '0') { 116 if (c == '0') {
90 if (*++cp == 'x' || *cp == 'X') 117 c = *++cp;
91 base = 16, cp++; 118 if (c == 'x' || c == 'X')
119 base = 16, c = *++cp;
92 else 120 else
93 base = 8; 121 base = 8;
94 } 122 }
95 while ((c = *cp) != '\0') { 123 for (;;) {
96 if (isascii(c) && isdigit(c)) { 124 if (isascii(c) && isdigit(c)) {
97 val = (val * base) + (c - '0'); 125 val = (val * base) + (c - '0');
98 cp++; 126 c = *++cp;
99 continue; 127 } else if (base == 16 && isascii(c) && isxdigit(c)) {
100 } 128 val = (val << 4) |
101 if (base == 16 && isascii(c) && isxdigit(c)) {
102 val = (val << 4) +
103 (c + 10 - (islower(c) ? 'a' : 'A')); 129 (c + 10 - (islower(c) ? 'a' : 'A'));
104 cp++; 130 c = *++cp;
105 continue; 131 } else
106 } 132 break;
107 break;
108 } 133 }
109 if (*cp == '.') { 134 if (c == '.') {
110 /* 135 /*
111 * Internet format: 136 * Internet format:
112 * a.b.c.d 137 * a.b.c.d
113 * a.b.c (with c treated as 16-bits) 138 * a.b.c (with c treated as 16 bits)
114 * a.b (with b treated as 24 bits) 139 * a.b (with b treated as 24 bits)
115 */ 140 */
116 if (pp >= parts + 3 || val > 0xff) 141 if (pp >= parts + 3)
117 return (0); 142 return (0);
118 *pp++ = val, cp++; 143 *pp++ = val;
144 c = *++cp;
119 } else 145 } else
120 break; 146 break;
121 } 147 }
122 /* 148 /*
123 * Check for trailing characters. 149 * Check for trailing characters.
124 */ 150 */
125 if (*cp && (!isascii(*cp) || !isspace(*cp))) 151 if (c != '\0' && (!isascii(c) || !isspace(c)))
126 return (0); 152 return (0);
127 /* 153 /*
128 * Concoct the address according to 154 * Concoct the address according to
@@ -131,6 +157,9 @@ inet_aton(cp, addr)
131 n = pp - parts + 1; 157 n = pp - parts + 1;
132 switch (n) { 158 switch (n) {
133 159
160 case 0:
161 return (0); /* initial nondigit */
162
134 case 1: /* a -- 32 bits */ 163 case 1: /* a -- 32 bits */
135 break; 164 break;
136 165
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c
index ce1257bf68..6aed18699b 100644
--- a/src/lib/libc/net/inet_lnaof.c
+++ b/src/lib/libc/net/inet_lnaof.c
@@ -1,5 +1,3 @@
1/* $NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: inet_lnaof.c,v 1.3 1997/04/05 21:13:11 millert Exp $";
38static char sccsid[] = "@(#)inet_lnaof.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/param.h> 38#include <sys/param.h>
@@ -50,11 +44,11 @@ static char rcsid[] = "$NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $
50 * internet address; handles class a/b/c network 44 * internet address; handles class a/b/c network
51 * number formats. 45 * number formats.
52 */ 46 */
53u_long 47in_addr_t
54inet_lnaof(in) 48inet_lnaof(in)
55 struct in_addr in; 49 struct in_addr in;
56{ 50{
57 register u_long i = ntohl(in.s_addr); 51 register in_addr_t i = ntohl(in.s_addr);
58 52
59 if (IN_CLASSA(i)) 53 if (IN_CLASSA(i))
60 return ((i)&IN_CLASSA_HOST); 54 return ((i)&IN_CLASSA_HOST);
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c
index 84d366e03a..196a589e4c 100644
--- a/src/lib/libc/net/inet_makeaddr.c
+++ b/src/lib/libc/net/inet_makeaddr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: inet_makeaddr.c,v 1.3 1997/04/05 21:13:12 millert Exp $";
38static char sccsid[] = "@(#)inet_makeaddr.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/param.h> 38#include <sys/param.h>
@@ -51,9 +45,9 @@ static char rcsid[] = "$NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Ex
51 */ 45 */
52struct in_addr 46struct in_addr
53inet_makeaddr(net, host) 47inet_makeaddr(net, host)
54 u_long net, host; 48 in_addr_t net, host;
55{ 49{
56 u_long addr; 50 in_addr_t addr;
57 51
58 if (net < 128) 52 if (net < 128)
59 addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); 53 addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
diff --git a/src/lib/libc/net/inet_net.3 b/src/lib/libc/net/inet_net.3
new file mode 100644
index 0000000000..1a42aff6ea
--- /dev/null
+++ b/src/lib/libc/net/inet_net.3
@@ -0,0 +1,149 @@
1.\" $OpenBSD: inet_net.3,v 1.1 1997/06/23 03:37:26 millert Exp $
2.\" $NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $
3.\"
4.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Luke Mewburn.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\" notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\" notice, this list of conditions and the following disclaimer in the
17.\" documentation and/or other materials provided with the distribution.
18.\" 3. All advertising materials mentioning features or use of this software
19.\" must display the following acknowledgement:
20.\" This product includes software developed by the NetBSD
21.\" Foundation, Inc. and its contributors.
22.\" 4. Neither the name of The NetBSD Foundation nor the names of its
23.\" contributors may be used to endorse or promote products derived
24.\" from this software without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36.\" POSSIBILITY OF SUCH DAMAGE.
37.\"
38.Dd June 18, 1997
39.Dt INET_NET 3
40.Os
41.Sh NAME
42.Nm inet_net_ntop ,
43.Nm inet_net_pton
44.Nd Internet network number manipulation routines
45.Sh SYNOPSIS
46.Fd #include <sys/socket.h>
47.Fd #include <netinet/in.h>
48.Fd #include <arpa/inet.h>
49.Ft char *
50.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
51.Ft int
52.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
53.Sh DESCRIPTION
54The
55.Fn inet_net_ntop
56function converts an Internet network number from network format (usually a
57.Ft struct in_addr
58or some other binary form, in network byte order) to CIDR presentation format
59(suitable for external display purposes).
60.Fa bits
61is the number of bits in
62.Fa src
63that are the network number.
64It returns NULL if a system error occurs (in which case,
65.Va errno
66will have been set), or it returns a pointer to the destination string.
67.Pp
68The
69.Fn inet_net_pton
70function converts a presentation format Internet network number (that is,
71printable form as held in a character string) to network format (usually a
72.Ft struct in_addr
73or some other internal binary representation, in network byte order).
74It returns the number of bits (either computed based on the class, or
75specified with /CIDR), or -1 if a failure occurred
76(in which case
77.Va errno
78will have been set.
79It will be set to
80.Er ENOENT
81if the Internet network number was not valid).
82.Pp
83The currently supported value for
84.Fa af
85is: AF_INET.
86.Fa size
87is the size of the result buffer
88.Fa dst .
89.Pp
90.Sh NETWORK NUMBERS (IP VERSION 4)
91Internet network numbers may be specified in one of the following forms:
92.Bd -literal -offset indent
93a.b.c.d/bits
94a.b.c.d
95a.b.c
96a.b
97a
98.Ed
99.Pp
100When four parts are specified, each is interpreted
101as a byte of data and assigned, from left to right,
102to the four bytes of an Internet network number. Note
103that when an Internet network number is viewed as a 32-bit
104integer quantity on a system that uses little-endian
105byte order (such as the
106.Tn Intel 386, 486
107and
108.Tn Pentium
109processors) the bytes referred to above appear as
110.Dq Li d.c.b.a .
111That is, little-endian bytes are ordered from right to left.
112.Pp
113When a three part number is specified, the last
114part is interpreted as a 16-bit quantity and placed
115in the right-most two bytes of the Internet network number.
116This makes the three part number format convenient
117for specifying Class B network numbers as
118.Dq Li 128.net.host .
119.Pp
120When a two part number is supplied, the last part
121is interpreted as a 24-bit quantity and placed in
122the right most three bytes of the Internet network number.
123This makes the two part number format convenient
124for specifying Class A network numbers as
125.Dq Li net.host .
126.Pp
127When only one part is given, the value is stored
128directly in the Internet network number without any byte
129rearrangement.
130.Pp
131All numbers supplied as
132.Dq parts
133in a
134.Ql \&.
135notation
136may be decimal, octal, or hexadecimal, as specified
137in the C language (i.e., a leading 0x or 0X implies
138hexadecimal; otherwise, a leading 0 implies octal;
139otherwise, the number is interpreted as decimal).
140.Sh SEE ALSO
141.Xr byteorder 3 ,
142.Xr inet 3 ,
143.Xr networks 5
144.Sh HISTORY
145The
146.Nm inet_net_ntop
147and
148.Nm inet_net_pton
149functions first appeared in BIND 4.9.4.
diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c
new file mode 100644
index 0000000000..943ec44550
--- /dev/null
+++ b/src/lib/libc/net/inet_net_ntop.c
@@ -0,0 +1,139 @@
1/* $OpenBSD: inet_net_ntop.c,v 1.1 1997/03/13 19:07:30 downsj Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21#if 0
22static const char rcsid[] = "$From: inet_net_ntop.c,v 8.2 1996/08/08 06:54:44 vixie Exp $";
23#else
24static const char rcsid[] = "$OpenBSD: inet_net_ntop.c,v 1.1 1997/03/13 19:07:30 downsj Exp $";
25#endif
26#endif
27
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33#include <errno.h>
34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37
38static char * inet_net_ntop_ipv4 __P((const u_char *src, int bits,
39 char *dst, size_t size));
40
41/*
42 * char *
43 * inet_net_ntop(af, src, bits, dst, size)
44 * convert network number from network to presentation format.
45 * generates CIDR style result always.
46 * return:
47 * pointer to dst, or NULL if an error occurred (check errno).
48 * author:
49 * Paul Vixie (ISC), July 1996
50 */
51char *
52inet_net_ntop(af, src, bits, dst, size)
53 int af;
54 const void *src;
55 int bits;
56 char *dst;
57 size_t size;
58{
59 switch (af) {
60 case AF_INET:
61 return (inet_net_ntop_ipv4(src, bits, dst, size));
62 default:
63 errno = EAFNOSUPPORT;
64 return (NULL);
65 }
66}
67
68/*
69 * static char *
70 * inet_net_ntop_ipv4(src, bits, dst, size)
71 * convert IPv4 network number from network to presentation format.
72 * generates CIDR style result always.
73 * return:
74 * pointer to dst, or NULL if an error occurred (check errno).
75 * note:
76 * network byte order assumed. this means 192.5.5.240/28 has
77 * 0x11110000 in its fourth octet.
78 * author:
79 * Paul Vixie (ISC), July 1996
80 */
81static char *
82inet_net_ntop_ipv4(src, bits, dst, size)
83 const u_char *src;
84 int bits;
85 char *dst;
86 size_t size;
87{
88 char *odst = dst;
89 char *t;
90 u_int m;
91 int b;
92
93 if (bits < 0 || bits > 32) {
94 errno = EINVAL;
95 return (NULL);
96 }
97 if (bits == 0) {
98 if (size < sizeof "0")
99 goto emsgsize;
100 *dst++ = '0';
101 *dst = '\0';
102 }
103
104 /* Format whole octets. */
105 for (b = bits / 8; b > 0; b--) {
106 if (size < sizeof "255.")
107 goto emsgsize;
108 t = dst;
109 dst += sprintf(dst, "%u", *src++);
110 if (b > 1) {
111 *dst++ = '.';
112 *dst = '\0';
113 }
114 size -= (size_t)(dst - t);
115 }
116
117 /* Format partial octet. */
118 b = bits % 8;
119 if (b > 0) {
120 if (size < sizeof ".255")
121 goto emsgsize;
122 t = dst;
123 if (dst != odst)
124 *dst++ = '.';
125 m = ((1 << b) - 1) << (8 - b);
126 dst += sprintf(dst, "%u", *src & m);
127 size -= (size_t)(dst - t);
128 }
129
130 /* Format CIDR /width. */
131 if (size < sizeof "/32")
132 goto emsgsize;
133 dst += sprintf(dst, "/%u", bits);
134 return (odst);
135
136 emsgsize:
137 errno = EMSGSIZE;
138 return (NULL);
139}
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c
new file mode 100644
index 0000000000..b529e83664
--- /dev/null
+++ b/src/lib/libc/net/inet_net_pton.c
@@ -0,0 +1,207 @@
1/* $OpenBSD: inet_net_pton.c,v 1.1 1997/03/13 19:07:30 downsj Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21#if 0
22static const char rcsid[] = "$From: inet_net_pton.c,v 8.3 1996/11/11 06:36:52 vixie Exp $";
23#else
24static const char rcsid[] = "$OpenBSD: inet_net_pton.c,v 1.1 1997/03/13 19:07:30 downsj Exp $";
25#endif
26#endif
27
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33#include <assert.h>
34#include <ctype.h>
35#include <errno.h>
36#include <stdio.h>
37#include <string.h>
38#include <stdlib.h>
39
40static int inet_net_pton_ipv4 __P((const char *src, u_char *dst,
41 size_t size));
42
43/*
44 * static int
45 * inet_net_pton(af, src, dst, size)
46 * convert network number from presentation to network format.
47 * accepts hex octets, hex strings, decimal octets, and /CIDR.
48 * "size" is in bytes and describes "dst".
49 * return:
50 * number of bits, either imputed classfully or specified with /CIDR,
51 * or -1 if some failure occurred (check errno). ENOENT means it was
52 * not a valid network specification.
53 * author:
54 * Paul Vixie (ISC), June 1996
55 */
56int
57inet_net_pton(af, src, dst, size)
58 int af;
59 const char *src;
60 void *dst;
61 size_t size;
62{
63 switch (af) {
64 case AF_INET:
65 return (inet_net_pton_ipv4(src, dst, size));
66 default:
67 errno = EAFNOSUPPORT;
68 return (-1);
69 }
70}
71
72/*
73 * static int
74 * inet_net_pton_ipv4(src, dst, size)
75 * convert IPv4 network number from presentation to network format.
76 * accepts hex octets, hex strings, decimal octets, and /CIDR.
77 * "size" is in bytes and describes "dst".
78 * return:
79 * number of bits, either imputed classfully or specified with /CIDR,
80 * or -1 if some failure occurred (check errno). ENOENT means it was
81 * not an IPv4 network specification.
82 * note:
83 * network byte order assumed. this means 192.5.5.240/28 has
84 * 0x11110000 in its fourth octet.
85 * author:
86 * Paul Vixie (ISC), June 1996
87 */
88static int
89inet_net_pton_ipv4(src, dst, size)
90 const char *src;
91 u_char *dst;
92 size_t size;
93{
94 static const char
95 xdigits[] = "0123456789abcdef",
96 digits[] = "0123456789";
97 int n, ch, tmp, dirty, bits;
98 const u_char *odst = dst;
99
100 ch = *src++;
101 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
102 && isascii(src[1]) && isxdigit(src[1])) {
103 /* Hexadecimal: Eat nybble string. */
104 if (size <= 0)
105 goto emsgsize;
106 *dst = 0, dirty = 0;
107 src++; /* skip x or X. */
108 while ((ch = *src++) != '\0' &&
109 isascii(ch) && isxdigit(ch)) {
110 if (isupper(ch))
111 ch = tolower(ch);
112 n = strchr(xdigits, ch) - xdigits;
113 assert(n >= 0 && n <= 15);
114 *dst |= n;
115 if (!dirty++)
116 *dst <<= 4;
117 else if (size-- > 0)
118 *++dst = 0, dirty = 0;
119 else
120 goto emsgsize;
121 }
122 if (dirty)
123 size--;
124 } else if (isascii(ch) && isdigit(ch)) {
125 /* Decimal: eat dotted digit string. */
126 for (;;) {
127 tmp = 0;
128 do {
129 n = strchr(digits, ch) - digits;
130 assert(n >= 0 && n <= 9);
131 tmp *= 10;
132 tmp += n;
133 if (tmp > 255)
134 goto enoent;
135 } while ((ch = *src++) != '\0' &&
136 isascii(ch) && isdigit(ch));
137 if (size-- <= 0)
138 goto emsgsize;
139 *dst++ = (u_char) tmp;
140 if (ch == '\0' || ch == '/')
141 break;
142 if (ch != '.')
143 goto enoent;
144 ch = *src++;
145 if (!isascii(ch) || !isdigit(ch))
146 goto enoent;
147 }
148 } else
149 goto enoent;
150
151 bits = -1;
152 if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) {
153 /* CIDR width specifier. Nothing can follow it. */
154 ch = *src++; /* Skip over the /. */
155 bits = 0;
156 do {
157 n = strchr(digits, ch) - digits;
158 assert(n >= 0 && n <= 9);
159 bits *= 10;
160 bits += n;
161 } while ((ch = *src++) != '\0' &&
162 isascii(ch) && isdigit(ch));
163 if (ch != '\0')
164 goto enoent;
165 if (bits > 32)
166 goto emsgsize;
167 }
168
169 /* Firey death and destruction unless we prefetched EOS. */
170 if (ch != '\0')
171 goto enoent;
172
173 /* If nothing was written to the destination, we found no address. */
174 if (dst == odst)
175 goto enoent;
176 /* If no CIDR spec was given, infer width from net class. */
177 if (bits == -1) {
178 if (*odst >= 240) /* Class E */
179 bits = 32;
180 else if (*odst >= 224) /* Class D */
181 bits = 4;
182 else if (*odst >= 192) /* Class C */
183 bits = 24;
184 else if (*odst >= 128) /* Class B */
185 bits = 16;
186 else /* Class A */
187 bits = 8;
188 /* If imputed mask is narrower than specified octets, widen. */
189 if (bits >= 8 && bits < ((dst - odst) * 8))
190 bits = (dst - odst) * 8;
191 }
192 /* Extend network to cover the actual mask. */
193 while (bits > ((dst - odst) * 8)) {
194 if (size-- <= 0)
195 goto emsgsize;
196 *dst++ = '\0';
197 }
198 return (bits);
199
200 enoent:
201 errno = ENOENT;
202 return (-1);
203
204 emsgsize:
205 errno = EMSGSIZE;
206 return (-1);
207}
diff --git a/src/lib/libc/net/inet_neta.c b/src/lib/libc/net/inet_neta.c
new file mode 100644
index 0000000000..ffcddd8d91
--- /dev/null
+++ b/src/lib/libc/net/inet_neta.c
@@ -0,0 +1,83 @@
1/* $OpenBSD: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21#if 0
22static const char rcsid[] = "$Id: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $";
23#else
24static const char rcsid[] = "$OpenBSD: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $";
25#endif
26#endif
27
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33#include <errno.h>
34#include <stdio.h>
35#include <string.h>
36
37/*
38 * char *
39 * inet_neta(src, dst, size)
40 * format an in_addr_t network number into presentation format.
41 * return:
42 * pointer to dst, or NULL if an error occurred (check errno).
43 * note:
44 * format of ``src'' is as for inet_network().
45 * author:
46 * Paul Vixie (ISC), July 1996
47 */
48char *
49inet_neta(src, dst, size)
50 in_addr_t src;
51 char *dst;
52 size_t size;
53{
54 char *odst = dst;
55 char *tp;
56
57 while (src & 0xffffffff) {
58 u_char b = (src & 0xff000000) >> 24;
59
60 src <<= 8;
61 if (b) {
62 if (size < sizeof "255.")
63 goto emsgsize;
64 tp = dst;
65 dst += sprintf(dst, "%u", b);
66 if (src != 0L) {
67 *dst++ = '.';
68 *dst = '\0';
69 }
70 size -= (size_t)(dst - tp);
71 }
72 }
73 if (dst == odst) {
74 if (size < sizeof "0.0.0.0")
75 goto emsgsize;
76 strcpy(dst, "0.0.0.0");
77 }
78 return (odst);
79
80 emsgsize:
81 errno = EMSGSIZE;
82 return (NULL);
83}
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c
index 02f52ca318..f3b9c01697 100644
--- a/src/lib/libc/net/inet_netof.c
+++ b/src/lib/libc/net/inet_netof.c
@@ -1,5 +1,3 @@
1/* $NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: inet_netof.c,v 1.3 1997/04/05 21:13:13 millert Exp $";
38static char sccsid[] = "@(#)inet_netof.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/param.h> 38#include <sys/param.h>
@@ -49,11 +43,11 @@ static char rcsid[] = "$NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $
49 * Return the network number from an internet 43 * Return the network number from an internet
50 * address; handles class a/b/c network #'s. 44 * address; handles class a/b/c network #'s.
51 */ 45 */
52u_long 46in_addr_t
53inet_netof(in) 47inet_netof(in)
54 struct in_addr in; 48 struct in_addr in;
55{ 49{
56 register u_long i = ntohl(in.s_addr); 50 register in_addr_t i = ntohl(in.s_addr);
57 51
58 if (IN_CLASSA(i)) 52 if (IN_CLASSA(i))
59 return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); 53 return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c
index 35105fa75a..8a9a555d62 100644
--- a/src/lib/libc/net/inet_network.c
+++ b/src/lib/libc/net/inet_network.c
@@ -1,5 +1,3 @@
1/* $NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: inet_network.c,v 1.7 1997/07/09 01:08:37 millert Exp $";
38static char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
@@ -51,13 +45,13 @@ static char rcsid[] = "$NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp
51 * The library routines call this routine to interpret 45 * The library routines call this routine to interpret
52 * network numbers. 46 * network numbers.
53 */ 47 */
54u_long 48in_addr_t
55inet_network(cp) 49inet_network(cp)
56 register const char *cp; 50 register const char *cp;
57{ 51{
58 register u_long val, base, n; 52 register in_addr_t val, base, n;
59 register char c; 53 register char c;
60 u_long parts[4], *pp = parts; 54 in_addr_t parts[4], *pp = parts;
61 register int i; 55 register int i;
62 56
63again: 57again:
@@ -66,7 +60,7 @@ again:
66 base = 8, cp++; 60 base = 8, cp++;
67 if (*cp == 'x' || *cp == 'X') 61 if (*cp == 'x' || *cp == 'X')
68 base = 16, cp++; 62 base = 16, cp++;
69 while (c = *cp) { 63 while ((c = *cp)) {
70 if (isdigit(c)) { 64 if (isdigit(c)) {
71 val = (val * base) + (c - '0'); 65 val = (val * base) + (c - '0');
72 cp++; 66 cp++;
@@ -80,7 +74,7 @@ again:
80 break; 74 break;
81 } 75 }
82 if (*cp == '.') { 76 if (*cp == '.') {
83 if (pp >= parts + 4) 77 if (pp >= parts + 3)
84 return (INADDR_NONE); 78 return (INADDR_NONE);
85 *pp++ = val, cp++; 79 *pp++ = val, cp++;
86 goto again; 80 goto again;
@@ -89,11 +83,10 @@ again:
89 return (INADDR_NONE); 83 return (INADDR_NONE);
90 *pp++ = val; 84 *pp++ = val;
91 n = pp - parts; 85 n = pp - parts;
92 if (n > 4) 86 for (val = 0, i = 0; i < 4; i++) {
93 return (INADDR_NONE);
94 for (val = 0, i = 0; i < n; i++) {
95 val <<= 8; 87 val <<= 8;
96 val |= parts[i] & 0xff; 88 if (i < n)
89 val |= parts[i] & 0xff;
97 } 90 }
98 return (val); 91 return (val);
99} 92}
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c
index 2da0ab00ff..148732ba5a 100644
--- a/src/lib/libc/net/inet_ntoa.c
+++ b/src/lib/libc/net/inet_ntoa.c
@@ -1,5 +1,3 @@
1/* $NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1983, 1993 2 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: inet_ntoa.c,v 1.2 1996/08/19 08:29:16 tholo Exp $";
38static char sccsid[] = "@(#)inet_ntoa.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44/* 38/*
diff --git a/src/lib/libc/net/inet_ntop.c b/src/lib/libc/net/inet_ntop.c
new file mode 100644
index 0000000000..64d0d13768
--- /dev/null
+++ b/src/lib/libc/net/inet_ntop.c
@@ -0,0 +1,194 @@
1/* $OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $ */
2
3/* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19#if defined(LIBC_SCCS) && !defined(lint)
20#if 0
21static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $";
22#else
23static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $";
24#endif
25#endif /* LIBC_SCCS and not lint */
26
27#include <sys/param.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <arpa/nameser.h>
33#include <string.h>
34#include <errno.h>
35#include <stdio.h>
36
37/*
38 * WARNING: Don't even consider trying to compile this on a system where
39 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
40 */
41
42static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
43static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
44
45/* char *
46 * inet_ntop(af, src, dst, size)
47 * convert a network format address to presentation format.
48 * return:
49 * pointer to presentation format address (`dst'), or NULL (see errno).
50 * author:
51 * Paul Vixie, 1996.
52 */
53const char *
54inet_ntop(af, src, dst, size)
55 int af;
56 const void *src;
57 char *dst;
58 size_t size;
59{
60 switch (af) {
61 case AF_INET:
62 return (inet_ntop4(src, dst, size));
63 case AF_INET6:
64 return (inet_ntop6(src, dst, size));
65 default:
66 errno = EAFNOSUPPORT;
67 return (NULL);
68 }
69 /* NOTREACHED */
70}
71
72/* const char *
73 * inet_ntop4(src, dst, size)
74 * format an IPv4 address, more or less like inet_ntoa()
75 * return:
76 * `dst' (as a const)
77 * notes:
78 * (1) uses no statics
79 * (2) takes a u_char* not an in_addr as input
80 * author:
81 * Paul Vixie, 1996.
82 */
83static const char *
84inet_ntop4(src, dst, size)
85 const u_char *src;
86 char *dst;
87 size_t size;
88{
89 static const char fmt[] = "%u.%u.%u.%u";
90 char tmp[sizeof "255.255.255.255"];
91
92 if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
93 errno = ENOSPC;
94 return (NULL);
95 }
96 strcpy(dst, tmp);
97 return (dst);
98}
99
100/* const char *
101 * inet_ntop6(src, dst, size)
102 * convert IPv6 binary address into presentation (printable) format
103 * author:
104 * Paul Vixie, 1996.
105 */
106static const char *
107inet_ntop6(src, dst, size)
108 const u_char *src;
109 char *dst;
110 size_t size;
111{
112 /*
113 * Note that int32_t and int16_t need only be "at least" large enough
114 * to contain a value of the specified size. On some systems, like
115 * Crays, there is no such thing as an integer variable with 16 bits.
116 * Keep this in mind if you think this function should have been coded
117 * to use pointer overlays. All the world's not a VAX.
118 */
119 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
120 struct { int base, len; } best, cur;
121 u_int words[IN6ADDRSZ / INT16SZ];
122 int i;
123
124 /*
125 * Preprocess:
126 * Copy the input (bytewise) array into a wordwise array.
127 * Find the longest run of 0x00's in src[] for :: shorthanding.
128 */
129 memset(words, '\0', sizeof words);
130 for (i = 0; i < IN6ADDRSZ; i++)
131 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
132 best.base = -1;
133 cur.base = -1;
134 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
135 if (words[i] == 0) {
136 if (cur.base == -1)
137 cur.base = i, cur.len = 1;
138 else
139 cur.len++;
140 } else {
141 if (cur.base != -1) {
142 if (best.base == -1 || cur.len > best.len)
143 best = cur;
144 cur.base = -1;
145 }
146 }
147 }
148 if (cur.base != -1) {
149 if (best.base == -1 || cur.len > best.len)
150 best = cur;
151 }
152 if (best.base != -1 && best.len < 2)
153 best.base = -1;
154
155 /*
156 * Format the result.
157 */
158 tp = tmp;
159 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
160 /* Are we inside the best run of 0x00's? */
161 if (best.base != -1 && i >= best.base &&
162 i < (best.base + best.len)) {
163 if (i == best.base)
164 *tp++ = ':';
165 continue;
166 }
167 /* Are we following an initial run of 0x00s or any real hex? */
168 if (i != 0)
169 *tp++ = ':';
170 /* Is this address an encapsulated IPv4? */
171 if (i == 6 && best.base == 0 &&
172 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
173 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
174 return (NULL);
175 tp += strlen(tp);
176 break;
177 }
178 tp += sprintf(tp, "%x", words[i]);
179 }
180 /* Was it a trailing run of 0x00's? */
181 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
182 *tp++ = ':';
183 *tp++ = '\0';
184
185 /*
186 * Check for overflow, copy, and we're done.
187 */
188 if ((size_t)(tp - tmp) > size) {
189 errno = ENOSPC;
190 return (NULL);
191 }
192 strcpy(dst, tmp);
193 return (dst);
194}
diff --git a/src/lib/libc/net/inet_pton.c b/src/lib/libc/net/inet_pton.c
new file mode 100644
index 0000000000..46b4b24819
--- /dev/null
+++ b/src/lib/libc/net/inet_pton.c
@@ -0,0 +1,220 @@
1/* $OpenBSD: inet_pton.c,v 1.2 1997/04/13 05:08:24 deraadt Exp $ */
2
3/* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19#if defined(LIBC_SCCS) && !defined(lint)
20#if 0
21static char rcsid[] = "$From: inet_pton.c,v 8.7 1996/08/05 08:31:35 vixie Exp $";
22#else
23static char rcsid[] = "$OpenBSD: inet_pton.c,v 1.2 1997/04/13 05:08:24 deraadt Exp $";
24#endif
25#endif /* LIBC_SCCS and not lint */
26
27#include <sys/param.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <arpa/nameser.h>
33#include <string.h>
34#include <errno.h>
35
36/*
37 * WARNING: Don't even consider trying to compile this on a system where
38 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
39 */
40
41static int inet_pton4 __P((const char *src, u_char *dst));
42static int inet_pton6 __P((const char *src, u_char *dst));
43
44/* int
45 * inet_pton(af, src, dst)
46 * convert from presentation format (which usually means ASCII printable)
47 * to network format (which is usually some kind of binary format).
48 * return:
49 * 1 if the address was valid for the specified address family
50 * 0 if the address wasn't valid (`dst' is untouched in this case)
51 * -1 if some other error occurred (`dst' is untouched in this case, too)
52 * author:
53 * Paul Vixie, 1996.
54 */
55int
56inet_pton(af, src, dst)
57 int af;
58 const char *src;
59 void *dst;
60{
61 switch (af) {
62 case AF_INET:
63 return (inet_pton4(src, dst));
64 case AF_INET6:
65 return (inet_pton6(src, dst));
66 default:
67 errno = EAFNOSUPPORT;
68 return (-1);
69 }
70 /* NOTREACHED */
71}
72
73/* int
74 * inet_pton4(src, dst)
75 * like inet_aton() but without all the hexadecimal and shorthand.
76 * return:
77 * 1 if `src' is a valid dotted quad, else 0.
78 * notice:
79 * does not touch `dst' unless it's returning 1.
80 * author:
81 * Paul Vixie, 1996.
82 */
83static int
84inet_pton4(src, dst)
85 const char *src;
86 u_char *dst;
87{
88 static const char digits[] = "0123456789";
89 int saw_digit, octets, ch;
90 u_char tmp[INADDRSZ], *tp;
91
92 saw_digit = 0;
93 octets = 0;
94 *(tp = tmp) = 0;
95 while ((ch = *src++) != '\0') {
96 const char *pch;
97
98 if ((pch = strchr(digits, ch)) != NULL) {
99 u_int new = *tp * 10 + (pch - digits);
100
101 if (new > 255)
102 return (0);
103 if (! saw_digit) {
104 if (++octets > 4)
105 return (0);
106 saw_digit = 1;
107 }
108 *tp = new;
109 } else if (ch == '.' && saw_digit) {
110 if (octets == 4)
111 return (0);
112 *++tp = 0;
113 saw_digit = 0;
114 } else
115 return (0);
116 }
117 if (octets < 4)
118 return (0);
119
120 memcpy(dst, tmp, INADDRSZ);
121 return (1);
122}
123
124/* int
125 * inet_pton6(src, dst)
126 * convert presentation level address to network order binary form.
127 * return:
128 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
129 * notice:
130 * (1) does not touch `dst' unless it's returning 1.
131 * (2) :: in a full address is silently ignored.
132 * credit:
133 * inspired by Mark Andrews.
134 * author:
135 * Paul Vixie, 1996.
136 */
137static int
138inet_pton6(src, dst)
139 const char *src;
140 u_char *dst;
141{
142 static const char xdigits_l[] = "0123456789abcdef",
143 xdigits_u[] = "0123456789ABCDEF";
144 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
145 const char *xdigits, *curtok;
146 int ch, saw_xdigit;
147 u_int val;
148
149 memset((tp = tmp), '\0', IN6ADDRSZ);
150 endp = tp + IN6ADDRSZ;
151 colonp = NULL;
152 /* Leading :: requires some special handling. */
153 if (*src == ':')
154 if (*++src != ':')
155 return (0);
156 curtok = src;
157 saw_xdigit = 0;
158 val = 0;
159 while ((ch = *src++) != '\0') {
160 const char *pch;
161
162 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
163 pch = strchr((xdigits = xdigits_u), ch);
164 if (pch != NULL) {
165 val <<= 4;
166 val |= (pch - xdigits);
167 if (val > 0xffff)
168 return (0);
169 saw_xdigit = 1;
170 continue;
171 }
172 if (ch == ':') {
173 curtok = src;
174 if (!saw_xdigit) {
175 if (colonp)
176 return (0);
177 colonp = tp;
178 continue;
179 }
180 if (tp + INT16SZ > endp)
181 return (0);
182 *tp++ = (u_char) (val >> 8) & 0xff;
183 *tp++ = (u_char) val & 0xff;
184 saw_xdigit = 0;
185 val = 0;
186 continue;
187 }
188 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
189 inet_pton4(curtok, tp) > 0) {
190 tp += INADDRSZ;
191 saw_xdigit = 0;
192 break; /* '\0' was seen by inet_pton4(). */
193 }
194 return (0);
195 }
196 if (saw_xdigit) {
197 if (tp + INT16SZ > endp)
198 return (0);
199 *tp++ = (u_char) (val >> 8) & 0xff;
200 *tp++ = (u_char) val & 0xff;
201 }
202 if (colonp != NULL) {
203 /*
204 * Since some memmove()'s erroneously fail to handle
205 * overlapping regions, we'll do the shift by hand.
206 */
207 const int n = tp - colonp;
208 int i;
209
210 for (i = 1; i <= n; i++) {
211 endp[- i] = colonp[n - i];
212 colonp[n - i] = 0;
213 }
214 tp = endp;
215 }
216 if (tp != endp)
217 return (0);
218 memcpy(dst, tmp, IN6ADDRSZ);
219 return (1);
220}
diff --git a/src/lib/libc/net/ipx.3 b/src/lib/libc/net/ipx.3
new file mode 100644
index 0000000000..073be74807
--- /dev/null
+++ b/src/lib/libc/net/ipx.3
@@ -0,0 +1,126 @@
1.\" $OpenBSD: ipx.3,v 1.3 1997/09/09 11:45:17 kstailey Exp $
2.\"
3.\" Copyright (c) 1986, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.Dd June 4, 1993
35.Dt IPX 3
36.Os OpenBSD 1.2
37.Sh NAME
38.Nm ipx_addr ,
39.Nm ipx_ntoa
40.Nd IPX address conversion routines
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <netipx/ipx.h>
44.Ft struct ipx_addr
45.Fn ipx_addr "char *cp"
46.Ft char *
47.Fn ipx_ntoa "struct ipx_addr ipx"
48.Sh DESCRIPTION
49The routine
50.Fn ipx_addr
51interprets character strings representing
52.Tn IPX
53addresses, returning binary information suitable
54for use in system calls.
55The routine
56.Fn ipx_ntoa
57takes
58.Tn IPX
59addresses and returns
60.Tn ASCII
61strings representing the address in a
62notation in common use:
63.Bd -filled -offset indent
64<network number>.<host number>.<port number>
65.Ed
66.Pp
67Trailing zero fields are suppressed, and each number is printed in hexadecimal,
68in a format suitable for input to
69.Fn ipx_addr .
70Any fields lacking super-decimal digits will have a
71trailing
72.Ql H
73appended.
74.Pp
75An effort has been made to insure that
76.Fn ipx_addr
77be compatible with most formats in common use.
78It will first separate an address into 1 to 3 fields using a single delimiter
79chosen from
80period
81.Ql \&. ,
82colon
83.Ql \&:
84or pound-sign
85.Ql \&# .
86Each field is then examined for byte separators (colon or period).
87If there are byte separators, each subfield separated is taken to be
88a small hexadecimal number, and the entirety is taken as a network-byte-ordered
89quantity to be zero extended in the high-network-order bytes.
90Next, the field is inspected for hyphens, in which case
91the field is assumed to be a number in decimal notation
92with hyphens separating the millenia.
93Next, the field is assumed to be a number:
94It is interpreted
95as hexadecimal if there is a leading
96.Ql 0x
97(as in C),
98a trailing
99.Ql H
100(as in Mesa), or there are any super-decimal digits present.
101It is interpreted as octal is there is a leading
102.Ql 0
103and there are no super-octal digits.
104Otherwise, it is converted as a decimal number.
105.Sh RETURN VALUES
106None. (See
107.Sx BUGS . )
108.Sh SEE ALSO
109.Xr ns 4 ,
110.Xr hosts 5 ,
111.Xr networks 5
112.Sh HISTORY
113The precursor
114.Fn ns_addr
115and
116.Fn ns_ntoa
117functions appeared in
118.Bx 4.3 .
119.Sh BUGS
120The string returned by
121.Fn ipx_ntoa
122resides in a static memory area.
123The function
124.Fn ipx_addr
125should diagnose improperly formed input, and there should be an unambiguous
126way to recognize this.
diff --git a/src/lib/libc/net/ipx_addr.c b/src/lib/libc/net/ipx_addr.c
new file mode 100644
index 0000000000..a76e03e913
--- /dev/null
+++ b/src/lib/libc/net/ipx_addr.c
@@ -0,0 +1,229 @@
1/*
2 * Copyright (c) 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * J.Q. Johnson.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from @(#)ipx_addr.c
37 */
38
39#if defined(LIBC_SCCS) && !defined(lint)
40static char rcsid[] = "$OpenBSD: ipx_addr.c,v 1.3 1997/07/09 01:08:39 millert Exp $";
41#endif /* LIBC_SCCS and not lint */
42
43#include <sys/param.h>
44#include <netipx/ipx.h>
45#include <stdio.h>
46#include <string.h>
47
48static struct ipx_addr addr, zero_addr;
49
50static void Field(), cvtbase();
51
52struct ipx_addr
53ipx_addr(name)
54 const char *name;
55{
56 char separator;
57 char *hostname, *socketname, *cp;
58 char buf[50];
59
60 (void)strncpy(buf, name, sizeof(buf) - 1);
61 buf[sizeof(buf) - 1] = '\0';
62
63 /*
64 * First, figure out what he intends as a field separtor.
65 * Despite the way this routine is written, the prefered
66 * form 2-272.AA001234H.01777, i.e. XDE standard.
67 * Great efforts are made to insure backward compatability.
68 */
69 if ((hostname = strchr(buf, '#')))
70 separator = '#';
71 else {
72 hostname = strchr(buf, '.');
73 if ((cp = strchr(buf, ':')) &&
74 ((hostname && cp < hostname) || (hostname == 0))) {
75 hostname = cp;
76 separator = ':';
77 } else
78 separator = '.';
79 }
80 if (hostname)
81 *hostname++ = 0;
82
83 addr = zero_addr;
84 Field(buf, addr.ipx_net.c_net, 4);
85 if (hostname == 0)
86 return (addr); /* No separator means net only */
87
88 socketname = strchr(hostname, separator);
89 if (socketname) {
90 *socketname++ = 0;
91 Field(socketname, (u_char *)&addr.ipx_port, 2);
92 }
93
94 Field(hostname, addr.ipx_host.c_host, 6);
95
96 return (addr);
97}
98
99static void
100Field(buf, out, len)
101 char *buf;
102 u_char *out;
103 int len;
104{
105 register char *bp = buf;
106 int i, ibase, base16 = 0, base10 = 0, clen = 0;
107 int hb[6], *hp;
108 char *fmt;
109
110 /*
111 * first try 2-273#2-852-151-014#socket
112 */
113 if ((*buf != '-') &&
114 (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d",
115 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) {
116 cvtbase(1000L, 256, hb, i, out, len);
117 return;
118 }
119 /*
120 * try form 8E1#0.0.AA.0.5E.E6#socket
121 */
122 if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x",
123 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
124 cvtbase(256L, 256, hb, i, out, len);
125 return;
126 }
127 /*
128 * try form 8E1#0:0:AA:0:5E:E6#socket
129 */
130 if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x",
131 &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) {
132 cvtbase(256L, 256, hb, i, out, len);
133 return;
134 }
135 /*
136 * This is REALLY stretching it but there was a
137 * comma notation separting shorts -- definitely non standard
138 */
139 if (1 < (i = sscanf(buf,"%x,%x,%x",
140 &hb[0], &hb[1], &hb[2]))) {
141 hb[0] = htons(hb[0]); hb[1] = htons(hb[1]);
142 hb[2] = htons(hb[2]);
143 cvtbase(65536L, 256, hb, i, out, len);
144 return;
145 }
146
147 /* Need to decide if base 10, 16 or 8 */
148 while (*bp) switch (*bp++) {
149
150 case '0': case '1': case '2': case '3': case '4': case '5':
151 case '6': case '7': case '-':
152 break;
153
154 case '8': case '9':
155 base10 = 1;
156 break;
157
158 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
159 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
160 base16 = 1;
161 break;
162
163 case 'x': case 'X':
164 *--bp = '0';
165 base16 = 1;
166 break;
167
168 case 'h': case 'H':
169 base16 = 1;
170 /* fall into */
171
172 default:
173 *--bp = 0; /* Ends Loop */
174 }
175 if (base16) {
176 fmt = "%3x";
177 ibase = 4096;
178 } else if (base10 == 0 && *buf == '0') {
179 fmt = "%3o";
180 ibase = 512;
181 } else {
182 fmt = "%3d";
183 ibase = 1000;
184 }
185
186 for (bp = buf; *bp++; ) clen++;
187 if (clen == 0) clen++;
188 if (clen > 18) clen = 18;
189 i = ((clen - 1) / 3) + 1;
190 bp = clen + buf - 3;
191 hp = hb + i - 1;
192
193 while (hp > hb) {
194 (void)sscanf(bp, fmt, hp);
195 bp[0] = 0;
196 hp--;
197 bp -= 3;
198 }
199 (void)sscanf(buf, fmt, hp);
200 cvtbase((long)ibase, 256, hb, i, out, len);
201}
202
203static void
204cvtbase(oldbase,newbase,input,inlen,result,reslen)
205 long oldbase;
206 int newbase;
207 int input[];
208 int inlen;
209 unsigned char result[];
210 int reslen;
211{
212 int d, e;
213 long sum;
214
215 e = 1;
216 while (e > 0 && reslen > 0) {
217 d = 0; e = 0; sum = 0;
218 /* long division: input=input/newbase */
219 while (d < inlen) {
220 sum = sum*oldbase + (long) input[d];
221 e += (sum > 0);
222 input[d++] = sum / newbase;
223 sum %= newbase;
224 }
225 result[--reslen] = sum; /* accumulate remainder */
226 }
227 for (d=0; d < reslen; d++)
228 result[d] = 0;
229}
diff --git a/src/lib/libc/net/ipx_ntoa.c b/src/lib/libc/net/ipx_ntoa.c
new file mode 100644
index 0000000000..1dcfe7181b
--- /dev/null
+++ b/src/lib/libc/net/ipx_ntoa.c
@@ -0,0 +1,58 @@
1/*
2 * Copyright (c) 1986, 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char rcsid[] = "$OpenBSD: ipx_ntoa.c,v 1.2 1996/08/19 08:29:20 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/param.h>
39#include <netipx/ipx.h>
40#include <stdio.h>
41
42char *
43ipx_ntoa(addr)
44 struct ipx_addr addr;
45{
46 static char obuf[] = "xxxx.xx:xx:xx:xx:xx:xx.uuuuu";
47
48 sprintf(obuf, "%8xH.%02x:%02x:%02x:%02x:%02x:%02x.%u",
49 ntohl(addr.ipx_net.l_net),
50 addr.ipx_host.c_host[0],
51 addr.ipx_host.c_host[1],
52 addr.ipx_host.c_host[2],
53 addr.ipx_host.c_host[3],
54 addr.ipx_host.c_host[4],
55 addr.ipx_host.c_host[5],
56 ntohs(addr.ipx_port));
57 return (obuf);
58}
diff --git a/src/lib/libc/net/iso_addr.3 b/src/lib/libc/net/iso_addr.3
index 95c136e5fc..d9bf9086be 100644
--- a/src/lib/libc/net/iso_addr.3
+++ b/src/lib/libc/net/iso_addr.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: iso_addr.3,v 1.2 1995/02/25 06:20:46 cgd Exp $ 1.\" $OpenBSD: iso_addr.3,v 1.2 1996/08/19 08:29:22 tholo Exp $
2.\" 2.\"
3.\" Copyright (c) 1993 3.\" Copyright (c) 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)iso_addr.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt ISO_ADDR 3 35.Dt ISO_ADDR 3
38.Os 36.Os
diff --git a/src/lib/libc/net/iso_addr.c b/src/lib/libc/net/iso_addr.c
index c26ec1a64a..01561e395b 100644
--- a/src/lib/libc/net/iso_addr.c
+++ b/src/lib/libc/net/iso_addr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1989, 1993 2 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: iso_addr.c,v 1.2 1996/08/19 08:29:23 tholo Exp $";
38static char sccsid[] = "@(#)iso_addr.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
diff --git a/src/lib/libc/net/linkaddr.3 b/src/lib/libc/net/link_addr.3
index 1a2af9b30d..eb6c952177 100644
--- a/src/lib/libc/net/linkaddr.3
+++ b/src/lib/libc/net/link_addr.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: linkaddr.3,v 1.2 1995/02/25 06:20:48 cgd Exp $ 1.\" $OpenBSD: link_addr.3,v 1.2 1996/08/19 08:29:25 tholo Exp $
2.\" 2.\"
3.\" Copyright (c) 1993 3.\" Copyright (c) 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -34,8 +34,6 @@
34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35.\" SUCH DAMAGE. 35.\" SUCH DAMAGE.
36.\" 36.\"
37.\" @(#)linkaddr.3 8.1 (Berkeley) 7/28/93
38.\"
39.Dd July 28, 1993 37.Dd July 28, 1993
40.Dt LINK_ADDR 3 38.Dt LINK_ADDR 3
41.Os BSD 4.4 39.Os BSD 4.4
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c
index 19a0de3abd..fb522f3233 100644
--- a/src/lib/libc/net/linkaddr.c
+++ b/src/lib/libc/net/linkaddr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $ */
2
3/*- 1/*-
4 * Copyright (c) 1990, 1993 2 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: linkaddr.c,v 1.2 1996/08/19 08:29:27 tholo Exp $";
38static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
diff --git a/src/lib/libc/net/ns.3 b/src/lib/libc/net/ns.3
index f89b4fe042..6e096d9f4b 100644
--- a/src/lib/libc/net/ns.3
+++ b/src/lib/libc/net/ns.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: ns.3,v 1.3 1995/02/25 06:20:50 cgd Exp $ 1.\" $OpenBSD: ns.3,v 1.2 1996/08/19 08:29:29 tholo Exp $
2.\" 2.\"
3.\" Copyright (c) 1986, 1991, 1993 3.\" Copyright (c) 1986, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)ns.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt NS 3 35.Dt NS 3
38.Os BSD 4.3 36.Os BSD 4.3
diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c
index f75ddb23b7..8f2e4bc513 100644
--- a/src/lib/libc/net/ns_addr.c
+++ b/src/lib/libc/net/ns_addr.c
@@ -1,5 +1,3 @@
1/* $NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1986, 1993 2 * Copyright (c) 1986, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -37,11 +35,7 @@
37 */ 35 */
38 36
39#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
40#if 0 38static char rcsid[] = "$OpenBSD: ns_addr.c,v 1.4 1997/07/21 20:31:05 deraadt Exp $";
41static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93";
42#else
43static char rcsid[] = "$NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $";
44#endif
45#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
46 40
47#include <sys/param.h> 41#include <sys/param.h>
@@ -51,7 +45,8 @@ static char rcsid[] = "$NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $";
51 45
52static struct ns_addr addr, zero_addr; 46static struct ns_addr addr, zero_addr;
53 47
54static void Field(), cvtbase(); 48static void Field __P((char *, u_int8_t *, int));
49static void cvtbase __P((long, int, int[], int, u_int8_t[], int));
55 50
56struct ns_addr 51struct ns_addr
57ns_addr(name) 52ns_addr(name)
@@ -70,7 +65,7 @@ ns_addr(name)
70 * form 2-272.AA001234H.01777, i.e. XDE standard. 65 * form 2-272.AA001234H.01777, i.e. XDE standard.
71 * Great efforts are made to insure backward compatability. 66 * Great efforts are made to insure backward compatability.
72 */ 67 */
73 if (hostname = strchr(buf, '#')) 68 if ((hostname = strchr(buf, '#')))
74 separator = '#'; 69 separator = '#';
75 else { 70 else {
76 hostname = strchr(buf, '.'); 71 hostname = strchr(buf, '.');
@@ -95,7 +90,7 @@ ns_addr(name)
95 Field(socketname, (u_char *)&addr.x_port, 2); 90 Field(socketname, (u_char *)&addr.x_port, 2);
96 } 91 }
97 92
98 Field(hostname, addr.x_host.c_host, 6); 93 Field(hostname, (u_char *)addr.x_host.c_host, 6);
99 94
100 return (addr); 95 return (addr);
101} 96}
diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c
index ad3265399b..c33f710966 100644
--- a/src/lib/libc/net/ns_ntoa.c
+++ b/src/lib/libc/net/ns_ntoa.c
@@ -1,5 +1,3 @@
1/* $NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1986, 1993 2 * Copyright (c) 1986, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,32 +32,29 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: ns_ntoa.c,v 1.7 1997/08/24 21:25:48 millert Exp $";
38static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/param.h> 38#include <sys/param.h>
45#include <netns/ns.h> 39#include <netns/ns.h>
46#include <stdio.h> 40#include <stdio.h>
47 41
42static char *spectHex __P((char *));
43
48char * 44char *
49ns_ntoa(addr) 45ns_ntoa(addr)
50 struct ns_addr addr; 46 struct ns_addr addr;
51{ 47{
52 static char obuf[40]; 48 static char obuf[40];
53 union { union ns_net net_e; u_long long_e; } net; 49 union { union ns_net net_e; u_int32_t long_e; } net;
54 u_short port = htons(addr.x_port); 50 in_port_t port = htons(addr.x_port);
55 register char *cp; 51 register char *cp;
56 char *cp2; 52 char *cp2;
57 register u_char *up = addr.x_host.c_host; 53 register u_char *up = addr.x_host.c_host;
58 u_char *uplim = up + 6; 54 u_char *uplim = up + 6;
59 static char *spectHex();
60 55
61 net.net_e = addr.x_net; 56 net.net_e = addr.x_net;
62 sprintf(obuf, "%lx", ntohl(net.long_e)); 57 sprintf(obuf, "%x", ntohl(net.long_e));
63 cp = spectHex(obuf); 58 cp = spectHex(obuf);
64 cp2 = cp + 1; 59 cp2 = cp + 1;
65 while (*up==0 && up < uplim) up++; 60 while (*up==0 && up < uplim) up++;
diff --git a/src/lib/libc/net/nsap_addr.c b/src/lib/libc/net/nsap_addr.c
new file mode 100644
index 0000000000..22a5f8d66e
--- /dev/null
+++ b/src/lib/libc/net/nsap_addr.c
@@ -0,0 +1,109 @@
1/* $OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21#if 0
22static char rcsid[] = "$From: nsap_addr.c,v 8.3 1996/08/05 08:31:35 vixie Exp $";
23#else
24static char rcsid[] = "$OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $";
25#endif
26#endif /* LIBC_SCCS and not lint */
27
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <arpa/nameser.h>
33#include <ctype.h>
34#include <resolv.h>
35
36static char
37xtob(c)
38 register int c;
39{
40 return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
41}
42
43u_int
44inet_nsap_addr(ascii, binary, maxlen)
45 const char *ascii;
46 u_char *binary;
47 int maxlen;
48{
49 register u_char c, nib;
50 u_int len = 0;
51
52 while ((c = *ascii++) != '\0' && len < maxlen) {
53 if (c == '.' || c == '+' || c == '/')
54 continue;
55 if (!isascii(c))
56 return (0);
57 if (islower(c))
58 c = toupper(c);
59 if (isxdigit(c)) {
60 nib = xtob(c);
61 if ((c = *ascii++)) {
62 c = toupper(c);
63 if (isxdigit(c)) {
64 *binary++ = (nib << 4) | xtob(c);
65 len++;
66 } else
67 return (0);
68 }
69 else
70 return (0);
71 }
72 else
73 return (0);
74 }
75 return (len);
76}
77
78char *
79inet_nsap_ntoa(binlen, binary, ascii)
80 int binlen;
81 register const u_char *binary;
82 register char *ascii;
83{
84 register int nib;
85 int i;
86 static char tmpbuf[255*3];
87 char *start;
88
89 if (ascii)
90 start = ascii;
91 else {
92 ascii = tmpbuf;
93 start = tmpbuf;
94 }
95
96 if (binlen > 255)
97 binlen = 255;
98
99 for (i = 0; i < binlen; i++) {
100 nib = *binary >> 4;
101 *ascii++ = nib + (nib < 10 ? '0' : '7');
102 nib = *binary++ & 0x0f;
103 *ascii++ = nib + (nib < 10 ? '0' : '7');
104 if (((i % 2) == 0 && (i + 1) < binlen))
105 *ascii++ = '.';
106 }
107 *ascii = '\0';
108 return (start);
109}
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c
index 05b7f4c9a3..7d3e227e60 100644
--- a/src/lib/libc/net/ntohl.c
+++ b/src/lib/libc/net/ntohl.c
@@ -1,29 +1,25 @@
1/* $NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $ */
2
3/* 1/*
4 * Written by J.T. Conklin <jtc@netbsd.org>. 2 * Written by J.T. Conklin <jtc@netbsd.org>.
5 * Public domain. 3 * Public domain.
6 */ 4 */
7 5
8#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $"; 7static char *rcsid = "$OpenBSD: ntohl.c,v 1.4 1996/12/12 03:19:56 tholo Exp $";
10#endif 8#endif /* LIBC_SCCS and not lint */
11 9
12#include <sys/types.h> 10#include <sys/types.h>
13#include <machine/endian.h> 11#include <machine/endian.h>
14 12
15#undef ntohl 13#undef ntohl
16 14
17unsigned long 15u_int32_t
18ntohl(x) 16ntohl(x)
19 unsigned long x; 17 u_int32_t x;
20{ 18{
21 u_int32_t y = x;
22
23#if BYTE_ORDER == LITTLE_ENDIAN 19#if BYTE_ORDER == LITTLE_ENDIAN
24 u_char *s = (u_char *)&y; 20 u_char *s = (u_char *)&x;
25 return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; 21 return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
26#else 22#else
27 return y; 23 return x;
28#endif 24#endif
29} 25}
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c
index 93ab83ee4d..cf6414d4a6 100644
--- a/src/lib/libc/net/ntohs.c
+++ b/src/lib/libc/net/ntohs.c
@@ -1,26 +1,28 @@
1/* $NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $ */
2
3/* 1/*
4 * Written by J.T. Conklin <jtc@netbsd.org>. 2 * Written by J.T. Conklin <jtc@netbsd.org>.
5 * Public domain. 3 * Public domain.
6 */ 4 */
7 5
8#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $"; 7static char *rcsid = "$OpenBSD: ntohs.c,v 1.6 1997/07/25 20:30:07 mickey Exp $";
10#endif 8#endif /* LIBC_SCCS and not lint */
11 9
12#include <sys/types.h> 10#include <sys/types.h>
13#include <machine/endian.h> 11#include <machine/endian.h>
14 12
15#undef ntohs 13#undef ntohs
16 14
17unsigned short 15u_int16_t
16#ifdef __STDC__
17ntohs(u_int16_t x)
18#else
18ntohs(x) 19ntohs(x)
19 unsigned short x; 20 u_int16_t x;
21#endif
20{ 22{
21#if BYTE_ORDER == LITTLE_ENDIAN 23#if BYTE_ORDER == LITTLE_ENDIAN
22 u_char *s = (u_char *) &x; 24 u_char *s = (u_char *) &x;
23 return s[0] << 8 | s[1]; 25 return (u_int16_t)(s[0] << 8 | s[1]);
24#else 26#else
25 return x; 27 return x;
26#endif 28#endif
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3
index 4db847c392..e69e822834 100644
--- a/src/lib/libc/net/rcmd.3
+++ b/src/lib/libc/net/rcmd.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: rcmd.3,v 1.8 1995/02/25 06:20:52 cgd Exp $ 1.\" $OpenBSD: rcmd.3,v 1.10 1997/09/29 18:25:47 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1983, 1991, 1993 3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)rcmd.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt RCMD 3 35.Dt RCMD 3
38.Os BSD 4.2 36.Os BSD 4.2
@@ -49,16 +47,30 @@
49.Ft int 47.Ft int
50.Fn rresvport "int *port" 48.Fn rresvport "int *port"
51.Ft int 49.Ft int
52.Fn iruserok "u_long raddr" "int superuser" "const char *ruser" "const char *luser" 50.Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser"
53.Ft int 51.Ft int
54.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" 52.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser"
55.Sh DESCRIPTION 53.Sh DESCRIPTION
56The 54The
57.Fn rcmd 55.Fn rcmd
58function 56function
59is used by the super-user to execute a command on 57is used by the super-user to execute a command on a remote
60a remote machine using an authentication scheme based 58machine using an authentication scheme based on reserved
61on reserved port numbers. 59port numbers. If the calling process is not setuid and the
60.Li RSH
61environment variable is set and
62.Fa inport
63is
64.Li shell/tcp ,
65.Xr rcmdsh 3
66is called instead with the value of
67.Li RSH .
68Alternately, if the user is not the super-user,
69.Fn rcmd
70will invoke
71.Xr rcmdsh 3
72to run the command via
73.Xr rsh 1 .
62The 74The
63.Fn rresvport 75.Fn rresvport
64function 76function
@@ -90,6 +102,9 @@ is set to the standard name of the host
90and a connection is established to a server 102and a connection is established to a server
91residing at the well-known Internet port 103residing at the well-known Internet port
92.Fa inport . 104.Fa inport .
105If the user is not the super-user, the only valid port is
106.Li shell/tcp ,
107(usually port 514).
93.Pp 108.Pp
94If the connection succeeds, 109If the connection succeeds,
95a socket in the Internet domain of type 110a socket in the Internet domain of type
@@ -121,6 +136,9 @@ command) will be made the same as the
121and no 136and no
122provision is made for sending arbitrary signals to the remote process, 137provision is made for sending arbitrary signals to the remote process,
123although you may be able to get its attention by using out-of-band data. 138although you may be able to get its attention by using out-of-band data.
139Note that if the user is not the super-user,
140.Fa fd2p
141must be 0.
124.Pp 142.Pp
125The protocol is described in detail in 143The protocol is described in detail in
126.Xr rshd 8 . 144.Xr rshd 8 .
@@ -134,6 +152,9 @@ by
134and several other functions. Privileged Internet ports are those 152and several other functions. Privileged Internet ports are those
135in the range 0 to 1023. Only the super-user 153in the range 0 to 1023. Only the super-user
136is allowed to bind an address of this sort to a socket. 154is allowed to bind an address of this sort to a socket.
155.Fn rresvport
156needs to be seeded with a port number; if that port
157is not available it will find another.
137.Pp 158.Pp
138The 159The
139.Fn iruserok 160.Fn iruserok
@@ -165,7 +186,7 @@ and
165.Fn ruserok 186.Fn ruserok
166return \-1. 187return \-1.
167If the local domain (as obtained from 188If the local domain (as obtained from
168.Xr gethostname 2 ) 189.Xr gethostname 3 )
169is the same as the remote domain, only the machine name need be specified. 190is the same as the remote domain, only the machine name need be specified.
170.Pp 191.Pp
171If the IP address of the remote host is known, 192If the IP address of the remote host is known,
@@ -195,8 +216,10 @@ is overloaded to mean ``All network ports in use.''
195.Xr rsh 1 , 216.Xr rsh 1 ,
196.Xr intro 2 , 217.Xr intro 2 ,
197.Xr rexec 3 , 218.Xr rexec 3 ,
219.Xr rcmdsh 3 ,
198.Xr rexecd 8 , 220.Xr rexecd 8 ,
199.Xr rlogind 8 , 221.Xr rlogind 8 ,
222.Xr bindresvport 3 ,
200.Xr rshd 8 223.Xr rshd 8
201.Sh HISTORY 224.Sh HISTORY
202These 225These
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c
index e0310031b0..c933f5b447 100644
--- a/src/lib/libc/net/rcmd.c
+++ b/src/lib/libc/net/rcmd.c
@@ -1,6 +1,5 @@
1/* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $ */
2
3/* 1/*
2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
4 * Copyright (c) 1983, 1993, 1994 3 * Copyright (c) 1983, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 4 * The Regents of the University of California. All rights reserved.
6 * 5 *
@@ -16,6 +15,7 @@
16 * must display the following acknowledgement: 15 * must display the following acknowledgement:
17 * This product includes software developed by the University of 16 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 17 * California, Berkeley and its contributors.
18 * This product includes software developed by Theo de Raadt.
19 * 4. Neither the name of the University nor the names of its contributors 19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 21 * without specific prior written permission.
@@ -34,11 +34,7 @@
34 */ 34 */
35 35
36#if defined(LIBC_SCCS) && !defined(lint) 36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 37static char *rcsid = "$OpenBSD: rcmd.c,v 1.31 1998/03/19 00:30:05 millert Exp $";
38static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
39#else
40static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 38#endif /* LIBC_SCCS and not lint */
43 39
44#include <sys/param.h> 40#include <sys/param.h>
@@ -57,24 +53,46 @@ static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $";
57#include <stdio.h> 53#include <stdio.h>
58#include <ctype.h> 54#include <ctype.h>
59#include <string.h> 55#include <string.h>
56#include <syslog.h>
57#include <stdlib.h>
58#include <netgroup.h>
60 59
61int __ivaliduser __P((FILE *, u_long, const char *, const char *)); 60int __ivaliduser __P((FILE *, in_addr_t, const char *, const char *));
62static int __icheckhost __P((u_long, const char *)); 61static int __icheckhost __P((u_int32_t, const char *));
62static char *__gethostloop __P((u_int32_t));
63 63
64int 64int
65rcmd(ahost, rport, locuser, remuser, cmd, fd2p) 65rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
66 char **ahost; 66 char **ahost;
67 u_short rport; 67 in_port_t rport;
68 const char *locuser, *remuser, *cmd; 68 const char *locuser, *remuser, *cmd;
69 int *fd2p; 69 int *fd2p;
70{ 70{
71 struct hostent *hp; 71 struct hostent *hp;
72 struct sockaddr_in sin, from; 72 struct sockaddr_in sin, from;
73 fd_set reads; 73 fd_set *readsp = NULL;
74 long oldmask; 74 int oldmask;
75 pid_t pid; 75 pid_t pid;
76 int s, lport, timo; 76 int s, lport, timo;
77 char c; 77 char c, *p;
78
79 /* call rcmdsh() with specified remote shell if appropriate. */
80 if (!issetugid() && (p = getenv("RSH"))) {
81 struct servent *sp = getservbyname("shell", "tcp");
82
83 if (sp && sp->s_port == rport)
84 return (rcmdsh(ahost, rport, locuser, remuser,
85 cmd, p));
86 }
87
88 /* use rsh(1) if non-root and remote port is shell. */
89 if (geteuid()) {
90 struct servent *sp = getservbyname("shell", "tcp");
91
92 if (sp && sp->s_port == rport)
93 return (rcmdsh(ahost, rport, locuser, remuser,
94 cmd, NULL));
95 }
78 96
79 pid = getpid(); 97 pid = getpid();
80 hp = gethostbyname(*ahost); 98 hp = gethostbyname(*ahost);
@@ -83,6 +101,7 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
83 return (-1); 101 return (-1);
84 } 102 }
85 *ahost = hp->h_name; 103 *ahost = hp->h_name;
104
86 oldmask = sigblock(sigmask(SIGURG)); 105 oldmask = sigblock(sigmask(SIGURG));
87 for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 106 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
88 s = rresvport(&lport); 107 s = rresvport(&lport);
@@ -97,6 +116,7 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
97 return (-1); 116 return (-1);
98 } 117 }
99 fcntl(s, F_SETOWN, pid); 118 fcntl(s, F_SETOWN, pid);
119 bzero(&sin, sizeof sin);
100 sin.sin_len = sizeof(struct sockaddr_in); 120 sin.sin_len = sizeof(struct sockaddr_in);
101 sin.sin_family = hp->h_addrtype; 121 sin.sin_family = hp->h_addrtype;
102 sin.sin_port = rport; 122 sin.sin_port = rport;
@@ -130,7 +150,13 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
130 sigsetmask(oldmask); 150 sigsetmask(oldmask);
131 return (-1); 151 return (-1);
132 } 152 }
153#if 0
154 /*
155 * try to rresvport() to the same port. This will make rresvport()
156 * fail it's first bind, resulting in it choosing a random port.
157 */
133 lport--; 158 lport--;
159#endif
134 if (fd2p == 0) { 160 if (fd2p == 0) {
135 write(s, "", 1); 161 write(s, "", 1);
136 lport = 0; 162 lport = 0;
@@ -138,9 +164,13 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
138 char num[8]; 164 char num[8];
139 int s2 = rresvport(&lport), s3; 165 int s2 = rresvport(&lport), s3;
140 int len = sizeof(from); 166 int len = sizeof(from);
167 int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask);
141 168
142 if (s2 < 0) 169 if (s2 < 0)
143 goto bad; 170 goto bad;
171 readsp = (fd_set *)malloc(fdssize);
172 if (readsp == NULL)
173 goto bad;
144 listen(s2, 1); 174 listen(s2, 1);
145 (void)snprintf(num, sizeof(num), "%d", lport); 175 (void)snprintf(num, sizeof(num), "%d", lport);
146 if (write(s, num, strlen(num)+1) != strlen(num)+1) { 176 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
@@ -150,12 +180,13 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
150 (void)close(s2); 180 (void)close(s2);
151 goto bad; 181 goto bad;
152 } 182 }
153 FD_ZERO(&reads); 183again:
154 FD_SET(s, &reads); 184 bzero(readsp, fdssize);
155 FD_SET(s2, &reads); 185 FD_SET(s, readsp);
186 FD_SET(s2, readsp);
156 errno = 0; 187 errno = 0;
157 if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || 188 if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 ||
158 !FD_ISSET(s2, &reads)) { 189 !FD_ISSET(s2, readsp)) {
159 if (errno != 0) 190 if (errno != 0)
160 (void)fprintf(stderr, 191 (void)fprintf(stderr,
161 "rcmd: select (setting up stderr): %s\n", 192 "rcmd: select (setting up stderr): %s\n",
@@ -167,6 +198,14 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
167 goto bad; 198 goto bad;
168 } 199 }
169 s3 = accept(s2, (struct sockaddr *)&from, &len); 200 s3 = accept(s2, (struct sockaddr *)&from, &len);
201 /*
202 * XXX careful for ftp bounce attacks. If discovered, shut them
203 * down and check for the real auxiliary channel to connect.
204 */
205 if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
206 close(s3);
207 goto again;
208 }
170 (void)close(s2); 209 (void)close(s2);
171 if (s3 < 0) { 210 if (s3 < 0) {
172 (void)fprintf(stderr, 211 (void)fprintf(stderr,
@@ -201,11 +240,14 @@ rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
201 goto bad2; 240 goto bad2;
202 } 241 }
203 sigsetmask(oldmask); 242 sigsetmask(oldmask);
243 free(readsp);
204 return (s); 244 return (s);
205bad2: 245bad2:
206 if (lport) 246 if (lport)
207 (void)close(*fd2p); 247 (void)close(*fd2p);
208bad: 248bad:
249 if (readsp)
250 free(readsp);
209 (void)close(s); 251 (void)close(s);
210 sigsetmask(oldmask); 252 sigsetmask(oldmask);
211 return (-1); 253 return (-1);
@@ -218,27 +260,29 @@ rresvport(alport)
218 struct sockaddr_in sin; 260 struct sockaddr_in sin;
219 int s; 261 int s;
220 262
263 bzero(&sin, sizeof sin);
221 sin.sin_len = sizeof(struct sockaddr_in); 264 sin.sin_len = sizeof(struct sockaddr_in);
222 sin.sin_family = AF_INET; 265 sin.sin_family = AF_INET;
223 sin.sin_addr.s_addr = INADDR_ANY; 266 sin.sin_addr.s_addr = INADDR_ANY;
224 s = socket(AF_INET, SOCK_STREAM, 0); 267 s = socket(AF_INET, SOCK_STREAM, 0);
225 if (s < 0) 268 if (s < 0)
226 return (-1); 269 return (-1);
227 for (;;) { 270 sin.sin_port = htons((in_port_t)*alport);
228 sin.sin_port = htons((u_short)*alport); 271 if (*alport < IPPORT_RESERVED - 1) {
229 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 272 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
230 return (s); 273 return (s);
231 if (errno != EADDRINUSE) { 274 if (errno != EADDRINUSE) {
232 (void)close(s); 275 (void)close(s);
233 return (-1); 276 return (-1);
234 } 277 }
235 (*alport)--;
236 if (*alport == IPPORT_RESERVED/2) {
237 (void)close(s);
238 errno = EAGAIN; /* close */
239 return (-1);
240 }
241 } 278 }
279 sin.sin_port = 0;
280 if (bindresvport(s, &sin) == -1) {
281 (void)close(s);
282 return (-1);
283 }
284 *alport = (int)ntohs(sin.sin_port);
285 return (s);
242} 286}
243 287
244int __check_rhosts_file = 1; 288int __check_rhosts_file = 1;
@@ -253,7 +297,7 @@ ruserok(rhost, superuser, ruser, luser)
253 char **ap; 297 char **ap;
254 int i; 298 int i;
255#define MAXADDRS 35 299#define MAXADDRS 35
256 u_long addrs[MAXADDRS + 1]; 300 u_int32_t addrs[MAXADDRS + 1];
257 301
258 if ((hp = gethostbyname(rhost)) == NULL) 302 if ((hp = gethostbyname(rhost)) == NULL)
259 return (-1); 303 return (-1);
@@ -262,7 +306,7 @@ ruserok(rhost, superuser, ruser, luser)
262 addrs[i] = 0; 306 addrs[i] = 0;
263 307
264 for (i = 0; i < MAXADDRS && addrs[i]; i++) 308 for (i = 0; i < MAXADDRS && addrs[i]; i++)
265 if (iruserok(addrs[i], superuser, ruser, luser) == 0) 309 if (iruserok((in_addr_t)addrs[i], superuser, ruser, luser) == 0)
266 return (0); 310 return (0);
267 return (-1); 311 return (-1);
268} 312}
@@ -278,7 +322,7 @@ ruserok(rhost, superuser, ruser, luser)
278 */ 322 */
279int 323int
280iruserok(raddr, superuser, ruser, luser) 324iruserok(raddr, superuser, ruser, luser)
281 u_long raddr; 325 u_int32_t raddr;
282 int superuser; 326 int superuser;
283 const char *ruser, *luser; 327 const char *ruser, *luser;
284{ 328{
@@ -352,41 +396,47 @@ again:
352 * Returns 0 if ok, -1 if not ok. 396 * Returns 0 if ok, -1 if not ok.
353 */ 397 */
354int 398int
355__ivaliduser(hostf, raddr, luser, ruser) 399__ivaliduser(hostf, raddrl, luser, ruser)
356 FILE *hostf; 400 FILE *hostf;
357 u_long raddr; 401 in_addr_t raddrl;
358 const char *luser, *ruser; 402 const char *luser, *ruser;
359{ 403{
360 register char *user, *p; 404 register char *user, *p;
361 int ch; 405 char *buf;
362 char buf[MAXHOSTNAMELEN + 128]; /* host + login */
363 const char *auser, *ahost; 406 const char *auser, *ahost;
364 int hostok, userok; 407 int hostok, userok;
365 char rhost[MAXHOSTNAMELEN]; 408 char *rhost = (char *)-1;
366 struct hostent *hp;
367 char domain[MAXHOSTNAMELEN]; 409 char domain[MAXHOSTNAMELEN];
410 u_int32_t raddr = (u_int32_t)raddrl;
411 size_t buflen;
368 412
369 getdomainname(domain, sizeof(domain)); 413 getdomainname(domain, sizeof(domain));
370 414
371 while (fgets(buf, sizeof(buf), hostf)) { 415 while ((buf = fgetln(hostf, &buflen))) {
372 p = buf; 416 p = buf;
373 /* Skip lines that are too long. */ 417 if (*p == '#')
374 if (strchr(p, '\n') == NULL) {
375 while ((ch = getc(hostf)) != '\n' && ch != EOF);
376 continue; 418 continue;
377 } 419 while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) {
378 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 420 if (!isprint(*p))
421 goto bail;
379 *p = isupper(*p) ? tolower(*p) : *p; 422 *p = isupper(*p) ? tolower(*p) : *p;
380 p++; 423 p++;
381 } 424 }
425 if (p >= buf + buflen)
426 continue;
382 if (*p == ' ' || *p == '\t') { 427 if (*p == ' ' || *p == '\t') {
383 *p++ = '\0'; 428 *p++ = '\0';
384 while (*p == ' ' || *p == '\t') 429 while ((*p == ' ' || *p == '\t') && p < buf + buflen)
385 p++; 430 p++;
431 if (p >= buf + buflen)
432 continue;
386 user = p; 433 user = p;
387 while (*p != '\n' && *p != ' ' && 434 while (*p != '\n' && *p != ' ' &&
388 *p != '\t' && *p != '\0') 435 *p != '\t' && p < buf + buflen) {
436 if (!isprint(*p))
437 goto bail;
389 p++; 438 p++;
439 }
390 } else 440 } else
391 user = p; 441 user = p;
392 *p = '\0'; 442 *p = '\0';
@@ -397,25 +447,27 @@ __ivaliduser(hostf, raddr, luser, ruser)
397 auser = *user ? user : luser; 447 auser = *user ? user : luser;
398 ahost = buf; 448 ahost = buf;
399 449
400 if ((hp = gethostbyaddr((char *) &raddr, 450 if (strlen(ahost) >= MAXHOSTNAMELEN)
401 sizeof(raddr), AF_INET)) == NULL) { 451 continue;
402 abort();
403 return -1;
404 }
405 (void) strncpy(rhost, hp->h_name, sizeof(rhost));
406 rhost[sizeof(rhost) - 1] = '\0';
407 452
453 /*
454 * innetgr() must lookup a hostname (we do not attempt
455 * to change the semantics so that netgroups may have
456 * #.#.#.# addresses in the list.)
457 */
408 if (ahost[0] == '+') 458 if (ahost[0] == '+')
409 switch (ahost[1]) { 459 switch (ahost[1]) {
410 case '\0': 460 case '\0':
411 hostok = 1; 461 hostok = 1;
412 break; 462 break;
413
414 case '@': 463 case '@':
415 hostok = innetgr(&ahost[2], rhost, NULL, 464 if (rhost == (char *)-1)
416 domain); 465 rhost = __gethostloop(raddr);
466 hostok = 0;
467 if (rhost)
468 hostok = innetgr(&ahost[2], rhost,
469 NULL, domain);
417 break; 470 break;
418
419 default: 471 default:
420 hostok = __icheckhost(raddr, &ahost[1]); 472 hostok = __icheckhost(raddr, &ahost[1]);
421 break; 473 break;
@@ -425,12 +477,14 @@ __ivaliduser(hostf, raddr, luser, ruser)
425 case '\0': 477 case '\0':
426 hostok = -1; 478 hostok = -1;
427 break; 479 break;
428
429 case '@': 480 case '@':
430 hostok = -innetgr(&ahost[2], rhost, NULL, 481 if (rhost == (char *)-1)
431 domain); 482 rhost = __gethostloop(raddr);
483 hostok = 0;
484 if (rhost)
485 hostok = -innetgr(&ahost[2], rhost,
486 NULL, domain);
432 break; 487 break;
433
434 default: 488 default:
435 hostok = -__icheckhost(raddr, &ahost[1]); 489 hostok = -__icheckhost(raddr, &ahost[1]);
436 break; 490 break;
@@ -444,14 +498,12 @@ __ivaliduser(hostf, raddr, luser, ruser)
444 case '\0': 498 case '\0':
445 userok = 1; 499 userok = 1;
446 break; 500 break;
447
448 case '@': 501 case '@':
449 userok = innetgr(&auser[2], NULL, ruser, 502 userok = innetgr(&auser[2], NULL, ruser,
450 domain); 503 domain);
451 break; 504 break;
452
453 default: 505 default:
454 userok = strcmp(ruser, &auser[1]) == 0; 506 userok = strcmp(ruser, &auser[1]) ? 0 : 1;
455 break; 507 break;
456 } 508 }
457 else if (auser[0] == '-') 509 else if (auser[0] == '-')
@@ -459,59 +511,97 @@ __ivaliduser(hostf, raddr, luser, ruser)
459 case '\0': 511 case '\0':
460 userok = -1; 512 userok = -1;
461 break; 513 break;
462
463 case '@': 514 case '@':
464 userok = -innetgr(&auser[2], NULL, ruser, 515 userok = -innetgr(&auser[2], NULL, ruser,
465 domain); 516 domain);
466 break; 517 break;
467
468 default: 518 default:
469 userok = -(strcmp(ruser, &auser[1]) == 0); 519 userok = strcmp(ruser, &auser[1]) ? 0 : -1;
470 break; 520 break;
471 } 521 }
472 else 522 else
473 userok = strcmp(ruser, auser) == 0; 523 userok = strcmp(ruser, auser) ? 0 : 1;
474 524
475 /* Check if one component did not match */ 525 /* Check if one component did not match */
476 if (hostok == 0 || userok == 0) 526 if (hostok == 0 || userok == 0)
477 continue; 527 continue;
478 528
479 /* Check if we got a forbidden pair */ 529 /* Check if we got a forbidden pair */
480 if (userok == -1 || hostok == -1) 530 if (userok <= -1 || hostok <= -1)
481 return -1; 531 return (-1);
482 532
483 /* Check if we got a valid pair */ 533 /* Check if we got a valid pair */
484 if (hostok == 1 && userok == 1) 534 if (hostok >= 1 && userok >= 1)
485 return 0; 535 return (0);
486 } 536 }
487 return -1; 537bail:
538 return (-1);
488} 539}
489 540
490/* 541/*
491 * Returns "true" if match, 0 if no match. 542 * Returns "true" if match, 0 if no match. If we do not find any
543 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
492 */ 544 */
493static int 545static int
494__icheckhost(raddr, lhost) 546__icheckhost(raddr, lhost)
495 u_long raddr; 547 u_int32_t raddr;
496 const char *lhost; 548 const char *lhost;
497{ 549{
498 register struct hostent *hp; 550 register struct hostent *hp;
499 register u_long laddr;
500 register char **pp; 551 register char **pp;
552 struct in_addr in;
553
554 hp = gethostbyname(lhost);
555 if (hp != NULL) {
556 /* Spin through ip addresses. */
557 for (pp = hp->h_addr_list; *pp; ++pp)
558 if (!bcmp(&raddr, *pp, sizeof(raddr)))
559 return (1);
560 }
501 561
502 /* Try for raw ip address first. */ 562 in.s_addr = raddr;
503 if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1) 563 if (strcmp(lhost, inet_ntoa(in)) == 0)
504 return (raddr == laddr); 564 return (1);
505
506 /* Better be a hostname. */
507 if ((hp = gethostbyname(lhost)) == NULL)
508 return (0);
509
510 /* Spin through ip addresses. */
511 for (pp = hp->h_addr_list; *pp; ++pp)
512 if (!bcmp(&raddr, *pp, sizeof(u_long)))
513 return (1);
514
515 /* No match. */
516 return (0); 565 return (0);
517} 566}
567
568/*
569 * Return the hostname associated with the supplied address.
570 * Do a reverse lookup as well for security. If a loop cannot
571 * be found, pack the result of inet_ntoa() into the string.
572 */
573static char *
574__gethostloop(raddr)
575 u_int32_t raddr;
576{
577 static char remotehost[MAXHOSTNAMELEN];
578 struct hostent *hp;
579 struct in_addr in;
580
581 hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET);
582 if (hp == NULL)
583 return (NULL);
584
585 /*
586 * Look up the name and check that the supplied
587 * address is in the list
588 */
589 strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
590 remotehost[sizeof(remotehost) - 1] = '\0';
591 hp = gethostbyname(remotehost);
592 if (hp == NULL)
593 return (NULL);
594
595 for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++)
596 if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr)))
597 return (remotehost);
598
599 /*
600 * either the DNS adminstrator has made a configuration
601 * mistake, or someone has attempted to spoof us
602 */
603 in.s_addr = raddr;
604 syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s",
605 inet_ntoa(in), hp->h_name);
606 return (NULL);
607}
diff --git a/src/lib/libc/stdlib/realloc.3 b/src/lib/libc/net/rcmdsh.3
index 66f09b2081..fd89acee8f 100644
--- a/src/lib/libc/stdlib/realloc.3
+++ b/src/lib/libc/net/rcmdsh.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1991 The Regents of the University of California. 1.\" $OpenBSD: rcmdsh.3,v 1.3 1998/06/26 17:54:09 millert Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
@@ -29,72 +31,78 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
31.\" 33.\"
32.\" from: @(#)realloc.3 5.1 (Berkeley) 5/2/91 34.Dd Sep 1, 1996
33.\" $Id: realloc.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ 35.Dt RCMDSH 3
34.\" 36.Os OpenBSD
35.Dd May 2, 1991
36.Dt REALLOC 3
37.Os
38.Sh NAME 37.Sh NAME
39.Nm realloc 38.Nm rcmdsh
40.Nd reallocation of memory function 39.Nd return a stream to a remote command without superuser
41.Sh SYNOPSIS 40.Sh SYNOPSIS
42.Fd #include <stdlib.h> 41.Fd #include <unistd.h>
43.Ft void * 42.Ft int
44.Fn realloc "void *ptr" "size_t size" 43.Fn rcmdsh "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "char *rshprog"
45.Sh DESCRIPTION 44.Sh DESCRIPTION
46The 45The
47.Fn realloc 46.Fn rcmdsh
48function changes the size of the object pointed to by 47function
49.Fa ptr 48is used by normal users to execute a command on
50to the size specified by 49a remote machine using an authentication scheme based
51.Fa size . 50on reserved port numbers using
52The contents of the object are unchanged up to the lesser 51.Xr rshd 8
53of the new and old sizes. 52or the value of
54If the new size is larger, the value of the newly allocated portion 53.Fa rshprog
55of the object is indeterminate. 54(if non-NULL).
56If
57.Fa ptr
58is a null pointer, the
59.Fn realloc
60function behaves like the
61.Xr malloc 3
62function for the specified size.
63Otherwise, if
64.Fa ptr
65does not match a pointer earlier returned by the
66.Xr calloc 3 ,
67.Xr malloc 3 ,
68or
69.Fn realloc
70function, or if the space has been deallocated
71by a call to the
72.Xr free
73or
74.Fn realloc
75function, unpredictable and usually detrimental
76behavior will occur.
77If the space cannot be allocated, the object
78pointed to by
79.Fa ptr
80is unchanged.
81If
82.Fa size
83is zero and
84.Fa ptr
85is not a null pointer, the object it points to is freed.
86.Pp 55.Pp
87The 56The
88.Fn realloc 57.Fn rcmdsh
89function returns either a null pointer or a pointer 58function
90to the possibly moved allocated space. 59looks up the host
60.Fa *ahost
61using
62.Xr gethostbyname 3 ,
63returning \-1 if the host does not exist.
64Otherwise
65.Fa *ahost
66is set to the standard name of the host
67and a connection is established to a server
68residing at the well-known Internet port
69.Li shell/tcp
70(or whatever port is used by
71.Fa rshprog
72). The parameter
73.Fa inport
74is ignored; it is only included to provide an interface similar to
75.Xr rcmd 3 .
76.Pp
77If the connection succeeds,
78a socket in the
79.Tn UNIX
80domain of type
81.Dv SOCK_STREAM
82is returned to the caller, and given to the remote
83command as
84.Em stdin
85and
86.Em stdout ,
87and
88.Em stderr .
89.Sh DIAGNOSTICS
90The
91.Fn rcmdsh
92function
93returns a valid socket descriptor on success.
94It returns \-1 on error and prints a diagnostic message on the standard error.
91.Sh SEE ALSO 95.Sh SEE ALSO
92.Xr alloca 3 , 96.Xr rsh 1 ,
93.Xr calloc 3 , 97.Xr socketpair 2 ,
94.Xr free 3 , 98.Xr rcmd 3 ,
95.Xr malloc 3 , 99.Xr rshd 8
96.Sh STANDARDS 100.Sh BUGS
101If
102.Xr rsh 1
103gets an error a file descriptor is still returned instead of \-1.
104.Sh HISTORY
97The 105The
98.Fn realloc 106.Fn rcmdsh
99function conforms to 107function first appeared in
100.St -ansiC . 108.Ox 2.0 .
diff --git a/src/lib/libc/net/rcmdsh.c b/src/lib/libc/net/rcmdsh.c
new file mode 100644
index 0000000000..93523a4c56
--- /dev/null
+++ b/src/lib/libc/net/rcmdsh.c
@@ -0,0 +1,124 @@
1/* $OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $ */
2
3/*
4 * This is an rcmd() replacement originally by
5 * Chris Siebenmann <cks@utcc.utoronto.ca>.
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $";
10#endif /* LIBC_SCCS and not lint */
11
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <sys/wait.h>
15#include <signal.h>
16#include <errno.h>
17#include <netdb.h>
18#include <stdio.h>
19#include <string.h>
20#include <pwd.h>
21#include <paths.h>
22#include <unistd.h>
23
24/*
25 * This is a replacement rcmd() function that uses the rsh(1)
26 * program in place of a direct rcmd(3) function call so as to
27 * avoid having to be root. Note that rport is ignored.
28 */
29/* ARGSUSED */
30int
31rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog)
32 char **ahost;
33 int rport;
34 const char *locuser, *remuser, *cmd;
35 char *rshprog;
36{
37 struct hostent *hp;
38 int cpid, sp[2];
39 char *p;
40 struct passwd *pw;
41
42 /* What rsh/shell to use. */
43 if (rshprog == NULL)
44 rshprog = _PATH_RSH;
45
46 /* locuser must exist on this host. */
47 if ((pw = getpwnam(locuser)) == NULL) {
48 (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
49 return(-1);
50 }
51
52 /* Validate remote hostname. */
53 if (strcmp(*ahost, "localhost") != 0) {
54 if ((hp = gethostbyname(*ahost)) == NULL) {
55 herror(*ahost);
56 return(-1);
57 }
58 *ahost = hp->h_name;
59 }
60
61 /* Get a socketpair we'll use for stdin and stdout. */
62 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) {
63 perror("rcmdsh: socketpair");
64 return(-1);
65 }
66
67 cpid = fork();
68 if (cpid < 0) {
69 perror("rcmdsh: fork failed");
70 return(-1);
71 } else if (cpid == 0) {
72 /*
73 * Child. We use sp[1] to be stdin/stdout, and close sp[0].
74 */
75 (void) close(sp[0]);
76 if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) {
77 perror("rcmdsh: dup2 failed");
78 _exit(255);
79 }
80 /* Fork again to lose parent. */
81 cpid = fork();
82 if (cpid < 0) {
83 perror("rcmdsh: fork to lose parent failed");
84 _exit(255);
85 }
86 if (cpid > 0)
87 _exit(0);
88
89 /* In grandchild here. Become local user for rshprog. */
90 if (setuid(pw->pw_uid)) {
91 (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
92 pw->pw_uid, strerror(errno));
93 _exit(255);
94 }
95
96 /*
97 * If remote host is "localhost" and local and remote user
98 * are the same, avoid running remote shell for efficiency.
99 */
100 if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
101 if (pw->pw_shell[0] == '\0')
102 rshprog = _PATH_BSHELL;
103 else
104 rshprog = pw->pw_shell;
105 p = strrchr(rshprog, '/');
106 execlp(rshprog, p ? p+1 : rshprog, "-c", cmd,
107 (char *) NULL);
108 } else {
109 p = strrchr(rshprog, '/');
110 execlp(rshprog, p ? p+1 : rshprog, *ahost, "-l",
111 remuser, cmd, (char *) NULL);
112 }
113 (void) fprintf(stderr, "rcmdsh: execlp %s failed: %s\n",
114 rshprog, strerror(errno));
115 _exit(255);
116 } else {
117 /* Parent. close sp[1], return sp[0]. */
118 (void) close(sp[1]);
119 /* Reap child. */
120 (void) wait(NULL);
121 return(sp[0]);
122 }
123 /* NOTREACHED */
124}
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c
index 44296378cb..d209a07213 100644
--- a/src/lib/libc/net/recv.c
+++ b/src/lib/libc/net/recv.c
@@ -1,5 +1,3 @@
1/* $NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1988, 1993 2 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: recv.c,v 1.2 1996/08/19 08:29:40 tholo Exp $";
38static char sccsid[] = "@(#)recv.c 8.2 (Berkeley) 2/21/94";
39#else
40static char rcsid[] = "$NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
index 9d7bbecdda..f7a0358967 100644
--- a/src/lib/libc/net/res_comp.c
+++ b/src/lib/libc/net/res_comp.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $ */ 1/* $OpenBSD: res_comp.c,v 1.8 1997/07/09 01:08:49 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1993
5 * -
4 * Copyright (c) 1985, 1993 6 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,19 +58,26 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 61static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: res_comp.c,v 1.8 1997/07/09 01:08:49 millert Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <arpa/nameser.h>
67#include <netinet/in.h> 69#include <netinet/in.h>
68#include <resolv.h> 70#include <arpa/nameser.h>
71
69#include <stdio.h> 72#include <stdio.h>
73#include <resolv.h>
74#include <ctype.h>
70 75
71static int dn_find(); 76#include <unistd.h>
77#include <string.h>
78
79static int dn_find __P((u_char *exp_dn, u_char *msg,
80 u_char **dnptrs, u_char **lastdnptr));
72 81
73/* 82/*
74 * Expand compressed domain name 'comp_dn' to full domain name. 83 * Expand compressed domain name 'comp_dn' to full domain name.
@@ -77,23 +86,27 @@ static int dn_find();
77 * 'exp_dn' is a pointer to a buffer of size 'length' for the result. 86 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
78 * Return size of compressed name or -1 if there was an error. 87 * Return size of compressed name or -1 if there was an error.
79 */ 88 */
89int
80dn_expand(msg, eomorig, comp_dn, exp_dn, length) 90dn_expand(msg, eomorig, comp_dn, exp_dn, length)
81 const u_char *msg, *eomorig, *comp_dn; 91 const u_char *msg, *eomorig, *comp_dn;
82 u_char *exp_dn; 92 char *exp_dn;
83 int length; 93 int length;
84{ 94{
85 register u_char *cp, *dn; 95 register const u_char *cp;
96 register char *dn;
86 register int n, c; 97 register int n, c;
87 u_char *eom; 98 char *eom;
88 int len = -1, checked = 0; 99 int len = -1, checked = 0;
89 100
90 dn = exp_dn; 101 dn = exp_dn;
91 cp = (u_char *)comp_dn; 102 cp = comp_dn;
103 if (length > MAXHOSTNAMELEN-1)
104 length = MAXHOSTNAMELEN-1;
92 eom = exp_dn + length; 105 eom = exp_dn + length;
93 /* 106 /*
94 * fetch next label in domain name 107 * fetch next label in domain name
95 */ 108 */
96 while (n = *cp++) { 109 while ((n = *cp++)) {
97 /* 110 /*
98 * Check for indirection 111 * Check for indirection
99 */ 112 */
@@ -108,23 +121,23 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
108 return (-1); 121 return (-1);
109 checked += n + 1; 122 checked += n + 1;
110 while (--n >= 0) { 123 while (--n >= 0) {
111 if ((c = *cp++) == '.') { 124 if (((c = *cp++) == '.') || (c == '\\')) {
112 if (dn + n + 2 >= eom) 125 if (dn + n + 2 >= eom)
113 return (-1); 126 return (-1);
114 *dn++ = '\\'; 127 *dn++ = '\\';
115 } 128 }
116 *dn++ = c; 129 *dn++ = c;
117 if (cp >= eomorig) /* out of range */ 130 if (cp >= eomorig) /* out of range */
118 return(-1); 131 return (-1);
119 } 132 }
120 break; 133 break;
121 134
122 case INDIR_MASK: 135 case INDIR_MASK:
123 if (len < 0) 136 if (len < 0)
124 len = cp - comp_dn + 1; 137 len = cp - comp_dn + 1;
125 cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); 138 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
126 if (cp < msg || cp >= eomorig) /* out of range */ 139 if (cp < msg || cp >= eomorig) /* out of range */
127 return(-1); 140 return (-1);
128 checked += 2; 141 checked += 2;
129 /* 142 /*
130 * Check for loops in the compressed name; 143 * Check for loops in the compressed name;
@@ -157,8 +170,9 @@ dn_expand(msg, eomorig, comp_dn, exp_dn, length)
157 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' 170 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
158 * is NULL, we don't update the list. 171 * is NULL, we don't update the list.
159 */ 172 */
173int
160dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) 174dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
161 const u_char *exp_dn; 175 const char *exp_dn;
162 u_char *comp_dn, **dnptrs, **lastdnptr; 176 u_char *comp_dn, **dnptrs, **lastdnptr;
163 int length; 177 int length;
164{ 178{
@@ -170,6 +184,7 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
170 dn = (u_char *)exp_dn; 184 dn = (u_char *)exp_dn;
171 cp = comp_dn; 185 cp = comp_dn;
172 eob = cp + length; 186 eob = cp + length;
187 lpp = cpp = NULL;
173 if (dnptrs != NULL) { 188 if (dnptrs != NULL) {
174 if ((msg = *dnptrs++) != NULL) { 189 if ((msg = *dnptrs++) != NULL) {
175 for (cpp = dnptrs; *cpp != NULL; cpp++) 190 for (cpp = dnptrs; *cpp != NULL; cpp++)
@@ -235,13 +250,14 @@ dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
235/* 250/*
236 * Skip over a compressed domain name. Return the size or -1. 251 * Skip over a compressed domain name. Return the size or -1.
237 */ 252 */
253int
238__dn_skipname(comp_dn, eom) 254__dn_skipname(comp_dn, eom)
239 const u_char *comp_dn, *eom; 255 const u_char *comp_dn, *eom;
240{ 256{
241 register u_char *cp; 257 register const u_char *cp;
242 register int n; 258 register int n;
243 259
244 cp = (u_char *)comp_dn; 260 cp = comp_dn;
245 while (cp < eom && (n = *cp++)) { 261 while (cp < eom && (n = *cp++)) {
246 /* 262 /*
247 * check for indirection 263 * check for indirection
@@ -259,10 +275,19 @@ __dn_skipname(comp_dn, eom)
259 break; 275 break;
260 } 276 }
261 if (cp > eom) 277 if (cp > eom)
262 return -1; 278 return (-1);
263 return (cp - comp_dn); 279 return (cp - comp_dn);
264} 280}
265 281
282static int
283mklower(ch)
284 register int ch;
285{
286 if (isascii(ch) && isupper(ch))
287 return (tolower(ch));
288 return (ch);
289}
290
266/* 291/*
267 * Search for expanded name from a list of previously compressed names. 292 * Search for expanded name from a list of previously compressed names.
268 * Return the offset from msg if found or -1. 293 * Return the offset from msg if found or -1.
@@ -281,7 +306,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
281 for (cpp = dnptrs; cpp < lastdnptr; cpp++) { 306 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
282 dn = exp_dn; 307 dn = exp_dn;
283 sp = cp = *cpp; 308 sp = cp = *cpp;
284 while (n = *cp++) { 309 while ((n = *cp++)) {
285 /* 310 /*
286 * check for indirection 311 * check for indirection
287 */ 312 */
@@ -292,7 +317,7 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
292 goto next; 317 goto next;
293 if (*dn == '\\') 318 if (*dn == '\\')
294 dn++; 319 dn++;
295 if (*dn++ != *cp++) 320 if (mklower(*dn++) != mklower(*cp++))
296 goto next; 321 goto next;
297 } 322 }
298 if ((n = *dn++) == '\0' && *cp == '\0') 323 if ((n = *dn++) == '\0' && *cp == '\0')
@@ -301,11 +326,12 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
301 continue; 326 continue;
302 goto next; 327 goto next;
303 328
304 default: /* illegal type */
305 return (-1);
306
307 case INDIR_MASK: /* indirection */ 329 case INDIR_MASK: /* indirection */
308 cp = msg + (((n & 0x3f) << 8) | *cp); 330 cp = msg + (((n & 0x3f) << 8) | *cp);
331 break;
332
333 default: /* illegal type */
334 return (-1);
309 } 335 }
310 } 336 }
311 if (*dn == '\0') 337 if (*dn == '\0')
@@ -316,16 +342,124 @@ dn_find(exp_dn, msg, dnptrs, lastdnptr)
316} 342}
317 343
318/* 344/*
319 * Routines to insert/extract short/long's. Must account for byte 345 * Verify that a domain name uses an acceptable character set.
320 * order and non-alignment problems. This code at least has the 346 */
321 * advantage of being portable. 347
322 * 348/*
323 * used by sendmail. 349 * Note the conspicuous absence of ctype macros in these definitions. On
350 * non-ASCII hosts, we can't depend on string literals or ctype macros to
351 * tell us anything about network-format data. The rest of the BIND system
352 * is not careful about this, but for some reason, we're doing it right here.
353 */
354#define PERIOD 0x2e
355#define hyphenchar(c) ((c) == 0x2d)
356#define bslashchar(c) ((c) == 0x5c)
357#define periodchar(c) ((c) == PERIOD)
358#define asterchar(c) ((c) == 0x2a)
359#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
360 || ((c) >= 0x61 && (c) <= 0x7a))
361#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
362
363#define borderchar(c) (alphachar(c) || digitchar(c))
364#define middlechar(c) (borderchar(c) || hyphenchar(c))
365#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
366
367int
368res_hnok(dn)
369 const char *dn;
370{
371 int pch = PERIOD, ch = *dn++;
372
373 while (ch != '\0') {
374 int nch = *dn++;
375
376 if (periodchar(ch)) {
377 ;
378 } else if (periodchar(pch)) {
379 if (!borderchar(ch))
380 return (0);
381 } else if (periodchar(nch) || nch == '\0') {
382 if (!borderchar(ch))
383 return (0);
384 } else {
385 if (!middlechar(ch))
386 return (0);
387 }
388 pch = ch, ch = nch;
389 }
390 return (1);
391}
392
393/*
394 * hostname-like (A, MX, WKS) owners can have "*" as their first label
395 * but must otherwise be as a host name.
396 */
397int
398res_ownok(dn)
399 const char *dn;
400{
401 if (asterchar(dn[0])) {
402 if (periodchar(dn[1]))
403 return (res_hnok(dn+2));
404 if (dn[1] == '\0')
405 return (1);
406 }
407 return (res_hnok(dn));
408}
409
410/*
411 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
412 * label, but the rest of the name has to look like a host name.
324 */ 413 */
414int
415res_mailok(dn)
416 const char *dn;
417{
418 int ch, escaped = 0;
419
420 /* "." is a valid missing representation */
421 if (*dn == '\0')
422 return(1);
423
424 /* otherwise <label>.<hostname> */
425 while ((ch = *dn++) != '\0') {
426 if (!domainchar(ch))
427 return (0);
428 if (!escaped && periodchar(ch))
429 break;
430 if (escaped)
431 escaped = 0;
432 else if (bslashchar(ch))
433 escaped = 1;
434 }
435 if (periodchar(ch))
436 return (res_hnok(dn));
437 return(0);
438}
325 439
326u_short 440/*
441 * This function is quite liberal, since RFC 1034's character sets are only
442 * recommendations.
443 */
444int
445res_dnok(dn)
446 const char *dn;
447{
448 int ch;
449
450 while ((ch = *dn++) != '\0')
451 if (!domainchar(ch))
452 return (0);
453 return (1);
454}
455
456/*
457 * Routines to insert/extract short/long's.
458 */
459
460u_int16_t
327_getshort(msgp) 461_getshort(msgp)
328 register u_char *msgp; 462 register const u_char *msgp;
329{ 463{
330 register u_int16_t u; 464 register u_int16_t u;
331 465
@@ -333,9 +467,21 @@ _getshort(msgp)
333 return (u); 467 return (u);
334} 468}
335 469
470#ifdef NeXT
471/*
472 * nExt machines have some funky library conventions, which we must maintain.
473 */
474u_int16_t
475res_getshort(msgp)
476 register const u_char *msgp;
477{
478 return (_getshort(msgp));
479}
480#endif
481
336u_int32_t 482u_int32_t
337_getlong(msgp) 483_getlong(msgp)
338 register u_char *msgp; 484 register const u_char *msgp;
339{ 485{
340 register u_int32_t u; 486 register u_int32_t u;
341 487
@@ -345,7 +491,7 @@ _getlong(msgp)
345 491
346void 492void
347#if defined(__STDC__) || defined(__cplusplus) 493#if defined(__STDC__) || defined(__cplusplus)
348__putshort(register u_int16_t s, register u_char *msgp) 494__putshort(register u_int16_t s, register u_char *msgp) /* must match proto */
349#else 495#else
350__putshort(s, msgp) 496__putshort(s, msgp)
351 register u_int16_t s; 497 register u_int16_t s;
diff --git a/src/lib/libc/net/res_data.c b/src/lib/libc/net/res_data.c
new file mode 100644
index 0000000000..b0d19c36bb
--- /dev/null
+++ b/src/lib/libc/net/res_data.c
@@ -0,0 +1,117 @@
1/* $OpenBSD: res_data.c,v 1.1 1997/03/13 19:07:36 downsj Exp $ */
2
3/*
4 * ++Copyright++ 1995
5 * -
6 * Copyright (c) 1995
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 * -
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 * -
55 * --Copyright--
56 */
57
58#if defined(LIBC_SCCS) && !defined(lint)
59#if 0
60static char rcsid[] = "$From: res_data.c,v 8.2 1996/08/05 08:31:35 vixie Exp $";
61#else
62static char rcsid[] = "$OpenBSD: res_data.c,v 1.1 1997/03/13 19:07:36 downsj Exp $";
63#endif
64#endif /* LIBC_SCCS and not lint */
65
66#include <sys/types.h>
67#include <sys/param.h>
68#include <sys/socket.h>
69#include <sys/time.h>
70#include <netinet/in.h>
71#include <arpa/inet.h>
72#include <arpa/nameser.h>
73
74#include <stdio.h>
75#include <ctype.h>
76#include <resolv.h>
77#include <unistd.h>
78#include <stdlib.h>
79#include <string.h>
80
81const char *_res_opcodes[] = {
82 "QUERY",
83 "IQUERY",
84 "CQUERYM",
85 "CQUERYU", /* experimental */
86 "NOTIFY", /* experimental */
87 "5",
88 "6",
89 "7",
90 "8",
91 "UPDATEA",
92 "UPDATED",
93 "UPDATEDA",
94 "UPDATEM",
95 "UPDATEMA",
96 "ZONEINIT",
97 "ZONEREF",
98};
99
100const char *_res_resultcodes[] = {
101 "NOERROR",
102 "FORMERR",
103 "SERVFAIL",
104 "NXDOMAIN",
105 "NOTIMP",
106 "REFUSED",
107 "6",
108 "7",
109 "8",
110 "9",
111 "10",
112 "11",
113 "12",
114 "13",
115 "14",
116 "NOCHANGE",
117};
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c
index d841293f18..c2725395a0 100644
--- a/src/lib/libc/net/res_debug.c
+++ b/src/lib/libc/net/res_debug.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $ */ 1/* $OpenBSD: res_debug.c,v 1.9 1998/03/19 00:30:06 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1990, 1993
5 * -
4 * Copyright (c) 1985, 1990, 1993 6 * Copyright (c) 1985, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -50,174 +52,185 @@
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE. 53 * SOFTWARE.
52 * - 54 * -
55 * Portions Copyright (c) 1995 by International Business Machines, Inc.
56 *
57 * International Business Machines, Inc. (hereinafter called IBM) grants
58 * permission under its copyrights to use, copy, modify, and distribute this
59 * Software with or without fee, provided that the above copyright notice and
60 * all paragraphs of this notice appear in all copies, and that the name of IBM
61 * not be used in connection with the marketing of any product incorporating
62 * the Software or modifications thereof, without specific, written prior
63 * permission.
64 *
65 * To the extent it has a right to do so, IBM grants an immunity from suit
66 * under its patents, if any, for the use, sale or manufacture of products to
67 * the extent that such products are used for performing Domain Name System
68 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
69 * granted for any product per se or for any other function of any product.
70 *
71 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
72 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
73 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
74 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
75 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
76 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
53 * --Copyright-- 77 * --Copyright--
54 */ 78 */
55 79
56#if defined(LIBC_SCCS) && !defined(lint) 80#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 81#if 0
58static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; 82static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93";
83static char rcsid[] = "$From: res_debug.c,v 8.19 1996/11/26 10:11:23 vixie Exp $";
59#else 84#else
60static char rcsid[] = "$NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $"; 85static char rcsid[] = "$OpenBSD: res_debug.c,v 1.9 1998/03/19 00:30:06 millert Exp $";
61#endif 86#endif
62#endif /* LIBC_SCCS and not lint */ 87#endif /* LIBC_SCCS and not lint */
63 88
64#include <sys/param.h> 89#include <sys/param.h>
90#include <sys/types.h>
91#include <sys/socket.h>
65#include <netinet/in.h> 92#include <netinet/in.h>
66#include <arpa/inet.h> 93#include <arpa/inet.h>
67#include <arpa/nameser.h> 94#include <arpa/nameser.h>
95
96#include <ctype.h>
97#include <netdb.h>
68#include <resolv.h> 98#include <resolv.h>
69#include <stdio.h> 99#include <stdio.h>
70#include <string.h> 100#include <time.h>
71 101
72void __fp_query(); 102#include <stdlib.h>
73char *__p_class(), *__p_time(), *__p_type(); 103#include <string.h>
74char *p_cdname(), *p_fqname(), *p_rr();
75static char *p_option __P((u_int32_t));
76
77char *_res_opcodes[] = {
78 "QUERY",
79 "IQUERY",
80 "CQUERYM",
81 "CQUERYU",
82 "4",
83 "5",
84 "6",
85 "7",
86 "8",
87 "UPDATEA",
88 "UPDATED",
89 "UPDATEDA",
90 "UPDATEM",
91 "UPDATEMA",
92 "ZONEINIT",
93 "ZONEREF",
94};
95
96char *_res_resultcodes[] = {
97 "NOERROR",
98 "FORMERR",
99 "SERVFAIL",
100 "NXDOMAIN",
101 "NOTIMP",
102 "REFUSED",
103 "6",
104 "7",
105 "8",
106 "9",
107 "10",
108 "11",
109 "12",
110 "13",
111 "14",
112 "NOCHANGE",
113};
114 104
115static char retbuf[16]; 105extern const char *_res_opcodes[];
106extern const char *_res_resultcodes[];
116 107
117static char * 108/* XXX: we should use getservbyport() instead. */
109static const char *
118dewks(wks) 110dewks(wks)
119 int wks; 111 int wks;
120{ 112{
113 static char nbuf[20];
114
121 switch (wks) { 115 switch (wks) {
122 case 5: return("rje"); 116 case 5: return "rje";
123 case 7: return("echo"); 117 case 7: return "echo";
124 case 9: return("discard"); 118 case 9: return "discard";
125 case 11: return("systat"); 119 case 11: return "systat";
126 case 13: return("daytime"); 120 case 13: return "daytime";
127 case 15: return("netstat"); 121 case 15: return "netstat";
128 case 17: return("qotd"); 122 case 17: return "qotd";
129 case 19: return("chargen"); 123 case 19: return "chargen";
130 case 20: return("ftp-data"); 124 case 20: return "ftp-data";
131 case 21: return("ftp"); 125 case 21: return "ftp";
132 case 23: return("telnet"); 126 case 23: return "telnet";
133 case 25: return("smtp"); 127 case 25: return "smtp";
134 case 37: return("time"); 128 case 37: return "time";
135 case 39: return("rlp"); 129 case 39: return "rlp";
136 case 42: return("name"); 130 case 42: return "name";
137 case 43: return("whois"); 131 case 43: return "whois";
138 case 53: return("domain"); 132 case 53: return "domain";
139 case 57: return("apts"); 133 case 57: return "apts";
140 case 59: return("apfs"); 134 case 59: return "apfs";
141 case 67: return("bootps"); 135 case 67: return "bootps";
142 case 68: return("bootpc"); 136 case 68: return "bootpc";
143 case 69: return("tftp"); 137 case 69: return "tftp";
144 case 77: return("rje"); 138 case 77: return "rje";
145 case 79: return("finger"); 139 case 79: return "finger";
146 case 87: return("link"); 140 case 87: return "link";
147 case 95: return("supdup"); 141 case 95: return "supdup";
148 case 100: return("newacct"); 142 case 100: return "newacct";
149 case 101: return("hostnames"); 143 case 101: return "hostnames";
150 case 102: return("iso-tsap"); 144 case 102: return "iso-tsap";
151 case 103: return("x400"); 145 case 103: return "x400";
152 case 104: return("x400-snd"); 146 case 104: return "x400-snd";
153 case 105: return("csnet-ns"); 147 case 105: return "csnet-ns";
154 case 109: return("pop-2"); 148 case 109: return "pop-2";
155 case 111: return("sunrpc"); 149 case 111: return "sunrpc";
156 case 113: return("auth"); 150 case 113: return "auth";
157 case 115: return("sftp"); 151 case 115: return "sftp";
158 case 117: return("uucp-path"); 152 case 117: return "uucp-path";
159 case 119: return("nntp"); 153 case 119: return "nntp";
160 case 121: return("erpc"); 154 case 121: return "erpc";
161 case 123: return("ntp"); 155 case 123: return "ntp";
162 case 133: return("statsrv"); 156 case 133: return "statsrv";
163 case 136: return("profile"); 157 case 136: return "profile";
164 case 144: return("NeWS"); 158 case 144: return "NeWS";
165 case 161: return("snmp"); 159 case 161: return "snmp";
166 case 162: return("snmp-trap"); 160 case 162: return "snmp-trap";
167 case 170: return("print-srv"); 161 case 170: return "print-srv";
168 default: (void) sprintf(retbuf, "%d", wks); return(retbuf); 162 default: (void) sprintf(nbuf, "%d", wks); return (nbuf);
169 } 163 }
170} 164}
171 165
172static char * 166/* XXX: we should use getprotobynumber() instead. */
167static const char *
173deproto(protonum) 168deproto(protonum)
174 int protonum; 169 int protonum;
175{ 170{
171 static char nbuf[20];
172
176 switch (protonum) { 173 switch (protonum) {
177 case 1: return("icmp"); 174 case 1: return "icmp";
178 case 2: return("igmp"); 175 case 2: return "igmp";
179 case 3: return("ggp"); 176 case 3: return "ggp";
180 case 5: return("st"); 177 case 5: return "st";
181 case 6: return("tcp"); 178 case 6: return "tcp";
182 case 7: return("ucl"); 179 case 7: return "ucl";
183 case 8: return("egp"); 180 case 8: return "egp";
184 case 9: return("igp"); 181 case 9: return "igp";
185 case 11: return("nvp-II"); 182 case 11: return "nvp-II";
186 case 12: return("pup"); 183 case 12: return "pup";
187 case 16: return("chaos"); 184 case 16: return "chaos";
188 case 17: return("udp"); 185 case 17: return "udp";
189 default: (void) sprintf(retbuf, "%d", protonum); return(retbuf); 186 default: (void) sprintf(nbuf, "%d", protonum); return (nbuf);
190 } 187 }
191} 188}
192 189
193static char * 190static const u_char *
194do_rrset(msg, cp, cnt, pflag, file, hs) 191do_rrset(msg, len, cp, cnt, pflag, file, hs)
195 int cnt, pflag; 192 int cnt, pflag, len;
196 char *cp,*msg, *hs; 193 const u_char *cp, *msg;
194 const char *hs;
197 FILE *file; 195 FILE *file;
198{ 196{
199 int n; 197 int n;
200 int sflag; 198 int sflag;
199
201 /* 200 /*
202 * Print answer records 201 * Print answer records.
203 */ 202 */
204 sflag = (_res.pfcode & pflag); 203 sflag = (_res.pfcode & pflag);
205 if (n = ntohs(cnt)) { 204 if ((n = ntohs(cnt))) {
206 if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) 205 if ((!_res.pfcode) ||
206 ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
207 fprintf(file, hs); 207 fprintf(file, hs);
208 while (--n >= 0) { 208 while (--n >= 0) {
209 cp = p_rr(cp, msg, file); 209 if ((!_res.pfcode) || sflag) {
210 if ((cp-msg) > PACKETSZ) 210 cp = p_rr(cp, msg, file);
211 } else {
212 unsigned int dlen;
213 cp += __dn_skipname(cp, cp + MAXCDNAME);
214 cp += INT16SZ;
215 cp += INT16SZ;
216 cp += INT32SZ;
217 dlen = _getshort((u_char*)cp);
218 cp += INT16SZ;
219 cp += dlen;
220 }
221 if ((cp - msg) > len)
211 return (NULL); 222 return (NULL);
212 } 223 }
213 if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) 224 if ((!_res.pfcode) ||
225 ((sflag) && (_res.pfcode & RES_PRF_HEAD1)))
214 putc('\n', file); 226 putc('\n', file);
215 } 227 }
216 return(cp); 228 return (cp);
217} 229}
218 230
231void
219__p_query(msg) 232__p_query(msg)
220 char *msg; 233 const u_char *msg;
221{ 234{
222 __fp_query(msg, stdout); 235 __fp_query(msg, stdout);
223} 236}
@@ -231,15 +244,14 @@ __fp_resstat(statp, file)
231 struct __res_state *statp; 244 struct __res_state *statp;
232 FILE *file; 245 FILE *file;
233{ 246{
234 int bit; 247 register u_long mask;
235 248
236 fprintf(file, ";; res options:"); 249 fprintf(file, ";; res options:");
237 if (!statp) 250 if (!statp)
238 statp = &_res; 251 statp = &_res;
239 for (bit = 0; bit < 32; bit++) { /* XXX 32 - bad assumption! */ 252 for (mask = 1; mask != 0; mask <<= 1)
240 if (statp->options & (1<<bit)) 253 if (statp->options & mask)
241 fprintf(file, " %s", p_option(1<<bit)); 254 fprintf(file, " %s", p_option(mask));
242 }
243 putc('\n', file); 255 putc('\n', file);
244} 256}
245 257
@@ -248,109 +260,151 @@ __fp_resstat(statp, file)
248 * This is intended to be primarily a debugging routine. 260 * This is intended to be primarily a debugging routine.
249 */ 261 */
250void 262void
251__fp_query(msg,file) 263__fp_nquery(msg, len, file)
252 char *msg; 264 const u_char *msg;
265 int len;
253 FILE *file; 266 FILE *file;
254{ 267{
255 register char *cp; 268 register const u_char *cp, *endMark;
256 register HEADER *hp; 269 register const HEADER *hp;
257 register int n; 270 register int n;
258 271
272 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
273 return;
274
275#define TruncTest(x) if (x > endMark) goto trunc
276#define ErrorTest(x) if (x == NULL) goto error
277
259 /* 278 /*
260 * Print header fields. 279 * Print header fields.
261 */ 280 */
262 hp = (HEADER *)msg; 281 hp = (HEADER *)msg;
263 cp = msg + sizeof(HEADER); 282 cp = msg + HFIXEDSZ;
283 endMark = msg + len;
264 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) { 284 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) {
265 fprintf(file,";; ->>HEADER<<- opcode: %s, status: %s, id: %d", 285 fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %d",
266 _res_opcodes[hp->opcode], 286 _res_opcodes[hp->opcode],
267 _res_resultcodes[hp->rcode], 287 _res_resultcodes[hp->rcode],
268 ntohs(hp->id)); 288 ntohs(hp->id));
269 putc('\n', file); 289 putc('\n', file);
270 } 290 }
271 putc(';', file); 291 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX))
292 putc(';', file);
272 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) { 293 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) {
273 fprintf(file,"; flags:"); 294 fprintf(file, "; flags:");
274 if (hp->qr) 295 if (hp->qr)
275 fprintf(file," qr"); 296 fprintf(file, " qr");
276 if (hp->aa) 297 if (hp->aa)
277 fprintf(file," aa"); 298 fprintf(file, " aa");
278 if (hp->tc) 299 if (hp->tc)
279 fprintf(file," tc"); 300 fprintf(file, " tc");
280 if (hp->rd) 301 if (hp->rd)
281 fprintf(file," rd"); 302 fprintf(file, " rd");
282 if (hp->ra) 303 if (hp->ra)
283 fprintf(file," ra"); 304 fprintf(file, " ra");
284 if (hp->pr) 305 if (hp->unused)
285 fprintf(file," pr"); 306 fprintf(file, " UNUSED-BIT-ON");
307 if (hp->ad)
308 fprintf(file, " ad");
309 if (hp->cd)
310 fprintf(file, " cd");
286 } 311 }
287 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) { 312 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) {
288 fprintf(file,"; Ques: %d", ntohs(hp->qdcount)); 313 fprintf(file, "; Ques: %d", ntohs(hp->qdcount));
289 fprintf(file,", Ans: %d", ntohs(hp->ancount)); 314 fprintf(file, ", Ans: %d", ntohs(hp->ancount));
290 fprintf(file,", Auth: %d", ntohs(hp->nscount)); 315 fprintf(file, ", Auth: %d", ntohs(hp->nscount));
291 fprintf(file,", Addit: %d\n", ntohs(hp->arcount)); 316 fprintf(file, ", Addit: %d", ntohs(hp->arcount));
292 } 317 }
293#if 0 318 if ((!_res.pfcode) || (_res.pfcode &
294 if (_res.pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1)) { 319 (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) {
295 putc('\n',file); 320 putc('\n',file);
296 } 321 }
297#endif
298 /* 322 /*
299 * Print question records. 323 * Print question records.
300 */ 324 */
301 if (n = ntohs(hp->qdcount)) { 325 if ((n = ntohs(hp->qdcount))) {
302 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) 326 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
303 fprintf(file,";; QUESTIONS:\n"); 327 fprintf(file, ";; QUESTIONS:\n");
304 while (--n >= 0) { 328 while (--n >= 0) {
305 fprintf(file,";;\t"); 329 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
306 cp = p_cdname(cp, msg, file); 330 fprintf(file, ";;\t");
307 if (cp == NULL) 331 TruncTest(cp);
308 return; 332 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
333 cp = p_cdnname(cp, msg, len, file);
334 else {
335 int n;
336 char name[MAXDNAME];
337
338 if ((n = dn_expand(msg, msg+len, cp, name,
339 sizeof name)) < 0)
340 cp = NULL;
341 else
342 cp += n;
343 }
344 ErrorTest(cp);
345 TruncTest(cp);
309 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) 346 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
310 fprintf(file, ", type = %s", 347 fprintf(file, ", type = %s",
311 __p_type(_getshort(cp))); 348 __p_type(_getshort((u_char*)cp)));
312 cp += sizeof(u_int16_t); 349 cp += INT16SZ;
350 TruncTest(cp);
313 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) 351 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
314 fprintf(file, ", class = %s\n\n", 352 fprintf(file, ", class = %s\n",
315 __p_class(_getshort(cp))); 353 __p_class(_getshort((u_char*)cp)));
316 cp += sizeof(u_int16_t); 354 cp += INT16SZ;
355 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES))
356 putc('\n', file);
317 } 357 }
318 } 358 }
319 /* 359 /*
320 * Print authoritative answer records 360 * Print authoritative answer records
321 */ 361 */
322 cp = do_rrset(msg, cp, hp->ancount, RES_PRF_ANS, file, 362 TruncTest(cp);
363 cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file,
323 ";; ANSWERS:\n"); 364 ";; ANSWERS:\n");
324 if (cp == NULL) 365 ErrorTest(cp);
325 return;
326 366
327 /* 367 /*
328 * print name server records 368 * print name server records
329 */ 369 */
330 cp = do_rrset(msg, cp, hp->nscount, RES_PRF_AUTH, file, 370 TruncTest(cp);
371 cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file,
331 ";; AUTHORITY RECORDS:\n"); 372 ";; AUTHORITY RECORDS:\n");
332 if (!cp) 373 ErrorTest(cp);
333 return;
334 374
375 TruncTest(cp);
335 /* 376 /*
336 * print additional records 377 * print additional records
337 */ 378 */
338 cp = do_rrset(msg, cp, hp->arcount, RES_PRF_ADD, file, 379 cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file,
339 ";; ADDITIONAL RECORDS:\n"); 380 ";; ADDITIONAL RECORDS:\n");
340 if (!cp) 381 ErrorTest(cp);
341 return; 382 return;
383 trunc:
384 fprintf(file, "\n;; ...truncated\n");
385 return;
386 error:
387 fprintf(file, "\n;; ...malformed\n");
342} 388}
343 389
344char * 390void
345p_cdname(cp, msg, file) 391__fp_query(msg, file)
346 char *cp, *msg; 392 const u_char *msg;
393 FILE *file;
394{
395 fp_nquery(msg, PACKETSZ, file);
396}
397
398const u_char *
399__p_cdnname(cp, msg, len, file)
400 const u_char *cp, *msg;
401 int len;
347 FILE *file; 402 FILE *file;
348{ 403{
349 char name[MAXDNAME]; 404 char name[MAXDNAME];
350 int n; 405 int n;
351 406
352 if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, 407 if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0)
353 (u_char *)cp, (u_char *)name, sizeof(name))) < 0)
354 return (NULL); 408 return (NULL);
355 if (name[0] == '\0') 409 if (name[0] == '\0')
356 putc('.', file); 410 putc('.', file);
@@ -359,54 +413,94 @@ p_cdname(cp, msg, file)
359 return (cp + n); 413 return (cp + n);
360} 414}
361 415
362char * 416const u_char *
363p_fqname(cp, msg, file) 417__p_cdname(cp, msg, file)
364 char *cp, *msg; 418 const u_char *cp, *msg;
365 FILE *file; 419 FILE *file;
366{ 420{
367 char name[MAXDNAME]; 421 return (p_cdnname(cp, msg, PACKETSZ, file));
368 int n, len; 422}
369 423
370 if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, 424
371 (u_char *)cp, (u_char *)name, sizeof(name))) < 0) 425/* Return a fully-qualified domain name from a compressed name (with
426 length supplied). */
427
428const u_char *
429__p_fqnname(cp, msg, msglen, name, namelen)
430 const u_char *cp, *msg;
431 int msglen;
432 char *name;
433 int namelen;
434{
435 int n, newlen;
436
437 if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0)
372 return (NULL); 438 return (NULL);
373 if (name[0] == '\0') { 439 newlen = strlen (name);
374 putc('.', file); 440 if (newlen == 0 || name[newlen - 1] != '.') {
375 } else { 441 if (newlen+1 >= namelen) /* Lack space for final dot */
376 fputs(name, file); 442 return (NULL);
377 if (name[strlen(name) - 1] != '.') 443 else
378 putc('.', file); 444 strcpy(name + newlen, ".");
379 } 445 }
380 return (cp + n); 446 return (cp + n);
381} 447}
382 448
449/* XXX: the rest of these functions need to become length-limited, too. (vix)
450 */
451
452const u_char *
453__p_fqname(cp, msg, file)
454 const u_char *cp, *msg;
455 FILE *file;
456{
457 char name[MAXDNAME];
458 const u_char *n;
459
460 n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name);
461 if (n == NULL)
462 return (NULL);
463 fputs(name, file);
464 return (n);
465}
466
383/* 467/*
384 * Print resource record fields in human readable form. 468 * Print resource record fields in human readable form.
385 */ 469 */
386char * 470const u_char *
387p_rr(cp, msg, file) 471__p_rr(cp, msg, file)
388 char *cp, *msg; 472 const u_char *cp, *msg;
389 FILE *file; 473 FILE *file;
390{ 474{
391 int type, class, dlen, n, c; 475 int type, class, dlen, n, c;
392 struct in_addr inaddr; 476 struct in_addr inaddr;
393 char *cp1, *cp2; 477 const u_char *cp1, *cp2;
394 u_int32_t tmpttl, t; 478 u_int32_t tmpttl, t;
395 int lcnt; 479 int lcnt;
480 u_int16_t keyflags;
481 char rrname[MAXDNAME]; /* The fqdn of this RR */
482 char base64_key[MAX_KEY_BASE64];
396 483
397 if ((cp = p_fqname(cp, msg, file)) == NULL) 484 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
485 h_errno = NETDB_INTERNAL;
486 return (NULL);
487 }
488 cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname);
489 if (!cp)
398 return (NULL); /* compression error */ 490 return (NULL); /* compression error */
399 type = _getshort(cp); 491 fputs(rrname, file);
400 cp += sizeof(u_int16_t); 492
401 class = _getshort(cp); 493 type = _getshort((u_char*)cp);
402 cp += sizeof(u_int16_t); 494 cp += INT16SZ;
403 tmpttl = _getlong(cp); 495 class = _getshort((u_char*)cp);
404 cp += sizeof(u_int32_t); 496 cp += INT16SZ;
405 dlen = _getshort(cp); 497 tmpttl = _getlong((u_char*)cp);
406 cp += sizeof(u_int16_t); 498 cp += INT32SZ;
499 dlen = _getshort((u_char*)cp);
500 cp += INT16SZ;
407 cp1 = cp; 501 cp1 = cp;
408 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID)) 502 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID))
409 fprintf(file, "\t%lu", tmpttl); 503 fprintf(file, "\t%lu", (u_long)tmpttl);
410 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS)) 504 if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS))
411 fprintf(file, "\t%s", __p_class(class)); 505 fprintf(file, "\t%s", __p_class(class));
412 fprintf(file, "\t%s", __p_type(type)); 506 fprintf(file, "\t%s", __p_type(type));
@@ -418,21 +512,21 @@ p_rr(cp, msg, file)
418 switch (class) { 512 switch (class) {
419 case C_IN: 513 case C_IN:
420 case C_HS: 514 case C_HS:
421 bcopy(cp, (char *)&inaddr, sizeof(inaddr)); 515 bcopy(cp, (char *)&inaddr, INADDRSZ);
422 if (dlen == 4) { 516 if (dlen == 4) {
423 fprintf(file,"\t%s", inet_ntoa(inaddr)); 517 fprintf(file, "\t%s", inet_ntoa(inaddr));
424 cp += dlen; 518 cp += dlen;
425 } else if (dlen == 7) { 519 } else if (dlen == 7) {
426 char *address; 520 char *address;
427 u_char protocol; 521 u_char protocol;
428 u_short port; 522 in_port_t port;
429 523
430 address = inet_ntoa(inaddr); 524 address = inet_ntoa(inaddr);
431 cp += sizeof(inaddr); 525 cp += INADDRSZ;
432 protocol = *(u_char*)cp; 526 protocol = *(u_char*)cp;
433 cp += sizeof(u_char); 527 cp += sizeof (u_char);
434 port = _getshort(cp); 528 port = _getshort((u_char*)cp);
435 cp += sizeof(u_int16_t); 529 cp += INT16SZ;
436 fprintf(file, "\t%s\t; proto %d, port %d", 530 fprintf(file, "\t%s\t; proto %d, port %d",
437 address, protocol, port); 531 address, protocol, port);
438 } 532 }
@@ -448,98 +542,205 @@ p_rr(cp, msg, file)
448 case T_NS: 542 case T_NS:
449 case T_PTR: 543 case T_PTR:
450 putc('\t', file); 544 putc('\t', file);
451 cp = p_fqname(cp, msg, file); 545 if ((cp = p_fqname(cp, msg, file)) == NULL)
546 return (NULL);
452 break; 547 break;
453 548
454 case T_HINFO: 549 case T_HINFO:
455 if (n = *cp++) { 550 case T_ISDN:
456 fprintf(file,"\t%.*s", n, cp); 551 cp2 = cp + dlen;
457 cp += n; 552 (void) fputs("\t\"", file);
553 if ((n = (unsigned char) *cp++) != 0) {
554 for (c = n; c > 0 && cp < cp2; c--) {
555 if (strchr("\n\"\\", *cp))
556 (void) putc('\\', file);
557 (void) putc(*cp++, file);
558 }
458 } 559 }
459 if (n = *cp++) { 560 putc('"', file);
460 fprintf(file,"\t%.*s", n, cp); 561 if (cp < cp2 && (n = (unsigned char) *cp++) != 0) {
461 cp += n; 562 (void) fputs ("\t\"", file);
563 for (c = n; c > 0 && cp < cp2; c--) {
564 if (strchr("\n\"\\", *cp))
565 (void) putc('\\', file);
566 (void) putc(*cp++, file);
567 }
568 putc('"', file);
569 } else if (type == T_HINFO) {
570 (void) fputs("\"?\"", file);
571 fprintf(file, "\n;; *** Warning *** OS-type missing");
462 } 572 }
463 break; 573 break;
464 574
465 case T_SOA: 575 case T_SOA:
466 putc('\t', file); 576 putc('\t', file);
467 cp = p_fqname(cp, msg, file); /* origin */ 577 if ((cp = p_fqname(cp, msg, file)) == NULL)
578 return (NULL);
468 putc(' ', file); 579 putc(' ', file);
469 cp = p_fqname(cp, msg, file); /* mail addr */ 580 if ((cp = p_fqname(cp, msg, file)) == NULL)
581 return (NULL);
470 fputs(" (\n", file); 582 fputs(" (\n", file);
471 t = _getlong(cp); cp += sizeof(u_int32_t); 583 t = _getlong((u_char*)cp); cp += INT32SZ;
472 fprintf(file,"\t\t\t%lu\t; serial\n", t); 584 fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t);
473 t = _getlong(cp); cp += sizeof(u_int32_t); 585 t = _getlong((u_char*)cp); cp += INT32SZ;
474 fprintf(file,"\t\t\t%lu\t; refresh (%s)\n", t, __p_time(t)); 586 fprintf(file, "\t\t\t%lu\t; refresh (%s)\n",
475 t = _getlong(cp); cp += sizeof(u_int32_t); 587 (u_long)t, __p_time(t));
476 fprintf(file,"\t\t\t%lu\t; retry (%s)\n", t, __p_time(t)); 588 t = _getlong((u_char*)cp); cp += INT32SZ;
477 t = _getlong(cp); cp += sizeof(u_int32_t); 589 fprintf(file, "\t\t\t%lu\t; retry (%s)\n",
478 fprintf(file,"\t\t\t%lu\t; expire (%s)\n", t, __p_time(t)); 590 (u_long)t, __p_time(t));
479 t = _getlong(cp); cp += sizeof(u_int32_t); 591 t = _getlong((u_char*)cp); cp += INT32SZ;
480 fprintf(file,"\t\t\t%lu )\t; minimum (%s)", t, __p_time(t)); 592 fprintf(file, "\t\t\t%lu\t; expire (%s)\n",
593 (u_long)t, __p_time(t));
594 t = _getlong((u_char*)cp); cp += INT32SZ;
595 fprintf(file, "\t\t\t%lu )\t; minimum (%s)",
596 (u_long)t, __p_time(t));
481 break; 597 break;
482 598
483 case T_MX: 599 case T_MX:
484 case T_AFSDB: 600 case T_AFSDB:
485 fprintf(file,"\t%d ", _getshort(cp)); 601 case T_RT:
486 cp += sizeof(u_int16_t); 602 fprintf(file, "\t%d ", _getshort((u_char*)cp));
487 cp = p_fqname(cp, msg, file); 603 cp += INT16SZ;
604 if ((cp = p_fqname(cp, msg, file)) == NULL)
605 return (NULL);
606 break;
607
608 case T_PX:
609 fprintf(file, "\t%d ", _getshort((u_char*)cp));
610 cp += INT16SZ;
611 if ((cp = p_fqname(cp, msg, file)) == NULL)
612 return (NULL);
613 putc(' ', file);
614 if ((cp = p_fqname(cp, msg, file)) == NULL)
615 return (NULL);
488 break; 616 break;
489 617
490 case T_TXT: 618 case T_X25:
619 cp2 = cp + dlen;
491 (void) fputs("\t\"", file); 620 (void) fputs("\t\"", file);
621 if ((n = (unsigned char) *cp++) != 0) {
622 for (c = n; c > 0 && cp < cp2; c--) {
623 if (strchr("\n\"\\", *cp))
624 (void) putc('\\', file);
625 (void) putc(*cp++, file);
626 }
627 }
628 putc('"', file);
629 break;
630
631 case T_TXT:
632 (void) putc('\t', file);
492 cp2 = cp1 + dlen; 633 cp2 = cp1 + dlen;
493 while (cp < cp2) { 634 while (cp < cp2) {
494 if (n = (unsigned char) *cp++) { 635 putc('"', file);
495 for (c = n; c > 0 && cp < cp2; c--) 636 if ((n = (unsigned char) *cp++)) {
496 if (*cp == '\n') { 637 for (c = n; c > 0 && cp < cp2; c--) {
497 (void) putc('\\', file); 638 if (strchr("\n\"\\", *cp))
498 (void) putc(*cp++, file); 639 (void) putc('\\', file);
499 } else 640 (void) putc(*cp++, file);
500 (void) putc(*cp++, file); 641 }
501 } 642 }
643 putc('"', file);
644 if (cp < cp2)
645 putc(' ', file);
502 } 646 }
503 putc('"', file); 647 break;
504 break; 648
649 case T_NSAP:
650 (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL));
651 cp += dlen;
652 break;
653
654 case T_AAAA: {
655 char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
656
657 fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t));
658 cp += dlen;
659 break;
660 }
661
662 case T_LOC: {
663 char t[255];
664
665 fprintf(file, "\t%s", loc_ntoa(cp, t));
666 cp += dlen;
667 break;
668 }
669
670 case T_NAPTR: {
671 u_int order, preference;
672
673 order = _getshort(cp); cp += INT16SZ;
674 preference = _getshort(cp); cp += INT16SZ;
675 fprintf(file, "\t%u %u ",order, preference);
676 /* Flags */
677 n = *cp++;
678 fprintf(file,"\"%.*s\" ", (int)n, cp);
679 cp += n;
680 /* Service */
681 n = *cp++;
682 fprintf(file,"\"%.*s\" ", (int)n, cp);
683 cp += n;
684 /* Regexp */
685 n = *cp++;
686 fprintf(file,"\"%.*s\" ", (int)n, cp);
687 cp += n;
688 if ((cp = p_fqname(cp, msg, file)) == NULL)
689 return (NULL);
690 break;
691 }
692
693 case T_SRV: {
694 u_int priority, weight, port;
695
696 priority = _getshort(cp); cp += INT16SZ;
697 weight = _getshort(cp); cp += INT16SZ;
698 port = _getshort(cp); cp += INT16SZ;
699 fprintf(file, "\t%u %u %u ", priority, weight, port);
700 if ((cp = p_fqname(cp, msg, file)) == NULL)
701 return (NULL);
702 break;
703 }
505 704
506 case T_MINFO: 705 case T_MINFO:
507 case T_RP: 706 case T_RP:
508 putc('\t', file); 707 putc('\t', file);
509 cp = p_fqname(cp, msg, file); 708 if ((cp = p_fqname(cp, msg, file)) == NULL)
709 return (NULL);
510 putc(' ', file); 710 putc(' ', file);
511 cp = p_fqname(cp, msg, file); 711 if ((cp = p_fqname(cp, msg, file)) == NULL)
712 return (NULL);
512 break; 713 break;
513 714
514 case T_UINFO: 715 case T_UINFO:
515 putc('\t', file); 716 putc('\t', file);
516 fputs(cp, file); 717 fputs((char *)cp, file);
517 cp += dlen; 718 cp += dlen;
518 break; 719 break;
519 720
520 case T_UID: 721 case T_UID:
521 case T_GID: 722 case T_GID:
522 if (dlen == 4) { 723 if (dlen == 4) {
523 fprintf(file,"\t%u", _getlong(cp)); 724 fprintf(file, "\t%u", _getlong((u_char*)cp));
524 cp += sizeof(int32_t); 725 cp += INT32SZ;
525 } 726 }
526 break; 727 break;
527 728
528 case T_WKS: 729 case T_WKS:
529 if (dlen < sizeof(u_int32_t) + 1) 730 if (dlen < INT32SZ + 1)
530 break; 731 break;
531 bcopy(cp, (char *)&inaddr, sizeof(inaddr)); 732 bcopy(cp, (char *)&inaddr, INADDRSZ);
532 cp += sizeof(u_int32_t); 733 cp += INT32SZ;
533 fprintf(file, "\t%s %s ( ", 734 fprintf(file, "\t%s %s ( ",
534 inet_ntoa(inaddr), 735 inet_ntoa(inaddr),
535 deproto((int) *cp)); 736 deproto((int) *cp));
536 cp += sizeof(u_char); 737 cp += sizeof (u_char);
537 n = 0; 738 n = 0;
538 lcnt = 0; 739 lcnt = 0;
539 while (cp < cp1 + dlen) { 740 while (cp < cp1 + dlen) {
540 c = *cp++; 741 c = *cp++;
541 do { 742 do {
542 if (c & 0200) { 743 if (c & 0200) {
543 if (lcnt == 0) { 744 if (lcnt == 0) {
544 fputs("\n\t\t\t", file); 745 fputs("\n\t\t\t", file);
545 lcnt = 5; 746 lcnt = 5;
@@ -548,17 +749,83 @@ p_rr(cp, msg, file)
548 putc(' ', file); 749 putc(' ', file);
549 lcnt--; 750 lcnt--;
550 } 751 }
551 c <<= 1; 752 c <<= 1;
552 } while (++n & 07); 753 } while (++n & 07);
553 } 754 }
554 putc(')', file); 755 putc(')', file);
555 break; 756 break;
556 757
758 case T_KEY:
759 putc('\t', file);
760 keyflags = _getshort(cp);
761 cp += 2;
762 fprintf(file,"0x%04x", keyflags ); /* flags */
763 fprintf(file," %u", *cp++); /* protocol */
764 fprintf(file," %u (", *cp++); /* algorithm */
765
766 n = b64_ntop(cp, (cp1 + dlen) - cp,
767 base64_key, sizeof base64_key);
768 for (c = 0; c < n; ++c) {
769 if (0 == (c & 0x3F))
770 fprintf(file, "\n\t");
771 putc(base64_key[c], file); /* public key data */
772 }
773
774 fprintf(file, " )");
775 if (n < 0)
776 fprintf(file, "\t; BAD BASE64");
777 fflush(file);
778 cp = cp1 + dlen;
779 break;
780
781 case T_SIG:
782 type = _getshort((u_char*)cp);
783 cp += INT16SZ;
784 fprintf(file, " %s", p_type(type));
785 fprintf(file, "\t%d", *cp++); /* algorithm */
786 /* Check label value and print error if wrong. */
787 n = *cp++;
788 c = dn_count_labels (rrname);
789 if (n != c)
790 fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t",
791 n, c);
792 /* orig ttl */
793 n = _getlong((u_char*)cp);
794 if (n != tmpttl)
795 fprintf(file, " %u", n);
796 cp += INT32SZ;
797 /* sig expire */
798 fprintf(file, " (\n\t%s",
799 __p_secstodate(_getlong((u_char*)cp)));
800 cp += INT32SZ;
801 /* time signed */
802 fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp)));
803 cp += INT32SZ;
804 /* sig footprint */
805 fprintf(file," %u ", _getshort((u_char*)cp));
806 cp += INT16SZ;
807 /* signer's name */
808 cp = p_fqname(cp, msg, file);
809 n = b64_ntop(cp, (cp1 + dlen) - cp,
810 base64_key, sizeof base64_key);
811 for (c = 0; c < n; c++) {
812 if (0 == (c & 0x3F))
813 fprintf (file, "\n\t");
814 putc(base64_key[c], file); /* signature */
815 }
816 /* Clean up... */
817 fprintf(file, " )");
818 if (n < 0)
819 fprintf(file, "\t; BAD BASE64");
820 fflush(file);
821 cp = cp1+dlen;
822 break;
823
557#ifdef ALLOW_T_UNSPEC 824#ifdef ALLOW_T_UNSPEC
558 case T_UNSPEC: 825 case T_UNSPEC:
559 { 826 {
560 int NumBytes = 8; 827 int NumBytes = 8;
561 char *DataPtr; 828 u_char *DataPtr;
562 int i; 829 int i;
563 830
564 if (dlen < NumBytes) NumBytes = dlen; 831 if (dlen < NumBytes) NumBytes = dlen;
@@ -572,7 +839,7 @@ p_rr(cp, msg, file)
572#endif /* ALLOW_T_UNSPEC */ 839#endif /* ALLOW_T_UNSPEC */
573 840
574 default: 841 default:
575 fprintf(file,"\t?%d?", type); 842 fprintf(file, "\t?%d?", type);
576 cp += dlen; 843 cp += dlen;
577 } 844 }
578#if 0 845#if 0
@@ -581,136 +848,205 @@ p_rr(cp, msg, file)
581 putc('\n', file); 848 putc('\n', file);
582#endif 849#endif
583 if (cp - cp1 != dlen) { 850 if (cp - cp1 != dlen) {
584 fprintf(file,";; packet size error (found %d, dlen was %d)\n", 851 fprintf(file, ";; packet size error (found %ld, dlen was %d)\n",
585 cp - cp1, dlen); 852 (long)(cp - cp1), dlen);
586 cp = NULL; 853 cp = NULL;
587 } 854 }
588 return (cp); 855 return (cp);
589} 856}
590 857
591static char nbuf[40]; 858/*
859 * Names of RR classes and qclasses. Classes and qclasses are the same, except
860 * that C_ANY is a qclass but not a class. (You can ask for records of class
861 * C_ANY, but you can't have any records of that class in the database.)
862 */
863const struct res_sym __p_class_syms[] = {
864 {C_IN, "IN"},
865 {C_CHAOS, "CHAOS"},
866 {C_HS, "HS"},
867 {C_HS, "HESIOD"},
868 {C_ANY, "ANY"},
869 {C_IN, (char *)0}
870};
592 871
593/* 872/*
594 * Return a string for the type 873 * Names of RR types and qtypes. Types and qtypes are the same, except
874 * that T_ANY is a qtype but not a type. (You can ask for records of type
875 * T_ANY, but you can't have any records of that type in the database.)
595 */ 876 */
596char * 877const struct res_sym __p_type_syms[] = {
597__p_type(type) 878 {T_A, "A", "address"},
598 int type; 879 {T_NS, "NS", "name server"},
599{ 880 {T_MD, "MD", "mail destination (deprecated)"},
600 switch (type) { 881 {T_MF, "MF", "mail forwarder (deprecated)"},
601 case T_A: 882 {T_CNAME, "CNAME", "canonical name"},
602 return("A"); 883 {T_SOA, "SOA", "start of authority"},
603 case T_NS: /* authoritative server */ 884 {T_MB, "MB", "mailbox"},
604 return("NS"); 885 {T_MG, "MG", "mail group member"},
605 case T_CNAME: /* canonical name */ 886 {T_MR, "MR", "mail rename"},
606 return("CNAME"); 887 {T_NULL, "NULL", "null"},
607 case T_SOA: /* start of authority zone */ 888 {T_WKS, "WKS", "well-known service (deprecated)"},
608 return("SOA"); 889 {T_PTR, "PTR", "domain name pointer"},
609 case T_MB: /* mailbox domain name */ 890 {T_HINFO, "HINFO", "host information"},
610 return("MB"); 891 {T_MINFO, "MINFO", "mailbox information"},
611 case T_MG: /* mail group member */ 892 {T_MX, "MX", "mail exchanger"},
612 return("MG"); 893 {T_TXT, "TXT", "text"},
613 case T_MR: /* mail rename name */ 894 {T_RP, "RP", "responsible person"},
614 return("MR"); 895 {T_AFSDB, "AFSDB", "DCE or AFS server"},
615 case T_NULL: /* null resource record */ 896 {T_X25, "X25", "X25 address"},
616 return("NULL"); 897 {T_ISDN, "ISDN", "ISDN address"},
617 case T_WKS: /* well known service */ 898 {T_RT, "RT", "router"},
618 return("WKS"); 899 {T_NSAP, "NSAP", "nsap address"},
619 case T_PTR: /* domain name pointer */ 900 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
620 return("PTR"); 901 {T_SIG, "SIG", "signature"},
621 case T_HINFO: /* host information */ 902 {T_KEY, "KEY", "key"},
622 return("HINFO"); 903 {T_PX, "PX", "mapping information"},
623 case T_MINFO: /* mailbox information */ 904 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
624 return("MINFO"); 905 {T_AAAA, "AAAA", "IPv6 address"},
625 case T_MX: /* mail routing info */ 906 {T_LOC, "LOC", "location"},
626 return("MX"); 907 {T_NXT, "NXT", "next valid name (unimplemented)"},
627 case T_TXT: /* text */ 908 {T_EID, "EID", "endpoint identifier (unimplemented)"},
628 return("TXT"); 909 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
629 case T_RP: /* responsible person */ 910 {T_SRV, "SRV", "server selection"},
630 return("RP"); 911 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
631 case T_AFSDB: /* AFS cell database */ 912 {T_IXFR, "IXFR", "incremental zone transfer"},
632 return("AFSDB"); 913 {T_AXFR, "AXFR", "zone transfer"},
633 case T_AXFR: /* zone transfer */ 914 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
634 return("AXFR"); 915 {T_MAILA, "MAILA", "mail agent (deprecated)"},
635 case T_MAILB: /* mail box */ 916 {T_UINFO, "UINFO", "user information (nonstandard)"},
636 return("MAILB"); 917 {T_UID, "UID", "user ID (nonstandard)"},
637 case T_MAILA: /* mail address */ 918 {T_GID, "GID", "group ID (nonstandard)"},
638 return("MAILA"); 919 {T_NAPTR, "NAPTR", "URN Naming Authority"},
639 case T_ANY: /* matches any type */
640 return("ANY");
641 case T_UINFO:
642 return("UINFO");
643 case T_UID:
644 return("UID");
645 case T_GID:
646 return("GID");
647#ifdef ALLOW_T_UNSPEC 920#ifdef ALLOW_T_UNSPEC
648 case T_UNSPEC: 921 {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"},
649 return("UNSPEC");
650#endif /* ALLOW_T_UNSPEC */ 922#endif /* ALLOW_T_UNSPEC */
923 {T_ANY, "ANY", "\"any\""},
924 {0, NULL, NULL}
925};
651 926
652 default: 927int
653 (void)sprintf(nbuf, "%d", type); 928__sym_ston(syms, name, success)
654 return(nbuf); 929 const struct res_sym *syms;
930 char *name;
931 int *success;
932{
933 for (; syms->name != 0; syms++) {
934 if (strcasecmp (name, syms->name) == 0) {
935 if (success)
936 *success = 1;
937 return (syms->number);
938 }
939 }
940 if (success)
941 *success = 0;
942 return (syms->number); /* The default value. */
943}
944
945const char *
946__sym_ntos(syms, number, success)
947 const struct res_sym *syms;
948 int number;
949 int *success;
950{
951 static char unname[20];
952
953 for (; syms->name != 0; syms++) {
954 if (number == syms->number) {
955 if (success)
956 *success = 1;
957 return (syms->name);
958 }
959 }
960
961 sprintf (unname, "%d", number);
962 if (success)
963 *success = 0;
964 return (unname);
965}
966
967
968const char *
969__sym_ntop(syms, number, success)
970 const struct res_sym *syms;
971 int number;
972 int *success;
973{
974 static char unname[20];
975
976 for (; syms->name != 0; syms++) {
977 if (number == syms->number) {
978 if (success)
979 *success = 1;
980 return (syms->humanname);
981 }
655 } 982 }
983 sprintf(unname, "%d", number);
984 if (success)
985 *success = 0;
986 return (unname);
987}
988
989/*
990 * Return a string for the type
991 */
992const char *
993__p_type(type)
994 int type;
995{
996 return (__sym_ntos (__p_type_syms, type, (int *)0));
656} 997}
657 998
658/* 999/*
659 * Return a mnemonic for class 1000 * Return a mnemonic for class
660 */ 1001 */
661char * 1002const char *
662__p_class(class) 1003__p_class(class)
663 int class; 1004 int class;
664{ 1005{
665 1006 return (__sym_ntos (__p_class_syms, class, (int *)0));
666 switch (class) {
667 case C_IN: /* internet class */
668 return("IN");
669 case C_HS: /* hesiod class */
670 return("HS");
671 case C_ANY: /* matches any class */
672 return("ANY");
673 default:
674 (void)sprintf(nbuf, "%d", class);
675 return(nbuf);
676 }
677} 1007}
678 1008
679/* 1009/*
680 * Return a mnemonic for an option 1010 * Return a mnemonic for an option
681 */ 1011 */
682static char * 1012const char *
683p_option(option) 1013__p_option(option)
684 u_int32_t option; 1014 u_long option;
685{ 1015{
1016 static char nbuf[40];
1017
686 switch (option) { 1018 switch (option) {
687 case RES_INIT: return "init"; 1019 case RES_INIT: return "init";
688 case RES_DEBUG: return "debug"; 1020 case RES_DEBUG: return "debug";
689 case RES_AAONLY: return "aaonly"; 1021 case RES_AAONLY: return "aaonly(unimpl)";
690 case RES_USEVC: return "usevc"; 1022 case RES_USEVC: return "usevc";
691 case RES_PRIMARY: return "primry"; 1023 case RES_PRIMARY: return "primry(unimpl)";
692 case RES_IGNTC: return "igntc"; 1024 case RES_IGNTC: return "igntc";
693 case RES_RECURSE: return "recurs"; 1025 case RES_RECURSE: return "recurs";
694 case RES_DEFNAMES: return "defnam"; 1026 case RES_DEFNAMES: return "defnam";
695 case RES_STAYOPEN: return "styopn"; 1027 case RES_STAYOPEN: return "styopn";
696 case RES_DNSRCH: return "dnsrch"; 1028 case RES_DNSRCH: return "dnsrch";
697 default: sprintf(nbuf, "?0x%x?", option); return nbuf; 1029 case RES_INSECURE1: return "insecure1";
1030 case RES_INSECURE2: return "insecure2";
1031 default: sprintf(nbuf, "?0x%lx?", (u_long)option);
1032 return (nbuf);
698 } 1033 }
699} 1034}
700 1035
701/* 1036/*
702 * Return a mnemonic for a time to live 1037 * Return a mnemonic for a time to live
703 */ 1038 */
704char * 1039const char *
705__p_time(value) 1040p_time(value)
706 u_int32_t value; 1041 u_int32_t value;
707{ 1042{
1043 static char nbuf[40];
708 int secs, mins, hours, days; 1044 int secs, mins, hours, days;
709 register char *p; 1045 register char *p;
710 1046
711 if (value == 0) { 1047 if (value == 0) {
712 strcpy(nbuf, "0 secs"); 1048 strcpy(nbuf, "0 secs");
713 return(nbuf); 1049 return (nbuf);
714 } 1050 }
715 1051
716 secs = value % 60; 1052 secs = value % 60;
@@ -745,5 +1081,438 @@ __p_time(value)
745 *p++ = ' '; 1081 *p++ = ' ';
746 (void)sprintf(p, "%d sec%s", PLURALIZE(secs)); 1082 (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
747 } 1083 }
748 return(nbuf); 1084 return (nbuf);
1085}
1086
1087/*
1088 * routines to convert between on-the-wire RR format and zone file format.
1089 * Does not contain conversion to/from decimal degrees; divide or multiply
1090 * by 60*60*1000 for that.
1091 */
1092
1093static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000,
1094 1000000,10000000,100000000,1000000000};
1095
1096/* takes an XeY precision/size value, returns a string representation. */
1097static const char *
1098precsize_ntoa(prec)
1099 u_int8_t prec;
1100{
1101 static char retbuf[sizeof "90000000.00"];
1102 unsigned long val;
1103 int mantissa, exponent;
1104
1105 mantissa = (int)((prec >> 4) & 0x0f) % 10;
1106 exponent = (int)((prec >> 0) & 0x0f) % 10;
1107
1108 val = mantissa * poweroften[exponent];
1109
1110 (void) sprintf(retbuf, "%ld.%.2ld", val/100, val%100);
1111 return (retbuf);
1112}
1113
1114/* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */
1115static u_int8_t
1116precsize_aton(strptr)
1117 char **strptr;
1118{
1119 unsigned int mval = 0, cmval = 0;
1120 u_int8_t retval = 0;
1121 register char *cp;
1122 register int exponent;
1123 register int mantissa;
1124
1125 cp = *strptr;
1126
1127 while (isdigit(*cp))
1128 mval = mval * 10 + (*cp++ - '0');
1129
1130 if (*cp == '.') { /* centimeters */
1131 cp++;
1132 if (isdigit(*cp)) {
1133 cmval = (*cp++ - '0') * 10;
1134 if (isdigit(*cp)) {
1135 cmval += (*cp++ - '0');
1136 }
1137 }
1138 }
1139 cmval = (mval * 100) + cmval;
1140
1141 for (exponent = 0; exponent < 9; exponent++)
1142 if (cmval < poweroften[exponent+1])
1143 break;
1144
1145 mantissa = cmval / poweroften[exponent];
1146 if (mantissa > 9)
1147 mantissa = 9;
1148
1149 retval = (mantissa << 4) | exponent;
1150
1151 *strptr = cp;
1152
1153 return (retval);
1154}
1155
1156/* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */
1157static u_int32_t
1158latlon2ul(latlonstrptr,which)
1159 char **latlonstrptr;
1160 int *which;
1161{
1162 register char *cp;
1163 u_int32_t retval;
1164 int deg = 0, min = 0, secs = 0, secsfrac = 0;
1165
1166 cp = *latlonstrptr;
1167
1168 while (isdigit(*cp))
1169 deg = deg * 10 + (*cp++ - '0');
1170
1171 while (isspace(*cp))
1172 cp++;
1173
1174 if (!(isdigit(*cp)))
1175 goto fndhemi;
1176
1177 while (isdigit(*cp))
1178 min = min * 10 + (*cp++ - '0');
1179
1180 while (isspace(*cp))
1181 cp++;
1182
1183 if (!(isdigit(*cp)))
1184 goto fndhemi;
1185
1186 while (isdigit(*cp))
1187 secs = secs * 10 + (*cp++ - '0');
1188
1189 if (*cp == '.') { /* decimal seconds */
1190 cp++;
1191 if (isdigit(*cp)) {
1192 secsfrac = (*cp++ - '0') * 100;
1193 if (isdigit(*cp)) {
1194 secsfrac += (*cp++ - '0') * 10;
1195 if (isdigit(*cp)) {
1196 secsfrac += (*cp++ - '0');
1197 }
1198 }
1199 }
1200 }
1201
1202 while (!isspace(*cp)) /* if any trailing garbage */
1203 cp++;
1204
1205 while (isspace(*cp))
1206 cp++;
1207
1208 fndhemi:
1209 switch (*cp) {
1210 case 'N': case 'n':
1211 case 'E': case 'e':
1212 retval = ((unsigned)1<<31)
1213 + (((((deg * 60) + min) * 60) + secs) * 1000)
1214 + secsfrac;
1215 break;
1216 case 'S': case 's':
1217 case 'W': case 'w':
1218 retval = ((unsigned)1<<31)
1219 - (((((deg * 60) + min) * 60) + secs) * 1000)
1220 - secsfrac;
1221 break;
1222 default:
1223 retval = 0; /* invalid value -- indicates error */
1224 break;
1225 }
1226
1227 switch (*cp) {
1228 case 'N': case 'n':
1229 case 'S': case 's':
1230 *which = 1; /* latitude */
1231 break;
1232 case 'E': case 'e':
1233 case 'W': case 'w':
1234 *which = 2; /* longitude */
1235 break;
1236 default:
1237 *which = 0; /* error */
1238 break;
1239 }
1240
1241 cp++; /* skip the hemisphere */
1242
1243 while (!isspace(*cp)) /* if any trailing garbage */
1244 cp++;
1245
1246 while (isspace(*cp)) /* move to next field */
1247 cp++;
1248
1249 *latlonstrptr = cp;
1250
1251 return (retval);
1252}
1253
1254/* converts a zone file representation in a string to an RDATA on-the-wire
1255 * representation. */
1256int
1257loc_aton(ascii, binary)
1258 const char *ascii;
1259 u_char *binary;
1260{
1261 const char *maxcp;
1262 u_char *bcp;
1263 char *cp;
1264
1265 u_int32_t latit = 0, longit = 0, alt = 0;
1266 u_int32_t lltemp1 = 0, lltemp2 = 0;
1267 int altmeters = 0, altfrac = 0, altsign = 1;
1268 u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */
1269 u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */
1270 u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */
1271 int which1 = 0, which2 = 0;
1272
1273 cp = (char *)ascii;
1274 maxcp = cp + strlen(ascii);
1275
1276 lltemp1 = latlon2ul(&cp, &which1);
1277
1278 lltemp2 = latlon2ul(&cp, &which2);
1279
1280 switch (which1 + which2) {
1281 case 3: /* 1 + 2, the only valid combination */
1282 if ((which1 == 1) && (which2 == 2)) { /* normal case */
1283 latit = lltemp1;
1284 longit = lltemp2;
1285 } else if ((which1 == 2) && (which2 == 1)) { /* reversed */
1286 longit = lltemp1;
1287 latit = lltemp2;
1288 } else { /* some kind of brokenness */
1289 return (0);
1290 }
1291 break;
1292 default: /* we didn't get one of each */
1293 return (0);
1294 }
1295
1296 /* altitude */
1297 if (*cp == '-') {
1298 altsign = -1;
1299 cp++;
1300 }
1301
1302 if (*cp == '+')
1303 cp++;
1304
1305 while (isdigit(*cp))
1306 altmeters = altmeters * 10 + (*cp++ - '0');
1307
1308 if (*cp == '.') { /* decimal meters */
1309 cp++;
1310 if (isdigit(*cp)) {
1311 altfrac = (*cp++ - '0') * 10;
1312 if (isdigit(*cp)) {
1313 altfrac += (*cp++ - '0');
1314 }
1315 }
1316 }
1317
1318 alt = (10000000 + (altsign * (altmeters * 100 + altfrac)));
1319
1320 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1321 cp++;
1322
1323 while (isspace(*cp) && (cp < maxcp))
1324 cp++;
1325
1326 if (cp >= maxcp)
1327 goto defaults;
1328
1329 siz = precsize_aton(&cp);
1330
1331 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1332 cp++;
1333
1334 while (isspace(*cp) && (cp < maxcp))
1335 cp++;
1336
1337 if (cp >= maxcp)
1338 goto defaults;
1339
1340 hp = precsize_aton(&cp);
1341
1342 while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */
1343 cp++;
1344
1345 while (isspace(*cp) && (cp < maxcp))
1346 cp++;
1347
1348 if (cp >= maxcp)
1349 goto defaults;
1350
1351 vp = precsize_aton(&cp);
1352
1353 defaults:
1354
1355 bcp = binary;
1356 *bcp++ = (u_int8_t) 0; /* version byte */
1357 *bcp++ = siz;
1358 *bcp++ = hp;
1359 *bcp++ = vp;
1360 PUTLONG(latit,bcp);
1361 PUTLONG(longit,bcp);
1362 PUTLONG(alt,bcp);
1363
1364 return (16); /* size of RR in octets */
1365}
1366
1367/* takes an on-the-wire LOC RR and formats it in a human readable format. */
1368const char *
1369loc_ntoa(binary, ascii)
1370 const u_char *binary;
1371 char *ascii;
1372{
1373 static char *error = "?";
1374 register const u_char *cp = binary;
1375
1376 int latdeg, latmin, latsec, latsecfrac;
1377 int longdeg, longmin, longsec, longsecfrac;
1378 char northsouth, eastwest;
1379 int altmeters, altfrac, altsign;
1380
1381 const int referencealt = 100000 * 100;
1382
1383 int32_t latval, longval, altval;
1384 u_int32_t templ;
1385 u_int8_t sizeval, hpval, vpval, versionval;
1386
1387 char *sizestr, *hpstr, *vpstr;
1388
1389 versionval = *cp++;
1390
1391 if (versionval) {
1392 sprintf(ascii, "; error: unknown LOC RR version");
1393 return (ascii);
1394 }
1395
1396 sizeval = *cp++;
1397
1398 hpval = *cp++;
1399 vpval = *cp++;
1400
1401 GETLONG(templ, cp);
1402 latval = (templ - ((unsigned)1<<31));
1403
1404 GETLONG(templ, cp);
1405 longval = (templ - ((unsigned)1<<31));
1406
1407 GETLONG(templ, cp);
1408 if (templ < referencealt) { /* below WGS 84 spheroid */
1409 altval = referencealt - templ;
1410 altsign = -1;
1411 } else {
1412 altval = templ - referencealt;
1413 altsign = 1;
1414 }
1415
1416 if (latval < 0) {
1417 northsouth = 'S';
1418 latval = -latval;
1419 } else
1420 northsouth = 'N';
1421
1422 latsecfrac = latval % 1000;
1423 latval = latval / 1000;
1424 latsec = latval % 60;
1425 latval = latval / 60;
1426 latmin = latval % 60;
1427 latval = latval / 60;
1428 latdeg = latval;
1429
1430 if (longval < 0) {
1431 eastwest = 'W';
1432 longval = -longval;
1433 } else
1434 eastwest = 'E';
1435
1436 longsecfrac = longval % 1000;
1437 longval = longval / 1000;
1438 longsec = longval % 60;
1439 longval = longval / 60;
1440 longmin = longval % 60;
1441 longval = longval / 60;
1442 longdeg = longval;
1443
1444 altfrac = altval % 100;
1445 altmeters = (altval / 100) * altsign;
1446
1447 if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL)
1448 sizestr = error;
1449 if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL)
1450 hpstr = error;
1451 if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL)
1452 vpstr = error;
1453
1454 sprintf(ascii,
1455 "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm",
1456 latdeg, latmin, latsec, latsecfrac, northsouth,
1457 longdeg, longmin, longsec, longsecfrac, eastwest,
1458 altmeters, altfrac, sizestr, hpstr, vpstr);
1459
1460 if (sizestr != error)
1461 free(sizestr);
1462 if (hpstr != error)
1463 free(hpstr);
1464 if (vpstr != error)
1465 free(vpstr);
1466
1467 return (ascii);
1468}
1469
1470
1471/* Return the number of DNS hierarchy levels in the name. */
1472int
1473__dn_count_labels(name)
1474 char *name;
1475{
1476 int i, len, count;
1477
1478 len = strlen(name);
1479
1480 for(i = 0, count = 0; i < len; i++) {
1481 if (name[i] == '.')
1482 count++;
1483 }
1484
1485 /* don't count initial wildcard */
1486 if (name[0] == '*')
1487 if (count)
1488 count--;
1489
1490 /* don't count the null label for root. */
1491 /* if terminating '.' not found, must adjust */
1492 /* count to include last label */
1493 if (len > 0 && name[len-1] != '.')
1494 count++;
1495 return (count);
1496}
1497
1498
1499/*
1500 * Make dates expressed in seconds-since-Jan-1-1970 easy to read.
1501 * SIG records are required to be printed like this, by the Secure DNS RFC.
1502 */
1503char *
1504__p_secstodate (secs)
1505 unsigned long secs;
1506{
1507 static char output[15]; /* YYYYMMDDHHMMSS and null */
1508 time_t clock = secs;
1509 struct tm *time;
1510
1511 time = gmtime(&clock);
1512 time->tm_year += 1900;
1513 time->tm_mon += 1;
1514 sprintf(output, "%04d%02d%02d%02d%02d%02d",
1515 time->tm_year, time->tm_mon, time->tm_mday,
1516 time->tm_hour, time->tm_min, time->tm_sec);
1517 return (output);
749} 1518}
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c
index 33cc8d39f1..df176b7fa1 100644
--- a/src/lib/libc/net/res_init.c
+++ b/src/lib/libc/net/res_init.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $ */ 1/* $OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1989, 1993
5 * -
4 * Copyright (c) 1985, 1989, 1993 6 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,77 +58,150 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; 60static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93";
59static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 61static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $"; 63static char rcsid[] = "$OpenBSD: res_init.c,v 1.16 1998/03/16 05:07:01 millert Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <sys/socket.h> 69#include <sys/socket.h>
70#include <sys/time.h>
67#include <netinet/in.h> 71#include <netinet/in.h>
68#include <arpa/inet.h> 72#include <arpa/inet.h>
69#include <arpa/nameser.h> 73#include <arpa/nameser.h>
74
75#include <stdio.h>
76#include <ctype.h>
70#include <resolv.h> 77#include <resolv.h>
71#include <unistd.h> 78#include <unistd.h>
72#include <stdio.h>
73#include <stdlib.h> 79#include <stdlib.h>
74#include <string.h> 80#include <string.h>
75 81
82/*-------------------------------------- info about "sortlist" --------------
83 * Marc Majka 1994/04/16
84 * Allan Nathanson 1994/10/29 (BIND 4.9.3.x)
85 *
86 * NetInfo resolver configuration directory support.
87 *
88 * Allow a NetInfo directory to be created in the hierarchy which
89 * contains the same information as the resolver configuration file.
90 *
91 * - The local domain name is stored as the value of the "domain" property.
92 * - The Internet address(es) of the name server(s) are stored as values
93 * of the "nameserver" property.
94 * - The name server addresses are stored as values of the "nameserver"
95 * property.
96 * - The search list for host-name lookup is stored as values of the
97 * "search" property.
98 * - The sortlist comprised of IP address netmask pairs are stored as
99 * values of the "sortlist" property. The IP address and optional netmask
100 * should be seperated by a slash (/) or ampersand (&) character.
101 * - Internal resolver variables can be set from the value of the "options"
102 * property.
103 */
104
76static void res_setoptions __P((char *, char *)); 105static void res_setoptions __P((char *, char *));
106
107#ifdef RESOLVSORT
108static const char sort_mask[] = "/&";
109#define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL)
77static u_int32_t net_mask __P((struct in_addr)); 110static u_int32_t net_mask __P((struct in_addr));
111#endif
78 112
79/* 113/*
80 * Resolver state default settings 114 * Resolver state default settings.
81 */ 115 */
82 116
83struct __res_state _res = { 117struct __res_state _res
84 RES_TIMEOUT, /* retransmition time interval */ 118# if defined(__BIND_RES_TEXT)
85 4, /* number of times to retransmit */ 119 = { RES_TIMEOUT, } /* Motorola, et al. */
86 RES_DEFAULT, /* options flags */ 120# endif
87 1, /* number of name servers */ 121 ;
88};
89 122
90/* 123/*
91 * Set up default settings. If the configuration file exist, the values 124 * Set up default settings. If the configuration file exist, the values
92 * there will have precedence. Otherwise, the server address is set to 125 * there will have precedence. Otherwise, the server address is set to
93 * INADDR_ANY and the default domain name comes from the gethostname(). 126 * INADDR_ANY and the default domain name comes from the gethostname().
94 * 127 *
95 * The configuration file should only be used if you want to redefine your 128 * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
96 * domain or run without a server on your machine. 129 * rather than INADDR_ANY ("0.0.0.0") as the default name server address
130 * since it was noted that INADDR_ANY actually meant ``the first interface
131 * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
132 * it had to be "up" in order for you to reach your own name server. It
133 * was later decided that since the recommended practice is to always
134 * install local static routes through 127.0.0.1 for all your network
135 * interfaces, that we could solve this problem without a code change.
136 *
137 * The configuration file should always be used, since it is the only way
138 * to specify a default domain. If you are running a server on your local
139 * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
140 * in the configuration file.
97 * 141 *
98 * Return 0 if completes successfully, -1 on error 142 * Return 0 if completes successfully, -1 on error
99 */ 143 */
144int
100res_init() 145res_init()
101{ 146{
102 register FILE *fp; 147 register FILE *fp;
103 register char *cp, **pp, *net; 148 register char *cp, **pp;
104 register int n; 149 register int n;
105 char buf[BUFSIZ], buf2[BUFSIZ]; 150 char buf[BUFSIZ];
106 int nserv = 0; /* number of nameserver records read from file */ 151 int nserv = 0; /* number of nameserver records read from file */
107 int haveenv = 0; 152 int haveenv = 0;
108 int havesearch = 0; 153 int havesearch = 0;
154 size_t len;
155#ifdef RESOLVSORT
109 int nsort = 0; 156 int nsort = 0;
110 u_long mask; 157 char *net;
158#endif
159#ifndef RFC1535
160 int dots;
161#endif
162
163 /*
164 * These three fields used to be statically initialized. This made
165 * it hard to use this code in a shared library. It is necessary,
166 * now that we're doing dynamic initialization here, that we preserve
167 * the old semantics: if an application modifies one of these three
168 * fields of _res before res_init() is called, res_init() will not
169 * alter them. Of course, if an application is setting them to
170 * _zero_ before calling res_init(), hoping to override what used
171 * to be the static default, we can't detect it and unexpected results
172 * will follow. Zero for any of these fields would make no sense,
173 * so one can safely assume that the applications were already getting
174 * unexpected results.
175 *
176 * _res.options is tricky since some apps were known to diddle the bits
177 * before res_init() was first called. We can't replicate that semantic
178 * with dynamic initialization (they may have turned bits off that are
179 * set in RES_DEFAULT). Our solution is to declare such applications
180 * "broken". They could fool us by setting RES_INIT but none do (yet).
181 */
182 if (!_res.retrans)
183 _res.retrans = RES_TIMEOUT;
184 if (!_res.retry)
185 _res.retry = 4;
186 if (!(_res.options & RES_INIT))
187 _res.options = RES_DEFAULT;
111 188
112 _res.nsaddr.sin_len = sizeof(struct sockaddr_in);
113 _res.nsaddr.sin_family = AF_INET;
114 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
115#ifdef USELOOPBACK 189#ifdef USELOOPBACK
116 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); 190 _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1);
117#else 191#else
118 _res.nsaddr.sin_addr.s_addr = INADDR_ANY; 192 _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
119#endif 193#endif
194 _res.nsaddr.sin_family = AF_INET;
195 _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
120 _res.nscount = 1; 196 _res.nscount = 1;
121 _res.ndots = 1; 197 _res.ndots = 1;
122 _res.pfcode = 0; 198 _res.pfcode = 0;
123 strncpy(_res.lookups, "f", sizeof _res.lookups); 199 strncpy(_res.lookups, "f", sizeof _res.lookups);
124 200
125 /* Allow user to override the local domain definition */ 201 /* Allow user to override the local domain definition */
126 if ((cp = getenv("LOCALDOMAIN")) != NULL) { 202 if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) {
127 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); 203 (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
128 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) 204 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
129 *cp = '\0';
130 haveenv++; 205 haveenv++;
131 206
132 /* 207 /*
@@ -140,7 +215,7 @@ res_init()
140 pp = _res.dnsrch; 215 pp = _res.dnsrch;
141 *pp++ = cp; 216 *pp++ = cp;
142 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { 217 for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
143 if (*cp == '\n') /* silly backwards compat */ 218 if (*cp == '\n') /* silly backwards compat */
144 break; 219 break;
145 else if (*cp == ' ' || *cp == '\t') { 220 else if (*cp == ' ' || *cp == '\t') {
146 *cp = 0; 221 *cp = 0;
@@ -158,16 +233,29 @@ res_init()
158 *pp++ = 0; 233 *pp++ = 0;
159 } 234 }
160 235
236#define MATCH(line, name) \
237 (!strncmp(line, name, sizeof(name) - 1) && \
238 (line[sizeof(name) - 1] == ' ' || \
239 line[sizeof(name) - 1] == '\t'))
240
161 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { 241 if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
162 strncpy(_res.lookups, "bf", sizeof _res.lookups); 242 strncpy(_res.lookups, "bf", sizeof _res.lookups);
163 243
164 /* read the config file */ 244 /* read the config file */
165 while (fgets(buf, sizeof(buf), fp) != NULL) { 245 buf[0] = '\0';
246 while ((cp = fgetln(fp, &len)) != NULL) {
247 /* skip lines that are too long or zero length */
248 if (len >= sizeof(buf) || len == 0)
249 continue;
250 (void)memcpy(buf, cp, len);
251 buf[len] = '\0';
166 /* skip comments */ 252 /* skip comments */
167 if ((*buf == ';') || (*buf == '#')) 253 if ((cp = strpbrk(buf, ";#")) != NULL)
254 *cp = '\0';
255 if (buf[0] == '\0')
168 continue; 256 continue;
169 /* read default domain name */ 257 /* read default domain name */
170 if (!strncmp(buf, "domain", sizeof("domain") - 1)) { 258 if (MATCH(buf, "domain")) {
171 if (haveenv) /* skip if have from environ */ 259 if (haveenv) /* skip if have from environ */
172 continue; 260 continue;
173 cp = buf + sizeof("domain") - 1; 261 cp = buf + sizeof("domain") - 1;
@@ -175,15 +263,15 @@ res_init()
175 cp++; 263 cp++;
176 if ((*cp == '\0') || (*cp == '\n')) 264 if ((*cp == '\0') || (*cp == '\n'))
177 continue; 265 continue;
178 (void)strncpy(_res.defdname, cp, 266 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
179 sizeof(_res.defdname) - 1); 267 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
180 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) 268 if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL)
181 *cp = '\0'; 269 *cp = '\0';
182 havesearch = 0; 270 havesearch = 0;
183 continue; 271 continue;
184 } 272 }
185 /* lookup types */ 273 /* lookup types */
186 if (!strncmp(buf, "lookup", sizeof("lookup") -1)) { 274 if (MATCH(buf, "lookup")) {
187 char *sp = NULL; 275 char *sp = NULL;
188 276
189 bzero(_res.lookups, sizeof _res.lookups); 277 bzero(_res.lookups, sizeof _res.lookups);
@@ -210,7 +298,7 @@ res_init()
210 continue; 298 continue;
211 } 299 }
212 /* set search list */ 300 /* set search list */
213 if (!strncmp(buf, "search", sizeof("search") - 1)) { 301 if (MATCH(buf, "search")) {
214 if (haveenv) /* skip if have from environ */ 302 if (haveenv) /* skip if have from environ */
215 continue; 303 continue;
216 cp = buf + sizeof("search") - 1; 304 cp = buf + sizeof("search") - 1;
@@ -218,8 +306,8 @@ res_init()
218 cp++; 306 cp++;
219 if ((*cp == '\0') || (*cp == '\n')) 307 if ((*cp == '\0') || (*cp == '\n'))
220 continue; 308 continue;
221 (void)strncpy(_res.defdname, cp, 309 strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
222 sizeof(_res.defdname) - 1); 310 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
223 if ((cp = strchr(_res.defdname, '\n')) != NULL) 311 if ((cp = strchr(_res.defdname, '\n')) != NULL)
224 *cp = '\0'; 312 *cp = '\0';
225 /* 313 /*
@@ -247,92 +335,125 @@ res_init()
247 continue; 335 continue;
248 } 336 }
249 /* read nameservers to query */ 337 /* read nameservers to query */
250 if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && 338 if (MATCH(buf, "nameserver") && nserv < MAXNS) {
251 nserv < MAXNS) { 339 struct in_addr a;
252 struct in_addr a;
253 340
254 cp = buf + sizeof("nameserver") - 1; 341 cp = buf + sizeof("nameserver") - 1;
255 while (*cp == ' ' || *cp == '\t') 342 while (*cp == ' ' || *cp == '\t')
256 cp++; 343 cp++;
257 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { 344 if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) {
258 _res.nsaddr_list[nserv].sin_len = sizeof(struct sockaddr_in); 345 _res.nsaddr_list[nserv].sin_addr = a;
259 _res.nsaddr_list[nserv].sin_family = AF_INET; 346 _res.nsaddr_list[nserv].sin_family = AF_INET;
260 _res.nsaddr_list[nserv].sin_port = 347 _res.nsaddr_list[nserv].sin_port =
261 htons(NAMESERVER_PORT); 348 htons(NAMESERVER_PORT);
262 _res.nsaddr_list[nserv].sin_addr = a;
263 nserv++; 349 nserv++;
264 } 350 }
265 continue; 351 continue;
266 } 352 }
267 if (!strncmp(buf, "sortlist", sizeof("sortlist") - 1)) { 353#ifdef RESOLVSORT
354 if (MATCH(buf, "sortlist")) {
268 struct in_addr a; 355 struct in_addr a;
269 356
270 cp = buf + sizeof("sortlist") - 1; 357 cp = buf + sizeof("sortlist") - 1;
271 while (*cp == ' ' || *cp == '\t') 358 while (nsort < MAXRESOLVSORT) {
272 cp++; 359 while (*cp == ' ' || *cp == '\t')
273 while (sscanf(cp,"%[0-9./]s", buf2) && nsort < MAXRESOLVSORT) { 360 cp++;
274 if (net = strchr(buf2, '/')) 361 if (*cp == '\0' || *cp == '\n' || *cp == ';')
275 *net = '\0'; 362 break;
276 if (inet_aton(buf2, &a)) { 363 net = cp;
364 while (*cp && !ISSORTMASK(*cp) && *cp != ';' &&
365 isascii(*cp) && !isspace(*cp))
366 cp++;
367 n = *cp;
368 *cp = 0;
369 if (inet_aton(net, &a)) {
277 _res.sort_list[nsort].addr = a; 370 _res.sort_list[nsort].addr = a;
278 if (net && inet_aton(net+1, &a)) { 371 if (ISSORTMASK(n)) {
279 _res.sort_list[nsort].mask = a.s_addr; 372 *cp++ = n;
280 } else { 373 net = cp;
281 _res.sort_list[nsort].mask = 374 while (*cp && *cp != ';' &&
375 isascii(*cp) && !isspace(*cp))
376 cp++;
377 n = *cp;
378 *cp = 0;
379 if (inet_aton(net, &a)) {
380 _res.sort_list[nsort].mask = a.s_addr;
381 } else {
382 _res.sort_list[nsort].mask =
282 net_mask(_res.sort_list[nsort].addr); 383 net_mask(_res.sort_list[nsort].addr);
384 }
385 } else {
386 _res.sort_list[nsort].mask =
387 net_mask(_res.sort_list[nsort].addr);
283 } 388 }
284 nsort++; 389 nsort++;
285 } 390 }
286 if (net) 391 *cp = n;
287 *net = '/';
288 cp += strlen(buf2);
289 while (*cp == ' ' || *cp == '\t')
290 cp++;
291 } 392 }
292 continue; 393 continue;
293 } 394 }
294 if (!strncmp(buf, "options", sizeof("options") -1)) { 395#endif
396 if (MATCH(buf, "options")) {
295 res_setoptions(buf + sizeof("options") - 1, "conf"); 397 res_setoptions(buf + sizeof("options") - 1, "conf");
296 continue; 398 continue;
297 } 399 }
298 } 400 }
299 if (nserv > 1) 401 if (nserv > 1)
300 _res.nscount = nserv; 402 _res.nscount = nserv;
403#ifdef RESOLVSORT
301 _res.nsort = nsort; 404 _res.nsort = nsort;
405#endif
302 (void) fclose(fp); 406 (void) fclose(fp);
303 } 407 }
304 if (_res.defdname[0] == 0) { 408 if (_res.defdname[0] == 0 &&
305 if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && 409 gethostname(buf, sizeof(_res.defdname) - 1) == 0 &&
306 (cp = strchr(buf, '.'))) 410 (cp = strchr(buf, '.')) != NULL)
307 (void)strcpy(_res.defdname, cp + 1); 411 {
412 strncpy(_res.defdname, cp + 1,
413 sizeof(_res.defdname) - 1);
414 _res.defdname[sizeof(_res.defdname) - 1] = '\0';
308 } 415 }
309 416
310 /* find components of local domain that might be searched */ 417 /* find components of local domain that might be searched */
311 if (havesearch == 0) { 418 if (havesearch == 0) {
312 pp = _res.dnsrch; 419 pp = _res.dnsrch;
313 *pp++ = _res.defdname; 420 *pp++ = _res.defdname;
314#ifndef SEARCH_LOCAL_DOMAINS
315 *pp = NULL; 421 *pp = NULL;
316#else 422
317 for (cp = _res.defdname, n = 0; *cp; cp++) 423#ifndef RFC1535
318 if (*cp == '.') 424 dots = 0;
319 n++; 425 for (cp = _res.defdname; *cp; cp++)
426 dots += (*cp == '.');
427
320 cp = _res.defdname; 428 cp = _res.defdname;
321 for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; 429 while (pp < _res.dnsrch + MAXDFLSRCH) {
322 n--) { 430 if (dots < LOCALDOMAINPARTS)
323 cp = strchr(cp, '.'); 431 break;
324 *pp++ = ++cp; 432 cp = strchr(cp, '.') + 1; /* we know there is one */
433 *pp++ = cp;
434 dots--;
325 } 435 }
326 *pp++ = 0; 436 *pp = NULL;
327#endif 437#ifdef DEBUG
438 if (_res.options & RES_DEBUG) {
439 printf(";; res_init()... default dnsrch list:\n");
440 for (pp = _res.dnsrch; *pp; pp++)
441 printf(";;\t%s\n", *pp);
442 printf(";;\t..END..\n");
443 }
444#endif /* DEBUG */
445#endif /* !RFC1535 */
328 } 446 }
329 447
330 if ((cp = getenv("RES_OPTIONS")) != NULL) 448 if (issetugid())
449 _res.options |= RES_NOALIASES;
450 else if ((cp = getenv("RES_OPTIONS")) != NULL)
331 res_setoptions(cp, "env"); 451 res_setoptions(cp, "env");
332 _res.options |= RES_INIT; 452 _res.options |= RES_INIT;
333 return (0); 453 return (0);
334} 454}
335 455
456/* ARGSUSED */
336static void 457static void
337res_setoptions(options, source) 458res_setoptions(options, source)
338 char *options, *source; 459 char *options, *source;
@@ -341,28 +462,26 @@ res_setoptions(options, source)
341 int i; 462 int i;
342 463
343#ifdef DEBUG 464#ifdef DEBUG
344 if (_res.options & RES_DEBUG) { 465 if (_res.options & RES_DEBUG)
345 printf(";; res_setoptions(\"%s\", \"%s\")...\n", 466 printf(";; res_setoptions(\"%s\", \"%s\")...\n",
346 options, source); 467 options, source);
347 }
348#endif 468#endif
349 while (*cp) { 469 while (*cp) {
350 /* skip leading and inner runs of spaces */ 470 /* skip leading and inner runs of spaces */
351 while (*cp == ' ' || *cp == '\t') 471 while (*cp == ' ' || *cp == '\t')
352 cp++; 472 cp++;
353 /* search for and process individual options */ 473 /* search for and process individual options */
354 if (!strncmp(cp, "ndots:", sizeof("ndots:")-1)) { 474 if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
355 i = atoi(cp + sizeof("ndots:") - 1); 475 i = atoi(cp + sizeof("ndots:") - 1);
356 if (i <= RES_MAXNDOTS) 476 if (i <= RES_MAXNDOTS)
357 _res.ndots = i; 477 _res.ndots = i;
358 else 478 else
359 _res.ndots = RES_MAXNDOTS; 479 _res.ndots = RES_MAXNDOTS;
360#ifdef DEBUG 480#ifdef DEBUG
361 if (_res.options & RES_DEBUG) { 481 if (_res.options & RES_DEBUG)
362 printf(";;\tndots=%d\n", _res.ndots); 482 printf(";;\tndots=%d\n", _res.ndots);
363 }
364#endif 483#endif
365 } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { 484 } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
366#ifdef DEBUG 485#ifdef DEBUG
367 if (!(_res.options & RES_DEBUG)) { 486 if (!(_res.options & RES_DEBUG)) {
368 printf(";; res_setoptions(\"%s\", \"%s\")..\n", 487 printf(";; res_setoptions(\"%s\", \"%s\")..\n",
@@ -371,6 +490,8 @@ res_setoptions(options, source)
371 } 490 }
372 printf(";;\tdebug\n"); 491 printf(";;\tdebug\n");
373#endif 492#endif
493 } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
494 _res.options |= RES_USE_INET6;
374 } else { 495 } else {
375 /* XXX - print a warning here? */ 496 /* XXX - print a warning here? */
376 } 497 }
@@ -380,6 +501,8 @@ res_setoptions(options, source)
380 } 501 }
381} 502}
382 503
504#ifdef RESOLVSORT
505/* XXX - should really support CIDR which means explicit masks always. */
383static u_int32_t 506static u_int32_t
384net_mask(in) /* XXX - should really use system's version of this */ 507net_mask(in) /* XXX - should really use system's version of this */
385 struct in_addr in; 508 struct in_addr in;
@@ -388,7 +511,8 @@ net_mask(in) /* XXX - should really use system's version of this */
388 511
389 if (IN_CLASSA(i)) 512 if (IN_CLASSA(i))
390 return (htonl(IN_CLASSA_NET)); 513 return (htonl(IN_CLASSA_NET));
391 if (IN_CLASSB(i)) 514 else if (IN_CLASSB(i))
392 return (htonl(IN_CLASSB_NET)); 515 return (htonl(IN_CLASSB_NET));
393 return (htonl(IN_CLASSC_NET)); 516 return (htonl(IN_CLASSC_NET));
394} 517}
518#endif
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c
index 25f025e147..3e7e2ae5d3 100644
--- a/src/lib/libc/net/res_mkquery.c
+++ b/src/lib/libc/net/res_mkquery.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $ */ 1/* $OpenBSD: res_mkquery.c,v 1.8 1997/04/13 22:37:21 provos Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1993
5 * -
4 * Copyright (c) 1985, 1993 6 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,39 +58,47 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: res_mkquery.c,v 4.9.1.2 1993/05/17 10:00:01 vixie Exp "; 61static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.8 1997/04/13 22:37:21 provos Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <netinet/in.h> 69#include <netinet/in.h>
67#include <arpa/nameser.h> 70#include <arpa/nameser.h>
68#include <resolv.h> 71
69#include <stdio.h> 72#include <stdio.h>
73#include <netdb.h>
74#include <resolv.h>
70#include <string.h> 75#include <string.h>
71 76
72/* 77/*
73 * Form all types of queries. 78 * Form all types of queries.
74 * Returns the size of the result or -1. 79 * Returns the size of the result or -1.
75 */ 80 */
81/* ARGSUSED */
82int
76res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) 83res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
77 int op; /* opcode of query */ 84 int op; /* opcode of query */
78 const char *dname; /* domain name */ 85 const char *dname; /* domain name */
79 int class, type; /* class and type of query */ 86 int class, type; /* class and type of query */
80 const char *data; /* resource record data */ 87 const u_char *data; /* resource record data */
81 int datalen; /* length of data */ 88 int datalen; /* length of data */
82 const char *newrr_in; /* new rr for modify or append */ 89 const u_char *newrr_in; /* new rr for modify or append */
83 char *buf; /* buffer to put query */ 90 u_char *buf; /* buffer to put query */
84 int buflen; /* size of buffer */ 91 int buflen; /* size of buffer */
85{ 92{
86 register HEADER *hp; 93 register HEADER *hp;
87 register char *cp; 94 register u_char *cp;
88 register int n; 95 register int n;
89 struct rrec *newrr = (struct rrec *) newrr_in; 96 u_char *dnptrs[20], **dpp, **lastdnptr;
90 char *dnptrs[10], **dpp, **lastdnptr;
91 97
98 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
99 h_errno = NETDB_INTERNAL;
100 return (-1);
101 }
92#ifdef DEBUG 102#ifdef DEBUG
93 if (_res.options & RES_DEBUG) 103 if (_res.options & RES_DEBUG)
94 printf(";; res_mkquery(%d, %s, %d, %d)\n", 104 printf(";; res_mkquery(%d, %s, %d, %d)\n",
@@ -96,38 +106,43 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
96#endif 106#endif
97 /* 107 /*
98 * Initialize header fields. 108 * Initialize header fields.
109 *
110 * A special random number generator is used to create non predictable
111 * and non repeating ids over a long period. It also avoids reuse
112 * by switching between two distinct number cycles.
99 */ 113 */
100 if ((buf == NULL) || (buflen < sizeof(HEADER))) 114
101 return(-1); 115 if ((buf == NULL) || (buflen < HFIXEDSZ))
102 bzero(buf, sizeof(HEADER)); 116 return (-1);
117 bzero(buf, HFIXEDSZ);
103 hp = (HEADER *) buf; 118 hp = (HEADER *) buf;
104 hp->id = htons(++_res.id); 119 _res.id = res_randomid();
120 hp->id = htons(_res.id);
105 hp->opcode = op; 121 hp->opcode = op;
106 hp->pr = (_res.options & RES_PRIMARY) != 0;
107 hp->rd = (_res.options & RES_RECURSE) != 0; 122 hp->rd = (_res.options & RES_RECURSE) != 0;
108 hp->rcode = NOERROR; 123 hp->rcode = NOERROR;
109 cp = buf + sizeof(HEADER); 124 cp = buf + HFIXEDSZ;
110 buflen -= sizeof(HEADER); 125 buflen -= HFIXEDSZ;
111 dpp = dnptrs; 126 dpp = dnptrs;
112 *dpp++ = buf; 127 *dpp++ = buf;
113 *dpp++ = NULL; 128 *dpp++ = NULL;
114 lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]); 129 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
115 /* 130 /*
116 * perform opcode specific processing 131 * perform opcode specific processing
117 */ 132 */
118 switch (op) { 133 switch (op) {
119 case QUERY: 134 case QUERY: /*FALLTHROUGH*/
135 case NS_NOTIFY_OP:
120 if ((buflen -= QFIXEDSZ) < 0) 136 if ((buflen -= QFIXEDSZ) < 0)
121 return(-1); 137 return (-1);
122 if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen, 138 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
123 (u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
124 return (-1); 139 return (-1);
125 cp += n; 140 cp += n;
126 buflen -= n; 141 buflen -= n;
127 __putshort(type, (u_char *)cp); 142 __putshort(type, cp);
128 cp += sizeof(u_int16_t); 143 cp += INT16SZ;
129 __putshort(class, (u_char *)cp); 144 __putshort(class, cp);
130 cp += sizeof(u_int16_t); 145 cp += INT16SZ;
131 hp->qdcount = htons(1); 146 hp->qdcount = htons(1);
132 if (op == QUERY || data == NULL) 147 if (op == QUERY || data == NULL)
133 break; 148 break;
@@ -135,19 +150,19 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
135 * Make an additional record for completion domain. 150 * Make an additional record for completion domain.
136 */ 151 */
137 buflen -= RRFIXEDSZ; 152 buflen -= RRFIXEDSZ;
138 if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen, 153 n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
139 (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) 154 if (n < 0)
140 return (-1); 155 return (-1);
141 cp += n; 156 cp += n;
142 buflen -= n; 157 buflen -= n;
143 __putshort(T_NULL, (u_char *)cp); 158 __putshort(T_NULL, cp);
144 cp += sizeof(u_int16_t); 159 cp += INT16SZ;
145 __putshort(class, (u_char *)cp); 160 __putshort(class, cp);
146 cp += sizeof(u_int16_t); 161 cp += INT16SZ;
147 __putlong(0, (u_char *)cp); 162 __putlong(0, cp);
148 cp += sizeof(u_int32_t); 163 cp += INT32SZ;
149 __putshort(0, (u_char *)cp); 164 __putshort(0, cp);
150 cp += sizeof(u_int16_t); 165 cp += INT16SZ;
151 hp->arcount = htons(1); 166 hp->arcount = htons(1);
152 break; 167 break;
153 168
@@ -158,79 +173,23 @@ res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen)
158 if (buflen < 1 + RRFIXEDSZ + datalen) 173 if (buflen < 1 + RRFIXEDSZ + datalen)
159 return (-1); 174 return (-1);
160 *cp++ = '\0'; /* no domain name */ 175 *cp++ = '\0'; /* no domain name */
161 __putshort(type, (u_char *)cp);
162 cp += sizeof(u_int16_t);
163 __putshort(class, (u_char *)cp);
164 cp += sizeof(u_int16_t);
165 __putlong(0, (u_char *)cp);
166 cp += sizeof(u_int32_t);
167 __putshort(datalen, (u_char *)cp);
168 cp += sizeof(u_int16_t);
169 if (datalen) {
170 bcopy(data, cp, datalen);
171 cp += datalen;
172 }
173 hp->ancount = htons(1);
174 break;
175
176#ifdef ALLOW_UPDATES
177 /*
178 * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
179 * (Record to be modified is followed by its replacement in msg.)
180 */
181 case UPDATEM:
182 case UPDATEMA:
183
184 case UPDATED:
185 /*
186 * The res code for UPDATED and UPDATEDA is the same; user
187 * calls them differently: specifies data for UPDATED; server
188 * ignores data if specified for UPDATEDA.
189 */
190 case UPDATEDA:
191 buflen -= RRFIXEDSZ + datalen;
192 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
193 return (-1);
194 cp += n;
195 __putshort(type, cp); 176 __putshort(type, cp);
196 cp += sizeof(u_int16_t); 177 cp += INT16SZ;
197 __putshort(class, cp); 178 __putshort(class, cp);
198 cp += sizeof(u_int16_t); 179 cp += INT16SZ;
199 __putlong(0, cp); 180 __putlong(0, cp);
200 cp += sizeof(u_int32_t); 181 cp += INT32SZ;
201 __putshort(datalen, cp); 182 __putshort(datalen, cp);
202 cp += sizeof(u_int16_t); 183 cp += INT16SZ;
203 if (datalen) { 184 if (datalen) {
204 bcopy(data, cp, datalen); 185 bcopy(data, cp, datalen);
205 cp += datalen; 186 cp += datalen;
206 } 187 }
207 if ( (op == UPDATED) || (op == UPDATEDA) ) { 188 hp->ancount = htons(1);
208 hp->ancount = htons(0);
209 break;
210 }
211 /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
212
213 case UPDATEA: /* Add new resource record */
214 buflen -= RRFIXEDSZ + datalen;
215 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
216 return (-1);
217 cp += n;
218 __putshort(newrr->r_type, cp);
219 cp += sizeof(u_int16_t);
220 __putshort(newrr->r_class, cp);
221 cp += sizeof(u_int16_t);
222 __putlong(0, cp);
223 cp += sizeof(u_int32_t);
224 __putshort(newrr->r_size, cp);
225 cp += sizeof(u_int16_t);
226 if (newrr->r_size) {
227 bcopy(newrr->r_data, cp, newrr->r_size);
228 cp += newrr->r_size;
229 }
230 hp->ancount = htons(0);
231 break; 189 break;
232 190
233#endif /* ALLOW_UPDATES */ 191 default:
192 return (-1);
234 } 193 }
235 return (cp - buf); 194 return (cp - buf);
236} 195}
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c
index 7649462e56..a08897b45a 100644
--- a/src/lib/libc/net/res_query.c
+++ b/src/lib/libc/net/res_query.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $ */ 1/* $OpenBSD: res_query.c,v 1.12 1998/08/31 18:15:29 deraadt Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1988, 1993
5 * -
4 * Copyright (c) 1988, 1993 6 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,23 +58,26 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie "; 61static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: res_query.c,v 1.12 1998/08/31 18:15:29 deraadt Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67#include <sys/types.h>
65#include <sys/param.h> 68#include <sys/param.h>
66#include <netinet/in.h> 69#include <netinet/in.h>
67#include <arpa/inet.h> 70#include <arpa/inet.h>
68#include <arpa/nameser.h> 71#include <arpa/nameser.h>
72
73#include <stdio.h>
69#include <netdb.h> 74#include <netdb.h>
70#include <resolv.h> 75#include <resolv.h>
71#include <stdio.h>
72#include <ctype.h> 76#include <ctype.h>
73#include <errno.h> 77#include <errno.h>
74#include <stdlib.h> 78#include <stdlib.h>
75#include <string.h> 79#include <string.h>
80#include <unistd.h>
76 81
77#if PACKETSZ > 1024 82#if PACKETSZ > 1024
78#define MAXPACKET PACKETSZ 83#define MAXPACKET PACKETSZ
@@ -80,7 +85,7 @@ static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $"
80#define MAXPACKET 1024 85#define MAXPACKET 1024
81#endif 86#endif
82 87
83char *__hostalias __P((const char *)); 88const char *hostalias __P((const char *));
84int h_errno; 89int h_errno;
85 90
86/* 91/*
@@ -90,27 +95,33 @@ int h_errno;
90 * if no error is indicated and the answer count is nonzero. 95 * if no error is indicated and the answer count is nonzero.
91 * Return the size of the response on success, -1 on error. 96 * Return the size of the response on success, -1 on error.
92 * Error number is left in h_errno. 97 * Error number is left in h_errno.
98 *
93 * Caller must parse answer and determine whether it answers the question. 99 * Caller must parse answer and determine whether it answers the question.
94 */ 100 */
101int
95res_query(name, class, type, answer, anslen) 102res_query(name, class, type, answer, anslen)
96 char *name; /* domain name */ 103 const char *name; /* domain name */
97 int class, type; /* class and type of query */ 104 int class, type; /* class and type of query */
98 u_char *answer; /* buffer to put answer */ 105 u_char *answer; /* buffer to put answer */
99 int anslen; /* size of answer buffer */ 106 int anslen; /* size of answer buffer */
100{ 107{
101 char buf[MAXPACKET]; 108 u_char buf[MAXPACKET];
102 HEADER *hp; 109 register HEADER *hp = (HEADER *) answer;
103 int n; 110 int n;
104 111
105 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 112 hp->rcode = NOERROR; /* default */
113
114 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
115 h_errno = NETDB_INTERNAL;
106 return (-1); 116 return (-1);
117 }
107#ifdef DEBUG 118#ifdef DEBUG
108 if (_res.options & RES_DEBUG) 119 if (_res.options & RES_DEBUG)
109 printf(";; res_query(%s, %d, %d)\n", name, class, type); 120 printf(";; res_query(%s, %d, %d)\n", name, class, type);
110#endif 121#endif
111 n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
112 buf, sizeof(buf));
113 122
123 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
124 buf, sizeof(buf));
114 if (n <= 0) { 125 if (n <= 0) {
115#ifdef DEBUG 126#ifdef DEBUG
116 if (_res.options & RES_DEBUG) 127 if (_res.options & RES_DEBUG)
@@ -119,7 +130,7 @@ res_query(name, class, type, answer, anslen)
119 h_errno = NO_RECOVERY; 130 h_errno = NO_RECOVERY;
120 return (n); 131 return (n);
121 } 132 }
122 n = res_send(buf, n, (char *)answer, anslen); 133 n = res_send(buf, n, answer, anslen);
123 if (n < 0) { 134 if (n < 0) {
124#ifdef DEBUG 135#ifdef DEBUG
125 if (_res.options & RES_DEBUG) 136 if (_res.options & RES_DEBUG)
@@ -129,7 +140,6 @@ res_query(name, class, type, answer, anslen)
129 return (n); 140 return (n);
130 } 141 }
131 142
132 hp = (HEADER *) answer;
133 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 143 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
134#ifdef DEBUG 144#ifdef DEBUG
135 if (_res.options & RES_DEBUG) 145 if (_res.options & RES_DEBUG)
@@ -137,21 +147,21 @@ res_query(name, class, type, answer, anslen)
137 ntohs(hp->ancount)); 147 ntohs(hp->ancount));
138#endif 148#endif
139 switch (hp->rcode) { 149 switch (hp->rcode) {
140 case NXDOMAIN: 150 case NXDOMAIN:
141 h_errno = HOST_NOT_FOUND; 151 h_errno = HOST_NOT_FOUND;
142 break; 152 break;
143 case SERVFAIL: 153 case SERVFAIL:
144 h_errno = TRY_AGAIN; 154 h_errno = TRY_AGAIN;
145 break; 155 break;
146 case NOERROR: 156 case NOERROR:
147 h_errno = NO_DATA; 157 h_errno = NO_DATA;
148 break; 158 break;
149 case FORMERR: 159 case FORMERR:
150 case NOTIMP: 160 case NOTIMP:
151 case REFUSED: 161 case REFUSED:
152 default: 162 default:
153 h_errno = NO_RECOVERY; 163 h_errno = NO_RECOVERY;
154 break; 164 break;
155 } 165 }
156 return (-1); 166 return (-1);
157 } 167 }
@@ -162,9 +172,7 @@ res_query(name, class, type, answer, anslen)
162 * Formulate a normal query, send, and retrieve answer in supplied buffer. 172 * Formulate a normal query, send, and retrieve answer in supplied buffer.
163 * Return the size of the response on success, -1 on error. 173 * Return the size of the response on success, -1 on error.
164 * If enabled, implement search rules until answer or unrecoverable failure 174 * If enabled, implement search rules until answer or unrecoverable failure
165 * is detected. Error number is left in h_errno. 175 * is detected. Error code, if any, is left in h_errno.
166 * Only useful for queries in the same name hierarchy as the local host
167 * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
168 */ 176 */
169int 177int
170res_search(name, class, type, answer, anslen) 178res_search(name, class, type, answer, anslen)
@@ -173,28 +181,29 @@ res_search(name, class, type, answer, anslen)
173 u_char *answer; /* buffer to put answer */ 181 u_char *answer; /* buffer to put answer */
174 int anslen; /* size of answer */ 182 int anslen; /* size of answer */
175{ 183{
176 register char *cp, **domain; 184 register const char *cp, * const *domain;
177 int dots, trailing_dot, ret, got_nodata, saved_herrno, tried_as_is; 185 HEADER *hp = (HEADER *) answer;
186 u_int dots;
187 int trailing_dot, ret, saved_herrno;
188 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
178 189
179 if ((_res.options & RES_INIT) == 0 && res_init() == -1) 190 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
191 h_errno = NETDB_INTERNAL;
180 return (-1); 192 return (-1);
181 193 }
182 got_nodata = 0;
183 errno = 0; 194 errno = 0;
184 h_errno = HOST_NOT_FOUND; /* default, if we never query */ 195 h_errno = HOST_NOT_FOUND; /* default, if we never query */
185 dots = 0; 196 dots = 0;
186 for (cp = (char *)name; *cp; cp++) { 197 for (cp = name; *cp; cp++)
187 if (*cp == '.') 198 dots += (*cp == '.');
188 dots++;
189 }
190 trailing_dot = 0; 199 trailing_dot = 0;
191 if ((cp > name) && (*--cp == '.')) 200 if (cp > name && *--cp == '.')
192 trailing_dot++; 201 trailing_dot++;
193 202
194 /* 203 /*
195 * if there aren't any dots, it could be a user-level alias 204 * if there aren't any dots, it could be a user-level alias
196 */ 205 */
197 if (!dots && (cp = __hostalias(name))) 206 if (!dots && (cp = __hostalias(name)) != NULL)
198 return (res_query(cp, class, type, answer, anslen)); 207 return (res_query(cp, class, type, answer, anslen));
199 208
200 /* 209 /*
@@ -202,7 +211,6 @@ res_search(name, class, type, answer, anslen)
202 * 'as is'. The threshold can be set with the "ndots" option. 211 * 'as is'. The threshold can be set with the "ndots" option.
203 */ 212 */
204 saved_herrno = -1; 213 saved_herrno = -1;
205 tried_as_is = 0;
206 if (dots >= _res.ndots) { 214 if (dots >= _res.ndots) {
207 ret = res_querydomain(name, NULL, class, type, answer, anslen); 215 ret = res_querydomain(name, NULL, class, type, answer, anslen);
208 if (ret > 0) 216 if (ret > 0)
@@ -219,13 +227,17 @@ res_search(name, class, type, answer, anslen)
219 */ 227 */
220 if ((!dots && (_res.options & RES_DEFNAMES)) || 228 if ((!dots && (_res.options & RES_DEFNAMES)) ||
221 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 229 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
222 for (domain = _res.dnsrch; *domain; domain++) { 230 int done = 0;
223 int done = 0; 231
232 for (domain = (const char * const *)_res.dnsrch;
233 *domain && !done;
234 domain++) {
224 235
225 ret = res_querydomain(name, *domain, class, type, 236 ret = res_querydomain(name, *domain, class, type,
226 answer, anslen); 237 answer, anslen);
227 if (ret > 0) 238 if (ret > 0)
228 return (ret); 239 return (ret);
240
229 /* 241 /*
230 * If no server present, give up. 242 * If no server present, give up.
231 * If name isn't found in this domain, 243 * If name isn't found in this domain,
@@ -251,24 +263,27 @@ res_search(name, class, type, answer, anslen)
251 case HOST_NOT_FOUND: 263 case HOST_NOT_FOUND:
252 /* keep trying */ 264 /* keep trying */
253 break; 265 break;
266 case TRY_AGAIN:
267 if (hp->rcode == SERVFAIL) {
268 /* try next search element, if any */
269 got_servfail++;
270 break;
271 }
272 /* FALLTHROUGH */
254 default: 273 default:
255 /* anything else implies that we're done */ 274 /* anything else implies that we're done */
256 done++; 275 done++;
257 } 276 }
258 /* 277
259 * if we got here for some reason other than DNSRCH, 278 /* if we got here for some reason other than DNSRCH,
260 * we only wanted one iteration of the loop, so stop. 279 * we only wanted one iteration of the loop, so stop.
261 */ 280 */
262 if (!(_res.options & RES_DNSRCH)) 281 if (!(_res.options & RES_DNSRCH))
263 done++; 282 done++;
264
265 if (done)
266 break;
267 } 283 }
268 } 284 }
269 285
270 /* 286 /* if we have not already tried the name "as is", do that now.
271 * if we have not already tried the name "as is", do that now.
272 * note that we do this regardless of how many dots were in the 287 * note that we do this regardless of how many dots were in the
273 * name or whether it ends with a dot. 288 * name or whether it ends with a dot.
274 */ 289 */
@@ -276,11 +291,9 @@ res_search(name, class, type, answer, anslen)
276 ret = res_querydomain(name, NULL, class, type, answer, anslen); 291 ret = res_querydomain(name, NULL, class, type, answer, anslen);
277 if (ret > 0) 292 if (ret > 0)
278 return (ret); 293 return (ret);
279 saved_herrno = h_errno;
280 } 294 }
281 295
282 /* 296 /* if we got here, we didn't satisfy the search.
283 * if we got here, we didn't satisfy the search.
284 * if we did an initial full query, return that query's h_errno 297 * if we did an initial full query, return that query's h_errno
285 * (note that we wouldn't be here if that query had succeeded). 298 * (note that we wouldn't be here if that query had succeeded).
286 * else if we ever got a nodata, send that back as the reason. 299 * else if we ever got a nodata, send that back as the reason.
@@ -291,6 +304,8 @@ res_search(name, class, type, answer, anslen)
291 h_errno = saved_herrno; 304 h_errno = saved_herrno;
292 else if (got_nodata) 305 else if (got_nodata)
293 h_errno = NO_DATA; 306 h_errno = NO_DATA;
307 else if (got_servfail)
308 h_errno = TRY_AGAIN;
294 return (-1); 309 return (-1);
295} 310}
296 311
@@ -298,20 +313,25 @@ res_search(name, class, type, answer, anslen)
298 * Perform a call on res_query on the concatenation of name and domain, 313 * Perform a call on res_query on the concatenation of name and domain,
299 * removing a trailing dot from name if domain is NULL. 314 * removing a trailing dot from name if domain is NULL.
300 */ 315 */
316int
301res_querydomain(name, domain, class, type, answer, anslen) 317res_querydomain(name, domain, class, type, answer, anslen)
302 char *name, *domain; 318 const char *name, *domain;
303 int class, type; /* class and type of query */ 319 int class, type; /* class and type of query */
304 u_char *answer; /* buffer to put answer */ 320 u_char *answer; /* buffer to put answer */
305 int anslen; /* size of answer */ 321 int anslen; /* size of answer */
306{ 322{
307 char nbuf[2*MAXDNAME+2]; 323 char nbuf[MAXDNAME*2+1];
308 char *longname = nbuf; 324 const char *longname = nbuf;
309 int n; 325 int n;
310 326
327 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
328 h_errno = NETDB_INTERNAL;
329 return (-1);
330 }
311#ifdef DEBUG 331#ifdef DEBUG
312 if (_res.options & RES_DEBUG) 332 if (_res.options & RES_DEBUG)
313 printf(";; res_querydomain(%s, %s, %d, %d)\n", 333 printf(";; res_querydomain(%s, %s, %d, %d)\n",
314 name, domain, class, type); 334 name, domain?domain:"<Nil>", class, type);
315#endif 335#endif
316 if (domain == NULL) { 336 if (domain == NULL) {
317 /* 337 /*
@@ -325,38 +345,50 @@ res_querydomain(name, domain, class, type, answer, anslen)
325 } else 345 } else
326 longname = name; 346 longname = name;
327 } else 347 } else
328 (void)sprintf(nbuf, "%.*s.%.*s", 348 sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain);
329 MAXDNAME, name, MAXDNAME, domain);
330 349
331 return (res_query(longname, class, type, answer, anslen)); 350 return (res_query(longname, class, type, answer, anslen));
332} 351}
333 352
334char * 353const char *
335__hostalias(name) 354hostalias(name)
336 register const char *name; 355 register const char *name;
337{ 356{
338 register char *cp1, *cp2; 357 register char *cp1, *cp2;
339 FILE *fp; 358 FILE *fp;
340 char *file, *getenv(), *strcpy(), *strncpy(); 359 char *file;
341 char buf[BUFSIZ]; 360 char buf[BUFSIZ];
342 static char abuf[MAXDNAME]; 361 static char abuf[MAXDNAME];
362 size_t len;
343 363
364 if (_res.options & RES_NOALIASES)
365 return (NULL);
344 file = getenv("HOSTALIASES"); 366 file = getenv("HOSTALIASES");
345 if (file == NULL || (fp = fopen(file, "r")) == NULL) 367 if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL)
346 return (NULL); 368 return (NULL);
347 buf[sizeof(buf) - 1] = '\0'; 369 setbuf(fp, NULL);
348 while (fgets(buf, sizeof(buf), fp)) { 370 while ((cp1 = fgetln(fp, &len)) != NULL) {
349 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1); 371 if (cp1[len-1] == '\n')
372 len--;
373 if (len >= sizeof(buf) || len == 0)
374 continue;
375 (void)memcpy(buf, cp1, len);
376 buf[len] = '\0';
377
378 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
379 ;
350 if (!*cp1) 380 if (!*cp1)
351 break; 381 break;
352 *cp1 = '\0'; 382 *cp1 = '\0';
353 if (!strcasecmp(buf, name)) { 383 if (!strcasecmp(buf, name)) {
354 while (isspace(*++cp1)); 384 while (isspace(*++cp1))
385 ;
355 if (!*cp1) 386 if (!*cp1)
356 break; 387 break;
357 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2); 388 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
389 ;
390 strncpy(abuf, cp1, sizeof(abuf) - 1);
358 abuf[sizeof(abuf) - 1] = *cp2 = '\0'; 391 abuf[sizeof(abuf) - 1] = *cp2 = '\0';
359 (void)strncpy(abuf, cp1, sizeof(abuf) - 1);
360 fclose(fp); 392 fclose(fp);
361 return (abuf); 393 return (abuf);
362 } 394 }
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c
new file mode 100644
index 0000000000..bd32a50c33
--- /dev/null
+++ b/src/lib/libc/net/res_random.c
@@ -0,0 +1,233 @@
1/* $OpenBSD: res_random.c,v 1.7 1997/07/25 20:30:08 mickey Exp $ */
2
3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * All rights reserved.
6 *
7 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
8 * such a mathematical system to generate more random (yet non-repeating)
9 * ids to solve the resolver/named problem. But Niels designed the
10 * actual system based on the constraints.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Niels Provos.
23 * 4. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * seed = random 15bit
40 * n = prime, g0 = generator to n,
41 * j = random so that gcd(j,n-1) == 1
42 * g = g0^j mod n will be a generator again.
43 *
44 * X[0] = random seed.
45 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
46 * with a = 7^(even random) mod m,
47 * b = random with gcd(b,m) == 1
48 * m = 31104 and a maximal period of m-1.
49 *
50 * The transaction id is determined by:
51 * id[n] = seed xor (g^X[n] mod n)
52 *
53 * Effectivly the id is restricted to the lower 15 bits, thus
54 * yielding two different cycles by toggling the msb on and off.
55 * This avoids reuse issues caused by reseeding.
56 *
57 * The 16 bit space is very small and brute force attempts are
58 * entirly feasible, we skip a random number of transaction ids
59 * so that an attacker will not get sequential ids.
60 */
61
62#include <sys/types.h>
63#include <netinet/in.h>
64#include <sys/time.h>
65#include <resolv.h>
66
67#include <unistd.h>
68#include <stdlib.h>
69#include <string.h>
70
71#define RU_OUT 180 /* Time after wich will be reseeded */
72#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */
73#define RU_GEN 2 /* Starting generator */
74#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */
75#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */
76#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */
77
78#define PFAC_N 3
79const static u_int16_t pfacts[PFAC_N] = {
80 2,
81 3,
82 2729
83};
84
85static u_int16_t ru_x;
86static u_int16_t ru_seed;
87static u_int16_t ru_a, ru_b;
88static u_int16_t ru_g;
89static u_int16_t ru_counter = 0;
90static u_int16_t ru_msb = 0;
91static long ru_reseed;
92static u_int32_t tmp; /* Storage for unused random */
93static struct timeval tv;
94
95static u_int16_t pmod __P((u_int16_t, u_int16_t, u_int16_t));
96static void res_initid __P((void));
97
98/*
99 * Do a fast modular exponation, returned value will be in the range
100 * of 0 - (mod-1)
101 */
102
103#ifdef __STDC__
104static u_int16_t
105pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod)
106#else
107static u_int16_t
108pmod(gen, exp, mod)
109 u_int16_t gen, exp, mod;
110#endif
111{
112 u_int16_t s, t, u;
113
114 s = 1;
115 t = gen;
116 u = exp;
117
118 while (u) {
119 if (u & 1)
120 s = (s*t) % mod;
121 u >>= 1;
122 t = (t*t) % mod;
123 }
124 return (s);
125}
126
127/*
128 * Initalizes the seed and chooses a suitable generator. Also toggles
129 * the msb flag. The msb flag is used to generate two distinct
130 * cycles of random numbers and thus avoiding reuse of ids.
131 *
132 * This function is called from res_randomid() when needed, an
133 * application does not have to worry about it.
134 */
135static void
136res_initid()
137{
138 u_int16_t j, i;
139 int noprime = 1;
140
141 tmp = arc4random();
142 ru_x = (tmp & 0xFFFF) % RU_M;
143
144 /* 15 bits of random seed */
145 ru_seed = (tmp >> 16) & 0x7FFF;
146
147 tmp = arc4random();
148
149 /* Determine the LCG we use */
150 ru_b = (tmp & 0xfffe) | 1;
151 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
152 while (ru_b % 3 == 0)
153 ru_b += 2;
154
155 tmp = arc4random();
156 j = tmp % RU_N;
157 tmp = tmp >> 16;
158
159 /*
160 * Do a fast gcd(j,RU_N-1), so we can find a j with
161 * gcd(j, RU_N-1) == 1, giving a new generator for
162 * RU_GEN^j mod RU_N
163 */
164
165 while (noprime) {
166 for (i=0; i<PFAC_N; i++)
167 if (j%pfacts[i] == 0)
168 break;
169
170 if (i>=PFAC_N)
171 noprime = 0;
172 else
173 j = (j+1) % RU_N;
174 }
175
176 ru_g = pmod(RU_GEN,j,RU_N);
177 ru_counter = 0;
178
179 gettimeofday(&tv, NULL);
180 ru_reseed = tv.tv_sec + RU_OUT;
181 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
182}
183
184u_int
185res_randomid()
186{
187 int i, n;
188
189 gettimeofday(&tv, NULL);
190 if (ru_counter >= RU_MAX || tv.tv_sec > ru_reseed)
191 res_initid();
192
193 if (!tmp)
194 tmp = arc4random();
195
196 /* Skip a random number of ids */
197 n = tmp & 0x7; tmp = tmp >> 3;
198 if (ru_counter + n >= RU_MAX)
199 res_initid();
200
201 for (i=0; i<=n; i++)
202 /* Linear Congruential Generator */
203 ru_x = (ru_a*ru_x + ru_b) % RU_M;
204
205 ru_counter += i;
206
207 return (ru_seed ^ pmod(ru_g,ru_x,RU_N)) | ru_msb;
208}
209
210#if 0
211void
212main(int argc, char **argv)
213{
214 int i, n;
215 u_int16_t wert;
216
217 res_initid();
218
219 printf("Generator: %d\n", ru_g);
220 printf("Seed: %d\n", ru_seed);
221 printf("Reseed at %ld\n", ru_reseed);
222 printf("Ru_X: %d\n", ru_x);
223 printf("Ru_A: %d\n", ru_a);
224 printf("Ru_B: %d\n", ru_b);
225
226 n = atoi(argv[1]);
227 for (i=0;i<n;i++) {
228 wert = res_randomid();
229 printf("%06d\n", wert);
230 }
231}
232#endif
233
diff --git a/src/lib/libc/net/res_send.c b/src/lib/libc/net/res_send.c
index e608358180..0cda3510eb 100644
--- a/src/lib/libc/net/res_send.c
+++ b/src/lib/libc/net/res_send.c
@@ -1,9 +1,11 @@
1/* $NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $ */ 1/* $OpenBSD: res_send.c,v 1.8 1998/03/19 00:30:08 millert Exp $ */
2 2
3/*- 3/*
4 * ++Copyright++ 1985, 1989, 1993
5 * -
4 * Copyright (c) 1985, 1989, 1993 6 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved. 7 * The Regents of the University of California. All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
9 * are met: 11 * are met:
@@ -14,12 +16,12 @@
14 * documentation and/or other materials provided with the distribution. 16 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 17 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 18 * must display the following acknowledgement:
17 * This product includes software developed by the University of 19 * This product includes software developed by the University of
18 * California, Berkeley and its contributors. 20 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors 21 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software 22 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission. 23 * without specific prior written permission.
22 * 24 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,16 +58,24 @@
56#if defined(LIBC_SCCS) && !defined(lint) 58#if defined(LIBC_SCCS) && !defined(lint)
57#if 0 59#if 0
58static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; 60static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
59static char rcsid[] = "$Id: res_send.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; 61static char rcsid[] = "$From: res_send.c,v 8.12 1996/10/08 04:51:06 vixie Exp $";
60#else 62#else
61static char rcsid[] = "$NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $"; 63static char rcsid[] = "$OpenBSD: res_send.c,v 1.8 1998/03/19 00:30:08 millert Exp $";
62#endif 64#endif
63#endif /* LIBC_SCCS and not lint */ 65#endif /* LIBC_SCCS and not lint */
64 66
67 /* change this to "0"
68 * if you talk to a lot
69 * of multi-homed SunOS
70 * ("broken") name servers.
71 */
72#define CHECK_SRVR_ADDR 1 /* XXX - should be in options.h */
73
65/* 74/*
66 * Send query to name server and wait for reply. 75 * Send query to name server and wait for reply.
67 */ 76 */
68 77
78#include <sys/types.h>
69#include <sys/param.h> 79#include <sys/param.h>
70#include <sys/time.h> 80#include <sys/time.h>
71#include <sys/socket.h> 81#include <sys/socket.h>
@@ -73,16 +83,21 @@ static char rcsid[] = "$NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $";
73#include <netinet/in.h> 83#include <netinet/in.h>
74#include <arpa/nameser.h> 84#include <arpa/nameser.h>
75#include <arpa/inet.h> 85#include <arpa/inet.h>
86
76#include <stdio.h> 87#include <stdio.h>
88#include <netdb.h>
77#include <errno.h> 89#include <errno.h>
78#include <resolv.h> 90#include <resolv.h>
79#include <unistd.h> 91#include <stdlib.h>
80#include <string.h> 92#include <string.h>
93#include <unistd.h>
81 94
82static int s = -1; /* socket used for communications */ 95static int s = -1; /* socket used for communications */
83static struct sockaddr no_addr; 96static int connected = 0; /* is the socket connected */
97static int vc = 0; /* is the socket a virtual ciruit? */
84 98
85#ifndef FD_SET 99#ifndef FD_SET
100/* XXX - should be in portability.h */
86#define NFDBITS 32 101#define NFDBITS 32
87#define FD_SETSIZE 32 102#define FD_SETSIZE 32
88#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) 103#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
@@ -91,121 +106,313 @@ static struct sockaddr no_addr;
91#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) 106#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
92#endif 107#endif
93 108
94res_send(buf, buflen, answer, anslen) 109#define CAN_RECONNECT 1
95 const char *buf; 110
111#ifndef DEBUG
112# define Dprint(cond, args) /*empty*/
113# define DprintQ(cond, args, query, size) /*empty*/
114# define Aerror(file, string, error, address) /*empty*/
115# define Perror(file, string, error) /*empty*/
116#else
117# define Dprint(cond, args) if (cond) {fprintf args;} else {}
118# define DprintQ(cond, args, query, size) if (cond) {\
119 fprintf args;\
120 __fp_nquery(query, size, stdout);\
121 } else {}
122 static void
123 Aerror(file, string, error, address)
124 FILE *file;
125 char *string;
126 int error;
127 struct sockaddr_in address;
128 {
129 int save = errno;
130
131 if (_res.options & RES_DEBUG) {
132 fprintf(file, "res_send: %s ([%s].%u): %s\n",
133 string,
134 inet_ntoa(address.sin_addr),
135 ntohs(address.sin_port),
136 strerror(error));
137 }
138 errno = save;
139 }
140 static void
141 Perror(file, string, error)
142 FILE *file;
143 char *string;
144 int error;
145 {
146 int save = errno;
147
148 if (_res.options & RES_DEBUG) {
149 fprintf(file, "res_send: %s: %s\n",
150 string, strerror(error));
151 }
152 errno = save;
153 }
154#endif
155
156static res_send_qhook Qhook = NULL;
157static res_send_rhook Rhook = NULL;
158
159void
160res_send_setqhook(hook)
161 res_send_qhook hook;
162{
163
164 Qhook = hook;
165}
166
167void
168res_send_setrhook(hook)
169 res_send_rhook hook;
170{
171
172 Rhook = hook;
173}
174
175/* int
176 * res_isourserver(ina)
177 * looks up "ina" in _res.ns_addr_list[]
178 * returns:
179 * 0 : not found
180 * >0 : found
181 * author:
182 * paul vixie, 29may94
183 */
184int
185res_isourserver(inp)
186 const struct sockaddr_in *inp;
187{
188 struct sockaddr_in ina;
189 register int ns, ret;
190
191 ina = *inp;
192 ret = 0;
193 for (ns = 0; ns < _res.nscount; ns++) {
194 register const struct sockaddr_in *srv = &_res.nsaddr_list[ns];
195
196 if (srv->sin_family == ina.sin_family &&
197 srv->sin_port == ina.sin_port &&
198 (srv->sin_addr.s_addr == INADDR_ANY ||
199 srv->sin_addr.s_addr == ina.sin_addr.s_addr)) {
200 ret++;
201 break;
202 }
203 }
204 return (ret);
205}
206
207/* int
208 * res_nameinquery(name, type, class, buf, eom)
209 * look for (name,type,class) in the query section of packet (buf,eom)
210 * returns:
211 * -1 : format error
212 * 0 : not found
213 * >0 : found
214 * author:
215 * paul vixie, 29may94
216 */
217int
218res_nameinquery(name, type, class, buf, eom)
219 const char *name;
220 register int type, class;
221 const u_char *buf, *eom;
222{
223 register const u_char *cp = buf + HFIXEDSZ;
224 int qdcount = ntohs(((HEADER*)buf)->qdcount);
225
226 while (qdcount-- > 0) {
227 char tname[MAXDNAME+1];
228 register int n, ttype, tclass;
229
230 n = dn_expand(buf, eom, cp, tname, sizeof tname);
231 if (n < 0)
232 return (-1);
233 cp += n;
234 ttype = _getshort(cp); cp += INT16SZ;
235 tclass = _getshort(cp); cp += INT16SZ;
236 if (ttype == type &&
237 tclass == class &&
238 strcasecmp(tname, name) == 0)
239 return (1);
240 }
241 return (0);
242}
243
244/* int
245 * res_queriesmatch(buf1, eom1, buf2, eom2)
246 * is there a 1:1 mapping of (name,type,class)
247 * in (buf1,eom1) and (buf2,eom2)?
248 * returns:
249 * -1 : format error
250 * 0 : not a 1:1 mapping
251 * >0 : is a 1:1 mapping
252 * author:
253 * paul vixie, 29may94
254 */
255int
256res_queriesmatch(buf1, eom1, buf2, eom2)
257 const u_char *buf1, *eom1;
258 const u_char *buf2, *eom2;
259{
260 register const u_char *cp = buf1 + HFIXEDSZ;
261 int qdcount = ntohs(((HEADER*)buf1)->qdcount);
262
263 if (qdcount != ntohs(((HEADER*)buf2)->qdcount))
264 return (0);
265 while (qdcount-- > 0) {
266 char tname[MAXDNAME+1];
267 register int n, ttype, tclass;
268
269 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
270 if (n < 0)
271 return (-1);
272 cp += n;
273 ttype = _getshort(cp); cp += INT16SZ;
274 tclass = _getshort(cp); cp += INT16SZ;
275 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
276 return (0);
277 }
278 return (1);
279}
280
281int
282res_send(buf, buflen, ans, anssiz)
283 const u_char *buf;
96 int buflen; 284 int buflen;
97 char *answer; 285 u_char *ans;
98 int anslen; 286 int anssiz;
99{ 287{
100 register int n;
101 int try, v_circuit, resplen, ns;
102 int gotsomewhere = 0, connected = 0;
103 int connreset = 0;
104 u_short id, len;
105 char *cp;
106 fd_set dsmask;
107 struct timeval timeout;
108 HEADER *hp = (HEADER *) buf; 288 HEADER *hp = (HEADER *) buf;
109 HEADER *anhp = (HEADER *) answer; 289 HEADER *anhp = (HEADER *) ans;
110 u_int badns; /* XXX NSMAX can't exceed #/bits per this */ 290 int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns;
111 struct iovec iov[2]; 291 register int n;
112 int terrno = ETIMEDOUT; 292 u_int badns; /* XXX NSMAX can't exceed #/bits in this var */
113 char junk[512]; 293
114 294 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
115#ifdef DEBUG 295 /* errno should have been set by res_init() in this case. */
116 if ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY)) { 296 return (-1);
117 printf(";; res_send()\n");
118 __p_query(buf);
119 } 297 }
120#endif 298 DprintQ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY),
121 if (!(_res.options & RES_INIT)) 299 (stdout, ";; res_send()\n"), buf, buflen);
122 if (res_init() == -1) {
123 return(-1);
124 }
125 v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; 300 v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
126 id = hp->id; 301 gotsomewhere = 0;
302 connreset = 0;
303 terrno = ETIMEDOUT;
127 badns = 0; 304 badns = 0;
305
128 /* 306 /*
129 * Send request, RETRY times, or until successful 307 * Send request, RETRY times, or until successful
130 */ 308 */
131 for (try = 0; try < _res.retry; try++) { 309 for (try = 0; try < _res.retry; try++) {
132 for (ns = 0; ns < _res.nscount; ns++) { 310 for (ns = 0; ns < _res.nscount; ns++) {
133 if (badns & (1<<ns)) 311 struct sockaddr_in *nsap = &_res.nsaddr_list[ns];
134 continue; 312 same_ns:
135#ifdef DEBUG 313 if (badns & (1 << ns)) {
136 if (_res.options & RES_DEBUG) 314 res_close();
137 printf(";; Querying server (# %d) address = %s\n", 315 goto next_ns;
138 ns+1, 316 }
139 inet_ntoa(_res.nsaddr_list[ns].sin_addr)); 317
140#endif 318 if (Qhook) {
141 usevc: 319 int done = 0, loops = 0;
320
321 do {
322 res_sendhookact act;
323
324 act = (*Qhook)(&nsap, &buf, &buflen,
325 ans, anssiz, &resplen);
326 switch (act) {
327 case res_goahead:
328 done = 1;
329 break;
330 case res_nextns:
331 res_close();
332 goto next_ns;
333 case res_done:
334 return (resplen);
335 case res_modified:
336 /* give the hook another try */
337 if (++loops < 42) /*doug adams*/
338 break;
339 /*FALLTHROUGH*/
340 case res_error:
341 /*FALLTHROUGH*/
342 default:
343 return (-1);
344 }
345 } while (!done);
346 }
347
348 Dprint(_res.options & RES_DEBUG,
349 (stdout, ";; Querying server (# %d) address = %s\n",
350 ns + 1, inet_ntoa(nsap->sin_addr)));
351
142 if (v_circuit) { 352 if (v_circuit) {
143 int truncated = 0; 353 int truncated;
354 struct iovec iov[2];
355 u_short len;
356 u_char *cp;
144 357
145 /* 358 /*
146 * Use virtual circuit; 359 * Use virtual circuit;
147 * at most one attempt per server. 360 * at most one attempt per server.
148 */ 361 */
149 try = _res.retry; 362 try = _res.retry;
150 if (s < 0) { 363 truncated = 0;
151 s = socket(AF_INET, SOCK_STREAM, 0); 364 if ((s < 0) || (!vc)) {
365 if (s >= 0)
366 res_close();
367
368 s = socket(PF_INET, SOCK_STREAM, 0);
152 if (s < 0) { 369 if (s < 0) {
153 terrno = errno; 370 terrno = errno;
154#ifdef DEBUG 371 Perror(stderr, "socket(vc)", errno);
155 if (_res.options & RES_DEBUG) 372 return (-1);
156 perror("socket (vc) failed");
157#endif
158 continue;
159 } 373 }
160 if (connect(s, 374 errno = 0;
161 (struct sockaddr *)&(_res.nsaddr_list[ns]), 375 if (connect(s, (struct sockaddr *)nsap,
162 sizeof(struct sockaddr)) < 0) { 376 sizeof(struct sockaddr)) < 0) {
163 terrno = errno; 377 terrno = errno;
164#ifdef DEBUG 378 Aerror(stderr, "connect/vc",
165 if (_res.options & RES_DEBUG) 379 errno, *nsap);
166 perror("connect failed"); 380 badns |= (1 << ns);
167#endif 381 res_close();
168 (void) close(s); 382 goto next_ns;
169 s = -1;
170 continue;
171 } 383 }
384 vc = 1;
172 } 385 }
173 /* 386 /*
174 * Send length & message 387 * Send length & message
175 */ 388 */
176 len = htons((u_short)buflen); 389 putshort((u_short)buflen, (u_char*)&len);
177 iov[0].iov_base = (caddr_t)&len; 390 iov[0].iov_base = (caddr_t)&len;
178 iov[0].iov_len = sizeof(len); 391 iov[0].iov_len = INT16SZ;
179 iov[1].iov_base = (char *)buf; 392 iov[1].iov_base = (caddr_t)buf;
180 iov[1].iov_len = buflen; 393 iov[1].iov_len = buflen;
181 if (writev(s, iov, 2) != sizeof(len) + buflen) { 394 if (writev(s, iov, 2) != (INT16SZ + buflen)) {
182 terrno = errno; 395 terrno = errno;
183#ifdef DEBUG 396 Perror(stderr, "write failed", errno);
184 if (_res.options & RES_DEBUG) 397 badns |= (1 << ns);
185 perror("write failed"); 398 res_close();
186#endif 399 goto next_ns;
187 (void) close(s);
188 s = -1;
189 continue;
190 } 400 }
191 /* 401 /*
192 * Receive length & response 402 * Receive length & response
193 */ 403 */
194 cp = answer; 404read_len:
195 len = sizeof(short); 405 cp = ans;
196 while (len != 0 && 406 len = INT16SZ;
197 (n = read(s, (char *)cp, (int)len)) > 0) { 407 while ((n = read(s, (char *)cp, (int)len)) > 0) {
198 cp += n; 408 cp += n;
199 len -= n; 409 if ((len -= n) <= 0)
410 break;
200 } 411 }
201 if (n <= 0) { 412 if (n <= 0) {
202 terrno = errno; 413 terrno = errno;
203#ifdef DEBUG 414 Perror(stderr, "read failed", errno);
204 if (_res.options & RES_DEBUG) 415 res_close();
205 perror("read failed");
206#endif
207 (void) close(s);
208 s = -1;
209 /* 416 /*
210 * A long running process might get its TCP 417 * A long running process might get its TCP
211 * connection reset if the remote server was 418 * connection reset if the remote server was
@@ -217,35 +424,32 @@ res_send(buf, buflen, answer, anslen)
217 */ 424 */
218 if (terrno == ECONNRESET && !connreset) { 425 if (terrno == ECONNRESET && !connreset) {
219 connreset = 1; 426 connreset = 1;
220 ns--; 427 res_close();
428 goto same_ns;
221 } 429 }
222 continue; 430 res_close();
431 goto next_ns;
223 } 432 }
224 cp = answer; 433 resplen = _getshort(ans);
225 if ((resplen = ntohs(*(u_short *)cp)) > anslen) { 434 if (resplen > anssiz) {
226#ifdef DEBUG 435 Dprint(_res.options & RES_DEBUG,
227 if (_res.options & RES_DEBUG) 436 (stdout, ";; response truncated\n")
228 fprintf(stderr, 437 );
229 ";; response truncated\n");
230#endif
231 len = anslen;
232 truncated = 1; 438 truncated = 1;
439 len = anssiz;
233 } else 440 } else
234 len = resplen; 441 len = resplen;
442 cp = ans;
235 while (len != 0 && 443 while (len != 0 &&
236 (n = read(s, (char *)cp, (int)len)) > 0) { 444 (n = read(s, (char *)cp, (int)len)) > 0) {
237 cp += n; 445 cp += n;
238 len -= n; 446 len -= n;
239 } 447 }
240 if (n <= 0) { 448 if (n <= 0) {
241 terrno = errno; 449 terrno = errno;
242#ifdef DEBUG 450 Perror(stderr, "read(vc)", errno);
243 if (_res.options & RES_DEBUG) 451 res_close();
244 perror("read failed"); 452 goto next_ns;
245#endif
246 (void) close(s);
247 s = -1;
248 continue;
249 } 453 }
250 if (truncated) { 454 if (truncated) {
251 /* 455 /*
@@ -253,33 +457,57 @@ res_send(buf, buflen, answer, anslen)
253 * so connection stays in synch. 457 * so connection stays in synch.
254 */ 458 */
255 anhp->tc = 1; 459 anhp->tc = 1;
256 len = resplen - anslen; 460 len = resplen - anssiz;
257 while (len != 0) { 461 while (len != 0) {
258 n = (len > sizeof(junk) ? 462 char junk[PACKETSZ];
259 sizeof(junk) : len); 463
464 n = (len > sizeof(junk)
465 ? sizeof(junk)
466 : len);
260 if ((n = read(s, junk, n)) > 0) 467 if ((n = read(s, junk, n)) > 0)
261 len -= n; 468 len -= n;
262 else 469 else
263 break; 470 break;
264 } 471 }
265 } 472 }
473 /*
474 * The calling applicating has bailed out of
475 * a previous call and failed to arrange to have
476 * the circuit closed or the server has got
477 * itself confused. Anyway drop the packet and
478 * wait for the correct one.
479 */
480 if (hp->id != anhp->id) {
481 DprintQ((_res.options & RES_DEBUG) ||
482 (_res.pfcode & RES_PRF_REPLY),
483 (stdout, ";; old answer (unexpected):\n"),
484 ans, (resplen>anssiz)?anssiz:resplen);
485 goto read_len;
486 }
266 } else { 487 } else {
267 /* 488 /*
268 * Use datagrams. 489 * Use datagrams.
269 */ 490 */
270 if (s < 0) { 491 struct timeval timeout;
271 s = socket(AF_INET, SOCK_DGRAM, 0); 492 fd_set *dsmaskp;
493 struct sockaddr_in from;
494 int fromlen;
495
496 if ((s < 0) || vc) {
497 if (vc)
498 res_close();
499 s = socket(PF_INET, SOCK_DGRAM, 0);
272 if (s < 0) { 500 if (s < 0) {
273 terrno = errno; 501#if !CAN_RECONNECT
274#ifdef DEBUG 502 bad_dg_sock:
275 if (_res.options & RES_DEBUG)
276 perror("socket (dg) failed");
277#endif 503#endif
278 continue; 504 terrno = errno;
505 Perror(stderr, "socket(dg)", errno);
506 return (-1);
279 } 507 }
508 connected = 0;
280 } 509 }
281 /* 510 /*
282 * I'm tired of answering this question, so:
283 * On a 4.3BSD+ machine (client and server, 511 * On a 4.3BSD+ machine (client and server,
284 * actually), sending to a nameserver datagram 512 * actually), sending to a nameserver datagram
285 * port with no nameserver will cause an 513 * port with no nameserver will cause an
@@ -296,29 +524,27 @@ res_send(buf, buflen, answer, anslen)
296 */ 524 */
297 if (_res.nscount == 1 || (try == 0 && ns == 0)) { 525 if (_res.nscount == 1 || (try == 0 && ns == 0)) {
298 /* 526 /*
299 * Don't use connect if we might 527 * Connect only if we are sure we won't
300 * still receive a response 528 * receive a response from another server.
301 * from another server.
302 */ 529 */
303 if (connected == 0) { 530 if (!connected) {
304 if (connect(s, 531 if (connect(s, (struct sockaddr *)nsap,
305 (struct sockaddr *) 532 sizeof(struct sockaddr)
306 &_res.nsaddr_list[ns], 533 ) < 0) {
307 sizeof(struct sockaddr)) < 0) { 534 Aerror(stderr,
308#ifdef DEBUG 535 "connect(dg)",
309 if (_res.options & RES_DEBUG) 536 errno, *nsap);
310 perror("connect"); 537 badns |= (1 << ns);
311#endif 538 res_close();
312 continue; 539 goto next_ns;
313 } 540 }
314 connected = 1; 541 connected = 1;
315 } 542 }
316 if (send(s, buf, buflen, 0) != buflen) { 543 if (send(s, (char*)buf, buflen, 0) != buflen) {
317#ifdef DEBUG 544 Perror(stderr, "send", errno);
318 if (_res.options & RES_DEBUG) 545 badns |= (1 << ns);
319 perror("send"); 546 res_close();
320#endif 547 goto next_ns;
321 continue;
322 } 548 }
323 } else { 549 } else {
324 /* 550 /*
@@ -326,18 +552,36 @@ res_send(buf, buflen, answer, anslen)
326 * for responses from more than one server. 552 * for responses from more than one server.
327 */ 553 */
328 if (connected) { 554 if (connected) {
329 (void) connect(s, &no_addr, 555#if CAN_RECONNECT
330 sizeof(no_addr)); 556 struct sockaddr_in no_addr;
557
558 no_addr.sin_family = AF_INET;
559 no_addr.sin_addr.s_addr = INADDR_ANY;
560 no_addr.sin_port = 0;
561 (void) connect(s,
562 (struct sockaddr *)
563 &no_addr,
564 sizeof(no_addr));
565#else
566 int s1 = socket(PF_INET, SOCK_DGRAM,0);
567 if (s1 < 0)
568 goto bad_dg_sock;
569 (void) dup2(s1, s);
570 (void) close(s1);
571 Dprint(_res.options & RES_DEBUG,
572 (stdout, ";; new DG socket\n"))
573#endif
331 connected = 0; 574 connected = 0;
575 errno = 0;
332 } 576 }
333 if (sendto(s, buf, buflen, 0, 577 if (sendto(s, (char*)buf, buflen, 0,
334 (struct sockaddr *)&_res.nsaddr_list[ns], 578 (struct sockaddr *)nsap,
335 sizeof(struct sockaddr)) != buflen) { 579 sizeof(struct sockaddr))
336#ifdef DEBUG 580 != buflen) {
337 if (_res.options & RES_DEBUG) 581 Aerror(stderr, "sendto", errno, *nsap);
338 perror("sendto"); 582 badns |= (1 << ns);
339#endif 583 res_close();
340 continue; 584 goto next_ns;
341 } 585 }
342 } 586 }
343 587
@@ -350,110 +594,169 @@ res_send(buf, buflen, answer, anslen)
350 if ((long) timeout.tv_sec <= 0) 594 if ((long) timeout.tv_sec <= 0)
351 timeout.tv_sec = 1; 595 timeout.tv_sec = 1;
352 timeout.tv_usec = 0; 596 timeout.tv_usec = 0;
353wait: 597 wait:
354 FD_ZERO(&dsmask); 598 dsmaskp = (fd_set *)calloc(howmany(s+1, NFDBITS),
355 FD_SET(s, &dsmask); 599 sizeof(fd_mask));
356 n = select(s+1, &dsmask, (fd_set *)NULL, 600 if (dsmaskp == NULL) {
357 (fd_set *)NULL, &timeout); 601 res_close();
602 goto next_ns;
603 }
604 FD_SET(s, dsmaskp);
605 n = select(s+1, dsmaskp, (fd_set *)NULL,
606 (fd_set *)NULL, &timeout);
607 free(dsmaskp);
358 if (n < 0) { 608 if (n < 0) {
359#ifdef DEBUG 609 if (errno == EINTR)
360 if (_res.options & RES_DEBUG) 610 goto wait;
361 perror("select"); 611 Perror(stderr, "select", errno);
362#endif 612 res_close();
363 continue; 613 goto next_ns;
364 } 614 }
365 if (n == 0) { 615 if (n == 0) {
366 /* 616 /*
367 * timeout 617 * timeout
368 */ 618 */
369#ifdef DEBUG 619 Dprint(_res.options & RES_DEBUG,
370 if (_res.options & RES_DEBUG) 620 (stdout, ";; timeout\n"));
371 printf(";; timeout\n");
372#endif
373 gotsomewhere = 1; 621 gotsomewhere = 1;
374 continue; 622 res_close();
623 goto next_ns;
375 } 624 }
376 if ((resplen = recv(s, answer, anslen, 0)) <= 0) { 625 errno = 0;
377#ifdef DEBUG 626 fromlen = sizeof(struct sockaddr_in);
378 if (_res.options & RES_DEBUG) 627 resplen = recvfrom(s, (char*)ans, anssiz, 0,
379 perror("recvfrom"); 628 (struct sockaddr *)&from, &fromlen);
380#endif 629 if (resplen <= 0) {
381 continue; 630 Perror(stderr, "recvfrom", errno);
631 res_close();
632 goto next_ns;
382 } 633 }
383 gotsomewhere = 1; 634 gotsomewhere = 1;
384 if (id != anhp->id) { 635 if (hp->id != anhp->id) {
385 /* 636 /*
386 * response from old query, ignore it 637 * response from old query, ignore it.
638 * XXX - potential security hazard could
639 * be detected here.
387 */ 640 */
388#ifdef DEBUG 641 DprintQ((_res.options & RES_DEBUG) ||
389 if ((_res.options & RES_DEBUG) || 642 (_res.pfcode & RES_PRF_REPLY),
390 (_res.pfcode & RES_PRF_REPLY)) { 643 (stdout, ";; old answer:\n"),
391 printf(";; old answer:\n"); 644 ans, (resplen>anssiz)?anssiz:resplen);
392 __p_query(answer); 645 goto wait;
393 } 646 }
647#if CHECK_SRVR_ADDR
648 if (!(_res.options & RES_INSECURE1) &&
649 !res_isourserver(&from)) {
650 /*
651 * response from wrong server? ignore it.
652 * XXX - potential security hazard could
653 * be detected here.
654 */
655 DprintQ((_res.options & RES_DEBUG) ||
656 (_res.pfcode & RES_PRF_REPLY),
657 (stdout, ";; not our server:\n"),
658 ans, (resplen>anssiz)?anssiz:resplen);
659 goto wait;
660 }
394#endif 661#endif
662 if (!(_res.options & RES_INSECURE2) &&
663 !res_queriesmatch(buf, buf + buflen,
664 ans, ans + anssiz)) {
665 /*
666 * response contains wrong query? ignore it.
667 * XXX - potential security hazard could
668 * be detected here.
669 */
670 DprintQ((_res.options & RES_DEBUG) ||
671 (_res.pfcode & RES_PRF_REPLY),
672 (stdout, ";; wrong query name:\n"),
673 ans, (resplen>anssiz)?anssiz:resplen);
395 goto wait; 674 goto wait;
396 } 675 }
397 if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || 676 if (anhp->rcode == SERVFAIL ||
677 anhp->rcode == NOTIMP ||
398 anhp->rcode == REFUSED) { 678 anhp->rcode == REFUSED) {
399#ifdef DEBUG 679 DprintQ(_res.options & RES_DEBUG,
400 if (_res.options & RES_DEBUG) { 680 (stdout, "server rejected query:\n"),
401 printf("server rejected query:\n"); 681 ans, (resplen>anssiz)?anssiz:resplen);
402 __p_query(answer); 682 badns |= (1 << ns);
403 } 683 res_close();
404#endif 684 /* don't retry if called from dig */
405 badns |= (1<<ns); 685 if (!_res.pfcode)
406 continue; 686 goto next_ns;
407 } 687 }
408 if (!(_res.options & RES_IGNTC) && anhp->tc) { 688 if (!(_res.options & RES_IGNTC) && anhp->tc) {
409 /* 689 /*
410 * get rest of answer; 690 * get rest of answer;
411 * use TCP with same server. 691 * use TCP with same server.
412 */ 692 */
413#ifdef DEBUG 693 Dprint(_res.options & RES_DEBUG,
414 if (_res.options & RES_DEBUG) 694 (stdout, ";; truncated answer\n"));
415 printf(";; truncated answer\n");
416#endif
417 (void) close(s);
418 s = -1;
419 v_circuit = 1; 695 v_circuit = 1;
420 goto usevc; 696 res_close();
697 goto same_ns;
421 } 698 }
422 } 699 } /*if vc/dg*/
423#ifdef DEBUG 700 Dprint((_res.options & RES_DEBUG) ||
424 if (_res.options & RES_DEBUG) 701 ((_res.pfcode & RES_PRF_REPLY) &&
425 printf(";; got answer:\n"); 702 (_res.pfcode & RES_PRF_HEAD1)),
426 if ((_res.options & RES_DEBUG) || 703 (stdout, ";; got answer:\n"));
427 (_res.pfcode & RES_PRF_REPLY)) 704 DprintQ((_res.options & RES_DEBUG) ||
428 __p_query(answer); 705 (_res.pfcode & RES_PRF_REPLY),
429#endif 706 (stdout, ""),
707 ans, (resplen>anssiz)?anssiz:resplen);
430 /* 708 /*
431 * If using virtual circuits, we assume that the first server 709 * If using virtual circuits, we assume that the first server
432 * is preferred * over the rest (i.e. it is on the local 710 * is preferred over the rest (i.e. it is on the local
433 * machine) and only keep that one open. 711 * machine) and only keep that one open.
434 * If we have temporarily opened a virtual circuit, 712 * If we have temporarily opened a virtual circuit,
435 * or if we haven't been asked to keep a socket open, 713 * or if we haven't been asked to keep a socket open,
436 * close the socket. 714 * close the socket.
437 */ 715 */
438 if ((v_circuit && 716 if ((v_circuit && (!(_res.options & RES_USEVC) || ns != 0)) ||
439 ((_res.options & RES_USEVC) == 0 || ns != 0)) || 717 !(_res.options & RES_STAYOPEN)) {
440 (_res.options & RES_STAYOPEN) == 0) { 718 res_close();
441 (void) close(s); 719 }
442 s = -1; 720 if (Rhook) {
721 int done = 0, loops = 0;
722
723 do {
724 res_sendhookact act;
725
726 act = (*Rhook)(nsap, buf, buflen,
727 ans, anssiz, &resplen);
728 switch (act) {
729 case res_goahead:
730 case res_done:
731 done = 1;
732 break;
733 case res_nextns:
734 res_close();
735 goto next_ns;
736 case res_modified:
737 /* give the hook another try */
738 if (++loops < 42) /*doug adams*/
739 break;
740 /*FALLTHROUGH*/
741 case res_error:
742 /*FALLTHROUGH*/
743 default:
744 return (-1);
745 }
746 } while (!done);
747
443 } 748 }
444 return (resplen); 749 return (resplen);
445 } 750 next_ns: ;
446 } 751 } /*foreach ns*/
447 if (s >= 0) { 752 } /*foreach retry*/
448 (void) close(s); 753 res_close();
449 s = -1; 754 if (!v_circuit) {
450 } 755 if (!gotsomewhere)
451 if (v_circuit == 0)
452 if (gotsomewhere == 0)
453 errno = ECONNREFUSED; /* no nameservers found */ 756 errno = ECONNREFUSED; /* no nameservers found */
454 else 757 else
455 errno = ETIMEDOUT; /* no answer obtained */ 758 errno = ETIMEDOUT; /* no answer obtained */
456 else 759 } else
457 errno = terrno; 760 errno = terrno;
458 return (-1); 761 return (-1);
459} 762}
@@ -465,10 +768,13 @@ wait:
465 * 768 *
466 * This routine is not expected to be user visible. 769 * This routine is not expected to be user visible.
467 */ 770 */
468_res_close() 771void
772res_close()
469{ 773{
470 if (s != -1) { 774 if (s >= 0) {
471 (void) close(s); 775 (void) close(s);
472 s = -1; 776 s = -1;
777 connected = 0;
778 vc = 0;
473 } 779 }
474} 780}
diff --git a/src/lib/libc/net/resolver.3 b/src/lib/libc/net/resolver.3
index 99abe17f03..98718d448a 100644
--- a/src/lib/libc/net/resolver.3
+++ b/src/lib/libc/net/resolver.3
@@ -1,4 +1,4 @@
1.\" $NetBSD: resolver.3,v 1.5 1995/02/25 06:21:02 cgd Exp $ 1.\" $OpenBSD: resolver.3,v 1.5 1998/09/05 17:41:44 deraadt Exp $
2.\" 2.\"
3.\" Copyright (c) 1985, 1991, 1993 3.\" Copyright (c) 1985, 1991, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
@@ -31,8 +31,6 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" @(#)resolver.3 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993 34.Dd June 4, 1993
37.Dt RESOLVER 3 35.Dt RESOLVER 3
38.Os BSD 4.3 36.Os BSD 4.3
@@ -112,7 +110,7 @@ are defined in
112and are as follows. 110and are as follows.
113Options are stored as a simple bit mask containing the bitwise ``or'' 111Options are stored as a simple bit mask containing the bitwise ``or''
114of the options enabled. 112of the options enabled.
115.Bl -tag -width RES_DEFNAMES 113.Bl -tag -width RES_USE_INET6
116.It Dv RES_INIT 114.It Dv RES_INIT
117True if the initial name server address and default domain name are 115True if the initial name server address and default domain name are
118initialized (i.e., 116initialized (i.e.,
@@ -165,6 +163,8 @@ will search for host names in the current domain and in parent domains; see
165This is used by the standard host lookup routine 163This is used by the standard host lookup routine
166.Xr gethostbyname 3 . 164.Xr gethostbyname 3 .
167This option is enabled by default. 165This option is enabled by default.
166.It Dv RES_USE_INET6
167Enable support for IPv6 addresses.
168.El 168.El
169.Pp 169.Pp
170The 170The
@@ -282,7 +282,7 @@ The compression uses
282an array of pointers 282an array of pointers
283.Fa dnptrs 283.Fa dnptrs
284to previously-compressed names in the current message. 284to previously-compressed names in the current message.
285The first pointer points to 285The first pointer points
286to the beginning of the message and the list ends with 286to the beginning of the message and the list ends with
287.Dv NULL . 287.Dv NULL .
288The limit to the array is specified by 288The limit to the array is specified by
@@ -333,6 +333,7 @@ see
333.%T RFC1033 , 333.%T RFC1033 ,
334.%T RFC1034 , 334.%T RFC1034 ,
335.%T RFC1035 , 335.%T RFC1035 ,
336.%T RFC1535 ,
336.%T RFC974 337.%T RFC974
337.Rs 338.Rs
338.%T "Name Server Operations Guide for BIND" 339.%T "Name Server Operations Guide for BIND"
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c
index 88f3550ec6..8495931ca3 100644
--- a/src/lib/libc/net/send.c
+++ b/src/lib/libc/net/send.c
@@ -1,5 +1,3 @@
1/* $NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1988, 1993 2 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: send.c,v 1.2 1996/08/19 08:29:52 tholo Exp $";
38static char sccsid[] = "@(#)send.c 8.2 (Berkeley) 2/21/94";
39#else
40static char rcsid[] = "$NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/types.h> 38#include <sys/types.h>
diff --git a/src/lib/libc/net/sethostent.c b/src/lib/libc/net/sethostent.c
index 00f6499695..5392a2f11b 100644
--- a/src/lib/libc/net/sethostent.c
+++ b/src/lib/libc/net/sethostent.c
@@ -1,5 +1,3 @@
1/* $NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $ */
2
3/* 1/*
4 * Copyright (c) 1985, 1993 2 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved. 3 * The Regents of the University of California. All rights reserved.
@@ -34,11 +32,7 @@
34 */ 32 */
35 33
36#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
37#if 0 35static char rcsid[] = "$OpenBSD: sethostent.c,v 1.4 1997/03/15 21:53:50 pefo Exp $";
38static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
43 37
44#include <sys/param.h> 38#include <sys/param.h>
@@ -49,7 +43,11 @@ static char rcsid[] = "$NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $
49 43
50void 44void
51sethostent(stayopen) 45sethostent(stayopen)
46 int stayopen;
52{ 47{
48
49 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
50 return;
53 if (stayopen) 51 if (stayopen)
54 _res.options |= RES_STAYOPEN | RES_USEVC; 52 _res.options |= RES_STAYOPEN | RES_USEVC;
55} 53}
@@ -58,5 +56,5 @@ void
58endhostent() 56endhostent()
59{ 57{
60 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 58 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
61 _res_close(); 59 res_close();
62} 60}
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 782a4ab022..e75fc0d8bf 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,24 +1,28 @@
1# from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 1# $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $
2# $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
3 2
4# stdlib sources 3# stdlib sources
5.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib 4.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib
6 5
7SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ 6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \
8 exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \ 7 cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \
9 multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ 8 malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \
10 setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \ 9 random.c realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \
10 strtouq.c system.c tfind.c tsearch.c \
11 _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ 11 _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \
12 mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c 12 mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c
13 13
14.if (${MACHINE_ARCH} == "m68k") 14.if (${MACHINE_ARCH} == "m68k")
15SRCS+= abs.S div.c labs.c ldiv.c 15SRCS+= abs.S div.c labs.c ldiv.c
16LSRCS+= abs.c
16.elif (${MACHINE_ARCH} == "i386") 17.elif (${MACHINE_ARCH} == "i386")
17SRCS+= abs.S div.S labs.S ldiv.S 18SRCS+= abs.S div.S labs.S ldiv.S
19LSRCS+= abs.c div.c labs.c ldiv.c
18.elif (${MACHINE_ARCH} == "ns32k") 20.elif (${MACHINE_ARCH} == "ns32k")
19SRCS+= abs.S div.c labs.c ldiv.c 21SRCS+= abs.S div.c labs.c ldiv.c
22LSRCS+= abs.c
20.elif (${MACHINE_ARCH} == "tahoe") 23.elif (${MACHINE_ARCH} == "tahoe")
21SRCS+= abs.S div.c labs.c ldiv.c 24SRCS+= abs.S div.c labs.c ldiv.c
25LSRCS+= abs.c
22.elif (${MACHINE_ARCH} == "vax") 26.elif (${MACHINE_ARCH} == "vax")
23SRCS+= abs.c div.c labs.c ldiv.c 27SRCS+= abs.c div.c labs.c ldiv.c
24.elif (${MACHINE_ARCH} == "alpha") 28.elif (${MACHINE_ARCH} == "alpha")
@@ -28,18 +32,23 @@ SRCS+= abs.c div.c labs.c ldiv.c
28SRCS+= abs.c div.c labs.c ldiv.c 32SRCS+= abs.c div.c labs.c ldiv.c
29.endif 33.endif
30 34
31MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ 35MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
32 calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ 36 calloc.3 div.3 exit.3 getenv.3 getopt.3 getsubopt.3 labs.3 ldiv.3 \
33 malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ 37 malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \
34 rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ 38 random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3
35 system.3
36 39
37MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 40MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3
41MLINKS+=malloc.3 free.3 malloc.3 realloc.3
42MLINKS+=malloc.3 cfree.3
38MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 43MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
39MLINKS+=rand.3 srand.3 44MLINKS+=rand.3 srand.3
40MLINKS+=strtol.3 strtoq.3
41MLINKS+=strtoul.3 strtouq.3
42MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 45MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3
43MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 46MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
44MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 47MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
45MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 48MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
49MLINKS+=strtol.3 strtoq.3
50MLINKS+=strtoul.3 strtouq.3
51MLINKS+=tsearch.3 tfind.3
52MLINKS+=tsearch.3 tdelete.3
53MLINKS+=tsearch.3 twalk.3
54MLINKS+=a64l.3 l64a.3
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c
index 83ade4645a..fed7372f68 100644
--- a/src/lib/libc/stdlib/_rand48.c
+++ b/src/lib/libc/stdlib/_rand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: _rand48.c,v 1.2 1996/08/19 08:33:19 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16unsigned short __rand48_seed[3] = { 20unsigned short __rand48_seed[3] = {
diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3
new file mode 100644
index 0000000000..4ec707adf1
--- /dev/null
+++ b/src/lib/libc/stdlib/a64l.3
@@ -0,0 +1,122 @@
1.\"
2.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
3.\" 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. The name of the author may not be used to endorse or promote products
14.\" derived from this software without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26.\"
27.\" $OpenBSD: a64l.3,v 1.2 1998/06/21 22:13:48 millert Exp $
28.\"
29.Dd August 17, 1997
30.Dt A64L 3
31.Os
32.Sh NAME
33.Nm a64l ,
34.Nm l64a
35.Nd convert between 32-bit integer and radix-64 ASCII string
36.Sh SYNOPSIS
37.Fd #include <stdlib.h>
38.Ft long
39.Fn a64l "const char *s"
40.Ft char *
41.Fn l64a "long l"
42.Sh DESCRIPTION
43The
44.Fn a64l
45and
46.Fn l64a
47functions are used to maintain numbers stored in radix-64
48ASCII characters. This is a notation by which 32-bit integers
49can be represented by up to six characters; each character
50represents a "digit" in a radix-64 notation.
51.Pp
52The characters used to represent "digits" are '.' for 0, '/' for 1,
53'0' through '9' for 2-11, 'A' through 'Z' for 12-37, and 'a' through
54'z' for 38-63.
55.Pp
56The
57.Fn a64l
58function takes a pointer to a null-terminated radix-64 representation
59and returns a corresponding 32-bit value. If the string pointed to by
60.Ar s
61contains more than six characters,
62.Fn a64l
63will use the first six.
64.Fn A64l
65scans the character string from left to right, decoding
66each character as a 6-bit radix-64 number. If a long integer is
67larger than 32 bits, the return value will be sign-extended.
68.Pp
69.Fn l64a
70takes a long integer argument
71.Ar l
72and returns a pointer to the corresponding radix-64 representation.
73.Sh RETURN VALUES
74On success,
75.Fn a64l
76returns a 32-bit representation of
77.Ar s .
78If
79.Ar s
80is a NULL pointer or if it contains "digits" other than those described above,
81.Fn a64l
82returns -1L and sets the global variable errno to
83.Va EINVAL .
84.Pp
85On success,
86.Fn l64a
87returns a pointer to a string containing the radix-64 representation of
88.Ar l .
89If
90.Ar l
91is 0,
92.Fn l64a
93returns a pointer to the empty string.
94If
95.Ar l
96is negative,
97.Fn l64a
98returns a NULL pointer and sets the global variable errno to
99.Va EINVAL .
100.Sh WARNINGS
101The value returned by
102.Fn l64a
103is a pointer into a static buffer, the contents of which
104will be overwritten by subsequent calls.
105.Pp
106The value returned by
107.Fn a64l
108may be incorrect if the value is too large; for that reason, only strings
109that resulted from a call to
110.Fn l64a
111should be used to call
112.Fn a64l .
113.Pp
114If a long integer is larger than 32 bits, only the low-order
11532 bits are used.
116.Sh STANDARDS
117The
118.Fn a64l
119and
120.Fn l64a
121functions conform to
122.St -xpg4.2 .
diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c
index 03fc77e034..a68f0a6dcd 100644
--- a/src/lib/libc/stdlib/a64l.c
+++ b/src/lib/libc/stdlib/a64l.c
@@ -4,8 +4,11 @@
4 */ 4 */
5 5
6#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
7static char *rcsid = "$NetBSD: a64l.c,v 1.3 1995/05/11 23:04:47 jtc Exp $"; 7static char *rcsid = "$OpenBSD: a64l.c,v 1.3 1997/08/17 22:58:34 millert Exp $";
8#endif 8#endif /* LIBC_SCCS and not lint */
9
10#include <errno.h>
11#include <stdlib.h>
9 12
10long 13long
11a64l(s) 14a64l(s)
@@ -14,21 +17,30 @@ a64l(s)
14 long value, digit, shift; 17 long value, digit, shift;
15 int i; 18 int i;
16 19
20 if (s == NULL) {
21 errno = EINVAL;
22 return(-1L);
23 }
24
17 value = 0; 25 value = 0;
18 shift = 0; 26 shift = 0;
19 for (i = 0; *s && i < 6; i++, s++) { 27 for (i = 0; *s && i < 6; i++, s++) {
20 if (*s <= '/') 28 if (*s >= '.' && *s <= '/')
21 digit = *s - '.'; 29 digit = *s - '.';
22 else if (*s <= '9') 30 else if (*s >= '0' && *s <= '9')
23 digit = *s - '0' + 2; 31 digit = *s - '0' + 2;
24 else if (*s <= 'Z') 32 else if (*s >= 'A' && *s <= 'Z')
25 digit = *s - 'A' + 12; 33 digit = *s - 'A' + 12;
26 else 34 else if (*s >= 'a' && *s <= 'z')
27 digit = *s - 'a' + 38; 35 digit = *s - 'a' + 38;
36 else {
37 errno = EINVAL;
38 return(-1L);
39 }
28 40
29 value |= digit << shift; 41 value |= digit << shift;
30 shift += 6; 42 shift += 6;
31 } 43 }
32 44
33 return (long) value; 45 return(value);
34} 46}
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3
index ab57327585..92c9a354d0 100644
--- a/src/lib/libc/stdlib/abort.3
+++ b/src/lib/libc/stdlib/abort.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)abort.3 6.7 (Berkeley) 6/29/91 36.\" $OpenBSD: abort.3,v 1.4 1997/07/17 07:39:41 deraadt Exp $
37.\" $Id: abort.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt ABORT 3 39.Dt ABORT 3
@@ -54,7 +53,7 @@ signal
54.Dv SIGABRT 53.Dv SIGABRT
55is being caught and the signal handler does not return. 54is being caught and the signal handler does not return.
56.Pp 55.Pp
57No open streams are closed or flushed. 56Any open streams are flushed and closed.
58.Sh RETURN VALUES 57.Sh RETURN VALUES
59The 58The
60.Nm abort 59.Nm abort
@@ -62,10 +61,10 @@ function
62never returns. 61never returns.
63.Sh SEE ALSO 62.Sh SEE ALSO
64.Xr sigaction 2 , 63.Xr sigaction 2 ,
65.Xr exit 2 64.Xr exit 3
66.Sh STANDARDS 65.Sh STANDARDS
67The 66The
68.Fn abort 67.Fn abort
69function 68function
70conforms to 69conforms to
71.St -ansiC . 70.St -p1003.1-90 .
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c
index c298e016b4..4ea8a2ca4b 100644
--- a/src/lib/libc/stdlib/abort.c
+++ b/src/lib/libc/stdlib/abort.c
@@ -32,19 +32,22 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)abort.c 5.11 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: abort.c,v 1.5 1997/06/22 20:21:25 tholo Exp $";
36static char *rcsid = "$Id: abort.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <signal.h> 38#include <signal.h>
40#include <stdlib.h> 39#include <stdlib.h>
41#include <unistd.h> 40#include <unistd.h>
42 41
42void (*__cleanup)();
43
43void 44void
44abort() 45abort()
45{ 46{
47 static int cleanup_called = 0;
46 sigset_t mask; 48 sigset_t mask;
47 49
50
48 sigfillset(&mask); 51 sigfillset(&mask);
49 /* 52 /*
50 * don't block SIGABRT to give any handler a chance; we ignore 53 * don't block SIGABRT to give any handler a chance; we ignore
@@ -52,6 +55,15 @@ abort()
52 */ 55 */
53 sigdelset(&mask, SIGABRT); 56 sigdelset(&mask, SIGABRT);
54 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); 57 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
58
59 /*
60 * POSIX requires we flush stdio buffers on abort
61 */
62 if (cleanup_called == 0 && __cleanup != NULL) {
63 cleanup_called = 1;
64 (*__cleanup)();
65 }
66
55 (void)kill(getpid(), SIGABRT); 67 (void)kill(getpid(), SIGABRT);
56 68
57 /* 69 /*
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3
index 4748d89e77..77f82b9402 100644
--- a/src/lib/libc/stdlib/abs.3
+++ b/src/lib/libc/stdlib/abs.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)abs.3 6.4 (Berkeley) 6/29/91 36.\" $OpenBSD: abs.3,v 1.2 1996/08/19 08:33:21 tholo Exp $
37.\" $Id: abs.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt ABS 3 39.Dt ABS 3
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c
index 64468e0224..7c79e4073c 100644
--- a/src/lib/libc/stdlib/abs.c
+++ b/src/lib/libc/stdlib/abs.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)abs.c 5.2 (Berkeley) 5/17/90";*/ 35static char *rcsid = "$OpenBSD: abs.c,v 1.2 1996/08/19 08:33:21 tholo Exp $";
36static char *rcsid = "$Id: abs.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3
index dcb97ab11c..ef87220772 100644
--- a/src/lib/libc/stdlib/alloca.3
+++ b/src/lib/libc/stdlib/alloca.3
@@ -29,8 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)alloca.3 5.1 (Berkeley) 5/2/91 32.\" $OpenBSD: alloca.3,v 1.3 1996/08/19 08:33:22 tholo Exp $
33.\" $Id: alloca.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
34.\" 33.\"
35.Dd May 2, 1991 34.Dd May 2, 1991
36.Dt ALLOCA 3 35.Dt ALLOCA 3
@@ -55,9 +54,6 @@ return.
55The 54The
56.Fn alloca 55.Fn alloca
57function returns a pointer to the beginning of the allocated space. 56function returns a pointer to the beginning of the allocated space.
58If the allocation failed, a
59.Dv NULL
60pointer is returned.
61.Sh SEE ALSO 57.Sh SEE ALSO
62.Xr brk 2 , 58.Xr brk 2 ,
63.Xr pagesize 2 59.Xr pagesize 2
diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3
index 07de054d3c..0b10f010fa 100644
--- a/src/lib/libc/stdlib/atexit.3
+++ b/src/lib/libc/stdlib/atexit.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)atexit.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: atexit.3,v 1.2 1996/08/19 08:33:22 tholo Exp $
37.\" $Id: atexit.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt ATEXIT 3 39.Dt ATEXIT 3
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c
index 4da1eb0d9c..c0fb624141 100644
--- a/src/lib/libc/stdlib/atexit.c
+++ b/src/lib/libc/stdlib/atexit.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ 38static char *rcsid = "$OpenBSD: atexit.c,v 1.2 1996/08/19 08:33:22 tholo Exp $";
39static char *rcsid = "$Id: atexit.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <stdlib.h> 41#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h
index 8b756e8fe2..e41a7cb86c 100644
--- a/src/lib/libc/stdlib/atexit.h
+++ b/src/lib/libc/stdlib/atexit.h
@@ -30,8 +30,7 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * from: @(#)atexit.h 5.1 (Berkeley) 5/15/90 33 * $OpenBSD: atexit.h,v 1.2 1996/08/19 08:33:23 tholo Exp $
34 * $Id: atexit.h,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
35 */ 34 */
36 35
37/* must be at least 32 to guarantee ANSI conformance */ 36/* must be at least 32 to guarantee ANSI conformance */
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3
index 53e04f71c5..cc1b500b0f 100644
--- a/src/lib/libc/stdlib/atof.3
+++ b/src/lib/libc/stdlib/atof.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)atof.3 6.4 (Berkeley) 6/29/91 36.\" $OpenBSD: atof.3,v 1.2 1996/08/19 08:33:23 tholo Exp $
37.\" $Id: atof.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt ATOF 3 39.Dt ATOF 3
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c
index 9202de50bb..30bac19899 100644
--- a/src/lib/libc/stdlib/atof.c
+++ b/src/lib/libc/stdlib/atof.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)atof.c 5.3 (Berkeley) 1/8/93";*/ 35static char *rcsid = "$OpenBSD: atof.c,v 1.2 1996/08/19 08:33:24 tholo Exp $";
36static char *rcsid = "$Id: atof.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3
index 219ba73c00..280a989e8e 100644
--- a/src/lib/libc/stdlib/atoi.3
+++ b/src/lib/libc/stdlib/atoi.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" Copyright (c) 1990, 1991, 1993
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -33,10 +33,9 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)atoi.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: atoi.3,v 1.2 1996/08/10 04:51:31 tholo Exp $
37.\" $Id: atoi.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 4, 1993
40.Dt ATOI 3 39.Dt ATOI 3
41.Os 40.Os
42.Sh NAME 41.Sh NAME
@@ -58,7 +57,6 @@ to
58representation. 57representation.
59.Pp 58.Pp
60It is equivalent to: 59It is equivalent to:
61.Pp
62.Bd -literal -offset indent 60.Bd -literal -offset indent
63(int)strtol(nptr, (char **)NULL, 10); 61(int)strtol(nptr, (char **)NULL, 10);
64.Ed 62.Ed
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c
index df7845f90c..a74d6e1351 100644
--- a/src/lib/libc/stdlib/atoi.c
+++ b/src/lib/libc/stdlib/atoi.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)atoi.c 5.7 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: atoi.c,v 1.2 1996/08/19 08:33:24 tholo Exp $";
36static char *rcsid = "$Id: atoi.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3
index 86e3d324a3..2b49bd1f2c 100644
--- a/src/lib/libc/stdlib/atol.3
+++ b/src/lib/libc/stdlib/atol.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)atol.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: atol.3,v 1.2 1996/08/19 08:33:25 tholo Exp $
37.\" $Id: atol.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt ATOL 3 39.Dt ATOL 3
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c
index 31ed06298b..528a932214 100644
--- a/src/lib/libc/stdlib/atol.c
+++ b/src/lib/libc/stdlib/atol.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)atol.c 5.7 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: atol.c,v 1.2 1996/08/19 08:33:26 tholo Exp $";
36static char *rcsid = "$Id: atol.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3
index 1622c96c6b..570a4227b4 100644
--- a/src/lib/libc/stdlib/bsearch.3
+++ b/src/lib/libc/stdlib/bsearch.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" Copyright (c) 1990, 1991, 1993, 1994
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -33,10 +33,9 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)bsearch.3 5.6 (Berkeley) 6/29/91 36.\" $OpenBSD: bsearch.3,v 1.3 1997/06/13 23:41:35 deraadt Exp $
37.\" $Id: bsearch.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd April 19, 1994
40.Dt BSEARCH 3 39.Dt BSEARCH 3
41.Os 40.Os
42.Sh NAME 41.Sh NAME
@@ -65,7 +64,7 @@ to the comparison function referenced by
65The 64The
66.Fa compar 65.Fa compar
67routine 66routine
68is expected to have two 67is expected to have
69two arguments which point to the 68two arguments which point to the
70.Fa key 69.Fa key
71object and to an array member, in that order, and should return an integer 70object and to an array member, in that order, and should return an integer
@@ -83,7 +82,7 @@ If two members compare as equal, which member is matched is unspecified.
83.Xr db 3 , 82.Xr db 3 ,
84.Xr lsearch 3 , 83.Xr lsearch 3 ,
85.Xr qsort 3 , 84.Xr qsort 3 ,
86.\" .Xr tsearch 3 85.Xr tsearch 3
87.Sh STANDARDS 86.Sh STANDARDS
88The 87The
89.Fn bsearch 88.Fn bsearch
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c
index fac03f694f..eeef9bffc6 100644
--- a/src/lib/libc/stdlib/bsearch.c
+++ b/src/lib/libc/stdlib/bsearch.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)bsearch.c 5.4 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: bsearch.c,v 1.2 1996/08/19 08:33:26 tholo Exp $";
36static char *rcsid = "$Id: bsearch.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/calloc.3 b/src/lib/libc/stdlib/calloc.3
index d0754b46a0..13a912169c 100644
--- a/src/lib/libc/stdlib/calloc.3
+++ b/src/lib/libc/stdlib/calloc.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)calloc.3 5.2 (Berkeley) 6/29/91 36.\" $OpenBSD: calloc.3,v 1.3 1996/12/10 09:06:10 deraadt Exp $
37.\" $Id: calloc.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt CALLOC 3 39.Dt CALLOC 3
@@ -58,7 +57,7 @@ The space is initialized to all bits zero.
58The 57The
59.Fn calloc 58.Fn calloc
60function returns 59function returns
61a pointer to the 60a pointer to
62the allocated space if successful; otherwise a null pointer is returned. 61the allocated space if successful; otherwise a null pointer is returned.
63.Sh SEE ALSO 62.Sh SEE ALSO
64.Xr malloc 3 , 63.Xr malloc 3 ,
diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c
index 3353fab052..a2c4f84493 100644
--- a/src/lib/libc/stdlib/calloc.c
+++ b/src/lib/libc/stdlib/calloc.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)calloc.c 5.6 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: calloc.c,v 1.3 1996/08/20 17:42:33 downsj Exp $";
36static char *rcsid = "$Id: calloc.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
@@ -47,7 +46,8 @@ calloc(num, size)
47 register void *p; 46 register void *p;
48 47
49 size *= num; 48 size *= num;
50 if (p = malloc(size)) 49 p = malloc(size);
50 if (p)
51 memset(p, '\0', size); 51 memset(p, '\0', size);
52 return(p); 52 return(p);
53} 53}
diff --git a/src/lib/libc/stdlib/cfree.c b/src/lib/libc/stdlib/cfree.c
new file mode 100644
index 0000000000..3af32039a9
--- /dev/null
+++ b/src/lib/libc/stdlib/cfree.c
@@ -0,0 +1,49 @@
1/* $OpenBSD: cfree.c,v 1.1 1996/08/21 03:47:22 tholo Exp $ */
2
3/*
4 * Copyright (c) 1996 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by SigmaSoft, Th. Lockert.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char rcsid[] = "$OpenBSD: cfree.c,v 1.1 1996/08/21 03:47:22 tholo Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <sys/cdefs.h>
38
39#ifdef __indr_reference
40__indr_reference(free, cfree);
41#else
42
43void
44cfree(p)
45 void *p;
46{
47 free(p);
48}
49#endif
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3
index a4730694a5..0d8c56d86b 100644
--- a/src/lib/libc/stdlib/div.3
+++ b/src/lib/libc/stdlib/div.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)div.3 5.2 (Berkeley) 4/19/91 34.\" $OpenBSD: div.3,v 1.2 1996/08/19 08:33:28 tholo Exp $
35.\" $Id: div.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt DIV 3 37.Dt DIV 3
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c
index 122ac0deec..c1fae29008 100644
--- a/src/lib/libc/stdlib/div.c
+++ b/src/lib/libc/stdlib/div.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)div.c 5.2 (Berkeley) 4/16/91";*/ 38static char *rcsid = "$OpenBSD: div.c,v 1.2 1996/08/19 08:33:29 tholo Exp $";
39static char *rcsid = "$Id: div.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <stdlib.h> /* div_t */ 41#include <stdlib.h> /* div_t */
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c
index ae1a8634dc..02886d5b62 100644
--- a/src/lib/libc/stdlib/drand48.c
+++ b/src/lib/libc/stdlib/drand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: drand48.c,v 1.2 1996/08/19 08:33:29 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c
index cc9fbf770c..b92dacffcc 100644
--- a/src/lib/libc/stdlib/erand48.c
+++ b/src/lib/libc/stdlib/erand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: erand48.c,v 1.2 1996/08/19 08:33:29 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16double 20double
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3
index adb81ffcb4..6dd2affef9 100644
--- a/src/lib/libc/stdlib/exit.3
+++ b/src/lib/libc/stdlib/exit.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)exit.3 6.6 (Berkeley) 6/29/91 36.\" $OpenBSD: exit.3,v 1.2 1996/08/19 08:33:30 tholo Exp $
37.\" $Id: exit.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt EXIT 3 39.Dt EXIT 3
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c
index b1412f42bb..e358c94fd6 100644
--- a/src/lib/libc/stdlib/exit.c
+++ b/src/lib/libc/stdlib/exit.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)exit.c 5.4 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $";
36static char *rcsid = "$Id: exit.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/free.3 b/src/lib/libc/stdlib/free.3
deleted file mode 100644
index 3d0131d7de..0000000000
--- a/src/lib/libc/stdlib/free.3
+++ /dev/null
@@ -1,82 +0,0 @@
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" from: @(#)free.3 5.2 (Berkeley) 6/29/91
37.\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\"
39.Dd June 29, 1991
40.Dt FREE 3
41.Os
42.Sh NAME
43.Nm free
44.Nd free up memory allocated with malloc, calloc or realloc
45.Sh SYNOPSIS
46.Fd #include <stdlib.h>
47.Ft void
48.Fn free "void *ptr"
49.Sh DESCRIPTION
50The
51.Fn free
52function causes the space pointed to by
53.Fa ptr
54to be deallocated, that is, made available
55for further allocation.
56If
57.Fa ptr
58is a null pointer, no action occurs.
59Otherwise, if the argument does not match a pointer earlier
60returned by the
61.Xr calloc ,
62.Xr malloc ,
63or
64.Xr realloc
65function, or if the space has been deallocated by a call to
66.Fn free
67or
68.Xr realloc ,
69general havoc may occur.
70.Sh RETURN VALUES
71The
72.Fn free
73function returns no value.
74.Sh SEE ALSO
75.Xr calloc 3 ,
76.Xr malloc 3 ,
77.Xr realloc 3
78.Sh STANDARDS
79The
80.Fn free
81function conforms to
82.St -ansiC .
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3
index 411eb35da4..24a8d3d095 100644
--- a/src/lib/libc/stdlib/getenv.3
+++ b/src/lib/libc/stdlib/getenv.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1988, 1991 The Regents of the University of California. 1.\" Copyright (c) 1988, 1991, 1993
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -33,10 +33,9 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)getenv.3 6.11 (Berkeley) 6/29/91 36.\" $OpenBSD: getenv.3,v 1.2 1996/08/10 05:03:00 tholo Exp $
37.\" $Id: getenv.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd December 11, 1993
40.Dt GETENV 3 39.Dt GETENV 3
41.Os 40.Os
42.Sh NAME 41.Sh NAME
@@ -75,7 +74,7 @@ function obtains the current value of the environment variable,
75.Ar name . 74.Ar name .
76If the variable 75If the variable
77.Ar name 76.Ar name
78is not in the current environment , 77is not in the current environment,
79a null pointer is returned. 78a null pointer is returned.
80.Pp 79.Pp
81The 80The
@@ -121,7 +120,7 @@ return zero if successful; otherwise the global variable
121is set to indicate the error and a 120is set to indicate the error and a
122\-1 is returned. 121\-1 is returned.
123.Sh ERRORS 122.Sh ERRORS
124.Bl -tag -width Er 123.Bl -tag -width [ENOMEM]
125.It Bq Er ENOMEM 124.It Bq Er ENOMEM
126The function 125The function
127.Fn setenv 126.Fn setenv
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c
index 09d47f2149..4db86df915 100644
--- a/src/lib/libc/stdlib/getenv.c
+++ b/src/lib/libc/stdlib/getenv.c
@@ -1,6 +1,6 @@
1/* 1/*
2 * Copyright (c) 1987 Regents of the University of California. 2 * Copyright (c) 1987, 1993
3 * All rights reserved. 3 * The Regents of the University of California. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -32,28 +32,13 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: getenv.c,v 1.4 1998/07/16 18:02:33 deraadt Exp $";
36static char *rcsid = "$Id: getenv.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
40#include <string.h> 39#include <string.h>
41 40
42/* 41/*
43 * getenv --
44 * Returns ptr to value associated with name, if any, else NULL.
45 */
46char *
47getenv(name)
48 const char *name;
49{
50 int offset;
51 char *__findenv();
52
53 return(__findenv(name, &offset));
54}
55
56/*
57 * __findenv -- 42 * __findenv --
58 * Returns pointer to value associated with name, if any, else NULL. 43 * Returns pointer to value associated with name, if any, else NULL.
59 * Sets offset to be the offset of the name/value combination in the 44 * Sets offset to be the offset of the name/value combination in the
@@ -64,19 +49,41 @@ getenv(name)
64 */ 49 */
65char * 50char *
66__findenv(name, offset) 51__findenv(name, offset)
67 register char *name; 52 register const char *name;
68 int *offset; 53 int *offset;
69{ 54{
70 extern char **environ; 55 extern char **environ;
71 register int len; 56 register int len, i;
72 register char **P, *C; 57 register const char *np;
58 register char **p, *cp;
73 59
74 for (C = name, len = 0; *C && *C != '='; ++C, ++len); 60 if (name == NULL || environ == NULL)
75 for (P = environ; *P; ++P) 61 return (NULL);
76 if (!strncmp(*P, name, len)) 62 for (np = name; *np && *np != '='; ++np)
77 if (*(C = *P + len) == '=') { 63 ;
78 *offset = P - environ; 64 len = np - name;
79 return(++C); 65 for (p = environ; (cp = *p) != NULL; ++p) {
80 } 66 for (np = name, i = len; i && *cp; i--)
81 return(NULL); 67 if (*cp++ != *np++)
68 break;
69 if (i == 0 && *cp++ == '=') {
70 *offset = p - environ;
71 return (cp);
72 }
73 }
74 return (NULL);
75}
76
77/*
78 * getenv --
79 * Returns ptr to value associated with name, if any, else NULL.
80 */
81char *
82getenv(name)
83 const char *name;
84{
85 int offset;
86 char *__findenv();
87
88 return(__findenv(name, &offset));
82} 89}
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index f843881afd..4340ff54fd 100644
--- a/src/lib/libc/stdlib/getopt.3
+++ b/src/lib/libc/stdlib/getopt.3
@@ -29,7 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 32.\" $OpenBSD: getopt.3,v 1.6 1998/05/05 19:36:03 deraadt Exp $
33.\" 33.\"
34.Dd April 19, 1994 34.Dd April 19, 1994
35.Dt GETOPT 3 35.Dt GETOPT 3
@@ -137,12 +137,12 @@ returns \-1.
137If the 137If the
138.Fn getopt 138.Fn getopt
139function encounters a character not found in the string 139function encounters a character not found in the string
140.Va optarg 140.Va optstring
141or detects 141or detects
142a missing option argument it writes an error message and returns 142a missing option argument it writes an error message to
143.Ql ? 143.Em stderr
144to the 144and returns
145.Em stderr . 145.Ql ? .
146Setting 146Setting
147.Va opterr 147.Va opterr
148to a zero will disable these error messages. 148to a zero will disable these error messages.
@@ -174,8 +174,8 @@ extern int optind;
174int bflag, ch, fd; 174int bflag, ch, fd;
175 175
176bflag = 0; 176bflag = 0;
177while ((ch = getopt(argc, argv, "bf:")) != -1) 177while ((ch = getopt(argc, argv, "bf:")) != -1) {
178 switch(ch) { 178 switch (ch) {
179 case 'b': 179 case 'b':
180 bflag = 1; 180 bflag = 1;
181 break; 181 break;
@@ -189,6 +189,7 @@ while ((ch = getopt(argc, argv, "bf:")) != -1)
189 case '?': 189 case '?':
190 default: 190 default:
191 usage(); 191 usage();
192 }
192} 193}
193argc -= optind; 194argc -= optind;
194argv += optind; 195argv += optind;
@@ -213,7 +214,7 @@ from
213.Pp 214.Pp
214A single dash 215A single dash
215.Dq Li - 216.Dq Li -
216may be specified as an character in 217may be specified as a character in
217.Fa optstring , 218.Fa optstring ,
218however it should 219however it should
219.Em never 220.Em never
diff --git a/src/lib/libc/stdlib/getopt.c b/src/lib/libc/stdlib/getopt.c
index 63c5e6a479..b7f6163662 100644
--- a/src/lib/libc/stdlib/getopt.c
+++ b/src/lib/libc/stdlib/getopt.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */ 35static char *rcsid = "$OpenBSD: getopt.c,v 1.2 1996/08/19 08:33:32 tholo Exp $";
36static char *rcsid = "$Id: getopt.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdio.h> 38#include <stdio.h>
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3
new file mode 100644
index 0000000000..8acc91bdd1
--- /dev/null
+++ b/src/lib/libc/stdlib/getsubopt.3
@@ -0,0 +1,148 @@
1.\" $OpenBSD: getsubopt.3,v 1.2 1998/06/15 17:55:07 mickey Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93
35.\"
36.Dd June 9, 1993
37.Dt GETSUBOPT 3
38.Os
39.Sh NAME
40.Nm getsubopt
41.Nd get sub options from an argument
42.Sh SYNOPSIS
43.Fd #include <stdlib.h>
44.Vt extern char *suboptarg
45.Ft int
46.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
47.Sh DESCRIPTION
48The
49.Fn getsubopt
50function
51parses a string containing tokens delimited by one or more tab, space or
52comma
53.Pq Ql \&,
54characters.
55It is intended for use in parsing groups of option arguments provided
56as part of a utility command line.
57.Pp
58The argument
59.Fa optionp
60is a pointer to a pointer to the string.
61The argument
62.Fa tokens
63is a pointer to a
64.Dv NULL Ns -terminated
65array of pointers to strings.
66.Pp
67The
68.Fn getsubopt
69function
70returns the zero-based offset of the pointer in the
71.Fa tokens
72array referencing a string which matches the first token
73in the string, or, \-1 if the string contains no tokens or
74.Fa tokens
75does not contain a matching string.
76.Pp
77If the token is of the form ``name=value'', the location referenced by
78.Fa valuep
79will be set to point to the start of the ``value'' portion of the token.
80.Pp
81On return from
82.Fn getsubopt ,
83.Fa optionp
84will be set to point to the start of the next token in the string,
85or the null at the end of the string if no more tokens are present.
86The external variable
87.Fa suboptarg
88will be set to point to the start of the current token, or
89.Dv NULL
90if no
91tokens were present.
92The argument
93.Fa valuep
94will be set to point to the ``value'' portion of the token, or
95.Dv NULL
96if no ``value'' portion was present.
97.Sh EXAMPLE
98.Bd -literal -compact
99char *tokens[] = {
100 #define ONE 0
101 "one",
102 #define TWO 1
103 "two",
104 NULL
105};
106
107\&...
108
109extern char *optarg, *suboptarg;
110char *options, *value;
111
112while ((ch = getopt(argc, argv, "ab:")) != \-1) {
113 switch(ch) {
114 case 'a':
115 /* process ``a'' option */
116 break;
117 case 'b':
118 options = optarg;
119 while (*options) {
120 switch(getsubopt(&options, tokens, &value)) {
121 case ONE:
122 /* process ``one'' sub option */
123 break;
124 case TWO:
125 /* process ``two'' sub option */
126 if (!value)
127 error("no value for two");
128 i = atoi(value);
129 break;
130 case \-1:
131 if (suboptarg)
132 error("illegal sub option %s",
133 suboptarg);
134 else
135 error("missing sub option");
136 break;
137 }
138 break;
139 }
140.Ed
141.Sh SEE ALSO
142.Xr getopt 3 ,
143.Xr strsep 3
144.Sh HISTORY
145The
146.Fn getsubopt
147function first appeared in
148.Bx 4.4 .
diff --git a/src/lib/libc/stdlib/getsubopt.c b/src/lib/libc/stdlib/getsubopt.c
new file mode 100644
index 0000000000..1667a31d7d
--- /dev/null
+++ b/src/lib/libc/stdlib/getsubopt.c
@@ -0,0 +1,106 @@
1/* $OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $ */
2
3/*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)getsubopt.c 8.1 (Berkeley) 6/4/93";
39#else
40static char rcsid[] = "$OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $";
41#endif
42#endif /* not lint */
43
44#include <unistd.h>
45#include <stdlib.h>
46#include <string.h>
47
48/*
49 * The SVID interface to getsubopt provides no way of figuring out which
50 * part of the suboptions list wasn't matched. This makes error messages
51 * tricky... The extern variable suboptarg is a pointer to the token
52 * which didn't match.
53 */
54char *suboptarg;
55
56int
57getsubopt(optionp, tokens, valuep)
58 register char **optionp, **valuep;
59 register char * const *tokens;
60{
61 register int cnt;
62 register char *p;
63
64 suboptarg = *valuep = NULL;
65
66 if (!optionp || !*optionp)
67 return(-1);
68
69 /* skip leading white-space, commas */
70 for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
71
72 if (!*p) {
73 *optionp = p;
74 return(-1);
75 }
76
77 /* save the start of the token, and skip the rest of the token. */
78 for (suboptarg = p;
79 *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
80
81 if (*p) {
82 /*
83 * If there's an equals sign, set the value pointer, and
84 * skip over the value part of the token. Terminate the
85 * token.
86 */
87 if (*p == '=') {
88 *p = '\0';
89 for (*valuep = ++p;
90 *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
91 if (*p)
92 *p++ = '\0';
93 } else
94 *p++ = '\0';
95 /* Skip any whitespace or commas after this token. */
96 for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
97 }
98
99 /* set optionp for next round. */
100 *optionp = p;
101
102 for (cnt = 0; *tokens; ++tokens, ++cnt)
103 if (!strcmp(suboptarg, *tokens))
104 return(cnt);
105 return(-1);
106}
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c
index bd998fa357..e3e4392e05 100644
--- a/src/lib/libc/stdlib/heapsort.c
+++ b/src/lib/libc/stdlib/heapsort.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char sccsid[] = "from: @(#)heapsort.c 8.1 (Berkeley) 6/4/93";*/ 38static char *rcsid = "$OpenBSD: heapsort.c,v 1.2 1996/08/19 08:33:32 tholo Exp $";
39static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <sys/types.h> 41#include <sys/types.h>
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c
index 205781e0ee..99cddb71e5 100644
--- a/src/lib/libc/stdlib/jrand48.c
+++ b/src/lib/libc/stdlib/jrand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: jrand48.c,v 1.2 1996/08/19 08:33:33 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16long 20long
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c
index 3069b31bf6..4e99391254 100644
--- a/src/lib/libc/stdlib/l64a.c
+++ b/src/lib/libc/stdlib/l64a.c
@@ -4,13 +4,14 @@
4 */ 4 */
5 5
6#if defined(LIBC_SCCS) && !defined(lint) 6#if defined(LIBC_SCCS) && !defined(lint)
7static char *rcsid = "$NetBSD: l64a.c,v 1.4 1995/05/11 23:04:52 jtc Exp $"; 7static char *rcsid = "$OpenBSD: l64a.c,v 1.3 1997/08/17 22:58:34 millert Exp $";
8#endif 8#endif /* LIBC_SCCS and not lint */
9 9
10#include <errno.h>
10#include <stdlib.h> 11#include <stdlib.h>
11 12
12char * 13char *
13l64a (value) 14l64a(value)
14 long value; 15 long value;
15{ 16{
16 static char buf[8]; 17 static char buf[8];
@@ -18,8 +19,10 @@ l64a (value)
18 int digit; 19 int digit;
19 int i; 20 int i;
20 21
21 if (!value) 22 if (value < 0) {
22 return NULL; 23 errno = EINVAL;
24 return(NULL);
25 }
23 26
24 for (i = 0; value != 0 && i < 6; i++) { 27 for (i = 0; value != 0 && i < 6; i++) {
25 digit = value & 0x3f; 28 digit = value & 0x3f;
@@ -39,5 +42,5 @@ l64a (value)
39 42
40 *s = '\0'; 43 *s = '\0';
41 44
42 return buf; 45 return(buf);
43} 46}
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3
index 28e4d2053c..05eae329df 100644
--- a/src/lib/libc/stdlib/labs.3
+++ b/src/lib/libc/stdlib/labs.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: labs.3,v 1.2 1996/08/19 08:33:34 tholo Exp $
37.\" $Id: labs.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt LABS 3 39.Dt LABS 3
diff --git a/src/lib/libc/stdlib/labs.c b/src/lib/libc/stdlib/labs.c
index ccf1415792..f20e2c29be 100644
--- a/src/lib/libc/stdlib/labs.c
+++ b/src/lib/libc/stdlib/labs.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ 35static char *rcsid = "$OpenBSD: labs.c,v 1.2 1996/08/19 08:33:34 tholo Exp $";
36static char *rcsid = "$Id: labs.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c
index 965d46b17a..44bd74e48a 100644
--- a/src/lib/libc/stdlib/lcong48.c
+++ b/src/lib/libc/stdlib/lcong48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: lcong48.c,v 1.2 1996/08/19 08:33:35 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3
index a7b5ccf878..8757f4ddd5 100644
--- a/src/lib/libc/stdlib/ldiv.3
+++ b/src/lib/libc/stdlib/ldiv.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)ldiv.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: ldiv.3,v 1.2 1996/08/19 08:33:35 tholo Exp $
37.\" $Id: ldiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt LDIV 3 39.Dt LDIV 3
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c
index f7074507e5..908c2bf0aa 100644
--- a/src/lib/libc/stdlib/ldiv.c
+++ b/src/lib/libc/stdlib/ldiv.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ 38static char *rcsid = "$OpenBSD: ldiv.c,v 1.2 1996/08/19 08:33:35 tholo Exp $";
39static char *rcsid = "$Id: ldiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <stdlib.h> /* ldiv_t */ 41#include <stdlib.h> /* ldiv_t */
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c
index 8e7f26237f..6b7524a51b 100644
--- a/src/lib/libc/stdlib/lrand48.c
+++ b/src/lib/libc/stdlib/lrand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: lrand48.c,v 1.2 1996/08/19 08:33:36 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 3bbf2bf65e..d5f8837ec2 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -1,5 +1,5 @@
1.\" Copyright (c) 1980, 1991 Regents of the University of California. 1.\" Copyright (c) 1980, 1991, 1993
2.\" All rights reserved. 2.\" The Regents of the University of California. All rights reserved.
3.\" 3.\"
4.\" This code is derived from software contributed to Berkeley by 4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information 5.\" the American National Standards Committee X3, on Information
@@ -33,19 +33,33 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 36.\" $OpenBSD: malloc.3,v 1.9 1998/08/15 20:32:02 deraadt Exp $
37.\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd August 27, 1996
40.Dt MALLOC 3 39.Dt MALLOC 3
41.Os BSD 4 40.Os OpenBSD
42.Sh NAME 41.Sh NAME
43.Nm malloc 42.Nm malloc ,
44.Nd general memory allocation function 43.Nd general memory allocation function
44.Pp
45.Nm free ,
46.Nm cfree
47.Nd free up memory allocated with malloc, calloc or realloc
48.Pp
49.Nm realloc
50.Nd reallocation of memory function
45.Sh SYNOPSIS 51.Sh SYNOPSIS
46.Fd #include <stdlib.h> 52.Fd #include <stdlib.h>
47.Ft void * 53.Ft void *
48.Fn malloc "size_t size" 54.Fn malloc "size_t size"
55.Ft void
56.Fn free "void *ptr"
57.Ft void
58.Fn cfree "void *ptr"
59.Ft void *
60.Fn realloc "void *ptr" "size_t size"
61.Ft char *
62.Va malloc_options
49.Sh DESCRIPTION 63.Sh DESCRIPTION
50The 64The
51.Fn malloc 65.Fn malloc
@@ -62,30 +76,275 @@ suitably aligned (after possible pointer
62coercion) for storage of any type of object. If the space is of 76coercion) for storage of any type of object. If the space is of
63.Em pagesize 77.Em pagesize
64or larger, the memory returned will be page-aligned. 78or larger, the memory returned will be page-aligned.
79.Pp
80Allocation of a zero size object returns a pointer to a zero size object.
81.Pp
82The
83.Fn free
84function causes the space pointed to by
85.Fa ptr
86to be deallocated, that is, at least made available for further allocation,
87but if possible, it will passed back to the kernel with
88.Xr sbrk 2 .
89If
90.Fa ptr
91is a null pointer, no action occurs.
92.Pp
93A
94.Fn cfree
95function is also provided for compatibility with old systems and other
96.Nm malloc
97libraries; it is simply an alias for
98.Fn free .
99.Pp
100The
101.Fn realloc
102function changes the size of the object pointed to by
103.Fa ptr
104to the size specified by
105.Fa size .
106The contents of the object are unchanged up to the lesser
107of the new and old sizes.
108If the new size is larger, the value of the newly allocated portion
109of the object is indeterminate and uninitialized.
110If
111.Fa ptr
112is a null pointer, the
113.Fn realloc
114function behaves like the
115.Fn malloc
116function for the specified size.
117If the space cannot be allocated, the object
118pointed to by
119.Fa ptr
120is unchanged.
121If
122.Fa size
123is zero and
124.Fa ptr
125is not a null pointer, the object it points to is freed and a new zero size
126object is returned.
127.Pp
128When using
129.Fn realloc
130one must be careful to avoid the following idiom:
131.Pp
132.Bd -literal -offset indent
133if ((p = realloc(p, nsize)) == NULL)
134 return NULL;
135.Ed
136.Pp
137In most cases, this will result in a leak of memory.
138As stated earlier, a return value of
139.Fa NULL
140indicates that the old object still remains allocated.
141Better code looks like this:
142.Bd -literal -offset indent
143if ((p2 = realloc(p, nsize)) == NULL) {
144 if (p)
145 free(p);
146 p = NULL;
147 return NULL;
148}
149p = p2;
150.Ed
151.Pp
152Malloc will first look for a symbolic link called
153.Pa /etc/malloc.conf
154and next check the environment for a variable called
155.Ev MALLOC_OPTIONS
156and finally for the global variable
157.Va malloc_options
158and scan them for flags in that order.
159Flags are single letters, uppercase means on, lowercase means off.
160.Bl -tag -width indent
161.It A
162``abort'' malloc will coredump the process, rather than tolerate failure.
163This is a very handy debugging aid, since the core file will represent the
164time of failure,
165rather than when the NULL pointer was accessed.
166
167.It D
168``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit.
169This option requires the library to have been compiled with -DMALLOC_STATS in
170order to have any effect.
171
172.It J
173``junk'' fill some junk into the area allocated.
174Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-)
175
176.It H
177``hint'' pass a hint to the kernel about pages we don't use. If the
178machine is paging a lot this may help a bit.
179
180.It N
181Do not output warning messages when encountering possible corruption
182or bad pointers.
183
184.It R
185``realloc'' always reallocate when
186.Fn realloc
187is called, even if the initial allocation was big enough.
188This can substantially aid in compacting memory.
189
190.It U
191``utrace'' generate entries for
192.Xr ktrace 1
193for all operations.
194Consult the source for this one.
195
196.It X
197``xmalloc''
198rather than return failure,
199.Xr abort 3
200the program with a diagnostic message on stderr.
201It is the intention that this option be set at compile time by
202including in the source:
203.Bd -literal -offset indent
204extern char *malloc_options;
205malloc_options = "X";
206.Ed
207
208.It Z
209``zero'' fill some junk into the area allocated (see ``J''),
210except for the exact length the user asked for, which is zeroed.
211
212.It <
213``Half the cache size'' Reduce the size of the cache by a factor of two.
214
215.It >
216``Double the cache size'' Double the size of the cache by a factor of two.
217.El
218.Pp
219So to set a systemwide reduction of cache size and coredumps on problems
220one would:
221.Li ln -s 'A<' /etc/malloc.conf
222.Pp
223The ``J'' and ``Z'' is mostly for testing and debugging,
224if a program changes behavior if either of these options are used,
225it is buggy.
226.Pp
227The default cache size is 16 pages.
228.Sh ENVIRONMENT
229See above.
65.Sh RETURN VALUES 230.Sh RETURN VALUES
66The 231The
67.Fn malloc 232.Fn malloc
68function returns 233function returns
69a pointer to the allocated space if successful; otherwise 234a pointer to the allocated space if successful; otherwise
70a null pointer is returned. 235a null pointer is returned.
236.Pp
237The
238.Fn free
239function returns no value.
240.Pp
241The
242.Fn realloc
243function a pointer to the possibly moved allocated space;
244otherwise a null pointer is returned.
245.Sh MESSAGES
246If
247.Fn malloc ,
248.Fn free
249or
250.Fn realloc
251detects an error or warning condition,
252a message will be printed to filedescriptor
2532 (not using stdio).
254Errors will always result in the process being
255.Xr abort 2 'ed,
256If the ``A'' option has been specified, also warnings will
257.Xr abort 2
258the process.
259.Pp
260Here is a brief description of the error messages and what they mean:
261.Pp
262``(ES): mumble mumble mumble'':
263malloc have been compiled with -DEXTRA_SANITY and something looks
264fishy in there. Consult sources and or wizards.
265.Pp
266``allocation failed''
267if the ``A'' option is specified it is an error for
268.Fn malloc
269or
270.Fn realloc
271to return NULL.
272.Pp
273``mmap(2) failed, check limits.''
274This is a rather weird condition that is most likely to mean that
275the system is seriously overloaded or that your ulimits are sick.
276.Pp
277``freelist is destroyed.''
278mallocs internal freelist has been stomped on.
279.Pp
280Here is a brief description of the warning messages and what they mean:
281.Pp
282``chunk/page is already free.''
283A pointer to a free chunk is attempted freed again.
284.Pp
285``junk pointer, too high to make sense.''
286The pointer doesn't make sense. It's above the area of memory that
287malloc knows something about.
288This could be a pointer from some
289.Xr mmap 2 'ed
290memory.
291.Pp
292``junk pointer, too low to make sense.''
293The pointer doesn't make sense. It's below the area of memory that
294malloc knows something about.
295This pointer probably came from your data or bss segments.
296.Pp
297``malloc() has never been called.''
298Nothing has ever been allocated, yet something is being freed or
299realloc'ed.
300.Pp
301``modified (chunk-/page-) pointer.''
302The pointer passed to free or realloc has been modified.
303.Pp
304``pointer to wrong page.''
305The pointer that malloc is trying to free is not pointing to
306a sensible page.
307.Pp
308``recursive call.''
309You have tried to call recursively into these functions.
310I can only imagine this as happening if you call one of these
311functions from a signal function, which happens to be called
312while you're already in here.
313Well, sorry to say: that's not supported.
314If this is a problem for you I'd like to hear about it. It
315would be possible to add a sigblock() around this package,
316but it would have a performance penalty that is not acceptable
317as the default.
318.Pp
319``unknown char in MALLOC_OPTIONS''
320we found something we didn't understand.
71.Sh SEE ALSO 321.Sh SEE ALSO
72.Xr brk 2 , 322.Xr brk 2 ,
73.Xr getpagesize 2 ,
74.Xr free 3 ,
75.Xr calloc 3 ,
76.Xr alloca 3 , 323.Xr alloca 3 ,
77.Xr realloc 3 , 324.Xr calloc 3 ,
325.Xr getpagesize 3 ,
78.Xr memory 3 326.Xr memory 3
327.Pa /usr/share/doc/papers/malloc.ascii.gz
79.Sh STANDARDS 328.Sh STANDARDS
80The 329The
81.Fn malloc 330.Fn malloc
82function conforms to 331function conforms to
83.St -ansiC . 332.St -ansiC .
84.Sh BUGS 333.Sh HISTORY
85The current implementation of 334The present implementation of malloc started out as a filesystem on a drum
86.Xr malloc 335attached to a 20bit binary challenged computer built with discrete germanium
87does not always fail gracefully when system 336transistors, and it has since graduated to handle primary storage rather than
88memory limits are approached. 337secondary.
89It may fail to allocate memory when larger free blocks could be broken 338.Pp
90up, or when limits are exceeded because the size is rounded up. 339The main difference from other malloc implementations are believed to be that
91It is optimized for sizes that are powers of two. 340the free pages are not accessed until allocated.
341Most malloc implementations will store a data structure containing a,
342possibly double-, linked list in the free chunks of memory, used to tie
343all the free memory together.
344That is a quite suboptimal thing to do.
345Every time the free-list is traversed, all the otherwise unused, and very
346likely paged out, pages get faulted into primary memory, just to see what
347lies after them in the list.
348.Pp
349On systems which are paging, this can make a factor five in difference on the
350page-faults of a process.
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 3c57fad024..d1d8759791 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,421 +1,1242 @@
1/* 1/*
2 * Copyright (c) 1983 Regents of the University of California. 2 * ----------------------------------------------------------------------------
3 * All rights reserved. 3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * 4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * Redistribution and use in source and binary forms, with or without 5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * modification, are permitted provided that the following conditions 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * are met: 7 * ----------------------------------------------------------------------------
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 */ 8 */
33 9
34#if defined(LIBC_SCCS) && !defined(lint) 10#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ 11static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp $";
36static char *rcsid = "$Id: malloc.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 12#endif /* LIBC_SCCS and not lint */
38 13
39/* 14/*
40 * malloc.c (Caltech) 2/21/82 15 * Defining MALLOC_EXTRA_SANITY will enable extra checks which are
41 * Chris Kingsley, kingsley@cit-20. 16 * related to internal conditions and consistency in malloc.c. This has
42 * 17 * a noticeable runtime performance hit, and generally will not do you
43 * This is a very fast storage allocator. It allocates blocks of a small 18 * any good unless you fiddle with the internals of malloc or want
44 * number of different sizes, and keeps free lists of each size. Blocks that 19 * to catch random pointer corruption as early as possible.
45 * don't exactly fit are passed up to the next larger size. In this
46 * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
47 * This is designed for use in a virtual memory environment.
48 */ 20 */
21#ifndef MALLOC_EXTRA_SANITY
22#undef MALLOC_EXTRA_SANITY
23#endif
49 24
50#include <sys/types.h> 25/*
26 * Defining MALLOC_STATS will enable you to call malloc_dump() and set
27 * the [dD] options in the MALLOC_OPTIONS environment variable.
28 * It has no run-time performance hit, but does pull in stdio...
29 */
30#ifndef MALLOC_STATS
31#undef MALLOC_STATS
32#endif
33
34/*
35 * What to use for Junk. This is the byte value we use to fill with
36 * when the 'J' option is enabled.
37 */
38#define SOME_JUNK 0xd0 /* as in "Duh" :-) */
39
40#include <stdio.h>
51#include <stdlib.h> 41#include <stdlib.h>
52#include <string.h> 42#include <string.h>
53#include <unistd.h> 43#include <unistd.h>
44#include <fcntl.h>
45#include <errno.h>
46#include <sys/types.h>
47#include <sys/param.h>
48#include <sys/mman.h>
49
50/*
51 * The basic parameters you can tweak.
52 *
53 * malloc_pageshift pagesize = 1 << malloc_pageshift
54 * It's probably best if this is the native
55 * page size, but it shouldn't have to be.
56 *
57 * malloc_minsize minimum size of an allocation in bytes.
58 * If this is too small it's too much work
59 * to manage them. This is also the smallest
60 * unit of alignment used for the storage
61 * returned by malloc/realloc.
62 *
63 */
64
65#if defined(__i386__) && defined(__FreeBSD__)
66# define malloc_pageshift 12U
67# define malloc_minsize 16U
68#endif /* __i386__ && __FreeBSD__ */
54 69
55#define NULL 0 70#if defined(__sparc__) && !defined(__OpenBSD__)
71# define malloc_pageshirt 12U
72# define malloc_minsize 16U
73# define MAP_ANON (0)
74# define USE_DEV_ZERO
75# define MADV_FREE MADV_DONTNEED
76#endif /* __sparc__ */
56 77
57static void morecore(); 78/* Insert your combination here... */
58static int findbucket(); 79#if defined(__FOOCPU__) && defined(__BAROS__)
80# define malloc_pageshift 12U
81# define malloc_minsize 16U
82#endif /* __FOOCPU__ && __BAROS__ */
83
84#if defined(__OpenBSD__) && !defined(__sparc__)
85# define malloc_pageshift (PGSHIFT)
86# define malloc_minsize 16U
87#endif /* __OpenBSD__ */
88
89#ifdef _THREAD_SAFE
90#include <pthread.h>
91static pthread_mutex_t malloc_lock;
92#define THREAD_LOCK() pthread_mutex_lock(&malloc_lock)
93#define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock)
94#define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0);
95#else
96#define THREAD_LOCK()
97#define THREAD_UNLOCK()
98#define THREAD_LOCK_INIT()
99#endif
59 100
60/* 101/*
61 * The overhead on a block is at least 4 bytes. When free, this space 102 * No user serviceable parts behind this point.
62 * contains a pointer to the next free block, and the bottom two bits must 103 *
63 * be zero. When in use, the first byte is set to MAGIC, and the second 104 * This structure describes a page worth of chunks.
64 * byte is the size index. The remaining bytes are for alignment.
65 * If range checking is enabled then a second word holds the size of the
66 * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
67 * The order of elements is critical: ov_magic must overlay the low order
68 * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
69 */ 105 */
70union overhead { 106
71 union overhead *ov_next; /* when free */ 107struct pginfo {
72 struct { 108 struct pginfo *next; /* next on the free list */
73 u_char ovu_magic; /* magic number */ 109 void *page; /* Pointer to the page */
74 u_char ovu_index; /* bucket # */ 110 u_short size; /* size of this page's chunks */
75#ifdef RCHECK 111 u_short shift; /* How far to shift for this size chunks */
76 u_short ovu_rmagic; /* range magic number */ 112 u_short free; /* How many free chunks */
77 u_long ovu_size; /* actual block size */ 113 u_short total; /* How many chunk */
78#endif 114 u_long bits[1]; /* Which chunks are free */
79 } ovu; 115};
80#define ov_magic ovu.ovu_magic 116
81#define ov_index ovu.ovu_index 117/*
82#define ov_rmagic ovu.ovu_rmagic 118 * This structure describes a number of free pages.
83#define ov_size ovu.ovu_size 119 */
120
121struct pgfree {
122 struct pgfree *next; /* next run of free pages */
123 struct pgfree *prev; /* prev run of free pages */
124 void *page; /* pointer to free pages */
125 void *end; /* pointer to end of free pages */
126 u_long size; /* number of bytes free */
84}; 127};
85 128
86#define MAGIC 0xef /* magic # on accounting info */ 129/*
87#define RMAGIC 0x5555 /* magic # on range info */ 130 * How many bits per u_long in the bitmap.
131 * Change only if not 8 bits/byte
132 */
133#define MALLOC_BITS (8*sizeof(u_long))
134
135/*
136 * Magic values to put in the page_directory
137 */
138#define MALLOC_NOT_MINE ((struct pginfo*) 0)
139#define MALLOC_FREE ((struct pginfo*) 1)
140#define MALLOC_FIRST ((struct pginfo*) 2)
141#define MALLOC_FOLLOW ((struct pginfo*) 3)
142#define MALLOC_MAGIC ((struct pginfo*) 4)
143
144#ifndef malloc_pageshift
145#define malloc_pageshift 12U
146#endif
147
148#ifndef malloc_minsize
149#define malloc_minsize 16U
150#endif
151
152#ifndef malloc_pageshift
153#error "malloc_pageshift undefined"
154#endif
155
156#if !defined(malloc_pagesize)
157#define malloc_pagesize (1UL<<malloc_pageshift)
158#endif
159
160#if ((1UL<<malloc_pageshift) != malloc_pagesize)
161#error "(1UL<<malloc_pageshift) != malloc_pagesize"
162#endif
163
164#ifndef malloc_maxsize
165#define malloc_maxsize ((malloc_pagesize)>>1)
166#endif
167
168/* A mask for the offset inside a page. */
169#define malloc_pagemask ((malloc_pagesize)-1)
170
171#define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask)))
172#define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)-malloc_origo)
88 173
89#ifdef RCHECK 174/* fd of /dev/zero */
90#define RSLOP sizeof (u_short) 175#ifdef USE_DEV_ZERO
176static int fdzero;
177#define MMAP_FD fdzero
178#define INIT_MMAP() \
179 { if ((fdzero=open("/dev/zero", O_RDWR, 0000)) == -1) \
180 wrterror("open of /dev/zero"); }
91#else 181#else
92#define RSLOP 0 182#define MMAP_FD (-1)
183#define INIT_MMAP()
93#endif 184#endif
94 185
186/* Set when initialization has been done */
187static unsigned malloc_started;
188
189/* Number of free pages we cache */
190static unsigned malloc_cache = 16;
191
192/* The offset from pagenumber to index into the page directory */
193static u_long malloc_origo;
194
195/* The last index in the page directory we care about */
196static u_long last_index;
197
198/* Pointer to page directory. Allocated "as if with" malloc */
199static struct pginfo **page_dir;
200
201/* How many slots in the page directory */
202static size_t malloc_ninfo;
203
204/* Free pages line up here */
205static struct pgfree free_list;
206
207/* Abort(), user doesn't handle problems. */
208static int malloc_abort;
209
210/* Are we trying to die ? */
211static int suicide;
212
213#ifdef MALLOC_STATS
214/* dump statistics */
215static int malloc_stats;
216#endif
217
218/* avoid outputting warnings? */
219static int malloc_silent;
220
221/* always realloc ? */
222static int malloc_realloc;
223
224#ifdef __FreeBSD__
225/* pass the kernel a hint on free pages ? */
226static int malloc_hint;
227#endif
228
229/* xmalloc behaviour ? */
230static int malloc_xmalloc;
231
232/* zero fill ? */
233static int malloc_zero;
234
235/* junk fill ? */
236static int malloc_junk;
237
238#ifdef __FreeBSD__
239/* utrace ? */
240static int malloc_utrace;
241
242struct ut { void *p; size_t s; void *r; };
243
244void utrace __P((struct ut *, int));
245
246#define UTRACE(a, b, c) \
247 if (malloc_utrace) \
248 {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);}
249#else /* !__FreeBSD__ */
250#define UTRACE(a,b,c)
251#endif
252
253/* my last break. */
254static void *malloc_brk;
255
256/* one location cache for free-list holders */
257static struct pgfree *px;
258
259/* compile-time options */
260char *malloc_options;
261
262/* Name of the current public function */
263static char *malloc_func;
264
265/* Macro for mmap */
266#define MMAP(size) \
267 mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \
268 MMAP_FD, (off_t)0);
269
95/* 270/*
96 * nextf[i] is the pointer to the next free block of size 2^(i+3). The 271 * Necessary function declarations
97 * smallest allocatable block is 8 bytes. The overhead information
98 * precedes the data area returned to the user.
99 */ 272 */
100#define NBUCKETS 30 273static int extend_pgdir(u_long index);
101static union overhead *nextf[NBUCKETS]; 274static void *imalloc(size_t size);
102extern char *sbrk(); 275static void ifree(void *ptr);
276static void *irealloc(void *ptr, size_t size);
277static void *malloc_bytes(size_t size);
278
279#ifdef MALLOC_STATS
280void
281malloc_dump(fd)
282 FILE *fd;
283{
284 struct pginfo **pd;
285 struct pgfree *pf;
286 int j;
287
288 pd = page_dir;
289
290 /* print out all the pages */
291 for(j=0;j<=last_index;j++) {
292 fprintf(fd, "%08lx %5d ", (j+malloc_origo) << malloc_pageshift, j);
293 if (pd[j] == MALLOC_NOT_MINE) {
294 for(j++;j<=last_index && pd[j] == MALLOC_NOT_MINE;j++)
295 ;
296 j--;
297 fprintf(fd, ".. %5d not mine\n", j);
298 } else if (pd[j] == MALLOC_FREE) {
299 for(j++;j<=last_index && pd[j] == MALLOC_FREE;j++)
300 ;
301 j--;
302 fprintf(fd, ".. %5d free\n", j);
303 } else if (pd[j] == MALLOC_FIRST) {
304 for(j++;j<=last_index && pd[j] == MALLOC_FOLLOW;j++)
305 ;
306 j--;
307 fprintf(fd, ".. %5d in use\n", j);
308 } else if (pd[j] < MALLOC_MAGIC) {
309 fprintf(fd, "(%p)\n", pd[j]);
310 } else {
311 fprintf(fd, "%p %d (of %d) x %d @ %p --> %p\n",
312 pd[j], pd[j]->free, pd[j]->total,
313 pd[j]->size, pd[j]->page, pd[j]->next);
314 }
315 }
316
317 for(pf=free_list.next; pf; pf=pf->next) {
318 fprintf(fd, "Free: @%p [%p...%p[ %ld ->%p <-%p\n",
319 pf, pf->page, pf->end, pf->size, pf->prev, pf->next);
320 if (pf == pf->next) {
321 fprintf(fd, "Free_list loops.\n");
322 break;
323 }
324 }
325
326 /* print out various info */
327 fprintf(fd, "Minsize\t%d\n", malloc_minsize);
328 fprintf(fd, "Maxsize\t%d\n", malloc_maxsize);
329 fprintf(fd, "Pagesize\t%lu\n", (u_long)malloc_pagesize);
330 fprintf(fd, "Pageshift\t%d\n", malloc_pageshift);
331 fprintf(fd, "FirstPage\t%ld\n", malloc_origo);
332 fprintf(fd, "LastPage\t%ld %lx\n", last_index+malloc_pageshift,
333 (last_index + malloc_pageshift) << malloc_pageshift);
334 fprintf(fd, "Break\t%ld\n", (u_long)sbrk(0) >> malloc_pageshift);
335}
336#endif /* MALLOC_STATS */
337
338extern char *__progname;
339
340static void
341wrterror(p)
342 char *p;
343{
344 char *q = " error: ";
345 write(2, __progname, strlen(__progname));
346 write(2, malloc_func, strlen(malloc_func));
347 write(2, q, strlen(q));
348 write(2, p, strlen(p));
349 suicide = 1;
350#ifdef MALLOC_STATS
351 if (malloc_stats)
352 malloc_dump(stderr);
353#endif /* MALLOC_STATS */
354 abort();
355}
356
357static void
358wrtwarning(p)
359 char *p;
360{
361 char *q = " warning: ";
362 if (malloc_abort)
363 wrterror(p);
364 else if (malloc_silent)
365 return;
366 write(2, __progname, strlen(__progname));
367 write(2, malloc_func, strlen(malloc_func));
368 write(2, q, strlen(q));
369 write(2, p, strlen(p));
370}
371
372#ifdef MALLOC_STATS
373static void
374malloc_exit()
375{
376 FILE *fd = fopen("malloc.out", "a");
377 char *q = "malloc() warning: Couldn't dump stats.\n";
378 if (fd) {
379 malloc_dump(fd);
380 fclose(fd);
381 } else
382 write(2, q, strlen(q));
383}
384#endif /* MALLOC_STATS */
103 385
104static int pagesz; /* page size */
105static int pagebucket; /* page size bucket */
106 386
107#ifdef MSTATS
108/* 387/*
109 * nmalloc[i] is the difference between the number of mallocs and frees 388 * Allocate a number of pages from the OS
110 * for a given block size.
111 */ 389 */
112static u_int nmalloc[NBUCKETS]; 390static void *
113#include <stdio.h> 391map_pages(pages)
114#endif 392 int pages;
393{
394 caddr_t result, tail;
115 395
116#if defined(DEBUG) || defined(RCHECK) 396 result = (caddr_t)pageround((u_long)sbrk(0));
117#define ASSERT(p) if (!(p)) botch("p") 397 tail = result + (pages << malloc_pageshift);
118#include <stdio.h> 398
119static 399 if (brk(tail)) {
120botch(s) 400#ifdef MALLOC_EXTRA_SANITY
121 char *s; 401 wrterror("(ES): map_pages fails\n");
402#endif /* MALLOC_EXTRA_SANITY */
403 return 0;
404 }
405
406 last_index = ptr2index(tail) - 1;
407 malloc_brk = tail;
408
409 if ((last_index+1) >= malloc_ninfo && !extend_pgdir(last_index))
410 return 0;
411
412 return result;
413}
414
415/*
416 * Extend page directory
417 */
418static int
419extend_pgdir(index)
420 u_long index;
122{ 421{
123 fprintf(stderr, "\r\nassertion botched: %s\r\n", s); 422 struct pginfo **new, **old;
124 (void) fflush(stderr); /* just in case user buffered it */ 423 size_t i, oldlen;
125 abort(); 424
425 /* Make it this many pages */
426 i = index * sizeof *page_dir;
427 i /= malloc_pagesize;
428 i += 2;
429
430 /* remember the old mapping size */
431 oldlen = malloc_ninfo * sizeof *page_dir;
432
433 /*
434 * NOTE: we allocate new pages and copy the directory rather than tempt
435 * fate by trying to "grow" the region.. There is nothing to prevent
436 * us from accidently re-mapping space that's been allocated by our caller
437 * via dlopen() or other mmap().
438 *
439 * The copy problem is not too bad, as there is 4K of page index per
440 * 4MB of malloc arena.
441 *
442 * We can totally avoid the copy if we open a file descriptor to associate
443 * the anon mappings with. Then, when we remap the pages at the new
444 * address, the old pages will be "magically" remapped.. But this means
445 * keeping open a "secret" file descriptor.....
446 */
447
448 /* Get new pages */
449 new = (struct pginfo**) MMAP(i * malloc_pagesize);
450 if (new == (struct pginfo **)-1)
451 return 0;
452
453 /* Copy the old stuff */
454 memcpy(new, page_dir,
455 malloc_ninfo * sizeof *page_dir);
456
457 /* register the new size */
458 malloc_ninfo = i * malloc_pagesize / sizeof *page_dir;
459
460 /* swap the pointers */
461 old = page_dir;
462 page_dir = new;
463
464 /* Now free the old stuff */
465 munmap(old, oldlen);
466 return 1;
126} 467}
127#else
128#define ASSERT(p)
129#endif
130 468
131void * 469/*
132malloc(nbytes) 470 * Initialize the world
133 size_t nbytes; 471 */
472static void
473malloc_init ()
134{ 474{
135 register union overhead *op; 475 char *p, b[64];
136 register long bucket, n; 476 int i, j;
137 register unsigned amt; 477 int save_errno = errno;
138 478
139 /* 479 THREAD_LOCK_INIT();
140 * First time malloc is called, setup page size and 480
141 * align break pointer so all data will be page aligned. 481 INIT_MMAP();
142 */ 482
143 if (pagesz == 0) { 483#ifdef MALLOC_EXTRA_SANITY
144 pagesz = n = getpagesize(); 484 malloc_junk = 1;
145 op = (union overhead *)sbrk(0); 485#endif /* MALLOC_EXTRA_SANITY */
146 n = n - sizeof (*op) - ((long)op & (n - 1)); 486
147 if (n < 0) 487 for (i = 0; i < 3; i++) {
148 n += pagesz; 488 if (i == 0) {
149 if (n) { 489 j = readlink("/etc/malloc.conf", b, sizeof b - 1);
150 if (sbrk(n) == (char *)-1) 490 if (j <= 0)
151 return (NULL); 491 continue;
152 } 492 b[j] = '\0';
153 bucket = 0; 493 p = b;
154 amt = 8; 494 } else if (i == 1) {
155 while (pagesz > amt) { 495 if (issetugid() == 0)
156 amt <<= 1; 496 p = getenv("MALLOC_OPTIONS");
157 bucket++; 497 else
158 } 498 continue;
159 pagebucket = bucket; 499 } else if (i == 2) {
500 p = malloc_options;
160 } 501 }
161 /* 502 for (; p && *p; p++) {
162 * Convert amount of memory requested into closest block size 503 switch (*p) {
163 * stored in hash buckets which satisfies request. 504 case '>': malloc_cache <<= 1; break;
164 * Account for space used per block for accounting. 505 case '<': malloc_cache >>= 1; break;
165 */ 506 case 'a': malloc_abort = 0; break;
166 if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { 507 case 'A': malloc_abort = 1; break;
167#ifndef RCHECK 508#ifdef MALLOC_STATS
168 amt = 8; /* size of first bucket */ 509 case 'd': malloc_stats = 0; break;
169 bucket = 0; 510 case 'D': malloc_stats = 1; break;
170#else 511#endif /* MALLOC_STATS */
171 amt = 16; /* size of first bucket */ 512#ifdef __FreeBSD__
172 bucket = 1; 513 case 'h': malloc_hint = 0; break;
173#endif 514 case 'H': malloc_hint = 1; break;
174 n = -((long)sizeof (*op) + RSLOP); 515#endif /* __FreeBSD__ */
175 } else { 516 case 'r': malloc_realloc = 0; break;
176 amt = pagesz; 517 case 'R': malloc_realloc = 1; break;
177 bucket = pagebucket; 518 case 'j': malloc_junk = 0; break;
519 case 'J': malloc_junk = 1; break;
520 case 'n': malloc_silent = 0; break;
521 case 'N': malloc_silent = 1; break;
522#ifdef __FreeBSD__
523 case 'u': malloc_utrace = 0; break;
524 case 'U': malloc_utrace = 1; break;
525#endif /* __FreeBSD__ */
526 case 'x': malloc_xmalloc = 0; break;
527 case 'X': malloc_xmalloc = 1; break;
528 case 'z': malloc_zero = 0; break;
529 case 'Z': malloc_zero = 1; break;
530 default:
531 j = malloc_abort;
532 malloc_abort = 0;
533 wrtwarning("unknown char in MALLOC_OPTIONS\n");
534 malloc_abort = j;
535 break;
536 }
178 } 537 }
179 while (nbytes > amt + n) { 538 }
180 amt <<= 1; 539
181 if (amt == 0) 540 UTRACE(0, 0, 0);
182 return (NULL); 541
183 bucket++; 542 /*
543 * We want junk in the entire allocation, and zero only in the part
544 * the user asked for.
545 */
546 if (malloc_zero)
547 malloc_junk=1;
548
549#ifdef MALLOC_STATS
550 if (malloc_stats)
551 atexit(malloc_exit);
552#endif /* MALLOC_STATS */
553
554 /* Allocate one page for the page directory */
555 page_dir = (struct pginfo **) MMAP(malloc_pagesize);
556
557 if (page_dir == (struct pginfo **) -1)
558 wrterror("mmap(2) failed, check limits.\n");
559
560 /*
561 * We need a maximum of malloc_pageshift buckets, steal these from the
562 * front of the page_directory;
563 */
564 malloc_origo = ((u_long)pageround((u_long)sbrk(0))) >> malloc_pageshift;
565 malloc_origo -= malloc_pageshift;
566
567 malloc_ninfo = malloc_pagesize / sizeof *page_dir;
568
569 /* Been here, done that */
570 malloc_started++;
571
572 /* Recalculate the cache size in bytes, and make sure it's nonzero */
573
574 if (!malloc_cache)
575 malloc_cache++;
576
577 malloc_cache <<= malloc_pageshift;
578
579 /*
580 * This is a nice hack from Kaleb Keithly (kaleb@x.org).
581 * We can sbrk(2) further back when we keep this on a low address.
582 */
583 px = (struct pgfree *) imalloc (sizeof *px);
584 errno = save_errno;
585}
586
587/*
588 * Allocate a number of complete pages
589 */
590static void *
591malloc_pages(size)
592 size_t size;
593{
594 void *p, *delay_free = 0;
595 int i;
596 struct pgfree *pf;
597 u_long index;
598
599 size = pageround(size);
600
601 p = 0;
602 /* Look for free pages before asking for more */
603 for(pf = free_list.next; pf; pf = pf->next) {
604
605#ifdef MALLOC_EXTRA_SANITY
606 if (pf->size & malloc_pagemask)
607 wrterror("(ES): junk length entry on free_list\n");
608 if (!pf->size)
609 wrterror("(ES): zero length entry on free_list\n");
610 if (pf->page == pf->end)
611 wrterror("(ES): zero entry on free_list\n");
612 if (pf->page > pf->end)
613 wrterror("(ES): sick entry on free_list\n");
614 if ((void*)pf->page >= (void*)sbrk(0))
615 wrterror("(ES): entry on free_list past brk\n");
616 if (page_dir[ptr2index(pf->page)] != MALLOC_FREE)
617 wrterror("(ES): non-free first page on free-list\n");
618 if (page_dir[ptr2index(pf->end)-1] != MALLOC_FREE)
619 wrterror("(ES): non-free last page on free-list\n");
620#endif /* MALLOC_EXTRA_SANITY */
621
622 if (pf->size < size)
623 continue;
624
625 if (pf->size == size) {
626 p = pf->page;
627 if (pf->next)
628 pf->next->prev = pf->prev;
629 pf->prev->next = pf->next;
630 delay_free = pf;
631 break;
632 }
633
634 p = pf->page;
635 pf->page = (char *)pf->page + size;
636 pf->size -= size;
637 break;
638 }
639
640#ifdef MALLOC_EXTRA_SANITY
641 if (p && page_dir[ptr2index(p)] != MALLOC_FREE)
642 wrterror("(ES): allocated non-free page on free-list\n");
643#endif /* MALLOC_EXTRA_SANITY */
644
645 size >>= malloc_pageshift;
646
647 /* Map new pages */
648 if (!p)
649 p = map_pages(size);
650
651 if (p) {
652
653 index = ptr2index(p);
654 page_dir[index] = MALLOC_FIRST;
655 for (i=1;i<size;i++)
656 page_dir[index+i] = MALLOC_FOLLOW;
657
658 if (malloc_junk)
659 memset(p, SOME_JUNK, size << malloc_pageshift);
660 }
661
662 if (delay_free) {
663 if (!px)
664 px = delay_free;
665 else
666 ifree(delay_free);
667 }
668
669 return p;
670}
671
672/*
673 * Allocate a page of fragments
674 */
675
676static __inline__ int
677malloc_make_chunks(bits)
678 int bits;
679{
680 struct pginfo *bp;
681 void *pp;
682 int i, k, l;
683
684 /* Allocate a new bucket */
685 pp = malloc_pages((size_t)malloc_pagesize);
686 if (!pp)
687 return 0;
688
689 /* Find length of admin structure */
690 l = sizeof *bp - sizeof(u_long);
691 l += sizeof(u_long) *
692 (((malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS);
693
694 /* Don't waste more than two chunks on this */
695 if ((1UL<<(bits)) <= l+l) {
696 bp = (struct pginfo *)pp;
697 } else {
698 bp = (struct pginfo *)imalloc(l);
699 if (!bp) {
700 ifree(pp);
701 return 0;
184 } 702 }
185 /* 703 }
186 * If nothing in hash bucket right now, 704
187 * request more memory from the system. 705 bp->size = (1UL<<bits);
188 */ 706 bp->shift = bits;
189 if ((op = nextf[bucket]) == NULL) { 707 bp->total = bp->free = malloc_pagesize >> bits;
190 morecore(bucket); 708 bp->page = pp;
191 if ((op = nextf[bucket]) == NULL) 709
192 return (NULL); 710 /* set all valid bits in the bitmap */
711 k = bp->total;
712 i = 0;
713
714 /* Do a bunch at a time */
715 for(;k-i >= MALLOC_BITS; i += MALLOC_BITS)
716 bp->bits[i / MALLOC_BITS] = ~0UL;
717
718 for(; i < k; i++)
719 bp->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS);
720
721 if (bp == bp->page) {
722 /* Mark the ones we stole for ourselves */
723 for(i=0;l > 0;i++) {
724 bp->bits[i/MALLOC_BITS] &= ~(1UL<<(i%MALLOC_BITS));
725 bp->free--;
726 bp->total--;
727 l -= (1 << bits);
193 } 728 }
194 /* remove from linked list */ 729 }
195 nextf[bucket] = op->ov_next; 730
196 op->ov_magic = MAGIC; 731 /* MALLOC_LOCK */
197 op->ov_index = bucket; 732
198#ifdef MSTATS 733 page_dir[ptr2index(pp)] = bp;
199 nmalloc[bucket]++; 734
200#endif 735 bp->next = page_dir[bits];
201#ifdef RCHECK 736 page_dir[bits] = bp;
202 /* 737
203 * Record allocated size of block and 738 /* MALLOC_UNLOCK */
204 * bound space with magic numbers. 739
205 */ 740 return 1;
206 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
207 op->ov_rmagic = RMAGIC;
208 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
209#endif
210 return ((char *)(op + 1));
211} 741}
212 742
213/* 743/*
214 * Allocate more memory to the indicated bucket. 744 * Allocate a fragment
215 */ 745 */
216static void 746static void *
217morecore(bucket) 747malloc_bytes(size)
218 int bucket; 748 size_t size;
219{ 749{
220 register union overhead *op; 750 int i,j;
221 register long sz; /* size of desired block */ 751 u_long u;
222 long amt; /* amount to allocate */ 752 struct pginfo *bp;
223 int nblks; /* how many blocks we get */ 753 int k;
754 u_long *lp;
224 755
225 /* 756 /* Don't bother with anything less than this */
226 * sbrk_size <= 0 only for big, FLUFFY, requests (about 757 if (size < malloc_minsize)
227 * 2^30 bytes on a VAX, I think) or for a negative arg. 758 size = malloc_minsize;
228 */ 759
229 sz = 1 << (bucket + 3); 760 /* Find the right bucket */
230#ifdef DEBUG 761 j = 1;
231 ASSERT(sz > 0); 762 i = size-1;
232#else 763 while (i >>= 1)
233 if (sz <= 0) 764 j++;
234 return; 765
235#endif 766 /* If it's empty, make a page more of that size chunks */
236 if (sz < pagesz) { 767 if (!page_dir[j] && !malloc_make_chunks(j))
237 amt = pagesz; 768 return 0;
238 nblks = amt / sz; 769
239 } else { 770 bp = page_dir[j];
240 amt = sz + pagesz; 771
241 nblks = 1; 772 /* Find first word of bitmap which isn't empty */
242 } 773 for (lp = bp->bits; !*lp; lp++)
243 op = (union overhead *)sbrk(amt); 774 ;
244 /* no more room! */ 775
245 if ((long)op == -1) 776 /* Find that bit, and tweak it */
246 return; 777 u = 1;
247 /* 778 k = 0;
248 * Add new memory allocated to that on 779 while (!(*lp & u)) {
249 * free list for this hash bucket. 780 u += u;
250 */ 781 k++;
251 nextf[bucket] = op; 782 }
252 while (--nblks > 0) { 783 *lp ^= u;
253 op->ov_next = (union overhead *)((caddr_t)op + sz); 784
254 op = (union overhead *)((caddr_t)op + sz); 785 /* If there are no more free, remove from free-list */
255 } 786 if (!--bp->free) {
787 page_dir[j] = bp->next;
788 bp->next = 0;
789 }
790
791 /* Adjust to the real offset of that chunk */
792 k += (lp-bp->bits)*MALLOC_BITS;
793 k <<= bp->shift;
794
795 if (malloc_junk)
796 memset((char *)bp->page + k, SOME_JUNK, bp->size);
797
798 return (u_char *)bp->page + k;
256} 799}
257 800
258void 801/*
259free(cp) 802 * Allocate a piece of memory
260 void *cp; 803 */
261{ 804static void *
262 register long size; 805imalloc(size)
263 register union overhead *op; 806 size_t size;
264 807{
265 if (cp == NULL) 808 void *result;
266 return; 809
267 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 810 if (!malloc_started)
268#ifdef DEBUG 811 malloc_init();
269 ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ 812
270#else 813 if (suicide)
271 if (op->ov_magic != MAGIC) 814 abort();
272 return; /* sanity */ 815
273#endif 816 if ((size + malloc_pagesize) < size) /* Check for overflow */
274#ifdef RCHECK 817 result = 0;
275 ASSERT(op->ov_rmagic == RMAGIC); 818 else if (size <= malloc_maxsize)
276 ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); 819 result = malloc_bytes(size);
277#endif 820 else
278 size = op->ov_index; 821 result = malloc_pages(size);
279 ASSERT(size < NBUCKETS); 822
280 op->ov_next = nextf[size]; /* also clobbers ov_magic */ 823 if (malloc_abort && !result)
281 nextf[size] = op; 824 wrterror("allocation failed.\n");
282#ifdef MSTATS 825
283 nmalloc[size]--; 826 if (malloc_zero && result)
284#endif 827 memset(result, 0, size);
828
829 return result;
285} 830}
286 831
287/* 832/*
288 * When a program attempts "storage compaction" as mentioned in the 833 * Change the size of an allocation.
289 * old malloc man page, it realloc's an already freed block. Usually
290 * this is the last block it freed; occasionally it might be farther
291 * back. We have to search all the free lists for the block in order
292 * to determine its bucket: 1st we make one pass thru the lists
293 * checking only the first block in each; if that fails we search
294 * ``realloc_srchlen'' blocks in each list for a match (the variable
295 * is extern so the caller can modify it). If that fails we just copy
296 * however many bytes was given to realloc() and hope it's not huge.
297 */ 834 */
298int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ 835static void *
836irealloc(ptr, size)
837 void *ptr;
838 size_t size;
839{
840 void *p;
841 u_long osize, index;
842 struct pginfo **mp;
843 int i;
299 844
300void * 845 if (suicide)
301realloc(cp, nbytes) 846 abort();
302 void *cp; 847
303 size_t nbytes; 848 if (!malloc_started) {
304{ 849 wrtwarning("malloc() has never been called.\n");
305 register u_long onb; 850 return 0;
306 register long i; 851 }
307 union overhead *op; 852
308 char *res; 853 index = ptr2index(ptr);
309 int was_alloced = 0; 854
310 855 if (index < malloc_pageshift) {
311 if (cp == NULL) 856 wrtwarning("junk pointer, too low to make sense.\n");
312 return (malloc(nbytes)); 857 return 0;
313 op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); 858 }
314 if (op->ov_magic == MAGIC) { 859
315 was_alloced++; 860 if (index > last_index) {
316 i = op->ov_index; 861 wrtwarning("junk pointer, too high to make sense.\n");
317 } else { 862 return 0;
318 /* 863 }
319 * Already free, doing "compaction". 864
320 * 865 mp = &page_dir[index];
321 * Search for the old block of memory on the 866
322 * free list. First, check the most common 867 if (*mp == MALLOC_FIRST) { /* Page allocation */
323 * case (last element free'd), then (this failing) 868
324 * the last ``realloc_srchlen'' items free'd. 869 /* Check the pointer */
325 * If all lookups fail, then assume the size of 870 if ((u_long)ptr & malloc_pagemask) {
326 * the memory block being realloc'd is the 871 wrtwarning("modified (page-) pointer.\n");
327 * largest possible (so that all "nbytes" of new 872 return 0;
328 * memory are copied into). Note that this could cause 873 }
329 * a memory fault if the old area was tiny, and the moon 874
330 * is gibbous. However, that is very unlikely. 875 /* Find the size in bytes */
331 */ 876 for (osize = malloc_pagesize; *++mp == MALLOC_FOLLOW;)
332 if ((i = findbucket(op, 1)) < 0 && 877 osize += malloc_pagesize;
333 (i = findbucket(op, realloc_srchlen)) < 0) 878
334 i = NBUCKETS; 879 if (!malloc_realloc && /* unless we have to, */
880 size <= osize && /* .. or are too small, */
881 size > (osize - malloc_pagesize)) { /* .. or can free a page, */
882 return ptr; /* don't do anything. */
883 }
884
885 } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */
886
887 /* Check the pointer for sane values */
888 if (((u_long)ptr & ((*mp)->size-1))) {
889 wrtwarning("modified (chunk-) pointer.\n");
890 return 0;
335 } 891 }
336 onb = 1 << (i + 3); 892
337 if (onb < pagesz) 893 /* Find the chunk index in the page */
338 onb -= sizeof (*op) + RSLOP; 894 i = ((u_long)ptr & malloc_pagemask) >> (*mp)->shift;
895
896 /* Verify that it isn't a free chunk already */
897 if ((*mp)->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) {
898 wrtwarning("chunk is already free.\n");
899 return 0;
900 }
901
902 osize = (*mp)->size;
903
904 if (!malloc_realloc && /* Unless we have to, */
905 size < osize && /* ..or are too small, */
906 (size > osize/2 || /* ..or could use a smaller size, */
907 osize == malloc_minsize)) { /* ..(if there is one) */
908 return ptr; /* ..Don't do anything */
909 }
910
911 } else {
912 wrtwarning("pointer to wrong page.\n");
913 return 0;
914 }
915
916 p = imalloc(size);
917
918 if (p) {
919 /* copy the lesser of the two sizes, and free the old one */
920 if (osize < size)
921 memcpy(p, ptr, osize);
339 else 922 else
340 onb += pagesz - sizeof (*op) - RSLOP; 923 memcpy(p, ptr, size);
341 /* avoid the copy if same size block */ 924 ifree(ptr);
342 if (was_alloced) { 925 }
343 if (i) { 926 return p;
344 i = 1 << (i + 2); 927}
345 if (i < pagesz) 928
346 i -= sizeof (*op) + RSLOP; 929/*
347 else 930 * Free a sequence of pages
348 i += pagesz - sizeof (*op) - RSLOP; 931 */
349 } 932
350 if (nbytes <= onb && nbytes > i) { 933static __inline__ void
351#ifdef RCHECK 934free_pages(ptr, index, info)
352 op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); 935 void *ptr;
353 *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; 936 int index;
937 struct pginfo *info;
938{
939 int i;
940 struct pgfree *pf, *pt=0;
941 u_long l;
942 void *tail;
943
944 if (info == MALLOC_FREE) {
945 wrtwarning("page is already free.\n");
946 return;
947 }
948
949 if (info != MALLOC_FIRST) {
950 wrtwarning("pointer to wrong page.\n");
951 return;
952 }
953
954 if ((u_long)ptr & malloc_pagemask) {
955 wrtwarning("modified (page-) pointer.\n");
956 return;
957 }
958
959 /* Count how many pages and mark them free at the same time */
960 page_dir[index] = MALLOC_FREE;
961 for (i = 1; page_dir[index+i] == MALLOC_FOLLOW; i++)
962 page_dir[index + i] = MALLOC_FREE;
963
964 l = i << malloc_pageshift;
965
966 if (malloc_junk)
967 memset(ptr, SOME_JUNK, l);
968
969#ifdef __FreeBSD__
970 if (malloc_hint)
971 madvise(ptr, l, MADV_FREE);
354#endif 972#endif
355 return(cp); 973
356 } else 974 tail = (char *)ptr+l;
357 free(cp); 975
976 /* add to free-list */
977 if (!px)
978 px = imalloc(sizeof *px); /* This cannot fail... */
979 px->page = ptr;
980 px->end = tail;
981 px->size = l;
982 if (!free_list.next) {
983
984 /* Nothing on free list, put this at head */
985 px->next = free_list.next;
986 px->prev = &free_list;
987 free_list.next = px;
988 pf = px;
989 px = 0;
990
991 } else {
992
993 /* Find the right spot, leave pf pointing to the modified entry. */
994 tail = (char *)ptr+l;
995
996 for(pf = free_list.next; pf->end < ptr && pf->next; pf = pf->next)
997 ; /* Race ahead here */
998
999 if (pf->page > tail) {
1000 /* Insert before entry */
1001 px->next = pf;
1002 px->prev = pf->prev;
1003 pf->prev = px;
1004 px->prev->next = px;
1005 pf = px;
1006 px = 0;
1007 } else if (pf->end == ptr ) {
1008 /* Append to the previous entry */
1009 pf->end = (char *)pf->end + l;
1010 pf->size += l;
1011 if (pf->next && pf->end == pf->next->page ) {
1012 /* And collapse the next too. */
1013 pt = pf->next;
1014 pf->end = pt->end;
1015 pf->size += pt->size;
1016 pf->next = pt->next;
1017 if (pf->next)
1018 pf->next->prev = pf;
1019 }
1020 } else if (pf->page == tail) {
1021 /* Prepend to entry */
1022 pf->size += l;
1023 pf->page = ptr;
1024 } else if (!pf->next) {
1025 /* Append at tail of chain */
1026 px->next = 0;
1027 px->prev = pf;
1028 pf->next = px;
1029 pf = px;
1030 px = 0;
1031 } else {
1032 wrterror("freelist is destroyed.\n");
358 } 1033 }
359 if ((res = malloc(nbytes)) == NULL) 1034 }
360 return (NULL); 1035
361 if (cp != res) /* common optimization if "compacting" */ 1036 /* Return something to OS ? */
362 bcopy(cp, res, (nbytes < onb) ? nbytes : onb); 1037 if (!pf->next && /* If we're the last one, */
363 return (res); 1038 pf->size > malloc_cache && /* ..and the cache is full, */
1039 pf->end == malloc_brk && /* ..and none behind us, */
1040 malloc_brk == sbrk(0)) { /* ..and it's OK to do... */
1041
1042 /*
1043 * Keep the cache intact. Notice that the '>' above guarantees that
1044 * the pf will always have at least one page afterwards.
1045 */
1046 pf->end = (char *)pf->page + malloc_cache;
1047 pf->size = malloc_cache;
1048
1049 brk(pf->end);
1050 malloc_brk = pf->end;
1051
1052 index = ptr2index(pf->end);
1053 last_index = index - 1;
1054
1055 for(i=index;i <= last_index;)
1056 page_dir[i++] = MALLOC_NOT_MINE;
1057
1058 /* XXX: We could realloc/shrink the pagedir here I guess. */
1059 }
1060 if (pt)
1061 ifree(pt);
364} 1062}
365 1063
366/* 1064/*
367 * Search ``srchlen'' elements of each free list for a block whose 1065 * Free a chunk, and possibly the page it's on, if the page becomes empty.
368 * header starts at ``freep''. If srchlen is -1 search the whole list.
369 * Return bucket number, or -1 if not found.
370 */ 1066 */
371static 1067
372findbucket(freep, srchlen) 1068/* ARGSUSED */
373 union overhead *freep; 1069static __inline__ void
374 int srchlen; 1070free_bytes(ptr, index, info)
1071 void *ptr;
1072 int index;
1073 struct pginfo *info;
375{ 1074{
376 register union overhead *p; 1075 int i;
377 register int i, j; 1076 struct pginfo **mp;
378 1077 void *vp;
379 for (i = 0; i < NBUCKETS; i++) { 1078
380 j = 0; 1079 /* Find the chunk number on the page */
381 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { 1080 i = ((u_long)ptr & malloc_pagemask) >> info->shift;
382 if (p == freep) 1081
383 return (i); 1082 if (((u_long)ptr & (info->size-1))) {
384 j++; 1083 wrtwarning("modified (chunk-) pointer.\n");
385 } 1084 return;
386 } 1085 }
387 return (-1); 1086
1087 if (info->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) {
1088 wrtwarning("chunk is already free.\n");
1089 return;
1090 }
1091
1092 if (malloc_junk)
1093 memset(ptr, SOME_JUNK, info->size);
1094
1095 info->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS);
1096 info->free++;
1097
1098 mp = page_dir + info->shift;
1099
1100 if (info->free == 1) {
1101
1102 /* Page became non-full */
1103
1104 mp = page_dir + info->shift;
1105 /* Insert in address order */
1106 while (*mp && (*mp)->next && (*mp)->next->page < info->page)
1107 mp = &(*mp)->next;
1108 info->next = *mp;
1109 *mp = info;
1110 return;
1111 }
1112
1113 if (info->free != info->total)
1114 return;
1115
1116 /* Find & remove this page in the queue */
1117 while (*mp != info) {
1118 mp = &((*mp)->next);
1119#ifdef MALLOC_EXTRA_SANITY
1120 if (!*mp)
1121 wrterror("(ES): Not on queue\n");
1122#endif /* MALLOC_EXTRA_SANITY */
1123 }
1124 *mp = info->next;
1125
1126 /* Free the page & the info structure if need be */
1127 page_dir[ptr2index(info->page)] = MALLOC_FIRST;
1128 vp = info->page; /* Order is important ! */
1129 if(vp != (void*)info)
1130 ifree(info);
1131 ifree(vp);
1132}
1133
1134static void
1135ifree(ptr)
1136 void *ptr;
1137{
1138 struct pginfo *info;
1139 int index;
1140
1141 /* This is legal */
1142 if (!ptr)
1143 return;
1144
1145 if (!malloc_started) {
1146 wrtwarning("malloc() has never been called.\n");
1147 return;
1148 }
1149
1150 /* If we're already sinking, don't make matters any worse. */
1151 if (suicide)
1152 return;
1153
1154 index = ptr2index(ptr);
1155
1156 if (index < malloc_pageshift) {
1157 wrtwarning("junk pointer, too low to make sense.\n");
1158 return;
1159 }
1160
1161 if (index > last_index) {
1162 wrtwarning("junk pointer, too high to make sense.\n");
1163 return;
1164 }
1165
1166 info = page_dir[index];
1167
1168 if (info < MALLOC_MAGIC)
1169 free_pages(ptr, index, info);
1170 else
1171 free_bytes(ptr, index, info);
1172 return;
388} 1173}
389 1174
390#ifdef MSTATS
391/* 1175/*
392 * mstats - print out statistics about malloc 1176 * These are the public exported interface routines.
393 *
394 * Prints two lines of numbers, one showing the length of the free list
395 * for each size category, the second showing the number of mallocs -
396 * frees for each size category.
397 */ 1177 */
398mstats(s) 1178
399 char *s; 1179static int malloc_active;
1180
1181void *
1182malloc(size_t size)
1183{
1184 register void *r;
1185
1186 malloc_func = " in malloc():";
1187 THREAD_LOCK();
1188 if (malloc_active++) {
1189 wrtwarning("recursive call.\n");
1190 malloc_active--;
1191 return (0);
1192 }
1193 r = imalloc(size);
1194 UTRACE(0, size, r);
1195 malloc_active--;
1196 THREAD_UNLOCK();
1197 if (malloc_xmalloc && !r)
1198 wrterror("out of memory.\n");
1199 return (r);
1200}
1201
1202void
1203free(void *ptr)
400{ 1204{
401 register int i, j; 1205 malloc_func = " in free():";
402 register union overhead *p; 1206 THREAD_LOCK();
403 int totfree = 0, 1207 if (malloc_active++) {
404 totused = 0; 1208 wrtwarning("recursive call.\n");
405 1209 malloc_active--;
406 fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); 1210 return;
407 for (i = 0; i < NBUCKETS; i++) { 1211 }
408 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) 1212 ifree(ptr);
409 ; 1213 UTRACE(ptr, 0, 0);
410 fprintf(stderr, " %d", j); 1214 malloc_active--;
411 totfree += j * (1 << (i + 3)); 1215 THREAD_UNLOCK();
412 } 1216 return;
413 fprintf(stderr, "\nused:\t"); 1217}
414 for (i = 0; i < NBUCKETS; i++) { 1218
415 fprintf(stderr, " %d", nmalloc[i]); 1219void *
416 totused += nmalloc[i] * (1 << (i + 3)); 1220realloc(void *ptr, size_t size)
417 } 1221{
418 fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", 1222 register void *r;
419 totused, totfree); 1223
1224 malloc_func = " in realloc():";
1225 THREAD_LOCK();
1226 if (malloc_active++) {
1227 wrtwarning("recursive call.\n");
1228 malloc_active--;
1229 return (0);
1230 }
1231 if (!ptr) {
1232 r = imalloc(size);
1233 } else {
1234 r = irealloc(ptr, size);
1235 }
1236 UTRACE(ptr, size, r);
1237 malloc_active--;
1238 THREAD_UNLOCK();
1239 if (malloc_xmalloc && !r)
1240 wrterror("out of memory.\n");
1241 return (r);
420} 1242}
421#endif
diff --git a/src/lib/libc/stdlib/memory.3 b/src/lib/libc/stdlib/memory.3
index 735252c837..712c7c45d2 100644
--- a/src/lib/libc/stdlib/memory.3
+++ b/src/lib/libc/stdlib/memory.3
@@ -29,8 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)memory.3 5.1 (Berkeley) 5/2/91 32.\" $OpenBSD: memory.3,v 1.2 1996/08/19 08:33:37 tholo Exp $
33.\" $Id: memory.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
34.\" 33.\"
35.Dd May 2, 1991 34.Dd May 2, 1991
36.Dt MEMORY 3 35.Dt MEMORY 3
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c
index 381fdc0830..0a1015ad9d 100644
--- a/src/lib/libc/stdlib/merge.c
+++ b/src/lib/libc/stdlib/merge.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char sccsid[] = "from: @(#)merge.c 8.2 (Berkeley) 2/14/94";*/ 38static char *rcsid = "$OpenBSD: merge.c,v 1.3 1996/09/15 09:31:50 tholo Exp $";
39static char *rcsid = "$Id: merge.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42/* 41/*
@@ -148,7 +147,7 @@ mergesort(base, nmemb, size, cmp)
148 sense = 0; 147 sense = 0;
149 } 148 }
150 if (!big) { /* here i = 0 */ 149 if (!big) { /* here i = 0 */
151LINEAR: while ((b += size) < t && cmp(q, b) >sense) 150 while ((b += size) < t && cmp(q, b) >sense)
152 if (++i == 6) { 151 if (++i == 6) {
153 big = 1; 152 big = 1;
154 goto EXPONENTIAL; 153 goto EXPONENTIAL;
@@ -169,7 +168,7 @@ EXPONENTIAL: for (i = size; ; i <<= 1)
169 goto FASTCASE; 168 goto FASTCASE;
170 } else 169 } else
171 b = p; 170 b = p;
172SLOWCASE: while (t > b+size) { 171 while (t > b+size) {
173 i = (((t - b) / size) >> 1) * size; 172 i = (((t - b) / size) >> 1) * size;
174 if ((*cmp)(q, p = b + i) <= sense) 173 if ((*cmp)(q, p = b + i) <= sense)
175 t = p; 174 t = p;
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c
index 43356e66b3..cd34260b5c 100644
--- a/src/lib/libc/stdlib/mrand48.c
+++ b/src/lib/libc/stdlib/mrand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: mrand48.c,v 1.2 1996/08/19 08:33:39 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/multibyte.c b/src/lib/libc/stdlib/multibyte.c
index fe1cd5781b..12e70c4a2c 100644
--- a/src/lib/libc/stdlib/multibyte.c
+++ b/src/lib/libc/stdlib/multibyte.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";*/ 35static char *rcsid = "$OpenBSD: multibyte.c,v 1.2 1996/08/19 08:33:39 tholo Exp $";
36static char *rcsid = "$Id: multibyte.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c
index 63f839cb05..b1ec2cebb1 100644
--- a/src/lib/libc/stdlib/nrand48.c
+++ b/src/lib/libc/stdlib/nrand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: nrand48.c,v 1.2 1996/08/19 08:33:40 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16long 20long
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c
index 2194c2c608..d8c4886d4b 100644
--- a/src/lib/libc/stdlib/putenv.c
+++ b/src/lib/libc/stdlib/putenv.c
@@ -1,6 +1,6 @@
1/*- 1/*-
2 * Copyright (c) 1988 The Regents of the University of California. 2 * Copyright (c) 1988, 1993
3 * All rights reserved. 3 * The Regents of the University of California. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)putenv.c 5.4 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: putenv.c,v 1.2 1996/08/10 05:03:00 tholo Exp $";
36static char *rcsid = "$Id: putenv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
@@ -43,17 +42,17 @@ int
43putenv(str) 42putenv(str)
44 const char *str; 43 const char *str;
45{ 44{
46 register char *p, *equal; 45 char *p, *equal;
47 int rval; 46 int rval;
48 47
49 if (!(p = strdup(str))) 48 if ((p = strdup(str)) == NULL)
50 return(1); 49 return (-1);
51 if (!(equal = strchr(p, '='))) { 50 if ((equal = strchr(p, '=')) == NULL) {
52 (void)free(p); 51 (void)free(p);
53 return(1); 52 return (-1);
54 } 53 }
55 *equal = '\0'; 54 *equal = '\0';
56 rval = setenv(p, equal + 1, 1); 55 rval = setenv(p, equal + 1, 1);
57 (void)free(p); 56 (void)free(p);
58 return(rval); 57 return (rval);
59} 58}
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3
index cb1e052191..92a8bdc4b9 100644
--- a/src/lib/libc/stdlib/qabs.3
+++ b/src/lib/libc/stdlib/qabs.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: qabs.3,v 1.2 1996/08/19 08:33:40 tholo Exp $
37.\" $Id: qabs.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt QABS 3 39.Dt QABS 3
diff --git a/src/lib/libc/stdlib/qabs.c b/src/lib/libc/stdlib/qabs.c
index 9c51a8baa9..ccc42cbec6 100644
--- a/src/lib/libc/stdlib/qabs.c
+++ b/src/lib/libc/stdlib/qabs.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ 35static char *rcsid = "$OpenBSD: qabs.c,v 1.2 1996/08/19 08:33:40 tholo Exp $";
36static char *rcsid = "$Id: qabs.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3
index 0efcfc96ef..12aca0b1ea 100644
--- a/src/lib/libc/stdlib/qdiv.3
+++ b/src/lib/libc/stdlib/qdiv.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)qdiv.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: qdiv.3,v 1.2 1996/08/19 08:33:41 tholo Exp $
37.\" $Id: qdiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt QDIV 3 39.Dt QDIV 3
diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c
index 8f8e3f89c4..07e84cd649 100644
--- a/src/lib/libc/stdlib/qdiv.c
+++ b/src/lib/libc/stdlib/qdiv.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ 38static char *rcsid = "$OpenBSD: qdiv.c,v 1.2 1996/08/19 08:33:41 tholo Exp $";
39static char *rcsid = "$Id: qdiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <stdlib.h> /* qdiv_t */ 41#include <stdlib.h> /* qdiv_t */
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3
index eb122cde12..a65c5819d0 100644
--- a/src/lib/libc/stdlib/qsort.3
+++ b/src/lib/libc/stdlib/qsort.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 36.\" $OpenBSD: qsort.3,v 1.2 1996/08/19 08:33:42 tholo Exp $
37.\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
38.\" 37.\"
39.Dd June 4, 1993 38.Dd June 4, 1993
40.Dt QSORT 3 39.Dt QSORT 3
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c
index c06bd54054..1c3020b595 100644
--- a/src/lib/libc/stdlib/qsort.c
+++ b/src/lib/libc/stdlib/qsort.c
@@ -32,15 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char sccsid[] = "from: @(#)qsort.c 8.1 (Berkeley) 6/4/93";*/ 35static char *rcsid = "$OpenBSD: qsort.c,v 1.5 1997/06/20 11:19:38 deraadt Exp $";
36static char *rcsid = "$Id: qsort.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <sys/types.h> 38#include <sys/types.h>
40#include <stdlib.h> 39#include <stdlib.h>
41 40
42static inline char *med3 __P((char *, char *, char *, int (*)())); 41static __inline char *med3 __P((char *, char *, char *, int (*)()));
43static inline void swapfunc __P((char *, char *, int, int)); 42static __inline void swapfunc __P((char *, char *, int, int));
44 43
45#define min(a, b) (a) < (b) ? a : b 44#define min(a, b) (a) < (b) ? a : b
46 45
@@ -61,12 +60,12 @@ static inline void swapfunc __P((char *, char *, int, int));
61#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ 60#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
62 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; 61 es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
63 62
64static inline void 63static __inline void
65swapfunc(a, b, n, swaptype) 64swapfunc(a, b, n, swaptype)
66 char *a, *b; 65 char *a, *b;
67 int n, swaptype; 66 int n, swaptype;
68{ 67{
69 if(swaptype <= 1) 68 if (swaptype <= 1)
70 swapcode(long, a, b, n) 69 swapcode(long, a, b, n)
71 else 70 else
72 swapcode(char, a, b, n) 71 swapcode(char, a, b, n)
@@ -82,7 +81,7 @@ swapfunc(a, b, n, swaptype)
82 81
83#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) 82#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
84 83
85static inline char * 84static __inline char *
86med3(a, b, c, cmp) 85med3(a, b, c, cmp)
87 char *a, *b, *c; 86 char *a, *b, *c;
88 int (*cmp)(); 87 int (*cmp)();
@@ -93,27 +92,28 @@ med3(a, b, c, cmp)
93} 92}
94 93
95void 94void
96qsort(a, n, es, cmp) 95qsort(aa, n, es, cmp)
97 void *a; 96 void *aa;
98 size_t n, es; 97 size_t n, es;
99 int (*cmp)(); 98 int (*cmp)();
100{ 99{
101 char *pa, *pb, *pc, *pd, *pl, *pm, *pn; 100 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
102 int d, r, swaptype, swap_cnt; 101 int d, r, swaptype, swap_cnt;
102 register char *a = aa;
103 103
104loop: SWAPINIT(a, es); 104loop: SWAPINIT(a, es);
105 swap_cnt = 0; 105 swap_cnt = 0;
106 if (n < 7) { 106 if (n < 7) {
107 for (pm = a + es; pm < (char *) a + n * es; pm += es) 107 for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
108 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 108 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
109 pl -= es) 109 pl -= es)
110 swap(pl, pl - es); 110 swap(pl, pl - es);
111 return; 111 return;
112 } 112 }
113 pm = a + (n / 2) * es; 113 pm = (char *)a + (n / 2) * es;
114 if (n > 7) { 114 if (n > 7) {
115 pl = a; 115 pl = (char *)a;
116 pn = a + (n - 1) * es; 116 pn = (char *)a + (n - 1) * es;
117 if (n > 40) { 117 if (n > 40) {
118 d = (n / 8) * es; 118 d = (n / 8) * es;
119 pl = med3(pl, pl + d, pl + 2 * d, cmp); 119 pl = med3(pl, pl + d, pl + 2 * d, cmp);
@@ -123,9 +123,9 @@ loop: SWAPINIT(a, es);
123 pm = med3(pl, pm, pn, cmp); 123 pm = med3(pl, pm, pn, cmp);
124 } 124 }
125 swap(a, pm); 125 swap(a, pm);
126 pa = pb = a + es; 126 pa = pb = (char *)a + es;
127 127
128 pc = pd = a + (n - 1) * es; 128 pc = pd = (char *)a + (n - 1) * es;
129 for (;;) { 129 for (;;) {
130 while (pb <= pc && (r = cmp(pb, a)) <= 0) { 130 while (pb <= pc && (r = cmp(pb, a)) <= 0) {
131 if (r == 0) { 131 if (r == 0) {
@@ -151,14 +151,14 @@ loop: SWAPINIT(a, es);
151 pc -= es; 151 pc -= es;
152 } 152 }
153 if (swap_cnt == 0) { /* Switch to insertion sort */ 153 if (swap_cnt == 0) { /* Switch to insertion sort */
154 for (pm = a + es; pm < (char *) a + n * es; pm += es) 154 for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
155 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; 155 for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
156 pl -= es) 156 pl -= es)
157 swap(pl, pl - es); 157 swap(pl, pl - es);
158 return; 158 return;
159 } 159 }
160 160
161 pn = a + n * es; 161 pn = (char *)a + n * es;
162 r = min(pa - (char *)a, pb - pa); 162 r = min(pa - (char *)a, pb - pa);
163 vecswap(a, pb - r, r); 163 vecswap(a, pb - r, r);
164 r = min(pd - pc, pn - pd - es); 164 r = min(pd - pc, pn - pd - es);
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3
index a2af9f17a4..8b0ea89a0f 100644
--- a/src/lib/libc/stdlib/radixsort.3
+++ b/src/lib/libc/stdlib/radixsort.3
@@ -29,8 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94 32.\" $OpenBSD: radixsort.3,v 1.4 1998/06/15 17:55:08 mickey Exp $
33.\" $Id: radixsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $
34.\" 33.\"
35.Dd January 27, 1994 34.Dd January 27, 1994
36.Dt RADIXSORT 3 35.Dt RADIXSORT 3
@@ -42,9 +41,9 @@
42.Fd #include <limits.h> 41.Fd #include <limits.h>
43.Fd #include <stdlib.h> 42.Fd #include <stdlib.h>
44.Ft int 43.Ft int
45.Fn radixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 44.Fn radixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte"
46.Ft int 45.Ft int
47.Fn sradixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" 46.Fn sradixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte"
48.Sh DESCRIPTION 47.Sh DESCRIPTION
49The 48The
50.Fn radixsort 49.Fn radixsort
@@ -158,4 +157,5 @@ for any of the errors specified for the library routine
158.Sh HISTORY 157.Sh HISTORY
159The 158The
160.Fn radixsort 159.Fn radixsort
161function first appeared in 4.4BSD. 160function first appeared in
161.Bx 4.4 .
diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c
index dd51013c94..41ed962466 100644
--- a/src/lib/libc/stdlib/radixsort.c
+++ b/src/lib/libc/stdlib/radixsort.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char sccsid[] = "from: @(#)radixsort.c 8.1 (Berkeley) 6/4/93";*/ 38static char *rcsid = "$OpenBSD: radixsort.c,v 1.3 1996/08/19 08:33:44 tholo Exp $";
39static char *rcsid = "$Id: radixsort.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42/* 41/*
@@ -61,7 +60,7 @@ typedef struct {
61 int sn, si; 60 int sn, si;
62} stack; 61} stack;
63 62
64static inline void simplesort 63static __inline void simplesort
65 __P((const u_char **, int, int, const u_char *, u_int)); 64 __P((const u_char **, int, int, const u_char *, u_int));
66static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int)); 65static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int));
67static void r_sort_b __P((const u_char **, 66static void r_sort_b __P((const u_char **,
@@ -295,7 +294,7 @@ r_sort_b(a, ta, n, i, tr, endch)
295 } 294 }
296} 295}
297 296
298static inline void 297static __inline void
299simplesort(a, n, b, tr, endch) /* insertion sort */ 298simplesort(a, n, b, tr, endch) /* insertion sort */
300 register const u_char **a; 299 register const u_char **a;
301 int n, b; 300 int n, b;
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3
index a0e7740e66..32d32761f1 100644
--- a/src/lib/libc/stdlib/rand.3
+++ b/src/lib/libc/stdlib/rand.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)rand.3 6.7 (Berkeley) 6/29/91 36.\" $OpenBSD: rand.3,v 1.4 1998/07/05 19:54:22 millert Exp $
37.\" $Id: rand.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt RAND 3 39.Dt RAND 3
@@ -51,7 +50,8 @@
51.Fn rand void 50.Fn rand void
52.Sh DESCRIPTION 51.Sh DESCRIPTION
53.Bf -symbolic 52.Bf -symbolic
54These interfaces are obsoleted by random(3). 53These interfaces are obsoleted by
54.Xr random 3 .
55.Ef 55.Ef
56.Pp 56.Pp
57The 57The
@@ -74,6 +74,8 @@ with the same seed value.
74If no seed value is provided, the functions are automatically 74If no seed value is provided, the functions are automatically
75seeded with a value of 1. 75seeded with a value of 1.
76.Sh SEE ALSO 76.Sh SEE ALSO
77.Xr arc4random 3 ,
78.Xr rand48 3 ,
77.Xr random 3 79.Xr random 3
78.Sh STANDARDS 80.Sh STANDARDS
79The 81The
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c
index 361d473448..f270ffd986 100644
--- a/src/lib/libc/stdlib/rand.c
+++ b/src/lib/libc/stdlib/rand.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)rand.c 5.6 (Berkeley) 6/24/91";*/ 35static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $";
36static char *rcsid = "$Id: rand.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <sys/types.h> 38#include <sys/types.h>
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3
index 5a772c9a8c..d5236a4779 100644
--- a/src/lib/libc/stdlib/rand48.3
+++ b/src/lib/libc/stdlib/rand48.3
@@ -9,7 +9,7 @@
9.\" of any kind. I shall in no event be liable for anything that happens 9.\" of any kind. I shall in no event be liable for anything that happens
10.\" to anyone/anything when using this software. 10.\" to anyone/anything when using this software.
11.\" 11.\"
12.\" $Id: rand48.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ 12.\" $OpenBSD: rand48.3,v 1.3 1998/07/05 19:54:23 millert Exp $
13.\" 13.\"
14.Dd October 8, 1993 14.Dd October 8, 1993
15.Dt RAND48 3 15.Dt RAND48 3
@@ -156,5 +156,6 @@ For a more powerful random number generator, see
156.Sh AUTHOR 156.Sh AUTHOR
157Martin Birgmeier 157Martin Birgmeier
158.Sh SEE ALSO 158.Sh SEE ALSO
159.Xr arc4random 3 ,
159.Xr rand 3 , 160.Xr rand 3 ,
160.Xr random 3 . 161.Xr random 3 .
diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h
index 12496d1c8c..e7cb3e0333 100644
--- a/src/lib/libc/stdlib/rand48.h
+++ b/src/lib/libc/stdlib/rand48.h
@@ -9,6 +9,8 @@
9 * This software is provided ``as is'', and comes with no warranties 9 * This software is provided ``as is'', and comes with no warranties
10 * of any kind. I shall in no event be liable for anything that happens 10 * of any kind. I shall in no event be liable for anything that happens
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 *
13 * $OpenBSD: rand48.h,v 1.2 1996/08/19 08:33:45 tholo Exp $
12 */ 14 */
13 15
14#ifndef _RAND48_H_ 16#ifndef _RAND48_H_
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
index 38c15a9803..acfbd8cdeb 100644
--- a/src/lib/libc/stdlib/random.3
+++ b/src/lib/libc/stdlib/random.3
@@ -29,8 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)random.3 6.5 (Berkeley) 4/19/91 32.\" $OpenBSD: random.3,v 1.6 1998/07/05 19:54:25 millert Exp $
33.\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
34.\" 33.\"
35.Dd April 19, 1991 34.Dd April 19, 1991
36.Dt RANDOM 3 35.Dt RANDOM 3
@@ -46,11 +45,11 @@
46.Ft long 45.Ft long
47.Fn random void 46.Fn random void
48.Ft void 47.Ft void
49.Fn srandom "unsigned seed" 48.Fn srandom "unsigned int seed"
50.Ft char * 49.Ft char *
51.Fn initstate "unsigned seed" "char *state" "int n" 50.Fn initstate "unsigned int seed" "char *state" "size_t n"
52.Ft char * 51.Ft char *
53.Fn setstate "char *state" 52.Fn setstate "const char *state"
54.Sh DESCRIPTION 53.Sh DESCRIPTION
55The 54The
56.Fn random 55.Fn random
@@ -60,6 +59,12 @@ default table of size 31 long integers to return successive pseudo-random
60numbers in the range from 0 to 59numbers in the range from 0 to
61.if t 2\u\s731\s10\d\(mi1. 60.if t 2\u\s731\s10\d\(mi1.
62.if n (2**31)\(mi1. 61.if n (2**31)\(mi1.
62The maximum value returned by
63.Fn random
64is
65.Dv LONG_MAX
66(as defined by the header file
67.Aq Pa limits.h ) .
63The period of this random number generator is very large, approximately 68The period of this random number generator is very large, approximately
64.if t 16\(mu(2\u\s731\s10\d\(mi1). 69.if t 16\(mu(2\u\s731\s10\d\(mi1).
65.if n 16*((2**31)\(mi1). 70.if n 16*((2**31)\(mi1).
@@ -157,7 +162,18 @@ is called with less than 8 bytes of state information, or if
157detects that the state information has been garbled, error 162detects that the state information has been garbled, error
158messages are printed on the standard error output. 163messages are printed on the standard error output.
159.Sh SEE ALSO 164.Sh SEE ALSO
165.Xr arc4random 3 ,
166.Xr drand48 3 ,
160.Xr rand 3 167.Xr rand 3
168.Sh STANDARDS
169The
170.Fn random ,
171.Fn srandom ,
172.Fn initstate ,
173and
174.Fn setstate
175functions conform to
176.St -xpg4.2 .
161.Sh HISTORY 177.Sh HISTORY
162These 178These
163functions appeared in 179functions appeared in
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
index 469b6d976a..79344f30f1 100644
--- a/src/lib/libc/stdlib/random.c
+++ b/src/lib/libc/stdlib/random.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)random.c 5.9 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: random.c,v 1.6 1998/02/07 02:16:25 millert Exp $";
36static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdio.h> 38#include <stdio.h>
@@ -136,12 +135,12 @@ static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
136 135
137static long randtbl[DEG_3 + 1] = { 136static long randtbl[DEG_3 + 1] = {
138 TYPE_3, 137 TYPE_3,
139 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, 138 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
140 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 139 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
141 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 140 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
142 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, 141 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
143 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, 142 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
144 0x27fb47b9, 143 0xf3bec5da,
145}; 144};
146 145
147/* 146/*
@@ -193,15 +192,26 @@ void
193srandom(x) 192srandom(x)
194 u_int x; 193 u_int x;
195{ 194{
196 register int i, j; 195 register long int test;
196 register int i;
197 ldiv_t val;
197 198
198 if (rand_type == TYPE_0) 199 if (rand_type == TYPE_0)
199 state[0] = x; 200 state[0] = x;
200 else { 201 else {
201 j = 1;
202 state[0] = x; 202 state[0] = x;
203 for (i = 1; i < rand_deg; i++) 203 for (i = 1; i < rand_deg; i++) {
204 state[i] = 1103515245 * state[i - 1] + 12345; 204 /*
205 * Implement the following, without overflowing 31 bits:
206 *
207 * state[i] = (16807 * state[i - 1]) % 2147483647;
208 *
209 * 2^31-1 (prime) = 2147483647 = 127773*16807+2836
210 */
211 val = ldiv(state[i-1], 127773);
212 test = 16807 * val.rem - 2836 * val.quot;
213 state[i] = test + (test < 0 ? 2147483647 : 0);
214 }
205 fptr = &state[rand_sep]; 215 fptr = &state[rand_sep];
206 rptr = &state[0]; 216 rptr = &state[0];
207 for (i = 0; i < 10 * rand_deg; i++) 217 for (i = 0; i < 10 * rand_deg; i++)
@@ -232,7 +242,7 @@ char *
232initstate(seed, arg_state, n) 242initstate(seed, arg_state, n)
233 u_int seed; /* seed for R.N.G. */ 243 u_int seed; /* seed for R.N.G. */
234 char *arg_state; /* pointer to state array */ 244 char *arg_state; /* pointer to state array */
235 int n; /* # bytes of state info */ 245 size_t n; /* # bytes of state info */
236{ 246{
237 register char *ostate = (char *)(&state[-1]); 247 register char *ostate = (char *)(&state[-1]);
238 248
@@ -240,11 +250,8 @@ initstate(seed, arg_state, n)
240 state[-1] = rand_type; 250 state[-1] = rand_type;
241 else 251 else
242 state[-1] = MAX_TYPES * (rptr - state) + rand_type; 252 state[-1] = MAX_TYPES * (rptr - state) + rand_type;
243 if (n < BREAK_0) { 253 if (n < BREAK_0)
244 (void)fprintf(stderr, 254 return(NULL);
245 "random: not enough state (%d bytes); ignored.\n", n);
246 return(0);
247 }
248 if (n < BREAK_1) { 255 if (n < BREAK_1) {
249 rand_type = TYPE_0; 256 rand_type = TYPE_0;
250 rand_deg = DEG_0; 257 rand_deg = DEG_0;
@@ -293,7 +300,7 @@ initstate(seed, arg_state, n)
293 */ 300 */
294char * 301char *
295setstate(arg_state) 302setstate(arg_state)
296 char *arg_state; 303 const char *arg_state;
297{ 304{
298 register long *new_state = (long *)arg_state; 305 register long *new_state = (long *)arg_state;
299 register int type = new_state[0] % MAX_TYPES; 306 register int type = new_state[0] % MAX_TYPES;
@@ -315,8 +322,7 @@ setstate(arg_state)
315 rand_sep = seps[type]; 322 rand_sep = seps[type];
316 break; 323 break;
317 default: 324 default:
318 (void)fprintf(stderr, 325 return(NULL);
319 "random: state info corrupted; not changed.\n");
320 } 326 }
321 state = &new_state[1]; 327 state = &new_state[1];
322 if (rand_type != TYPE_0) { 328 if (rand_type != TYPE_0) {
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3
index 9d8b1ff2ce..cc140029e4 100644
--- a/src/lib/libc/stdlib/realpath.3
+++ b/src/lib/libc/stdlib/realpath.3
@@ -32,10 +32,9 @@
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE. 33.\" SUCH DAMAGE.
34.\" 34.\"
35.\" from: @(#)realpath.3 8.2 (Berkeley) 2/16/94 35.\" $OpenBSD: realpath.3,v 1.3 1997/05/30 07:48:30 deraadt Exp $
36.\" $Id: realpath.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
37.\" 36.\"
38.Dd "February 16, 1994" 37.Dd February, 16, 1994
39.Dt REALPATH 3 38.Dt REALPATH 3
40.Os 39.Os
41.Sh NAME 40.Sh NAME
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c
index e349b7e068..0288601464 100644
--- a/src/lib/libc/stdlib/realpath.c
+++ b/src/lib/libc/stdlib/realpath.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char sccsid[] = "from: @(#)realpath.c 8.1 (Berkeley) 2/16/94";*/ 38static char *rcsid = "$OpenBSD: realpath.c,v 1.4 1998/05/18 09:55:19 deraadt Exp $";
39static char *rcsid = "$Id: realpath.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <sys/param.h> 41#include <sys/param.h>
@@ -63,6 +62,7 @@ realpath(path, resolved)
63 struct stat sb; 62 struct stat sb;
64 int fd, n, rootd, serrno; 63 int fd, n, rootd, serrno;
65 char *p, *q, wbuf[MAXPATHLEN]; 64 char *p, *q, wbuf[MAXPATHLEN];
65 int symlinks = 0;
66 66
67 /* Save the starting point. */ 67 /* Save the starting point. */
68 if ((fd = open(".", O_RDONLY)) < 0) { 68 if ((fd = open(".", O_RDONLY)) < 0) {
@@ -101,7 +101,11 @@ loop:
101 /* Deal with the last component. */ 101 /* Deal with the last component. */
102 if (lstat(p, &sb) == 0) { 102 if (lstat(p, &sb) == 0) {
103 if (S_ISLNK(sb.st_mode)) { 103 if (S_ISLNK(sb.st_mode)) {
104 n = readlink(p, resolved, MAXPATHLEN); 104 if (++symlinks > MAXSYMLINKS) {
105 errno = ELOOP;
106 goto err1;
107 }
108 n = readlink(p, resolved, MAXPATHLEN-1);
105 if (n < 0) 109 if (n < 0)
106 goto err1; 110 goto err1;
107 resolved[n] = '\0'; 111 resolved[n] = '\0';
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c
index e3d31901dd..c4dcd0ead8 100644
--- a/src/lib/libc/stdlib/seed48.c
+++ b/src/lib/libc/stdlib/seed48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: seed48.c,v 1.2 1996/08/19 08:33:48 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c
index a36669888d..b6f261e61c 100644
--- a/src/lib/libc/stdlib/setenv.c
+++ b/src/lib/libc/stdlib/setenv.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)setenv.c 5.6 (Berkeley) 6/4/91";*/ 35static char *rcsid = "$OpenBSD: setenv.c,v 1.3 1998/02/02 22:44:53 millert Exp $";
36static char *rcsid = "$Id: setenv.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <stdlib.h> 38#include <stdlib.h>
@@ -72,10 +71,11 @@ setenv(name, value, rewrite)
72 71
73 for (P = environ, cnt = 0; *P; ++P, ++cnt); 72 for (P = environ, cnt = 0; *P; ++P, ++cnt);
74 if (alloced) { /* just increase size */ 73 if (alloced) { /* just increase size */
75 environ = (char **)realloc((char *)environ, 74 P = (char **)realloc((void *)environ,
76 (size_t)(sizeof(char *) * (cnt + 2))); 75 (size_t)(sizeof(char *) * (cnt + 2)));
77 if (!environ) 76 if (!P)
78 return (-1); 77 return (-1);
78 environ = P;
79 } 79 }
80 else { /* get new space */ 80 else { /* get new space */
81 alloced = 1; /* copy old entries into it */ 81 alloced = 1; /* copy old entries into it */
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c
index daf733f93e..fcff8a172e 100644
--- a/src/lib/libc/stdlib/srand48.c
+++ b/src/lib/libc/stdlib/srand48.c
@@ -11,6 +11,10 @@
11 * to anyone/anything when using this software. 11 * to anyone/anything when using this software.
12 */ 12 */
13 13
14#if defined(LIBC_SCCS) && !defined(lint)
15static char rcsid[] = "$OpenBSD: srand48.c,v 1.2 1996/08/19 08:33:49 tholo Exp $";
16#endif /* LIBC_SCCS and not lint */
17
14#include "rand48.h" 18#include "rand48.h"
15 19
16extern unsigned short __rand48_seed[3]; 20extern unsigned short __rand48_seed[3];
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3
index 0b7f973857..3476fa41d6 100644
--- a/src/lib/libc/stdlib/strtod.3
+++ b/src/lib/libc/stdlib/strtod.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strtod.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: strtod.3,v 1.2 1996/08/19 08:33:49 tholo Exp $
37.\" $Id: strtod.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRTOD 3 39.Dt STRTOD 3
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c
index b13fa128f5..55d9e91224 100644
--- a/src/lib/libc/stdlib/strtod.c
+++ b/src/lib/libc/stdlib/strtod.c
@@ -90,12 +90,13 @@
90 */ 90 */
91 91
92#if defined(LIBC_SCCS) && !defined(lint) 92#if defined(LIBC_SCCS) && !defined(lint)
93static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; 93static char *rcsid = "$OpenBSD: strtod.c,v 1.12 1998/08/28 20:49:24 mickey Exp $";
94#endif /* LIBC_SCCS and not lint */ 94#endif /* LIBC_SCCS and not lint */
95 95
96#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 96#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
97 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) 97 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
98#include <machine/endian.h> 98 defined(__powerpc__) || defined(__m88k__) || defined(__hppa__)
99#include <sys/types.h>
99#if BYTE_ORDER == BIG_ENDIAN 100#if BYTE_ORDER == BIG_ENDIAN
100#define IEEE_BIG_ENDIAN 101#define IEEE_BIG_ENDIAN
101#else 102#else
@@ -103,6 +104,15 @@ static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"
103#endif 104#endif
104#endif 105#endif
105 106
107#ifdef __arm32__
108/*
109 * Although the CPU is little endian the FP has different
110 * byte and word endianness. The byte order is still little endian
111 * but the word order is big endian.
112 */
113#define IEEE_BIG_ENDIAN
114#endif
115
106#ifdef vax 116#ifdef vax
107#define VAX 117#define VAX
108#endif 118#endif
@@ -212,19 +222,24 @@ Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
212IBM should be defined. 222IBM should be defined.
213#endif 223#endif
214 224
225typedef union {
226 double d;
227 ULong ul[2];
228} _double;
229#define value(x) ((x).d)
215#ifdef IEEE_LITTLE_ENDIAN 230#ifdef IEEE_LITTLE_ENDIAN
216#define word0(x) ((ULong *)&x)[1] 231#define word0(x) ((x).ul[1])
217#define word1(x) ((ULong *)&x)[0] 232#define word1(x) ((x).ul[0])
218#else 233#else
219#define word0(x) ((ULong *)&x)[0] 234#define word0(x) ((x).ul[0])
220#define word1(x) ((ULong *)&x)[1] 235#define word1(x) ((x).ul[1])
221#endif 236#endif
222 237
223/* The following definition of Storeinc is appropriate for MIPS processors. 238/* The following definition of Storeinc is appropriate for MIPS processors.
224 * An alternative that might be better on some machines is 239 * An alternative that might be better on some machines is
225 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) 240 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
226 */ 241 */
227#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) 242#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm32__)
228#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ 243#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
229((unsigned short *)a)[0] = (unsigned short)c, a++) 244((unsigned short *)a)[0] = (unsigned short)c, a++)
230#else 245#else
@@ -899,14 +914,16 @@ diff
899 static double 914 static double
900ulp 915ulp
901#ifdef KR_headers 916#ifdef KR_headers
902 (x) double x; 917 (_x) double _x;
903#else 918#else
904 (double x) 919 (double _x)
905#endif 920#endif
906{ 921{
922 _double x;
907 register Long L; 923 register Long L;
908 double a; 924 _double a;
909 925
926 value(x) = _x;
910 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; 927 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
911#ifndef Sudden_Underflow 928#ifndef Sudden_Underflow
912 if (L > 0) { 929 if (L > 0) {
@@ -931,7 +948,7 @@ ulp
931 } 948 }
932 } 949 }
933#endif 950#endif
934 return a; 951 return value(a);
935 } 952 }
936 953
937 static double 954 static double
@@ -944,7 +961,7 @@ b2d
944{ 961{
945 ULong *xa, *xa0, w, y, z; 962 ULong *xa, *xa0, w, y, z;
946 int k; 963 int k;
947 double d; 964 _double d;
948#ifdef VAX 965#ifdef VAX
949 ULong d0, d1; 966 ULong d0, d1;
950#else 967#else
@@ -1001,22 +1018,27 @@ b2d
1001#undef d0 1018#undef d0
1002#undef d1 1019#undef d1
1003#endif 1020#endif
1004 return d; 1021 return value(d);
1005 } 1022 }
1006 1023
1007 static Bigint * 1024 static Bigint *
1008d2b 1025d2b
1009#ifdef KR_headers 1026#ifdef KR_headers
1010 (d, e, bits) double d; int *e, *bits; 1027 (_d, e, bits) double d; int *e, *bits;
1011#else 1028#else
1012 (double d, int *e, int *bits) 1029 (double _d, int *e, int *bits)
1013#endif 1030#endif
1014{ 1031{
1015 Bigint *b; 1032 Bigint *b;
1016 int de, i, k; 1033 int de, i, k;
1017 ULong *x, y, z; 1034 ULong *x, y, z;
1035 _double d;
1018#ifdef VAX 1036#ifdef VAX
1019 ULong d0, d1; 1037 ULong d0, d1;
1038#endif
1039
1040 value(d) = _d;
1041#ifdef VAX
1020 d0 = word0(d) >> 16 | word0(d) << 16; 1042 d0 = word0(d) >> 16 | word0(d) << 16;
1021 d1 = word1(d) >> 16 | word1(d) << 16; 1043 d1 = word1(d) >> 16 | word1(d) << 16;
1022#else 1044#else
@@ -1141,11 +1163,11 @@ ratio
1141 (Bigint *a, Bigint *b) 1163 (Bigint *a, Bigint *b)
1142#endif 1164#endif
1143{ 1165{
1144 double da, db; 1166 _double da, db;
1145 int k, ka, kb; 1167 int k, ka, kb;
1146 1168
1147 da = b2d(a, &ka); 1169 value(da) = b2d(a, &ka);
1148 db = b2d(b, &kb); 1170 value(db) = b2d(b, &kb);
1149#ifdef Pack_32 1171#ifdef Pack_32
1150 k = ka - kb + 32*(a->wds - b->wds); 1172 k = ka - kb + 32*(a->wds - b->wds);
1151#else 1173#else
@@ -1171,7 +1193,7 @@ ratio
1171 word0(db) += k*Exp_msk1; 1193 word0(db) += k*Exp_msk1;
1172 } 1194 }
1173#endif 1195#endif
1174 return da / db; 1196 return value(da) / value(db);
1175 } 1197 }
1176 1198
1177static CONST double 1199static CONST double
@@ -1211,7 +1233,8 @@ strtod
1211 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, 1233 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1212 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; 1234 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1213 CONST char *s, *s0, *s1; 1235 CONST char *s, *s0, *s1;
1214 double aadj, aadj1, adj, rv, rv0; 1236 double aadj, aadj1, adj;
1237 _double rv, rv0;
1215 Long L; 1238 Long L;
1216 ULong y, z; 1239 ULong y, z;
1217 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; 1240 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
@@ -1223,10 +1246,10 @@ strtod
1223#endif 1246#endif
1224 1247
1225 sign = nz0 = nz = 0; 1248 sign = nz0 = nz = 0;
1226 rv = 0.; 1249 value(rv) = 0.;
1227 1250
1228 1251
1229 for(s = s00; isspace(*s); s++) 1252 for(s = s00; isspace((unsigned char) *s); s++)
1230 ; 1253 ;
1231 1254
1232 if (*s == '-') { 1255 if (*s == '-') {
@@ -1340,9 +1363,9 @@ strtod
1340 if (!nd0) 1363 if (!nd0)
1341 nd0 = nd; 1364 nd0 = nd;
1342 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; 1365 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1343 rv = y; 1366 value(rv) = y;
1344 if (k > 9) 1367 if (k > 9)
1345 rv = tens[k - 9] * rv + z; 1368 value(rv) = tens[k - 9] * value(rv) + z;
1346 bd0 = 0; 1369 bd0 = 0;
1347 if (nd <= DBL_DIG 1370 if (nd <= DBL_DIG
1348#ifndef RND_PRODQUOT 1371#ifndef RND_PRODQUOT
@@ -1356,7 +1379,8 @@ strtod
1356#ifdef VAX 1379#ifdef VAX
1357 goto vax_ovfl_check; 1380 goto vax_ovfl_check;
1358#else 1381#else
1359 /* rv = */ rounded_product(rv, tens[e]); 1382 /* value(rv) = */ rounded_product(value(rv),
1383 tens[e]);
1360 goto ret; 1384 goto ret;
1361#endif 1385#endif
1362 } 1386 }
@@ -1366,27 +1390,30 @@ strtod
1366 * this for larger i values. 1390 * this for larger i values.
1367 */ 1391 */
1368 e -= i; 1392 e -= i;
1369 rv *= tens[i]; 1393 value(rv) *= tens[i];
1370#ifdef VAX 1394#ifdef VAX
1371 /* VAX exponent range is so narrow we must 1395 /* VAX exponent range is so narrow we must
1372 * worry about overflow here... 1396 * worry about overflow here...
1373 */ 1397 */
1374 vax_ovfl_check: 1398 vax_ovfl_check:
1375 word0(rv) -= P*Exp_msk1; 1399 word0(rv) -= P*Exp_msk1;
1376 /* rv = */ rounded_product(rv, tens[e]); 1400 /* value(rv) = */ rounded_product(value(rv),
1401 tens[e]);
1377 if ((word0(rv) & Exp_mask) 1402 if ((word0(rv) & Exp_mask)
1378 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) 1403 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1379 goto ovfl; 1404 goto ovfl;
1380 word0(rv) += P*Exp_msk1; 1405 word0(rv) += P*Exp_msk1;
1381#else 1406#else
1382 /* rv = */ rounded_product(rv, tens[e]); 1407 /* value(rv) = */ rounded_product(value(rv),
1408 tens[e]);
1383#endif 1409#endif
1384 goto ret; 1410 goto ret;
1385 } 1411 }
1386 } 1412 }
1387#ifndef Inaccurate_Divide 1413#ifndef Inaccurate_Divide
1388 else if (e >= -Ten_pmax) { 1414 else if (e >= -Ten_pmax) {
1389 /* rv = */ rounded_quotient(rv, tens[-e]); 1415 /* value(rv) = */ rounded_quotient(value(rv),
1416 tens[-e]);
1390 goto ret; 1417 goto ret;
1391 } 1418 }
1392#endif 1419#endif
@@ -1397,13 +1424,13 @@ strtod
1397 1424
1398 if (e1 > 0) { 1425 if (e1 > 0) {
1399 if (i = e1 & 15) 1426 if (i = e1 & 15)
1400 rv *= tens[i]; 1427 value(rv) *= tens[i];
1401 if (e1 &= ~15) { 1428 if (e1 &= ~15) {
1402 if (e1 > DBL_MAX_10_EXP) { 1429 if (e1 > DBL_MAX_10_EXP) {
1403 ovfl: 1430 ovfl:
1404 errno = ERANGE; 1431 errno = ERANGE;
1405#ifdef __STDC__ 1432#ifdef __STDC__
1406 rv = HUGE_VAL; 1433 value(rv) = HUGE_VAL;
1407#else 1434#else
1408 /* Can't trust HUGE_VAL */ 1435 /* Can't trust HUGE_VAL */
1409#ifdef IEEE_Arith 1436#ifdef IEEE_Arith
@@ -1421,10 +1448,10 @@ strtod
1421 if (e1 >>= 4) { 1448 if (e1 >>= 4) {
1422 for(j = 0; e1 > 1; j++, e1 >>= 1) 1449 for(j = 0; e1 > 1; j++, e1 >>= 1)
1423 if (e1 & 1) 1450 if (e1 & 1)
1424 rv *= bigtens[j]; 1451 value(rv) *= bigtens[j];
1425 /* The last multiplication could overflow. */ 1452 /* The last multiplication could overflow. */
1426 word0(rv) -= P*Exp_msk1; 1453 word0(rv) -= P*Exp_msk1;
1427 rv *= bigtens[j]; 1454 value(rv) *= bigtens[j];
1428 if ((z = word0(rv) & Exp_mask) 1455 if ((z = word0(rv) & Exp_mask)
1429 > Exp_msk1*(DBL_MAX_EXP+Bias-P)) 1456 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1430 goto ovfl; 1457 goto ovfl;
@@ -1443,23 +1470,23 @@ strtod
1443 else if (e1 < 0) { 1470 else if (e1 < 0) {
1444 e1 = -e1; 1471 e1 = -e1;
1445 if (i = e1 & 15) 1472 if (i = e1 & 15)
1446 rv /= tens[i]; 1473 value(rv) /= tens[i];
1447 if (e1 &= ~15) { 1474 if (e1 &= ~15) {
1448 e1 >>= 4; 1475 e1 >>= 4;
1449 if (e1 >= 1 << n_bigtens) 1476 if (e1 >= 1 << n_bigtens)
1450 goto undfl; 1477 goto undfl;
1451 for(j = 0; e1 > 1; j++, e1 >>= 1) 1478 for(j = 0; e1 > 1; j++, e1 >>= 1)
1452 if (e1 & 1) 1479 if (e1 & 1)
1453 rv *= tinytens[j]; 1480 value(rv) *= tinytens[j];
1454 /* The last multiplication could underflow. */ 1481 /* The last multiplication could underflow. */
1455 rv0 = rv; 1482 value(rv0) = value(rv);
1456 rv *= tinytens[j]; 1483 value(rv) *= tinytens[j];
1457 if (!rv) { 1484 if (!value(rv)) {
1458 rv = 2.*rv0; 1485 value(rv) = 2.*value(rv0);
1459 rv *= tinytens[j]; 1486 value(rv) *= tinytens[j];
1460 if (!rv) { 1487 if (!value(rv)) {
1461 undfl: 1488 undfl:
1462 rv = 0.; 1489 value(rv) = 0.;
1463 errno = ERANGE; 1490 errno = ERANGE;
1464 if (bd0) 1491 if (bd0)
1465 goto retfree; 1492 goto retfree;
@@ -1483,7 +1510,7 @@ strtod
1483 for(;;) { 1510 for(;;) {
1484 bd = Balloc(bd0->k); 1511 bd = Balloc(bd0->k);
1485 Bcopy(bd, bd0); 1512 Bcopy(bd, bd0);
1486 bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ 1513 bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
1487 bs = i2b(1); 1514 bs = i2b(1);
1488 1515
1489 if (e >= 0) { 1516 if (e >= 0) {
@@ -1595,12 +1622,12 @@ strtod
1595 break; 1622 break;
1596#endif 1623#endif
1597 if (dsign) 1624 if (dsign)
1598 rv += ulp(rv); 1625 value(rv) += ulp(value(rv));
1599#ifndef ROUND_BIASED 1626#ifndef ROUND_BIASED
1600 else { 1627 else {
1601 rv -= ulp(rv); 1628 value(rv) -= ulp(value(rv));
1602#ifndef Sudden_Underflow 1629#ifndef Sudden_Underflow
1603 if (!rv) 1630 if (!value(rv))
1604 goto undfl; 1631 goto undfl;
1605#endif 1632#endif
1606 } 1633 }
@@ -1651,10 +1678,10 @@ strtod
1651 /* Check for overflow */ 1678 /* Check for overflow */
1652 1679
1653 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { 1680 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1654 rv0 = rv; 1681 value(rv0) = value(rv);
1655 word0(rv) -= P*Exp_msk1; 1682 word0(rv) -= P*Exp_msk1;
1656 adj = aadj1 * ulp(rv); 1683 adj = aadj1 * ulp(value(rv));
1657 rv += adj; 1684 value(rv) += adj;
1658 if ((word0(rv) & Exp_mask) >= 1685 if ((word0(rv) & Exp_mask) >=
1659 Exp_msk1*(DBL_MAX_EXP+Bias-P)) { 1686 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1660 if (word0(rv0) == Big0 && word1(rv0) == Big1) 1687 if (word0(rv0) == Big0 && word1(rv0) == Big1)
@@ -1669,10 +1696,10 @@ strtod
1669 else { 1696 else {
1670#ifdef Sudden_Underflow 1697#ifdef Sudden_Underflow
1671 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { 1698 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
1672 rv0 = rv; 1699 value(rv0) = value(rv);
1673 word0(rv) += P*Exp_msk1; 1700 word0(rv) += P*Exp_msk1;
1674 adj = aadj1 * ulp(rv); 1701 adj = aadj1 * ulp(value(rv));
1675 rv += adj; 1702 value(rv) += adj;
1676#ifdef IBM 1703#ifdef IBM
1677 if ((word0(rv) & Exp_mask) < P*Exp_msk1) 1704 if ((word0(rv) & Exp_mask) < P*Exp_msk1)
1678#else 1705#else
@@ -1690,8 +1717,8 @@ strtod
1690 word0(rv) -= P*Exp_msk1; 1717 word0(rv) -= P*Exp_msk1;
1691 } 1718 }
1692 else { 1719 else {
1693 adj = aadj1 * ulp(rv); 1720 adj = aadj1 * ulp(value(rv));
1694 rv += adj; 1721 value(rv) += adj;
1695 } 1722 }
1696#else 1723#else
1697 /* Compute adj so that the IEEE rounding rules will 1724 /* Compute adj so that the IEEE rounding rules will
@@ -1706,8 +1733,8 @@ strtod
1706 if (!dsign) 1733 if (!dsign)
1707 aadj1 = -aadj1; 1734 aadj1 = -aadj1;
1708 } 1735 }
1709 adj = aadj1 * ulp(rv); 1736 adj = aadj1 * ulp(value(rv));
1710 rv += adj; 1737 value(rv) += adj;
1711#endif 1738#endif
1712 } 1739 }
1713 z = word0(rv) & Exp_mask; 1740 z = word0(rv) & Exp_mask;
@@ -1738,7 +1765,7 @@ strtod
1738 ret: 1765 ret:
1739 if (se) 1766 if (se)
1740 *se = (char *)s; 1767 *se = (char *)s;
1741 return sign ? -rv : rv; 1768 return sign ? -value(rv) : value(rv);
1742 } 1769 }
1743 1770
1744 static int 1771 static int
@@ -1884,10 +1911,10 @@ quorem
1884 char * 1911 char *
1885__dtoa 1912__dtoa
1886#ifdef KR_headers 1913#ifdef KR_headers
1887 (d, mode, ndigits, decpt, sign, rve) 1914 (_d, mode, ndigits, decpt, sign, rve)
1888 double d; int mode, ndigits, *decpt, *sign; char **rve; 1915 double _d; int mode, ndigits, *decpt, *sign; char **rve;
1889#else 1916#else
1890 (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) 1917 (double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
1891#endif 1918#endif
1892{ 1919{
1893 /* Arguments ndigits, decpt, sign are similar to those 1920 /* Arguments ndigits, decpt, sign are similar to those
@@ -1933,11 +1960,13 @@ __dtoa
1933 ULong x; 1960 ULong x;
1934#endif 1961#endif
1935 Bigint *b, *b1, *delta, *mlo, *mhi, *S; 1962 Bigint *b, *b1, *delta, *mlo, *mhi, *S;
1936 double d2, ds, eps; 1963 double ds;
1937 char *s, *s0; 1964 char *s, *s0;
1938 static Bigint *result; 1965 static Bigint *result;
1939 static int result_k; 1966 static int result_k;
1967 _double d, d2, eps;
1940 1968
1969 value(d) = _d;
1941 if (result) { 1970 if (result) {
1942 result->k = result_k; 1971 result->k = result_k;
1943 result->maxwds = 1 << result_k; 1972 result->maxwds = 1 << result_k;
@@ -1977,9 +2006,9 @@ __dtoa
1977 } 2006 }
1978#endif 2007#endif
1979#ifdef IBM 2008#ifdef IBM
1980 d += 0; /* normalize */ 2009 value(d) += 0; /* normalize */
1981#endif 2010#endif
1982 if (!d) { 2011 if (!value(d)) {
1983 *decpt = 1; 2012 *decpt = 1;
1984 s = "0"; 2013 s = "0";
1985 if (rve) 2014 if (rve)
@@ -1987,18 +2016,18 @@ __dtoa
1987 return s; 2016 return s;
1988 } 2017 }
1989 2018
1990 b = d2b(d, &be, &bbits); 2019 b = d2b(value(d), &be, &bbits);
1991#ifdef Sudden_Underflow 2020#ifdef Sudden_Underflow
1992 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); 2021 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
1993#else 2022#else
1994 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { 2023 if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) {
1995#endif 2024#endif
1996 d2 = d; 2025 value(d2) = value(d);
1997 word0(d2) &= Frac_mask1; 2026 word0(d2) &= Frac_mask1;
1998 word0(d2) |= Exp_11; 2027 word0(d2) |= Exp_11;
1999#ifdef IBM 2028#ifdef IBM
2000 if (j = 11 - hi0bits(word0(d2) & Frac_mask)) 2029 if (j = 11 - hi0bits(word0(d2) & Frac_mask))
2001 d2 /= 1 << j; 2030 value(d2) /= 1 << j;
2002#endif 2031#endif
2003 2032
2004 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 2033 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
@@ -2037,19 +2066,20 @@ __dtoa
2037 i = bbits + be + (Bias + (P-1) - 1); 2066 i = bbits + be + (Bias + (P-1) - 1);
2038 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 2067 x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32
2039 : word1(d) << 32 - i; 2068 : word1(d) << 32 - i;
2040 d2 = x; 2069 value(d2) = x;
2041 word0(d2) -= 31*Exp_msk1; /* adjust exponent */ 2070 word0(d2) -= 31*Exp_msk1; /* adjust exponent */
2042 i -= (Bias + (P-1) - 1) + 1; 2071 i -= (Bias + (P-1) - 1) + 1;
2043 denorm = 1; 2072 denorm = 1;
2044 } 2073 }
2045#endif 2074#endif
2046 ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; 2075 ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 +
2076 i*0.301029995663981;
2047 k = (int)ds; 2077 k = (int)ds;
2048 if (ds < 0. && ds != k) 2078 if (ds < 0. && ds != k)
2049 k--; /* want k = floor(ds) */ 2079 k--; /* want k = floor(ds) */
2050 k_check = 1; 2080 k_check = 1;
2051 if (k >= 0 && k <= Ten_pmax) { 2081 if (k >= 0 && k <= Ten_pmax) {
2052 if (d < tens[k]) 2082 if (value(d) < tens[k])
2053 k--; 2083 k--;
2054 k_check = 0; 2084 k_check = 0;
2055 } 2085 }
@@ -2116,7 +2146,7 @@ __dtoa
2116 /* Try to get by with floating-point arithmetic. */ 2146 /* Try to get by with floating-point arithmetic. */
2117 2147
2118 i = 0; 2148 i = 0;
2119 d2 = d; 2149 value(d2) = value(d);
2120 k0 = k; 2150 k0 = k;
2121 ilim0 = ilim; 2151 ilim0 = ilim;
2122 ieps = 2; /* conservative */ 2152 ieps = 2; /* conservative */
@@ -2126,7 +2156,7 @@ __dtoa
2126 if (j & Bletch) { 2156 if (j & Bletch) {
2127 /* prevent overflows */ 2157 /* prevent overflows */
2128 j &= Bletch - 1; 2158 j &= Bletch - 1;
2129 d /= bigtens[n_bigtens-1]; 2159 value(d) /= bigtens[n_bigtens-1];
2130 ieps++; 2160 ieps++;
2131 } 2161 }
2132 for(; j; j >>= 1, i++) 2162 for(; j; j >>= 1, i++)
@@ -2134,32 +2164,32 @@ __dtoa
2134 ieps++; 2164 ieps++;
2135 ds *= bigtens[i]; 2165 ds *= bigtens[i];
2136 } 2166 }
2137 d /= ds; 2167 value(d) /= ds;
2138 } 2168 }
2139 else if (j1 = -k) { 2169 else if (j1 = -k) {
2140 d *= tens[j1 & 0xf]; 2170 value(d) *= tens[j1 & 0xf];
2141 for(j = j1 >> 4; j; j >>= 1, i++) 2171 for(j = j1 >> 4; j; j >>= 1, i++)
2142 if (j & 1) { 2172 if (j & 1) {
2143 ieps++; 2173 ieps++;
2144 d *= bigtens[i]; 2174 value(d) *= bigtens[i];
2145 } 2175 }
2146 } 2176 }
2147 if (k_check && d < 1. && ilim > 0) { 2177 if (k_check && value(d) < 1. && ilim > 0) {
2148 if (ilim1 <= 0) 2178 if (ilim1 <= 0)
2149 goto fast_failed; 2179 goto fast_failed;
2150 ilim = ilim1; 2180 ilim = ilim1;
2151 k--; 2181 k--;
2152 d *= 10.; 2182 value(d) *= 10.;
2153 ieps++; 2183 ieps++;
2154 } 2184 }
2155 eps = ieps*d + 7.; 2185 value(eps) = ieps*value(d) + 7.;
2156 word0(eps) -= (P-1)*Exp_msk1; 2186 word0(eps) -= (P-1)*Exp_msk1;
2157 if (ilim == 0) { 2187 if (ilim == 0) {
2158 S = mhi = 0; 2188 S = mhi = 0;
2159 d -= 5.; 2189 value(d) -= 5.;
2160 if (d > eps) 2190 if (value(d) > value(eps))
2161 goto one_digit; 2191 goto one_digit;
2162 if (d < -eps) 2192 if (value(d) < -value(eps))
2163 goto no_digits; 2193 goto no_digits;
2164 goto fast_failed; 2194 goto fast_failed;
2165 } 2195 }
@@ -2168,33 +2198,33 @@ __dtoa
2168 /* Use Steele & White method of only 2198 /* Use Steele & White method of only
2169 * generating digits needed. 2199 * generating digits needed.
2170 */ 2200 */
2171 eps = 0.5/tens[ilim-1] - eps; 2201 value(eps) = 0.5/tens[ilim-1] - value(eps);
2172 for(i = 0;;) { 2202 for(i = 0;;) {
2173 L = d; 2203 L = value(d);
2174 d -= L; 2204 value(d) -= L;
2175 *s++ = '0' + (int)L; 2205 *s++ = '0' + (int)L;
2176 if (d < eps) 2206 if (value(d) < value(eps))
2177 goto ret1; 2207 goto ret1;
2178 if (1. - d < eps) 2208 if (1. - value(d) < value(eps))
2179 goto bump_up; 2209 goto bump_up;
2180 if (++i >= ilim) 2210 if (++i >= ilim)
2181 break; 2211 break;
2182 eps *= 10.; 2212 value(eps) *= 10.;
2183 d *= 10.; 2213 value(d) *= 10.;
2184 } 2214 }
2185 } 2215 }
2186 else { 2216 else {
2187#endif 2217#endif
2188 /* Generate ilim digits, then fix them up. */ 2218 /* Generate ilim digits, then fix them up. */
2189 eps *= tens[ilim-1]; 2219 value(eps) *= tens[ilim-1];
2190 for(i = 1;; i++, d *= 10.) { 2220 for(i = 1;; i++, value(d) *= 10.) {
2191 L = d; 2221 L = value(d);
2192 d -= L; 2222 value(d) -= L;
2193 *s++ = '0' + (int)L; 2223 *s++ = '0' + (int)L;
2194 if (i == ilim) { 2224 if (i == ilim) {
2195 if (d > 0.5 + eps) 2225 if (value(d) > 0.5 + value(eps))
2196 goto bump_up; 2226 goto bump_up;
2197 else if (d < 0.5 - eps) { 2227 else if (value(d) < 0.5 - value(eps)) {
2198 while(*--s == '0'); 2228 while(*--s == '0');
2199 s++; 2229 s++;
2200 goto ret1; 2230 goto ret1;
@@ -2207,7 +2237,7 @@ __dtoa
2207#endif 2237#endif
2208 fast_failed: 2238 fast_failed:
2209 s = s0; 2239 s = s0;
2210 d = d2; 2240 value(d) = value(d2);
2211 k = k0; 2241 k = k0;
2212 ilim = ilim0; 2242 ilim = ilim0;
2213 } 2243 }
@@ -2219,24 +2249,24 @@ __dtoa
2219 ds = tens[k]; 2249 ds = tens[k];
2220 if (ndigits < 0 && ilim <= 0) { 2250 if (ndigits < 0 && ilim <= 0) {
2221 S = mhi = 0; 2251 S = mhi = 0;
2222 if (ilim < 0 || d <= 5*ds) 2252 if (ilim < 0 || value(d) <= 5*ds)
2223 goto no_digits; 2253 goto no_digits;
2224 goto one_digit; 2254 goto one_digit;
2225 } 2255 }
2226 for(i = 1;; i++) { 2256 for(i = 1;; i++) {
2227 L = d / ds; 2257 L = value(d) / ds;
2228 d -= L*ds; 2258 value(d) -= L*ds;
2229#ifdef Check_FLT_ROUNDS 2259#ifdef Check_FLT_ROUNDS
2230 /* If FLT_ROUNDS == 2, L will usually be high by 1 */ 2260 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2231 if (d < 0) { 2261 if (value(d) < 0) {
2232 L--; 2262 L--;
2233 d += ds; 2263 value(d) += ds;
2234 } 2264 }
2235#endif 2265#endif
2236 *s++ = '0' + (int)L; 2266 *s++ = '0' + (int)L;
2237 if (i == ilim) { 2267 if (i == ilim) {
2238 d += d; 2268 value(d) += value(d);
2239 if (d > ds || d == ds && L & 1) { 2269 if (value(d) > ds || value(d) == ds && L & 1) {
2240 bump_up: 2270 bump_up:
2241 while(*--s == '9') 2271 while(*--s == '9')
2242 if (s == s0) { 2272 if (s == s0) {
@@ -2248,7 +2278,7 @@ __dtoa
2248 } 2278 }
2249 break; 2279 break;
2250 } 2280 }
2251 if (!(d *= 10.)) 2281 if (!(value(d) *= 10.))
2252 break; 2282 break;
2253 } 2283 }
2254 goto ret1; 2284 goto ret1;
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3
index 808ba90165..b7d2cd1225 100644
--- a/src/lib/libc/stdlib/strtol.3
+++ b/src/lib/libc/stdlib/strtol.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 36.\" $OpenBSD: strtol.3,v 1.3 1996/08/19 08:33:51 tholo Exp $
37.\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $
38.\" 37.\"
39.Dd June 25, 1992 38.Dd June 25, 1992
40.Dt STRTOL 3 39.Dt STRTOL 3
@@ -46,13 +45,13 @@
46.Fd #include <stdlib.h> 45.Fd #include <stdlib.h>
47.Fd #include <limits.h> 46.Fd #include <limits.h>
48.Ft long 47.Ft long
49.Fn strtol "char *nptr" "char **endptr" "int base" 48.Fn strtol "const char *nptr" "char **endptr" "int base"
50 49
51.Fd #include <sys/types.h> 50.Fd #include <sys/types.h>
52.Fd #include <stdlib.h> 51.Fd #include <stdlib.h>
53.Fd #include <limits.h> 52.Fd #include <limits.h>
54.Ft quad_t 53.Ft quad_t
55.Fn strtoq "char *nptr" "char **endptr" "int base" 54.Fn strtoq "const char *nptr" "char **endptr" "int base"
56.Sh DESCRIPTION 55.Sh DESCRIPTION
57The 56The
58.Fn strtol 57.Fn strtol
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c
index 6f374abd5f..e4ad557fd5 100644
--- a/src/lib/libc/stdlib/strtol.c
+++ b/src/lib/libc/stdlib/strtol.c
@@ -32,13 +32,12 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: strtol.c,v 1.4 1996/08/19 08:33:51 tholo Exp $";
36static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <limits.h>
40#include <ctype.h> 38#include <ctype.h>
41#include <errno.h> 39#include <errno.h>
40#include <limits.h>
42#include <stdlib.h> 41#include <stdlib.h>
43 42
44 43
@@ -54,25 +53,28 @@ strtol(nptr, endptr, base)
54 char **endptr; 53 char **endptr;
55 register int base; 54 register int base;
56{ 55{
57 register const char *s = nptr; 56 register const char *s;
58 register unsigned long acc; 57 register long acc, cutoff;
59 register int c; 58 register int c;
60 register unsigned long cutoff; 59 register int neg, any, cutlim;
61 register int neg = 0, any, cutlim;
62 60
63 /* 61 /*
64 * Skip white space and pick up leading +/- sign if any. 62 * Skip white space and pick up leading +/- sign if any.
65 * If base is 0, allow 0x for hex and 0 for octal, else 63 * If base is 0, allow 0x for hex and 0 for octal, else
66 * assume decimal; if base is already 16, allow 0x. 64 * assume decimal; if base is already 16, allow 0x.
67 */ 65 */
66 s = nptr;
68 do { 67 do {
69 c = *s++; 68 c = (unsigned char) *s++;
70 } while (isspace(c)); 69 } while (isspace(c));
71 if (c == '-') { 70 if (c == '-') {
72 neg = 1; 71 neg = 1;
73 c = *s++; 72 c = *s++;
74 } else if (c == '+') 73 } else {
75 c = *s++; 74 neg = 0;
75 if (c == '+')
76 c = *s++;
77 }
76 if ((base == 0 || base == 16) && 78 if ((base == 0 || base == 16) &&
77 c == '0' && (*s == 'x' || *s == 'X')) { 79 c == '0' && (*s == 'x' || *s == 'X')) {
78 c = s[1]; 80 c = s[1];
@@ -99,10 +101,17 @@ strtol(nptr, endptr, base)
99 * Set any if any `digits' consumed; make it negative to indicate 101 * Set any if any `digits' consumed; make it negative to indicate
100 * overflow. 102 * overflow.
101 */ 103 */
102 cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 104 cutoff = neg ? LONG_MIN : LONG_MAX;
103 cutlim = cutoff % (unsigned long)base; 105 cutlim = cutoff % base;
104 cutoff /= (unsigned long)base; 106 cutoff /= base;
105 for (acc = 0, any = 0;; c = *s++) { 107 if (neg) {
108 if (cutlim > 0) {
109 cutlim -= base;
110 cutoff += 1;
111 }
112 cutlim = -cutlim;
113 }
114 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
106 if (isdigit(c)) 115 if (isdigit(c))
107 c -= '0'; 116 c -= '0';
108 else if (isalpha(c)) 117 else if (isalpha(c))
@@ -111,19 +120,30 @@ strtol(nptr, endptr, base)
111 break; 120 break;
112 if (c >= base) 121 if (c >= base)
113 break; 122 break;
114 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 123 if (any < 0)
115 any = -1; 124 continue;
116 else { 125 if (neg) {
117 any = 1; 126 if (acc < cutoff || acc == cutoff && c > cutlim) {
118 acc *= base; 127 any = -1;
119 acc += c; 128 acc = LONG_MIN;
129 errno = ERANGE;
130 } else {
131 any = 1;
132 acc *= base;
133 acc -= c;
134 }
135 } else {
136 if (acc > cutoff || acc == cutoff && c > cutlim) {
137 any = -1;
138 acc = LONG_MAX;
139 errno = ERANGE;
140 } else {
141 any = 1;
142 acc *= base;
143 acc += c;
144 }
120 } 145 }
121 } 146 }
122 if (any < 0) {
123 acc = neg ? LONG_MIN : LONG_MAX;
124 errno = ERANGE;
125 } else if (neg)
126 acc = -acc;
127 if (endptr != 0) 147 if (endptr != 0)
128 *endptr = (char *) (any ? s - 1 : nptr); 148 *endptr = (char *) (any ? s - 1 : nptr);
129 return (acc); 149 return (acc);
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoq.c
index fc559e9d7f..44aabd73f0 100644
--- a/src/lib/libc/stdlib/strtoq.c
+++ b/src/lib/libc/stdlib/strtoq.c
@@ -32,14 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; 35static char rcsid[] = "$OpenBSD: strtoq.c,v 1.4 1996/08/19 08:33:52 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h> 38#include <sys/types.h>
39 39
40#include <limits.h>
41#include <errno.h>
42#include <ctype.h> 40#include <ctype.h>
41#include <errno.h>
42#include <limits.h>
43#include <stdlib.h> 43#include <stdlib.h>
44 44
45/* 45/*
@@ -55,9 +55,8 @@ strtoq(nptr, endptr, base)
55 register int base; 55 register int base;
56{ 56{
57 register const char *s; 57 register const char *s;
58 register u_quad_t acc; 58 register quad_t acc, cutoff;
59 register int c; 59 register int c;
60 register u_quad_t qbase, cutoff;
61 register int neg, any, cutlim; 60 register int neg, any, cutlim;
62 61
63 /* 62 /*
@@ -67,7 +66,7 @@ strtoq(nptr, endptr, base)
67 */ 66 */
68 s = nptr; 67 s = nptr;
69 do { 68 do {
70 c = *s++; 69 c = (unsigned char) *s++;
71 } while (isspace(c)); 70 } while (isspace(c));
72 if (c == '-') { 71 if (c == '-') {
73 neg = 1; 72 neg = 1;
@@ -104,11 +103,17 @@ strtoq(nptr, endptr, base)
104 * Set any if any `digits' consumed; make it negative to indicate 103 * Set any if any `digits' consumed; make it negative to indicate
105 * overflow. 104 * overflow.
106 */ 105 */
107 qbase = (unsigned)base; 106 cutoff = neg ? QUAD_MIN : QUAD_MAX;
108 cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; 107 cutlim = cutoff % base;
109 cutlim = cutoff % qbase; 108 cutoff /= base;
110 cutoff /= qbase; 109 if (neg) {
111 for (acc = 0, any = 0;; c = *s++) { 110 if (cutlim > 0) {
111 cutlim -= base;
112 cutoff += 1;
113 }
114 cutlim = -cutlim;
115 }
116 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
112 if (isdigit(c)) 117 if (isdigit(c))
113 c -= '0'; 118 c -= '0';
114 else if (isalpha(c)) 119 else if (isalpha(c))
@@ -117,19 +122,30 @@ strtoq(nptr, endptr, base)
117 break; 122 break;
118 if (c >= base) 123 if (c >= base)
119 break; 124 break;
120 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 125 if (any < 0)
121 any = -1; 126 continue;
122 else { 127 if (neg) {
123 any = 1; 128 if (acc < cutoff || acc == cutoff && c > cutlim) {
124 acc *= qbase; 129 any = -1;
125 acc += c; 130 acc = QUAD_MIN;
131 errno = ERANGE;
132 } else {
133 any = 1;
134 acc *= base;
135 acc -= c;
136 }
137 } else {
138 if (acc > cutoff || acc == cutoff && c > cutlim) {
139 any = -1;
140 acc = QUAD_MAX;
141 errno = ERANGE;
142 } else {
143 any = 1;
144 acc *= base;
145 acc += c;
146 }
126 } 147 }
127 } 148 }
128 if (any < 0) {
129 acc = neg ? QUAD_MIN : QUAD_MAX;
130 errno = ERANGE;
131 } else if (neg)
132 acc = -acc;
133 if (endptr != 0) 149 if (endptr != 0)
134 *endptr = (char *) (any ? s - 1 : nptr); 150 *endptr = (char *) (any ? s - 1 : nptr);
135 return (acc); 151 return (acc);
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
index db551b0141..b8234122a2 100644
--- a/src/lib/libc/stdlib/strtoul.3
+++ b/src/lib/libc/stdlib/strtoul.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 36.\" $OpenBSD: strtoul.3,v 1.2 1996/08/19 08:33:52 tholo Exp $
37.\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
38.\" 37.\"
39.Dd June 25, 1992 38.Dd June 25, 1992
40.Dt STRTOUL 3 39.Dt STRTOUL 3
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c
index 00f7210fa1..d3b363fa04 100644
--- a/src/lib/libc/stdlib/strtoul.c
+++ b/src/lib/libc/stdlib/strtoul.c
@@ -32,13 +32,12 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: strtoul.c,v 1.4 1996/08/19 08:33:52 tholo Exp $";
36static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <limits.h>
40#include <ctype.h> 38#include <ctype.h>
41#include <errno.h> 39#include <errno.h>
40#include <limits.h>
42#include <stdlib.h> 41#include <stdlib.h>
43 42
44/* 43/*
@@ -53,23 +52,26 @@ strtoul(nptr, endptr, base)
53 char **endptr; 52 char **endptr;
54 register int base; 53 register int base;
55{ 54{
56 register const char *s = nptr; 55 register const char *s;
57 register unsigned long acc; 56 register unsigned long acc, cutoff;
58 register int c; 57 register int c;
59 register unsigned long cutoff; 58 register int neg, any, cutlim;
60 register int neg = 0, any, cutlim;
61 59
62 /* 60 /*
63 * See strtol for comments as to the logic used. 61 * See strtol for comments as to the logic used.
64 */ 62 */
63 s = nptr;
65 do { 64 do {
66 c = *s++; 65 c = (unsigned char) *s++;
67 } while (isspace(c)); 66 } while (isspace(c));
68 if (c == '-') { 67 if (c == '-') {
69 neg = 1; 68 neg = 1;
70 c = *s++; 69 c = *s++;
71 } else if (c == '+') 70 } else {
72 c = *s++; 71 neg = 0;
72 if (c == '+')
73 c = *s++;
74 }
73 if ((base == 0 || base == 16) && 75 if ((base == 0 || base == 16) &&
74 c == '0' && (*s == 'x' || *s == 'X')) { 76 c == '0' && (*s == 'x' || *s == 'X')) {
75 c = s[1]; 77 c = s[1];
@@ -78,9 +80,10 @@ strtoul(nptr, endptr, base)
78 } 80 }
79 if (base == 0) 81 if (base == 0)
80 base = c == '0' ? 8 : 10; 82 base = c == '0' ? 8 : 10;
81 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; 83
82 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; 84 cutoff = ULONG_MAX / (unsigned long)base;
83 for (acc = 0, any = 0;; c = *s++) { 85 cutlim = ULONG_MAX % (unsigned long)base;
86 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
84 if (isdigit(c)) 87 if (isdigit(c))
85 c -= '0'; 88 c -= '0';
86 else if (isalpha(c)) 89 else if (isalpha(c))
@@ -89,18 +92,19 @@ strtoul(nptr, endptr, base)
89 break; 92 break;
90 if (c >= base) 93 if (c >= base)
91 break; 94 break;
92 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 95 if (any < 0)
96 continue;
97 if (acc > cutoff || acc == cutoff && c > cutlim) {
93 any = -1; 98 any = -1;
94 else { 99 acc = ULONG_MAX;
100 errno = ERANGE;
101 } else {
95 any = 1; 102 any = 1;
96 acc *= base; 103 acc *= (unsigned long)base;
97 acc += c; 104 acc += c;
98 } 105 }
99 } 106 }
100 if (any < 0) { 107 if (neg && any > 0)
101 acc = ULONG_MAX;
102 errno = ERANGE;
103 } else if (neg)
104 acc = -acc; 108 acc = -acc;
105 if (endptr != 0) 109 if (endptr != 0)
106 *endptr = (char *) (any ? s - 1 : nptr); 110 *endptr = (char *) (any ? s - 1 : nptr);
diff --git a/src/lib/libc/stdlib/strtouq.c b/src/lib/libc/stdlib/strtouq.c
index cc647d8d28..1f29a22f33 100644
--- a/src/lib/libc/stdlib/strtouq.c
+++ b/src/lib/libc/stdlib/strtouq.c
@@ -32,14 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92"; 35static char rcsid[] = "$OpenBSD: strtouq.c,v 1.4 1996/08/19 08:33:53 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h> 38#include <sys/types.h>
39 39
40#include <limits.h>
41#include <errno.h>
42#include <ctype.h> 40#include <ctype.h>
41#include <errno.h>
42#include <limits.h>
43#include <stdlib.h> 43#include <stdlib.h>
44 44
45/* 45/*
@@ -54,10 +54,9 @@ strtouq(nptr, endptr, base)
54 char **endptr; 54 char **endptr;
55 register int base; 55 register int base;
56{ 56{
57 register const char *s = nptr; 57 register const char *s;
58 register u_quad_t acc; 58 register u_quad_t acc, cutoff;
59 register int c; 59 register int c;
60 register u_quad_t qbase, cutoff;
61 register int neg, any, cutlim; 60 register int neg, any, cutlim;
62 61
63 /* 62 /*
@@ -65,7 +64,7 @@ strtouq(nptr, endptr, base)
65 */ 64 */
66 s = nptr; 65 s = nptr;
67 do { 66 do {
68 c = *s++; 67 c = (unsigned char) *s++;
69 } while (isspace(c)); 68 } while (isspace(c));
70 if (c == '-') { 69 if (c == '-') {
71 neg = 1; 70 neg = 1;
@@ -83,10 +82,10 @@ strtouq(nptr, endptr, base)
83 } 82 }
84 if (base == 0) 83 if (base == 0)
85 base = c == '0' ? 8 : 10; 84 base = c == '0' ? 8 : 10;
86 qbase = (unsigned)base; 85
87 cutoff = (u_quad_t)UQUAD_MAX / qbase; 86 cutoff = UQUAD_MAX / (u_quad_t)base;
88 cutlim = (u_quad_t)UQUAD_MAX % qbase; 87 cutlim = UQUAD_MAX % (u_quad_t)base;
89 for (acc = 0, any = 0;; c = *s++) { 88 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
90 if (isdigit(c)) 89 if (isdigit(c))
91 c -= '0'; 90 c -= '0';
92 else if (isalpha(c)) 91 else if (isalpha(c))
@@ -95,18 +94,19 @@ strtouq(nptr, endptr, base)
95 break; 94 break;
96 if (c >= base) 95 if (c >= base)
97 break; 96 break;
98 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 97 if (any < 0)
98 continue;
99 if (acc > cutoff || acc == cutoff && c > cutlim) {
99 any = -1; 100 any = -1;
100 else { 101 acc = UQUAD_MAX;
102 errno = ERANGE;
103 } else {
101 any = 1; 104 any = 1;
102 acc *= qbase; 105 acc *= (u_quad_t)base;
103 acc += c; 106 acc += c;
104 } 107 }
105 } 108 }
106 if (any < 0) { 109 if (neg && any > 0)
107 acc = UQUAD_MAX;
108 errno = ERANGE;
109 } else if (neg)
110 acc = -acc; 110 acc = -acc;
111 if (endptr != 0) 111 if (endptr != 0)
112 *endptr = (char *) (any ? s - 1 : nptr); 112 *endptr = (char *) (any ? s - 1 : nptr);
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3
index 520f51db0a..985adb07de 100644
--- a/src/lib/libc/stdlib/system.3
+++ b/src/lib/libc/stdlib/system.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)system.3 6.5 (Berkeley) 6/29/91 36.\" $OpenBSD: system.3,v 1.5 1996/12/11 23:09:53 tholo Exp $
37.\" $Id: system.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt SYSTEM 3 39.Dt SYSTEM 3
@@ -72,7 +71,7 @@ will return non-zero.
72Otherwise, 71Otherwise,
73.Fn system 72.Fn system
74returns the termination status of the shell in the format specified by 73returns the termination status of the shell in the format specified by
75.Xr waitpid 3 . 74.Xr waitpid 2 .
76.Sh RETURN VALUES 75.Sh RETURN VALUES
77If a child process cannot be created, or the termination status of 76If a child process cannot be created, or the termination status of
78the shell cannot be obtained, 77the shell cannot be obtained,
@@ -87,8 +86,8 @@ returns the termination status for a program that terminates with a call of
87.Sh SEE ALSO 86.Sh SEE ALSO
88.Xr sh 1 , 87.Xr sh 1 ,
89.Xr execve 2 , 88.Xr execve 2 ,
90.Xr popen 3 , 89.Xr waitpid 2 ,
91.Xr waitpid 3 , 90.Xr popen 3
92.Sh STANDARDS 91.Sh STANDARDS
93The 92The
94.Fn system 93.Fn system
@@ -96,4 +95,4 @@ function
96conforms to 95conforms to
97.St -ansiC 96.St -ansiC
98and 97and
99.St -1003.2-92 . 98.St -p1003.2-92 .
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c
index c2f39325f6..3e1b047393 100644
--- a/src/lib/libc/stdlib/system.c
+++ b/src/lib/libc/stdlib/system.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)system.c 5.10 (Berkeley) 2/23/91";*/ 35static char *rcsid = "$OpenBSD: system.c,v 1.3 1996/09/15 09:31:52 tholo Exp $";
36static char *rcsid = "$Id: system.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <sys/types.h> 38#include <sys/types.h>
@@ -53,11 +52,13 @@ system(command)
53 sig_t intsave, quitsave; 52 sig_t intsave, quitsave;
54 int omask; 53 int omask;
55 int pstat; 54 int pstat;
56 char *argp[] = {"sh", "-c", (char *) command, NULL}; 55 char *argp[] = {"sh", "-c", NULL, NULL};
57 56
58 if (!command) /* just checking... */ 57 if (!command) /* just checking... */
59 return(1); 58 return(1);
60 59
60 argp[2] = (char *)command;
61
61 omask = sigblock(sigmask(SIGCHLD)); 62 omask = sigblock(sigmask(SIGCHLD));
62 switch(pid = vfork()) { 63 switch(pid = vfork()) {
63 case -1: /* error */ 64 case -1: /* error */
diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c
new file mode 100644
index 0000000000..9e5bd4b0f2
--- /dev/null
+++ b/src/lib/libc/stdlib/tfind.c
@@ -0,0 +1,41 @@
1/*
2 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3 * the AT&T man page says.
4 *
5 * The node_t structure is for internal use only, lint doesn't grok it.
6 *
7 * Written by reading the System V Interface Definition, not the code.
8 *
9 * Totally public domain.
10 */
11/*LINTLIBRARY*/
12#include <search.h>
13
14typedef struct node_t
15{
16 char *key;
17 struct node_t *llink, *rlink;
18} node;
19
20/* find a node, or return 0 */
21void *
22tfind(vkey, vrootp, compar)
23 const void *vkey; /* key to be found */
24 void *const *vrootp; /* address of the tree root */
25 int (*compar) __P((const void *, const void *));
26{
27 char *key = (char *)vkey;
28 node **rootp = (node **)vrootp;
29
30 if (rootp == (struct node_t **)0)
31 return ((struct node_t *)0);
32 while (*rootp != (struct node_t *)0) { /* T1: */
33 int r;
34 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
35 return (*rootp); /* key found */
36 rootp = (r < 0) ?
37 &(*rootp)->llink : /* T3: follow left branch */
38 &(*rootp)->rlink; /* T4: follow right branch */
39 }
40 return (node *)0;
41}
diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3
new file mode 100644
index 0000000000..dbdae7943c
--- /dev/null
+++ b/src/lib/libc/stdlib/tsearch.3
@@ -0,0 +1,116 @@
1.\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. The name of the author may not be used to endorse or promote products
13.\" derived from this software without specific prior written permission.
14.\"
15.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.\" $OpenBSD: tsearch.3,v 1.2 1998/06/21 22:13:49 millert Exp $
27.\"
28.Dd June 15, 1997
29.Dt TSEARCH 3
30.Os
31.Sh NAME
32.Nm tsearch, tfind, tdelete, twalk
33.Nd manipulate binary search trees
34.Sh SYNOPSIS
35.Fd #include <search.h>
36.Ft void *
37.Fn tdelete "const void *key" "void **rootp", "int (*compar) (const void *, const void *)"
38.Ft void *
39.Fn tfind "const void *key" "void * const *rootp", "int (*compar) (const void *, const void *)"
40.Ft void *
41.Fn tsearch "const void *key" "void **rootp", "int (*compar) (const void *, const void *)"
42.Ft void
43.Fn twalk "const void *root" "void (*compar) (const void *, VISIT, int)"
44.Sh DESCRIPTION
45The
46.Fn tdelete ,
47.Fn tfind ,
48.Fn tsearch ,
49and
50.Fn twalk
51functions manage binary search trees based on algorithms T and D
52from Knuth (6.2.2). The comparison function passed in by
53the user has the same style of return values as
54.Xr strcmp 3 .
55.Pp
56.Fn Tfind
57searches for the datum matched by the argument
58.Fa key
59in the binary tree rooted at
60.Fa rootp ,
61returning a pointer to the datum if it is found and NULL
62if it is not.
63.Pp
64.Fn Tsearch
65is identical to
66.Fn tfind
67except that if no match is found,
68.Fa key
69is inserted into the tree and a pointer to it is returned. If
70.Fa rootp
71points to a NULL value a new binary search tree is created.
72.Pp
73.Fn Tdelete
74deletes a node from the specified binary search tree and returns
75a pointer to the parent of the node to be deleted.
76It takes the same arguments as
77.Fn tfind
78and
79.Fn tsearch .
80If the node to be deleted is the root of the binary search tree,
81.Fa rootp
82will be adjusted.
83.Pp
84.Fn Twalk
85walks the binary search tree rooted in
86.fa root
87and calls the function
88.Fa action
89on each node.
90.Fa Action
91is called with three arguments: a pointer to the current node,
92a value from the enum
93.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
94specifying the traversal type, and a node level (where level
95zero is the root of the tree).
96.Sh SEE ALSO
97.Xr bsearch 3 ,
98.Xr hsearch 3 ,
99.Xr lsearch 3
100.Sh RETURN VALUES
101The
102.Fn tsearch
103function returns NULL if allocation of a new node fails (usually
104due to a lack of free memory).
105.Pp
106.Fn Tfind ,
107.Fn tsearch ,
108and
109.Fn tdelete
110return NULL if
111.Fa rootp
112is NULL or the datum cannot be found.
113.Pp
114The
115.Fn twalk
116function returns no value.
diff --git a/src/lib/libc/stdlib/tsearch.c b/src/lib/libc/stdlib/tsearch.c
new file mode 100644
index 0000000000..562ace1845
--- /dev/null
+++ b/src/lib/libc/stdlib/tsearch.c
@@ -0,0 +1,126 @@
1/*
2 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3 * the AT&T man page says.
4 *
5 * The node_t structure is for internal use only, lint doesn't grok it.
6 *
7 * Written by reading the System V Interface Definition, not the code.
8 *
9 * Totally public domain.
10 */
11/*LINTLIBRARY*/
12
13#include <search.h>
14#include <stdlib.h>
15
16typedef struct node_t {
17 char *key;
18 struct node_t *left, *right;
19} node;
20
21/* find or insert datum into search tree */
22void *
23tsearch(vkey, vrootp, compar)
24 const void *vkey; /* key to be located */
25 void **vrootp; /* address of tree root */
26 int (*compar) __P((const void *, const void *));
27{
28 register node *q;
29 char *key = (char *)vkey;
30 node **rootp = (node **)vrootp;
31
32 if (rootp == (struct node_t **)0)
33 return ((void *)0);
34 while (*rootp != (struct node_t *)0) { /* Knuth's T1: */
35 int r;
36
37 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
38 return ((void *)*rootp); /* we found it! */
39 rootp = (r < 0) ?
40 &(*rootp)->left : /* T3: follow left branch */
41 &(*rootp)->right; /* T4: follow right branch */
42 }
43 q = (node *) malloc(sizeof(node)); /* T5: key not found */
44 if (q != (struct node_t *)0) { /* make new node */
45 *rootp = q; /* link new node to old */
46 q->key = key; /* initialize new node */
47 q->left = q->right = (struct node_t *)0;
48 }
49 return ((void *)q);
50}
51
52/* delete node with given key */
53void *
54tdelete(vkey, vrootp, compar)
55 const void *vkey; /* key to be deleted */
56 void **vrootp; /* address of the root of tree */
57 int (*compar) __P((const void *, const void *));
58{
59 node **rootp = (node **)vrootp;
60 char *key = (char *)vkey;
61 node *p;
62 register node *q;
63 register node *r;
64 int cmp;
65
66 if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0)
67 return ((struct node_t *)0);
68 while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
69 p = *rootp;
70 rootp = (cmp < 0) ?
71 &(*rootp)->left : /* follow left branch */
72 &(*rootp)->right; /* follow right branch */
73 if (*rootp == (struct node_t *)0)
74 return ((void *)0); /* key not found */
75 }
76 r = (*rootp)->right; /* D1: */
77 if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
78 q = r;
79 else if (r != (struct node_t *)0) { /* Right link is null? */
80 if (r->left == (struct node_t *)0) { /* D2: Find successor */
81 r->left = q;
82 q = r;
83 } else { /* D3: Find (struct node_t *)0 link */
84 for (q = r->left; q->left != (struct node_t *)0; q = r->left)
85 r = q;
86 r->left = q->right;
87 q->left = (*rootp)->left;
88 q->right = (*rootp)->right;
89 }
90 }
91 free((struct node_t *) *rootp); /* D4: Free node */
92 *rootp = q; /* link parent to new node */
93 return(p);
94}
95
96/* Walk the nodes of a tree */
97static void
98trecurse(root, action, level)
99 register node *root; /* Root of the tree to be walked */
100 register void (*action)(); /* Function to be called at each node */
101 register int level;
102{
103 if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
104 (*action)(root, leaf, level);
105 else {
106 (*action)(root, preorder, level);
107 if (root->left != (struct node_t *)0)
108 trecurse(root->left, action, level + 1);
109 (*action)(root, postorder, level);
110 if (root->right != (struct node_t *)0)
111 trecurse(root->right, action, level + 1);
112 (*action)(root, endorder, level);
113 }
114}
115
116/* Walk the nodes of a tree */
117void
118twalk(vroot, action)
119 const void *vroot; /* Root of the tree to be walked */
120 void (*action) __P((const void *, VISIT, int));
121{
122 node *root = (node *)vroot;
123
124 if (root != (node *)0 && action != (void(*)())0)
125 trecurse(root, action, 0);
126}
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 2b7ce63a63..076db78945 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,11 +1,10 @@
1# from: @(#)Makefile.inc 5.6 (Berkeley) 3/5/91 1# $OpenBSD: Makefile.inc,v 1.5 1998/07/01 01:29:44 millert Exp $
2# $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
3 2
4# string sources 3# string sources
5.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/string ${.CURDIR}/string 4.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/string ${.CURDIR}/string
6 5
7SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ 6SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \
8 strftime.c strmode.c strsignal.c strtok.c strxfrm.c \ 7 strlcat.c strlcpy.c strmode.c strsignal.c strtok.c strxfrm.c \
9 __strerror.c __strsignal.c 8 __strerror.c __strsignal.c
10 9
11# machine-dependent net sources 10# machine-dependent net sources
@@ -95,14 +94,35 @@ strrchr.so: rindex.c
95 -o ${.TARGET} 94 -o ${.TARGET}
96.endif 95.endif
97 96
97# build .ln files for memmove, memcpy, strchr and strrchr always from
98# bcopy, index, and rindex
99LOBJS+= memmove.ln memcpy.ln strchr.ln strrchr.ln
100
101memmove.ln: bcopy.c
102 lint ${LINTFLAGS} -DMEMMOVE ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
103 ${.CURDIR}/string/bcopy.c
104
105memcpy.ln: bcopy.c
106 lint ${LINTFLAGS} -DMEMCOPY ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
107 ${.CURDIR}/string/bcopy.c
108
109strchr.ln: index.c
110 lint ${LINTFLAGS} -DSTRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
111 ${.CURDIR}/string/index.c
112
113strrchr.ln: rindex.c
114 lint ${LINTFLAGS} -DSTRRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
115 ${.CURDIR}/string/rindex.c
116
98MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \ 117MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \
99 memcmp.3 memcpy.3 memmove.3 memset.3 rindex.3 strcasecmp.3 strcat.3 \ 118 memcmp.3 memcpy.3 memmove.3 memset.3 rindex.3 strcasecmp.3 strcat.3 \
100 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 strftime.3 \ 119 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \
101 string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ 120 string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \
102 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 121 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3
103 122
104MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 123MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
105MLINKS+=strcasecmp.3 strncasecmp.3 124MLINKS+=strcasecmp.3 strncasecmp.3
106MLINKS+=strcat.3 strncat.3 125MLINKS+=strcat.3 strncat.3
107MLINKS+=strcmp.3 strncmp.3 126MLINKS+=strcmp.3 strncmp.3
108MLINKS+=strcpy.3 strncpy.3 127MLINKS+=strcpy.3 strncpy.3
128MLINKS+=strlcpy.3 strlcat.3
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c
index cd604906db..9c023f8a53 100644
--- a/src/lib/libc/string/__strerror.c
+++ b/src/lib/libc/string/__strerror.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ 35static char *rcsid = "$OpenBSD: __strerror.c,v 1.6 1996/09/25 08:17:30 deraadt Exp $";
36static char *rcsid = "$Id: __strerror.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#ifdef NLS 38#ifdef NLS
@@ -46,9 +45,26 @@ static char *rcsid = "$Id: __strerror.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Ex
46#define sys_errlist _sys_errlist 45#define sys_errlist _sys_errlist
47#define sys_nerr _sys_nerr 46#define sys_nerr _sys_nerr
48 47
48#include <errno.h>
49#include <limits.h>
49#include <stdio.h> 50#include <stdio.h>
50#include <string.h> 51#include <string.h>
51 52
53static char *itoa(num)
54 int num;
55{
56 static char buffer[11];
57 char *p;
58
59 p = buffer + 4;
60 while (num >= 10) {
61 *--p = (num % 10) + '0';
62 num /= 10;
63 }
64 *p = (num % 10) + '0';
65 return p;
66}
67
52/* 68/*
53 * Since perror() is not allowed to change the contents of strerror()'s 69 * Since perror() is not allowed to change the contents of strerror()'s
54 * static buffer, both functions supply their own buffers to the 70 * static buffer, both functions supply their own buffers to the
@@ -60,28 +76,31 @@ __strerror(num, buf)
60 int num; 76 int num;
61 char *buf; 77 char *buf;
62{ 78{
63#define UPREFIX "Unknown error: %u" 79#define UPREFIX "Unknown error: "
64 register unsigned int errnum; 80 register unsigned int errnum;
65 81
66#ifdef NLS 82#ifdef NLS
67 nl_catd catd ; 83 nl_catd catd;
68 catd = catopen("libc", 0); 84 catd = catopen("libc", 0);
69#endif 85#endif
70 86
71 errnum = num; /* convert to unsigned */ 87 errnum = num; /* convert to unsigned */
72 if (errnum < sys_nerr) { 88 if (errnum < sys_nerr) {
73#ifdef NLS 89#ifdef NLS
74 strcpy(buf, catgets(catd, 1, errnum, 90 strncpy(buf, catgets(catd, 1, errnum,
75 (char *)sys_errlist[errnum])); 91 (char *)sys_errlist[errnum]), NL_TEXTMAX-1);
92 buf[NL_TEXTMAX - 1] = '\0';
76#else 93#else
77 return(sys_errlist[errnum]); 94 return(sys_errlist[errnum]);
78#endif 95#endif
79 } else { 96 } else {
80#ifdef NLS 97#ifdef NLS
81 sprintf(buf, catgets(catd, 1, 0xffff, UPREFIX), errnum); 98 strncpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX-1);
99 buf[NL_TEXTMAX - 1] = '\0';
82#else 100#else
83 sprintf(buf, UPREFIX, errnum); 101 strcpy(buf, UPREFIX);
84#endif 102#endif
103 strncat(buf, itoa(errnum), NL_TEXTMAX-strlen(buf)-1);
85 } 104 }
86 105
87#ifdef NLS 106#ifdef NLS
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c
index 1937e2d608..ae0df72cd3 100644
--- a/src/lib/libc/string/__strsignal.c
+++ b/src/lib/libc/string/__strsignal.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ 35static char *rcsid = "$OpenBSD: __strsignal.c,v 1.5 1996/09/25 13:19:01 deraadt Exp $";
36static char *rcsid = "$Id: __strsignal.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#ifdef NLS 38#ifdef NLS
@@ -46,15 +45,31 @@ static char *rcsid = "$Id: __strsignal.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt E
46#define sys_siglist _sys_siglist 45#define sys_siglist _sys_siglist
47 46
48#include <stdio.h> 47#include <stdio.h>
48#include <limits.h>
49#include <signal.h> 49#include <signal.h>
50#include <string.h> 50#include <string.h>
51 51
52static char *itoa(num)
53 int num;
54{
55 static char buffer[11];
56 char *p;
57
58 p = buffer + 4;
59 while (num >= 10) {
60 *--p = (num % 10) + '0';
61 num /= 10;
62 }
63 *p = (num % 10) + '0';
64 return p;
65}
66
52char * 67char *
53__strsignal(num, buf) 68__strsignal(num, buf)
54 int num; 69 int num;
55 char *buf; 70 char *buf;
56{ 71{
57#define UPREFIX "Unknown signal: %u" 72#define UPREFIX "Unknown signal: "
58 register unsigned int signum; 73 register unsigned int signum;
59 74
60#ifdef NLS 75#ifdef NLS
@@ -65,17 +80,20 @@ __strsignal(num, buf)
65 signum = num; /* convert to unsigned */ 80 signum = num; /* convert to unsigned */
66 if (signum < NSIG) { 81 if (signum < NSIG) {
67#ifdef NLS 82#ifdef NLS
68 strcpy(buf, catgets(catd, 2, signum, 83 strncpy(buf, catgets(catd, 2, signum,
69 (char *)sys_siglist[signum])); 84 (char *)sys_siglist[signum]), NL_TEXTMAX-1);
85 buf[NL_TEXTMAX-1] = '\0';
70#else 86#else
71 return((char *)sys_siglist[signum]); 87 return((char *)sys_siglist[signum]);
72#endif 88#endif
73 } else { 89 } else {
74#ifdef NLS 90#ifdef NLS
75 sprintf(buf, catgets(catd, 1, 0xffff, UPREFIX), signum); 91 strncpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX-1);
92 buf[NL_TEXTMAX-1] = '\0';
76#else 93#else
77 sprintf(buf, UPREFIX, signum); 94 strcpy(buf, UPREFIX);
78#endif 95#endif
96 strncat(buf, itoa(signum), NL_TEXTMAX-strlen(buf)-1);
79 } 97 }
80 98
81#ifdef NLS 99#ifdef NLS
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3
index 118c55c579..9234b5739d 100644
--- a/src/lib/libc/string/bcmp.3
+++ b/src/lib/libc/string/bcmp.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)bcmp.3 5.4 (Berkeley) 4/19/91 34.\" $OpenBSD: bcmp.3,v 1.2 1996/08/19 08:33:56 tholo Exp $
35.\" $Id: bcmp.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt BCMP 3 37.Dt BCMP 3
diff --git a/src/lib/libc/string/bcmp.c b/src/lib/libc/string/bcmp.c
index 2cc38baee3..4ed00975a4 100644
--- a/src/lib/libc/string/bcmp.c
+++ b/src/lib/libc/string/bcmp.c
@@ -32,15 +32,19 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)bcmp.c 5.6 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: bcmp.c,v 1.4 1996/08/19 08:33:57 tholo Exp $";
36static char *rcsid = "$Id: bcmp.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41/* 44/*
42 * bcmp -- vax cmpc3 instruction 45 * bcmp -- vax cmpc3 instruction
43 */ 46 */
47int
44bcmp(b1, b2, length) 48bcmp(b1, b2, length)
45 const void *b1, *b2; 49 const void *b1, *b2;
46 register size_t length; 50 register size_t length;
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3
index 6db3812caf..4e841562e0 100644
--- a/src/lib/libc/string/bcopy.3
+++ b/src/lib/libc/string/bcopy.3
@@ -32,8 +32,7 @@
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE. 33.\" SUCH DAMAGE.
34.\" 34.\"
35.\" from: @(#)bcopy.3 5.3 (Berkeley) 4/19/91 35.\" $OpenBSD: bcopy.3,v 1.2 1996/08/19 08:33:57 tholo Exp $
36.\" $Id: bcopy.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
37.\" 36.\"
38.Dd April 19, 1991 37.Dd April 19, 1991
39.Dt BCOPY 3 38.Dt BCOPY 3
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c
index 92feed66ea..023a3b2db2 100644
--- a/src/lib/libc/string/bcopy.c
+++ b/src/lib/libc/string/bcopy.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)bcopy.c 5.11 (Berkeley) 6/21/91";*/ 38static char *rcsid = "$OpenBSD: bcopy.c,v 1.2 1996/08/19 08:33:58 tholo Exp $";
39static char *rcsid = "$Id: bcopy.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3
index 2264a6a1c4..c942930163 100644
--- a/src/lib/libc/string/bm.3
+++ b/src/lib/libc/string/bm.3
@@ -32,8 +32,7 @@
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE. 33.\" SUCH DAMAGE.
34.\" 34.\"
35.\" from: @(#)bm.3 8.4 (Berkeley) 6/21/94 35.\" $OpenBSD: bm.3,v 1.2 1996/08/19 08:33:58 tholo Exp $
36.\" $Id: bm.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $
37.\" 36.\"
38.TH BM 3 37.TH BM 3
39.SH NAME 38.SH NAME
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c
index 68eac22ecc..b191d340f6 100644
--- a/src/lib/libc/string/bm.c
+++ b/src/lib/libc/string/bm.c
@@ -34,10 +34,9 @@
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 */ 35 */
36 36
37#ifndef lint 37#if defined(LIBC_SCCS) && !defined(lint)
38/* from: static char sccsid[] = "@(#)bm.c 8.7 (Berkeley) 6/21/94"; */ 38static char *rcsid = "$OpenBSD: bm.c,v 1.3 1996/08/19 08:33:59 tholo Exp $";
39static char *rcsid = "$Id: bm.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; 39#endif /* LIBC_SCCS and not lint */
40#endif /* not lint */
41 40
42#include <sys/types.h> 41#include <sys/types.h>
43 42
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3
index 12fcfb0cc1..b553fd0beb 100644
--- a/src/lib/libc/string/bstring.3
+++ b/src/lib/libc/string/bstring.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)bstring.3 6.8 (Berkeley) 4/19/91 34.\" $OpenBSD: bstring.3,v 1.2 1996/08/19 08:33:59 tholo Exp $
35.\" $Id: bstring.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt BSTRING 3 37.Dt BSTRING 3
diff --git a/src/lib/libc/string/bzero.3 b/src/lib/libc/string/bzero.3
index 4f0141e051..a8e55a63c1 100644
--- a/src/lib/libc/string/bzero.3
+++ b/src/lib/libc/string/bzero.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)bzero.3 5.3 (Berkeley) 4/19/91 34.\" $OpenBSD: bzero.3,v 1.2 1996/08/19 08:34:00 tholo Exp $
35.\" $Id: bzero.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt BZERO 3 37.Dt BZERO 3
diff --git a/src/lib/libc/string/bzero.c b/src/lib/libc/string/bzero.c
index 4865e396ef..3e660a307f 100644
--- a/src/lib/libc/string/bzero.c
+++ b/src/lib/libc/string/bzero.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)bzero.c 5.7 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: bzero.c,v 1.3 1996/08/19 08:34:00 tholo Exp $";
36static char *rcsid = "$Id: bzero.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41/* 44/*
42 * bzero -- vax movc5 instruction 45 * bzero -- vax movc5 instruction
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3
index 6464bea2b1..9ef08aef8e 100644
--- a/src/lib/libc/string/ffs.3
+++ b/src/lib/libc/string/ffs.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)ffs.3 5.3 (Berkeley) 4/19/91 34.\" $OpenBSD: ffs.3,v 1.2 1996/08/19 08:34:01 tholo Exp $
35.\" $Id: ffs.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt FFS 3 37.Dt FFS 3
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c
index 42bc87ddea..a0767e50ad 100644
--- a/src/lib/libc/string/ffs.c
+++ b/src/lib/libc/string/ffs.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)ffs.c 5.4 (Berkeley) 5/17/90";*/ 35static char *rcsid = "$OpenBSD: ffs.c,v 1.3 1996/08/19 08:34:01 tholo Exp $";
36static char *rcsid = "$Id: ffs.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41/* 44/*
42 * ffs -- vax ffs instruction 45 * ffs -- vax ffs instruction
diff --git a/src/lib/libc/string/index.3 b/src/lib/libc/string/index.3
index 847b03628b..d236a73a48 100644
--- a/src/lib/libc/string/index.3
+++ b/src/lib/libc/string/index.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)index.3 5.3 (Berkeley) 4/19/91 34.\" $OpenBSD: index.3,v 1.4 1997/12/30 01:09:49 deraadt Exp $
35.\" $Id: index.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt INDEX 3 37.Dt INDEX 3
@@ -55,9 +54,9 @@ locates the first character matching
55in the null-terminated string 54in the null-terminated string
56.Fa s . 55.Fa s .
57.Sh RETURN VALUES 56.Sh RETURN VALUES
58The character 57If the character
59.Fa c 58.Fa c
60is returned if it is found; otherwise 59is found, a pointer to it is returned; otherwise
61.Dv NULL 60.Dv NULL
62is returned. 61is returned.
63If 62If
@@ -77,7 +76,7 @@ locates the terminating '\e0'.
77.Xr strstr 3 , 76.Xr strstr 3 ,
78.Xr strtok 3 77.Xr strtok 3
79.Sh HISTORY 78.Sh HISTORY
80A 79An
81.Fn index 80.Fn index
82function appeared in 81function appeared in
83.At v6 . 82.At v6 .
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c
index 3d9c05f961..86c4e75f12 100644
--- a/src/lib/libc/string/index.c
+++ b/src/lib/libc/string/index.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)index.c 5.7 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: index.c,v 1.2 1996/08/19 08:34:02 tholo Exp $";
36static char *rcsid = "$Id: index.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3
index 61df704028..c2e15b1405 100644
--- a/src/lib/libc/string/memccpy.3
+++ b/src/lib/libc/string/memccpy.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: memccpy.3,v 1.4 1998/06/15 17:55:09 mickey Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
@@ -29,14 +31,14 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
31.\" 33.\"
32.\" from: @(#)memccpy.3 5.4 (Berkeley) 4/19/91 34.\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93
33.\" $Id: memccpy.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
34.\" 35.\"
35.Dd April 19, 1991 36.Dd June 9, 1993
36.Dt MEMCCPY 3 37.Dt MEMCCPY 3
37.Os 38.Os
38.Sh NAME 39.Sh NAME
39.Nm memccpy 40.Nm memccpy
41.Nd copy string until character found
40.Sh SYNOPSIS 42.Sh SYNOPSIS
41.Fd #include <string.h> 43.Fd #include <string.h>
42.Ft void * 44.Ft void *
@@ -69,5 +71,5 @@ bytes are copied, and a NULL pointer is returned.
69.Sh HISTORY 71.Sh HISTORY
70The 72The
71.Fn memccpy 73.Fn memccpy
72function is 74function first appeared in
73.Ud . 75.Bx 4.4 .
diff --git a/src/lib/libc/string/memccpy.c b/src/lib/libc/string/memccpy.c
index 3a1d7bcca1..020e6e5679 100644
--- a/src/lib/libc/string/memccpy.c
+++ b/src/lib/libc/string/memccpy.c
@@ -1,6 +1,8 @@
1/* $OpenBSD: memccpy.c,v 1.3 1997/08/20 04:09:39 millert Exp $ */
2
1/*- 3/*-
2 * Copyright (c) 1990 The Regents of the University of California. 4 * Copyright (c) 1990, 1993
3 * All rights reserved. 5 * The Regents of the University of California. All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
@@ -32,8 +34,11 @@
32 */ 34 */
33 35
34#if defined(LIBC_SCCS) && !defined(lint) 36#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)memccpy.c 5.8 (Berkeley) 5/30/91";*/ 37#if 0
36static char *rcsid = "$Id: memccpy.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; 38static char sccsid[] = "@(#)memccpy.c 8.1 (Berkeley) 6/4/93";
39#else
40static char *rcsid = "$OpenBSD: memccpy.c,v 1.3 1997/08/20 04:09:39 millert Exp $";
41#endif
37#endif /* LIBC_SCCS and not lint */ 42#endif /* LIBC_SCCS and not lint */
38 43
39#include <string.h> 44#include <string.h>
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3
index 265711e3b5..56a2aee9ce 100644
--- a/src/lib/libc/string/memchr.3
+++ b/src/lib/libc/string/memchr.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)memchr.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: memchr.3,v 1.2 1996/08/19 08:34:04 tholo Exp $
37.\" $Id: memchr.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt MEMCHR 3 39.Dt MEMCHR 3
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c
index 61652c6bb1..2ebb5dab32 100644
--- a/src/lib/libc/string/memchr.c
+++ b/src/lib/libc/string/memchr.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)memchr.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: memchr.c,v 1.2 1996/08/19 08:34:04 tholo Exp $";
39static char *rcsid = "$Id: memchr.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3
index 13901c1009..34c5f60861 100644
--- a/src/lib/libc/string/memcmp.3
+++ b/src/lib/libc/string/memcmp.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)memcmp.3 5.5 (Berkeley) 6/29/91 36.\" $OpenBSD: memcmp.3,v 1.3 1996/12/10 09:06:11 deraadt Exp $
37.\" $Id: memcmp.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt MEMCMP 3 39.Dt MEMCMP 3
@@ -61,7 +60,7 @@ bytes long.
61The 60The
62.Fn memcmp 61.Fn memcmp
63function 62function
64returns zero if the the two strings are identical, 63returns zero if the two strings are identical,
65otherwise returns the difference between the first two differing bytes 64otherwise returns the difference between the first two differing bytes
66(treated as unsigned char values, so that 65(treated as unsigned char values, so that
67.Sq Li \e200 66.Sq Li \e200
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c
index 23d2ab2393..5ce33e2998 100644
--- a/src/lib/libc/string/memcmp.c
+++ b/src/lib/libc/string/memcmp.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)memcmp.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: memcmp.c,v 1.2 1996/08/19 08:34:05 tholo Exp $";
39static char *rcsid = "$Id: memcmp.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3
index 3f4bb643c9..75eb00b2d5 100644
--- a/src/lib/libc/string/memcpy.3
+++ b/src/lib/libc/string/memcpy.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)memcpy.3 5.5 (Berkeley) 6/29/91 36.\" $OpenBSD: memcpy.3,v 1.2 1996/08/19 08:34:06 tholo Exp $
37.\" $Id: memcpy.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt MEMCPY 3 39.Dt MEMCPY 3
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3
index 24422e7971..95f6b7596c 100644
--- a/src/lib/libc/string/memmove.3
+++ b/src/lib/libc/string/memmove.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)memmove.3 5.5 (Berkeley) 6/29/91 36.\" $OpenBSD: memmove.3,v 1.2 1996/08/19 08:34:07 tholo Exp $
37.\" $Id: memmove.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt MEMMOVE 3 39.Dt MEMMOVE 3
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3
index 1afc052182..e1d8583732 100644
--- a/src/lib/libc/string/memset.3
+++ b/src/lib/libc/string/memset.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)memset.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: memset.3,v 1.3 1997/08/24 21:56:45 deraadt Exp $
37.\" $Id: memset.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt MEMSET 3 39.Dt MEMSET 3
@@ -56,6 +55,12 @@ bytes of value
56.Fa c 55.Fa c
57(converted to an unsigned char) to the string 56(converted to an unsigned char) to the string
58.Fa b . 57.Fa b .
58.Sh RETURN VALUES
59The
60.Fn memset
61function
62returns the original value of
63.Fa b .
59.Sh SEE ALSO 64.Sh SEE ALSO
60.Xr bzero 3 , 65.Xr bzero 3 ,
61.Xr swab 3 66.Xr swab 3
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c
index 117de2e80b..c3373a21a9 100644
--- a/src/lib/libc/string/memset.c
+++ b/src/lib/libc/string/memset.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)memset.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: memset.c,v 1.2 1996/08/19 08:34:07 tholo Exp $";
39static char *rcsid = "$Id: memset.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/rindex.3 b/src/lib/libc/string/rindex.3
index b13b3513e0..db7f8e1cd8 100644
--- a/src/lib/libc/string/rindex.3
+++ b/src/lib/libc/string/rindex.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)rindex.3 5.2 (Berkeley) 4/19/91 34.\" $OpenBSD: rindex.3,v 1.3 1997/12/30 01:09:49 deraadt Exp $
35.\" $Id: rindex.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt RINDEX 3 37.Dt RINDEX 3
@@ -55,7 +54,7 @@ matching
55.Em char ) 54.Em char )
56in the null-terminated string 55in the null-terminated string
57.Fa s . 56.Fa s .
58The character c is returned if it is found; otherwise NULL is returned. 57If the character c is found, a pointer to it is returned; otherwise NULL is returned.
59If 58If
60.Fa c 59.Fa c
61is 60is
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c
index 1b84c92072..f18553f667 100644
--- a/src/lib/libc/string/rindex.c
+++ b/src/lib/libc/string/rindex.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)rindex.c 5.9 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: rindex.c,v 1.2 1996/08/19 08:34:08 tholo Exp $";
36static char *rcsid = "$Id: rindex.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3
index 46e9010e4f..52a13055ee 100644
--- a/src/lib/libc/string/strcasecmp.3
+++ b/src/lib/libc/string/strcasecmp.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strcasecmp.3,v 1.4 1998/06/15 17:55:11 mickey Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" This code is derived from software contributed to Berkeley by 6.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek. 7.\" Chris Torek.
@@ -31,10 +33,9 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
33.\" 35.\"
34.\" from: @(#)strcasecmp.3 5.4 (Berkeley) 4/19/91 36.\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93
35.\" $Id: strcasecmp.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $
36.\" 37.\"
37.Dd April 19, 1991 38.Dd June 9, 1993
38.Dt STRCASECMP 3 39.Dt STRCASECMP 3
39.Os 40.Os
40.Sh NAME 41.Sh NAME
@@ -84,5 +85,5 @@ The
84.Fn strcasecmp 85.Fn strcasecmp
85and 86and
86.Fn strncasecmp 87.Fn strncasecmp
87functions are 88functions first appeared in
88.Ud . 89.Bx 4.4 .
diff --git a/src/lib/libc/string/strcasecmp.c b/src/lib/libc/string/strcasecmp.c
index 79bd0081e3..1f487524aa 100644
--- a/src/lib/libc/string/strcasecmp.c
+++ b/src/lib/libc/string/strcasecmp.c
@@ -1,6 +1,8 @@
1/* $OpenBSD: strcasecmp.c,v 1.3 1997/08/20 04:13:57 millert Exp $ */
2
1/* 3/*
2 * Copyright (c) 1987 Regents of the University of California. 4 * Copyright (c) 1987, 1993
3 * All rights reserved. 5 * The Regents of the University of California. All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
@@ -31,13 +33,16 @@
31 * SUCH DAMAGE. 33 * SUCH DAMAGE.
32 */ 34 */
33 35
36#include <string.h>
37
34#if defined(LIBC_SCCS) && !defined(lint) 38#if defined(LIBC_SCCS) && !defined(lint)
35/*static const char sccsid[] = "from: @(#)strcasecmp.c 5.10 (Berkeley) 1/26/91";*/ 39#if 0
36static char *rcsid = "$Id: strcasecmp.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; 40static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93";
41#else
42static char *rcsid = "$OpenBSD: strcasecmp.c,v 1.3 1997/08/20 04:13:57 millert Exp $";
43#endif
37#endif /* LIBC_SCCS and not lint */ 44#endif /* LIBC_SCCS and not lint */
38 45
39#include <string.h>
40
41typedef unsigned char u_char; 46typedef unsigned char u_char;
42 47
43/* 48/*
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
index 5357d65754..686afd3f2e 100644
--- a/src/lib/libc/string/strcat.3
+++ b/src/lib/libc/string/strcat.3
@@ -33,10 +33,9 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcat.3 5.6 (Berkeley) 6/29/91 36.\" $OpenBSD: strcat.3,v 1.3 1997/07/09 00:19:53 millert Exp $
37.\" $Id: strcat.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd July 8, 1997
40.Dt STRCAT 3 39.Dt STRCAT 3
41.Os 40.Os
42.Sh NAME 41.Sh NAME
@@ -69,7 +68,10 @@ The
69function 68function
70appends not more than 69appends not more than
71.Fa count 70.Fa count
72characters. 71characters where space for the terminating
72.Ql \e0
73should not be included in
74.Fa count .
73.Sh RETURN VALUES 75.Sh RETURN VALUES
74The 76The
75.Fn strcat 77.Fn strcat
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c
index e741b84f03..374a2b7464 100644
--- a/src/lib/libc/string/strcat.c
+++ b/src/lib/libc/string/strcat.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strcat.c 5.6 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: strcat.c,v 1.4 1996/08/19 08:34:10 tholo Exp $";
36static char *rcsid = "$Id: strcat.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41char * 44char *
42strcat(s, append) 45strcat(s, append)
@@ -46,6 +49,6 @@ strcat(s, append)
46 char *save = s; 49 char *save = s;
47 50
48 for (; *s; ++s); 51 for (; *s; ++s);
49 while (*s++ = *append++); 52 while ((*s++ = *append++) != '\0');
50 return(save); 53 return(save);
51} 54}
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3
index 18b50301f3..c3bc2f8817 100644
--- a/src/lib/libc/string/strchr.3
+++ b/src/lib/libc/string/strchr.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strchr.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strchr.3,v 1.3 1997/12/29 22:31:50 deraadt Exp $
37.\" $Id: strchr.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCHR 3 39.Dt STRCHR 3
@@ -54,7 +53,7 @@ function locates the first occurrence of
54in the string pointed to by 53in the string pointed to by
55.Ar s . 54.Ar s .
56The terminating 55The terminating
57.Dv NULL 56.Dv NUL
58character is considered part of the string. 57character is considered part of the string.
59If 58If
60.Fa c 59.Fa c
diff --git a/src/lib/libc/string/strcmp.3 b/src/lib/libc/string/strcmp.3
index fecaa85410..91e51d68fc 100644
--- a/src/lib/libc/string/strcmp.3
+++ b/src/lib/libc/string/strcmp.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcmp.3 5.6 (Berkeley) 6/29/91 36.\" $OpenBSD: strcmp.3,v 1.2 1996/08/19 08:34:11 tholo Exp $
37.\" $Id: strcmp.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCMP 3 39.Dt STRCMP 3
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c
index ae19e2e26e..9a5b208323 100644
--- a/src/lib/libc/string/strcmp.c
+++ b/src/lib/libc/string/strcmp.c
@@ -35,11 +35,14 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strcmp.c 5.5 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strcmp.c,v 1.3 1996/08/19 08:34:12 tholo Exp $";
39static char *rcsid = "$Id: strcmp.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
41#ifndef _KERNEL
42#include <string.h> 42#include <string.h>
43#else
44#include <lib/libkern/libkern.h>
45#endif
43 46
44/* 47/*
45 * Compare strings. 48 * Compare strings.
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3
index 12f73f98f2..20af998885 100644
--- a/src/lib/libc/string/strcoll.3
+++ b/src/lib/libc/string/strcoll.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcoll.3 5.6 (Berkeley) 6/29/91 36.\" $OpenBSD: strcoll.3,v 1.2 1996/08/19 08:34:12 tholo Exp $
37.\" $Id: strcoll.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCOLL 3 39.Dt STRCOLL 3
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c
index 86c742cba9..dca0b10d25 100644
--- a/src/lib/libc/string/strcoll.c
+++ b/src/lib/libc/string/strcoll.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strcoll.c 5.2 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strcoll.c,v 1.2 1996/08/19 08:34:13 tholo Exp $";
39static char *rcsid = "$Id: strcoll.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
index 1ca12c2707..33da6e619e 100644
--- a/src/lib/libc/string/strcpy.3
+++ b/src/lib/libc/string/strcpy.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcpy.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strcpy.3,v 1.2 1996/08/19 08:34:13 tholo Exp $
37.\" $Id: strcpy.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCPY 3 39.Dt STRCPY 3
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c
index 669bfde23e..76b063fc10 100644
--- a/src/lib/libc/string/strcpy.c
+++ b/src/lib/libc/string/strcpy.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strcpy.c 5.7 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: strcpy.c,v 1.4 1996/08/19 08:34:14 tholo Exp $";
36static char *rcsid = "$Id: strcpy.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41char * 44char *
42strcpy(to, from) 45strcpy(to, from)
@@ -45,6 +48,6 @@ strcpy(to, from)
45{ 48{
46 char *save = to; 49 char *save = to;
47 50
48 for (; *to = *from; ++from, ++to); 51 for (; (*to = *from) != '\0'; ++from, ++to);
49 return(save); 52 return(save);
50} 53}
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3
index cc9e5c2fe3..93c6d8f84c 100644
--- a/src/lib/libc/string/strcspn.3
+++ b/src/lib/libc/string/strcspn.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strcspn.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: strcspn.3,v 1.2 1996/08/19 08:34:14 tholo Exp $
37.\" $Id: strcspn.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRCSPN 3 39.Dt STRCSPN 3
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c
index acb4d2a3af..f7261564a7 100644
--- a/src/lib/libc/string/strcspn.c
+++ b/src/lib/libc/string/strcspn.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strcspn.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strcspn.c,v 1.2 1996/08/19 08:34:15 tholo Exp $";
39static char *rcsid = "$Id: strcspn.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
index 925cbf3d46..6abbba57cb 100644
--- a/src/lib/libc/string/strdup.3
+++ b/src/lib/libc/string/strdup.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strdup.3,v 1.6 1998/08/19 05:51:14 pjanzen Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
@@ -29,10 +31,9 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
31.\" 33.\"
32.\" from: @(#)strdup.3 5.3 (Berkeley) 4/19/91 34.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
33.\" $Id: strdup.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
34.\" 35.\"
35.Dd April 19, 1991 36.Dd June 9, 1993
36.Dt STRDUP 3 37.Dt STRDUP 3
37.Os 38.Os
38.Sh NAME 39.Sh NAME
@@ -53,13 +54,15 @@ does the copy, and returns a pointer to it.
53The pointer may subsequently be used as an 54The pointer may subsequently be used as an
54argument to the function 55argument to the function
55.Xr free 3 . 56.Xr free 3 .
57.Pp
58If insufficient memory is available, NULL is returned.
56.Sh SEE ALSO 59.Sh SEE ALSO
57.Xr free 3 , 60.Xr free 3 ,
58.Xr malloc 3 , 61.Xr malloc 3 ,
59.Xt strcpy 3 , 62.Xr strcpy 3 ,
60.Xt strlen 3 63.Xr strlen 3
61.Sh HISTORY 64.Sh HISTORY
62The 65The
63.Fn strdup 66.Fn strdup
64function 67function first appeared in
65.Ud . 68.Bx 4.4 .
diff --git a/src/lib/libc/string/strdup.c b/src/lib/libc/string/strdup.c
index 27ede44110..be7f7ad094 100644
--- a/src/lib/libc/string/strdup.c
+++ b/src/lib/libc/string/strdup.c
@@ -1,6 +1,8 @@
1/* $OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert Exp $ */
2
1/* 3/*
2 * Copyright (c) 1988 The Regents of the University of California. 4 * Copyright (c) 1988, 1993
3 * All rights reserved. 5 * The Regents of the University of California. All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
@@ -32,10 +34,16 @@
32 */ 34 */
33 35
34#if defined(LIBC_SCCS) && !defined(lint) 36#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strdup.c 5.4 (Berkeley) 2/24/91";*/ 37#if 0
36static char *rcsid = "$Id: strdup.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; 38static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93";
39#else
40static char *rcsid = "$OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert Exp $";
41#endif
37#endif /* LIBC_SCCS and not lint */ 42#endif /* LIBC_SCCS and not lint */
38 43
44#include <sys/types.h>
45
46#include <stddef.h>
39#include <stdlib.h> 47#include <stdlib.h>
40#include <string.h> 48#include <string.h>
41 49
@@ -43,12 +51,12 @@ char *
43strdup(str) 51strdup(str)
44 const char *str; 52 const char *str;
45{ 53{
46 size_t len; 54 size_t siz;
47 char *copy; 55 char *copy;
48 56
49 len = strlen(str) + 1; 57 siz = strlen(str) + 1;
50 if (!(copy = malloc(len))) 58 if ((copy = malloc(siz)) == NULL)
51 return((char *)NULL); 59 return(NULL);
52 memcpy(copy, str, len); 60 (void)memcpy(copy, str, siz);
53 return(copy); 61 return(copy);
54} 62}
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3
index c9d8504dbb..487c2b0e9f 100644
--- a/src/lib/libc/string/strerror.3
+++ b/src/lib/libc/string/strerror.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strerror.3 6.9 (Berkeley) 6/29/91 36.\" $OpenBSD: strerror.3,v 1.2 1996/08/19 08:34:16 tholo Exp $
37.\" $Id: strerror.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRERROR 3 39.Dt STRERROR 3
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c
index c3f5ab5d98..0e2690c3dd 100644
--- a/src/lib/libc/string/strerror.c
+++ b/src/lib/libc/string/strerror.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ 35static char *rcsid = "$OpenBSD: strerror.c,v 1.2 1996/08/19 08:34:17 tholo Exp $";
36static char *rcsid = "$Id: strerror.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strftime.3 b/src/lib/libc/string/strftime.3
deleted file mode 100644
index f14db4bb13..0000000000
--- a/src/lib/libc/string/strftime.3
+++ /dev/null
@@ -1,202 +0,0 @@
1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" from: @(#)strftime.3 5.12 (Berkeley) 6/29/91
37.\" $Id: strftime.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\"
39.Dd June 29, 1991
40.Dt STRFTIME 3
41.Os
42.Sh NAME
43.Nm strftime
44.Nd format date and time
45.Sh SYNOPSIS
46.Fd #include <time.h>
47.Ft size_t
48.Fn strftime "char *buf" "size_t maxsize" "const char *format" "const struct tm *timeptr"
49.Sh DESCRIPTION
50The
51.Fn strftime
52function formats the information from
53.Fa timeptr
54into the buffer
55.Fa buf
56according to the string pointed to by
57.Fa format .
58.Pp
59The
60.Fa format
61string consists of zero or more conversion specifications and
62ordinary characters.
63All ordinary characters are copied directly into the buffer.
64A conversion specification consists of a percent sign
65.Ql %
66and one other character.
67.Pp
68No more than
69.Fa maxsize
70characters will be placed into the array.
71If the total number of resulting characters, including the terminating
72null character, is not more than
73.Fa maxsize ,
74.Fn strftime
75returns the number of characters in the array, not counting the
76terminating null.
77Otherwise, zero is returned.
78.Pp
79Each conversion specification is replaced by the characters as
80follows which are then copied into the buffer.
81.Bl -tag -width "xxxx"
82.It Cm \&%A
83is replaced by the locale's full weekday name.
84.It Cm %a
85is replaced by the locale's abbreviated weekday name.
86.It Cm \&%B
87is replaced by the locale's full month name.
88.It Cm \&%b No or Cm \&%h
89is replaced by the locale's abbreviated month name.
90.It Cm \&%C
91is replaced by the century (a year divided by 100 and truncated to an integer)
92as a decimal number (00-99).
93.It Cm \&%c
94is replaced by the locale's appropriate date and time representation.
95.It Cm \&%D
96is replaced by the date in the format
97.Dq Li %m/%d/%y .
98.It Cm \&%d
99is replaced by the day of the month as a decimal number (01-31).
100.It Cm \&%e
101is replaced by the day of month as a decimal number (1-31);
102single digits are preceded by a blank.
103.It Cm \&%H
104is replaced by the hour (24-hour clock) as a decimal number (00-23).
105.It Cm \&%I
106is replaced by the hour (12-hour clock) as a decimal number (01-12).
107.It Cm \&%j
108is replaced by the day of the year as a decimal number (001-366).
109.It Cm \&%k
110is replaced by the hour (24-hour clock) as a decimal number (0-23);
111single digits are preceded by a blank.
112.It Cm \&%l
113is replaced by the hour (12-hour clock) as a decimal number (1-12);
114single digits are preceded by a blank.
115.It Cm \&%M
116is replaced by the minute as a decimal number (00-59).
117.It Cm %m
118is replaced by the month as a decimal number (01-12).
119.It Cm %n
120is replaced by a newline.
121.It Cm %p
122is replaced by the locale's equivalent of either
123.Dq Tn AM
124or
125.Dq Tn PM .
126.It Cm \&%R
127is replaced by the time in the format
128.Dq Li %H:%M .
129.It Cm \&%r
130is replaced by the locale's representation of 12-hour clock time
131using AM/PM notation.
132.It Cm \&%T
133is replaced by the time in the format
134.Dq Li %H:%M:%S .
135.It Cm \&%t
136is replaced by a tab.
137.It Cm \&%S
138is replaced by the second as a decimal number (00-60).
139.It Cm %s
140is replaced by the number of seconds since the Epoch, UCT (see
141.Xr mktime 3 ) .
142.It Cm \&%U
143is replaced by the week number of the year (Sunday as the first day of
144the week) as a decimal number (00-53).
145.It Cm \&%u
146is replaced by the weekday (Monday as the first day of the week)
147as a decimal number (1-7).
148.It Cm \&%V
149is replaced by the week number of the year (Monday as the first day of
150the week) as a decimal number (01-53). If the week containing January
1511 has four or more days in the new year, then it is week 1; otherwise
152it is week 53 of the previous year, and the next week is week 1.
153.It Cm \&%W
154is replaced by the week number of the year (Monday as the first day of
155the week) as a decimal number (00-53).
156.It Cm \&%w
157is replaced by the weekday (Sunday as the first day of the week)
158as a decimal number (0-6).
159.It Cm \&%X
160is replaced by the locale's appropriate date representation.
161.It Cm \&%x
162is replaced by the locale's appropriate time representation.
163.It Cm \&%Y
164is replaced by the year with century as a decimal number.
165.It Cm \&%y
166is replaced by the year without century as a decimal number (00-99).
167.It Cm \&%Z
168is replaced by the time zone name.
169.It Cm %%
170is replaced by
171.Ql % .
172.El
173.Sh SEE ALSO
174.Xr date 1 ,
175.Xr ctime 3 ,
176.Xr printf 1 ,
177.Xr printf 3
178.Sh STANDARDS
179The
180.Fn strftime
181function
182conforms to
183.St -ansiC .
184The
185.Ql \&%C ,
186.Ql \&%D ,
187.Ql \&%e ,
188.Ql \&%h ,
189.Ql \&%k ,
190.Ql \&%l ,
191.Ql \&%n ,
192.Ql \&%r ,
193.Ql \&%R ,
194.Ql \&%s .
195.Ql \&%t ,
196.Ql \&%T ,
197.Ql \&%u ,
198and
199.Ql \&%V
200conversion specifications are extensions.
201.Sh BUGS
202There is no conversion specification for the phase of the moon.
diff --git a/src/lib/libc/string/strftime.c b/src/lib/libc/string/strftime.c
deleted file mode 100644
index fffa9ecbb0..0000000000
--- a/src/lib/libc/string/strftime.c
+++ /dev/null
@@ -1,317 +0,0 @@
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * 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
34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strftime.c 5.11 (Berkeley) 2/24/91";*/
36static char *rcsid = "$Id: strftime.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */
38
39#include <sys/localedef.h>
40#include <locale.h>
41#include <string.h>
42#include <tzfile.h>
43#include <time.h>
44
45static size_t gsize;
46static char *pt;
47static int _add(), _conv(), _secs();
48static size_t _fmt();
49
50size_t
51strftime(s, maxsize, format, t)
52 char *s;
53 size_t maxsize;
54 const char *format;
55 const struct tm *t;
56{
57 tzset();
58
59 pt = s;
60 if ((gsize = maxsize) < 1)
61 return(0);
62 if (_fmt(format, t)) {
63 *pt = '\0';
64 return(maxsize - gsize);
65 }
66 return(0);
67}
68
69#define SUN_WEEK(t) (((t)->tm_yday + 7 - \
70 ((t)->tm_wday)) / 7)
71#define MON_WEEK(t) (((t)->tm_yday + 7 - \
72 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
73static size_t
74_fmt(format, t)
75 register char *format;
76 struct tm *t;
77{
78 for (; *format; ++format) {
79 if (*format == '%') {
80 ++format;
81 if (*format == 'E') {
82 /* Alternate Era */
83 ++format;
84 } else if (*format == 'O') {
85 /* Alternate numeric symbols */
86 ++format;
87 }
88 switch(*format) {
89 case '\0':
90 --format;
91 break;
92 case 'A':
93 if (t->tm_wday < 0 || t->tm_wday > 6)
94 return(0);
95 if (!_add(_CurrentTimeLocale->day[t->tm_wday]))
96 return(0);
97 continue;
98 case 'a':
99 if (t->tm_wday < 0 || t->tm_wday > 6)
100 return(0);
101 if (!_add(_CurrentTimeLocale->abday[t->tm_wday]))
102 return(0);
103 continue;
104 case 'B':
105 if (t->tm_mon < 0 || t->tm_mon > 11)
106 return(0);
107 if (!_add(_CurrentTimeLocale->mon[t->tm_mon]))
108 return(0);
109 continue;
110 case 'b':
111 case 'h':
112 if (t->tm_mon < 0 || t->tm_mon > 11)
113 return(0);
114 if (!_add(_CurrentTimeLocale->abmon[t->tm_mon]))
115 return(0);
116 continue;
117 case 'C':
118 if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
119 2, '0'))
120 return(0);
121 continue;
122 case 'c':
123 if (!_fmt(_CurrentTimeLocale->d_t_fmt, t))
124 return(0);
125 continue;
126 case 'D':
127 if (!_fmt("%m/%d/%y", t))
128 return(0);
129 continue;
130 case 'd':
131 if (!_conv(t->tm_mday, 2, '0'))
132 return(0);
133 continue;
134 case 'e':
135 if (!_conv(t->tm_mday, 2, ' '))
136 return(0);
137 continue;
138 case 'H':
139 if (!_conv(t->tm_hour, 2, '0'))
140 return(0);
141 continue;
142 case 'I':
143 if (!_conv(t->tm_hour % 12 ?
144 t->tm_hour % 12 : 12, 2, '0'))
145 return(0);
146 continue;
147 case 'j':
148 if (!_conv(t->tm_yday + 1, 3, '0'))
149 return(0);
150 continue;
151 case 'k':
152 if (!_conv(t->tm_hour, 2, ' '))
153 return(0);
154 continue;
155 case 'l':
156 if (!_conv(t->tm_hour % 12 ?
157 t->tm_hour % 12: 12, 2, ' '))
158 return(0);
159 continue;
160 case 'M':
161 if (!_conv(t->tm_min, 2, '0'))
162 return(0);
163 continue;
164 case 'm':
165 if (!_conv(t->tm_mon + 1, 2, '0'))
166 return(0);
167 continue;
168 case 'n':
169 if (!_add("\n"))
170 return(0);
171 continue;
172 case 'p':
173 if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour >= 12]))
174 return(0);
175 continue;
176 case 'R':
177 if (!_fmt("%H:%M", t))
178 return(0);
179 continue;
180 case 'r':
181 if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t))
182 return(0);
183 continue;
184 case 'S':
185 if (!_conv(t->tm_sec, 2, '0'))
186 return(0);
187 continue;
188 case 's':
189 if (!_secs(t))
190 return(0);
191 continue;
192 case 'T':
193 if (!_fmt("%H:%M:%S", t))
194 return(0);
195 continue;
196 case 't':
197 if (!_add("\t"))
198 return(0);
199 continue;
200 case 'U':
201 if (!_conv(SUN_WEEK(t), 2, '0'))
202 return(0);
203 continue;
204 case 'u':
205 if (!_conv(t->tm_wday ? t->tm_wday : 7, 2, '0'))
206 return(0);
207 continue;
208 case 'V':
209 {
210 /* ISO 8601 Week Of Year:
211 If the week (Monday - Sunday) containing
212 January 1 has four or more days in the new
213 year, then it is week 1; otherwise it is
214 week 53 of the previous year and the next
215 week is week one. */
216
217 int week = MON_WEEK(t);
218
219 if (((t->tm_yday + 7 - (t->tm_wday + 1)) % 7) >= 4) {
220 week++;
221 } else if (week == 0) {
222 week = 53;
223 }
224
225 if (!_conv(week, 2, '0'))
226 return(0);
227 continue;
228 }
229 case 'W':
230 if (!_conv(MON_WEEK(t), 2, '0'))
231 return(0);
232 continue;
233 case 'w':
234 if (!_conv(t->tm_wday, 1, '0'))
235 return(0);
236 continue;
237 case 'x':
238 if (!_fmt(_CurrentTimeLocale->d_fmt, t))
239 return(0);
240 continue;
241 case 'X':
242 if (!_fmt(_CurrentTimeLocale->t_fmt, t))
243 return(0);
244 continue;
245 case 'y':
246 if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
247 2, '0'))
248 return(0);
249 continue;
250 case 'Y':
251 if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0'))
252 return(0);
253 continue;
254 case 'Z':
255 if (t->tm_zone && !_add(t->tm_zone))
256 return(0);
257 continue;
258 case '%':
259 /*
260 * X311J/88-090 (4.12.3.5): if conversion char is
261 * undefined, behavior is undefined. Print out the
262 * character itself as printf(3) does.
263 */
264 default:
265 break;
266 }
267 }
268 if (!gsize--)
269 return(0);
270 *pt++ = *format;
271 }
272 return(gsize);
273}
274
275static
276_secs(t)
277 struct tm *t;
278{
279 static char buf[15];
280 register time_t s;
281 register char *p;
282 struct tm tmp;
283
284 /* Make a copy, mktime(3) modifies the tm struct. */
285 tmp = *t;
286 s = mktime(&tmp);
287 for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
288 *p-- = s % 10 + '0';
289 return(_add(++p));
290}
291
292static
293_conv(n, digits, pad)
294 int n, digits;
295 char pad;
296{
297 static char buf[10];
298 register char *p;
299
300 for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
301 *p-- = n % 10 + '0';
302 while (p > buf && digits-- > 0)
303 *p-- = pad;
304 return(_add(++p));
305}
306
307static
308_add(str)
309 register char *str;
310{
311 for (;; ++pt, --gsize) {
312 if (!gsize)
313 return(0);
314 if (!(*pt = *str++))
315 return(1);
316 }
317}
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3
index aaf97e0321..323d4f3040 100644
--- a/src/lib/libc/string/string.3
+++ b/src/lib/libc/string/string.3
@@ -31,8 +31,7 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
33.\" 33.\"
34.\" from: @(#)string.3 6.9 (Berkeley) 4/19/91 34.\" $OpenBSD: string.3,v 1.3 1998/09/06 22:23:18 aaron Exp $
35.\" $Id: string.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
36.\" 35.\"
37.Dd April 19, 1991 36.Dd April 19, 1991
38.Dt STRING 3 37.Dt STRING 3
@@ -103,7 +102,7 @@
103.Fn rindex "const char *s" "int c" 102.Fn rindex "const char *s" "int c"
104.Sh DESCRIPTION 103.Sh DESCRIPTION
105The string functions 104The string functions
106functions manipulate strings terminated by a 105manipulate strings terminated by a
107null byte. 106null byte.
108.Pp 107.Pp
109See the specific manual pages for more information. 108See the specific manual pages for more information.
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c
new file mode 100644
index 0000000000..2e8c56926e
--- /dev/null
+++ b/src/lib/libc/string/strlcat.c
@@ -0,0 +1,71 @@
1/* $OpenBSD: strlcat.c,v 1.1 1998/07/01 01:29:45 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strlcat.c,v 1.1 1998/07/01 01:29:45 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <string.h>
36
37/*
38 * Appends src to string dst of size siz (unlike strncat, siz is the
39 * full size of dst, not space left). At most siz-1 characters
40 * will be copied. Always NUL terminates (unless siz == 0).
41 * Returns strlen(src); if retval >= siz, truncation occurred.
42 */
43size_t strlcat(dst, src, siz)
44 char *dst;
45 const char *src;
46 size_t siz;
47{
48 register char *d = dst;
49 register const char *s = src;
50 register size_t n = siz;
51 size_t dlen;
52
53 /* Find the end of dst and adjust bytes left */
54 while (*d != '\0' && n != 0)
55 d++;
56 dlen = d - dst;
57 n -= dlen;
58
59 if (n == 0)
60 return(dlen + strlen(s));
61 while (*s != '\0') {
62 if (n != 1) {
63 *d++ = *s;
64 n--;
65 }
66 s++;
67 }
68 *d = '\0';
69
70 return(dlen + (s - src)); /* count does not include NUL */
71}
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3
new file mode 100644
index 0000000000..5ce444d6fa
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.3
@@ -0,0 +1,140 @@
1.\" $OpenBSD: strlcpy.3,v 1.2 1998/07/06 19:17:21 millert Exp $
2.\"
3.\" Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\" derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd June 22, 1998
29.Dt STRLCPY 3
30.Os
31.Sh NAME
32.Nm strlcpy,
33.Nm strlcat
34.Nd size-bounded string copying and concatenation
35.Sh SYNOPSIS
36.Fd #include <string.h>
37.Ft size_t
38.Fn strlcpy "char *dst" "const char *src" "size_t size"
39.Ft size_t
40.Fn strlcat "char *dst" "const char *src" "size_t size"
41.Sh DESCRIPTION
42The
43.Fn strlcpy
44and
45.Fn strlcat
46functions copy and concatenate strings respectively. They are designed
47to be safer, more consistent, and less error prone replacements for
48.Xr strncpy 3
49and
50.Xr strncat 3 .
51Unlike those functions,
52.Fn strlcpy
53and
54.Fn strlcat
55take the full size of the buffer (not just the length) and guarantee to
56NUL-terminate the result (as long as
57.Fa size
58is larger than 0). Note that you should include a byte for the NUL in
59.Fa size .
60.Pp
61The
62.Fn strlcpy
63function copies up to
64.Fa size
65- 1 characters from the NUL-terminated string
66.Fa src
67to
68.Fa dst ,
69NUL-terminating the result.
70.Pp
71The
72.Fn strlcat
73function appends the NUL-terminated string
74.Fa src
75to the end of
76.Fa dst .
77It will append at most
78.Fa size
79- strlen(dst) - 1 bytes, NUL-terminating the result.
80.Sh RETURN VALUES
81The
82.Fn strlcpy
83and
84.Fn strlcat
85functions return the total length of the string they tried to
86create. For
87.Fn strlcpy
88that means the length of
89.Fa src .
90For
91.Fn strlcat
92that means the initial length of
93.Fa dst
94plus
95the length of
96.Fa src .
97While this may seem somewhat confusing it was done to make
98truncation detection simple.
99.Sh EXAMPLES
100The following code fragment illustrates the simple case:
101.Bd -literal -offset indent
102char *s, *p, buf[BUFSIZ];
103
104.Li ...
105
106(void)strlcpy(buf, s, sizeof(buf));
107(void)strlcat(buf, p, sizeof(buf));
108.Ed
109.Pp
110To detect truncation, perhaps while building a pathname, something
111like the following might be used:
112.Bd -literal -offset indent
113char *dir, *file, pname[MAXPATHNAMELEN];
114
115.Li ...
116
117if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
118 goto toolong;
119if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
120 goto toolong;
121.Ed
122.Pp
123Since we know how many characters we copied the first time, we can
124speed things up a bit by using a copy instead on an append:
125.Bd -literal -offset indent
126char *dir, *file, pname[MAXPATHNAMELEN];
127size_t n;
128
129.Li ...
130
131n = strlcpy(pname, dir, sizeof(pname));
132if (n >= sizeof(pname))
133 goto toolong;
134if (strlcpy(pname + n, file, sizeof(pname)) >= sizeof(pname) - n)
135 goto toolong;
136.Ed
137.Sh SEE ALSO
138.Xr snprintf 3 ,
139.Xr strncpy 3 ,
140.Xr strncat 3
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c
new file mode 100644
index 0000000000..1a60445599
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.c
@@ -0,0 +1,63 @@
1/* $OpenBSD: strlcpy.c,v 1.1 1998/07/01 01:29:45 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strlcpy.c,v 1.1 1998/07/01 01:29:45 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <string.h>
36
37/*
38 * Copy src to string dst of size siz. At most siz-1 characters
39 * will be copied. Always NUL terminates (unless siz == 0).
40 * Returns strlen(src); if retval >= siz, truncation occurred.
41 */
42size_t strlcpy(dst, src, siz)
43 char *dst;
44 char *src;
45 size_t siz;
46{
47 register char *d = dst;
48 register const char *s = src;
49 register size_t n = siz;
50
51 if (n == 0)
52 return(strlen(s));
53 while (*s != '\0') {
54 if (n != 1) {
55 *d++ = *s;
56 n--;
57 }
58 s++;
59 }
60 *d = '\0';
61
62 return(s - src); /* count does not include NUL */
63}
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3
index f4aff363ee..99e7dd19c3 100644
--- a/src/lib/libc/string/strlen.3
+++ b/src/lib/libc/string/strlen.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strlen.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strlen.3,v 1.2 1996/08/19 08:34:19 tholo Exp $
37.\" $Id: strlen.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRLEN 3 39.Dt STRLEN 3
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c
index d23aadafc0..332d5766f9 100644
--- a/src/lib/libc/string/strlen.c
+++ b/src/lib/libc/string/strlen.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strlen.c 5.5 (Berkeley) 1/26/91";*/ 35static char *rcsid = "$OpenBSD: strlen.c,v 1.3 1996/08/19 08:34:19 tholo Exp $";
36static char *rcsid = "$Id: strlen.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41size_t 44size_t
42strlen(str) 45strlen(str)
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3
index 1907e7ab03..3db953704d 100644
--- a/src/lib/libc/string/strmode.3
+++ b/src/lib/libc/string/strmode.3
@@ -1,5 +1,7 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strmode.3,v 1.6 1998/06/15 17:55:13 mickey Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" Redistribution and use in source and binary forms, with or without 6.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions 7.\" modification, are permitted provided that the following conditions
@@ -29,10 +31,9 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 32.\" SUCH DAMAGE.
31.\" 33.\"
32.\" from: @(#)strmode.3 5.4 (Berkeley) 7/31/91 34.\" @(#)strmode.3 8.3 (Berkeley) 7/28/94
33.\" $Id: strmode.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $
34.\" 35.\"
35.Dd July 31, 1991 36.Dd July 28, 1994
36.Dt STRMODE 3 37.Dt STRMODE 3
37.Os 38.Os
38.Sh NAME 39.Sh NAME
@@ -53,7 +54,7 @@ converts a file
53into a symbolic string which is stored in the location referenced by 54into a symbolic string which is stored in the location referenced by
54.Fa bp . 55.Fa bp .
55This stored string is eleven characters in length plus a trailing 56This stored string is eleven characters in length plus a trailing
56.Dv NULL . 57.Dv NUL .
57.Pp 58.Pp
58The first character is the inode type, and will be one of the following: 59The first character is the inode type, and will be one of the following:
59.Pp 60.Pp
@@ -72,6 +73,8 @@ symbolic link
72fifo 73fifo
73.It s 74.It s
74socket 75socket
76.It w
77whiteout
75.It ? 78.It ?
76unknown inode type 79unknown inode type
77.El 80.El
@@ -98,28 +101,28 @@ The third character is the first of the following characters that apply:
98.Bl -tag -width xxxx 101.Bl -tag -width xxxx
99.It S 102.It S
100If the character is part of the owner permissions and the file is not 103If the character is part of the owner permissions and the file is not
101executable or the directory is not searchable, by the owner, and the 104executable or the directory is not searchable by the owner, and the
102set-user-id bit is set. 105set-user-id bit is set.
103.It S 106.It S
104If the character is part of the group permissions and the file is not 107If the character is part of the group permissions and the file is not
105executable or the directory is not searchable, by the group, and the 108executable or the directory is not searchable by the group, and the
106set-group-id bit is set. 109set-group-id bit is set.
107.It T 110.It T
108If the character is part of the other permissions and the file is not 111If the character is part of the other permissions and the file is not
109executable or the directory is not searchable, by others, and the ``sticky'' 112executable or the directory is not searchable by others, and the ``sticky''
110.Pq Dv S_ISVTX 113.Pq Dv S_ISVTX
111bit is set. 114bit is set.
112.It s 115.It s
113If the character is part of the owner permissions and the file is 116If the character is part of the owner permissions and the file is
114executable or the directory searchable, by the owner, and the set-user-id 117executable or the directory searchable by the owner, and the set-user-id
115bit is set. 118bit is set.
116.It s 119.It s
117If the character is part of the group permissions and the file is 120If the character is part of the group permissions and the file is
118executable or the directory searchable, by the group, and the set-group-id 121executable or the directory searchable by the group, and the set-group-id
119bit is set. 122bit is set.
120.It t 123.It t
121If the character is part of the other permissions and the file is 124If the character is part of the other permissions and the file is
122executable or the directory searchable, by others, and the ``sticky'' 125executable or the directory searchable by others, and the ``sticky''
123.Pq Dv S_ISVTX 126.Pq Dv S_ISVTX
124bit is set. 127bit is set.
125.It x 128.It x
@@ -128,7 +131,7 @@ The file is executable or the directory is searchable.
128None of the above apply. 131None of the above apply.
129.El 132.El
130.Pp 133.Pp
131The last character is a plus sign ``+'' if any there are any alternate 134The last character is a plus sign ``+'' if there are any alternate
132or additional access control methods associated with the inode, otherwise 135or additional access control methods associated with the inode, otherwise
133it will be a space. 136it will be a space.
134.Sh RETURN VALUES 137.Sh RETURN VALUES
@@ -145,5 +148,5 @@ always returns 0.
145.Sh HISTORY 148.Sh HISTORY
146The 149The
147.Fn strmode 150.Fn strmode
148function 151function first appeared in
149.Ud . 152.Bx 4.4 .
diff --git a/src/lib/libc/string/strmode.c b/src/lib/libc/string/strmode.c
index 441fc76e90..5e7f15e857 100644
--- a/src/lib/libc/string/strmode.c
+++ b/src/lib/libc/string/strmode.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strmode.c 5.3 (Berkeley) 5/18/90";*/ 35static char *rcsid = "$OpenBSD: strmode.c,v 1.3 1997/06/13 13:57:20 deraadt Exp $";
36static char *rcsid = "$Id: strmode.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <sys/types.h> 38#include <sys/types.h>
@@ -70,6 +69,11 @@ strmode(mode, p)
70 *p++ = 'p'; 69 *p++ = 'p';
71 break; 70 break;
72#endif 71#endif
72#ifdef S_IFWHT
73 case S_IFWHT: /* whiteout */
74 *p++ = 'w';
75 break;
76#endif
73 default: /* unknown */ 77 default: /* unknown */
74 *p++ = '?'; 78 *p++ = '?';
75 break; 79 break;
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c
index 3d96452af4..27ae2ba324 100644
--- a/src/lib/libc/string/strncat.c
+++ b/src/lib/libc/string/strncat.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strncat.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strncat.c,v 1.2 1996/08/19 08:34:21 tholo Exp $";
39static char *rcsid = "$Id: strncat.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c
index 0638d4dcf2..0224957f8b 100644
--- a/src/lib/libc/string/strncmp.c
+++ b/src/lib/libc/string/strncmp.c
@@ -32,11 +32,14 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strncmp.c 5.6 (Berkeley) 1/26/91";*/ 35static char *rcsid = "$OpenBSD: strncmp.c,v 1.3 1996/08/19 08:34:21 tholo Exp $";
36static char *rcsid = "$Id: strncmp.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
38#ifndef _KERNEL
39#include <string.h> 39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
40 43
41int 44int
42strncmp(s1, s2, n) 45strncmp(s1, s2, n)
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c
index 5215311b75..01bc8a872e 100644
--- a/src/lib/libc/string/strncpy.c
+++ b/src/lib/libc/string/strncpy.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strncpy.c 5.6 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strncpy.c,v 1.2 1996/08/19 08:34:22 tholo Exp $";
39static char *rcsid = "$Id: strncpy.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3
index 8578546c05..5876f560a9 100644
--- a/src/lib/libc/string/strpbrk.3
+++ b/src/lib/libc/string/strpbrk.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strpbrk.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strpbrk.3,v 1.2 1996/08/19 08:34:22 tholo Exp $
37.\" $Id: strpbrk.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRPBRK 3 39.Dt STRPBRK 3
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c
index f1d542a525..748a3a8c94 100644
--- a/src/lib/libc/string/strpbrk.c
+++ b/src/lib/libc/string/strpbrk.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strpbrk.c 5.8 (Berkeley) 1/26/91";*/ 35static char *rcsid = "$OpenBSD: strpbrk.c,v 1.2 1996/08/19 08:34:23 tholo Exp $";
36static char *rcsid = "$Id: strpbrk.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3
index 1d98cbff24..6dd00d32fb 100644
--- a/src/lib/libc/string/strrchr.3
+++ b/src/lib/libc/string/strrchr.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strrchr.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: strrchr.3,v 1.2 1996/08/19 08:34:23 tholo Exp $
37.\" $Id: strrchr.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRRCHR 3 39.Dt STRRCHR 3
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3
index 21aa7376f2..5af262a074 100644
--- a/src/lib/libc/string/strsep.3
+++ b/src/lib/libc/string/strsep.3
@@ -1,8 +1,11 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 1.\" $OpenBSD: strsep.3,v 1.4 1998/06/15 17:55:14 mickey Exp $
2.\" All rights reserved. 2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
3.\" 5.\"
4.\" This code is derived from software contributed to Berkeley by 6.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek. 7.\" Chris Torek.
8.\"
6.\" Redistribution and use in source and binary forms, with or without 9.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions 10.\" modification, are permitted provided that the following conditions
8.\" are met: 11.\" are met:
@@ -31,10 +34,9 @@
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE. 35.\" SUCH DAMAGE.
33.\" 36.\"
34.\" from: @(#)strsep.3 5.3 (Berkeley) 4/19/91 37.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93
35.\" $Id: strsep.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
36.\" 38.\"
37.Dd April 19, 1991 39.Dd June 9, 1993
38.Dt STRSEP 3 40.Dt STRSEP 3
39.Os 41.Os
40.Sh NAME 42.Sh NAME
@@ -47,23 +49,29 @@
47.Sh DESCRIPTION 49.Sh DESCRIPTION
48The 50The
49.Fn strsep 51.Fn strsep
50locates in the null-terminated string at 52function locates, in the string referenced by
51.Fa *stringp
52the first occurrence of any character in
53.Fa delim
54and replaces this with a
55.Ql \e0 ,
56records the location of the immediate following character in
57.Fa *stringp , 53.Fa *stringp ,
58then returns the original value of 54the first occurrence of any character in the string
55.Fa delim
56(or the terminating
57.Ql \e0
58character) and replaces it with a
59.Ql \e0 .
60The location of the next character after the delimiter character
61(or NULL, if the end of the string was reached) is stored in
59.Fa *stringp . 62.Fa *stringp .
60If no delimiter characters are found, 63The original value of
61.Fn strsep 64.Fa *stringp
62sets 65is returned.
66.Pp
67An ``empty'' field, i.e. one caused by two adjacent delimiter characters,
68can be detected by comparing the location referenced by the pointer returned
69in
63.Fa *stringp 70.Fa *stringp
64to 71to
65.Dv NULL ; 72.Ql \e0 .
66if 73.Pp
74If
67.Fa *stringp 75.Fa *stringp
68is initially 76is initially
69.Dv NULL , 77.Dv NULL ,
@@ -73,20 +81,30 @@ returns
73.Sh EXAMPLES 81.Sh EXAMPLES
74The following uses 82The following uses
75.Fn strsep 83.Fn strsep
76to parse strings containing runs of white space, 84to parse a string, containing tokens delimited by white space, into an
77making up an argument vector: 85argument vector:
78.Bd -literal -offset indent 86.Bd -literal -offset indent
79char inputstring[100]; 87char **ap, *argv[10], *inputstring;
80char **argv[51], **ap = argv, *p, *val; 88
81/* set up inputstring */ 89for (ap = argv; (*ap = strsep(&inputstring, " \et")) != NULL;)
82for (p = inputstring; p != NULL; ) { 90 if (**ap != '\e0')
83 while ((val = strsep(&p, " \et")) != NULL && *val == '\e0'); 91 ++ap;
84 *ap++ = val;
85}
86*ap = 0;
87.Ed 92.Ed
88.Sh HISTORY 93.Sh HISTORY
89The 94The
90.Fn strsep 95.Fn strsep
91function is 96function
92.Ud . 97is intended as a replacement for the
98.Fn strtok
99function.
100While the
101.Fn strtok
102function should be preferred for portability reasons (it conforms to
103.St -ansiC )
104it is unable to handle empty fields, i.e. detect fields delimited by
105two adjacent delimiter characters, or to be used for more than a single
106string at a time.
107The
108.Fn strsep
109function first appeared in
110.Bx 4.4 .
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c
index 69be7fe046..b69b715fc5 100644
--- a/src/lib/libc/string/strsep.c
+++ b/src/lib/libc/string/strsep.c
@@ -1,6 +1,8 @@
1/* $OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $ */
2
1/*- 3/*-
2 * Copyright (c) 1990 The Regents of the University of California. 4 * Copyright (c) 1990, 1993
3 * All rights reserved. 5 * The Regents of the University of California. All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
@@ -31,15 +33,19 @@
31 * SUCH DAMAGE. 33 * SUCH DAMAGE.
32 */ 34 */
33 35
36#include <string.h>
37#include <stdio.h>
38
34#if defined(LIBC_SCCS) && !defined(lint) 39#if defined(LIBC_SCCS) && !defined(lint)
35/*static const char sccsid[] = "from: @(#)strsep.c 5.4 (Berkeley) 1/26/91";*/ 40#if 0
36static char *rcsid = "$Id: strsep.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; 41static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93";
42#else
43static char *rcsid = "$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $";
44#endif
37#endif /* LIBC_SCCS and not lint */ 45#endif /* LIBC_SCCS and not lint */
38 46
39#include <string.h>
40
41/* 47/*
42 * Get next token from string *stringp, where tokens are nonempty 48 * Get next token from string *stringp, where tokens are possibly-empty
43 * strings separated by characters from delim. 49 * strings separated by characters from delim.
44 * 50 *
45 * Writes NULs into the string at *stringp to end tokens. 51 * Writes NULs into the string at *stringp to end tokens.
@@ -47,7 +53,7 @@ static char *rcsid = "$Id: strsep.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"
47 * On return, *stringp points past the last NUL written (if there might 53 * On return, *stringp points past the last NUL written (if there might
48 * be further tokens), or is NULL (if there are definitely no more tokens). 54 * be further tokens), or is NULL (if there are definitely no more tokens).
49 * 55 *
50 * If *stringp is NULL, strtoken returns NULL. 56 * If *stringp is NULL, strsep returns NULL.
51 */ 57 */
52char * 58char *
53strsep(stringp, delim) 59strsep(stringp, delim)
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3
index 3287fef53e..42e433c259 100644
--- a/src/lib/libc/string/strsignal.3
+++ b/src/lib/libc/string/strsignal.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strerror.3 6.9 (Berkeley) 6/29/91 36.\" $OpenBSD: strsignal.3,v 1.2 1996/08/19 08:34:25 tholo Exp $
37.\" $Id: strsignal.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRSIGNAL 3 39.Dt STRSIGNAL 3
diff --git a/src/lib/libc/string/strsignal.c b/src/lib/libc/string/strsignal.c
index ec4a267edf..cf03af5963 100644
--- a/src/lib/libc/string/strsignal.c
+++ b/src/lib/libc/string/strsignal.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ 35static char *rcsid = "$OpenBSD: strsignal.c,v 1.2 1996/08/19 08:34:25 tholo Exp $";
36static char *rcsid = "$Id: strsignal.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3
index 4de03aa58b..7d15470dee 100644
--- a/src/lib/libc/string/strspn.3
+++ b/src/lib/libc/string/strspn.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strspn.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: strspn.3,v 1.3 1996/08/19 08:34:25 tholo Exp $
37.\" $Id: strspn.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRSPN 3 39.Dt STRSPN 3
@@ -48,7 +47,7 @@
48.Fn strspn "const char *s" "const char *charset" 47.Fn strspn "const char *s" "const char *charset"
49.Sh DESCRIPTION 48.Sh DESCRIPTION
50The 49The
51.Xr strcspn 50.Fn strspn
52function 51function
53spans the initial part of the null-terminated string 52spans the initial part of the null-terminated string
54.Fa s 53.Fa s
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c
index 6224b25c2a..41940f9190 100644
--- a/src/lib/libc/string/strspn.c
+++ b/src/lib/libc/string/strspn.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strspn.c 5.8 (Berkeley) 1/26/91";*/ 35static char *rcsid = "$OpenBSD: strspn.c,v 1.2 1996/08/19 08:34:26 tholo Exp $";
36static char *rcsid = "$Id: strspn.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3
index 24fdf540ed..fa455b426a 100644
--- a/src/lib/libc/string/strstr.3
+++ b/src/lib/libc/string/strstr.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strstr.3 5.3 (Berkeley) 6/29/91 36.\" $OpenBSD: strstr.3,v 1.2 1996/08/19 08:34:26 tholo Exp $
37.\" $Id: strstr.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRSTR 3 39.Dt STRSTR 3
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c
index 1ed59e357b..763c7e29d7 100644
--- a/src/lib/libc/string/strstr.c
+++ b/src/lib/libc/string/strstr.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strstr.c 5.2 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strstr.c,v 1.2 1996/08/19 08:34:27 tholo Exp $";
39static char *rcsid = "$Id: strstr.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3
index 644bd10aed..876e0eb515 100644
--- a/src/lib/libc/string/strtok.3
+++ b/src/lib/libc/string/strtok.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strtok.3 5.8 (Berkeley) 6/29/91 36.\" $OpenBSD: strtok.3,v 1.3 1998/04/28 07:36:55 deraadt Exp $
37.\" $Id: strtok.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRTOK 3 39.Dt STRTOK 3
@@ -49,7 +48,8 @@
49.Fn strtok "char *str" "const char *sep" 48.Fn strtok "char *str" "const char *sep"
50.Sh DESCRIPTION 49.Sh DESCRIPTION
51.Bf -symbolic 50.Bf -symbolic
52This interface is obsoleted by strsep(3). 51This interface is obsoleted by
52.Xr strsep 3 .
53.Ef 53.Ef
54.Pp 54.Pp
55The 55The
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c
index 9f712579bf..2fce04c3ad 100644
--- a/src/lib/libc/string/strtok.c
+++ b/src/lib/libc/string/strtok.c
@@ -32,8 +32,7 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)strtok.c 5.8 (Berkeley) 2/24/91";*/ 35static char *rcsid = "$OpenBSD: strtok.c,v 1.2 1996/08/19 08:34:28 tholo Exp $";
36static char *rcsid = "$Id: strtok.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
38 37
39#include <string.h> 38#include <string.h>
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3
index 84fd945472..3ebdd42c29 100644
--- a/src/lib/libc/string/strxfrm.3
+++ b/src/lib/libc/string/strxfrm.3
@@ -33,8 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" from: @(#)strxfrm.3 5.4 (Berkeley) 6/29/91 36.\" $OpenBSD: strxfrm.3,v 1.2 1996/08/19 08:34:28 tholo Exp $
37.\" $Id: strxfrm.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
38.\" 37.\"
39.Dd June 29, 1991 38.Dd June 29, 1991
40.Dt STRXFRM 3 39.Dt STRXFRM 3
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c
index d9df77b957..6b258edecc 100644
--- a/src/lib/libc/string/strxfrm.c
+++ b/src/lib/libc/string/strxfrm.c
@@ -35,8 +35,7 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)strxfrm.c 5.2 (Berkeley) 1/26/91";*/ 38static char *rcsid = "$OpenBSD: strxfrm.c,v 1.2 1996/08/19 08:34:29 tholo Exp $";
39static char *rcsid = "$Id: strxfrm.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <string.h>
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3
index 133c487bbd..d1bfc358fb 100644
--- a/src/lib/libc/string/swab.3
+++ b/src/lib/libc/string/swab.3
@@ -29,8 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" from: @(#)swab.3 6.6 (Berkeley) 5/1/91 32.\" $OpenBSD: swab.3,v 1.3 1998/02/10 02:19:48 deraadt Exp $
33.\" $Id: swab.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $
34.\" 33.\"
35.Dd May 1, 1991 34.Dd May 1, 1991
36.Dt SWAB 3 35.Dt SWAB 3
@@ -39,7 +38,7 @@
39.Nm swab 38.Nm swab
40.Nd swap adjacent bytes 39.Nd swap adjacent bytes
41.Sh SYNOPSIS 40.Sh SYNOPSIS
42.Fd #include <string.h> 41.Fd #include <unistd.h>
43.Ft void 42.Ft void
44.Fn swab "const void *src" "void *dst" "size_t len" 43.Fn swab "const void *src" "void *dst" "size_t len"
45.Sh DESCRIPTION 44.Sh DESCRIPTION
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c
index f33fc53bd6..311cf13a53 100644
--- a/src/lib/libc/string/swab.c
+++ b/src/lib/libc/string/swab.c
@@ -35,11 +35,10 @@
35 */ 35 */
36 36
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38/*static char *sccsid = "from: @(#)swab.c 5.10 (Berkeley) 3/6/91";*/ 38static char *rcsid = "$OpenBSD: swab.c,v 1.3 1998/02/10 02:19:48 deraadt Exp $";
39static char *rcsid = "$Id: swab.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $";
40#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
41 40
42#include <string.h> 41#include <unistd.h>
43 42
44void 43void
45swab(from, to, len) 44swab(from, to, len)
diff --git a/src/lib/libcrypto/Makefile.ssl b/src/lib/libcrypto/Makefile.ssl
new file mode 100644
index 0000000000..efdbba38ac
--- /dev/null
+++ b/src/lib/libcrypto/Makefile.ssl
@@ -0,0 +1,161 @@
1#
2# SSLeay/crypto/Makefile
3#
4
5DIR= crypto
6TOP= ..
7CC= cc
8INCLUDE= -I. -I../include
9INCLUDES= -I.. -I../../include
10CFLAG= -g
11INSTALLTOP= /usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15RM= /bin/rm -f
16AR= ar r
17
18MAKE= make -f Makefile.ssl
19MAKEDEPEND= makedepend -f Makefile.ssl
20MAKEFILE= Makefile.ssl
21
22PEX_LIBS=
23EX_LIBS=
24
25CFLAGS= $(INCLUDE) $(CFLAG) -DCFLAGS=" \"$(CC) $(CFLAG)\" "
26
27ERR=crypto
28ERRC=cpt_err
29
30LIBS=
31
32SDIRS= md2 md5 sha mdc2 hmac ripemd \
33 des rc2 rc4 rc5 idea bf cast \
34 bn rsa dsa dh \
35 buffer bio stack lhash rand err objects \
36 evp pem x509 \
37 asn1 conf txt_db pkcs7
38
39GENERAL=Makefile README
40
41LIB= $(TOP)/libcrypto.a
42LIBSRC= cryptlib.c mem.c cversion.c ex_data.c $(ERRC).c
43LIBOBJ= cryptlib.o mem.o cversion.o ex_data.o $(ERRC).o
44
45SRC= $(LIBSRC)
46
47EXHEADER= crypto.h cryptall.h
48HEADER= cryptlib.h date.h $(EXHEADER)
49
50ALL= $(GENERAL) $(SRC) $(HEADER)
51
52top:
53 @(cd ..; $(MAKE) DIRS=$(DIR) all)
54
55all: date.h lib subdirs
56
57date.h: ../Makefile.ssl ../VERSION
58 echo "#define DATE \"`date`\"" >date.h
59
60subdirs:
61 @for i in $(SDIRS) ;\
62 do \
63 (cd $$i; echo "making all in $$i..."; \
64 $(MAKE) CC='$(CC)' INCLUDES='${INCLUDES}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' BN_MULW='${BN_MULW}' DES_ENC='${DES_ENC}' SHA1_ASM_OBJ='${SHA1_ASM_OBJ}' MD5_ASM_OBJ='${MD5_ASM_OBJ}' RMD160_ASM_OBJ='${RMD160_ASM_OBJ}' BF_ENC='${BF_ENC}' CAST_ENC='${CAST_ENC}' RC4_ENC='${RC4_ENC}' RC5_ENC='${RC5_ENC}' AR='${AR}' all ); \
65 done;
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69 @for i in $(SDIRS) ;\
70 do \
71 (cd $$i; echo "making 'files' in $$i..."; \
72 $(MAKE) files ); \
73 done;
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../include $(HEADER) ;
79 $(TOP)/util/mklink.sh ../test $(TEST) ;
80 $(TOP)/util/mklink.sh ../apps $(APPS) ;
81 $(TOP)/util/point.sh Makefile.ssl Makefile;
82 @for i in $(SDIRS) ;\
83 do \
84 (cd $$i; echo "making links in $$i..."; \
85 $(MAKE) links ); \
86 done;
87
88lib: $(LIBOBJ)
89 $(AR) $(LIB) $(LIBOBJ)
90 sh $(TOP)/util/ranlib.sh $(LIB)
91 @touch lib
92
93libs:
94 @for i in $(SDIRS) ;\
95 do \
96 (cd $$i; echo "making libs in $$i..."; \
97 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' AR='${AR}' lib ); \
98 done;
99
100tests:
101 @for i in $(SDIRS) ;\
102 do \
103 (cd $$i; echo "making tests in $$i..."; \
104 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' AR='${AR}' tests ); \
105 done;
106
107install:
108 @for i in $(EXHEADER) ;\
109 do \
110 (cp $$i $(INSTALLTOP)/include/$$i; \
111 chmod 644 $(INSTALLTOP)/include/$$i ); \
112 done;
113 @for i in $(SDIRS) ;\
114 do \
115 (cd $$i; echo "making install in $$i..."; \
116 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' install ); \
117 done;
118
119lint:
120 @for i in $(SDIRS) ;\
121 do \
122 (cd $$i; echo "making lint in $$i..."; \
123 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' lint ); \
124 done;
125
126depend:
127 $(MAKEDEPEND) $(INCLUDE) $(PROGS) $(LIBSRC)
128 @for i in $(SDIRS) ;\
129 do \
130 (cd $$i; echo "making depend in $$i..."; \
131 $(MAKE) MAKEFILE='${MAKEFILE}' INCLUDES='${INCLUDES}' MAKEDEPEND='${MAKEDEPEND}' depend ); \
132 done;
133
134clean:
135 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
136 @for i in $(SDIRS) ;\
137 do \
138 (cd $$i; echo "making clean in $$i..."; \
139 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' clean ); \
140 done;
141
142dclean:
143 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
144 mv -f Makefile.new $(MAKEFILE)
145 @for i in $(SDIRS) ;\
146 do \
147 (cd $$i; echo "making dclean in $$i..."; \
148 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' dclean ); \
149 done;
150
151errors:
152 perl ./err/err_code.pl -conf err/ssleay.ec *.c */*.c ../ssl/*.c ../rsaref/*.c
153 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
154 perl err/err_genc.pl -s $(ERR).h $(ERRC).c
155 @for i in $(SDIRS) ;\
156 do \
157 (cd $$i; echo "making errors in $$i..."; \
158 $(MAKE) errors ); \
159 done;
160
161# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/asn1/Makefile.ssl b/src/lib/libcrypto/asn1/Makefile.ssl
new file mode 100644
index 0000000000..30751bd156
--- /dev/null
+++ b/src/lib/libcrypto/asn1/Makefile.ssl
@@ -0,0 +1,120 @@
1#
2# SSLeay/crypto/asn1/Makefile
3#
4
5DIR= asn1
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=asn1
19ERRC=asn1_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= a_object.c a_bitstr.c a_utctm.c a_int.c a_octet.c a_print.c \
26 a_type.c a_set.c a_dup.c a_d2i_fp.c a_i2d_fp.c \
27 a_sign.c a_digest.c a_verify.c \
28 x_algor.c x_val.c x_pubkey.c x_sig.c x_req.c x_attrib.c \
29 x_name.c x_cinf.c x_x509.c x_crl.c x_info.c x_spki.c \
30 d2i_r_pr.c i2d_r_pr.c d2i_r_pu.c i2d_r_pu.c \
31 d2i_s_pr.c i2d_s_pr.c d2i_s_pu.c i2d_s_pu.c \
32 d2i_pu.c d2i_pr.c i2d_pu.c i2d_pr.c\
33 t_req.c t_x509.c t_pkey.c \
34 p7_i_s.c p7_signi.c p7_signd.c p7_recip.c p7_enc_c.c p7_evp.c \
35 p7_dgst.c p7_s_e.c p7_enc.c p7_lib.c \
36 f_int.c f_string.c i2d_dhp.c i2d_dsap.c d2i_dhp.c d2i_dsap.c n_pkey.c \
37 a_hdr.c x_pkey.c a_bool.c x_exten.c \
38 asn1_par.c asn1_lib.c $(ERRC).c a_meth.c a_bytes.c \
39 evp_asn1.c
40LIBOBJ= a_object.o a_bitstr.o a_utctm.o a_int.o a_octet.o a_print.o \
41 a_type.o a_set.o a_dup.o a_d2i_fp.o a_i2d_fp.o \
42 a_sign.o a_digest.o a_verify.o \
43 x_algor.o x_val.o x_pubkey.o x_sig.o x_req.o x_attrib.o \
44 x_name.o x_cinf.o x_x509.o x_crl.o x_info.o x_spki.o \
45 d2i_r_pr.o i2d_r_pr.o d2i_r_pu.o i2d_r_pu.o \
46 d2i_s_pr.o i2d_s_pr.o d2i_s_pu.o i2d_s_pu.o \
47 d2i_pu.o d2i_pr.o i2d_pu.o i2d_pr.o \
48 t_req.o t_x509.o t_pkey.o \
49 p7_i_s.o p7_signi.o p7_signd.o p7_recip.o p7_enc_c.o p7_evp.o \
50 p7_dgst.o p7_s_e.o p7_enc.o p7_lib.o \
51 f_int.o f_string.o i2d_dhp.o i2d_dsap.o d2i_dhp.o d2i_dsap.o n_pkey.o \
52 a_hdr.o x_pkey.o a_bool.o x_exten.o \
53 asn1_par.o asn1_lib.o $(ERRC).o a_meth.o a_bytes.o \
54 evp_asn1.o
55
56SRC= $(LIBSRC)
57
58EXHEADER= asn1.h asn1_mac.h
59HEADER= $(EXHEADER)
60
61ALL= $(GENERAL) $(SRC) $(HEADER)
62
63top:
64 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
65
66test: test.c
67 cc -g -I../../include -c test.c
68 cc -g -I../../include -o test test.o -L../.. -lcrypto
69
70pk: pk.c
71 cc -g -I../../include -c pk.c
72 cc -g -I../../include -o pk pk.o -L../.. -lcrypto
73
74all: lib
75
76lib: $(LIBOBJ)
77 $(AR) $(LIB) $(LIBOBJ)
78 sh $(TOP)/util/ranlib.sh $(LIB)
79 @touch lib
80
81files:
82 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
83
84links:
85 /bin/rm -f Makefile
86 $(TOP)/util/point.sh Makefile.ssl Makefile ;
87 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
88 $(TOP)/util/mklink.sh ../../test $(TEST)
89 $(TOP)/util/mklink.sh ../../apps $(APPS)
90
91install:
92 @for i in $(EXHEADER) ; \
93 do \
94 (cp $$i $(INSTALLTOP)/include/$$i; \
95 chmod 644 $(INSTALLTOP)/include/$$i ); \
96 done;
97
98tags:
99 ctags $(SRC)
100
101tests:
102
103lint:
104 lint -DLINT $(INCLUDES) $(SRC)>fluff
105
106depend:
107 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
108
109dclean:
110 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
111 mv -f Makefile.new $(MAKEFILE)
112
113clean:
114 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
115
116errors:
117 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
118 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
119
120# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/asn1/f.c b/src/lib/libcrypto/asn1/f.c
new file mode 100644
index 0000000000..2ab3a262ac
--- /dev/null
+++ b/src/lib/libcrypto/asn1/f.c
@@ -0,0 +1,80 @@
1/* crypto/asn1/f.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "asn1.h"
60#include "err.h"
61
62main()
63 {
64 ASN1_TYPE *at;
65 char buf[512];
66 int n;
67 long l;
68
69 at=ASN1_TYPE_new();
70
71 n=ASN1_TYPE_set_int_octetstring(at,98736,"01234567",8);
72 printf("%d\n",n);
73 n=ASN1_TYPE_get_int_octetstring(at,&l,buf,8);
74 buf[8]='\0';
75 printf("%ld %d %d\n",l,n,buf[8]);
76 buf[8]='\0';
77 printf("%s\n",buf);
78 ERR_load_crypto_strings();
79 ERR_print_errors_fp(stderr);
80 }
diff --git a/src/lib/libcrypto/asn1/x_cinf.c b/src/lib/libcrypto/asn1/x_cinf.c
new file mode 100644
index 0000000000..4fc2cc9f6e
--- /dev/null
+++ b/src/lib/libcrypto/asn1/x_cinf.c
@@ -0,0 +1,197 @@
1/* crypto/asn1/x_cinf.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_CINF,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_CINF_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_CINF(a,pp)
69X509_CINF *a;
70unsigned char **pp;
71 {
72 int v1=0,v2=0;
73 M_ASN1_I2D_vars(a);
74
75 M_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);
76 M_ASN1_I2D_len(a->serialNumber, i2d_ASN1_INTEGER);
77 M_ASN1_I2D_len(a->signature, i2d_X509_ALGOR);
78 M_ASN1_I2D_len(a->issuer, i2d_X509_NAME);
79 M_ASN1_I2D_len(a->validity, i2d_X509_VAL);
80 M_ASN1_I2D_len(a->subject, i2d_X509_NAME);
81 M_ASN1_I2D_len(a->key, i2d_X509_PUBKEY);
82 M_ASN1_I2D_len_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING);
83 M_ASN1_I2D_len_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING);
84 M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);
85
86 M_ASN1_I2D_seq_total();
87
88 M_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);
89 M_ASN1_I2D_put(a->serialNumber, i2d_ASN1_INTEGER);
90 M_ASN1_I2D_put(a->signature, i2d_X509_ALGOR);
91 M_ASN1_I2D_put(a->issuer, i2d_X509_NAME);
92 M_ASN1_I2D_put(a->validity, i2d_X509_VAL);
93 M_ASN1_I2D_put(a->subject, i2d_X509_NAME);
94 M_ASN1_I2D_put(a->key, i2d_X509_PUBKEY);
95 M_ASN1_I2D_put_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING,1);
96 M_ASN1_I2D_put_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING,2);
97 M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);
98
99 M_ASN1_I2D_finish();
100 }
101
102X509_CINF *d2i_X509_CINF(a,pp,length)
103X509_CINF **a;
104unsigned char **pp;
105long length;
106 {
107 int ver=0;
108 M_ASN1_D2I_vars(a,X509_CINF *,X509_CINF_new);
109
110 M_ASN1_D2I_Init();
111 M_ASN1_D2I_start_sequence();
112 /* we have the optional version field */
113 if (M_ASN1_next == (V_ASN1_CONTEXT_SPECIFIC | V_ASN1_CONSTRUCTED | 0))
114 {
115 M_ASN1_D2I_get_EXP_opt(ret->version,d2i_ASN1_INTEGER,0);
116 if (ret->version->data != NULL)
117 ver=ret->version->data[0];
118 }
119 else
120 {
121 if (ret->version != NULL)
122 {
123 ASN1_INTEGER_free(ret->version);
124 ret->version=NULL;
125 }
126 }
127 M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER);
128 M_ASN1_D2I_get(ret->signature,d2i_X509_ALGOR);
129 M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME);
130 M_ASN1_D2I_get(ret->validity,d2i_X509_VAL);
131 M_ASN1_D2I_get(ret->subject,d2i_X509_NAME);
132 M_ASN1_D2I_get(ret->key,d2i_X509_PUBKEY);
133 if (ver >= 1) /* version 2 extensions */
134 {
135 if (ret->issuerUID != NULL)
136 {
137 ASN1_BIT_STRING_free(ret->issuerUID);
138 ret->issuerUID=NULL;
139 }
140 if (ret->subjectUID != NULL)
141 {
142 ASN1_BIT_STRING_free(ret->subjectUID);
143 ret->issuerUID=NULL;
144 }
145 M_ASN1_D2I_get_IMP_opt(ret->issuerUID,d2i_ASN1_BIT_STRING, 1,
146 V_ASN1_BIT_STRING);
147 M_ASN1_D2I_get_IMP_opt(ret->subjectUID,d2i_ASN1_BIT_STRING, 2,
148 V_ASN1_BIT_STRING);
149 }
150 if (ver >= 2) /* version 3 extensions */
151 {
152 if (ret->extensions != NULL)
153 while (sk_num(ret->extensions))
154 X509_EXTENSION_free((X509_EXTENSION *)
155 sk_pop(ret->extensions));
156 M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION,3,
157 V_ASN1_SEQUENCE);
158 }
159 M_ASN1_D2I_Finish(a,X509_CINF_free,ASN1_F_D2I_X509_CINF);
160 }
161
162X509_CINF *X509_CINF_new()
163 {
164 X509_CINF *ret=NULL;
165
166 M_ASN1_New_Malloc(ret,X509_CINF);
167 ret->version=NULL;
168 M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);
169 M_ASN1_New(ret->signature,X509_ALGOR_new);
170 M_ASN1_New(ret->issuer,X509_NAME_new);
171 M_ASN1_New(ret->validity,X509_VAL_new);
172 M_ASN1_New(ret->subject,X509_NAME_new);
173 M_ASN1_New(ret->key,X509_PUBKEY_new);
174 ret->issuerUID=NULL;
175 ret->subjectUID=NULL;
176 ret->extensions=NULL;
177 return(ret);
178 M_ASN1_New_Error(ASN1_F_X509_CINF_NEW);
179 }
180
181void X509_CINF_free(a)
182X509_CINF *a;
183 {
184 if (a == NULL) return;
185 ASN1_INTEGER_free(a->version);
186 ASN1_INTEGER_free(a->serialNumber);
187 X509_ALGOR_free(a->signature);
188 X509_NAME_free(a->issuer);
189 X509_VAL_free(a->validity);
190 X509_NAME_free(a->subject);
191 X509_PUBKEY_free(a->key);
192 ASN1_BIT_STRING_free(a->issuerUID);
193 ASN1_BIT_STRING_free(a->subjectUID);
194 sk_pop_free(a->extensions,X509_EXTENSION_free);
195 Free((char *)a);
196 }
197
diff --git a/src/lib/libcrypto/bf/Makefile.ssl b/src/lib/libcrypto/bf/Makefile.ssl
new file mode 100644
index 0000000000..236671f238
--- /dev/null
+++ b/src/lib/libcrypto/bf/Makefile.ssl
@@ -0,0 +1,107 @@
1#
2# SSLeay/crypto/blowfish/Makefile
3#
4
5DIR= bf
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17BF_ENC= bf_enc.o
18# or use
19#DES_ENC= bx86-elf.o
20
21CFLAGS= $(INCLUDES) $(CFLAG)
22
23GENERAL=Makefile
24TEST=bftest.c
25APPS=
26
27LIB=$(TOP)/libcrypto.a
28LIBSRC=bf_skey.c bf_ecb.c bf_enc.c bf_cfb64.c bf_ofb64.c
29LIBOBJ=bf_skey.o bf_ecb.o $(BF_ENC) bf_cfb64.o bf_ofb64.o
30
31SRC= $(LIBSRC)
32
33EXHEADER= blowfish.h
34HEADER= bf_pi.h bf_locl.h $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: lib
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48# elf
49asm/bx86-elf.o: asm/bx86unix.cpp
50 $(CPP) -DELF asm/bx86unix.cpp | as -o asm/bx86-elf.o
51
52# solaris
53asm/bx86-sol.o: asm/bx86unix.cpp
54 $(CC) -E -DSOL asm/bx86unix.cpp | sed 's/^#.*//' > asm/bx86-sol.s
55 as -o asm/bx86-sol.o asm/bx86-sol.s
56 rm -f asm/bx86-sol.s
57
58# a.out
59asm/bx86-out.o: asm/bx86unix.cpp
60 $(CPP) -DOUT asm/bx86unix.cpp | as -o asm/bx86-out.o
61
62# bsdi
63asm/bx86bsdi.o: asm/bx86unix.cpp
64 $(CPP) -DBSDI asm/bx86unix.cpp | as -o asm/bx86bsdi.o
65
66asm/bx86unix.cpp:
67 (cd asm; perl bf-586.pl cpp >bx86unix.cpp)
68
69files:
70 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
71
72links:
73 /bin/rm -f Makefile
74 $(TOP)/util/point.sh Makefile.ssl Makefile ;
75 $(TOP)/util/point.sh ../../doc/blowfish.doc blowfish.doc ;
76 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
77 $(TOP)/util/mklink.sh ../../test $(TEST)
78 $(TOP)/util/mklink.sh ../../apps $(APPS)
79
80install:
81 @for i in $(EXHEADER) ; \
82 do \
83 (cp $$i $(INSTALLTOP)/include/$$i; \
84 chmod 644 $(INSTALLTOP)/include/$$i ); \
85 done;
86
87tags:
88 ctags $(SRC)
89
90tests:
91
92lint:
93 lint -DLINT $(INCLUDES) $(SRC)>fluff
94
95depend:
96 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
97
98dclean:
99 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
100 mv -f Makefile.new $(MAKEFILE)
101
102clean:
103 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
104
105errors:
106
107# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/bio/Makefile.ssl b/src/lib/libcrypto/bio/Makefile.ssl
new file mode 100644
index 0000000000..42e11e1c94
--- /dev/null
+++ b/src/lib/libcrypto/bio/Makefile.ssl
@@ -0,0 +1,92 @@
1#
2# SSLeay/crypto/bio/Makefile
3#
4
5DIR= bio
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=bio
19ERRC=bio_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= bio_lib.c bio_cb.c $(ERRC).c \
26 bss_mem.c bss_null.c bss_fd.c \
27 bss_file.c bss_sock.c bss_conn.c \
28 bf_null.c bf_buff.c b_print.c b_dump.c \
29 b_sock.c bss_acpt.c bf_nbio.c
30LIBOBJ= bio_lib.o bio_cb.o $(ERRC).o \
31 bss_mem.o bss_null.o bss_fd.o \
32 bss_file.o bss_sock.o bss_conn.o \
33 bf_null.o bf_buff.o b_print.o b_dump.o \
34 b_sock.o bss_acpt.o bf_nbio.o
35
36SRC= $(LIBSRC)
37
38EXHEADER= bio.h bss_file.c
39HEADER= $(EXHEADER)
40
41ALL= $(GENERAL) $(SRC) $(HEADER)
42
43top:
44 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
45
46all: lib
47
48lib: $(LIBOBJ)
49 $(AR) $(LIB) $(LIBOBJ)
50 sh $(TOP)/util/ranlib.sh $(LIB)
51 @touch lib
52
53files:
54 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
55
56links:
57 /bin/rm -f Makefile
58 $(TOP)/util/point.sh Makefile.ssl Makefile;
59 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
60 $(TOP)/util/mklink.sh ../../test $(TEST)
61 $(TOP)/util/mklink.sh ../../apps $(APPS)
62
63install:
64 @for i in $(EXHEADER) bss_file.c ; \
65 do \
66 (cp $$i $(INSTALLTOP)/include/$$i; \
67 chmod 644 $(INSTALLTOP)/include/$$i ); \
68 done;
69
70tags:
71 ctags $(SRC)
72
73tests:
74
75lint:
76 lint -DLINT $(INCLUDES) $(SRC)>fluff
77
78depend:
79 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
80
81dclean:
82 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
83 mv -f Makefile.new $(MAKEFILE)
84
85clean:
86 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
87
88errors:
89 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
90 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
91
92# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/bn/Makefile.ssl b/src/lib/libcrypto/bn/Makefile.ssl
new file mode 100644
index 0000000000..9809d26cbc
--- /dev/null
+++ b/src/lib/libcrypto/bn/Makefile.ssl
@@ -0,0 +1,133 @@
1#
2# SSLeay/crypto/bn/Makefile
3#
4
5DIR= bn
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16BN_MULW= bn_mulw.o
17# or use
18#BN_MULW= bn86-elf.o
19
20CFLAGS= $(INCLUDES) $(CFLAG)
21
22ERR=bn
23ERRC=bn_err
24GENERAL=Makefile
25TEST=bntest.c exptest.c
26APPS=
27
28LIB=$(TOP)/libcrypto.a
29LIBSRC= bn_add.c bn_div.c bn_exp.c bn_lib.c bn_mod.c bn_mul.c \
30 bn_print.c bn_rand.c bn_shift.c bn_sub.c bn_word.c bn_blind.c \
31 bn_gcd.c bn_prime.c $(ERRC).c bn_sqr.c bn_mulw.c bn_recp.c bn_mont.c \
32 bn_mpi.c
33
34LIBOBJ= bn_add.o bn_div.o bn_exp.o bn_lib.o bn_mod.o bn_mul.o \
35 bn_print.o bn_rand.o bn_shift.o bn_sub.o bn_word.o bn_blind.o \
36 bn_gcd.o bn_prime.o $(ERRC).o bn_sqr.o $(BN_MULW) bn_recp.o bn_mont.o \
37 bn_mpi.o
38
39
40SRC= $(LIBSRC)
41
42EXHEADER= bn.h
43HEADER= bn_lcl.h bn_prime.h $(EXHEADER)
44
45ALL= $(GENERAL) $(SRC) $(HEADER)
46
47top:
48 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
49
50all: lib
51
52knuth: bn_knuth.c
53 cc -pg -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
54
55knuth.fast: bn_knuth.c
56 cc -pg -fast -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
57
58
59lib: $(LIBOBJ)
60 $(AR) $(LIB) $(LIBOBJ)
61 sh $(TOP)/util/ranlib.sh $(LIB)
62 @touch lib
63
64# elf
65asm/bn86-elf.o: asm/bn86unix.cpp
66 $(CPP) -DELF asm/bn86unix.cpp | as -o asm/bn86-elf.o
67
68# solaris
69asm/bn86-sol.o: asm/bn86unix.cpp
70 $(CC) -E -DSOL asm/bn86unix.cpp | sed 's/^#.*//' > asm/bn86-sol.s
71 as -o asm/bn86-sol.o asm/bn86-sol.s
72 rm -f asm/bn86-sol.s
73
74# a.out
75asm/bn86-out.o: asm/bn86unix.cpp
76 $(CPP) -DOUT asm/bn86unix.cpp | as -o asm/bn86-out.o
77
78# bsdi
79asm/bn86bsdi.o: asm/bn86unix.cpp
80 $(CPP) -DBSDI asm/bn86unix.cpp | as -o asm/bn86bsdi.o
81
82asm/bn86unix.cpp:
83 (cd asm; perl bn-586.pl cpp >bn86unix.cpp )
84
85files:
86 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
87
88links:
89 /bin/rm -f Makefile
90 $(TOP)/util/point.sh Makefile.ssl Makefile ;
91 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
92 $(TOP)/util/mklink.sh ../../test $(TEST)
93 $(TOP)/util/mklink.sh ../../apps $(APPS)
94
95install:
96 @for i in $(EXHEADER) ; \
97 do \
98 (cp $$i $(INSTALLTOP)/include/$$i; \
99 chmod 644 $(INSTALLTOP)/include/$$i ); \
100 done;
101
102exptest:
103 /bin/rm -f exptest
104 gcc -I../../include -g2 -ggdb -o exptest exptest.c ../../libcrypto.a
105
106div:
107 /bin/rm -f a.out
108 gcc -I.. -g div.c ../../libcrypto.a
109
110tags:
111 ctags $(SRC)
112
113tests:
114
115lint:
116 lint -DLINT $(INCLUDES) $(SRC)>fluff
117
118depend:
119 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
120
121dclean:
122 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
123 mv -f Makefile.new $(MAKEFILE)
124
125clean:
126 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff bn_mulw.s
127
128errors:
129 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # special case .org
130 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
131 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
132
133# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/buffer/Makefile.ssl b/src/lib/libcrypto/buffer/Makefile.ssl
new file mode 100644
index 0000000000..a5f150e523
--- /dev/null
+++ b/src/lib/libcrypto/buffer/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/buffer/Makefile
3#
4
5DIR= buffer
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=buffer
19ERRC=buf_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= buffer.c $(ERRC).c
26LIBOBJ= buffer.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= buffer.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/cast/Makefile.ssl b/src/lib/libcrypto/cast/Makefile.ssl
new file mode 100644
index 0000000000..0143827ae5
--- /dev/null
+++ b/src/lib/libcrypto/cast/Makefile.ssl
@@ -0,0 +1,109 @@
1#
2# SSLeay/crypto/cast/Makefile
3#
4
5DIR= cast
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17CAST_ENC=c_enc.o
18# or use
19#CAST_ENC=asm/cx86-elf.o
20#CAST_ENC=asm/cx86-out.o
21#CAST_ENC=asm/cx86-sol.o
22#CAST_ENC=asm/cx86bdsi.o
23
24CFLAGS= $(INCLUDES) $(CFLAG)
25
26GENERAL=Makefile
27TEST=casttest.c
28APPS=
29
30LIB=$(TOP)/libcrypto.a
31LIBSRC=c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c
32LIBOBJ=c_skey.o c_ecb.o $(CAST_ENC) c_cfb64.o c_ofb64.o
33
34SRC= $(LIBSRC)
35
36EXHEADER= cast.h
37HEADER= cast_s.h cast_lcl.h $(EXHEADER)
38
39ALL= $(GENERAL) $(SRC) $(HEADER)
40
41top:
42 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
43
44all: lib
45
46lib: $(LIBOBJ)
47 $(AR) $(LIB) $(LIBOBJ)
48 sh $(TOP)/util/ranlib.sh $(LIB)
49 @touch lib
50
51# elf
52asm/cx86-elf.o: asm/cx86unix.cpp
53 $(CPP) -DELF asm/cx86unix.cpp | as -o asm/cx86-elf.o
54
55# solaris
56asm/cx86-sol.o: asm/cx86unix.cpp
57 $(CC) -E -DSOL asm/cx86unix.cpp | sed 's/^#.*//' > asm/cx86-sol.s
58 as -o asm/cx86-sol.o asm/cx86-sol.s
59 rm -f asm/cx86-sol.s
60
61# a.out
62asm/cx86-out.o: asm/cx86unix.cpp
63 $(CPP) -DOUT asm/cx86unix.cpp | as -o asm/cx86-out.o
64
65# bsdi
66asm/cx86bsdi.o: asm/cx86unix.cpp
67 $(CPP) -DBSDI asm/cx86unix.cpp | as -o asm/cx86bsdi.o
68
69asm/cx86unix.cpp:
70 (cd asm; perl cast-586.pl cpp >cx86unix.cpp)
71
72files:
73 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
79 $(TOP)/util/mklink.sh ../../test $(TEST)
80 $(TOP)/util/mklink.sh ../../apps $(APPS)
81
82install:
83 @for i in $(EXHEADER) ; \
84 do \
85 (cp $$i $(INSTALLTOP)/include/$$i; \
86 chmod 644 $(INSTALLTOP)/include/$$i ); \
87 done;
88
89tags:
90 ctags $(SRC)
91
92tests:
93
94lint:
95 lint -DLINT $(INCLUDES) $(SRC)>fluff
96
97depend:
98 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
99
100dclean:
101 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
102 mv -f Makefile.new $(MAKEFILE)
103
104clean:
105 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
106
107errors:
108
109# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/conf/Makefile.ssl b/src/lib/libcrypto/conf/Makefile.ssl
new file mode 100644
index 0000000000..00e917aa44
--- /dev/null
+++ b/src/lib/libcrypto/conf/Makefile.ssl
@@ -0,0 +1,85 @@
1#
2# SSLeay/crypto/conf/Makefile
3#
4
5DIR= conf
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=conf
19ERRC=conf_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= conf.c $(ERRC).c
26
27LIBOBJ= conf.o $(ERRC).o
28
29SRC= $(LIBSRC)
30
31EXHEADER= conf.h
32HEADER= conf_lcl.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46files:
47 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
48
49links:
50 /bin/rm -f Makefile
51 $(TOP)/util/point.sh Makefile.ssl Makefile ;
52 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
53 $(TOP)/util/mklink.sh ../../test $(TEST)
54 $(TOP)/util/mklink.sh ../../apps $(APPS)
55
56install:
57 @for i in $(EXHEADER) ; \
58 do \
59 (cp $$i $(INSTALLTOP)/include/$$i; \
60 chmod 644 $(INSTALLTOP)/include/$$i ); \
61 done;
62
63tags:
64 ctags $(SRC)
65
66tests:
67
68lint:
69 lint -DLINT $(INCLUDES) $(SRC)>fluff
70
71depend:
72 $(MAKEDEPEND) $(INCLUDES) $(LIBSRC)
73
74dclean:
75 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
76 mv -f Makefile.new $(MAKEFILE)
77
78clean:
79 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
80
81errors:
82 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
83 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
84
85# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/des/FILES b/src/lib/libcrypto/des/FILES
new file mode 100644
index 0000000000..4c7ea2de7a
--- /dev/null
+++ b/src/lib/libcrypto/des/FILES
@@ -0,0 +1,96 @@
1/* General stuff */
2COPYRIGHT - Copyright info.
3MODES.DES - A description of the features of the different modes of DES.
4FILES - This file.
5INSTALL - How to make things compile.
6Imakefile - For use with kerberos.
7README - What this package is.
8VERSION - Which version this is and what was changed.
9KERBEROS - Kerberos version 4 notes.
10Makefile.PL - An old makefile to build with perl5, not current.
11Makefile.ssl - The SSLeay makefile
12Makefile.uni - The normal unix makefile.
13GNUmakefile - The makefile for use with glibc.
14makefile.bc - A Borland C makefile
15times - Some outputs from 'speed' on some machines.
16vms.com - For use when compiling under VMS
17
18/* My SunOS des(1) replacement */
19des.c - des(1) source code.
20des.man - des(1) manual.
21
22/* Testing and timing programs. */
23destest.c - Source for libdes.a test program.
24speed.c - Source for libdes.a timing program.
25rpw.c - Source for libdes.a testing password reading routines.
26
27/* libdes.a source code */
28des_crypt.man - libdes.a manual page.
29des.h - Public libdes.a header file.
30ecb_enc.c - des_ecb_encrypt() source, this contains the basic DES code.
31ecb3_enc.c - des_ecb3_encrypt() source.
32cbc_ckm.c - des_cbc_cksum() source.
33cbc_enc.c - des_cbc_encrypt() source.
34ncbc_enc.c - des_cbc_encrypt() that is 'normal' in that it copies
35 the new iv values back in the passed iv vector.
36ede_enc.c - des_ede3_cbc_encrypt() cbc mode des using triple DES.
37cbc3_enc.c - des_3cbc_encrypt() source, don't use this function.
38cfb_enc.c - des_cfb_encrypt() source.
39cfb64enc.c - des_cfb64_encrypt() cfb in 64 bit mode but setup to be
40 used as a stream cipher.
41cfb64ede.c - des_ede3_cfb64_encrypt() cfb in 64 bit mode but setup to be
42 used as a stream cipher and using triple DES.
43ofb_enc.c - des_cfb_encrypt() source.
44ofb64_enc.c - des_ofb_encrypt() ofb in 64 bit mode but setup to be
45 used as a stream cipher.
46ofb64ede.c - des_ede3_ofb64_encrypt() ofb in 64 bit mode but setup to be
47 used as a stream cipher and using triple DES.
48enc_read.c - des_enc_read() source.
49enc_writ.c - des_enc_write() source.
50pcbc_enc.c - des_pcbc_encrypt() source.
51qud_cksm.c - quad_cksum() source.
52rand_key.c - des_random_key() source.
53read_pwd.c - Source for des_read_password() plus related functions.
54set_key.c - Source for des_set_key().
55str2key.c - Covert a string of any length into a key.
56fcrypt.c - A small, fast version of crypt(3).
57des_locl.h - Internal libdes.a header file.
58podd.h - Odd parity tables - used in des_set_key().
59sk.h - Lookup tables used in des_set_key().
60spr.h - What is left of the S tables - used in ecb_encrypt().
61des_ver.h - header file for the external definition of the
62 version string.
63des.doc - SSLeay documentation for the library.
64
65/* The perl scripts - you can ignore these files they are only
66 * included for the curious */
67des.pl - des in perl anyone? des_set_key and des_ecb_encrypt
68 both done in a perl library.
69testdes.pl - Testing program for des.pl
70doIP - Perl script used to develop IP xor/shift code.
71doPC1 - Perl script used to develop PC1 xor/shift code.
72doPC2 - Generates sk.h.
73PC1 - Output of doPC1 should be the same as output from PC1.
74PC2 - used in development of doPC2.
75shifts.pl - Perl library used by my perl scripts.
76
77/* I started making a perl5 dynamic library for libdes
78 * but did not fully finish, these files are part of that effort. */
79DES.pm
80DES.pod
81DES.xs
82t
83typemap
84
85/* The following are for use with sun RPC implementaions. */
86rpc_des.h
87rpc_enc.c
88
89/* The following are contibuted by Mark Murray <mark@grondar.za>. They
90 * are not normally built into libdes due to machine specific routines
91 * contained in them. They are for use in the most recent incarnation of
92 * export kerberos v 4 (eBones). */
93supp.c
94new_rkey.c
95
96
diff --git a/src/lib/libcrypto/des/Makefile.ssl b/src/lib/libcrypto/des/Makefile.ssl
new file mode 100644
index 0000000000..78b5189ee3
--- /dev/null
+++ b/src/lib/libcrypto/des/Makefile.ssl
@@ -0,0 +1,140 @@
1#
2# SSLeay/crypto/des/Makefile
3#
4
5DIR= des
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16DES_ENC= des_enc.o fcrypt_b.o
17# or use
18#DES_ENC= dx86-elf.o yx86-elf.o
19
20CFLAGS= $(INCLUDES) $(CFLAG)
21
22GENERAL=Makefile des.org des_locl.org
23TEST=destest.c
24APPS=
25
26LIB=$(TOP)/libcrypto.a
27LIBSRC= cbc_cksm.c cbc_enc.c cfb64enc.c cfb_enc.c \
28 ecb3_enc.c ecb_enc.c enc_read.c enc_writ.c \
29 fcrypt.c ofb64enc.c ofb_enc.c pcbc_enc.c \
30 qud_cksm.c rand_key.c read_pwd.c rpc_enc.c set_key.c \
31 des_enc.c fcrypt_b.c read2pwd.c \
32 fcrypt.c xcbc_enc.c \
33 str2key.c cfb64ede.c ofb64ede.c supp.c
34
35LIBOBJ= set_key.o ecb_enc.o cbc_enc.o \
36 ecb3_enc.o cfb64enc.o cfb64ede.o cfb_enc.o ofb64ede.o \
37 enc_read.o enc_writ.o ofb64enc.o \
38 ofb_enc.o str2key.o pcbc_enc.o qud_cksm.o rand_key.o \
39 ${DES_ENC} read2pwd.o \
40 fcrypt.o xcbc_enc.o read_pwd.o rpc_enc.o cbc_cksm.o supp.o
41
42SRC= $(LIBSRC)
43
44EXHEADER= des.h
45HEADER= des_locl.h rpc_des.h podd.h sk.h spr.h des_ver.h $(EXHEADER)
46
47ALL= $(GENERAL) $(SRC) $(HEADER)
48
49top:
50 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
51
52all: lib
53
54lib: $(LIBOBJ)
55 $(AR) $(LIB) $(LIBOBJ)
56 sh $(TOP)/util/ranlib.sh $(LIB)
57 @touch lib
58
59# elf
60asm/dx86-elf.o: asm/dx86unix.cpp
61 $(CPP) -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
62
63asm/yx86-elf.o: asm/yx86unix.cpp
64 $(CPP) -DELF asm/yx86unix.cpp | as -o asm/yx86-elf.o
65
66# solaris
67asm/dx86-sol.o: asm/dx86unix.cpp
68 $(CC) -E -DSOL asm/dx86unix.cpp | sed 's/^#.*//' > asm/dx86-sol.s
69 as -o asm/dx86-sol.o asm/dx86-sol.s
70 rm -f asm/dx86-sol.s
71
72asm/yx86-sol.o: asm/yx86unix.cpp
73 $(CC) -E -DSOL asm/yx86unix.cpp | sed 's/^#.*//' > asm/yx86-sol.s
74 as -o asm/yx86-sol.o asm/yx86-sol.s
75 rm -f asm/yx86-sol.s
76
77# a.out
78asm/dx86-out.o: asm/dx86unix.cpp
79 $(CPP) -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
80
81asm/yx86-out.o: asm/yx86unix.cpp
82 $(CPP) -DOUT asm/yx86unix.cpp | as -o asm/yx86-out.o
83
84# bsdi
85asm/dx86bsdi.o: asm/dx86unix.cpp
86 $(CPP) -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
87
88asm/yx86bsdi.o: asm/yx86unix.cpp
89 $(CPP) -DBSDI asm/yx86unix.cpp | as -o asm/yx86bsdi.o
90
91asm/dx86unix.cpp:
92 (cd asm; perl des-586.pl cpp >dx86unix.cpp)
93
94asm/yx86unix.cpp:
95 (cd asm; perl crypt586.pl cpp >yx86unix.cpp)
96
97files:
98 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
99
100links:
101 /bin/rm -f Makefile
102 $(TOP)/util/point.sh Makefile.ssl Makefile
103 /bin/rm -f des.doc
104 /bin/rm -fr asm/perlasm
105 $(TOP)/util/point.sh ../../perlasm asm/perlasm
106 $(TOP)/util/point.sh ../../doc/des.doc des.doc
107 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
108 $(TOP)/util/mklink.sh ../../test $(TEST)
109 $(TOP)/util/mklink.sh ../../apps $(APPS)
110
111install: installs
112
113installs:
114 @for i in $(EXHEADER) ; \
115 do \
116 (cp $$i $(INSTALLTOP)/include/$$i; \
117 chmod 644 $(INSTALLTOP)/include/$$i ); \
118 done;
119
120tags:
121 ctags $(SRC)
122
123tests:
124
125lint:
126 lint -DLINT $(INCLUDES) $(SRC)>fluff
127
128depend:
129 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
130
131dclean:
132 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
133 mv -f Makefile.new $(MAKEFILE)
134
135clean:
136 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
137
138errors:
139
140# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/dh/Makefile.ssl b/src/lib/libcrypto/dh/Makefile.ssl
new file mode 100644
index 0000000000..dfa7e4525d
--- /dev/null
+++ b/src/lib/libcrypto/dh/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/dh/Makefile
3#
4
5DIR= dh
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=dh
19ERRC=dh_err
20GENERAL=Makefile
21TEST= dhtest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= dh_gen.c dh_key.c dh_lib.c dh_check.c $(ERRC).c
26LIBOBJ= dh_gen.o dh_key.o dh_lib.o dh_check.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= dh.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile ;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/dsa/Makefile.ssl b/src/lib/libcrypto/dsa/Makefile.ssl
new file mode 100644
index 0000000000..2cc4ddb39e
--- /dev/null
+++ b/src/lib/libcrypto/dsa/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/dsa/Makefile
3#
4
5DIR= dsa
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=dsa
19ERRC=dsa_err
20GENERAL=Makefile
21TEST=dsatest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= dsa_gen.c dsa_key.c dsa_lib.c dsa_vrf.c dsa_sign.c $(ERRC).c
26LIBOBJ= dsa_gen.o dsa_key.o dsa_lib.o dsa_vrf.o dsa_sign.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= dsa.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile ;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/err/Makefile.ssl b/src/lib/libcrypto/err/Makefile.ssl
new file mode 100644
index 0000000000..57c87eb041
--- /dev/null
+++ b/src/lib/libcrypto/err/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/err/Makefile
3#
4
5DIR= err
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=err.c err_all.c err_prn.c
24LIBOBJ=err.o err_all.o err_prn.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= err.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/evp/Makefile.ssl b/src/lib/libcrypto/evp/Makefile.ssl
new file mode 100644
index 0000000000..8bf2516458
--- /dev/null
+++ b/src/lib/libcrypto/evp/Makefile.ssl
@@ -0,0 +1,111 @@
1#
2# SSLeay/crypto/evp/Makefile
3#
4
5DIR= evp
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=evp
19ERRC=evp_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= encode.c digest.c evp_enc.c evp_key.c \
26 e_ecb_d.c e_cbc_d.c e_cfb_d.c e_ofb_d.c \
27 e_ecb_i.c e_cbc_i.c e_cfb_i.c e_ofb_i.c \
28 e_ecb_3d.c e_cbc_3d.c e_rc4.c names.c \
29 e_cfb_3d.c e_ofb_3d.c e_xcbc_d.c \
30 e_ecb_r2.c e_cbc_r2.c e_cfb_r2.c e_ofb_r2.c \
31 e_ecb_bf.c e_cbc_bf.c e_cfb_bf.c e_ofb_bf.c \
32 e_ecb_c.c e_cbc_c.c e_cfb_c.c e_ofb_c.c \
33 e_ecb_r5.c e_cbc_r5.c e_cfb_r5.c e_ofb_r5.c \
34 m_null.c m_md2.c m_md5.c m_sha.c m_sha1.c m_dss.c m_dss1.c m_mdc2.c \
35 m_ripemd.c \
36 p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
37 bio_md.c bio_b64.c bio_enc.c $(ERRC).c e_null.c \
38 c_all.c evp_lib.c
39
40LIBOBJ= encode.o digest.o evp_enc.o evp_key.o \
41 e_ecb_d.o e_cbc_d.o e_cfb_d.o e_ofb_d.o \
42 e_ecb_i.o e_cbc_i.o e_cfb_i.o e_ofb_i.o \
43 e_ecb_3d.o e_cbc_3d.o e_rc4.o names.o \
44 e_cfb_3d.o e_ofb_3d.o e_xcbc_d.o \
45 e_ecb_r2.o e_cbc_r2.o e_cfb_r2.o e_ofb_r2.o \
46 e_ecb_bf.o e_cbc_bf.o e_cfb_bf.o e_ofb_bf.o \
47 e_ecb_c.o e_cbc_c.o e_cfb_c.o e_ofb_c.o \
48 e_ecb_r5.o e_cbc_r5.o e_cfb_r5.o e_ofb_r5.o \
49 m_null.o m_md2.o m_md5.o m_sha.o m_sha1.o m_dss.o m_dss1.o m_mdc2.o \
50 m_ripemd.o \
51 p_open.o p_seal.o p_sign.o p_verify.o p_lib.o p_enc.o p_dec.o \
52 bio_md.o bio_b64.o bio_enc.o $(ERRC).o e_null.o \
53 c_all.o evp_lib.o
54
55SRC= $(LIBSRC)
56
57EXHEADER= evp.h
58HEADER= $(EXHEADER)
59
60ALL= $(GENERAL) $(SRC) $(HEADER)
61
62top:
63 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
64
65all: lib
66
67lib: $(LIBOBJ)
68 $(AR) $(LIB) $(LIBOBJ)
69 sh $(TOP)/util/ranlib.sh $(LIB)
70 @touch lib
71
72files:
73 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
79 $(TOP)/util/mklink.sh ../../test $(TEST)
80 $(TOP)/util/mklink.sh ../../apps $(APPS)
81
82install:
83 @for i in $(EXHEADER) ; \
84 do \
85 (cp $$i $(INSTALLTOP)/include/$$i; \
86 chmod 644 $(INSTALLTOP)/include/$$i ); \
87 done;
88
89tags:
90 ctags $(SRC)
91
92tests:
93
94lint:
95 lint -DLINT $(INCLUDES) $(SRC)>fluff
96
97depend:
98 $(MAKEDEPEND) $(INCLUDES) $(LIBSRC)
99
100dclean:
101 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
102 mv -f Makefile.new $(MAKEFILE)
103
104clean:
105 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
106
107errors:
108 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
109 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
110
111# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/hmac/Makefile.ssl b/src/lib/libcrypto/hmac/Makefile.ssl
new file mode 100644
index 0000000000..7a042b7261
--- /dev/null
+++ b/src/lib/libcrypto/hmac/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/md/Makefile
3#
4
5DIR= hmac
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=hmactest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=hmac.c
24LIBOBJ=hmac.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= hmac.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/idea/Makefile.ssl b/src/lib/libcrypto/idea/Makefile.ssl
new file mode 100644
index 0000000000..41b42ce03b
--- /dev/null
+++ b/src/lib/libcrypto/idea/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/idea/Makefile
3#
4
5DIR= idea
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=ideatest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=i_cbc.c i_cfb64.c i_ofb64.c i_ecb.c i_skey.c
24LIBOBJ=i_cbc.o i_cfb64.o i_ofb64.o i_ecb.o i_skey.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= idea.h
29HEADER= idea_lcl.h $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/lhash/Makefile.ssl b/src/lib/libcrypto/lhash/Makefile.ssl
new file mode 100644
index 0000000000..cb08547b4f
--- /dev/null
+++ b/src/lib/libcrypto/lhash/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/lhash/Makefile
3#
4
5DIR= lhash
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=lhash.c lh_stats.c
24LIBOBJ=lhash.o lh_stats.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= lhash.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/md2/Makefile.ssl b/src/lib/libcrypto/md2/Makefile.ssl
new file mode 100644
index 0000000000..d8e7200c83
--- /dev/null
+++ b/src/lib/libcrypto/md2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/md/Makefile
3#
4
5DIR= md
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=md2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=md2_dgst.c md5_one.c
24LIBOBJ=md2_dgst.o md2_one.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= md2.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/md5/Makefile.ssl b/src/lib/libcrypto/md5/Makefile.ssl
new file mode 100644
index 0000000000..47e1ce05ca
--- /dev/null
+++ b/src/lib/libcrypto/md5/Makefile.ssl
@@ -0,0 +1,104 @@
1#
2# SSLeay/crypto/md5/Makefile
3#
4
5DIR= md5
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17MD5_ASM_OBJ=
18
19CFLAGS= $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22TEST=md5test.c
23APPS=md5.c
24
25LIB=$(TOP)/libcrypto.a
26LIBSRC=md5_dgst.c md5_one.c
27LIBOBJ=md5_dgst.o md5_one.o $(MD5_ASM_OBJ)
28
29SRC= $(LIBSRC)
30
31EXHEADER= md5.h
32HEADER= md5_locl.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46# elf
47asm/mx86-elf.o: asm/mx86unix.cpp
48 $(CPP) -DELF asm/mx86unix.cpp | as -o asm/mx86-elf.o
49
50# solaris
51asm/mx86-sol.o: asm/mx86unix.cpp
52 $(CC) -E -DSOL asm/mx86unix.cpp | sed 's/^#.*//' > asm/mx86-sol.s
53 as -o asm/mx86-sol.o asm/mx86-sol.s
54 rm -f asm/mx86-sol.s
55
56# a.out
57asm/mx86-out.o: asm/mx86unix.cpp
58 $(CPP) -DOUT asm/mx86unix.cpp | as -o asm/mx86-out.o
59
60# bsdi
61asm/mx86bsdi.o: asm/mx86unix.cpp
62 $(CPP) -DBSDI asm/mx86unix.cpp | as -o asm/mx86bsdi.o
63
64asm/mx86unix.cpp:
65 (cd asm; perl md5-586.pl cpp >mx86unix.cpp)
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69
70links:
71 /bin/rm -f Makefile
72 $(TOP)/util/point.sh Makefile.ssl Makefile ;
73 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
74 $(TOP)/util/mklink.sh ../../test $(TEST)
75 $(TOP)/util/mklink.sh ../../apps $(APPS)
76
77install:
78 @for i in $(EXHEADER) ; \
79 do \
80 (cp $$i $(INSTALLTOP)/include/$$i; \
81 chmod 644 $(INSTALLTOP)/include/$$i ); \
82 done;
83
84tags:
85 ctags $(SRC)
86
87tests:
88
89lint:
90 lint -DLINT $(INCLUDES) $(SRC)>fluff
91
92depend:
93 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
94
95dclean:
96 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
97 mv -f Makefile.new $(MAKEFILE)
98
99clean:
100 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
101
102errors:
103
104# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/mdc2/Makefile.ssl b/src/lib/libcrypto/mdc2/Makefile.ssl
new file mode 100644
index 0000000000..495a2789a0
--- /dev/null
+++ b/src/lib/libcrypto/mdc2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/mdc2/Makefile
3#
4
5DIR= mdc2
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST= mdc2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=mdc2dgst.c mdc2_one.c
24LIBOBJ=mdc2dgst.o mdc2_one.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= mdc2.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/objects/Makefile.ssl b/src/lib/libcrypto/objects/Makefile.ssl
new file mode 100644
index 0000000000..320523cea1
--- /dev/null
+++ b/src/lib/libcrypto/objects/Makefile.ssl
@@ -0,0 +1,87 @@
1#
2# SSLeay/crypto/objects/Makefile
3#
4
5DIR= objects
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=objects
19ERRC=obj_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= obj_dat.c obj_lib.c $(ERRC).c
26LIBOBJ= obj_dat.o obj_lib.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= objects.h
31HEADER= $(EXHEADER) obj_dat.h
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: obj_dat.h lib
39
40obj_dat.h: objects.h obj_dat.pl
41 perl ./obj_dat.pl < objects.h > obj_dat.h
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48files:
49 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
50
51links:
52 /bin/rm -f Makefile
53 $(TOP)/util/point.sh Makefile.ssl Makefile ;
54 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
55 $(TOP)/util/mklink.sh ../../test $(TEST)
56 $(TOP)/util/mklink.sh ../../apps $(APPS)
57
58install:
59 @for i in $(EXHEADER) ; \
60 do \
61 (cp $$i $(INSTALLTOP)/include/$$i; \
62 chmod 644 $(INSTALLTOP)/include/$$i ); \
63 done;
64
65tags:
66 ctags $(SRC)
67
68tests:
69
70lint:
71 lint -DLINT $(INCLUDES) $(SRC)>fluff
72
73depend:
74 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
75
76dclean:
77 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
78 mv -f Makefile.new $(MAKEFILE)
79
80clean:
81 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
82
83errors:
84 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
85 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
86
87# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/pem/Makefile.ssl b/src/lib/libcrypto/pem/Makefile.ssl
new file mode 100644
index 0000000000..fc04a88fd9
--- /dev/null
+++ b/src/lib/libcrypto/pem/Makefile.ssl
@@ -0,0 +1,96 @@
1#
2# SSLeay/crypto/pem/Makefile
3#
4
5DIR= pem
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=pem
19ERRC=pem_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24CTX_SIZE= ctx_size
25
26LIB=$(TOP)/libcrypto.a
27LIBSRC= pem_sign.c pem_seal.c pem_info.c pem_lib.c pem_all.c $(ERRC).c
28
29LIBOBJ= pem_sign.o pem_seal.o pem_info.o pem_lib.o pem_all.o $(ERRC).o
30
31SRC= $(LIBSRC)
32
33EXHEADER= pem.h
34HEADER= $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: pem.h lib
42
43pem.h: $(CTX_SIZE)
44 ./$(CTX_SIZE) <pem.org >pem.new
45 if [ -f pem.h ]; then mv -f pem.h pem.old; fi
46 mv -f pem.new pem.h
47
48$(CTX_SIZE): $(CTX_SIZE).o
49 $(CC) $(CFLAGS) -o $(CTX_SIZE) $(CTX_SIZE).o
50
51lib: $(LIBOBJ)
52 $(AR) $(LIB) $(LIBOBJ)
53 sh $(TOP)/util/ranlib.sh $(LIB)
54 @touch lib
55
56files:
57 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
58
59links:
60 /bin/rm -f Makefile
61 $(TOP)/util/point.sh Makefile.ssl Makefile ;
62 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
63 $(TOP)/util/mklink.sh ../../test $(TEST)
64 $(TOP)/util/mklink.sh ../../apps $(APPS)
65
66install:
67 @for i in $(EXHEADER) ; \
68 do \
69 (cp $$i $(INSTALLTOP)/include/$$i; \
70 chmod 644 $(INSTALLTOP)/include/$$i ); \
71 done;
72
73tags:
74 ctags $(SRC)
75
76tests:
77
78lint:
79 lint -DLINT $(INCLUDES) $(SRC)>fluff
80
81depend:
82 $(MAKEDEPEND) $(INCLUDES) $(CTX_SIZE).c $(LIBSRC)
83
84dclean:
85 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
86 mv -f Makefile.new $(MAKEFILE)
87
88clean:
89 /bin/rm -f $(CTX_SIZE) *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
90
91errors:
92 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # SPECIAL CASE .org
93 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
94 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
95
96# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/pkcs7/Makefile.ssl b/src/lib/libcrypto/pkcs7/Makefile.ssl
new file mode 100644
index 0000000000..a88359b320
--- /dev/null
+++ b/src/lib/libcrypto/pkcs7/Makefile.ssl
@@ -0,0 +1,86 @@
1#
2# SSLeay/crypto/asn1/Makefile
3#
4
5DIR= pkcs7
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=pkcs7
19ERRC=pkcs7err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= pk7_lib.c pkcs7err.c pk7_doit.c
26LIBOBJ= pk7_lib.o pkcs7err.o pk7_doit.o
27
28SRC= $(LIBSRC)
29
30EXHEADER= pkcs7.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38test:
39
40all: lib
41
42lib: $(LIBOBJ)
43 $(AR) $(LIB) $(LIBOBJ)
44 sh $(TOP)/util/ranlib.sh $(LIB)
45 @touch lib
46
47files:
48 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
49
50links:
51 /bin/rm -f Makefile
52 $(TOP)/util/point.sh Makefile.ssl Makefile ;
53 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
54 $(TOP)/util/mklink.sh ../../test $(TEST)
55 $(TOP)/util/mklink.sh ../../apps $(APPS)
56
57install:
58 @for i in $(EXHEADER) ; \
59 do \
60 (cp $$i $(INSTALLTOP)/include/$$i; \
61 chmod 644 $(INSTALLTOP)/include/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
74
75dclean:
76 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
77 mv -f Makefile.new $(MAKEFILE)
78
79clean:
80 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
81
82errors:
83 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
84 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
85
86# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rand/Makefile.ssl b/src/lib/libcrypto/rand/Makefile.ssl
new file mode 100644
index 0000000000..d04f0a9b43
--- /dev/null
+++ b/src/lib/libcrypto/rand/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/rand/Makefile
3#
4
5DIR= rand
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST= randtest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=md_rand.c randfile.c
24LIBOBJ=md_rand.o randfile.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= rand.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rc2/Makefile.ssl b/src/lib/libcrypto/rc2/Makefile.ssl
new file mode 100644
index 0000000000..c5138f13e2
--- /dev/null
+++ b/src/lib/libcrypto/rc2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/rc2/Makefile
3#
4
5DIR= rc2
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=rc2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=rc2_ecb.c rc2_skey.c rc2_cbc.c rc2cfb64.c rc2ofb64.c
24LIBOBJ=rc2_ecb.o rc2_skey.o rc2_cbc.o rc2cfb64.o rc2ofb64.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= rc2.h
29HEADER= rc2_locl.h $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rc4/Makefile.ssl b/src/lib/libcrypto/rc4/Makefile.ssl
new file mode 100644
index 0000000000..19c1e980f3
--- /dev/null
+++ b/src/lib/libcrypto/rc4/Makefile.ssl
@@ -0,0 +1,108 @@
1#
2# SSLeay/crypto/rc4/Makefile
3#
4
5DIR= rc4
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16RC4_ENC=rc4_enc.o
17# or use
18#RC4_ENC=asm/rx86-elf.o
19#RC4_ENC=asm/rx86-out.o
20#RC4_ENC=asm/rx86-sol.o
21#RC4_ENC=asm/rx86bdsi.o
22
23CFLAGS= $(INCLUDES) $(CFLAG)
24
25GENERAL=Makefile
26TEST=rc4test.c
27APPS=
28
29LIB=$(TOP)/libcrypto.a
30LIBSRC=rc4_skey.c rc4_enc.c
31LIBOBJ=rc4_skey.o $(RC4_ENC)
32
33SRC= $(LIBSRC)
34
35EXHEADER= rc4.h
36HEADER= $(EXHEADER) rc4_locl.h
37
38ALL= $(GENERAL) $(SRC) $(HEADER)
39
40top:
41 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
42
43all: lib
44
45lib: $(LIBOBJ)
46 $(AR) $(LIB) $(LIBOBJ)
47 sh $(TOP)/util/ranlib.sh $(LIB)
48 @touch lib
49
50# elf
51asm/rx86-elf.o: asm/rx86unix.cpp
52 $(CPP) -DELF asm/rx86unix.cpp | as -o asm/rx86-elf.o
53
54# solaris
55asm/rx86-sol.o: asm/rx86unix.cpp
56 $(CC) -E -DSOL asm/rx86unix.cpp | sed 's/^#.*//' > asm/rx86-sol.s
57 as -o asm/rx86-sol.o asm/rx86-sol.s
58 rm -f asm/rx86-sol.s
59
60# a.out
61asm/rx86-out.o: asm/rx86unix.cpp
62 $(CPP) -DOUT asm/rx86unix.cpp | as -o asm/rx86-out.o
63
64# bsdi
65asm/rx86bsdi.o: asm/rx86unix.cpp
66 $(CPP) -DBSDI asm/rx86unix.cpp | as -o asm/rx86bsdi.o
67
68asm/rx86unix.cpp:
69 (cd asm; perl rc4-586.pl cpp >rx86unix.cpp)
70
71files:
72 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
73
74links:
75 /bin/rm -f Makefile
76 $(TOP)/util/point.sh Makefile.ssl Makefile ;
77 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
78 $(TOP)/util/mklink.sh ../../test $(TEST)
79 $(TOP)/util/mklink.sh ../../apps $(APPS)
80
81install:
82 @for i in $(EXHEADER) ; \
83 do \
84 (cp $$i $(INSTALLTOP)/include/$$i; \
85 chmod 644 $(INSTALLTOP)/include/$$i ); \
86 done;
87
88tags:
89 ctags $(SRC)
90
91tests:
92
93lint:
94 lint -DLINT $(INCLUDES) $(SRC)>fluff
95
96depend:
97 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
98
99dclean:
100 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
101 mv -f Makefile.new $(MAKEFILE)
102
103clean:
104 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff asm/*.o
105
106errors:
107
108# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rc5/Makefile.ssl b/src/lib/libcrypto/rc5/Makefile.ssl
new file mode 100644
index 0000000000..5e98ee2348
--- /dev/null
+++ b/src/lib/libcrypto/rc5/Makefile.ssl
@@ -0,0 +1,107 @@
1#
2# SSLeay/crypto/rc5/Makefile
3#
4
5DIR= rc5
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17RC5_ENC= rc5_enc.o
18# or use
19#DES_ENC= r586-elf.o
20
21CFLAGS= $(INCLUDES) $(CFLAG)
22
23GENERAL=Makefile
24TEST=rc5test.c
25APPS=
26
27LIB=$(TOP)/libcrypto.a
28LIBSRC=rc5_skey.c rc5_ecb.c rc5_enc.c rc5cfb64.c rc5ofb64.c
29LIBOBJ=rc5_skey.o rc5_ecb.o $(RC5_ENC) rc5cfb64.o rc5ofb64.o
30
31SRC= $(LIBSRC)
32
33EXHEADER= rc5.h
34HEADER= rc5_locl.h $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: lib
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48# elf
49asm/r586-elf.o: asm/r586unix.cpp
50 $(CPP) -DELF asm/r586unix.cpp | as -o asm/r586-elf.o
51
52# solaris
53asm/r586-sol.o: asm/r586unix.cpp
54 $(CC) -E -DSOL asm/r586unix.cpp | sed 's/^#.*//' > asm/r586-sol.s
55 as -o asm/r586-sol.o asm/r586-sol.s
56 rm -f asm/r586-sol.s
57
58# a.out
59asm/r586-out.o: asm/r586unix.cpp
60 $(CPP) -DOUT asm/r586unix.cpp | as -o asm/r586-out.o
61
62# bsdi
63asm/r586bsdi.o: asm/r586unix.cpp
64 $(CPP) -DBSDI asm/r586unix.cpp | as -o asm/r586bsdi.o
65
66asm/r586unix.cpp:
67 (cd asm; perl rc5-586.pl cpp >r586unix.cpp)
68
69files:
70 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
71
72links:
73 /bin/rm -f Makefile
74 $(TOP)/util/point.sh Makefile.ssl Makefile ;
75 $(TOP)/util/point.sh ../../doc/rc5.doc rc5.doc ;
76 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
77 $(TOP)/util/mklink.sh ../../test $(TEST)
78 $(TOP)/util/mklink.sh ../../apps $(APPS)
79
80install:
81 @for i in $(EXHEADER) ; \
82 do \
83 (cp $$i $(INSTALLTOP)/include/$$i; \
84 chmod 644 $(INSTALLTOP)/include/$$i ); \
85 done;
86
87tags:
88 ctags $(SRC)
89
90tests:
91
92lint:
93 lint -DLINT $(INCLUDES) $(SRC)>fluff
94
95depend:
96 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
97
98dclean:
99 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
100 mv -f Makefile.new $(MAKEFILE)
101
102clean:
103 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
104
105errors:
106
107# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rc5/asm/rc5-586.pl b/src/lib/libcrypto/rc5/asm/rc5-586.pl
new file mode 100644
index 0000000000..172bd9ee1b
--- /dev/null
+++ b/src/lib/libcrypto/rc5/asm/rc5-586.pl
@@ -0,0 +1,109 @@
1#!/usr/bin/perl
2
3push(@INC,"perlasm","../../perlasm");
4require "x86asm.pl";
5require "cbc.pl";
6
7&asm_init($ARGV[0],"rc5-586.pl");
8
9$RC5_MAX_ROUNDS=16;
10$RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4;
11$A="edi";
12$B="esi";
13$S="ebp";
14$tmp1="eax";
15$r="ebx";
16$tmpc="ecx";
17$tmp4="edx";
18
19&RC5_32_encrypt("RC5_32_encrypt",1);
20&RC5_32_encrypt("RC5_32_decrypt",0);
21&cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1);
22&asm_finish();
23
24sub RC5_32_encrypt
25 {
26 local($name,$enc)=@_;
27
28 &function_begin_B($name,"");
29
30 &comment("");
31
32 &push("ebp");
33 &push("esi");
34 &push("edi");
35 &mov($tmp4,&wparam(0));
36 &mov($S,&wparam(1));
37
38 &comment("Load the 2 words");
39 &mov($A,&DWP(0,$tmp4,"",0));
40 &mov($B,&DWP(4,$tmp4,"",0));
41
42 &push($r);
43 &mov($r, &DWP(0,$S,"",0));
44
45 # encrypting part
46
47 if ($enc)
48 {
49 &add($A, &DWP(4+0,$S,"",0));
50 &add($B, &DWP(4+4,$S,"",0));
51
52 for ($i=0; $i<$RC5_MAX_ROUNDS; $i++)
53 {
54 &xor($A, $B);
55 &mov($tmp1, &DWP(12+$i*8,$S,"",0));
56 &mov($tmpc, $B);
57 &rotl($A, &LB("ecx"));
58 &add($A, $tmp1);
59
60 &xor($B, $A);
61 &mov($tmp1, &DWP(16+$i*8,$S,"",0));
62 &mov($tmpc, $A);
63 &rotl($B, &LB("ecx"));
64 &add($B, $tmp1);
65 if (($i == 7) || ($i == 11))
66 {
67 &cmp($r, $i+1);
68 &je(&label("rc5_exit"));
69 }
70 }
71 }
72 else
73 {
74 &cmp($r, 12);
75 &je(&label("rc5_dec_12"));
76 &cmp($r, 8);
77 &je(&label("rc5_dec_8"));
78 for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--)
79 {
80 &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8);
81 &mov($tmp1, &DWP($i*8+8,$S,"",0));
82 &sub($B, $tmp1);
83 &mov($tmpc, $A);
84 &rotr($B, &LB("ecx"));
85 &xor($B, $A);
86
87 &mov($tmp1, &DWP($i*8+4,$S,"",0));
88 &sub($A, $tmp1);
89 &mov($tmpc, $B);
90 &rotr($A, &LB("ecx"));
91 &xor($A, $B);
92 }
93 &sub($B, &DWP(4+4,$S,"",0));
94 &sub($A, &DWP(4+0,$S,"",0));
95 }
96
97 &set_label("rc5_exit");
98 &mov(&DWP(0,$tmp4,"",0),$A);
99 &mov(&DWP(4,$tmp4,"",0),$B);
100
101 &pop("ebx");
102 &pop("edi");
103 &pop("esi");
104 &pop("ebp");
105 &ret();
106 &function_end_B($name);
107 }
108
109
diff --git a/src/lib/libcrypto/ripemd/Makefile.ssl b/src/lib/libcrypto/ripemd/Makefile.ssl
new file mode 100644
index 0000000000..67d47ceb2c
--- /dev/null
+++ b/src/lib/libcrypto/ripemd/Makefile.ssl
@@ -0,0 +1,104 @@
1#
2# SSLeay/crypto/ripemd/Makefile
3#
4
5DIR= ripemd
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17RIP_ASM_OBJ=
18
19CFLAGS= $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22TEST=rmdtest.c
23APPS=rmd160.c
24
25LIB=$(TOP)/libcrypto.a
26LIBSRC=rmd_dgst.c rmd_one.c
27LIBOBJ=rmd_dgst.o rmd_one.o $(RMD160_ASM_OBJ)
28
29SRC= $(LIBSRC)
30
31EXHEADER= ripemd.h
32HEADER= rmd_locl.h rmdconst.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46# elf
47asm/rm86-elf.o: asm/rm86unix.cpp
48 $(CPP) -DELF asm/rm86unix.cpp | as -o asm/rm86-elf.o
49
50# solaris
51asm/rm86-sol.o: asm/rm86unix.cpp
52 $(CC) -E -DSOL asm/rm86unix.cpp | sed 's/^#.*//' > asm/rm86-sol.s
53 as -o asm/rm86-sol.o asm/rm86-sol.s
54 rm -f asm/rm86-sol.s
55
56# a.out
57asm/rm86-out.o: asm/rm86unix.cpp
58 $(CPP) -DOUT asm/rm86unix.cpp | as -o asm/rm86-out.o
59
60# bsdi
61asm/rm86bsdi.o: asm/rm86unix.cpp
62 $(CPP) -DBSDI asm/rm86unix.cpp | as -o asm/rm86bsdi.o
63
64asm/rm86unix.cpp:
65 (cd asm; perl rmd-586.pl cpp >rm86unix.cpp)
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69
70links:
71 /bin/rm -f Makefile
72 $(TOP)/util/point.sh Makefile.ssl Makefile ;
73 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
74 $(TOP)/util/mklink.sh ../../test $(TEST)
75 $(TOP)/util/mklink.sh ../../apps $(APPS)
76
77install:
78 @for i in $(EXHEADER) ; \
79 do \
80 (cp $$i $(INSTALLTOP)/include/$$i; \
81 chmod 644 $(INSTALLTOP)/include/$$i ); \
82 done;
83
84tags:
85 ctags $(SRC)
86
87tests:
88
89lint:
90 lint -DLINT $(INCLUDES) $(SRC)>fluff
91
92depend:
93 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
94
95dclean:
96 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
97 mv -f Makefile.new $(MAKEFILE)
98
99clean:
100 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
101
102errors:
103
104# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/rsa/Makefile.ssl b/src/lib/libcrypto/rsa/Makefile.ssl
new file mode 100644
index 0000000000..d52f2e609e
--- /dev/null
+++ b/src/lib/libcrypto/rsa/Makefile.ssl
@@ -0,0 +1,86 @@
1#
2# SSLeay/crypto/rsa/Makefile
3#
4
5DIR= rsa
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=rsa
19ERRC=rsa_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c $(ERRC).c \
26 rsa_pk1.c rsa_ssl.c rsa_none.c
27LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o $(ERRC).o \
28 rsa_pk1.o rsa_ssl.o rsa_none.o
29
30SRC= $(LIBSRC)
31
32EXHEADER= rsa.h
33HEADER= $(EXHEADER)
34
35ALL= $(GENERAL) $(SRC) $(HEADER)
36
37top:
38 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
39
40all: lib
41
42lib: $(LIBOBJ)
43 $(AR) $(LIB) $(LIBOBJ)
44 sh $(TOP)/util/ranlib.sh $(LIB)
45 @touch lib
46
47files:
48 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
49
50links:
51 /bin/rm -f Makefile
52 $(TOP)/util/point.sh Makefile.ssl Makefile ;
53 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
54 $(TOP)/util/mklink.sh ../../test $(TEST)
55 $(TOP)/util/mklink.sh ../../apps $(APPS)
56
57install:
58 @for i in $(EXHEADER) ; \
59 do \
60 (cp $$i $(INSTALLTOP)/include/$$i; \
61 chmod 644 $(INSTALLTOP)/include/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
74
75dclean:
76 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
77 mv -f Makefile.new $(MAKEFILE)
78
79clean:
80 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
81
82errors:
83 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
84 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
85
86# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/sha/Makefile.ssl b/src/lib/libcrypto/sha/Makefile.ssl
new file mode 100644
index 0000000000..eeb545d140
--- /dev/null
+++ b/src/lib/libcrypto/sha/Makefile.ssl
@@ -0,0 +1,103 @@
1#
2# SSLeay/crypto/sha/Makefile
3#
4
5DIR= sha
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16SHA1_ASM_OBJ=
17
18CFLAGS= $(INCLUDES) $(CFLAG)
19
20GENERAL=Makefile
21TEST=shatest.c sha1test.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC=sha_dgst.c sha1dgst.c sha_one.c sha1_one.c
26LIBOBJ=sha_dgst.o sha1dgst.o sha_one.o sha1_one.o $(SHA1_ASM_OBJ)
27
28SRC= $(LIBSRC)
29
30EXHEADER= sha.h
31HEADER= sha_locl.h $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45# elf
46asm/sx86-elf.o: asm/sx86unix.cpp
47 $(CPP) -DELF asm/sx86unix.cpp | as -o asm/sx86-elf.o
48
49# solaris
50asm/sx86-sol.o: asm/sx86unix.cpp
51 $(CC) -E -DSOL asm/sx86unix.cpp | sed 's/^#.*//' > asm/sx86-sol.s
52 as -o asm/sx86-sol.o asm/sx86-sol.s
53 rm -f asm/sx86-sol.s
54
55# a.out
56asm/sx86-out.o: asm/sx86unix.cpp
57 $(CPP) -DOUT asm/sx86unix.cpp | as -o asm/sx86-out.o
58
59# bsdi
60asm/sx86bsdi.o: asm/sx86unix.cpp
61 $(CPP) -DBSDI asm/sx86unix.cpp | as -o asm/sx86bsdi.o
62
63asm/sx86unix.cpp:
64 (cd asm; perl sha1-586.pl cpp >sx86unix.cpp)
65
66files:
67 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
68
69links:
70 /bin/rm -f Makefile
71 $(TOP)/util/point.sh Makefile.ssl Makefile ;
72 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
73 $(TOP)/util/mklink.sh ../../test $(TEST)
74 $(TOP)/util/mklink.sh ../../apps $(APPS)
75
76install:
77 @for i in $(EXHEADER) ; \
78 do \
79 (cp $$i $(INSTALLTOP)/include/$$i; \
80 chmod 644 $(INSTALLTOP)/include/$$i ); \
81 done;
82
83tags:
84 ctags $(SRC)
85
86tests:
87
88lint:
89 lint -DLINT $(INCLUDES) $(SRC)>fluff
90
91depend:
92 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
93
94dclean:
95 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
96 mv -f Makefile.new $(MAKEFILE)
97
98clean:
99 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff asm/*.o
100
101errors:
102
103# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/stack/Makefile.ssl b/src/lib/libcrypto/stack/Makefile.ssl
new file mode 100644
index 0000000000..0d232c08cf
--- /dev/null
+++ b/src/lib/libcrypto/stack/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/stack/Makefile
3#
4
5DIR= stack
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=stack.c
24LIBOBJ=stack.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= stack.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/txt_db/Makefile.ssl b/src/lib/libcrypto/txt_db/Makefile.ssl
new file mode 100644
index 0000000000..76e511534f
--- /dev/null
+++ b/src/lib/libcrypto/txt_db/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/txt_db/Makefile
3#
4
5DIR= txt_db
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=txt_db.c
24LIBOBJ=txt_db.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= txt_db.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libcrypto/x509/Makefile.ssl b/src/lib/libcrypto/x509/Makefile.ssl
new file mode 100644
index 0000000000..1c1ca2ffa0
--- /dev/null
+++ b/src/lib/libcrypto/x509/Makefile.ssl
@@ -0,0 +1,96 @@
1#
2# SSLeay/crypto/x509/Makefile
3#
4
5DIR= x509
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=x509
19ERRC=x509_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= x509_def.c x509_d2.c x509_r2x.c x509_cmp.c \
26 x509_obj.c x509_req.c x509_vfy.c \
27 x509_set.c x509rset.c $(ERRC).c \
28 x509name.c x509_v3.c x509_ext.c x509pack.c \
29 x509type.c x509_lu.c x_all.c x509_txt.c \
30 by_file.c by_dir.c \
31 v3_net.c v3_x509.c
32LIBOBJ= x509_def.o x509_d2.o x509_r2x.o x509_cmp.o \
33 x509_obj.o x509_req.o x509_vfy.o \
34 x509_set.o x509rset.o $(ERRC).o \
35 x509name.o x509_v3.o x509_ext.o x509pack.o \
36 x509type.o x509_lu.o x_all.o x509_txt.o \
37 by_file.o by_dir.o \
38 v3_net.o v3_x509.o
39
40SRC= $(LIBSRC)
41
42EXHEADER= x509.h x509_vfy.h
43HEADER= $(EXHEADER)
44
45ALL= $(GENERAL) $(SRC) $(HEADER)
46
47top:
48 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
49
50all: lib
51
52lib: $(LIBOBJ)
53 $(AR) $(LIB) $(LIBOBJ)
54 sh $(TOP)/util/ranlib.sh $(LIB)
55 @touch lib
56
57files:
58 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
59
60links:
61 /bin/rm -f Makefile
62 $(TOP)/util/point.sh Makefile.ssl Makefile ;
63 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
64 $(TOP)/util/mklink.sh ../../test $(TEST)
65 $(TOP)/util/mklink.sh ../../apps $(APPS)
66
67install:
68 @for i in $(EXHEADER) ; \
69 do \
70 (cp $$i $(INSTALLTOP)/include/$$i; \
71 chmod 644 $(INSTALLTOP)/include/$$i ); \
72 done;
73
74tags:
75 ctags $(SRC)
76
77tests:
78
79lint:
80 lint -DLINT $(INCLUDES) $(SRC)>fluff
81
82depend:
83 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
84
85dclean:
86 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
87 mv -f Makefile.new $(MAKEFILE)
88
89clean:
90 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
91
92errors:
93 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
94 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
95
96# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/Makefile.ssl b/src/lib/libssl/src/Makefile.ssl
new file mode 100644
index 0000000000..da7f885985
--- /dev/null
+++ b/src/lib/libssl/src/Makefile.ssl
@@ -0,0 +1,331 @@
1#
2# Makefile for all the SSL related library routines and utilities
3VERSION = 0.9.0a
4#
5# make install will install:
6# libraries into $INSTALLTOP/lib
7# headers into $INSTALLTOP/include
8# utilities into $INSTALLTOP/bin
9#
10# By default INSTALLTOP is set to /usr/local/ssl
11# If you want things install elsewere, consider running
12# perl util/ssldir.pl /new/path
13#
14# Interesting Mailing Lists:
15# ssl-bugs@mincom.oz.au
16# ssl-users@mincom.oz.au
17#
18# To join the Mailing Lists:
19# ssl-bugs-request@mincom.oz.au
20# ssl-users-request@mincom.oz.au
21#
22# If you must get hold of people directly (we much prefer the above
23# lists to be used if the question is of general interest!):
24# Eric Young <eay@cryptsoft.com>
25# Tim Hudson <tjh@cryptsoft.com>
26# or both <ssleay@cryptsoft.com>
27#
28# The primary distribution of SSLeay is from
29# ftp://ftp.psy.uq.oz.au/pub/Crypto/SSL
30#
31# NOCONST - Define for C compilers that don't like the const key word.
32# NOPROTO - Define in if your compiler does not support prototypes.
33# RSAref - Define if we are to link with RSAref.
34# NO_IDEA - Define to build without the IDEA algorithm
35# NO_RC4 - Define to build without the RC4 algorithm
36# NO_RC2 - Define to build without the RC2 algorithm
37# THREADS - Define when building with threads, you will probably also need any
38# system defines as well, i.e. _REENTERANT for Solaris 2.[34]
39# TERMIO - Define the termio terminal subsystem, needed if sgtty is missing.
40# TERMIOS - Define the termios terminal subsystem, Silicon Graphics.
41# LONGCRYPT - Define to use HPUX 10.x's long password modification to crypt(3).
42# DEVRANDOM - Give this the value of the 'random device' if your OS supports
43# one. 32 bytes will be read from this when the random
44# number generator is initalised.
45# SSL_ALLOW_ADH - define if you want the server to be able to use the
46# SSLv3 anon-DH ciphers.
47# SSL_ALLOW_ENULL - define if you want the server to be able to use the
48# NULL encryption ciphers.
49#
50# LOCK_DEBUG - turns on lots of lock debug output :-)
51# REF_CHECK - turn on some xyz_free() assertions.
52# REF_PRINT - prints some stuff on structure free.
53# CRYPTO_MDEBUG - turns on my 'memory leak' detecting stuff
54# MFUNC - Make all Malloc/Free/Realloc calls call
55# CRYPTO_malloc/CRYPTO_free/CRYPTO_realloc which can be setup to
56# call application defined callbacks via CRYPTO_set_mem_functions()
57# MD5_ASM needs to be defined to use the x86 assembler for MD5
58# SHA1_ASM needs to be defined to use the x86 assembler for SHA1
59# RMD160_ASM needs to be defined to use the x86 assembler for RIPEMD160
60
61
62CC= gcc
63#CFLAG= -DL_ENDIAN -DTERMIO -O3 -fomit-frame-pointer -m486 -Wall -Wuninitialized -DMD5_ASM -DSHA1_ASM -DRMD160_ASM
64CFLAG= -DNO_IDEA -DTERMIOS -DBN_ASM -DL_ENDIAN -D_ANSI_SOURCE -fomit-frame-pointer -O3 -m486 -Wall -DSHA1_ASM -DMD5_ASM -DRMD160_ASM
65PEX_LIBS= -L. -L.. -L../.. -L../../..
66EX_LIBS=
67AR=ar r
68
69# Set BN_MULW to bn_mulw.o if you want to use the C version
70BN_MULW= asm/bn86-out.o
71#BN_MULW= bn_mulw.o
72#BN_MULW= asm/bn86-elf.o # elf, linux-elf
73#BN_MULW= asm/bn86-sol.o # solaris
74#BN_MULW= asm/bn86-out.o # a.out, FreeBSD
75#BN_MULW= asm/bn86bsdi.o # bsdi
76#BN_MULW= asm/alpha.o # DEC Alpha
77#BN_MULW= asm/pa-risc2.o # HP-UX PA-RISC
78#BN_MULW= asm/r3000.o # SGI MIPS cpu
79#BN_MULW= asm/sparc.o # Sun solaris/SunOS
80#BN_MULW= asm/bn-win32.o # Windows 95/NT
81#BN_MULW= asm/x86w16.o # 16 bit code for Windows 3.1/DOS
82#BN_MULW= asm/x86w32.o # 32 bit code for Windows 3.1
83
84# Set DES_ENC to des_enc.o if you want to use the C version
85#There are 4 x86 assember options.
86DES_ENC= asm/dx86-out.o asm/yx86-out.o
87#DES_ENC= des_enc.o fcrypt_b.o # C
88#DES_ENC= asm/dx86-elf.o asm/yx86-elf.o # elf
89#DES_ENC= asm/dx86-sol.o asm/yx86-sol.o # solaris
90#DES_ENC= asm/dx86-out.o asm/yx86-out.o # a.out, FreeBSD
91#DES_ENC= asm/dx86bsdi.o asm/yx86bsdi.o # bsdi
92
93# Set BF_ENC to bf_enc.o if you want to use the C version
94#There are 4 x86 assember options.
95BF_ENC= asm/bx86-out.o
96#BF_ENC= bf_enc.o
97#BF_ENC= asm/bx86-elf.o # elf
98#BF_ENC= asm/bx86-sol.o # solaris
99#BF_ENC= asm/bx86-out.o # a.out, FreeBSD
100#BF_ENC= asm/bx86bsdi.o # bsdi
101
102# Set CAST_ENC to c_enc.o if you want to use the C version
103#There are 4 x86 assember options.
104CAST_ENC= asm/cx86-out.o
105#CAST_ENC= c_enc.o
106#CAST_ENC= asm/cx86-elf.o # elf
107#CAST_ENC= asm/cx86-sol.o # solaris
108#CAST_ENC= asm/cx86-out.o # a.out, FreeBSD
109#CAST_ENC= asm/cx86bsdi.o # bsdi
110
111# Set RC4_ENC to rc4_enc.o if you want to use the C version
112#There are 4 x86 assember options.
113RC4_ENC= asm/rx86-out.o
114#RC4_ENC= rc4_enc.o
115#RC4_ENC= asm/rx86-elf.o # elf
116#RC4_ENC= asm/rx86-sol.o # solaris
117#RC4_ENC= asm/rx86-out.o # a.out, FreeBSD
118#RC4_ENC= asm/rx86bsdi.o # bsdi
119
120# Set RC5_ENC to rc5_enc.o if you want to use the C version
121#There are 4 x86 assember options.
122RC5_ENC= asm/r586-out.o
123#RC5_ENC= rc5_enc.o
124#RC5_ENC= asm/r586-elf.o # elf
125#RC5_ENC= asm/r586-sol.o # solaris
126#RC5_ENC= asm/r586-out.o # a.out, FreeBSD
127#RC5_ENC= asm/r586bsdi.o # bsdi
128
129# Also need MD5_ASM defined
130MD5_ASM_OBJ= asm/mx86-out.o
131#MD5_ASM_OBJ= asm/mx86-elf.o # elf
132#MD5_ASM_OBJ= asm/mx86-sol.o # solaris
133#MD5_ASM_OBJ= asm/mx86-out.o # a.out, FreeBSD
134#MD5_ASM_OBJ= asm/mx86bsdi.o # bsdi
135
136# Also need SHA1_ASM defined
137SHA1_ASM_OBJ= asm/sx86-out.o
138#SHA1_ASM_OBJ= asm/sx86-elf.o # elf
139#SHA1_ASM_OBJ= asm/sx86-sol.o # solaris
140#SHA1_ASM_OBJ= asm/sx86-out.o # a.out, FreeBSD
141#SHA1_ASM_OBJ= asm/sx86bsdi.o # bsdi
142
143# Also need RMD160_ASM defined
144RMD160_ASM_OBJ= asm/rm86-out.o
145#RMD160_ASM_OBJ= asm/rm86-elf.o # elf
146#RMD160_ASM_OBJ= asm/rm86-sol.o # solaris
147#RMD160_ASM_OBJ= asm/rm86-out.o # a.out, FreeBSD
148#RMD160_ASM_OBJ= asm/rm86bsdi.o # bsdi
149
150DIRS= crypto ssl rsaref apps test tools
151# dirs in crypto to build
152SDIRS= \
153 md2 md5 sha mdc2 hmac ripemd \
154 des rc2 rc4 rc5 idea bf cast \
155 bn rsa dsa dh \
156 buffer bio stack lhash rand err objects \
157 evp pem asn1 x509 conf txt_db pkcs7
158
159# If you change the INSTALLTOP, make sure to also change the values
160# in crypto/location.h
161INSTALLTOP=/usr/ssl
162
163MAKEFILE= Makefile.ssl
164MAKE= make -f Makefile.ssl
165
166MAN1=1
167MAN3=3
168SHELL=/bin/sh
169
170TOP= .
171ONEDIRS=out tmp
172EDIRS= times doc bugs util include certs ms shlib mt demos perl dep
173MISC= COPYRIGHT Configure HISTORY.066 INSTALL Makefile.ssl Makefile \
174 README TODO HISTORY README.066 README.080 README.090 \
175 VERSION PROBLEMS MINFO makefile.one e_os.h \
176 MICROSOFT makevms.com config PATENTS
177WDIRS= windows
178LIBS= libcrypto.a libssl.a
179
180GENERAL= Makefile
181BASENAME= SSLeay
182NAME= $(BASENAME)-$(VERSION)
183TARFILE= $(NAME).tar
184WTARFILE= $(NAME)-win.tar
185EXHEADER= e_os.h
186HEADER= e_os.h
187
188all:
189 @for i in $(DIRS) ;\
190 do \
191 (cd $$i; echo "making $$i..."; \
192 $(MAKE) CC='${CC}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' BN_MULW='${BN_MULW}' DES_ENC='${DES_ENC}' BF_ENC='${BF_ENC}' CAST_ENC='${CAST_ENC}' RC4_ENC='${RC4_ENC}' RC5_ENC='${RC5_ENC}' SHA1_ASM_OBJ='${SHA1_ASM_OBJ}' MD5_ASM_OBJ='${MD5_ASM_OBJ}' RMD160_ASM_OBJ='${RMD160_ASM_OBJ}' SDIRS='${SDIRS}' AR='${AR}' all ); \
193 done;
194
195sub_all:
196 @for i in $(DIRS) ;\
197 do \
198 (cd $$i; echo "making $$i..."; \
199 $(MAKE) CC='${CC}' CFLAG='${CFLAG}' SDIRS='$(SDIRS)' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' BN_MULW='${BN_MULW}' DES_ENC='${DES_ENC}' BF_ENC='${BF_ENC}' CAST_ENC='${CAST_ENC}' RC4_ENC='${RC4_ENC}' RC5_ENC='${RC5_ENC}' SHA1_ASM_OBJ='${SHA1_ASM_OBJ}' MD5_ASM_OBJ='${MD5_ASM_OBJ}' RMD160_ASM_OBJ='${RMD160_ASM_OBJ}' AR='${AR}' all ); \
200 done;
201
202clean:
203 /bin/rm -f shlib/*.o *.o core a.out fluff *.map
204 @for i in $(DIRS) ;\
205 do \
206 (cd $$i; echo "cleaning $$i..."; \
207 $(MAKE) SDIRS='${SDIRS}' clean ); \
208 /bin/rm -f $(LIBS); \
209 done;
210 /bin/rm -f *.a *.o speed.* *.map *.so .pure core
211 /bin/rm -f $(TARFILE)
212 @for i in $(ONEDIRS) ;\
213 do \
214 /bin/rm -fr $$i/*; \
215 done
216
217makefile.one: files
218 perl util/mk1mf.pl >makefile.one; \
219 sh util/do_ms.sh
220
221files: MINFO
222 perl $(TOP)/util/files.pl Makefile.ssl > $(TOP)/MINFO
223 @for i in $(DIRS) ;\
224 do \
225 (cd $$i; echo "making 'files' in $$i..."; \
226 $(MAKE) SDIRS='${SDIRS}' files ); \
227 done;
228
229links:
230 /bin/rm -f Makefile;
231 ./util/point.sh Makefile.ssl Makefile;
232 $(TOP)/util/mklink.sh include $(EXHEADER) ;
233 @for i in $(DIRS) ;\
234 do \
235 (cd $$i; echo "making links in $$i..."; \
236 $(MAKE) SDIRS='${SDIRS}' links ); \
237 done;
238 # @(cd apps; sh ./mklinks)
239 @( SSLEAY="`pwd`/apps/ssleay"; export SSLEAY; sh tools/c_rehash certs )
240
241dclean:
242 /bin/rm -f *.bak
243 @for i in $(DIRS) ;\
244 do \
245 (cd $$i; echo "undoing makedepend in $$i..."; \
246 $(MAKE) SDIRS='${SDIRS}' dclean ); \
247 done;
248
249rehash:
250 @(PATH="`pwd`/apps:${PATH}"; sh tools/c_rehash certs)
251
252test: tests
253
254tests:
255 (cd test; echo "testing $$i..."; \
256 $(MAKE) CC='${CC}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' BN_MULW='${BN_MULW}' DES_ENC='${DES_ENC}' BF_ENC='${BF_ENC}' CAST_ENC='${CAST_ENC}' RC4_ENC='${RC4_ENC}' RC5_ENC='${RC5_ENC}' SDIRS='${SDIRS}' SHA1_ASM_OBJ='${SHA1_ASM_OBJ}' MD5_ASM_OBJ='${MD5_ASM_OBJ}' RMD160_ASM_OBJ='${RMD160_ASM_OBJ}' AR='${AR}' tests );
257 @apps/ssleay version -a
258
259depend:
260 @for i in $(DIRS) ;\
261 do \
262 (cd $$i; echo "making dependancies $$i..."; \
263 $(MAKE) SDIRS='${SDIRS}' depend ); \
264 done;
265
266lint:
267 @for i in $(DIRS) ;\
268 do \
269 (cd $$i; echo "making lint $$i..."; \
270 $(MAKE) SDIRS='${SDIRS}' lint ); \
271 done;
272
273tags:
274 @for i in $(DIRS) ;\
275 do \
276 (cd $$i; echo "making tags $$i..."; \
277 $(MAKE) SDIRS='${SDIRS}' tags ); \
278 done;
279
280errors:
281 @for i in $(DIRS) ;\
282 do \
283 (cd $$i; echo "making errors in $$i..."; \
284 $(MAKE) SDIRS='${SDIRS}' errors ); \
285 done;
286
287tar:
288 @(cd ..;\
289 mv $(BASENAME) $(NAME); \
290 export STUFF; \
291 for i in $(MISC) $(DIRS) $(EDIRS) $(ONEDIRS) ;\
292 do \
293 STUFF="$$STUFF $(NAME)/$$i"; \
294 done; \
295 tar cf $(NAME)/$(TARFILE) $$STUFF; \
296 mv $(NAME) $(BASENAME) )
297 gzip -f $(TARFILE)
298
299dist:
300 perl Configure dist
301 perl util/up_ver.pl ${VERSION}
302 @$(MAKE) dist_pem_h
303 @$(MAKE) SDIRS='${SDIRS}' clean
304 @$(MAKE) SDIRS='${SDIRS}' dclean
305 @(cd apps; sh ./rmlinks)
306 @$(MAKE) makefile.one
307 @$(MAKE) tar
308
309dist_pem_h:
310 (cd crypto/pem; $(MAKE) SDIRS='${SDIRS}' CFLAG='${CFLAG}' pem.h; $(MAKE) clean)
311
312install: all
313 @-mkdir -p $(INSTALLTOP)/bin 2>/dev/null
314 @-mkdir -p $(INSTALLTOP)/lib 2>/dev/null
315 @-mkdir -p $(INSTALLTOP)/include 2>/dev/null
316 @-mkdir -p $(INSTALLTOP)/certs 2>/dev/null
317 @-mkdir -p $(INSTALLTOP)/private 2>/dev/null
318 @for i in $(DIRS) ;\
319 do \
320 (cd $$i; echo "installing $$i..."; \
321 $(MAKE) CC='${CC}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' EX_LIBS='${EX_LIBS}' SDIRS='${SDIRS}' install ); \
322 done
323 @for i in $(LIBS) ;\
324 do \
325 ( echo installing $$i; \
326 cp $$i $(INSTALLTOP)/lib; \
327 sh util/ranlib.sh $(INSTALLTOP)/lib/$$i; \
328 chmod 644 $(INSTALLTOP)/lib/$$i ); \
329 done
330
331# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/apps/Makefile.ssl b/src/lib/libssl/src/apps/Makefile.ssl
new file mode 100644
index 0000000000..1cace40ab7
--- /dev/null
+++ b/src/lib/libssl/src/apps/Makefile.ssl
@@ -0,0 +1,144 @@
1#
2# SSLeay/apps/Makefile.ssl
3#
4
5DIR= apps
6TOP= ..
7CC= cc
8INCLUDES= -I../include
9CFLAG= -g -static
10INSTALLTOP= /usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14RM= /bin/rm -f
15
16PEX_LIBS=
17EX_LIBS=
18
19CFLAGS= -DMONOLITH $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22
23DLIBCRYPTO=../libcrypto.a
24DLIBSSL=../libssl.a
25LIBCRYPTO=-L.. -lcrypto
26LIBSSL=-L.. -lssl
27
28SSLEAY= ssleay
29
30SCRIPTS=CA.sh der_chop
31
32EXE= $(SSLEAY)
33
34E_EXE= verify asn1pars req dgst dh enc gendh errstr ca crl \
35 rsa dsa dsaparam \
36 x509 genrsa s_server s_client speed \
37 s_time version pkcs7 crl2pkcs7 sess_id ciphers
38
39PROGS= $(SSLEAY).c
40
41A_OBJ=apps.o
42A_SRC=apps.c
43S_OBJ= s_cb.o s_socket.o
44S_SRC= s_cb.c s_socket.c
45
46E_OBJ= verify.o asn1pars.o req.o dgst.o dh.o enc.o gendh.o errstr.o ca.o \
47 pkcs7.o crl2p7.o crl.o \
48 rsa.o dsa.o dsaparam.o \
49 x509.o genrsa.o s_server.o s_client.o speed.o \
50 s_time.o $(A_OBJ) $(S_OBJ) version.o sess_id.o \
51 ciphers.o
52
53# pem_mail.o
54
55E_SRC= verify.c asn1pars.c req.c dgst.c dh.c enc.c gendh.c errstr.c ca.c \
56 pkcs7.c crl2p7.c crl.c \
57 rsa.c dsa.c dsaparam.c \
58 x509.c genrsa.c s_server.c s_client.c speed.c \
59 s_time.c $(A_SRC) $(S_SRC) version.c sess_id.c \
60 ciphers.c
61
62# pem_mail.c
63
64SRC=$(E_SRC)
65
66EXHEADER=
67HEADER= apps.h progs.h s_apps.h \
68 testdsa.h testrsa.h \
69 $(EXHEADER)
70
71ALL= $(GENERAL) $(SRC) $(HEADER)
72
73top:
74 @(cd ..; $(MAKE) DIRS=$(DIR) all)
75
76all: exe
77
78exe: $(EXE)
79
80req: sreq.o $(A_OBJ) $(DLIBCRYPTO)
81 $(CC) -o req $(CFLAG) sreq.o $(A_OBJ) $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
82
83sreq.o: req.c
84 $(CC) -c $(INCLUDES) $(CFLAG) -o sreq.o req.c
85
86files:
87 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
88
89install: mklinks
90 @for i in $(EXE) $(SCRIPTS) mklinks; \
91 do \
92 (echo installing $$i; \
93 cp $$i $(INSTALLTOP)/bin/$$i; \
94 chmod 755 $(INSTALLTOP)/bin/$$i ); \
95 done; \
96 cp ssleay.cnf $(INSTALLTOP)/lib
97 chmod 644 $(INSTALLTOP)/lib/ssleay.cnf
98 cd $(INSTALLTOP)/bin; \
99 /bin/sh ./mklinks; \
100 /bin/rm -f ./mklinks
101
102tags:
103 ctags $(SRC)
104
105tests:
106
107links:
108 /bin/rm -f Makefile
109 $(TOP)/util/point.sh Makefile.ssl Makefile ;
110
111lint:
112 lint -DLINT $(INCLUDES) $(SRC)>fluff
113
114depend:
115 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(SRC)
116
117dclean:
118 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
119 mv -f Makefile.new $(MAKEFILE)
120
121errors:
122
123clean:
124 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff $(EXE)
125 /bin/rm -f req
126
127$(DLIBSSL):
128 (cd ../ssl; $(MAKE))
129
130$(DLIBCRYPTO):
131 (cd ../crypto; $(MAKE))
132
133$(SSLEAY): progs.h $(E_OBJ) $(SSLEAY).o $(DLIBCRYPTO) $(DLIBSSL)
134 $(RM) $(SSLEAY)
135 $(CC) -o $(SSLEAY) $(CFLAGS) $(SSLEAY).o $(E_OBJ) $(PEX_LIBS) $(LIBSSL) $(LIBCRYPTO) $(EX_LIBS)
136
137progs.h:
138 perl ./g_ssleay.pl $(E_EXE) >progs.h
139 $(RM) $(SSLEAY).o
140
141mklinks:
142 perl ./g_ssleay.pl $(E_EXE) >progs.h
143
144# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/apps/der_chop b/src/lib/libssl/src/apps/der_chop
new file mode 100644
index 0000000000..4639330c10
--- /dev/null
+++ b/src/lib/libssl/src/apps/der_chop
@@ -0,0 +1,305 @@
1#!/usr/local/bin/perl
2#
3# der_chop ... this is one total hack that Eric is really not proud of
4# so don't look at it and don't ask for support
5#
6# The "documentation" for this (i.e. all the comments) are my fault --tjh
7#
8# This program takes the "raw" output of derparse/asn1parse and
9# converts it into tokens and then runs regular expression matches
10# to try to figure out what to grab to get the things that are needed
11# and it is possible that this will do the wrong thing as it is a *hack*
12#
13# SSLeay 0.5.2+ should have direct read support for x509 (via -inform NET)
14# [I know ... promises promises :-)]
15#
16# To convert a Netscape Certificate:
17# der_chop < ServerCert.der > cert.pem
18# To convert a Netscape Key (and encrypt it again to protect it)
19# rsa -inform NET -in ServerKey.der -des > key.pem
20#
21# 23-Apr-96 eay Added the extra ASN.1 string types, I still think this
22# is an evil hack. If nothing else the parsing should
23# be relative, not absolute.
24# 19-Apr-96 tjh hacked (with eay) into 0.5.x format
25#
26# Tim Hudson
27# tjh@cryptsoft.com
28#
29
30
31require 'getopts.pl';
32
33$debug=0;
34
35# this was the 0.4.x way of doing things ...
36$cmd="derparse";
37$x509_cmd="x509";
38$crl_cmd="crl";
39$rc4_cmd="rc4";
40$md2_cmd="md2";
41$md4_cmd="md4";
42$rsa_cmd="rsa -des -inform der ";
43
44# this was the 0.5.x way of doing things ...
45$cmd="ssleay asn1parse";
46$x509_cmd="ssleay x509";
47$crl_cmd="ssleay crl";
48$rc4_cmd="ssleay rc4";
49$md2_cmd="ssleay md2";
50$md4_cmd="ssleay md4";
51$rsa_cmd="ssleay rsa -des -inform der ";
52
53&Getopts('vd:') || die "usage:$0 [-v] [-d num] file";
54$depth=($opt_d =~ /^\d+$/)?$opt_d:0;
55
56&init_der();
57
58if ($#ARGV != -1)
59 {
60 foreach $file (@ARGV)
61 {
62 print STDERR "doing $file\n";
63 &dofile($file);
64 }
65 }
66else
67 {
68 $file="/tmp/a$$.DER";
69 open(OUT,">$file") || die "unable to open $file:$!\n";
70 for (;;)
71 {
72 $i=sysread(STDIN,$b,1024*10);
73 last if ($i <= 0);
74 $i=syswrite(OUT,$b,$i);
75 }
76 &dofile($file);
77 unlink($file);
78 }
79
80sub dofile
81 {
82 local($file)=@_;
83 local(@p);
84
85 $b=&load_file($file);
86 @p=&load_file_parse($file);
87
88 foreach $_ (@p)
89 {
90 ($off,$d,$hl,$len)=&parse_line($_);
91 $d-=$depth;
92 next if ($d != 0);
93 next if ($len == 0);
94
95 $o=substr($b,$off,$len+$hl);
96 ($str,@data)=&der_str($o);
97 print "$str\n" if ($opt_v);
98 if ($str =~ /^$crl/)
99 {
100 open(OUT,"|$crl_cmd -inform d -hash -issuer") ||
101 die "unable to run $crl_cmd:$!\n";
102 print OUT $o;
103 close(OUT);
104 }
105 elsif ($str =~ /^$x509/)
106 {
107 open(OUT,"|$x509_cmd -inform d -hash -subject -issuer")
108 || die "unable to run $x509_cmd:$!\n";
109 print OUT $o;
110 close(OUT);
111 }
112 elsif ($str =~ /^$rsa/)
113 {
114 ($type)=($data[3] =~ /OBJECT_IDENTIFIER :(.*)\s*$/);
115 next unless ($type eq "rsaEncryption");
116 ($off,$d,$hl,$len)=&parse_line($data[5]);
117 $os=substr($o,$off+$hl,$len);
118 open(OUT,"|$rsa_cmd")
119 || die "unable to run $rsa_cmd:$!\n";
120 print OUT $os;
121 close(OUT);
122 }
123 elsif ($str =~ /^0G-1D-1G/)
124 {
125 ($off,$d,$hl,$len)=&parse_line($data[1]);
126 $os=substr($o,$off+$hl,$len);
127 print STDERR "<$os>\n" if $opt_v;
128 &do_certificate($o,@data)
129 if (($os eq "certificate") &&
130 ($str =! /^0G-1D-1G-2G-3F-3E-2D/));
131 &do_private_key($o,@data)
132 if (($os eq "private-key") &&
133 ($str =! /^0G-1D-1G-2G-3F-3E-2D/));
134 }
135 }
136 }
137
138sub der_str
139 {
140 local($str)=@_;
141 local(*OUT,*IN,@a,$t,$d,$ret);
142 local($file)="/tmp/b$$.DER";
143 local(@ret);
144
145 open(OUT,">$file");
146 print OUT $str;
147 close(OUT);
148 open(IN,"$cmd -inform 'd' -in $file |") ||
149 die "unable to run $cmd:$!\n";
150 $ret="";
151 while (<IN>)
152 {
153 chop;
154 push(@ret,$_);
155
156 print STDERR "$_\n" if ($debug);
157
158 @a=split(/\s*:\s*/);
159 ($d)=($a[1] =~ /d=\s*(\d+)/);
160 $a[2] =~ s/\s+$//;
161 $t=$DER_s2i{$a[2]};
162 $ret.="$d$t-";
163 }
164 close(IN);
165 unlink($file);
166 chop $ret;
167 $ret =~ s/(-3H(-4G-5F-5[IJKMQRS])+)+/-NAME/g;
168 $ret =~ s/(-3G-4B-4L)+/-RCERT/g;
169 return($ret,@ret);
170 }
171
172sub init_der
173 {
174 $crl= "0G-1G-2G-3F-3E-2G-NAME-2L-2L-2G-RCERT-1G-2F-2E-1C";
175 $x509="0G-1G-2B-2G-3F-3E-2G-NAME-2G-3L-3L-2G-NAME-2G-3G-4F-4E-3C-1G-2F-2E-1C";
176 $rsa= "0G-1B-1G-2F-2E-1D";
177
178 %DER_i2s=(
179 # SSLeay 0.4.x has this list
180 "A","EOC",
181 "B","INTEGER",
182 "C","BIT STRING",
183 "D","OCTET STRING",
184 "E","NULL",
185 "F","OBJECT",
186 "G","SEQUENCE",
187 "H","SET",
188 "I","PRINTABLESTRING",
189 "J","T61STRING",
190 "K","IA5STRING",
191 "L","UTCTIME",
192 "M","NUMERICSTRING",
193 "N","VIDEOTEXSTRING",
194 "O","GENERALIZEDTIME",
195 "P","GRAPHICSTRING",
196 "Q","ISO64STRING",
197 "R","GENERALSTRING",
198 "S","UNIVERSALSTRING",
199
200 # SSLeay 0.5.x changed some things ... and I'm
201 # leaving in the old stuff but adding in these
202 # to handle the new as well --tjh
203 # - Well I've just taken them out and added the extra new
204 # ones :-) - eay
205 );
206
207 foreach (keys %DER_i2s)
208 { $DER_s2i{$DER_i2s{$_}}=$_; }
209 }
210
211sub parse_line
212 {
213 local($_)=@_;
214
215 return(/\s*(\d+):d=\s*(\d+)\s+hl=\s*(\d+)\s+l=\s*(\d+|inf)\s/);
216 }
217
218# 0:d=0 hl=4 l=377 cons: univ: SEQUENCE
219# 4:d=1 hl=2 l= 11 prim: univ: OCTET_STRING
220# 17:d=1 hl=4 l=360 cons: univ: SEQUENCE
221# 21:d=2 hl=2 l= 12 cons: univ: SEQUENCE
222# 23:d=3 hl=2 l= 8 prim: univ: OBJECT_IDENTIFIER :rc4
223# 33:d=3 hl=2 l= 0 prim: univ: NULL
224# 35:d=2 hl=4 l=342 prim: univ: OCTET_STRING
225sub do_private_key
226 {
227 local($data,@struct)=@_;
228 local($file)="/tmp/b$$.DER";
229 local($off,$d,$hl,$len,$_,$b,@p,$s);
230
231 ($type)=($struct[4] =~ /OBJECT_IDENTIFIER :(.*)\s*$/);
232 if ($type eq "rc4")
233 {
234 ($off,$d,$hl,$len)=&parse_line($struct[6]);
235 open(OUT,"|$rc4_cmd >$file") ||
236 die "unable to run $rc4_cmd:$!\n";
237 print OUT substr($data,$off+$hl,$len);
238 close(OUT);
239
240 $b=&load_file($file);
241 unlink($file);
242
243 ($s,@p)=&der_str($b);
244 die "unknown rsa key type\n$s\n"
245 if ($s ne '0G-1B-1G-2F-2E-1D');
246 local($off,$d,$hl,$len)=&parse_line($p[5]);
247 $b=substr($b,$off+$hl,$len);
248 ($s,@p)=&der_str($b);
249 open(OUT,"|$rsa_cmd") || die "unable to run $rsa_cmd:$!\n";
250 print OUT $b;
251 close(OUT);
252 }
253 else
254 {
255 print "'$type' is unknown\n";
256 exit(1);
257 }
258 }
259
260sub do_certificate
261 {
262 local($data,@struct)=@_;
263 local($file)="/tmp/b$$.DER";
264 local($off,$d,$hl,$len,$_,$b,@p,$s);
265
266 ($off,$d,$hl,$len)=&parse_line($struct[2]);
267 $b=substr($data,$off,$len+$hl);
268
269 open(OUT,"|$x509_cmd -inform d") || die "unable to run $x509_cmd:$!\n";
270 print OUT $b;
271 close(OUT);
272 }
273
274sub load_file
275 {
276 local($file)=@_;
277 local(*IN,$r,$b,$i);
278
279 $r="";
280 open(IN,"<$file") || die "unable to open $file:$!\n";
281 for (;;)
282 {
283 $i=sysread(IN,$b,10240);
284 last if ($i <= 0);
285 $r.=$b;
286 }
287 close(IN);
288 return($r);
289 }
290
291sub load_file_parse
292 {
293 local($file)=@_;
294 local(*IN,$r,@ret,$_,$i,$n,$b);
295
296 open(IN,"$cmd -inform d -in $file|")
297 || die "unable to run der_parse\n";
298 while (<IN>)
299 {
300 chop;
301 push(@ret,$_);
302 }
303 return($r,@ret);
304 }
305
diff --git a/src/lib/libssl/src/certs/ICE-CA.pem b/src/lib/libssl/src/certs/ICE-CA.pem
new file mode 100644
index 0000000000..75652366c2
--- /dev/null
+++ b/src/lib/libssl/src/certs/ICE-CA.pem
@@ -0,0 +1,59 @@
1Certificate:
2 Data:
3 Version: 3 (0x2)
4 Serial Number: 1 (0x1)
5 Signature Algorithm: md5WithRSAEncryption
6 Issuer: O=European ICE-TEL project, OU=V3-Certification Authority
7 Validity
8 Not Before: Apr 2 17:35:53 1997 GMT
9 Not After : Apr 2 17:35:53 1998 GMT
10 Subject: O=European ICE-TEL project, OU=V3-Certification Authority, L=Darmstadt
11 Subject Public Key Info:
12 Public Key Algorithm: rsa
13 RSA Public Key: (512 bit)
14 Modulus (512 bit):
15 00:82:75:ba:f6:d1:60:b5:f9:15:b3:6a:dd:29:8f:
16 8b:a4:6f:1a:88:e0:50:43:40:0b:79:41:d5:d3:16:
17 44:7d:74:65:17:42:06:52:0b:e9:50:c8:10:cd:24:
18 e2:ae:8d:22:30:73:e6:b4:b7:93:1f:e5:6e:a2:ae:
19 49:11:a5:c9:45
20 Exponent: 65537 (0x10001)
21 X509v3 extensions:
22 X509v3 Authority Key Identifier:
23 0.........z.."p......e..
24 X509v3 Subject Key Identifier:
25 ..~r..:..B.44fu......3
26 X509v3 Key Usage: critical
27 ....
28 X509v3 Certificate Policies: critical
29 0.0...*...
30 X509v3 Subject Alternative Name:
31 0!..secude-support@darmstadt.gmd.de
32 X509v3 Issuer Alternative Name:
33 0I..ice-tel-ca@darmstadt.gmd.de.*http://www.darmstadt.gmd.de/ice-tel/euroca
34 X509v3 Basic Constraints: critical
35 0....
36 X509v3 CRL Distribution Points:
37 0200...,.*http://www.darmstadt.gmd.de/ice-tel/euroca
38 Signature Algorithm: md5WithRSAEncryption
39 17:a2:88:b7:99:5a:05:41:e4:13:34:67:e6:1f:3e:26:ec:4b:
40 69:f9:3e:28:22:be:9d:1c:ab:41:6f:0c:00:85:fe:45:74:f6:
41 98:f0:ce:9b:65:53:4a:50:42:c7:d4:92:bd:d7:a2:a8:3d:98:
42 88:73:cd:60:28:79:a3:fc:48:7a
43-----BEGIN CERTIFICATE-----
44MIICzDCCAnagAwIBAgIBATANBgkqhkiG9w0BAQQFADBIMSEwHwYDVQQKExhFdXJv
45cGVhbiBJQ0UtVEVMIHByb2plY3QxIzAhBgNVBAsTGlYzLUNlcnRpZmljYXRpb24g
46QXV0aG9yaXR5MB4XDTk3MDQwMjE3MzU1M1oXDTk4MDQwMjE3MzU1M1owXDEhMB8G
47A1UEChMYRXVyb3BlYW4gSUNFLVRFTCBwcm9qZWN0MSMwIQYDVQQLExpWMy1DZXJ0
48aWZpY2F0aW9uIEF1dGhvcml0eTESMBAGA1UEBxMJRGFybXN0YWR0MFkwCgYEVQgB
49AQICAgADSwAwSAJBAIJ1uvbRYLX5FbNq3SmPi6RvGojgUENAC3lB1dMWRH10ZRdC
50BlIL6VDIEM0k4q6NIjBz5rS3kx/lbqKuSRGlyUUCAwEAAaOCATgwggE0MB8GA1Ud
51IwQYMBaAFIr3yNUOx3ro1yJw4AuJ1bbsZbzPMB0GA1UdDgQWBBR+cvL4OoacQog0
52NGZ1w9T80aIRMzAOBgNVHQ8BAf8EBAMCAfYwFAYDVR0gAQH/BAowCDAGBgQqAwQF
53MCoGA1UdEQQjMCGBH3NlY3VkZS1zdXBwb3J0QGRhcm1zdGFkdC5nbWQuZGUwUgYD
54VR0SBEswSYEbaWNlLXRlbC1jYUBkYXJtc3RhZHQuZ21kLmRlhipodHRwOi8vd3d3
55LmRhcm1zdGFkdC5nbWQuZGUvaWNlLXRlbC9ldXJvY2EwDwYDVR0TAQH/BAUwAwEB
56/zA7BgNVHR8ENDAyMDCgLqAshipodHRwOi8vd3d3LmRhcm1zdGFkdC5nbWQuZGUv
57aWNlLXRlbC9ldXJvY2EwDQYJKoZIhvcNAQEEBQADQQAXooi3mVoFQeQTNGfmHz4m
587Etp+T4oIr6dHKtBbwwAhf5FdPaY8M6bZVNKUELH1JK916KoPZiIc81gKHmj/Eh6
59-----END CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/ICE-root.pem b/src/lib/libssl/src/certs/ICE-root.pem
new file mode 100644
index 0000000000..fa991599c9
--- /dev/null
+++ b/src/lib/libssl/src/certs/ICE-root.pem
@@ -0,0 +1,48 @@
1Certificate:
2 Data:
3 Version: 3 (0x2)
4 Serial Number: 0 (0x0)
5 Signature Algorithm: md5WithRSAEncryption
6 Issuer: O=European ICE-TEL project, OU=V3-Certification Authority
7 Validity
8 Not Before: Apr 2 17:33:36 1997 GMT
9 Not After : Apr 2 17:33:36 1998 GMT
10 Subject: O=European ICE-TEL project, OU=V3-Certification Authority
11 Subject Public Key Info:
12 Public Key Algorithm: rsa
13 RSA Public Key: (512 bit)
14 Modulus (512 bit):
15 00:80:3e:eb:ae:47:a9:fe:10:54:0b:81:8b:9c:2b:
16 82:ab:3a:61:36:65:8b:f3:73:9f:ac:ac:7a:15:a7:
17 13:8f:b4:c4:ba:a3:0f:bc:a5:58:8d:cc:b1:93:31:
18 9e:81:9e:8c:19:61:86:fa:52:73:54:d1:97:76:22:
19 e7:c7:9f:41:cd
20 Exponent: 65537 (0x10001)
21 X509v3 extensions:
22 X509v3 Subject Key Identifier:
23 ........z.."p......e..
24 X509v3 Key Usage: critical
25 ....
26 X509v3 Subject Alternative Name:
27 0I.*http://www.darmstadt.gmd.de/ice-tel/euroca..ice-tel-ca@darmstadt.gmd.de
28 X509v3 Basic Constraints: critical
29 0....
30 Signature Algorithm: md5WithRSAEncryption
31 76:69:61:db:b7:cf:8b:06:9e:d8:8c:96:53:d2:4d:a8:23:a6:
32 03:44:e8:8f:24:a5:c0:84:a8:4b:77:d4:2d:2b:7d:37:91:67:
33 f2:2c:ce:02:31:4c:6b:cc:ce:f2:68:a6:11:11:ab:7d:88:b8:
34 7e:22:9f:25:06:60:bd:79:30:3d
35-----BEGIN CERTIFICATE-----
36MIICFjCCAcCgAwIBAgIBADANBgkqhkiG9w0BAQQFADBIMSEwHwYDVQQKExhFdXJv
37cGVhbiBJQ0UtVEVMIHByb2plY3QxIzAhBgNVBAsTGlYzLUNlcnRpZmljYXRpb24g
38QXV0aG9yaXR5MB4XDTk3MDQwMjE3MzMzNloXDTk4MDQwMjE3MzMzNlowSDEhMB8G
39A1UEChMYRXVyb3BlYW4gSUNFLVRFTCBwcm9qZWN0MSMwIQYDVQQLExpWMy1DZXJ0
40aWZpY2F0aW9uIEF1dGhvcml0eTBZMAoGBFUIAQECAgIAA0sAMEgCQQCAPuuuR6n+
41EFQLgYucK4KrOmE2ZYvzc5+srHoVpxOPtMS6ow+8pViNzLGTMZ6BnowZYYb6UnNU
420Zd2IufHn0HNAgMBAAGjgZcwgZQwHQYDVR0OBBYEFIr3yNUOx3ro1yJw4AuJ1bbs
43ZbzPMA4GA1UdDwEB/wQEAwIB9jBSBgNVHREESzBJhipodHRwOi8vd3d3LmRhcm1z
44dGFkdC5nbWQuZGUvaWNlLXRlbC9ldXJvY2GBG2ljZS10ZWwtY2FAZGFybXN0YWR0
45LmdtZC5kZTAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBBAUAA0EAdmlh27fP
46iwae2IyWU9JNqCOmA0TojySlwISoS3fULSt9N5Fn8izOAjFMa8zO8mimERGrfYi4
47fiKfJQZgvXkwPQ==
48-----END CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/ICE-user.pem b/src/lib/libssl/src/certs/ICE-user.pem
new file mode 100644
index 0000000000..28065fd37d
--- /dev/null
+++ b/src/lib/libssl/src/certs/ICE-user.pem
@@ -0,0 +1,63 @@
1Certificate:
2 Data:
3 Version: 3 (0x2)
4 Serial Number: 1 (0x1)
5 Signature Algorithm: md5WithRSAEncryption
6 Issuer: O=European ICE-TEL project, OU=V3-Certification Authority, L=Darmstadt
7 Validity
8 Not Before: Apr 2 17:35:59 1997 GMT
9 Not After : Apr 2 17:35:59 1998 GMT
10 Subject: O=European ICE-TEL project, OU=V3-Certification Authority, L=Darmstadt, CN=USER
11 Subject Public Key Info:
12 Public Key Algorithm: rsa
13 RSA Public Key: (512 bit)
14 Modulus (512 bit):
15 00:a8:a8:53:63:49:1b:93:c3:c3:0b:6c:88:11:55:
16 de:7e:6a:e2:f9:52:a0:dc:69:25:c4:c8:bf:55:e1:
17 31:a8:ce:e4:a9:29:85:99:8a:15:9a:de:f6:2f:e1:
18 b4:50:5f:5e:04:75:a6:f4:76:dc:3c:0e:39:dc:3a:
19 be:3e:a4:61:8b
20 Exponent: 65537 (0x10001)
21 X509v3 extensions:
22 X509v3 Authority Key Identifier:
23 0...~r..:..B.44fu......3
24 X509v3 Subject Key Identifier:
25 ...... .*...1.*.......
26 X509v3 Key Usage: critical
27 ....
28 X509v3 Certificate Policies: critical
29 0.0...*...0.......
30 X509v3 Subject Alternative Name:
31 0:..user@darmstadt.gmd.de.!http://www.darmstadt.gmd.de/~user
32 X509v3 Issuer Alternative Name:
33 0....gmdca@gmd.de..http://www.gmd.de..saturn.darmstadt.gmd.de.\1!0...U.
34..European ICE-TEL project1#0!..U....V3-Certification Authority1.0...U....Darmstadt..141.12.62.26
35 X509v3 Basic Constraints: critical
36 0.
37 X509v3 CRL Distribution Points:
38 0.0.......gmdca@gmd.de
39 Signature Algorithm: md5WithRSAEncryption
40 69:0c:e1:b7:a7:f2:d8:fb:e8:69:c0:13:cd:37:ad:21:06:22:
41 4d:e8:c6:db:f1:04:0b:b7:e0:b3:d6:0c:81:03:ce:c3:6a:3e:
42 c7:e7:24:24:a4:92:64:c2:83:83:06:42:53:0e:6f:09:1e:84:
43 9a:f7:6f:63:9b:94:99:83:d6:a4
44-----BEGIN CERTIFICATE-----
45MIIDTzCCAvmgAwIBAgIBATANBgkqhkiG9w0BAQQFADBcMSEwHwYDVQQKExhFdXJv
46cGVhbiBJQ0UtVEVMIHByb2plY3QxIzAhBgNVBAsTGlYzLUNlcnRpZmljYXRpb24g
47QXV0aG9yaXR5MRIwEAYDVQQHEwlEYXJtc3RhZHQwHhcNOTcwNDAyMTczNTU5WhcN
48OTgwNDAyMTczNTU5WjBrMSEwHwYDVQQKExhFdXJvcGVhbiBJQ0UtVEVMIHByb2pl
49Y3QxIzAhBgNVBAsTGlYzLUNlcnRpZmljYXRpb24gQXV0aG9yaXR5MRIwEAYDVQQH
50EwlEYXJtc3RhZHQxDTALBgNVBAMTBFVTRVIwWTAKBgRVCAEBAgICAANLADBIAkEA
51qKhTY0kbk8PDC2yIEVXefmri+VKg3GklxMi/VeExqM7kqSmFmYoVmt72L+G0UF9e
52BHWm9HbcPA453Dq+PqRhiwIDAQABo4IBmDCCAZQwHwYDVR0jBBgwFoAUfnLy+DqG
53nEKINDRmdcPU/NGiETMwHQYDVR0OBBYEFJfc4B8gjSoRmLUx4Sq/ucIYiMrPMA4G
54A1UdDwEB/wQEAwIB8DAcBgNVHSABAf8EEjAQMAYGBCoDBAUwBgYECQgHBjBDBgNV
55HREEPDA6gRV1c2VyQGRhcm1zdGFkdC5nbWQuZGWGIWh0dHA6Ly93d3cuZGFybXN0
56YWR0LmdtZC5kZS9+dXNlcjCBsQYDVR0SBIGpMIGmgQxnbWRjYUBnbWQuZGWGEWh0
57dHA6Ly93d3cuZ21kLmRlghdzYXR1cm4uZGFybXN0YWR0LmdtZC5kZaRcMSEwHwYD
58VQQKExhFdXJvcGVhbiBJQ0UtVEVMIHByb2plY3QxIzAhBgNVBAsTGlYzLUNlcnRp
59ZmljYXRpb24gQXV0aG9yaXR5MRIwEAYDVQQHEwlEYXJtc3RhZHSHDDE0MS4xMi42
60Mi4yNjAMBgNVHRMBAf8EAjAAMB0GA1UdHwQWMBQwEqAQoA6BDGdtZGNhQGdtZC5k
61ZTANBgkqhkiG9w0BAQQFAANBAGkM4ben8tj76GnAE803rSEGIk3oxtvxBAu34LPW
62DIEDzsNqPsfnJCSkkmTCg4MGQlMObwkehJr3b2OblJmD1qQ=
63-----END CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/ICE.crl b/src/lib/libssl/src/certs/ICE.crl
new file mode 100644
index 0000000000..21939e8cc4
--- /dev/null
+++ b/src/lib/libssl/src/certs/ICE.crl
@@ -0,0 +1,9 @@
1-----BEGIN X509 CRL-----
2MIIBNDCBnjANBgkqhkiG9w0BAQIFADBFMSEwHwYDVQQKExhFdXJvcGVhbiBJQ0Ut
3VEVMIFByb2plY3QxIDAeBgNVBAsTF0NlcnRpZmljYXRpb24gQXV0aG9yaXR5Fw05
4NzA2MDkxNDQyNDNaFw05NzA3MDkxNDQyNDNaMCgwEgIBChcNOTcwMzAzMTQ0MjU0
5WjASAgEJFw05NjEwMDIxMjI5MjdaMA0GCSqGSIb3DQEBAgUAA4GBAH4vgWo2Tej/
6i7kbiw4Imd30If91iosjClNpBFwvwUDBclPEeMuYimHbLOk4H8Nofc0fw11+U/IO
7KSNouUDcqG7B64oY7c4SXKn+i1MWOb5OJiWeodX3TehHjBlyWzoNMWCnYA8XqFP1
8mOKp8Jla1BibEZf14+/HqCi2hnZUiEXh
9-----END X509 CRL-----
diff --git a/src/lib/libssl/src/certs/ca-cert.pem b/src/lib/libssl/src/certs/ca-cert.pem
new file mode 100644
index 0000000000..6dd974d70d
--- /dev/null
+++ b/src/lib/libssl/src/certs/ca-cert.pem
@@ -0,0 +1,31 @@
1issuer= /C=AU/ST=Queensland/O=CryptSoft Pty Ltd/CN=Test PCA (1024 bit)
2subject=/C=AU/ST=Queensland/O=CryptSoft Pty Ltd/CN=Test CA (1024 bit)
3-----BEGIN CERTIFICATE-----
4MIICJjCCAY8CAQAwDQYJKoZIhvcNAQEEBQAwXDELMAkGA1UEBhMCQVUxEzARBgNV
5BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRwwGgYD
6VQQDExNUZXN0IFBDQSAoMTAyNCBiaXQpMB4XDTk3MDYwOTEzNTc0M1oXDTAxMDYw
7OTEzNTc0M1owWzELMAkGA1UEBhMCQVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxGjAY
8BgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYDVQQDExJUZXN0IENBICgxMDI0
9IGJpdCkwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAKO7o8t116VP6cgybTsZ
10DCZhr95nYlZuya3aCi1IKoztqwWnjbmDFIriOqGFPrZQ+moMETC9D59iRW/dFXSv
111F65ka/XY2hLh9exCCo7XuUcDs53Qp3bI3AmMqHjgzE8oO3ajyJAzJkTTOUecQU2
12mw/gI4tMM0LqWMQS7luTy4+xAgMBAAEwDQYJKoZIhvcNAQEEBQADgYEAM7achv3v
13hLQJcv/65eGEpBXM40ZDVoFQFFJWaY5p883HTqLB1x4FdzsXHH0QKBTcKpWwqyu4
14YDm3fb8oDugw72bCzfyZK/zVZPR/hVlqI/fvU109Qoc+7oPvIXWky71HfcK6ZBCA
15q30KIqGM/uoM60INq97qjDmCJapagcNBGQs=
16-----END CERTIFICATE-----
17-----BEGIN RSA PRIVATE KEY-----
18MIICXQIBAAKBgQCju6PLddelT+nIMm07GQwmYa/eZ2JWbsmt2gotSCqM7asFp425
19gxSK4jqhhT62UPpqDBEwvQ+fYkVv3RV0r9ReuZGv12NoS4fXsQgqO17lHA7Od0Kd
202yNwJjKh44MxPKDt2o8iQMyZE0zlHnEFNpsP4COLTDNC6ljEEu5bk8uPsQIDAQAB
21AoGAVZmpFZsDZfr0l2S9tLLwpjRWNOlKATQkno6q2WesT0eGLQufTciY+c8ypfU6
22hyio8r5iUl/VhhdjhAtKx1mRpiotftHo/eYf8rtsrnprOnWG0bWjLjtIoMbcxGn2
23J3bN6LJmbJMjDs0eJ3KnTu646F3nDUw2oGAwmpzKXA1KAP0CQQDRvQhxk2D3Pehs
24HvG665u2pB5ipYQngEFlZO7RHJZzJOZEWSLuuMqaF/7pTfA5jiBvWqCgJeCRRInL
2521ru4dlPAkEAx9jj7BgKn5TYnMoBSSe0afjsV9oApVpN1Nacb1YDtCwy+scp3++s
26nFxlv98wxIlSdpwMUn+AUWfjiWR7Tu/G/wJBAJ/KjwZIrFVxewP0x2ILYsTRYLzz
27MS4PDsO7FB+I0i7DbBOifXS2oNSpd3I0CNMwrxFnUHzynpbOStVfN3ZL5w0CQQCa
28pwFahxBRhkJKsxhjoFJBX9yl75JoY4Wvm5Tbo9ih6UJaRx3kqfkN14L2BKYcsZgb
29KY9vmDOYy6iNfjDeWTfJAkBkfPUb8oTJ/nSP5zN6sqGxSY4krc4xLxpRmxoJ8HL2
30XfhqXkTzbU13RX9JJ/NZ8vQN9Vm2NhxRGJocQkmcdVtJ
31-----END RSA PRIVATE KEY-----
diff --git a/src/lib/libssl/src/certs/dsa-ca.pem b/src/lib/libssl/src/certs/dsa-ca.pem
new file mode 100644
index 0000000000..9eb08f3ddd
--- /dev/null
+++ b/src/lib/libssl/src/certs/dsa-ca.pem
@@ -0,0 +1,43 @@
1-----BEGIN DSA PRIVATE KEY-----
2Proc-Type: 4,ENCRYPTED
3DEK-Info: DES-EDE3-CBC,C5B6C7CC9E1FE2C0
4
5svCXBcBRhMuU22UXOfiKZA+thmz6KYXpt1Yg5Rd+TYQcQ1MdvNy0B0tkP1SxzDq0
6Xh1eMeTML9/9/0rKakgNXXXbpi5RB8t6BmwRSyej89F7nn1mtR3qzoyPRpp15SDl
7Tn67C+2v+HDF3MFk88hiNCYkNbcmi7TWvChsl8N1r7wdZwtIox56yXdgxw6ZIpa/
8par0oUCzN7fiavPgCWz1kfPNSaBQSdxwH7TZi5tMHAr0J3C7a7QRnZfE09R59Uqr
9zslrq+ndIw1BZAxoY0SlBu+iFOVaBVlwToC4AsHkv7j7l8ITtr7f42YbBa44D9TO
10uOhONmkk/v3Fso4RaOEzdKZC+hnmmzvHs6TiTWm6yzJgSFwyOUK0eGmKEeVxpcH5
11rUOlHOwzen+FFtocZDZAfdFnb7QY7L/boQvyA5A+ZbRG4DUpmBQeQsSaICHM5Rxx
121QaLF413VNPXTLPbW0ilSc2H8x2iZTIVKfd33oSO6NhXPtSYQgfecEF4BvNHY5c4
13HovjT4mckbK95bcBzoCHu43vuSQkmZzdYo/ydSZt6zoPavbBLueTpgSbdXiDi827
14MVqOsYxGCb+kez0FoDSTgw==
15-----END DSA PRIVATE KEY-----
16-----BEGIN CERTIFICATE REQUEST-----
17MIICUjCCAhECAQAwUjELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
18ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDELMAkGA1UEAxMCQ0Ew
19ggG0MIIBKQYFKw4DAgwwggEeAoGBAKc/boW/QWopffCfRxkwkJoJHdpqMx7FPYaW
20sxXgUy6P4FmCc5A+dTGZR3pS+4Xk2aZ7OJtoioSbh8YetX6GS1NbWc9xZRmIbs5m
21rmuINvvsKNzC16W75Sw5JkvamnAYlTeVEFYj9hXtugRe3jlP/bdDH7WkZW/NgBHk
22cJVbUM1JAhUA9wcx7fpsBgPVhYocrJxl51BmZW8CgYBN30wDppGK9RlvUEYlmeVo
23bzDjaeHls12YuyiGSPzemQQ/X4gMnHMkDSBduSqaPxiWJ+Rih8F7dGJT/GEnqHqR
24CZ228U2cVA9YBu5JdAfOVX4jzhb2ytxaYQF+yXG1TfbcNCmHaPZeIJOz2/XkCWxB
25F5WS6wG1c6Vqftgy7Q4CuAOBhAACgYAapll6iqz9XrZFlk2GCVcB+KihxWnH7IuH
26vSLw9YUrJahcBHmbpvt494lF4gC5w3WPM+vXJofbusk4GoQEEsQNMDaah4m49uUq
27AylOVFJJJXuirVJ+o+0TtOFDITEAl+YZZariXOD7tdOSOl9RLMPC6+daHKS9e68u
283enxhqnDGaAAMAkGBSsOAwIbBQADMAAwLQIVAJGVuFsG/0DBuSZ0jF7ypdU0/G0v
29AhQfeF5BoMMDbX/kidUVpQ6gadPlZA==
30-----END CERTIFICATE REQUEST-----
31-----BEGIN CERTIFICATE-----
32MIIBrjCCAWwCAQswCQYFKw4DAhsFADBTMQswCQYDVQQGEwJBVTETMBEGA1UECBMK
33U29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMQww
34CgYDVQQDEwNQQ0EwHhcNOTcwNjE1MDIxNDI5WhcNOTcwNzE1MDIxNDI5WjBSMQsw
35CQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJu
36ZXQgV2lkZ2l0cyBQdHkgTHRkMQswCQYDVQQDEwJDQTCBkjAJBgUrDgMCDAUAA4GE
37AAKBgBqmWXqKrP1etkWWTYYJVwH4qKHFacfsi4e9IvD1hSslqFwEeZum+3j3iUXi
38ALnDdY8z69cmh9u6yTgahAQSxA0wNpqHibj25SoDKU5UUkkle6KtUn6j7RO04UMh
39MQCX5hllquJc4Pu105I6X1Esw8Lr51ocpL17ry7d6fGGqcMZMAkGBSsOAwIbBQAD
40MQAwLgIVAJ4wtQsANPxHo7Q4IQZYsL12SKdbAhUAjJ9n38zxT+iai2164xS+LIfa
41C1Q=
42-----END CERTIFICATE-----
43
diff --git a/src/lib/libssl/src/certs/dsa-pca.pem b/src/lib/libssl/src/certs/dsa-pca.pem
new file mode 100644
index 0000000000..e3641ad47e
--- /dev/null
+++ b/src/lib/libssl/src/certs/dsa-pca.pem
@@ -0,0 +1,49 @@
1-----BEGIN DSA PRIVATE KEY-----
2Proc-Type: 4,ENCRYPTED
3DEK-Info: DES-EDE3-CBC,F80EEEBEEA7386C4
4
5GZ9zgFcHOlnhPoiSbVi/yXc9mGoj44A6IveD4UlpSEUt6Xbse3Fr0KHIUyQ3oGnS
6mClKoAp/eOTb5Frhto85SzdsxYtac+X1v5XwdzAMy2KowHVk1N8A5jmE2OlkNPNt
7of132MNlo2cyIRYaa35PPYBGNCmUm7YcYS8O90YtkrQZZTf4+2C4kllhMcdkQwkr
8FWSWC8YOQ7w0LHb4cX1FejHHom9Nd/0PN3vn3UyySvfOqoR7nbXkrpHXmPIr0hxX
9RcF0aXcV/CzZ1/nfXWQf4o3+oD0T22SDoVcZY60IzI0oIc3pNCbDV3uKNmgekrFd
10qOUJ+QW8oWp7oefRx62iBfIeC8DZunohMXaWAQCU0sLQOR4yEdeUCnzCSywe0bG1
11diD0KYaEe+Yub1BQH4aLsBgDjardgpJRTQLq0DUvw0/QGO1irKTJzegEDNVBKrVn
12V4AHOKT1CUKqvGNRP1UnccUDTF6miOAtaj/qpzra7sSk7dkGBvIEeFoAg84kfh9h
13hVvF1YyzC9bwZepruoqoUwke/WdNIR5ymOVZ/4Liw0JdIOcq+atbdRX08niqIRkf
14dsZrUj4leo3zdefYUQ7w4N2Ns37yDFq7
15-----END DSA PRIVATE KEY-----
16-----BEGIN CERTIFICATE REQUEST-----
17MIICVTCCAhMCAQAwUzELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx
18ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEMMAoGA1UEAxMDUENB
19MIIBtTCCASkGBSsOAwIMMIIBHgKBgQCnP26Fv0FqKX3wn0cZMJCaCR3aajMexT2G
20lrMV4FMuj+BZgnOQPnUxmUd6UvuF5NmmezibaIqEm4fGHrV+hktTW1nPcWUZiG7O
21Zq5riDb77Cjcwtelu+UsOSZL2ppwGJU3lRBWI/YV7boEXt45T/23Qx+1pGVvzYAR
225HCVW1DNSQIVAPcHMe36bAYD1YWKHKycZedQZmVvAoGATd9MA6aRivUZb1BGJZnl
23aG8w42nh5bNdmLsohkj83pkEP1+IDJxzJA0gXbkqmj8YlifkYofBe3RiU/xhJ6h6
24kQmdtvFNnFQPWAbuSXQHzlV+I84W9srcWmEBfslxtU323DQph2j2XiCTs9v15Als
25QReVkusBtXOlan7YMu0OArgDgYUAAoGBAKbtuR5AdW+ICjCFe2ixjUiJJzM2IKwe
266NZEMXg39+HQ1UTPTmfLZLps+rZfolHDXuRKMXbGFdSF0nXYzotPCzi7GauwEJTZ
27yr27ZZjA1C6apGSQ9GzuwNvZ4rCXystVEagAS8OQ4H3D4dWS17Zg31ICb5o4E5r0
28z09o/Uz46u0VoAAwCQYFKw4DAhsFAAMxADAuAhUArRubTxsbIXy3AhtjQ943AbNB
29nSICFQCu+g1iW3jwF+gOcbroD4S/ZcvB3w==
30-----END CERTIFICATE REQUEST-----
31-----BEGIN CERTIFICATE-----
32MIIC0zCCApECAQAwCQYFKw4DAhsFADBTMQswCQYDVQQGEwJBVTETMBEGA1UECBMK
33U29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMQww
34CgYDVQQDEwNQQ0EwHhcNOTcwNjE0MjI1NDQ1WhcNOTcwNzE0MjI1NDQ1WjBTMQsw
35CQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJu
36ZXQgV2lkZ2l0cyBQdHkgTHRkMQwwCgYDVQQDEwNQQ0EwggG1MIIBKQYFKw4DAgww
37ggEeAoGBAKc/boW/QWopffCfRxkwkJoJHdpqMx7FPYaWsxXgUy6P4FmCc5A+dTGZ
38R3pS+4Xk2aZ7OJtoioSbh8YetX6GS1NbWc9xZRmIbs5mrmuINvvsKNzC16W75Sw5
39JkvamnAYlTeVEFYj9hXtugRe3jlP/bdDH7WkZW/NgBHkcJVbUM1JAhUA9wcx7fps
40BgPVhYocrJxl51BmZW8CgYBN30wDppGK9RlvUEYlmeVobzDjaeHls12YuyiGSPze
41mQQ/X4gMnHMkDSBduSqaPxiWJ+Rih8F7dGJT/GEnqHqRCZ228U2cVA9YBu5JdAfO
42VX4jzhb2ytxaYQF+yXG1TfbcNCmHaPZeIJOz2/XkCWxBF5WS6wG1c6Vqftgy7Q4C
43uAOBhQACgYEApu25HkB1b4gKMIV7aLGNSIknMzYgrB7o1kQxeDf34dDVRM9OZ8tk
44umz6tl+iUcNe5EoxdsYV1IXSddjOi08LOLsZq7AQlNnKvbtlmMDULpqkZJD0bO7A
4529nisJfKy1URqABLw5DgfcPh1ZLXtmDfUgJvmjgTmvTPT2j9TPjq7RUwCQYFKw4D
46AhsFAAMxADAuAhUAvtv6AkMolix1Jvy3UnVEIUqdCUICFQC+jq8P49mwrY9oJ24n
475rKUjNBhSg==
48-----END CERTIFICATE-----
49
diff --git a/src/lib/libssl/src/certs/factory.pem b/src/lib/libssl/src/certs/factory.pem
new file mode 100644
index 0000000000..8e28b391b2
--- /dev/null
+++ b/src/lib/libssl/src/certs/factory.pem
@@ -0,0 +1,15 @@
1-----BEGIN CERTIFICATE-----
2MIICTTCCAbagAwIBAgIBADANBgkqhkiG9w0BAQQFADBMMQswCQYDVQQGEwJHQjEM
3MAoGA1UEChMDVUNMMRgwFgYDVQQLEw9JQ0UtVEVMIFByb2plY3QxFTATBgNVBAMT
4DFRydXN0RmFjdG9yeTAeFw05NzA0MjIxNDM5MTRaFw05ODA0MjIxNDM5MTRaMEwx
5CzAJBgNVBAYTAkdCMQwwCgYDVQQKEwNVQ0wxGDAWBgNVBAsTD0lDRS1URUwgUHJv
6amVjdDEVMBMGA1UEAxMMVHJ1c3RGYWN0b3J5MIGcMAoGBFUIAQECAgQAA4GNADCB
7iQKBgQCEieR8NcXkUW1f0G6aC6u0i8q/98JqS6RxK5YmHIGKCkuTWAUjzLfUa4dt
8U9igGCjTuxaDqlzEim+t/02pmiBZT9HaX++35MjQPUWmsChcYU5WyzGErXi+rQaw
9zlwS73zM8qiPj/97lXYycWhgL0VaiDSPxRXEUdWoaGruom4mNQIDAQABo0IwQDAd
10BgNVHQ4EFgQUHal1LZr7oVg5z6lYzrhTgZRCmcUwDgYDVR0PAQH/BAQDAgH2MA8G
11A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAfaggfl6FZoioecjv0dq8
12/DXo/u11iMZvXn08gjX/zl2b4wtPbShOSY5FhkSm8GeySasz+/Nwb/uzfnIhokWi
13lfPZHtlCWtXbIy/TN51eJyq04ceDCQDWvLC2enVg9KB+GJ34b5c5VaPRzq8MBxsA
14S7ELuYGtmYgYm9NZOIr7yU0=
15-----END CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/nortelCA.pem b/src/lib/libssl/src/certs/nortelCA.pem
new file mode 100644
index 0000000000..207f34ab3a
--- /dev/null
+++ b/src/lib/libssl/src/certs/nortelCA.pem
@@ -0,0 +1,16 @@
1-----BEGIN CERTIFICATE-----
2MIICajCCAdMCBDGA0QUwDQYJKoZIhvcNAQEEBQAwfTELMAkGA1UEBhMCQ2ExDzAN
3BgNVBAcTBk5lcGVhbjEeMBwGA1UECxMVTm8gTGlhYmlsaXR5IEFjY2VwdGVkMR8w
4HQYDVQQKExZGb3IgRGVtbyBQdXJwb3NlcyBPbmx5MRwwGgYDVQQDExNFbnRydXN0
5IERlbW8gV2ViIENBMB4XDTk2MDQyNjEzMzUwMVoXDTA2MDQyNjEzMzUwMVowfTEL
6MAkGA1UEBhMCQ2ExDzANBgNVBAcTBk5lcGVhbjEeMBwGA1UECxMVTm8gTGlhYmls
7aXR5IEFjY2VwdGVkMR8wHQYDVQQKExZGb3IgRGVtbyBQdXJwb3NlcyBPbmx5MRww
8GgYDVQQDExNFbnRydXN0IERlbW8gV2ViIENBMIGdMA0GCSqGSIb3DQEBAQUAA4GL
9ADCBhwKBgQCaroS7O1DA0hm4IefNYU1cx/nqOmzEnk291d1XqznDeF4wEgakbkCc
10zTKxK791yNpXG5RmngqH7cygDRTHZJ6mfCRn0wGC+AI00F2vYTGqPGRQL1N3lZT0
11YDKFC0SQeMMjFIZ1aeQigroFQnHo0VB3zWIMpNkka8PY9lxHZAmWwQIBAzANBgkq
12hkiG9w0BAQQFAAOBgQBAx0UMVA1s54lMQyXjMX5kj99FJN5itb8bK1Rk+cegPQPF
13cWO9SEWyEjjBjIkjjzAwBkaEszFsNGxemxtXvwjIm1xEUMTVlPEWTs2qnDvAUA9W
14YqhWbhH0toGT36236QAsqCZ76rbTRVSSX2BHyJwJMG2tCRv7kRJ//NIgxj3H4w==
15-----END CERTIFICATE-----
16
diff --git a/src/lib/libssl/src/certs/pca-cert.pem b/src/lib/libssl/src/certs/pca-cert.pem
new file mode 100644
index 0000000000..140e9a6b43
--- /dev/null
+++ b/src/lib/libssl/src/certs/pca-cert.pem
@@ -0,0 +1,31 @@
1issuer= /C=AU/ST=Queensland/O=CryptSoft Pty Ltd/CN=Test PCA (1024 bit)
2subject=/C=AU/ST=Queensland/O=CryptSoft Pty Ltd/CN=Test PCA (1024 bit)
3-----BEGIN CERTIFICATE-----
4MIICJzCCAZACAQAwDQYJKoZIhvcNAQEEBQAwXDELMAkGA1UEBhMCQVUxEzARBgNV
5BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRwwGgYD
6VQQDExNUZXN0IFBDQSAoMTAyNCBiaXQpMB4XDTk3MDYwOTEzNTczN1oXDTAxMDYw
7OTEzNTczN1owXDELMAkGA1UEBhMCQVUxEzARBgNVBAgTClF1ZWVuc2xhbmQxGjAY
8BgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRwwGgYDVQQDExNUZXN0IFBDQSAoMTAy
9NCBiaXQpMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdoWk/3+WcMlfjIrkg
1040ketmnQaEogQe1LLcuOJV6rKfUSAsPgwgsabJ/wn8TxA1yy3eKJbFl3OiUXMRsp
1122Jp85PmemiDzyUIStwk72qhp1imbANZvlmlCFKiQrjUyuDfu4TABmn+kkt3vR1Y
12BEOGt+IFye1UBVSATVdRJ2UVhwIDAQABMA0GCSqGSIb3DQEBBAUAA4GBABNA1u/S
13Cg/LJZWb7GliiKJsvuhxlE4E5JxQF2zMub/CSNbF97//tYSyj96sxeFQxZXbcjm9
14xt6mr/xNLA4szNQMJ4P+L7b5e/jC5DSqlwS+CUYJgaFs/SP+qJoCSu1bR3IM9XWO
15cRBpDmcBbYLkSyB92WURvsZ1LtjEcn+cdQVI
16-----END CERTIFICATE-----
17-----BEGIN RSA PRIVATE KEY-----
18MIICXAIBAAKBgQCdoWk/3+WcMlfjIrkg40ketmnQaEogQe1LLcuOJV6rKfUSAsPg
19wgsabJ/wn8TxA1yy3eKJbFl3OiUXMRsp22Jp85PmemiDzyUIStwk72qhp1imbANZ
20vlmlCFKiQrjUyuDfu4TABmn+kkt3vR1YBEOGt+IFye1UBVSATVdRJ2UVhwIDAQAB
21AoGAba4fTtuap5l7/8ZsbE7Z1O32KJY4ZcOZukLOLUUhXxXduT+FTgGWujc0/rgc
22z9qYCLlNZHOouMYTgtSfYvuMuLZ11VIt0GYH+nRioLShE59Yy+zCRyC+gPigS1kz
23xvo14AsOIPYV14Tk/SsHyq6E0eTk7VzaIE197giiINUERPECQQDSKmtPTh/lRKw7
24HSZSM0I1mFWn/1zqrAbontRQY5w98QWIOe5qmzYyFbPXYT3d9BzlsMyhgiRNoBbD
25yvohSHXJAkEAwAHx6ezAZeWWzD5yXD36nyjpkVCw7Tk7TSmOceLJMWt1QcrCfqlS
26xA5jjpQ6Z8suU5DdtWAryM2sAir1WisYzwJAd6Zcx56jvAQ3xcPXsE6scBTVFzrj
277FqZ6E+cclPzfLQ+QQsyOBE7bpI6e/FJppY26XGZXo3YGzV8IGXrt40oOQJALETG
28h86EFXo3qGOFbmsDy4pdP5nBERCu8X1xUCSfintiD4c2DInxgS5oGclnJeMcjTvL
29QjQoJCX3UJCi/OUO1QJBAKgcDHWjMvt+l1pjJBsSEZ0HX9AAIIVx0RQmbFGS+F2Q
30hhu5l77WnnZOQ9vvhV5u7NPCUF9nhU3jh60qWWO8mkc=
31-----END RSA PRIVATE KEY-----
diff --git a/src/lib/libssl/src/certs/rsa-cca.pem b/src/lib/libssl/src/certs/rsa-cca.pem
new file mode 100644
index 0000000000..69f5c1c84c
--- /dev/null
+++ b/src/lib/libssl/src/certs/rsa-cca.pem
@@ -0,0 +1,19 @@
1subject=/C=US/O=RSA Data Security, Inc./OU=Commercial Certification Authority
2issuer= /C=US/O=RSA Data Security, Inc./OU=Commercial Certification Authority
3notBefore=941104185834Z
4notAfter =991103185834Z
5-----BEGIN X509 CERTIFICATE-----
6
7MIICIzCCAZACBQJBAAAWMA0GCSqGSIb3DQEBAgUAMFwxCzAJBgNVBAYTAlVTMSAw
8HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjErMCkGA1UECxMiQ29tbWVy
9Y2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NDExMDQxODU4MzRaFw05
10OTExMDMxODU4MzRaMFwxCzAJBgNVBAYTAlVTMSAwHgYDVQQKExdSU0EgRGF0YSBT
11ZWN1cml0eSwgSW5jLjErMCkGA1UECxMiQ29tbWVyY2lhbCBDZXJ0aWZpY2F0aW9u
12IEF1dGhvcml0eTCBmzANBgkqhkiG9w0BAQEFAAOBiQAwgYUCfgCk+4Fie84QJ93o
13975sbsZwmdu41QUDaSiCnHJ/lj+O7Kwpkj+KFPhCdr69XQO5kNTQvAayUTNfxMK/
14touPmbZiImDd298ggrTKoi8tUO2UMt7gVY3UaOLgTNLNBRYulWZcYVI4HlGogqHE
157yXpCuaLK44xZtn42f29O2nZ6wIDAQABMA0GCSqGSIb3DQEBAgUAA34AdrW2EP4j
169/dZYkuwX5zBaLxJu7NJbyFHXSudVMQAKD+YufKKg5tgf+tQx6sFEC097TgCwaVI
170v5loMC86qYjFmZsGySp8+x5NRhPJsjjr1BKx6cxa9B8GJ1Qv6km+iYrRpwUqbtb
18MJhCKLVLU7tDCZJAuqiqWqTGtotXTcU=
19-----END X509 CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/timCA.pem b/src/lib/libssl/src/certs/timCA.pem
new file mode 100644
index 0000000000..9c8d5bf9c6
--- /dev/null
+++ b/src/lib/libssl/src/certs/timCA.pem
@@ -0,0 +1,16 @@
1Tims test GCI CA
2
3-----BEGIN CERTIFICATE-----
4MIIB8DCCAZoCAQAwDQYJKoZIhvcNAQEEBQAwgYIxCzAJBgNVBAYTAkFVMRMwEQYD
5VQQIEwpRdWVlbnNsYW5kMREwDwYDVQQHEwhCcmlzYmFuZTEaMBgGA1UEChMRQ3J5
6cHRTb2Z0IFB0eSBMdGQxFDASBgNVBAsTC2RldmVsb3BtZW50MRkwFwYDVQQDExBD
7cnlwdFNvZnQgRGV2IENBMB4XDTk3MDMyMjEzMzQwNFoXDTk4MDMyMjEzMzQwNFow
8gYIxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpRdWVlbnNsYW5kMREwDwYDVQQHEwhC
9cmlzYmFuZTEaMBgGA1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxFDASBgNVBAsTC2Rl
10dmVsb3BtZW50MRkwFwYDVQQDExBDcnlwdFNvZnQgRGV2IENBMFwwDQYJKoZIhvcN
11AQEBBQADSwAwSAJBAOAOAqogG5QwAmLhzyO4CoRnx/wVy4NZP4dxJy83O1EnL0rw
12OdsamJKvPOLHgSXo3gDu9uVyvCf/QJmZAmC5ml8CAwEAATANBgkqhkiG9w0BAQQF
13AANBADRRS/GVdd7rAqRW6SdmgLJduOU2yq3avBu99kRqbp9A/dLu6r6jU+eP4oOA
14TfdbFZtAAD2Hx9jUtY3tfdrJOb8=
15-----END CERTIFICATE-----
16
diff --git a/src/lib/libssl/src/certs/tjhCA.pem b/src/lib/libssl/src/certs/tjhCA.pem
new file mode 100644
index 0000000000..67bee1b200
--- /dev/null
+++ b/src/lib/libssl/src/certs/tjhCA.pem
@@ -0,0 +1,15 @@
1-----BEGIN CERTIFICATE-----
2MIICVjCCAgACAQAwDQYJKoZIhvcNAQEEBQAwgbUxCzAJBgNVBAYTAkFVMRMwEQYD
3VQQIEwpRdWVlbnNsYW5kMREwDwYDVQQHEwhCcmlzYmFuZTEaMBgGA1UEChMRQ3J5
4cHRTb2Z0IFB0eSBMdGQxLDAqBgNVBAsTI1dPUlRITEVTUyBDRVJUSUZJQ0FUSU9O
5IEFVVEhPUklUSUVTMTQwMgYDVQQDEytaRVJPIFZBTFVFIENBIC0gREVNT05TVFJB
6VElPTiBQVVJQT1NFUyBPTkxZMB4XDTk3MDQwMzEzMjI1NFoXDTk4MDQwMzEzMjI1
7NFowgbUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpRdWVlbnNsYW5kMREwDwYDVQQH
8EwhCcmlzYmFuZTEaMBgGA1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxLDAqBgNVBAsT
9I1dPUlRITEVTUyBDRVJUSUZJQ0FUSU9OIEFVVEhPUklUSUVTMTQwMgYDVQQDEyta
10RVJPIFZBTFVFIENBIC0gREVNT05TVFJBVElPTiBQVVJQT1NFUyBPTkxZMFwwDQYJ
11KoZIhvcNAQEBBQADSwAwSAJBAOZ7T7yqP/tyspcko3yPY1y0Cm2EmwNvzW4QgVXR
12Fjs3HmJ4xtSpXdo6mwcGezL3Abt/aQXaxv9PU8xt+Jr0OFUCAwEAATANBgkqhkiG
139w0BAQQFAANBAOQpYmGgyCqCy1OljgJhCqQOu627oVlHzK1L+t9vBaMfn40AVUR4
14WzQVWO31KTgi5vTK1U+3h46fgUWqQ0h+6rU=
15-----END CERTIFICATE-----
diff --git a/src/lib/libssl/src/certs/vsign2.pem b/src/lib/libssl/src/certs/vsign2.pem
new file mode 100644
index 0000000000..2386e149d0
--- /dev/null
+++ b/src/lib/libssl/src/certs/vsign2.pem
@@ -0,0 +1,31 @@
1 subject=/L=Internet/O=VeriSign, Inc./OU=VeriSign Class 2 CA - Individual Subscriber
2 issuer= /L=Internet/O=VeriSign, Inc./OU=VeriSign Class 2 CA - Individual Subscriber
3
4-----BEGIN CERTIFICATE-----
5MIIEkzCCA/ygAwIBAgIRANDTUpSRL3nTFeMrMayFSPAwDQYJKoZIhvcNAQECBQAw
6YjERMA8GA1UEBxMISW50ZXJuZXQxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMTQw
7MgYDVQQLEytWZXJpU2lnbiBDbGFzcyAyIENBIC0gSW5kaXZpZHVhbCBTdWJzY3Jp
8YmVyMB4XDTk2MDYwNDAwMDAwMFoXDTk4MDYwNDIzNTk1OVowYjERMA8GA1UEBxMI
9SW50ZXJuZXQxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMTQwMgYDVQQLEytWZXJp
10U2lnbiBDbGFzcyAyIENBIC0gSW5kaXZpZHVhbCBTdWJzY3JpYmVyMIGfMA0GCSqG
11SIb3DQEBAQUAA4GNADCBiQKBgQC6A+2czKGRcYMfm8gdnk+0de99TDDzsqo0v5nb
12RsbUmMcdRQ7nsMbRWe0SAb/9QoLTZ/cJ0iOBqdrkz7UpqqKarVoTSdlSMVM92tWp
133bJncZHQD1t4xd6lQVdI1/T6R+5J0T1ukOdsI9Jmf+F28S6g3R3L1SFwiHKeZKZv
14z+793wIDAQABo4ICRzCCAkMwggIpBgNVHQMBAf8EggIdMIICGTCCAhUwggIRBgtg
15hkgBhvhFAQcBATCCAgAWggGrVGhpcyBjZXJ0aWZpY2F0ZSBpbmNvcnBvcmF0ZXMg
16YnkgcmVmZXJlbmNlLCBhbmQgaXRzIHVzZSBpcyBzdHJpY3RseSBzdWJqZWN0IHRv
17LCB0aGUgVmVyaVNpZ24gQ2VydGlmaWNhdGlvbiBQcmFjdGljZSBTdGF0ZW1lbnQg
18KENQUyksIGF2YWlsYWJsZSBhdDogaHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL0NQ
19Uy0xLjA7IGJ5IEUtbWFpbCBhdCBDUFMtcmVxdWVzdHNAdmVyaXNpZ24uY29tOyBv
20ciBieSBtYWlsIGF0IFZlcmlTaWduLCBJbmMuLCAyNTkzIENvYXN0IEF2ZS4sIE1v
21dW50YWluIFZpZXcsIENBIDk0MDQzIFVTQSBUZWwuICsxICg0MTUpIDk2MS04ODMw
22IENvcHlyaWdodCAoYykgMTk5NiBWZXJpU2lnbiwgSW5jLiAgQWxsIFJpZ2h0cyBS
23ZXNlcnZlZC4gQ0VSVEFJTiBXQVJSQU5USUVTIERJU0NMQUlNRUQgYW5kIExJQUJJ
24TElUWSBMSU1JVEVELqAOBgxghkgBhvhFAQcBAQGhDgYMYIZIAYb4RQEHAQECMC8w
25LRYraHR0cHM6Ly93d3cudmVyaXNpZ24uY29tL3JlcG9zaXRvcnkvQ1BTLTEuMDAU
26BglghkgBhvhCAQEBAf8EBAMCAgQwDQYJKoZIhvcNAQECBQADgYEApRJRkNBqLLgs
2753IR/d18ODdLOWMTZ+QOOxBrq460iBEdUwgF8vmPRX1ku7UiDeNzaLlurE6eFqHq
282zPyK5j60zfTLVJMWKcQWwTJLjHtXrW8pxhNtFc6Fdvy5ZkHnC/9NIl7/t4U6WqB
29p4y+p7SdMIkEwIZfds0VbnQyX5MRUJY=
30-----END CERTIFICATE-----
31
diff --git a/src/lib/libssl/src/crypto/Makefile.ssl b/src/lib/libssl/src/crypto/Makefile.ssl
new file mode 100644
index 0000000000..efdbba38ac
--- /dev/null
+++ b/src/lib/libssl/src/crypto/Makefile.ssl
@@ -0,0 +1,161 @@
1#
2# SSLeay/crypto/Makefile
3#
4
5DIR= crypto
6TOP= ..
7CC= cc
8INCLUDE= -I. -I../include
9INCLUDES= -I.. -I../../include
10CFLAG= -g
11INSTALLTOP= /usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15RM= /bin/rm -f
16AR= ar r
17
18MAKE= make -f Makefile.ssl
19MAKEDEPEND= makedepend -f Makefile.ssl
20MAKEFILE= Makefile.ssl
21
22PEX_LIBS=
23EX_LIBS=
24
25CFLAGS= $(INCLUDE) $(CFLAG) -DCFLAGS=" \"$(CC) $(CFLAG)\" "
26
27ERR=crypto
28ERRC=cpt_err
29
30LIBS=
31
32SDIRS= md2 md5 sha mdc2 hmac ripemd \
33 des rc2 rc4 rc5 idea bf cast \
34 bn rsa dsa dh \
35 buffer bio stack lhash rand err objects \
36 evp pem x509 \
37 asn1 conf txt_db pkcs7
38
39GENERAL=Makefile README
40
41LIB= $(TOP)/libcrypto.a
42LIBSRC= cryptlib.c mem.c cversion.c ex_data.c $(ERRC).c
43LIBOBJ= cryptlib.o mem.o cversion.o ex_data.o $(ERRC).o
44
45SRC= $(LIBSRC)
46
47EXHEADER= crypto.h cryptall.h
48HEADER= cryptlib.h date.h $(EXHEADER)
49
50ALL= $(GENERAL) $(SRC) $(HEADER)
51
52top:
53 @(cd ..; $(MAKE) DIRS=$(DIR) all)
54
55all: date.h lib subdirs
56
57date.h: ../Makefile.ssl ../VERSION
58 echo "#define DATE \"`date`\"" >date.h
59
60subdirs:
61 @for i in $(SDIRS) ;\
62 do \
63 (cd $$i; echo "making all in $$i..."; \
64 $(MAKE) CC='$(CC)' INCLUDES='${INCLUDES}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' BN_MULW='${BN_MULW}' DES_ENC='${DES_ENC}' SHA1_ASM_OBJ='${SHA1_ASM_OBJ}' MD5_ASM_OBJ='${MD5_ASM_OBJ}' RMD160_ASM_OBJ='${RMD160_ASM_OBJ}' BF_ENC='${BF_ENC}' CAST_ENC='${CAST_ENC}' RC4_ENC='${RC4_ENC}' RC5_ENC='${RC5_ENC}' AR='${AR}' all ); \
65 done;
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69 @for i in $(SDIRS) ;\
70 do \
71 (cd $$i; echo "making 'files' in $$i..."; \
72 $(MAKE) files ); \
73 done;
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../include $(HEADER) ;
79 $(TOP)/util/mklink.sh ../test $(TEST) ;
80 $(TOP)/util/mklink.sh ../apps $(APPS) ;
81 $(TOP)/util/point.sh Makefile.ssl Makefile;
82 @for i in $(SDIRS) ;\
83 do \
84 (cd $$i; echo "making links in $$i..."; \
85 $(MAKE) links ); \
86 done;
87
88lib: $(LIBOBJ)
89 $(AR) $(LIB) $(LIBOBJ)
90 sh $(TOP)/util/ranlib.sh $(LIB)
91 @touch lib
92
93libs:
94 @for i in $(SDIRS) ;\
95 do \
96 (cd $$i; echo "making libs in $$i..."; \
97 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' AR='${AR}' lib ); \
98 done;
99
100tests:
101 @for i in $(SDIRS) ;\
102 do \
103 (cd $$i; echo "making tests in $$i..."; \
104 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' AR='${AR}' tests ); \
105 done;
106
107install:
108 @for i in $(EXHEADER) ;\
109 do \
110 (cp $$i $(INSTALLTOP)/include/$$i; \
111 chmod 644 $(INSTALLTOP)/include/$$i ); \
112 done;
113 @for i in $(SDIRS) ;\
114 do \
115 (cd $$i; echo "making install in $$i..."; \
116 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' install ); \
117 done;
118
119lint:
120 @for i in $(SDIRS) ;\
121 do \
122 (cd $$i; echo "making lint in $$i..."; \
123 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' lint ); \
124 done;
125
126depend:
127 $(MAKEDEPEND) $(INCLUDE) $(PROGS) $(LIBSRC)
128 @for i in $(SDIRS) ;\
129 do \
130 (cd $$i; echo "making depend in $$i..."; \
131 $(MAKE) MAKEFILE='${MAKEFILE}' INCLUDES='${INCLUDES}' MAKEDEPEND='${MAKEDEPEND}' depend ); \
132 done;
133
134clean:
135 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
136 @for i in $(SDIRS) ;\
137 do \
138 (cd $$i; echo "making clean in $$i..."; \
139 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' clean ); \
140 done;
141
142dclean:
143 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
144 mv -f Makefile.new $(MAKEFILE)
145 @for i in $(SDIRS) ;\
146 do \
147 (cd $$i; echo "making dclean in $$i..."; \
148 $(MAKE) CC='$(CC)' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' dclean ); \
149 done;
150
151errors:
152 perl ./err/err_code.pl -conf err/ssleay.ec *.c */*.c ../ssl/*.c ../rsaref/*.c
153 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
154 perl err/err_genc.pl -s $(ERR).h $(ERRC).c
155 @for i in $(SDIRS) ;\
156 do \
157 (cd $$i; echo "making errors in $$i..."; \
158 $(MAKE) errors ); \
159 done;
160
161# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/asn1/Makefile.ssl b/src/lib/libssl/src/crypto/asn1/Makefile.ssl
new file mode 100644
index 0000000000..30751bd156
--- /dev/null
+++ b/src/lib/libssl/src/crypto/asn1/Makefile.ssl
@@ -0,0 +1,120 @@
1#
2# SSLeay/crypto/asn1/Makefile
3#
4
5DIR= asn1
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=asn1
19ERRC=asn1_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= a_object.c a_bitstr.c a_utctm.c a_int.c a_octet.c a_print.c \
26 a_type.c a_set.c a_dup.c a_d2i_fp.c a_i2d_fp.c \
27 a_sign.c a_digest.c a_verify.c \
28 x_algor.c x_val.c x_pubkey.c x_sig.c x_req.c x_attrib.c \
29 x_name.c x_cinf.c x_x509.c x_crl.c x_info.c x_spki.c \
30 d2i_r_pr.c i2d_r_pr.c d2i_r_pu.c i2d_r_pu.c \
31 d2i_s_pr.c i2d_s_pr.c d2i_s_pu.c i2d_s_pu.c \
32 d2i_pu.c d2i_pr.c i2d_pu.c i2d_pr.c\
33 t_req.c t_x509.c t_pkey.c \
34 p7_i_s.c p7_signi.c p7_signd.c p7_recip.c p7_enc_c.c p7_evp.c \
35 p7_dgst.c p7_s_e.c p7_enc.c p7_lib.c \
36 f_int.c f_string.c i2d_dhp.c i2d_dsap.c d2i_dhp.c d2i_dsap.c n_pkey.c \
37 a_hdr.c x_pkey.c a_bool.c x_exten.c \
38 asn1_par.c asn1_lib.c $(ERRC).c a_meth.c a_bytes.c \
39 evp_asn1.c
40LIBOBJ= a_object.o a_bitstr.o a_utctm.o a_int.o a_octet.o a_print.o \
41 a_type.o a_set.o a_dup.o a_d2i_fp.o a_i2d_fp.o \
42 a_sign.o a_digest.o a_verify.o \
43 x_algor.o x_val.o x_pubkey.o x_sig.o x_req.o x_attrib.o \
44 x_name.o x_cinf.o x_x509.o x_crl.o x_info.o x_spki.o \
45 d2i_r_pr.o i2d_r_pr.o d2i_r_pu.o i2d_r_pu.o \
46 d2i_s_pr.o i2d_s_pr.o d2i_s_pu.o i2d_s_pu.o \
47 d2i_pu.o d2i_pr.o i2d_pu.o i2d_pr.o \
48 t_req.o t_x509.o t_pkey.o \
49 p7_i_s.o p7_signi.o p7_signd.o p7_recip.o p7_enc_c.o p7_evp.o \
50 p7_dgst.o p7_s_e.o p7_enc.o p7_lib.o \
51 f_int.o f_string.o i2d_dhp.o i2d_dsap.o d2i_dhp.o d2i_dsap.o n_pkey.o \
52 a_hdr.o x_pkey.o a_bool.o x_exten.o \
53 asn1_par.o asn1_lib.o $(ERRC).o a_meth.o a_bytes.o \
54 evp_asn1.o
55
56SRC= $(LIBSRC)
57
58EXHEADER= asn1.h asn1_mac.h
59HEADER= $(EXHEADER)
60
61ALL= $(GENERAL) $(SRC) $(HEADER)
62
63top:
64 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
65
66test: test.c
67 cc -g -I../../include -c test.c
68 cc -g -I../../include -o test test.o -L../.. -lcrypto
69
70pk: pk.c
71 cc -g -I../../include -c pk.c
72 cc -g -I../../include -o pk pk.o -L../.. -lcrypto
73
74all: lib
75
76lib: $(LIBOBJ)
77 $(AR) $(LIB) $(LIBOBJ)
78 sh $(TOP)/util/ranlib.sh $(LIB)
79 @touch lib
80
81files:
82 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
83
84links:
85 /bin/rm -f Makefile
86 $(TOP)/util/point.sh Makefile.ssl Makefile ;
87 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
88 $(TOP)/util/mklink.sh ../../test $(TEST)
89 $(TOP)/util/mklink.sh ../../apps $(APPS)
90
91install:
92 @for i in $(EXHEADER) ; \
93 do \
94 (cp $$i $(INSTALLTOP)/include/$$i; \
95 chmod 644 $(INSTALLTOP)/include/$$i ); \
96 done;
97
98tags:
99 ctags $(SRC)
100
101tests:
102
103lint:
104 lint -DLINT $(INCLUDES) $(SRC)>fluff
105
106depend:
107 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
108
109dclean:
110 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
111 mv -f Makefile.new $(MAKEFILE)
112
113clean:
114 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
115
116errors:
117 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
118 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
119
120# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/asn1/f.c b/src/lib/libssl/src/crypto/asn1/f.c
new file mode 100644
index 0000000000..2ab3a262ac
--- /dev/null
+++ b/src/lib/libssl/src/crypto/asn1/f.c
@@ -0,0 +1,80 @@
1/* crypto/asn1/f.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58#include <stdio.h>
59#include "asn1.h"
60#include "err.h"
61
62main()
63 {
64 ASN1_TYPE *at;
65 char buf[512];
66 int n;
67 long l;
68
69 at=ASN1_TYPE_new();
70
71 n=ASN1_TYPE_set_int_octetstring(at,98736,"01234567",8);
72 printf("%d\n",n);
73 n=ASN1_TYPE_get_int_octetstring(at,&l,buf,8);
74 buf[8]='\0';
75 printf("%ld %d %d\n",l,n,buf[8]);
76 buf[8]='\0';
77 printf("%s\n",buf);
78 ERR_load_crypto_strings();
79 ERR_print_errors_fp(stderr);
80 }
diff --git a/src/lib/libssl/src/crypto/asn1/x_cinf.c b/src/lib/libssl/src/crypto/asn1/x_cinf.c
new file mode 100644
index 0000000000..4fc2cc9f6e
--- /dev/null
+++ b/src/lib/libssl/src/crypto/asn1/x_cinf.c
@@ -0,0 +1,197 @@
1/* crypto/asn1/x_cinf.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include "asn1_mac.h"
62
63/*
64 * ASN1err(ASN1_F_D2I_X509_CINF,ASN1_R_LENGTH_MISMATCH);
65 * ASN1err(ASN1_F_X509_CINF_NEW,ASN1_R_LENGTH_MISMATCH);
66 */
67
68int i2d_X509_CINF(a,pp)
69X509_CINF *a;
70unsigned char **pp;
71 {
72 int v1=0,v2=0;
73 M_ASN1_I2D_vars(a);
74
75 M_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);
76 M_ASN1_I2D_len(a->serialNumber, i2d_ASN1_INTEGER);
77 M_ASN1_I2D_len(a->signature, i2d_X509_ALGOR);
78 M_ASN1_I2D_len(a->issuer, i2d_X509_NAME);
79 M_ASN1_I2D_len(a->validity, i2d_X509_VAL);
80 M_ASN1_I2D_len(a->subject, i2d_X509_NAME);
81 M_ASN1_I2D_len(a->key, i2d_X509_PUBKEY);
82 M_ASN1_I2D_len_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING);
83 M_ASN1_I2D_len_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING);
84 M_ASN1_I2D_len_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);
85
86 M_ASN1_I2D_seq_total();
87
88 M_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);
89 M_ASN1_I2D_put(a->serialNumber, i2d_ASN1_INTEGER);
90 M_ASN1_I2D_put(a->signature, i2d_X509_ALGOR);
91 M_ASN1_I2D_put(a->issuer, i2d_X509_NAME);
92 M_ASN1_I2D_put(a->validity, i2d_X509_VAL);
93 M_ASN1_I2D_put(a->subject, i2d_X509_NAME);
94 M_ASN1_I2D_put(a->key, i2d_X509_PUBKEY);
95 M_ASN1_I2D_put_IMP_opt(a->issuerUID, i2d_ASN1_BIT_STRING,1);
96 M_ASN1_I2D_put_IMP_opt(a->subjectUID, i2d_ASN1_BIT_STRING,2);
97 M_ASN1_I2D_put_EXP_set_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);
98
99 M_ASN1_I2D_finish();
100 }
101
102X509_CINF *d2i_X509_CINF(a,pp,length)
103X509_CINF **a;
104unsigned char **pp;
105long length;
106 {
107 int ver=0;
108 M_ASN1_D2I_vars(a,X509_CINF *,X509_CINF_new);
109
110 M_ASN1_D2I_Init();
111 M_ASN1_D2I_start_sequence();
112 /* we have the optional version field */
113 if (M_ASN1_next == (V_ASN1_CONTEXT_SPECIFIC | V_ASN1_CONSTRUCTED | 0))
114 {
115 M_ASN1_D2I_get_EXP_opt(ret->version,d2i_ASN1_INTEGER,0);
116 if (ret->version->data != NULL)
117 ver=ret->version->data[0];
118 }
119 else
120 {
121 if (ret->version != NULL)
122 {
123 ASN1_INTEGER_free(ret->version);
124 ret->version=NULL;
125 }
126 }
127 M_ASN1_D2I_get(ret->serialNumber,d2i_ASN1_INTEGER);
128 M_ASN1_D2I_get(ret->signature,d2i_X509_ALGOR);
129 M_ASN1_D2I_get(ret->issuer,d2i_X509_NAME);
130 M_ASN1_D2I_get(ret->validity,d2i_X509_VAL);
131 M_ASN1_D2I_get(ret->subject,d2i_X509_NAME);
132 M_ASN1_D2I_get(ret->key,d2i_X509_PUBKEY);
133 if (ver >= 1) /* version 2 extensions */
134 {
135 if (ret->issuerUID != NULL)
136 {
137 ASN1_BIT_STRING_free(ret->issuerUID);
138 ret->issuerUID=NULL;
139 }
140 if (ret->subjectUID != NULL)
141 {
142 ASN1_BIT_STRING_free(ret->subjectUID);
143 ret->issuerUID=NULL;
144 }
145 M_ASN1_D2I_get_IMP_opt(ret->issuerUID,d2i_ASN1_BIT_STRING, 1,
146 V_ASN1_BIT_STRING);
147 M_ASN1_D2I_get_IMP_opt(ret->subjectUID,d2i_ASN1_BIT_STRING, 2,
148 V_ASN1_BIT_STRING);
149 }
150 if (ver >= 2) /* version 3 extensions */
151 {
152 if (ret->extensions != NULL)
153 while (sk_num(ret->extensions))
154 X509_EXTENSION_free((X509_EXTENSION *)
155 sk_pop(ret->extensions));
156 M_ASN1_D2I_get_EXP_set_opt(ret->extensions,d2i_X509_EXTENSION,3,
157 V_ASN1_SEQUENCE);
158 }
159 M_ASN1_D2I_Finish(a,X509_CINF_free,ASN1_F_D2I_X509_CINF);
160 }
161
162X509_CINF *X509_CINF_new()
163 {
164 X509_CINF *ret=NULL;
165
166 M_ASN1_New_Malloc(ret,X509_CINF);
167 ret->version=NULL;
168 M_ASN1_New(ret->serialNumber,ASN1_INTEGER_new);
169 M_ASN1_New(ret->signature,X509_ALGOR_new);
170 M_ASN1_New(ret->issuer,X509_NAME_new);
171 M_ASN1_New(ret->validity,X509_VAL_new);
172 M_ASN1_New(ret->subject,X509_NAME_new);
173 M_ASN1_New(ret->key,X509_PUBKEY_new);
174 ret->issuerUID=NULL;
175 ret->subjectUID=NULL;
176 ret->extensions=NULL;
177 return(ret);
178 M_ASN1_New_Error(ASN1_F_X509_CINF_NEW);
179 }
180
181void X509_CINF_free(a)
182X509_CINF *a;
183 {
184 if (a == NULL) return;
185 ASN1_INTEGER_free(a->version);
186 ASN1_INTEGER_free(a->serialNumber);
187 X509_ALGOR_free(a->signature);
188 X509_NAME_free(a->issuer);
189 X509_VAL_free(a->validity);
190 X509_NAME_free(a->subject);
191 X509_PUBKEY_free(a->key);
192 ASN1_BIT_STRING_free(a->issuerUID);
193 ASN1_BIT_STRING_free(a->subjectUID);
194 sk_pop_free(a->extensions,X509_EXTENSION_free);
195 Free((char *)a);
196 }
197
diff --git a/src/lib/libssl/src/crypto/bf/Makefile.ssl b/src/lib/libssl/src/crypto/bf/Makefile.ssl
new file mode 100644
index 0000000000..236671f238
--- /dev/null
+++ b/src/lib/libssl/src/crypto/bf/Makefile.ssl
@@ -0,0 +1,107 @@
1#
2# SSLeay/crypto/blowfish/Makefile
3#
4
5DIR= bf
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17BF_ENC= bf_enc.o
18# or use
19#DES_ENC= bx86-elf.o
20
21CFLAGS= $(INCLUDES) $(CFLAG)
22
23GENERAL=Makefile
24TEST=bftest.c
25APPS=
26
27LIB=$(TOP)/libcrypto.a
28LIBSRC=bf_skey.c bf_ecb.c bf_enc.c bf_cfb64.c bf_ofb64.c
29LIBOBJ=bf_skey.o bf_ecb.o $(BF_ENC) bf_cfb64.o bf_ofb64.o
30
31SRC= $(LIBSRC)
32
33EXHEADER= blowfish.h
34HEADER= bf_pi.h bf_locl.h $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: lib
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48# elf
49asm/bx86-elf.o: asm/bx86unix.cpp
50 $(CPP) -DELF asm/bx86unix.cpp | as -o asm/bx86-elf.o
51
52# solaris
53asm/bx86-sol.o: asm/bx86unix.cpp
54 $(CC) -E -DSOL asm/bx86unix.cpp | sed 's/^#.*//' > asm/bx86-sol.s
55 as -o asm/bx86-sol.o asm/bx86-sol.s
56 rm -f asm/bx86-sol.s
57
58# a.out
59asm/bx86-out.o: asm/bx86unix.cpp
60 $(CPP) -DOUT asm/bx86unix.cpp | as -o asm/bx86-out.o
61
62# bsdi
63asm/bx86bsdi.o: asm/bx86unix.cpp
64 $(CPP) -DBSDI asm/bx86unix.cpp | as -o asm/bx86bsdi.o
65
66asm/bx86unix.cpp:
67 (cd asm; perl bf-586.pl cpp >bx86unix.cpp)
68
69files:
70 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
71
72links:
73 /bin/rm -f Makefile
74 $(TOP)/util/point.sh Makefile.ssl Makefile ;
75 $(TOP)/util/point.sh ../../doc/blowfish.doc blowfish.doc ;
76 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
77 $(TOP)/util/mklink.sh ../../test $(TEST)
78 $(TOP)/util/mklink.sh ../../apps $(APPS)
79
80install:
81 @for i in $(EXHEADER) ; \
82 do \
83 (cp $$i $(INSTALLTOP)/include/$$i; \
84 chmod 644 $(INSTALLTOP)/include/$$i ); \
85 done;
86
87tags:
88 ctags $(SRC)
89
90tests:
91
92lint:
93 lint -DLINT $(INCLUDES) $(SRC)>fluff
94
95depend:
96 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
97
98dclean:
99 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
100 mv -f Makefile.new $(MAKEFILE)
101
102clean:
103 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
104
105errors:
106
107# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/bio/Makefile.ssl b/src/lib/libssl/src/crypto/bio/Makefile.ssl
new file mode 100644
index 0000000000..42e11e1c94
--- /dev/null
+++ b/src/lib/libssl/src/crypto/bio/Makefile.ssl
@@ -0,0 +1,92 @@
1#
2# SSLeay/crypto/bio/Makefile
3#
4
5DIR= bio
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=bio
19ERRC=bio_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= bio_lib.c bio_cb.c $(ERRC).c \
26 bss_mem.c bss_null.c bss_fd.c \
27 bss_file.c bss_sock.c bss_conn.c \
28 bf_null.c bf_buff.c b_print.c b_dump.c \
29 b_sock.c bss_acpt.c bf_nbio.c
30LIBOBJ= bio_lib.o bio_cb.o $(ERRC).o \
31 bss_mem.o bss_null.o bss_fd.o \
32 bss_file.o bss_sock.o bss_conn.o \
33 bf_null.o bf_buff.o b_print.o b_dump.o \
34 b_sock.o bss_acpt.o bf_nbio.o
35
36SRC= $(LIBSRC)
37
38EXHEADER= bio.h bss_file.c
39HEADER= $(EXHEADER)
40
41ALL= $(GENERAL) $(SRC) $(HEADER)
42
43top:
44 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
45
46all: lib
47
48lib: $(LIBOBJ)
49 $(AR) $(LIB) $(LIBOBJ)
50 sh $(TOP)/util/ranlib.sh $(LIB)
51 @touch lib
52
53files:
54 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
55
56links:
57 /bin/rm -f Makefile
58 $(TOP)/util/point.sh Makefile.ssl Makefile;
59 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
60 $(TOP)/util/mklink.sh ../../test $(TEST)
61 $(TOP)/util/mklink.sh ../../apps $(APPS)
62
63install:
64 @for i in $(EXHEADER) bss_file.c ; \
65 do \
66 (cp $$i $(INSTALLTOP)/include/$$i; \
67 chmod 644 $(INSTALLTOP)/include/$$i ); \
68 done;
69
70tags:
71 ctags $(SRC)
72
73tests:
74
75lint:
76 lint -DLINT $(INCLUDES) $(SRC)>fluff
77
78depend:
79 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
80
81dclean:
82 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
83 mv -f Makefile.new $(MAKEFILE)
84
85clean:
86 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
87
88errors:
89 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
90 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
91
92# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/bn/Makefile.ssl b/src/lib/libssl/src/crypto/bn/Makefile.ssl
new file mode 100644
index 0000000000..9809d26cbc
--- /dev/null
+++ b/src/lib/libssl/src/crypto/bn/Makefile.ssl
@@ -0,0 +1,133 @@
1#
2# SSLeay/crypto/bn/Makefile
3#
4
5DIR= bn
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16BN_MULW= bn_mulw.o
17# or use
18#BN_MULW= bn86-elf.o
19
20CFLAGS= $(INCLUDES) $(CFLAG)
21
22ERR=bn
23ERRC=bn_err
24GENERAL=Makefile
25TEST=bntest.c exptest.c
26APPS=
27
28LIB=$(TOP)/libcrypto.a
29LIBSRC= bn_add.c bn_div.c bn_exp.c bn_lib.c bn_mod.c bn_mul.c \
30 bn_print.c bn_rand.c bn_shift.c bn_sub.c bn_word.c bn_blind.c \
31 bn_gcd.c bn_prime.c $(ERRC).c bn_sqr.c bn_mulw.c bn_recp.c bn_mont.c \
32 bn_mpi.c
33
34LIBOBJ= bn_add.o bn_div.o bn_exp.o bn_lib.o bn_mod.o bn_mul.o \
35 bn_print.o bn_rand.o bn_shift.o bn_sub.o bn_word.o bn_blind.o \
36 bn_gcd.o bn_prime.o $(ERRC).o bn_sqr.o $(BN_MULW) bn_recp.o bn_mont.o \
37 bn_mpi.o
38
39
40SRC= $(LIBSRC)
41
42EXHEADER= bn.h
43HEADER= bn_lcl.h bn_prime.h $(EXHEADER)
44
45ALL= $(GENERAL) $(SRC) $(HEADER)
46
47top:
48 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
49
50all: lib
51
52knuth: bn_knuth.c
53 cc -pg -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
54
55knuth.fast: bn_knuth.c
56 cc -pg -fast -I.. -I../../include bn_knuth.c -o knuth $(LIB) #../../../libefence.a
57
58
59lib: $(LIBOBJ)
60 $(AR) $(LIB) $(LIBOBJ)
61 sh $(TOP)/util/ranlib.sh $(LIB)
62 @touch lib
63
64# elf
65asm/bn86-elf.o: asm/bn86unix.cpp
66 $(CPP) -DELF asm/bn86unix.cpp | as -o asm/bn86-elf.o
67
68# solaris
69asm/bn86-sol.o: asm/bn86unix.cpp
70 $(CC) -E -DSOL asm/bn86unix.cpp | sed 's/^#.*//' > asm/bn86-sol.s
71 as -o asm/bn86-sol.o asm/bn86-sol.s
72 rm -f asm/bn86-sol.s
73
74# a.out
75asm/bn86-out.o: asm/bn86unix.cpp
76 $(CPP) -DOUT asm/bn86unix.cpp | as -o asm/bn86-out.o
77
78# bsdi
79asm/bn86bsdi.o: asm/bn86unix.cpp
80 $(CPP) -DBSDI asm/bn86unix.cpp | as -o asm/bn86bsdi.o
81
82asm/bn86unix.cpp:
83 (cd asm; perl bn-586.pl cpp >bn86unix.cpp )
84
85files:
86 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
87
88links:
89 /bin/rm -f Makefile
90 $(TOP)/util/point.sh Makefile.ssl Makefile ;
91 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
92 $(TOP)/util/mklink.sh ../../test $(TEST)
93 $(TOP)/util/mklink.sh ../../apps $(APPS)
94
95install:
96 @for i in $(EXHEADER) ; \
97 do \
98 (cp $$i $(INSTALLTOP)/include/$$i; \
99 chmod 644 $(INSTALLTOP)/include/$$i ); \
100 done;
101
102exptest:
103 /bin/rm -f exptest
104 gcc -I../../include -g2 -ggdb -o exptest exptest.c ../../libcrypto.a
105
106div:
107 /bin/rm -f a.out
108 gcc -I.. -g div.c ../../libcrypto.a
109
110tags:
111 ctags $(SRC)
112
113tests:
114
115lint:
116 lint -DLINT $(INCLUDES) $(SRC)>fluff
117
118depend:
119 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
120
121dclean:
122 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
123 mv -f Makefile.new $(MAKEFILE)
124
125clean:
126 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff bn_mulw.s
127
128errors:
129 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # special case .org
130 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
131 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
132
133# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/buffer/Makefile.ssl b/src/lib/libssl/src/crypto/buffer/Makefile.ssl
new file mode 100644
index 0000000000..a5f150e523
--- /dev/null
+++ b/src/lib/libssl/src/crypto/buffer/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/buffer/Makefile
3#
4
5DIR= buffer
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=buffer
19ERRC=buf_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= buffer.c $(ERRC).c
26LIBOBJ= buffer.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= buffer.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/cast/Makefile.ssl b/src/lib/libssl/src/crypto/cast/Makefile.ssl
new file mode 100644
index 0000000000..0143827ae5
--- /dev/null
+++ b/src/lib/libssl/src/crypto/cast/Makefile.ssl
@@ -0,0 +1,109 @@
1#
2# SSLeay/crypto/cast/Makefile
3#
4
5DIR= cast
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17CAST_ENC=c_enc.o
18# or use
19#CAST_ENC=asm/cx86-elf.o
20#CAST_ENC=asm/cx86-out.o
21#CAST_ENC=asm/cx86-sol.o
22#CAST_ENC=asm/cx86bdsi.o
23
24CFLAGS= $(INCLUDES) $(CFLAG)
25
26GENERAL=Makefile
27TEST=casttest.c
28APPS=
29
30LIB=$(TOP)/libcrypto.a
31LIBSRC=c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c
32LIBOBJ=c_skey.o c_ecb.o $(CAST_ENC) c_cfb64.o c_ofb64.o
33
34SRC= $(LIBSRC)
35
36EXHEADER= cast.h
37HEADER= cast_s.h cast_lcl.h $(EXHEADER)
38
39ALL= $(GENERAL) $(SRC) $(HEADER)
40
41top:
42 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
43
44all: lib
45
46lib: $(LIBOBJ)
47 $(AR) $(LIB) $(LIBOBJ)
48 sh $(TOP)/util/ranlib.sh $(LIB)
49 @touch lib
50
51# elf
52asm/cx86-elf.o: asm/cx86unix.cpp
53 $(CPP) -DELF asm/cx86unix.cpp | as -o asm/cx86-elf.o
54
55# solaris
56asm/cx86-sol.o: asm/cx86unix.cpp
57 $(CC) -E -DSOL asm/cx86unix.cpp | sed 's/^#.*//' > asm/cx86-sol.s
58 as -o asm/cx86-sol.o asm/cx86-sol.s
59 rm -f asm/cx86-sol.s
60
61# a.out
62asm/cx86-out.o: asm/cx86unix.cpp
63 $(CPP) -DOUT asm/cx86unix.cpp | as -o asm/cx86-out.o
64
65# bsdi
66asm/cx86bsdi.o: asm/cx86unix.cpp
67 $(CPP) -DBSDI asm/cx86unix.cpp | as -o asm/cx86bsdi.o
68
69asm/cx86unix.cpp:
70 (cd asm; perl cast-586.pl cpp >cx86unix.cpp)
71
72files:
73 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
79 $(TOP)/util/mklink.sh ../../test $(TEST)
80 $(TOP)/util/mklink.sh ../../apps $(APPS)
81
82install:
83 @for i in $(EXHEADER) ; \
84 do \
85 (cp $$i $(INSTALLTOP)/include/$$i; \
86 chmod 644 $(INSTALLTOP)/include/$$i ); \
87 done;
88
89tags:
90 ctags $(SRC)
91
92tests:
93
94lint:
95 lint -DLINT $(INCLUDES) $(SRC)>fluff
96
97depend:
98 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
99
100dclean:
101 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
102 mv -f Makefile.new $(MAKEFILE)
103
104clean:
105 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
106
107errors:
108
109# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/conf/Makefile.ssl b/src/lib/libssl/src/crypto/conf/Makefile.ssl
new file mode 100644
index 0000000000..00e917aa44
--- /dev/null
+++ b/src/lib/libssl/src/crypto/conf/Makefile.ssl
@@ -0,0 +1,85 @@
1#
2# SSLeay/crypto/conf/Makefile
3#
4
5DIR= conf
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=conf
19ERRC=conf_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= conf.c $(ERRC).c
26
27LIBOBJ= conf.o $(ERRC).o
28
29SRC= $(LIBSRC)
30
31EXHEADER= conf.h
32HEADER= conf_lcl.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46files:
47 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
48
49links:
50 /bin/rm -f Makefile
51 $(TOP)/util/point.sh Makefile.ssl Makefile ;
52 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
53 $(TOP)/util/mklink.sh ../../test $(TEST)
54 $(TOP)/util/mklink.sh ../../apps $(APPS)
55
56install:
57 @for i in $(EXHEADER) ; \
58 do \
59 (cp $$i $(INSTALLTOP)/include/$$i; \
60 chmod 644 $(INSTALLTOP)/include/$$i ); \
61 done;
62
63tags:
64 ctags $(SRC)
65
66tests:
67
68lint:
69 lint -DLINT $(INCLUDES) $(SRC)>fluff
70
71depend:
72 $(MAKEDEPEND) $(INCLUDES) $(LIBSRC)
73
74dclean:
75 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
76 mv -f Makefile.new $(MAKEFILE)
77
78clean:
79 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
80
81errors:
82 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
83 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
84
85# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/des/FILES b/src/lib/libssl/src/crypto/des/FILES
new file mode 100644
index 0000000000..4c7ea2de7a
--- /dev/null
+++ b/src/lib/libssl/src/crypto/des/FILES
@@ -0,0 +1,96 @@
1/* General stuff */
2COPYRIGHT - Copyright info.
3MODES.DES - A description of the features of the different modes of DES.
4FILES - This file.
5INSTALL - How to make things compile.
6Imakefile - For use with kerberos.
7README - What this package is.
8VERSION - Which version this is and what was changed.
9KERBEROS - Kerberos version 4 notes.
10Makefile.PL - An old makefile to build with perl5, not current.
11Makefile.ssl - The SSLeay makefile
12Makefile.uni - The normal unix makefile.
13GNUmakefile - The makefile for use with glibc.
14makefile.bc - A Borland C makefile
15times - Some outputs from 'speed' on some machines.
16vms.com - For use when compiling under VMS
17
18/* My SunOS des(1) replacement */
19des.c - des(1) source code.
20des.man - des(1) manual.
21
22/* Testing and timing programs. */
23destest.c - Source for libdes.a test program.
24speed.c - Source for libdes.a timing program.
25rpw.c - Source for libdes.a testing password reading routines.
26
27/* libdes.a source code */
28des_crypt.man - libdes.a manual page.
29des.h - Public libdes.a header file.
30ecb_enc.c - des_ecb_encrypt() source, this contains the basic DES code.
31ecb3_enc.c - des_ecb3_encrypt() source.
32cbc_ckm.c - des_cbc_cksum() source.
33cbc_enc.c - des_cbc_encrypt() source.
34ncbc_enc.c - des_cbc_encrypt() that is 'normal' in that it copies
35 the new iv values back in the passed iv vector.
36ede_enc.c - des_ede3_cbc_encrypt() cbc mode des using triple DES.
37cbc3_enc.c - des_3cbc_encrypt() source, don't use this function.
38cfb_enc.c - des_cfb_encrypt() source.
39cfb64enc.c - des_cfb64_encrypt() cfb in 64 bit mode but setup to be
40 used as a stream cipher.
41cfb64ede.c - des_ede3_cfb64_encrypt() cfb in 64 bit mode but setup to be
42 used as a stream cipher and using triple DES.
43ofb_enc.c - des_cfb_encrypt() source.
44ofb64_enc.c - des_ofb_encrypt() ofb in 64 bit mode but setup to be
45 used as a stream cipher.
46ofb64ede.c - des_ede3_ofb64_encrypt() ofb in 64 bit mode but setup to be
47 used as a stream cipher and using triple DES.
48enc_read.c - des_enc_read() source.
49enc_writ.c - des_enc_write() source.
50pcbc_enc.c - des_pcbc_encrypt() source.
51qud_cksm.c - quad_cksum() source.
52rand_key.c - des_random_key() source.
53read_pwd.c - Source for des_read_password() plus related functions.
54set_key.c - Source for des_set_key().
55str2key.c - Covert a string of any length into a key.
56fcrypt.c - A small, fast version of crypt(3).
57des_locl.h - Internal libdes.a header file.
58podd.h - Odd parity tables - used in des_set_key().
59sk.h - Lookup tables used in des_set_key().
60spr.h - What is left of the S tables - used in ecb_encrypt().
61des_ver.h - header file for the external definition of the
62 version string.
63des.doc - SSLeay documentation for the library.
64
65/* The perl scripts - you can ignore these files they are only
66 * included for the curious */
67des.pl - des in perl anyone? des_set_key and des_ecb_encrypt
68 both done in a perl library.
69testdes.pl - Testing program for des.pl
70doIP - Perl script used to develop IP xor/shift code.
71doPC1 - Perl script used to develop PC1 xor/shift code.
72doPC2 - Generates sk.h.
73PC1 - Output of doPC1 should be the same as output from PC1.
74PC2 - used in development of doPC2.
75shifts.pl - Perl library used by my perl scripts.
76
77/* I started making a perl5 dynamic library for libdes
78 * but did not fully finish, these files are part of that effort. */
79DES.pm
80DES.pod
81DES.xs
82t
83typemap
84
85/* The following are for use with sun RPC implementaions. */
86rpc_des.h
87rpc_enc.c
88
89/* The following are contibuted by Mark Murray <mark@grondar.za>. They
90 * are not normally built into libdes due to machine specific routines
91 * contained in them. They are for use in the most recent incarnation of
92 * export kerberos v 4 (eBones). */
93supp.c
94new_rkey.c
95
96
diff --git a/src/lib/libssl/src/crypto/des/Makefile.ssl b/src/lib/libssl/src/crypto/des/Makefile.ssl
new file mode 100644
index 0000000000..78b5189ee3
--- /dev/null
+++ b/src/lib/libssl/src/crypto/des/Makefile.ssl
@@ -0,0 +1,140 @@
1#
2# SSLeay/crypto/des/Makefile
3#
4
5DIR= des
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16DES_ENC= des_enc.o fcrypt_b.o
17# or use
18#DES_ENC= dx86-elf.o yx86-elf.o
19
20CFLAGS= $(INCLUDES) $(CFLAG)
21
22GENERAL=Makefile des.org des_locl.org
23TEST=destest.c
24APPS=
25
26LIB=$(TOP)/libcrypto.a
27LIBSRC= cbc_cksm.c cbc_enc.c cfb64enc.c cfb_enc.c \
28 ecb3_enc.c ecb_enc.c enc_read.c enc_writ.c \
29 fcrypt.c ofb64enc.c ofb_enc.c pcbc_enc.c \
30 qud_cksm.c rand_key.c read_pwd.c rpc_enc.c set_key.c \
31 des_enc.c fcrypt_b.c read2pwd.c \
32 fcrypt.c xcbc_enc.c \
33 str2key.c cfb64ede.c ofb64ede.c supp.c
34
35LIBOBJ= set_key.o ecb_enc.o cbc_enc.o \
36 ecb3_enc.o cfb64enc.o cfb64ede.o cfb_enc.o ofb64ede.o \
37 enc_read.o enc_writ.o ofb64enc.o \
38 ofb_enc.o str2key.o pcbc_enc.o qud_cksm.o rand_key.o \
39 ${DES_ENC} read2pwd.o \
40 fcrypt.o xcbc_enc.o read_pwd.o rpc_enc.o cbc_cksm.o supp.o
41
42SRC= $(LIBSRC)
43
44EXHEADER= des.h
45HEADER= des_locl.h rpc_des.h podd.h sk.h spr.h des_ver.h $(EXHEADER)
46
47ALL= $(GENERAL) $(SRC) $(HEADER)
48
49top:
50 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
51
52all: lib
53
54lib: $(LIBOBJ)
55 $(AR) $(LIB) $(LIBOBJ)
56 sh $(TOP)/util/ranlib.sh $(LIB)
57 @touch lib
58
59# elf
60asm/dx86-elf.o: asm/dx86unix.cpp
61 $(CPP) -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
62
63asm/yx86-elf.o: asm/yx86unix.cpp
64 $(CPP) -DELF asm/yx86unix.cpp | as -o asm/yx86-elf.o
65
66# solaris
67asm/dx86-sol.o: asm/dx86unix.cpp
68 $(CC) -E -DSOL asm/dx86unix.cpp | sed 's/^#.*//' > asm/dx86-sol.s
69 as -o asm/dx86-sol.o asm/dx86-sol.s
70 rm -f asm/dx86-sol.s
71
72asm/yx86-sol.o: asm/yx86unix.cpp
73 $(CC) -E -DSOL asm/yx86unix.cpp | sed 's/^#.*//' > asm/yx86-sol.s
74 as -o asm/yx86-sol.o asm/yx86-sol.s
75 rm -f asm/yx86-sol.s
76
77# a.out
78asm/dx86-out.o: asm/dx86unix.cpp
79 $(CPP) -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
80
81asm/yx86-out.o: asm/yx86unix.cpp
82 $(CPP) -DOUT asm/yx86unix.cpp | as -o asm/yx86-out.o
83
84# bsdi
85asm/dx86bsdi.o: asm/dx86unix.cpp
86 $(CPP) -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
87
88asm/yx86bsdi.o: asm/yx86unix.cpp
89 $(CPP) -DBSDI asm/yx86unix.cpp | as -o asm/yx86bsdi.o
90
91asm/dx86unix.cpp:
92 (cd asm; perl des-586.pl cpp >dx86unix.cpp)
93
94asm/yx86unix.cpp:
95 (cd asm; perl crypt586.pl cpp >yx86unix.cpp)
96
97files:
98 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
99
100links:
101 /bin/rm -f Makefile
102 $(TOP)/util/point.sh Makefile.ssl Makefile
103 /bin/rm -f des.doc
104 /bin/rm -fr asm/perlasm
105 $(TOP)/util/point.sh ../../perlasm asm/perlasm
106 $(TOP)/util/point.sh ../../doc/des.doc des.doc
107 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
108 $(TOP)/util/mklink.sh ../../test $(TEST)
109 $(TOP)/util/mklink.sh ../../apps $(APPS)
110
111install: installs
112
113installs:
114 @for i in $(EXHEADER) ; \
115 do \
116 (cp $$i $(INSTALLTOP)/include/$$i; \
117 chmod 644 $(INSTALLTOP)/include/$$i ); \
118 done;
119
120tags:
121 ctags $(SRC)
122
123tests:
124
125lint:
126 lint -DLINT $(INCLUDES) $(SRC)>fluff
127
128depend:
129 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
130
131dclean:
132 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
133 mv -f Makefile.new $(MAKEFILE)
134
135clean:
136 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
137
138errors:
139
140# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/dh/Makefile.ssl b/src/lib/libssl/src/crypto/dh/Makefile.ssl
new file mode 100644
index 0000000000..dfa7e4525d
--- /dev/null
+++ b/src/lib/libssl/src/crypto/dh/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/dh/Makefile
3#
4
5DIR= dh
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=dh
19ERRC=dh_err
20GENERAL=Makefile
21TEST= dhtest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= dh_gen.c dh_key.c dh_lib.c dh_check.c $(ERRC).c
26LIBOBJ= dh_gen.o dh_key.o dh_lib.o dh_check.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= dh.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile ;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/dsa/Makefile.ssl b/src/lib/libssl/src/crypto/dsa/Makefile.ssl
new file mode 100644
index 0000000000..2cc4ddb39e
--- /dev/null
+++ b/src/lib/libssl/src/crypto/dsa/Makefile.ssl
@@ -0,0 +1,84 @@
1#
2# SSLeay/crypto/dsa/Makefile
3#
4
5DIR= dsa
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=dsa
19ERRC=dsa_err
20GENERAL=Makefile
21TEST=dsatest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= dsa_gen.c dsa_key.c dsa_lib.c dsa_vrf.c dsa_sign.c $(ERRC).c
26LIBOBJ= dsa_gen.o dsa_key.o dsa_lib.o dsa_vrf.o dsa_sign.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= dsa.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45files:
46 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
47
48links:
49 /bin/rm -f Makefile
50 $(TOP)/util/point.sh Makefile.ssl Makefile ;
51 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
52 $(TOP)/util/mklink.sh ../../test $(TEST)
53 $(TOP)/util/mklink.sh ../../apps $(APPS)
54
55install:
56 @for i in $(EXHEADER) ; \
57 do \
58 (cp $$i $(INSTALLTOP)/include/$$i; \
59 chmod 644 $(INSTALLTOP)/include/$$i ); \
60 done;
61
62tags:
63 ctags $(SRC)
64
65tests:
66
67lint:
68 lint -DLINT $(INCLUDES) $(SRC)>fluff
69
70depend:
71 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
72
73dclean:
74 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
75 mv -f Makefile.new $(MAKEFILE)
76
77clean:
78 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
79
80errors:
81 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
82 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
83
84# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/err/Makefile.ssl b/src/lib/libssl/src/crypto/err/Makefile.ssl
new file mode 100644
index 0000000000..57c87eb041
--- /dev/null
+++ b/src/lib/libssl/src/crypto/err/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/err/Makefile
3#
4
5DIR= err
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=err.c err_all.c err_prn.c
24LIBOBJ=err.o err_all.o err_prn.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= err.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/evp/Makefile.ssl b/src/lib/libssl/src/crypto/evp/Makefile.ssl
new file mode 100644
index 0000000000..8bf2516458
--- /dev/null
+++ b/src/lib/libssl/src/crypto/evp/Makefile.ssl
@@ -0,0 +1,111 @@
1#
2# SSLeay/crypto/evp/Makefile
3#
4
5DIR= evp
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=evp
19ERRC=evp_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= encode.c digest.c evp_enc.c evp_key.c \
26 e_ecb_d.c e_cbc_d.c e_cfb_d.c e_ofb_d.c \
27 e_ecb_i.c e_cbc_i.c e_cfb_i.c e_ofb_i.c \
28 e_ecb_3d.c e_cbc_3d.c e_rc4.c names.c \
29 e_cfb_3d.c e_ofb_3d.c e_xcbc_d.c \
30 e_ecb_r2.c e_cbc_r2.c e_cfb_r2.c e_ofb_r2.c \
31 e_ecb_bf.c e_cbc_bf.c e_cfb_bf.c e_ofb_bf.c \
32 e_ecb_c.c e_cbc_c.c e_cfb_c.c e_ofb_c.c \
33 e_ecb_r5.c e_cbc_r5.c e_cfb_r5.c e_ofb_r5.c \
34 m_null.c m_md2.c m_md5.c m_sha.c m_sha1.c m_dss.c m_dss1.c m_mdc2.c \
35 m_ripemd.c \
36 p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
37 bio_md.c bio_b64.c bio_enc.c $(ERRC).c e_null.c \
38 c_all.c evp_lib.c
39
40LIBOBJ= encode.o digest.o evp_enc.o evp_key.o \
41 e_ecb_d.o e_cbc_d.o e_cfb_d.o e_ofb_d.o \
42 e_ecb_i.o e_cbc_i.o e_cfb_i.o e_ofb_i.o \
43 e_ecb_3d.o e_cbc_3d.o e_rc4.o names.o \
44 e_cfb_3d.o e_ofb_3d.o e_xcbc_d.o \
45 e_ecb_r2.o e_cbc_r2.o e_cfb_r2.o e_ofb_r2.o \
46 e_ecb_bf.o e_cbc_bf.o e_cfb_bf.o e_ofb_bf.o \
47 e_ecb_c.o e_cbc_c.o e_cfb_c.o e_ofb_c.o \
48 e_ecb_r5.o e_cbc_r5.o e_cfb_r5.o e_ofb_r5.o \
49 m_null.o m_md2.o m_md5.o m_sha.o m_sha1.o m_dss.o m_dss1.o m_mdc2.o \
50 m_ripemd.o \
51 p_open.o p_seal.o p_sign.o p_verify.o p_lib.o p_enc.o p_dec.o \
52 bio_md.o bio_b64.o bio_enc.o $(ERRC).o e_null.o \
53 c_all.o evp_lib.o
54
55SRC= $(LIBSRC)
56
57EXHEADER= evp.h
58HEADER= $(EXHEADER)
59
60ALL= $(GENERAL) $(SRC) $(HEADER)
61
62top:
63 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
64
65all: lib
66
67lib: $(LIBOBJ)
68 $(AR) $(LIB) $(LIBOBJ)
69 sh $(TOP)/util/ranlib.sh $(LIB)
70 @touch lib
71
72files:
73 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
74
75links:
76 /bin/rm -f Makefile
77 $(TOP)/util/point.sh Makefile.ssl Makefile ;
78 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
79 $(TOP)/util/mklink.sh ../../test $(TEST)
80 $(TOP)/util/mklink.sh ../../apps $(APPS)
81
82install:
83 @for i in $(EXHEADER) ; \
84 do \
85 (cp $$i $(INSTALLTOP)/include/$$i; \
86 chmod 644 $(INSTALLTOP)/include/$$i ); \
87 done;
88
89tags:
90 ctags $(SRC)
91
92tests:
93
94lint:
95 lint -DLINT $(INCLUDES) $(SRC)>fluff
96
97depend:
98 $(MAKEDEPEND) $(INCLUDES) $(LIBSRC)
99
100dclean:
101 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
102 mv -f Makefile.new $(MAKEFILE)
103
104clean:
105 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
106
107errors:
108 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
109 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
110
111# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/hmac/Makefile.ssl b/src/lib/libssl/src/crypto/hmac/Makefile.ssl
new file mode 100644
index 0000000000..7a042b7261
--- /dev/null
+++ b/src/lib/libssl/src/crypto/hmac/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/md/Makefile
3#
4
5DIR= hmac
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=hmactest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=hmac.c
24LIBOBJ=hmac.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= hmac.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/idea/Makefile.ssl b/src/lib/libssl/src/crypto/idea/Makefile.ssl
new file mode 100644
index 0000000000..41b42ce03b
--- /dev/null
+++ b/src/lib/libssl/src/crypto/idea/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/idea/Makefile
3#
4
5DIR= idea
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=ideatest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=i_cbc.c i_cfb64.c i_ofb64.c i_ecb.c i_skey.c
24LIBOBJ=i_cbc.o i_cfb64.o i_ofb64.o i_ecb.o i_skey.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= idea.h
29HEADER= idea_lcl.h $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/lhash/Makefile.ssl b/src/lib/libssl/src/crypto/lhash/Makefile.ssl
new file mode 100644
index 0000000000..cb08547b4f
--- /dev/null
+++ b/src/lib/libssl/src/crypto/lhash/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/lhash/Makefile
3#
4
5DIR= lhash
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=lhash.c lh_stats.c
24LIBOBJ=lhash.o lh_stats.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= lhash.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/md2/Makefile.ssl b/src/lib/libssl/src/crypto/md2/Makefile.ssl
new file mode 100644
index 0000000000..d8e7200c83
--- /dev/null
+++ b/src/lib/libssl/src/crypto/md2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/md/Makefile
3#
4
5DIR= md
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=md2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=md2_dgst.c md5_one.c
24LIBOBJ=md2_dgst.o md2_one.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= md2.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/md5/Makefile.ssl b/src/lib/libssl/src/crypto/md5/Makefile.ssl
new file mode 100644
index 0000000000..47e1ce05ca
--- /dev/null
+++ b/src/lib/libssl/src/crypto/md5/Makefile.ssl
@@ -0,0 +1,104 @@
1#
2# SSLeay/crypto/md5/Makefile
3#
4
5DIR= md5
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17MD5_ASM_OBJ=
18
19CFLAGS= $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22TEST=md5test.c
23APPS=md5.c
24
25LIB=$(TOP)/libcrypto.a
26LIBSRC=md5_dgst.c md5_one.c
27LIBOBJ=md5_dgst.o md5_one.o $(MD5_ASM_OBJ)
28
29SRC= $(LIBSRC)
30
31EXHEADER= md5.h
32HEADER= md5_locl.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46# elf
47asm/mx86-elf.o: asm/mx86unix.cpp
48 $(CPP) -DELF asm/mx86unix.cpp | as -o asm/mx86-elf.o
49
50# solaris
51asm/mx86-sol.o: asm/mx86unix.cpp
52 $(CC) -E -DSOL asm/mx86unix.cpp | sed 's/^#.*//' > asm/mx86-sol.s
53 as -o asm/mx86-sol.o asm/mx86-sol.s
54 rm -f asm/mx86-sol.s
55
56# a.out
57asm/mx86-out.o: asm/mx86unix.cpp
58 $(CPP) -DOUT asm/mx86unix.cpp | as -o asm/mx86-out.o
59
60# bsdi
61asm/mx86bsdi.o: asm/mx86unix.cpp
62 $(CPP) -DBSDI asm/mx86unix.cpp | as -o asm/mx86bsdi.o
63
64asm/mx86unix.cpp:
65 (cd asm; perl md5-586.pl cpp >mx86unix.cpp)
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69
70links:
71 /bin/rm -f Makefile
72 $(TOP)/util/point.sh Makefile.ssl Makefile ;
73 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
74 $(TOP)/util/mklink.sh ../../test $(TEST)
75 $(TOP)/util/mklink.sh ../../apps $(APPS)
76
77install:
78 @for i in $(EXHEADER) ; \
79 do \
80 (cp $$i $(INSTALLTOP)/include/$$i; \
81 chmod 644 $(INSTALLTOP)/include/$$i ); \
82 done;
83
84tags:
85 ctags $(SRC)
86
87tests:
88
89lint:
90 lint -DLINT $(INCLUDES) $(SRC)>fluff
91
92depend:
93 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
94
95dclean:
96 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
97 mv -f Makefile.new $(MAKEFILE)
98
99clean:
100 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
101
102errors:
103
104# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/mdc2/Makefile.ssl b/src/lib/libssl/src/crypto/mdc2/Makefile.ssl
new file mode 100644
index 0000000000..495a2789a0
--- /dev/null
+++ b/src/lib/libssl/src/crypto/mdc2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/mdc2/Makefile
3#
4
5DIR= mdc2
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST= mdc2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=mdc2dgst.c mdc2_one.c
24LIBOBJ=mdc2dgst.o mdc2_one.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= mdc2.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/objects/Makefile.ssl b/src/lib/libssl/src/crypto/objects/Makefile.ssl
new file mode 100644
index 0000000000..320523cea1
--- /dev/null
+++ b/src/lib/libssl/src/crypto/objects/Makefile.ssl
@@ -0,0 +1,87 @@
1#
2# SSLeay/crypto/objects/Makefile
3#
4
5DIR= objects
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=objects
19ERRC=obj_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= obj_dat.c obj_lib.c $(ERRC).c
26LIBOBJ= obj_dat.o obj_lib.o $(ERRC).o
27
28SRC= $(LIBSRC)
29
30EXHEADER= objects.h
31HEADER= $(EXHEADER) obj_dat.h
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: obj_dat.h lib
39
40obj_dat.h: objects.h obj_dat.pl
41 perl ./obj_dat.pl < objects.h > obj_dat.h
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48files:
49 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
50
51links:
52 /bin/rm -f Makefile
53 $(TOP)/util/point.sh Makefile.ssl Makefile ;
54 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
55 $(TOP)/util/mklink.sh ../../test $(TEST)
56 $(TOP)/util/mklink.sh ../../apps $(APPS)
57
58install:
59 @for i in $(EXHEADER) ; \
60 do \
61 (cp $$i $(INSTALLTOP)/include/$$i; \
62 chmod 644 $(INSTALLTOP)/include/$$i ); \
63 done;
64
65tags:
66 ctags $(SRC)
67
68tests:
69
70lint:
71 lint -DLINT $(INCLUDES) $(SRC)>fluff
72
73depend:
74 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
75
76dclean:
77 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
78 mv -f Makefile.new $(MAKEFILE)
79
80clean:
81 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
82
83errors:
84 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
85 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
86
87# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/pem/Makefile.ssl b/src/lib/libssl/src/crypto/pem/Makefile.ssl
new file mode 100644
index 0000000000..fc04a88fd9
--- /dev/null
+++ b/src/lib/libssl/src/crypto/pem/Makefile.ssl
@@ -0,0 +1,96 @@
1#
2# SSLeay/crypto/pem/Makefile
3#
4
5DIR= pem
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=pem
19ERRC=pem_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24CTX_SIZE= ctx_size
25
26LIB=$(TOP)/libcrypto.a
27LIBSRC= pem_sign.c pem_seal.c pem_info.c pem_lib.c pem_all.c $(ERRC).c
28
29LIBOBJ= pem_sign.o pem_seal.o pem_info.o pem_lib.o pem_all.o $(ERRC).o
30
31SRC= $(LIBSRC)
32
33EXHEADER= pem.h
34HEADER= $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: pem.h lib
42
43pem.h: $(CTX_SIZE)
44 ./$(CTX_SIZE) <pem.org >pem.new
45 if [ -f pem.h ]; then mv -f pem.h pem.old; fi
46 mv -f pem.new pem.h
47
48$(CTX_SIZE): $(CTX_SIZE).o
49 $(CC) $(CFLAGS) -o $(CTX_SIZE) $(CTX_SIZE).o
50
51lib: $(LIBOBJ)
52 $(AR) $(LIB) $(LIBOBJ)
53 sh $(TOP)/util/ranlib.sh $(LIB)
54 @touch lib
55
56files:
57 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
58
59links:
60 /bin/rm -f Makefile
61 $(TOP)/util/point.sh Makefile.ssl Makefile ;
62 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
63 $(TOP)/util/mklink.sh ../../test $(TEST)
64 $(TOP)/util/mklink.sh ../../apps $(APPS)
65
66install:
67 @for i in $(EXHEADER) ; \
68 do \
69 (cp $$i $(INSTALLTOP)/include/$$i; \
70 chmod 644 $(INSTALLTOP)/include/$$i ); \
71 done;
72
73tags:
74 ctags $(SRC)
75
76tests:
77
78lint:
79 lint -DLINT $(INCLUDES) $(SRC)>fluff
80
81depend:
82 $(MAKEDEPEND) $(INCLUDES) $(CTX_SIZE).c $(LIBSRC)
83
84dclean:
85 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
86 mv -f Makefile.new $(MAKEFILE)
87
88clean:
89 /bin/rm -f $(CTX_SIZE) *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
90
91errors:
92 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # SPECIAL CASE .org
93 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
94 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
95
96# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/pkcs7/Makefile.ssl b/src/lib/libssl/src/crypto/pkcs7/Makefile.ssl
new file mode 100644
index 0000000000..a88359b320
--- /dev/null
+++ b/src/lib/libssl/src/crypto/pkcs7/Makefile.ssl
@@ -0,0 +1,86 @@
1#
2# SSLeay/crypto/asn1/Makefile
3#
4
5DIR= pkcs7
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=pkcs7
19ERRC=pkcs7err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= pk7_lib.c pkcs7err.c pk7_doit.c
26LIBOBJ= pk7_lib.o pkcs7err.o pk7_doit.o
27
28SRC= $(LIBSRC)
29
30EXHEADER= pkcs7.h
31HEADER= $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38test:
39
40all: lib
41
42lib: $(LIBOBJ)
43 $(AR) $(LIB) $(LIBOBJ)
44 sh $(TOP)/util/ranlib.sh $(LIB)
45 @touch lib
46
47files:
48 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
49
50links:
51 /bin/rm -f Makefile
52 $(TOP)/util/point.sh Makefile.ssl Makefile ;
53 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
54 $(TOP)/util/mklink.sh ../../test $(TEST)
55 $(TOP)/util/mklink.sh ../../apps $(APPS)
56
57install:
58 @for i in $(EXHEADER) ; \
59 do \
60 (cp $$i $(INSTALLTOP)/include/$$i; \
61 chmod 644 $(INSTALLTOP)/include/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
74
75dclean:
76 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
77 mv -f Makefile.new $(MAKEFILE)
78
79clean:
80 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
81
82errors:
83 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
84 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
85
86# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rand/Makefile.ssl b/src/lib/libssl/src/crypto/rand/Makefile.ssl
new file mode 100644
index 0000000000..d04f0a9b43
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rand/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/rand/Makefile
3#
4
5DIR= rand
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST= randtest.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=md_rand.c randfile.c
24LIBOBJ=md_rand.o randfile.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= rand.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rc2/Makefile.ssl b/src/lib/libssl/src/crypto/rc2/Makefile.ssl
new file mode 100644
index 0000000000..c5138f13e2
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rc2/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/rc2/Makefile
3#
4
5DIR= rc2
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=rc2test.c
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=rc2_ecb.c rc2_skey.c rc2_cbc.c rc2cfb64.c rc2ofb64.c
24LIBOBJ=rc2_ecb.o rc2_skey.o rc2_cbc.o rc2cfb64.o rc2ofb64.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= rc2.h
29HEADER= rc2_locl.h $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rc4/Makefile.ssl b/src/lib/libssl/src/crypto/rc4/Makefile.ssl
new file mode 100644
index 0000000000..19c1e980f3
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rc4/Makefile.ssl
@@ -0,0 +1,108 @@
1#
2# SSLeay/crypto/rc4/Makefile
3#
4
5DIR= rc4
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16RC4_ENC=rc4_enc.o
17# or use
18#RC4_ENC=asm/rx86-elf.o
19#RC4_ENC=asm/rx86-out.o
20#RC4_ENC=asm/rx86-sol.o
21#RC4_ENC=asm/rx86bdsi.o
22
23CFLAGS= $(INCLUDES) $(CFLAG)
24
25GENERAL=Makefile
26TEST=rc4test.c
27APPS=
28
29LIB=$(TOP)/libcrypto.a
30LIBSRC=rc4_skey.c rc4_enc.c
31LIBOBJ=rc4_skey.o $(RC4_ENC)
32
33SRC= $(LIBSRC)
34
35EXHEADER= rc4.h
36HEADER= $(EXHEADER) rc4_locl.h
37
38ALL= $(GENERAL) $(SRC) $(HEADER)
39
40top:
41 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
42
43all: lib
44
45lib: $(LIBOBJ)
46 $(AR) $(LIB) $(LIBOBJ)
47 sh $(TOP)/util/ranlib.sh $(LIB)
48 @touch lib
49
50# elf
51asm/rx86-elf.o: asm/rx86unix.cpp
52 $(CPP) -DELF asm/rx86unix.cpp | as -o asm/rx86-elf.o
53
54# solaris
55asm/rx86-sol.o: asm/rx86unix.cpp
56 $(CC) -E -DSOL asm/rx86unix.cpp | sed 's/^#.*//' > asm/rx86-sol.s
57 as -o asm/rx86-sol.o asm/rx86-sol.s
58 rm -f asm/rx86-sol.s
59
60# a.out
61asm/rx86-out.o: asm/rx86unix.cpp
62 $(CPP) -DOUT asm/rx86unix.cpp | as -o asm/rx86-out.o
63
64# bsdi
65asm/rx86bsdi.o: asm/rx86unix.cpp
66 $(CPP) -DBSDI asm/rx86unix.cpp | as -o asm/rx86bsdi.o
67
68asm/rx86unix.cpp:
69 (cd asm; perl rc4-586.pl cpp >rx86unix.cpp)
70
71files:
72 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
73
74links:
75 /bin/rm -f Makefile
76 $(TOP)/util/point.sh Makefile.ssl Makefile ;
77 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
78 $(TOP)/util/mklink.sh ../../test $(TEST)
79 $(TOP)/util/mklink.sh ../../apps $(APPS)
80
81install:
82 @for i in $(EXHEADER) ; \
83 do \
84 (cp $$i $(INSTALLTOP)/include/$$i; \
85 chmod 644 $(INSTALLTOP)/include/$$i ); \
86 done;
87
88tags:
89 ctags $(SRC)
90
91tests:
92
93lint:
94 lint -DLINT $(INCLUDES) $(SRC)>fluff
95
96depend:
97 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
98
99dclean:
100 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
101 mv -f Makefile.new $(MAKEFILE)
102
103clean:
104 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff asm/*.o
105
106errors:
107
108# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rc5/Makefile.ssl b/src/lib/libssl/src/crypto/rc5/Makefile.ssl
new file mode 100644
index 0000000000..5e98ee2348
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rc5/Makefile.ssl
@@ -0,0 +1,107 @@
1#
2# SSLeay/crypto/rc5/Makefile
3#
4
5DIR= rc5
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17RC5_ENC= rc5_enc.o
18# or use
19#DES_ENC= r586-elf.o
20
21CFLAGS= $(INCLUDES) $(CFLAG)
22
23GENERAL=Makefile
24TEST=rc5test.c
25APPS=
26
27LIB=$(TOP)/libcrypto.a
28LIBSRC=rc5_skey.c rc5_ecb.c rc5_enc.c rc5cfb64.c rc5ofb64.c
29LIBOBJ=rc5_skey.o rc5_ecb.o $(RC5_ENC) rc5cfb64.o rc5ofb64.o
30
31SRC= $(LIBSRC)
32
33EXHEADER= rc5.h
34HEADER= rc5_locl.h $(EXHEADER)
35
36ALL= $(GENERAL) $(SRC) $(HEADER)
37
38top:
39 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
40
41all: lib
42
43lib: $(LIBOBJ)
44 $(AR) $(LIB) $(LIBOBJ)
45 sh $(TOP)/util/ranlib.sh $(LIB)
46 @touch lib
47
48# elf
49asm/r586-elf.o: asm/r586unix.cpp
50 $(CPP) -DELF asm/r586unix.cpp | as -o asm/r586-elf.o
51
52# solaris
53asm/r586-sol.o: asm/r586unix.cpp
54 $(CC) -E -DSOL asm/r586unix.cpp | sed 's/^#.*//' > asm/r586-sol.s
55 as -o asm/r586-sol.o asm/r586-sol.s
56 rm -f asm/r586-sol.s
57
58# a.out
59asm/r586-out.o: asm/r586unix.cpp
60 $(CPP) -DOUT asm/r586unix.cpp | as -o asm/r586-out.o
61
62# bsdi
63asm/r586bsdi.o: asm/r586unix.cpp
64 $(CPP) -DBSDI asm/r586unix.cpp | as -o asm/r586bsdi.o
65
66asm/r586unix.cpp:
67 (cd asm; perl rc5-586.pl cpp >r586unix.cpp)
68
69files:
70 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
71
72links:
73 /bin/rm -f Makefile
74 $(TOP)/util/point.sh Makefile.ssl Makefile ;
75 $(TOP)/util/point.sh ../../doc/rc5.doc rc5.doc ;
76 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
77 $(TOP)/util/mklink.sh ../../test $(TEST)
78 $(TOP)/util/mklink.sh ../../apps $(APPS)
79
80install:
81 @for i in $(EXHEADER) ; \
82 do \
83 (cp $$i $(INSTALLTOP)/include/$$i; \
84 chmod 644 $(INSTALLTOP)/include/$$i ); \
85 done;
86
87tags:
88 ctags $(SRC)
89
90tests:
91
92lint:
93 lint -DLINT $(INCLUDES) $(SRC)>fluff
94
95depend:
96 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
97
98dclean:
99 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
100 mv -f Makefile.new $(MAKEFILE)
101
102clean:
103 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
104
105errors:
106
107# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl b/src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl
new file mode 100644
index 0000000000..172bd9ee1b
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl
@@ -0,0 +1,109 @@
1#!/usr/bin/perl
2
3push(@INC,"perlasm","../../perlasm");
4require "x86asm.pl";
5require "cbc.pl";
6
7&asm_init($ARGV[0],"rc5-586.pl");
8
9$RC5_MAX_ROUNDS=16;
10$RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4;
11$A="edi";
12$B="esi";
13$S="ebp";
14$tmp1="eax";
15$r="ebx";
16$tmpc="ecx";
17$tmp4="edx";
18
19&RC5_32_encrypt("RC5_32_encrypt",1);
20&RC5_32_encrypt("RC5_32_decrypt",0);
21&cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1);
22&asm_finish();
23
24sub RC5_32_encrypt
25 {
26 local($name,$enc)=@_;
27
28 &function_begin_B($name,"");
29
30 &comment("");
31
32 &push("ebp");
33 &push("esi");
34 &push("edi");
35 &mov($tmp4,&wparam(0));
36 &mov($S,&wparam(1));
37
38 &comment("Load the 2 words");
39 &mov($A,&DWP(0,$tmp4,"",0));
40 &mov($B,&DWP(4,$tmp4,"",0));
41
42 &push($r);
43 &mov($r, &DWP(0,$S,"",0));
44
45 # encrypting part
46
47 if ($enc)
48 {
49 &add($A, &DWP(4+0,$S,"",0));
50 &add($B, &DWP(4+4,$S,"",0));
51
52 for ($i=0; $i<$RC5_MAX_ROUNDS; $i++)
53 {
54 &xor($A, $B);
55 &mov($tmp1, &DWP(12+$i*8,$S,"",0));
56 &mov($tmpc, $B);
57 &rotl($A, &LB("ecx"));
58 &add($A, $tmp1);
59
60 &xor($B, $A);
61 &mov($tmp1, &DWP(16+$i*8,$S,"",0));
62 &mov($tmpc, $A);
63 &rotl($B, &LB("ecx"));
64 &add($B, $tmp1);
65 if (($i == 7) || ($i == 11))
66 {
67 &cmp($r, $i+1);
68 &je(&label("rc5_exit"));
69 }
70 }
71 }
72 else
73 {
74 &cmp($r, 12);
75 &je(&label("rc5_dec_12"));
76 &cmp($r, 8);
77 &je(&label("rc5_dec_8"));
78 for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--)
79 {
80 &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8);
81 &mov($tmp1, &DWP($i*8+8,$S,"",0));
82 &sub($B, $tmp1);
83 &mov($tmpc, $A);
84 &rotr($B, &LB("ecx"));
85 &xor($B, $A);
86
87 &mov($tmp1, &DWP($i*8+4,$S,"",0));
88 &sub($A, $tmp1);
89 &mov($tmpc, $B);
90 &rotr($A, &LB("ecx"));
91 &xor($A, $B);
92 }
93 &sub($B, &DWP(4+4,$S,"",0));
94 &sub($A, &DWP(4+0,$S,"",0));
95 }
96
97 &set_label("rc5_exit");
98 &mov(&DWP(0,$tmp4,"",0),$A);
99 &mov(&DWP(4,$tmp4,"",0),$B);
100
101 &pop("ebx");
102 &pop("edi");
103 &pop("esi");
104 &pop("ebp");
105 &ret();
106 &function_end_B($name);
107 }
108
109
diff --git a/src/lib/libssl/src/crypto/ripemd/Makefile.ssl b/src/lib/libssl/src/crypto/ripemd/Makefile.ssl
new file mode 100644
index 0000000000..67d47ceb2c
--- /dev/null
+++ b/src/lib/libssl/src/crypto/ripemd/Makefile.ssl
@@ -0,0 +1,104 @@
1#
2# SSLeay/crypto/ripemd/Makefile
3#
4
5DIR= ripemd
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11INSTALLTOP=/usr/local/ssl
12MAKE= make -f Makefile.ssl
13MAKEDEPEND= makedepend -f Makefile.ssl
14MAKEFILE= Makefile.ssl
15AR= ar r
16
17RIP_ASM_OBJ=
18
19CFLAGS= $(INCLUDES) $(CFLAG)
20
21GENERAL=Makefile
22TEST=rmdtest.c
23APPS=rmd160.c
24
25LIB=$(TOP)/libcrypto.a
26LIBSRC=rmd_dgst.c rmd_one.c
27LIBOBJ=rmd_dgst.o rmd_one.o $(RMD160_ASM_OBJ)
28
29SRC= $(LIBSRC)
30
31EXHEADER= ripemd.h
32HEADER= rmd_locl.h rmdconst.h $(EXHEADER)
33
34ALL= $(GENERAL) $(SRC) $(HEADER)
35
36top:
37 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
38
39all: lib
40
41lib: $(LIBOBJ)
42 $(AR) $(LIB) $(LIBOBJ)
43 sh $(TOP)/util/ranlib.sh $(LIB)
44 @touch lib
45
46# elf
47asm/rm86-elf.o: asm/rm86unix.cpp
48 $(CPP) -DELF asm/rm86unix.cpp | as -o asm/rm86-elf.o
49
50# solaris
51asm/rm86-sol.o: asm/rm86unix.cpp
52 $(CC) -E -DSOL asm/rm86unix.cpp | sed 's/^#.*//' > asm/rm86-sol.s
53 as -o asm/rm86-sol.o asm/rm86-sol.s
54 rm -f asm/rm86-sol.s
55
56# a.out
57asm/rm86-out.o: asm/rm86unix.cpp
58 $(CPP) -DOUT asm/rm86unix.cpp | as -o asm/rm86-out.o
59
60# bsdi
61asm/rm86bsdi.o: asm/rm86unix.cpp
62 $(CPP) -DBSDI asm/rm86unix.cpp | as -o asm/rm86bsdi.o
63
64asm/rm86unix.cpp:
65 (cd asm; perl rmd-586.pl cpp >rm86unix.cpp)
66
67files:
68 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
69
70links:
71 /bin/rm -f Makefile
72 $(TOP)/util/point.sh Makefile.ssl Makefile ;
73 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
74 $(TOP)/util/mklink.sh ../../test $(TEST)
75 $(TOP)/util/mklink.sh ../../apps $(APPS)
76
77install:
78 @for i in $(EXHEADER) ; \
79 do \
80 (cp $$i $(INSTALLTOP)/include/$$i; \
81 chmod 644 $(INSTALLTOP)/include/$$i ); \
82 done;
83
84tags:
85 ctags $(SRC)
86
87tests:
88
89lint:
90 lint -DLINT $(INCLUDES) $(SRC)>fluff
91
92depend:
93 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
94
95dclean:
96 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
97 mv -f Makefile.new $(MAKEFILE)
98
99clean:
100 /bin/rm -f *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
101
102errors:
103
104# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/rsa/Makefile.ssl b/src/lib/libssl/src/crypto/rsa/Makefile.ssl
new file mode 100644
index 0000000000..d52f2e609e
--- /dev/null
+++ b/src/lib/libssl/src/crypto/rsa/Makefile.ssl
@@ -0,0 +1,86 @@
1#
2# SSLeay/crypto/rsa/Makefile
3#
4
5DIR= rsa
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=rsa
19ERRC=rsa_err
20GENERAL=Makefile
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c $(ERRC).c \
26 rsa_pk1.c rsa_ssl.c rsa_none.c
27LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o $(ERRC).o \
28 rsa_pk1.o rsa_ssl.o rsa_none.o
29
30SRC= $(LIBSRC)
31
32EXHEADER= rsa.h
33HEADER= $(EXHEADER)
34
35ALL= $(GENERAL) $(SRC) $(HEADER)
36
37top:
38 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
39
40all: lib
41
42lib: $(LIBOBJ)
43 $(AR) $(LIB) $(LIBOBJ)
44 sh $(TOP)/util/ranlib.sh $(LIB)
45 @touch lib
46
47files:
48 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
49
50links:
51 /bin/rm -f Makefile
52 $(TOP)/util/point.sh Makefile.ssl Makefile ;
53 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
54 $(TOP)/util/mklink.sh ../../test $(TEST)
55 $(TOP)/util/mklink.sh ../../apps $(APPS)
56
57install:
58 @for i in $(EXHEADER) ; \
59 do \
60 (cp $$i $(INSTALLTOP)/include/$$i; \
61 chmod 644 $(INSTALLTOP)/include/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
74
75dclean:
76 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
77 mv -f Makefile.new $(MAKEFILE)
78
79clean:
80 /bin/rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
81
82errors:
83 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
84 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
85
86# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/sha/Makefile.ssl b/src/lib/libssl/src/crypto/sha/Makefile.ssl
new file mode 100644
index 0000000000..eeb545d140
--- /dev/null
+++ b/src/lib/libssl/src/crypto/sha/Makefile.ssl
@@ -0,0 +1,103 @@
1#
2# SSLeay/crypto/sha/Makefile
3#
4
5DIR= sha
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16SHA1_ASM_OBJ=
17
18CFLAGS= $(INCLUDES) $(CFLAG)
19
20GENERAL=Makefile
21TEST=shatest.c sha1test.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC=sha_dgst.c sha1dgst.c sha_one.c sha1_one.c
26LIBOBJ=sha_dgst.o sha1dgst.o sha_one.o sha1_one.o $(SHA1_ASM_OBJ)
27
28SRC= $(LIBSRC)
29
30EXHEADER= sha.h
31HEADER= sha_locl.h $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 sh $(TOP)/util/ranlib.sh $(LIB)
43 @touch lib
44
45# elf
46asm/sx86-elf.o: asm/sx86unix.cpp
47 $(CPP) -DELF asm/sx86unix.cpp | as -o asm/sx86-elf.o
48
49# solaris
50asm/sx86-sol.o: asm/sx86unix.cpp
51 $(CC) -E -DSOL asm/sx86unix.cpp | sed 's/^#.*//' > asm/sx86-sol.s
52 as -o asm/sx86-sol.o asm/sx86-sol.s
53 rm -f asm/sx86-sol.s
54
55# a.out
56asm/sx86-out.o: asm/sx86unix.cpp
57 $(CPP) -DOUT asm/sx86unix.cpp | as -o asm/sx86-out.o
58
59# bsdi
60asm/sx86bsdi.o: asm/sx86unix.cpp
61 $(CPP) -DBSDI asm/sx86unix.cpp | as -o asm/sx86bsdi.o
62
63asm/sx86unix.cpp:
64 (cd asm; perl sha1-586.pl cpp >sx86unix.cpp)
65
66files:
67 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
68
69links:
70 /bin/rm -f Makefile
71 $(TOP)/util/point.sh Makefile.ssl Makefile ;
72 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
73 $(TOP)/util/mklink.sh ../../test $(TEST)
74 $(TOP)/util/mklink.sh ../../apps $(APPS)
75
76install:
77 @for i in $(EXHEADER) ; \
78 do \
79 (cp $$i $(INSTALLTOP)/include/$$i; \
80 chmod 644 $(INSTALLTOP)/include/$$i ); \
81 done;
82
83tags:
84 ctags $(SRC)
85
86tests:
87
88lint:
89 lint -DLINT $(INCLUDES) $(SRC)>fluff
90
91depend:
92 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
93
94dclean:
95 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
96 mv -f Makefile.new $(MAKEFILE)
97
98clean:
99 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff asm/*.o
100
101errors:
102
103# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/stack/Makefile.ssl b/src/lib/libssl/src/crypto/stack/Makefile.ssl
new file mode 100644
index 0000000000..0d232c08cf
--- /dev/null
+++ b/src/lib/libssl/src/crypto/stack/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/stack/Makefile
3#
4
5DIR= stack
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=stack.c
24LIBOBJ=stack.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= stack.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/txt_db/Makefile.ssl b/src/lib/libssl/src/crypto/txt_db/Makefile.ssl
new file mode 100644
index 0000000000..76e511534f
--- /dev/null
+++ b/src/lib/libssl/src/crypto/txt_db/Makefile.ssl
@@ -0,0 +1,80 @@
1#
2# SSLeay/crypto/txt_db/Makefile
3#
4
5DIR= txt_db
6TOP= ../..
7CC= cc
8INCLUDES=
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18GENERAL=Makefile
19TEST=
20APPS=
21
22LIB=$(TOP)/libcrypto.a
23LIBSRC=txt_db.c
24LIBOBJ=txt_db.o
25
26SRC= $(LIBSRC)
27
28EXHEADER= txt_db.h
29HEADER= $(EXHEADER)
30
31ALL= $(GENERAL) $(SRC) $(HEADER)
32
33top:
34 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
35
36all: lib
37
38lib: $(LIBOBJ)
39 $(AR) $(LIB) $(LIBOBJ)
40 sh $(TOP)/util/ranlib.sh $(LIB)
41 @touch lib
42
43files:
44 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
45
46links:
47 /bin/rm -f Makefile
48 $(TOP)/util/point.sh Makefile.ssl Makefile ;
49 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
50 $(TOP)/util/mklink.sh ../../test $(TEST)
51 $(TOP)/util/mklink.sh ../../apps $(APPS)
52
53install:
54 @for i in $(EXHEADER) ; \
55 do \
56 (cp $$i $(INSTALLTOP)/include/$$i; \
57 chmod 644 $(INSTALLTOP)/include/$$i ); \
58 done;
59
60tags:
61 ctags $(SRC)
62
63tests:
64
65lint:
66 lint -DLINT $(INCLUDES) $(SRC)>fluff
67
68depend:
69 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
70
71dclean:
72 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
73 mv -f Makefile.new $(MAKEFILE)
74
75clean:
76 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
77
78errors:
79
80# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/crypto/x509/Makefile.ssl b/src/lib/libssl/src/crypto/x509/Makefile.ssl
new file mode 100644
index 0000000000..1c1ca2ffa0
--- /dev/null
+++ b/src/lib/libssl/src/crypto/x509/Makefile.ssl
@@ -0,0 +1,96 @@
1#
2# SSLeay/crypto/x509/Makefile
3#
4
5DIR= x509
6TOP= ../..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=x509
19ERRC=x509_err
20GENERAL=Makefile README
21TEST=
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC= x509_def.c x509_d2.c x509_r2x.c x509_cmp.c \
26 x509_obj.c x509_req.c x509_vfy.c \
27 x509_set.c x509rset.c $(ERRC).c \
28 x509name.c x509_v3.c x509_ext.c x509pack.c \
29 x509type.c x509_lu.c x_all.c x509_txt.c \
30 by_file.c by_dir.c \
31 v3_net.c v3_x509.c
32LIBOBJ= x509_def.o x509_d2.o x509_r2x.o x509_cmp.o \
33 x509_obj.o x509_req.o x509_vfy.o \
34 x509_set.o x509rset.o $(ERRC).o \
35 x509name.o x509_v3.o x509_ext.o x509pack.o \
36 x509type.o x509_lu.o x_all.o x509_txt.o \
37 by_file.o by_dir.o \
38 v3_net.o v3_x509.o
39
40SRC= $(LIBSRC)
41
42EXHEADER= x509.h x509_vfy.h
43HEADER= $(EXHEADER)
44
45ALL= $(GENERAL) $(SRC) $(HEADER)
46
47top:
48 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
49
50all: lib
51
52lib: $(LIBOBJ)
53 $(AR) $(LIB) $(LIBOBJ)
54 sh $(TOP)/util/ranlib.sh $(LIB)
55 @touch lib
56
57files:
58 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
59
60links:
61 /bin/rm -f Makefile
62 $(TOP)/util/point.sh Makefile.ssl Makefile ;
63 $(TOP)/util/mklink.sh ../../include $(EXHEADER)
64 $(TOP)/util/mklink.sh ../../test $(TEST)
65 $(TOP)/util/mklink.sh ../../apps $(APPS)
66
67install:
68 @for i in $(EXHEADER) ; \
69 do \
70 (cp $$i $(INSTALLTOP)/include/$$i; \
71 chmod 644 $(INSTALLTOP)/include/$$i ); \
72 done;
73
74tags:
75 ctags $(SRC)
76
77tests:
78
79lint:
80 lint -DLINT $(INCLUDES) $(SRC)>fluff
81
82depend:
83 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
84
85dclean:
86 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
87 mv -f Makefile.new $(MAKEFILE)
88
89clean:
90 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
91
92errors:
93 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
94 perl ../err/err_genc.pl -s $(ERR).h $(ERRC).c
95
96# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/ssl/Makefile.ssl b/src/lib/libssl/src/ssl/Makefile.ssl
new file mode 100644
index 0000000000..f4b13bf83b
--- /dev/null
+++ b/src/lib/libssl/src/ssl/Makefile.ssl
@@ -0,0 +1,100 @@
1#
2# SSLeay/ssl/Makefile
3#
4
5DIR= ssl
6TOP= ..
7CC= cc
8INCLUDES= -I../crypto -I../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14AR= ar r
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17
18ERR=ssl
19ERRC=ssl_err
20GENERAL=Makefile README
21TEST=ssltest.c
22APPS=
23
24LIB=$(TOP)/libssl.a
25LIBSRC= \
26 s2_meth.c s2_srvr.c s2_clnt.c s2_lib.c s2_enc.c s2_pkt.c \
27 s3_meth.c s3_srvr.c s3_clnt.c s3_lib.c s3_enc.c s3_pkt.c s3_both.c \
28 s23_meth.c s23_srvr.c s23_clnt.c s23_lib.c s23_pkt.c \
29 t1_meth.c t1_srvr.c t1_clnt.c t1_lib.c t1_enc.c \
30 ssl_lib.c ssl_err2.c ssl_cert.c ssl_sess.c \
31 ssl_ciph.c ssl_stat.c ssl_rsa.c \
32 ssl_asn1.c ssl_txt.c ssl_algs.c \
33 bio_ssl.c $(ERRC).c
34LIBOBJ= \
35 s2_meth.o s2_srvr.o s2_clnt.o s2_lib.o s2_enc.o s2_pkt.o \
36 s3_meth.o s3_srvr.o s3_clnt.o s3_lib.o s3_enc.o s3_pkt.o s3_both.o \
37 s23_meth.o s23_srvr.o s23_clnt.o s23_lib.o s23_pkt.o \
38 t1_meth.o t1_srvr.o t1_clnt.o t1_lib.o t1_enc.o \
39 ssl_lib.o ssl_err2.o ssl_cert.o ssl_sess.o \
40 ssl_ciph.o ssl_stat.o ssl_rsa.o \
41 ssl_asn1.o ssl_txt.o ssl_algs.o \
42 bio_ssl.o $(ERRC).o
43
44SRC= $(LIBSRC)
45
46EXHEADER= ssl.h ssl2.h ssl3.h ssl23.h tls1.h
47HEADER= $(EXHEADER) ssl_locl.h
48
49ALL= $(GENERAL) $(SRC) $(HEADER)
50
51top:
52 (cd ..; $(MAKE) DIRS=$(DIR) all)
53
54all: lib
55
56lib: $(LIBOBJ)
57 $(AR) $(LIB) $(LIBOBJ)
58 sh $(TOP)/util/ranlib.sh $(LIB)
59 @touch lib
60
61files:
62 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
63
64links:
65 /bin/rm -f Makefile
66 $(TOP)/util/point.sh Makefile.ssl Makefile ;
67 $(TOP)/util/mklink.sh ../include $(EXHEADER)
68 $(TOP)/util/mklink.sh ../test $(TEST)
69 $(TOP)/util/mklink.sh ../apps $(APPS)
70
71install:
72 @for i in $(EXHEADER) ; \
73 do \
74 (cp $$i $(INSTALLTOP)/include/$$i; \
75 chmod 644 $(INSTALLTOP)/include/$$i ); \
76 done;
77
78tags:
79 ctags $(SRC)
80
81tests:
82
83lint:
84 lint -DLINT $(INCLUDES) $(SRC)>fluff
85
86depend:
87 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(LIBSRC)
88
89dclean:
90 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
91 mv -f Makefile.new $(MAKEFILE)
92
93clean:
94 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
95
96errors:
97 perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
98 perl ../crypto/err/err_genc.pl -s $(ERR).h $(ERRC).c
99
100# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/test/Makefile.ssl b/src/lib/libssl/src/test/Makefile.ssl
new file mode 100644
index 0000000000..b3de76751e
--- /dev/null
+++ b/src/lib/libssl/src/test/Makefile.ssl
@@ -0,0 +1,294 @@
1#
2# test/Makefile.ssl
3#
4
5DIR= test
6TOP= ..
7CC= cc
8INCLUDES= -I../include
9CFLAG= -g
10INSTALLTOP= /usr/local/ssl
11MAKEFILE= Makefile.ssl
12MAKE= make -f $(MAKEFILE)
13MAKEDEPEND= makedepend -f$(MAKEFILE)
14
15PEX_LIBS=
16EX_LIBS= #-lnsl -lsocket
17
18CFLAGS= $(INCLUDES) $(CFLAG)
19
20GENERAL=Makefile.ssl
21
22DLIBCRYPTO= ../libcrypto.a
23DLIBSSL= ../libssl.a
24LIBCRYPTO= -L.. -lcrypto
25LIBSSL= -L.. -lssl
26
27BNTEST= bntest
28EXPTEST= exptest
29IDEATEST= ideatest
30SHATEST= shatest
31SHA1TEST= sha1test
32MDC2TEST= mdc2test
33RMDTEST= rmdtest
34MD2TEST= md2test
35MD5TEST= md5test
36HMACTEST= hmactest
37RC2TEST= rc2test
38RC4TEST= rc4test
39RC5TEST= rc5test
40BFTEST= bftest
41CASTTEST= casttest
42DESTEST= destest
43RANDTEST= randtest
44DHTEST= dhtest
45DSATEST= dsatest
46METHTEST= methtest
47SSLTEST= ssltest
48
49EXE= $(BNTEST) $(IDEATEST) $(MD2TEST) $(MD5TEST) $(HMACTEST) \
50 $(RC2TEST) $(RC4TEST) $(RC5TEST) \
51 $(DESTEST) $(SHATEST) $(SHA1TEST) $(MDC2TEST) $(RMDTEST) \
52 $(RANDTEST) $(DHTEST) \
53 $(BFTEST) $(CASTTEST) $(SSLTEST) $(EXPTEST) $(DSATEST)
54
55# $(METHTEST)
56
57OBJ= $(BNTEST).o $(IDEATEST).o $(MD2TEST).o $(MD5TEST).o $(HMACTEST).o \
58 $(RC2TEST).o $(RC4TEST).o $(RC5TEST).o \
59 $(DESTEST).o $(SHATEST).o $(SHA1TEST).o $(MDC2TEST).o $(RMDTEST).o \
60 $(RANDTEST).o $(DHTEST).o $(CASTTEST).o \
61 $(BFTEST).o $(SSLTEST).o $(DSATEST).o $(EXPTEST).o
62SRC= $(BNTEST).c $(IDEATEST).c $(MD2TEST).c $(MD5TEST).c $(HMACTEST).c \
63 $(RC2TEST).c $(RC4TEST).c $(RC5TEST).c \
64 $(DESTEST).c $(SHATEST).c $(SHA1TEST).c $(MDC2TEST).c $(RMDTEST).c \
65 $(RANDTEST).c $(DHTEST).c $(CASTTEST).c \
66 $(BFTEST).c $(SSLTEST).c $(DSATEST).c $(EXPTEST).c
67
68EXHEADER=
69HEADER= $(EXHEADER)
70
71ALL= $(GENERAL) $(SRC) $(HEADER)
72
73top:
74 (cd ..; $(MAKE) DIRS=$(DIR) all)
75
76all: exe
77
78exe: $(EXE)
79
80files:
81 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
82
83links:
84 /bin/rm -f Makefile
85 $(TOP)/util/point.sh Makefile.ssl Makefile ;
86
87errors:
88
89install:
90
91tags:
92 ctags $(SRC)
93
94tests: exe apps \
95 test_des test_idea test_sha test_md5 test_hmac test_md2 test_mdc2 \
96 test_rc2 test_rc4 test_rc5 test_bf test_cast \
97 test_rand test_enc test_x509 test_rsa test_crl test_sid test_req \
98 test_pkcs7 test_bn test_verify test_dh test_dsa test_reqgen \
99 test_ss test_ssl test_ca
100
101apps:
102 @(cd ../apps; $(MAKE) CC='${CC}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' all)
103
104test_des:
105 #./$(DESTEST)
106
107test_idea:
108 ./$(IDEATEST)
109
110test_sha:
111 ./$(SHATEST)
112 ./$(SHA1TEST)
113
114test_mdc2:
115 ./$(MDC2TEST)
116
117test_md5:
118 ./$(MD5TEST)
119
120test_hmac:
121 ./$(HMACTEST)
122
123test_md2:
124 ./$(MD2TEST)
125
126test_rmd:
127 ./$(RMDTEST)
128
129test_bf:
130 ./$(BFTEST)
131
132test_cast:
133 ./$(CASTTEST)
134
135test_rc2:
136 ./$(RC2TEST)
137
138test_rc4:
139 ./$(RC4TEST)
140
141test_rc5:
142 ./$(RC5TEST)
143
144test_rand:
145 ./$(RANDTEST)
146
147test_enc:
148 @sh ./testenc
149
150test_x509:
151 echo test normal x509v1 certificate
152 sh ./tx509 2>/dev/null
153 echo test first x509v3 certificate
154 sh ./tx509 v3-cert1.pem 2>/dev/null
155 echo test second x509v3 certificate
156 sh ./tx509 v3-cert2.pem 2>/dev/null
157
158test_rsa:
159 @sh ./trsa 2>/dev/null
160
161test_crl:
162 @sh ./tcrl 2>/dev/null
163
164test_sid:
165 @sh ./tsid 2>/dev/null
166
167test_req:
168 @sh ./treq 2>/dev/null
169 @sh ./treq testreq2.pem 2>/dev/null
170
171test_pkcs7:
172 @sh ./tpkcs7 2>/dev/null
173 @sh ./tpkcs7d 2>/dev/null
174
175test_bn:
176 @echo 'test a^b%c implementations'
177 ./$(EXPTEST)
178 @echo starting big number library test, could take a while...
179 @(./$(BNTEST)|bc) | awk '{ \
180if ($$0 != "0") {print "error"; exit(1); } \
181if (((NR+1)%64) == 0) print NR+1," tests done"; }'
182
183test_verify:
184 @echo "The following command should have some OK's and some failures"
185 @echo "There are definitly a few expired certificates"
186 ../apps/ssleay verify -CApath ../certs ../certs/*.pem
187
188test_dh:
189 @echo "Generate as set of DH parameters"
190 ./$(DHTEST)
191
192test_dsa:
193 @echo "Generate as set of DSA parameters"
194 ./$(DSATEST)
195
196test_reqgen:
197 @echo "Generate and verify a certificate request"
198 @sh ./testgen
199
200test_ss:
201 @echo "Generate and certify a test certificate"
202 @sh ./testss
203
204test_ssl:
205 @echo "test SSL protocol"
206 @sh ./testssl
207
208test_ca:
209 @echo "Generate and certify a test certificate via the 'ca' program"
210 @sh ./testca
211
212lint:
213 lint -DLINT $(INCLUDES) $(SRC)>fluff
214
215depend:
216 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(SRC)
217
218dclean:
219 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
220 mv -f Makefile.new $(MAKEFILE)
221
222clean:
223 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff $(EXE) *.ss log
224
225$(DLIBSSL):
226 (cd ../ssl; $(MAKE))
227
228$(DLIBCRYPTO):
229 (cd ../crypto; $(MAKE))
230
231$(BNTEST): $(BNTEST).o $(DLIBCRYPTO)
232 $(CC) -o $(BNTEST) $(CFLAGS) $(BNTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
233
234$(EXPTEST): $(EXPTEST).o $(DLIBCRYPTO)
235 $(CC) -o $(EXPTEST) $(CFLAGS) $(EXPTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
236
237$(IDEATEST): $(IDEATEST).o $(DLIBCRYPTO)
238 $(CC) -o $(IDEATEST) $(CFLAGS) $(IDEATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
239
240$(MD2TEST): $(MD2TEST).o $(DLIBCRYPTO)
241 $(CC) -o $(MD2TEST) $(CFLAGS) $(MD2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
242
243$(SHATEST): $(SHATEST).o $(DLIBCRYPTO)
244 $(CC) -o $(SHATEST) $(CFLAGS) $(SHATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
245
246$(SHA1TEST): $(SHA1TEST).o $(DLIBCRYPTO)
247 $(CC) -o $(SHA1TEST) $(CFLAGS) $(SHA1TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
248
249$(RMDTEST): $(RMDTEST).o $(DLIBCRYPTO)
250 $(CC) -o $(RMDTEST) $(CFLAGS) $(RMDTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
251
252$(MDC2TEST): $(MDC2TEST).o $(DLIBCRYPTO)
253 $(CC) -o $(MDC2TEST) $(CFLAGS) $(MDC2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
254
255$(MD5TEST): $(MD5TEST).o $(DLIBCRYPTO)
256 $(CC) -o $(MD5TEST) $(CFLAGS) $(MD5TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
257
258$(HMACTEST): $(HMACTEST).o $(DLIBCRYPTO)
259 $(CC) -o $(HMACTEST) $(CFLAGS) $(HMACTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
260
261$(RC2TEST): $(RC2TEST).o $(DLIBCRYPTO)
262 $(CC) -o $(RC2TEST) $(CFLAGS) $(RC2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
263
264$(BFTEST): $(BFTEST).o $(DLIBCRYPTO)
265 $(CC) -o $(BFTEST) $(CFLAGS) $(BFTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
266
267$(CASTTEST): $(CASTTEST).o $(DLIBCRYPTO)
268 $(CC) -o $(CASTTEST) $(CFLAGS) $(CASTTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
269
270$(RC4TEST): $(RC4TEST).o $(DLIBCRYPTO)
271 $(CC) -o $(RC4TEST) $(CFLAGS) $(RC4TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
272
273$(RC5TEST): $(RC5TEST).o $(DLIBCRYPTO)
274 $(CC) -o $(RC5TEST) $(CFLAGS) $(RC5TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
275
276$(DESTEST): $(DESTEST).o $(DLIBCRYPTO)
277 $(CC) -o $(DESTEST) $(CFLAGS) $(DESTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
278
279$(RANDTEST): $(RANDTEST).o $(DLIBCRYPTO)
280 $(CC) -o $(RANDTEST) $(CFLAGS) $(RANDTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
281
282$(DHTEST): $(DHTEST).o $(DLIBCRYPTO)
283 $(CC) -o $(DHTEST) $(CFLAGS) $(DHTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
284
285$(DSATEST): $(DSATEST).o $(DLIBCRYPTO)
286 $(CC) -o $(DSATEST) $(CFLAGS) $(DSATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
287
288$(METHTEST): $(METHTEST).o $(DLIBCRYPTO)
289 $(CC) -o $(METHTEST) $(CFLAGS) $(METHTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
290
291$(SSLTEST): $(SSLTEST).o $(DLIBSSL) $(DLIBCRYPTO)
292 $(CC) -o $(SSLTEST) $(CFLAGS) $(SSLTEST).o $(PEX_LIBS) $(LIBSSL) $(LIBCRYPTO) $(EX_LIBS)
293
294# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/src/tools/Makefile.ssl b/src/lib/libssl/src/tools/Makefile.ssl
new file mode 100644
index 0000000000..537e97d268
--- /dev/null
+++ b/src/lib/libssl/src/tools/Makefile.ssl
@@ -0,0 +1,54 @@
1#
2# SSLeay/tools/Makefile
3#
4
5DIR= tools
6TOP= ..
7CC= cc
8INCLUDES= -I.. -I../../include
9CFLAG=-g
10INSTALLTOP=/usr/local/ssl
11MAKE= make -f Makefile.ssl
12MAKEDEPEND= makedepend -f Makefile.ssl
13MAKEFILE= Makefile.ssl
14
15CFLAGS= $(INCLUDES) $(CFLAG)
16
17GENERAL=Makefile.ssl
18TEST=
19APPS= c_hash c_info c_issuer c_name c_rehash
20
21all:
22
23install:
24 @for i in $(APPS) ; \
25 do \
26 (cp $$i $(INSTALLTOP)/bin/$$i; \
27 chmod 755 $(INSTALLTOP)/bin/$$i ); \
28 done;
29
30files:
31 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
32
33links:
34 /bin/rm -f Makefile
35 $(TOP)/util/point.sh Makefile.ssl Makefile ;
36
37lint:
38
39tags:
40
41errors:
42
43depend:
44
45dclean:
46 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
47 mv -f Makefile.new $(MAKEFILE)
48
49clean:
50 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
51
52errors:
53
54# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/lib/libssl/test/Makefile.ssl b/src/lib/libssl/test/Makefile.ssl
new file mode 100644
index 0000000000..b3de76751e
--- /dev/null
+++ b/src/lib/libssl/test/Makefile.ssl
@@ -0,0 +1,294 @@
1#
2# test/Makefile.ssl
3#
4
5DIR= test
6TOP= ..
7CC= cc
8INCLUDES= -I../include
9CFLAG= -g
10INSTALLTOP= /usr/local/ssl
11MAKEFILE= Makefile.ssl
12MAKE= make -f $(MAKEFILE)
13MAKEDEPEND= makedepend -f$(MAKEFILE)
14
15PEX_LIBS=
16EX_LIBS= #-lnsl -lsocket
17
18CFLAGS= $(INCLUDES) $(CFLAG)
19
20GENERAL=Makefile.ssl
21
22DLIBCRYPTO= ../libcrypto.a
23DLIBSSL= ../libssl.a
24LIBCRYPTO= -L.. -lcrypto
25LIBSSL= -L.. -lssl
26
27BNTEST= bntest
28EXPTEST= exptest
29IDEATEST= ideatest
30SHATEST= shatest
31SHA1TEST= sha1test
32MDC2TEST= mdc2test
33RMDTEST= rmdtest
34MD2TEST= md2test
35MD5TEST= md5test
36HMACTEST= hmactest
37RC2TEST= rc2test
38RC4TEST= rc4test
39RC5TEST= rc5test
40BFTEST= bftest
41CASTTEST= casttest
42DESTEST= destest
43RANDTEST= randtest
44DHTEST= dhtest
45DSATEST= dsatest
46METHTEST= methtest
47SSLTEST= ssltest
48
49EXE= $(BNTEST) $(IDEATEST) $(MD2TEST) $(MD5TEST) $(HMACTEST) \
50 $(RC2TEST) $(RC4TEST) $(RC5TEST) \
51 $(DESTEST) $(SHATEST) $(SHA1TEST) $(MDC2TEST) $(RMDTEST) \
52 $(RANDTEST) $(DHTEST) \
53 $(BFTEST) $(CASTTEST) $(SSLTEST) $(EXPTEST) $(DSATEST)
54
55# $(METHTEST)
56
57OBJ= $(BNTEST).o $(IDEATEST).o $(MD2TEST).o $(MD5TEST).o $(HMACTEST).o \
58 $(RC2TEST).o $(RC4TEST).o $(RC5TEST).o \
59 $(DESTEST).o $(SHATEST).o $(SHA1TEST).o $(MDC2TEST).o $(RMDTEST).o \
60 $(RANDTEST).o $(DHTEST).o $(CASTTEST).o \
61 $(BFTEST).o $(SSLTEST).o $(DSATEST).o $(EXPTEST).o
62SRC= $(BNTEST).c $(IDEATEST).c $(MD2TEST).c $(MD5TEST).c $(HMACTEST).c \
63 $(RC2TEST).c $(RC4TEST).c $(RC5TEST).c \
64 $(DESTEST).c $(SHATEST).c $(SHA1TEST).c $(MDC2TEST).c $(RMDTEST).c \
65 $(RANDTEST).c $(DHTEST).c $(CASTTEST).c \
66 $(BFTEST).c $(SSLTEST).c $(DSATEST).c $(EXPTEST).c
67
68EXHEADER=
69HEADER= $(EXHEADER)
70
71ALL= $(GENERAL) $(SRC) $(HEADER)
72
73top:
74 (cd ..; $(MAKE) DIRS=$(DIR) all)
75
76all: exe
77
78exe: $(EXE)
79
80files:
81 perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
82
83links:
84 /bin/rm -f Makefile
85 $(TOP)/util/point.sh Makefile.ssl Makefile ;
86
87errors:
88
89install:
90
91tags:
92 ctags $(SRC)
93
94tests: exe apps \
95 test_des test_idea test_sha test_md5 test_hmac test_md2 test_mdc2 \
96 test_rc2 test_rc4 test_rc5 test_bf test_cast \
97 test_rand test_enc test_x509 test_rsa test_crl test_sid test_req \
98 test_pkcs7 test_bn test_verify test_dh test_dsa test_reqgen \
99 test_ss test_ssl test_ca
100
101apps:
102 @(cd ../apps; $(MAKE) CC='${CC}' CFLAG='${CFLAG}' INSTALLTOP='${INSTALLTOP}' PEX_LIBS='${PEX_LIBS}' EX_LIBS='${EX_LIBS}' all)
103
104test_des:
105 #./$(DESTEST)
106
107test_idea:
108 ./$(IDEATEST)
109
110test_sha:
111 ./$(SHATEST)
112 ./$(SHA1TEST)
113
114test_mdc2:
115 ./$(MDC2TEST)
116
117test_md5:
118 ./$(MD5TEST)
119
120test_hmac:
121 ./$(HMACTEST)
122
123test_md2:
124 ./$(MD2TEST)
125
126test_rmd:
127 ./$(RMDTEST)
128
129test_bf:
130 ./$(BFTEST)
131
132test_cast:
133 ./$(CASTTEST)
134
135test_rc2:
136 ./$(RC2TEST)
137
138test_rc4:
139 ./$(RC4TEST)
140
141test_rc5:
142 ./$(RC5TEST)
143
144test_rand:
145 ./$(RANDTEST)
146
147test_enc:
148 @sh ./testenc
149
150test_x509:
151 echo test normal x509v1 certificate
152 sh ./tx509 2>/dev/null
153 echo test first x509v3 certificate
154 sh ./tx509 v3-cert1.pem 2>/dev/null
155 echo test second x509v3 certificate
156 sh ./tx509 v3-cert2.pem 2>/dev/null
157
158test_rsa:
159 @sh ./trsa 2>/dev/null
160
161test_crl:
162 @sh ./tcrl 2>/dev/null
163
164test_sid:
165 @sh ./tsid 2>/dev/null
166
167test_req:
168 @sh ./treq 2>/dev/null
169 @sh ./treq testreq2.pem 2>/dev/null
170
171test_pkcs7:
172 @sh ./tpkcs7 2>/dev/null
173 @sh ./tpkcs7d 2>/dev/null
174
175test_bn:
176 @echo 'test a^b%c implementations'
177 ./$(EXPTEST)
178 @echo starting big number library test, could take a while...
179 @(./$(BNTEST)|bc) | awk '{ \
180if ($$0 != "0") {print "error"; exit(1); } \
181if (((NR+1)%64) == 0) print NR+1," tests done"; }'
182
183test_verify:
184 @echo "The following command should have some OK's and some failures"
185 @echo "There are definitly a few expired certificates"
186 ../apps/ssleay verify -CApath ../certs ../certs/*.pem
187
188test_dh:
189 @echo "Generate as set of DH parameters"
190 ./$(DHTEST)
191
192test_dsa:
193 @echo "Generate as set of DSA parameters"
194 ./$(DSATEST)
195
196test_reqgen:
197 @echo "Generate and verify a certificate request"
198 @sh ./testgen
199
200test_ss:
201 @echo "Generate and certify a test certificate"
202 @sh ./testss
203
204test_ssl:
205 @echo "test SSL protocol"
206 @sh ./testssl
207
208test_ca:
209 @echo "Generate and certify a test certificate via the 'ca' program"
210 @sh ./testca
211
212lint:
213 lint -DLINT $(INCLUDES) $(SRC)>fluff
214
215depend:
216 $(MAKEDEPEND) $(INCLUDES) $(PROGS) $(SRC)
217
218dclean:
219 perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
220 mv -f Makefile.new $(MAKEFILE)
221
222clean:
223 /bin/rm -f *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff $(EXE) *.ss log
224
225$(DLIBSSL):
226 (cd ../ssl; $(MAKE))
227
228$(DLIBCRYPTO):
229 (cd ../crypto; $(MAKE))
230
231$(BNTEST): $(BNTEST).o $(DLIBCRYPTO)
232 $(CC) -o $(BNTEST) $(CFLAGS) $(BNTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
233
234$(EXPTEST): $(EXPTEST).o $(DLIBCRYPTO)
235 $(CC) -o $(EXPTEST) $(CFLAGS) $(EXPTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
236
237$(IDEATEST): $(IDEATEST).o $(DLIBCRYPTO)
238 $(CC) -o $(IDEATEST) $(CFLAGS) $(IDEATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
239
240$(MD2TEST): $(MD2TEST).o $(DLIBCRYPTO)
241 $(CC) -o $(MD2TEST) $(CFLAGS) $(MD2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
242
243$(SHATEST): $(SHATEST).o $(DLIBCRYPTO)
244 $(CC) -o $(SHATEST) $(CFLAGS) $(SHATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
245
246$(SHA1TEST): $(SHA1TEST).o $(DLIBCRYPTO)
247 $(CC) -o $(SHA1TEST) $(CFLAGS) $(SHA1TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
248
249$(RMDTEST): $(RMDTEST).o $(DLIBCRYPTO)
250 $(CC) -o $(RMDTEST) $(CFLAGS) $(RMDTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
251
252$(MDC2TEST): $(MDC2TEST).o $(DLIBCRYPTO)
253 $(CC) -o $(MDC2TEST) $(CFLAGS) $(MDC2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
254
255$(MD5TEST): $(MD5TEST).o $(DLIBCRYPTO)
256 $(CC) -o $(MD5TEST) $(CFLAGS) $(MD5TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
257
258$(HMACTEST): $(HMACTEST).o $(DLIBCRYPTO)
259 $(CC) -o $(HMACTEST) $(CFLAGS) $(HMACTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
260
261$(RC2TEST): $(RC2TEST).o $(DLIBCRYPTO)
262 $(CC) -o $(RC2TEST) $(CFLAGS) $(RC2TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
263
264$(BFTEST): $(BFTEST).o $(DLIBCRYPTO)
265 $(CC) -o $(BFTEST) $(CFLAGS) $(BFTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
266
267$(CASTTEST): $(CASTTEST).o $(DLIBCRYPTO)
268 $(CC) -o $(CASTTEST) $(CFLAGS) $(CASTTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
269
270$(RC4TEST): $(RC4TEST).o $(DLIBCRYPTO)
271 $(CC) -o $(RC4TEST) $(CFLAGS) $(RC4TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
272
273$(RC5TEST): $(RC5TEST).o $(DLIBCRYPTO)
274 $(CC) -o $(RC5TEST) $(CFLAGS) $(RC5TEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
275
276$(DESTEST): $(DESTEST).o $(DLIBCRYPTO)
277 $(CC) -o $(DESTEST) $(CFLAGS) $(DESTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
278
279$(RANDTEST): $(RANDTEST).o $(DLIBCRYPTO)
280 $(CC) -o $(RANDTEST) $(CFLAGS) $(RANDTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
281
282$(DHTEST): $(DHTEST).o $(DLIBCRYPTO)
283 $(CC) -o $(DHTEST) $(CFLAGS) $(DHTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
284
285$(DSATEST): $(DSATEST).o $(DLIBCRYPTO)
286 $(CC) -o $(DSATEST) $(CFLAGS) $(DSATEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
287
288$(METHTEST): $(METHTEST).o $(DLIBCRYPTO)
289 $(CC) -o $(METHTEST) $(CFLAGS) $(METHTEST).o $(PEX_LIBS) $(LIBCRYPTO) $(EX_LIBS)
290
291$(SSLTEST): $(SSLTEST).o $(DLIBSSL) $(DLIBCRYPTO)
292 $(CC) -o $(SSLTEST) $(CFLAGS) $(SSLTEST).o $(PEX_LIBS) $(LIBSSL) $(LIBCRYPTO) $(EX_LIBS)
293
294# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/src/regress/lib/libc/db/Makefile b/src/regress/lib/libc/db/Makefile
index 5bf343bf64..d42dc96315 100644
--- a/src/regress/lib/libc/db/Makefile
+++ b/src/regress/lib/libc/db/Makefile
@@ -1,11 +1,11 @@
1# $NetBSD: Makefile,v 1.10 1995/04/20 22:39:11 cgd Exp $ 1# $NetBSD: Makefile,v 1.11 1995/12/12 01:54:15 cgd Exp $
2# @(#)Makefile 8.1 (Berkeley) 6/4/93 2# @(#)Makefile 8.1 (Berkeley) 6/4/93
3 3
4PROG= dbtest 4PROG= dbtest
5 5
6# add -DSTATISTICS to CFLAGS to get usage statistics. Note that 6# add -DSTATISTICS to CFLAGS to get usage statistics. Note that
7# for this to work, libc must be compiled with -DSTATISTICS as well 7# for this to work, libc must be compiled with -DSTATISTICS as well
8CFLAGS= -g -D__DBINTERFACE_PRIVATE -DDEBUG 8CFLAGS+= -g -D__DBINTERFACE_PRIVATE -DDEBUG
9NOMAN= noman 9NOMAN= noman
10CLEANFILES+= t1 t2 t3 10CLEANFILES+= t1 t2 t3
11 11
diff --git a/src/regress/lib/libc/db/README b/src/regress/lib/libc/db/README
index 3b290b09d8..fddf5c3d5a 100644
--- a/src/regress/lib/libc/db/README
+++ b/src/regress/lib/libc/db/README
@@ -1,5 +1,5 @@
1# $NetBSD: README,v 1.4 1995/04/20 22:39:18 cgd Exp $ 1# $NetBSD: README,v 1.5 1996/05/03 21:54:19 cgd Exp $
2# @(#)README 8.4 (Berkeley) 6/20/94 2# @(#)README 8.8 (Berkeley) 7/31/94
3 3
4To run the tests, enter "make regress". 4To run the tests, enter "make regress".
5 5
@@ -9,8 +9,11 @@ the test runs, and even larger files (the database files) are created in
9variable TMPDIR to a directory where the files can be built. 9variable TMPDIR to a directory where the files can be built.
10 10
11=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 11=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12The script file consists of lines with a initial character which is 12The script file consists of lines with an initial character which is
13the "command" for that line. Legal characters are as follows: 13the command for that line, or an initial character indicating a key
14or data entry for a previous command.
15
16Legal command characters are as follows:
14 17
15c: compare a record 18c: compare a record
16 + must be followed by [kK][dD]; the data value in the database 19 + must be followed by [kK][dD]; the data value in the database
@@ -19,17 +22,24 @@ c: compare a record
19e: echo a string 22e: echo a string
20 + writes out the rest of the line into the output file; if the 23 + writes out the rest of the line into the output file; if the
21 last character is not a carriage-return, a newline is appended. 24 last character is not a carriage-return, a newline is appended.
25f: set the flags for the next command
26 + no value zero's the flags
22g: do a get command 27g: do a get command
23 + must be followed by [kK] 28 + must be followed by [kK]
24 + writes out the retrieved data DBT. 29 + writes out the retrieved data DBT.
30o [r]: dump [reverse]
31 + dump the database out, if 'r' is set, in reverse order.
25p: do a put command 32p: do a put command
26 + must be followed by [kK][dD] 33 + must be followed by [kK][dD]
27r: do a del command 34r: do a del command
28 + must be followed by [kK] 35 + must be followed by [kK] unless R_CURSOR flag set.
36S: sync the database
29s: do a seq command 37s: do a seq command
38 + must be followed by [kK] if R_CURSOR flag set.
30 + writes out the retrieved data DBT. 39 + writes out the retrieved data DBT.
31f: set the flags for the next command 40
32 + no value zero's the flags 41Legal key/data characters are as follows:
42
33D [file]: data file 43D [file]: data file
34 + set the current data value to the contents of the file 44 + set the current data value to the contents of the file
35d [data]: 45d [data]:
@@ -38,17 +48,21 @@ K [file]: key file
38 + set the current key value to the contents of the file 48 + set the current key value to the contents of the file
39k [data]: 49k [data]:
40 + set the current key value to the contents of the line. 50 + set the current key value to the contents of the line.
41o [r]: dump [reverse] 51
42 + dump the database out, if 'r' is set, in reverse order. 52Blank lines, lines with leading white space, and lines with leading
53hash marks (#) are ignored.
43 54
44Options to dbtest are as follows: 55Options to dbtest are as follows:
45 56
57 -d: Set the DB_LOCK flag.
46 -f: Use the file argument as the database file. 58 -f: Use the file argument as the database file.
47 -i: Use the rest of the argument to set elements in the info 59 -i: Use the rest of the argument to set elements in the info
48 structure. If the type is btree, then "-i cachesize=10240" 60 structure. If the type is btree, then "-i cachesize=10240"
49 will set BTREEINFO.cachesize to 10240. 61 will set BTREEINFO.cachesize to 10240.
50 -o: The rest of the argument is the output file instead of 62 -o: The rest of the argument is the output file instead of
51 using stdout. 63 using stdout.
64 -s: Don't delete the database file before opening it, i.e.
65 use the database file from a previous run.
52 66
53Dbtest requires two arguments, the type of access "hash", "recno" or 67Dbtest requires two arguments, the type of access "hash", "recno"
54"btree", and the script name. 68or "btree", and the script name or "-" to indicate stdin.
diff --git a/src/regress/lib/libc/db/dbtest.c b/src/regress/lib/libc/db/dbtest.c
index 1fcf09af97..6ccfba5011 100644
--- a/src/regress/lib/libc/db/dbtest.c
+++ b/src/regress/lib/libc/db/dbtest.c
@@ -1,7 +1,7 @@
1/* $NetBSD: dbtest.c,v 1.7 1995/04/20 22:39:22 cgd Exp $ */ 1/* $NetBSD: dbtest.c,v 1.8 1996/05/03 21:57:48 cgd Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
@@ -35,15 +35,15 @@
35 35
36#ifndef lint 36#ifndef lint
37static char copyright[] = 37static char copyright[] =
38"@(#) Copyright (c) 1992, 1993\n\ 38"@(#) Copyright (c) 1992, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n"; 39 The Regents of the University of California. All rights reserved.\n";
40#endif /* not lint */ 40#endif /* not lint */
41 41
42#ifndef lint 42#ifndef lint
43#if 0 43#if 0
44static char sccsid[] = "@(#)dbtest.c 8.8 (Berkeley) 2/21/94"; 44static char sccsid[] = "@(#)dbtest.c 8.17 (Berkeley) 9/1/94";
45#else 45#else
46static char rcsid[] = "$NetBSD: dbtest.c,v 1.7 1995/04/20 22:39:22 cgd Exp $"; 46static char rcsid[] = "$NetBSD: dbtest.c,v 1.8 1996/05/03 21:57:48 cgd Exp $";
47#endif 47#endif
48#endif /* not lint */ 48#endif /* not lint */
49 49
@@ -71,6 +71,8 @@ void get __P((DB *, DBT *));
71void getdata __P((DB *, DBT *, DBT *)); 71void getdata __P((DB *, DBT *, DBT *));
72void put __P((DB *, DBT *, DBT *)); 72void put __P((DB *, DBT *, DBT *));
73void rem __P((DB *, DBT *)); 73void rem __P((DB *, DBT *));
74char *sflags __P((int));
75void synk __P((DB *));
74void *rfile __P((char *, size_t *)); 76void *rfile __P((char *, size_t *));
75void seq __P((DB *, DBT *)); 77void seq __P((DB *, DBT *));
76u_int setflags __P((char *)); 78u_int setflags __P((char *));
@@ -78,13 +80,14 @@ void *setinfo __P((DBTYPE, char *));
78void usage __P((void)); 80void usage __P((void));
79void *xmalloc __P((char *, size_t)); 81void *xmalloc __P((char *, size_t));
80 82
81DBTYPE type; 83DBTYPE type; /* Database type. */
82void *infop; 84void *infop; /* Iflags. */
83u_long lineno; 85u_long lineno; /* Current line in test script. */
84u_int flags; 86u_int flags; /* Current DB flags. */
85int ofd = STDOUT_FILENO; 87int ofd = STDOUT_FILENO; /* Standard output fd. */
86 88
87DB *XXdbp; /* Global for gdb. */ 89DB *XXdbp; /* Global for gdb. */
90int XXlineno; /* Fast breakpoint for gdb. */
88 91
89int 92int
90main(argc, argv) 93main(argc, argv)
@@ -97,14 +100,15 @@ main(argc, argv)
97 DB *dbp; 100 DB *dbp;
98 DBT data, key, keydata; 101 DBT data, key, keydata;
99 size_t len; 102 size_t len;
100 int ch, oflags; 103 int ch, oflags, sflag;
101 char *fname, *infoarg, *p, buf[8 * 1024]; 104 char *fname, *infoarg, *p, *t, buf[8 * 1024];
102 105
103 infoarg = NULL; 106 infoarg = NULL;
104 fname = NULL; 107 fname = NULL;
105 oflags = O_CREAT | O_RDWR; 108 oflags = O_CREAT | O_RDWR;
106 while ((ch = getopt(argc, argv, "f:i:lo:")) != EOF) 109 sflag = 0;
107 switch(ch) { 110 while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
111 switch (ch) {
108 case 'f': 112 case 'f':
109 fname = optarg; 113 fname = optarg;
110 break; 114 break;
@@ -119,6 +123,9 @@ main(argc, argv)
119 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 123 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
120 err("%s: %s", optarg, strerror(errno)); 124 err("%s: %s", optarg, strerror(errno));
121 break; 125 break;
126 case 's':
127 sflag = 1;
128 break;
122 case '?': 129 case '?':
123 default: 130 default:
124 usage(); 131 usage();
@@ -133,8 +140,8 @@ main(argc, argv)
133 type = dbtype(*argv++); 140 type = dbtype(*argv++);
134 141
135 /* Open the descriptor file. */ 142 /* Open the descriptor file. */
136 if (freopen(*argv, "r", stdin) == NULL) 143 if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
137 err("%s: %s", *argv, strerror(errno)); 144 err("%s: %s", *argv, strerror(errno));
138 145
139 /* Set up the db structure as necessary. */ 146 /* Set up the db structure as necessary. */
140 if (infoarg == NULL) 147 if (infoarg == NULL)
@@ -145,7 +152,10 @@ main(argc, argv)
145 if (*p != '\0') 152 if (*p != '\0')
146 infop = setinfo(type, p); 153 infop = setinfo(type, p);
147 154
148 /* Open the DB. */ 155 /*
156 * Open the DB. Delete any preexisting copy, you almost never
157 * want it around, and it often screws up tests.
158 */
149 if (fname == NULL) { 159 if (fname == NULL) {
150 p = getenv("TMPDIR"); 160 p = getenv("TMPDIR");
151 if (p == NULL) 161 if (p == NULL)
@@ -153,7 +163,9 @@ main(argc, argv)
153 (void)sprintf(buf, "%s/__dbtest", p); 163 (void)sprintf(buf, "%s/__dbtest", p);
154 fname = buf; 164 fname = buf;
155 (void)unlink(buf); 165 (void)unlink(buf);
156 } 166 } else if (!sflag)
167 (void)unlink(fname);
168
157 if ((dbp = dbopen(fname, 169 if ((dbp = dbopen(fname,
158 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL) 170 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
159 err("dbopen: %s", strerror(errno)); 171 err("dbopen: %s", strerror(errno));
@@ -162,8 +174,16 @@ main(argc, argv)
162 state = COMMAND; 174 state = COMMAND;
163 for (lineno = 1; 175 for (lineno = 1;
164 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) { 176 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
165 len = strlen(buf); 177 /* Delete the newline, displaying the key/data is easier. */
166 switch(*p) { 178 if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
179 *t = '\0';
180 if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
181 continue;
182
183 /* Convenient gdb break point. */
184 if (XXlineno == lineno)
185 XXlineno = 1;
186 switch (*p) {
167 case 'c': /* compare */ 187 case 'c': /* compare */
168 if (state != COMMAND) 188 if (state != COMMAND)
169 err("line %lu: not expecting command", lineno); 189 err("line %lu: not expecting command", lineno);
@@ -176,7 +196,8 @@ main(argc, argv)
176 /* Don't display the newline, if CR at EOL. */ 196 /* Don't display the newline, if CR at EOL. */
177 if (p[len - 2] == '\r') 197 if (p[len - 2] == '\r')
178 --len; 198 --len;
179 if (write(ofd, p + 1, len - 1) != len - 1) 199 if (write(ofd, p + 1, len - 1) != len - 1 ||
200 write(ofd, "\n", 1) != 1)
180 err("write: %s", strerror(errno)); 201 err("write: %s", strerror(errno));
181 break; 202 break;
182 case 'g': /* get */ 203 case 'g': /* get */
@@ -194,8 +215,19 @@ main(argc, argv)
194 case 'r': /* remove */ 215 case 'r': /* remove */
195 if (state != COMMAND) 216 if (state != COMMAND)
196 err("line %lu: not expecting command", lineno); 217 err("line %lu: not expecting command", lineno);
197 state = KEY; 218 if (flags == R_CURSOR) {
198 command = REMOVE; 219 rem(dbp, &key);
220 state = COMMAND;
221 } else {
222 state = KEY;
223 command = REMOVE;
224 }
225 break;
226 case 'S': /* sync */
227 if (state != COMMAND)
228 err("line %lu: not expecting command", lineno);
229 synk(dbp);
230 state = COMMAND;
199 break; 231 break;
200 case 's': /* seq */ 232 case 's': /* seq */
201 if (state != COMMAND) 233 if (state != COMMAND)
@@ -219,7 +251,7 @@ main(argc, argv)
219 err("line %lu: not expecting data", lineno); 251 err("line %lu: not expecting data", lineno);
220 data.data = xmalloc(p + 1, len - 1); 252 data.data = xmalloc(p + 1, len - 1);
221 data.size = len - 1; 253 data.size = len - 1;
222ldata: switch(command) { 254ldata: switch (command) {
223 case COMPARE: 255 case COMPARE:
224 compare(&keydata, &data); 256 compare(&keydata, &data);
225 break; 257 break;
@@ -255,7 +287,7 @@ ldata: switch(command) {
255 key.data = xmalloc(p + 1, len - 1); 287 key.data = xmalloc(p + 1, len - 1);
256 key.size = len - 1; 288 key.size = len - 1;
257 } 289 }
258lkey: switch(command) { 290lkey: switch (command) {
259 case COMPARE: 291 case COMPARE:
260 getdata(dbp, &key, &keydata); 292 getdata(dbp, &key, &keydata);
261 state = DATA; 293 state = DATA;
@@ -271,13 +303,13 @@ lkey: switch(command) {
271 break; 303 break;
272 case REMOVE: 304 case REMOVE:
273 rem(dbp, &key); 305 rem(dbp, &key);
274 if (type != DB_RECNO) 306 if ((type != DB_RECNO) && (flags != R_CURSOR))
275 free(key.data); 307 free(key.data);
276 state = COMMAND; 308 state = COMMAND;
277 break; 309 break;
278 case SEQ: 310 case SEQ:
279 seq(dbp, &key); 311 seq(dbp, &key);
280 if (type != DB_RECNO) 312 if ((type != DB_RECNO) && (flags != R_CURSOR))
281 free(key.data); 313 free(key.data);
282 state = COMMAND; 314 state = COMMAND;
283 break; 315 break;
@@ -291,11 +323,15 @@ lkey: switch(command) {
291 break; 323 break;
292 default: 324 default:
293 err("line %lu: %s: unknown command character", 325 err("line %lu: %s: unknown command character",
294 p, lineno); 326 lineno, p);
295 } 327 }
296 } 328 }
297#ifdef STATISTICS 329#ifdef STATISTICS
298 if (type == DB_BTREE) 330 /*
331 * -l must be used (DB_LOCK must be set) for this to be
332 * used, otherwise a page will be locked and it will fail.
333 */
334 if (type == DB_BTREE && oflags & DB_LOCK)
299 __bt_stat(dbp); 335 __bt_stat(dbp);
300#endif 336#endif
301 if (dbp->close(dbp)) 337 if (dbp->close(dbp))
@@ -305,7 +341,6 @@ lkey: switch(command) {
305} 341}
306 342
307#define NOOVERWRITE "put failed, would overwrite key\n" 343#define NOOVERWRITE "put failed, would overwrite key\n"
308#define NOSUCHKEY "get failed, no such key\n"
309 344
310void 345void
311compare(db1, db2) 346compare(db1, db2)
@@ -334,17 +369,23 @@ get(dbp, kp)
334{ 369{
335 DBT data; 370 DBT data;
336 371
337 switch(dbp->get(dbp, kp, &data, flags)) { 372 switch (dbp->get(dbp, kp, &data, flags)) {
338 case 0: 373 case 0:
339 (void)write(ofd, data.data, data.size); 374 (void)write(ofd, data.data, data.size);
375 if (ofd == STDOUT_FILENO)
376 (void)write(ofd, "\n", 1);
340 break; 377 break;
341 case -1: 378 case -1:
342 err("line %lu: get: %s", lineno, strerror(errno)); 379 err("line %lu: get: %s", lineno, strerror(errno));
343 /* NOTREACHED */ 380 /* NOTREACHED */
344 case 1: 381 case 1:
345 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 382#define NOSUCHKEY "get failed, no such key\n"
346 (void)fprintf(stderr, "%d: %.*s: %s\n", 383 if (ofd != STDOUT_FILENO)
347 lineno, kp->size, kp->data, NOSUCHKEY); 384 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
385 else
386 (void)fprintf(stderr, "%d: %.*s: %s",
387 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
388#undef NOSUCHKEY
348 break; 389 break;
349 } 390 }
350} 391}
@@ -354,14 +395,14 @@ getdata(dbp, kp, dp)
354 DB *dbp; 395 DB *dbp;
355 DBT *kp, *dp; 396 DBT *kp, *dp;
356{ 397{
357 switch(dbp->get(dbp, kp, dp, flags)) { 398 switch (dbp->get(dbp, kp, dp, flags)) {
358 case 0: 399 case 0:
359 return; 400 return;
360 case -1: 401 case -1:
361 err("line %lu: getdata: %s", lineno, strerror(errno)); 402 err("line %lu: getdata: %s", lineno, strerror(errno));
362 /* NOTREACHED */ 403 /* NOTREACHED */
363 case 1: 404 case 1:
364 err("line %lu: get failed, no such key", lineno); 405 err("line %lu: getdata failed, no such key", lineno);
365 /* NOTREACHED */ 406 /* NOTREACHED */
366 } 407 }
367} 408}
@@ -371,7 +412,7 @@ put(dbp, kp, dp)
371 DB *dbp; 412 DB *dbp;
372 DBT *kp, *dp; 413 DBT *kp, *dp;
373{ 414{
374 switch(dbp->put(dbp, kp, dp, flags)) { 415 switch (dbp->put(dbp, kp, dp, flags)) {
375 case 0: 416 case 0:
376 break; 417 break;
377 case -1: 418 case -1:
@@ -388,34 +429,67 @@ rem(dbp, kp)
388 DB *dbp; 429 DB *dbp;
389 DBT *kp; 430 DBT *kp;
390{ 431{
391 switch(dbp->del(dbp, kp, flags)) { 432 switch (dbp->del(dbp, kp, flags)) {
392 case 0: 433 case 0:
393 break; 434 break;
394 case -1: 435 case -1:
395 err("line %lu: get: %s", lineno, strerror(errno)); 436 err("line %lu: rem: %s", lineno, strerror(errno));
396 /* NOTREACHED */ 437 /* NOTREACHED */
397 case 1: 438 case 1:
398 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 439#define NOSUCHKEY "rem failed, no such key\n"
440 if (ofd != STDOUT_FILENO)
441 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
442 else if (flags != R_CURSOR)
443 (void)fprintf(stderr, "%d: %.*s: %s",
444 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
445 else
446 (void)fprintf(stderr,
447 "%d: rem of cursor failed\n", lineno);
448#undef NOSUCHKEY
399 break; 449 break;
400 } 450 }
401} 451}
402 452
403void 453void
454synk(dbp)
455 DB *dbp;
456{
457 switch (dbp->sync(dbp, flags)) {
458 case 0:
459 break;
460 case -1:
461 err("line %lu: synk: %s", lineno, strerror(errno));
462 /* NOTREACHED */
463 }
464}
465
466void
404seq(dbp, kp) 467seq(dbp, kp)
405 DB *dbp; 468 DB *dbp;
406 DBT *kp; 469 DBT *kp;
407{ 470{
408 DBT data; 471 DBT data;
409 472
410 switch(dbp->seq(dbp, kp, &data, flags)) { 473 switch (dbp->seq(dbp, kp, &data, flags)) {
411 case 0: 474 case 0:
412 (void)write(ofd, data.data, data.size); 475 (void)write(ofd, data.data, data.size);
476 if (ofd == STDOUT_FILENO)
477 (void)write(ofd, "\n", 1);
413 break; 478 break;
414 case -1: 479 case -1:
415 err("line %lu: seq: %s", lineno, strerror(errno)); 480 err("line %lu: seq: %s", lineno, strerror(errno));
416 /* NOTREACHED */ 481 /* NOTREACHED */
417 case 1: 482 case 1:
418 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1); 483#define NOSUCHKEY "seq failed, no such key\n"
484 if (ofd != STDOUT_FILENO)
485 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
486 else if (flags == R_CURSOR)
487 (void)fprintf(stderr, "%d: %.*s: %s",
488 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
489 else
490 (void)fprintf(stderr,
491 "%d: seq (%s) failed\n", lineno, sflags(flags));
492#undef NOSUCHKEY
419 break; 493 break;
420 } 494 }
421} 495}
@@ -436,9 +510,11 @@ dump(dbp, rev)
436 nflags = R_NEXT; 510 nflags = R_NEXT;
437 } 511 }
438 for (;; flags = nflags) 512 for (;; flags = nflags)
439 switch(dbp->seq(dbp, &key, &data, flags)) { 513 switch (dbp->seq(dbp, &key, &data, flags)) {
440 case 0: 514 case 0:
441 (void)write(ofd, data.data, data.size); 515 (void)write(ofd, data.data, data.size);
516 if (ofd == STDOUT_FILENO)
517 (void)write(ofd, "\n", 1);
442 break; 518 break;
443 case 1: 519 case 1:
444 goto done; 520 goto done;
@@ -454,34 +530,45 @@ u_int
454setflags(s) 530setflags(s)
455 char *s; 531 char *s;
456{ 532{
457 char *p, *index(); 533 char *p;
458 534
459 for (; isspace(*s); ++s); 535 for (; isspace(*s); ++s);
460 if (*s == '\n') 536 if (*s == '\n' || *s == '\0')
461 return (0); 537 return (0);
462 if ((p = index(s, '\n')) != NULL) 538 if ((p = strchr(s, '\n')) != NULL)
463 *p = '\0'; 539 *p = '\0';
464 if (!strcmp(s, "R_CURSOR")) 540 if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
465 return (R_CURSOR); 541 if (!strcmp(s, "R_FIRST")) return (R_FIRST);
466 if (!strcmp(s, "R_FIRST")) 542 if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
467 return (R_FIRST); 543 if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
468 if (!strcmp(s, "R_IAFTER")) 544 if (!strcmp(s, "R_LAST")) return (R_LAST);
469 return (R_IAFTER); 545 if (!strcmp(s, "R_NEXT")) return (R_NEXT);
470 if (!strcmp(s, "R_IBEFORE")) 546 if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
471 return (R_IBEFORE); 547 if (!strcmp(s, "R_PREV")) return (R_PREV);
472 if (!strcmp(s, "R_LAST")) 548 if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
473 return (R_LAST); 549
474 if (!strcmp(s, "R_NEXT"))
475 return (R_NEXT);
476 if (!strcmp(s, "R_NOOVERWRITE"))
477 return (R_NOOVERWRITE);
478 if (!strcmp(s, "R_PREV"))
479 return (R_PREV);
480 if (!strcmp(s, "R_SETCURSOR"))
481 return (R_SETCURSOR);
482 err("line %lu: %s: unknown flag", lineno, s); 550 err("line %lu: %s: unknown flag", lineno, s);
483 /* NOTREACHED */ 551 /* NOTREACHED */
484} 552}
553
554char *
555sflags(flags)
556 int flags;
557{
558 switch (flags) {
559 case R_CURSOR: return ("R_CURSOR");
560 case R_FIRST: return ("R_FIRST");
561 case R_IAFTER: return ("R_IAFTER");
562 case R_IBEFORE: return ("R_IBEFORE");
563 case R_LAST: return ("R_LAST");
564 case R_NEXT: return ("R_NEXT");
565 case R_NOOVERWRITE: return ("R_NOOVERWRITE");
566 case R_PREV: return ("R_PREV");
567 case R_SETCURSOR: return ("R_SETCURSOR");
568 }
569
570 return ("UNKNOWN!");
571}
485 572
486DBTYPE 573DBTYPE
487dbtype(s) 574dbtype(s)
@@ -505,15 +592,15 @@ setinfo(type, s)
505 static BTREEINFO ib; 592 static BTREEINFO ib;
506 static HASHINFO ih; 593 static HASHINFO ih;
507 static RECNOINFO rh; 594 static RECNOINFO rh;
508 char *eq, *index(); 595 char *eq;
509 596
510 if ((eq = index(s, '=')) == NULL) 597 if ((eq = strchr(s, '=')) == NULL)
511 err("%s: illegal structure set statement", s); 598 err("%s: illegal structure set statement", s);
512 *eq++ = '\0'; 599 *eq++ = '\0';
513 if (!isdigit(*eq)) 600 if (!isdigit(*eq))
514 err("%s: structure set statement must be a number", s); 601 err("%s: structure set statement must be a number", s);
515 602
516 switch(type) { 603 switch (type) {
517 case DB_BTREE: 604 case DB_BTREE:
518 if (!strcmp("flags", s)) { 605 if (!strcmp("flags", s)) {
519 ib.flags = atoi(eq); 606 ib.flags = atoi(eq);
@@ -601,10 +688,10 @@ rfile(name, lenp)
601 struct stat sb; 688 struct stat sb;
602 void *p; 689 void *p;
603 int fd; 690 int fd;
604 char *np, *index(); 691 char *np;
605 692
606 for (; isspace(*name); ++name); 693 for (; isspace(*name); ++name);
607 if ((np = index(name, '\n')) != NULL) 694 if ((np = strchr(name, '\n')) != NULL)
608 *np = '\0'; 695 *np = '\0';
609 if ((fd = open(name, O_RDONLY, 0)) < 0 || 696 if ((fd = open(name, O_RDONLY, 0)) < 0 ||
610 fstat(fd, &sb)) 697 fstat(fd, &sb))
@@ -642,14 +729,14 @@ usage()
642 exit(1); 729 exit(1);
643} 730}
644 731
645#if __STDC__ 732#ifdef __STDC__
646#include <stdarg.h> 733#include <stdarg.h>
647#else 734#else
648#include <varargs.h> 735#include <varargs.h>
649#endif 736#endif
650 737
651void 738void
652#if __STDC__ 739#ifdef __STDC__
653err(const char *fmt, ...) 740err(const char *fmt, ...)
654#else 741#else
655err(fmt, va_alist) 742err(fmt, va_alist)
@@ -658,7 +745,7 @@ err(fmt, va_alist)
658#endif 745#endif
659{ 746{
660 va_list ap; 747 va_list ap;
661#if __STDC__ 748#ifdef __STDC__
662 va_start(ap, fmt); 749 va_start(ap, fmt);
663#else 750#else
664 va_start(ap); 751 va_start(ap);
diff --git a/src/regress/lib/libc/db/run.test b/src/regress/lib/libc/db/run.test
index 4073310a31..acbd3f49e1 100644
--- a/src/regress/lib/libc/db/run.test
+++ b/src/regress/lib/libc/db/run.test
@@ -1,19 +1,27 @@
1#!/bin/sh - 1#!/bin/sh -
2# $NetBSD: run.test,v 1.7 1995/04/20 22:39:27 cgd Exp $
3# 2#
4# @(#)run.test 8.8 (Berkeley) 6/16/94 3# $NetBSD: run.test,v 1.8 1996/05/03 21:57:51 cgd Exp $
4# @(#)run.test 8.10 (Berkeley) 7/26/94
5# 5#
6 6
7# db regression tests 7# db regression tests
8main() 8main()
9{ 9{
10 10
11DICT=/usr/share/dict/web2 11 PROG=./dbtest
12PROG=./dbtest 12 TMP1=t1
13TMP1=t1 13 TMP2=t2
14TMP2=t2 14 TMP3=t3
15TMP3=t3
16 15
16 if [ -f /usr/share/dict/words ]; then
17 DICT=/usr/share/dict/words
18 elif [ -f /usr/dict/words ]; then
19 DICT=/usr/dict/words
20 else
21 echo 'run.test: no dictionary'
22 exit 1
23 fi
24
17 if [ $# -eq 0 ]; then 25 if [ $# -eq 0 ]; then
18 for t in 1 2 3 4 5 6 7 8 9 10 11 12 13 20; do 26 for t in 1 2 3 4 5 6 7 8 9 10 11 12 13 20; do
19 test$t 27 test$t
@@ -345,7 +353,7 @@ test7()
345 for (i = 1; i <= 120; ++i) 353 for (i = 1; i <= 120; ++i)
346 printf("%05d: input key %d: %s\n", i, i, $0); 354 printf("%05d: input key %d: %s\n", i, i, $0);
347 printf("%05d: input key %d: %s\n", 120, 120, $0); 355 printf("%05d: input key %d: %s\n", 120, 120, $0);
348 printf("get failed, no such key\n"); 356 printf("seq failed, no such key\n");
349 printf("%05d: input key %d: %s\n", 1, 1, $0); 357 printf("%05d: input key %d: %s\n", 1, 1, $0);
350 printf("%05d: input key %d: %s\n", 2, 2, $0); 358 printf("%05d: input key %d: %s\n", 2, 2, $0);
351 exit; 359 exit;
@@ -364,10 +372,10 @@ test7()
364 for (i = 1; i <= 120; ++i) 372 for (i = 1; i <= 120; ++i)
365 printf("s\n"); 373 printf("s\n");
366 printf("fR_CURSOR\ns\nk120\n"); 374 printf("fR_CURSOR\ns\nk120\n");
367 printf("r\nk120\n"); 375 printf("r\n");
368 printf("fR_NEXT\ns\n"); 376 printf("fR_NEXT\ns\n");
369 printf("fR_CURSOR\ns\nk1\n"); 377 printf("fR_CURSOR\ns\nk1\n");
370 printf("r\nk1\n"); 378 printf("r\n");
371 printf("fR_FIRST\ns\n"); 379 printf("fR_FIRST\ns\n");
372 }' > $TMP2 380 }' > $TMP2
373 $PROG -o $TMP3 recno $TMP2 381 $PROG -o $TMP3 recno $TMP2
@@ -397,8 +405,6 @@ test8()
397 printf("e\t%d of 10 \n", i); 405 printf("e\t%d of 10 \n", i);
398 printf("r\nkkey1\nr\nkkey2\n"); 406 printf("r\nkkey1\nr\nkkey2\n");
399 } 407 }
400 printf("e\n");
401 printf("eend of test8 run\n");
402 }' > $TMP1 408 }' > $TMP1
403 $PROG btree $TMP1 409 $PROG btree $TMP1
404# $PROG hash $TMP1 410# $PROG hash $TMP1
@@ -459,7 +465,7 @@ test10()
459 printf("p\nk%d\nd%s\n", ++i, $0); 465 printf("p\nk%d\nd%s\n", ++i, $0);
460 } 466 }
461 END { 467 END {
462 printf("fR_CURSOR\nr\nk1\n"); 468 printf("fR_CURSOR\nr\n");
463 printf("eR_CURSOR SHOULD HAVE FAILED\n"); 469 printf("eR_CURSOR SHOULD HAVE FAILED\n");
464 }' > $TMP2 470 }' > $TMP2
465 $PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1 471 $PROG -o $TMP3 $type $TMP2 > /dev/null 2>&1
@@ -573,7 +579,8 @@ test13()
573 echo g 579 echo g
574 echo k$i 580 echo k$i
575 done > $TMP2 581 done > $TMP2
576 $PROG -ilorder=$order -f byte.file -o $TMP3 $type $TMP2 582 $PROG -s \
583 -ilorder=$order -f byte.file -o $TMP3 $type $TMP2
577 if (cmp -s $TMP1 $TMP3) ; then : 584 if (cmp -s $TMP1 $TMP3) ; then :
578 else 585 else
579 echo "test13: $type/$order get failed" 586 echo "test13: $type/$order get failed"
diff --git a/src/regress/lib/libc/ieeefp/Makefile b/src/regress/lib/libc/ieeefp/Makefile
index 4e2e517b03..0f453a31e9 100644
--- a/src/regress/lib/libc/ieeefp/Makefile
+++ b/src/regress/lib/libc/ieeefp/Makefile
@@ -1,10 +1,6 @@
1# $NetBSD: Makefile,v 1.4 1995/10/03 21:59:36 phil Exp $ 1# $NetBSD: Makefile,v 1.5 1996/04/09 16:54:18 phil Exp $
2 2
3.if ${MACHINE} == "pc532"
4SUBDIR+= round
5.else
6SUBDIR+= except round 3SUBDIR+= except round
7.endif
8 4
9regress: _SUBDIRUSE 5regress: _SUBDIRUSE
10 6
diff --git a/src/regress/lib/libc/ieeefp/except/except.c b/src/regress/lib/libc/ieeefp/except/except.c
index 0ffdcdd468..a2a107a788 100644
--- a/src/regress/lib/libc/ieeefp/except/except.c
+++ b/src/regress/lib/libc/ieeefp/except/except.c
@@ -12,6 +12,7 @@ static volatile const double zero = 0.0;
12static volatile const double huge = DBL_MAX; 12static volatile const double huge = DBL_MAX;
13static volatile const double tiny = DBL_MIN; 13static volatile const double tiny = DBL_MIN;
14 14
15int
15main() 16main()
16{ 17{
17 volatile double x; 18 volatile double x;
diff --git a/src/regress/lib/libc/regex/debug.c b/src/regress/lib/libc/regex/debug.c
index 861f550611..41cd4a557d 100644
--- a/src/regress/lib/libc/regex/debug.c
+++ b/src/regress/lib/libc/regex/debug.c
@@ -218,7 +218,7 @@ FILE *d;
218 fprintf(d, ">"); 218 fprintf(d, ">");
219 break; 219 break;
220 default: 220 default:
221 fprintf(d, "!%d(%d)!", OP(*s), opnd); 221 fprintf(d, "!%ld(%ld)!", (long)OP(*s), (long)opnd);
222 break; 222 break;
223 } 223 }
224 if (!done) 224 if (!done)
diff --git a/src/regress/lib/libc/regex/main.c b/src/regress/lib/libc/regex/main.c
index 8d88a8b9b8..6e63ffc235 100644
--- a/src/regress/lib/libc/regex/main.c
+++ b/src/regress/lib/libc/regex/main.c
@@ -1,10 +1,13 @@
1/* $OpenBSD: main.c,v 1.3 1997/01/15 23:41:07 millert Exp $ */
1/* $NetBSD: main.c,v 1.2 1995/04/20 22:39:51 cgd Exp $ */ 2/* $NetBSD: main.c,v 1.2 1995/04/20 22:39:51 cgd Exp $ */
2 3
3#include <stdio.h> 4#include <stdio.h>
5#include <stdlib.h>
4#include <string.h> 6#include <string.h>
5#include <sys/types.h> 7#include <sys/types.h>
6#include <regex.h> 8#include <regex.h>
7#include <assert.h> 9#include <assert.h>
10#include <unistd.h>
8 11
9#include "main.ih" 12#include "main.ih"
10 13
@@ -25,6 +28,7 @@ extern void regprint();
25/* 28/*
26 - main - do the simple case, hand off to regress() for regression 29 - main - do the simple case, hand off to regress() for regression
27 */ 30 */
31int
28main(argc, argv) 32main(argc, argv)
29int argc; 33int argc;
30char *argv[]; 34char *argv[];
@@ -43,7 +47,7 @@ char *argv[];
43 47
44 progname = argv[0]; 48 progname = argv[0];
45 49
46 while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF) 50 while ((c = getopt(argc, argv, "c:e:S:E:x")) != -1)
47 switch (c) { 51 switch (c) {
48 case 'c': /* compile options */ 52 case 'c': /* compile options */
49 copts = options('c', optarg); 53 copts = options('c', optarg);
@@ -102,10 +106,10 @@ char *argv[];
102 exit(status); 106 exit(status);
103 } 107 }
104 if (!(copts&REG_NOSUB)) { 108 if (!(copts&REG_NOSUB)) {
105 len = (int)(subs[0].rm_eo - subs[0].rm_so); 109 len = (size_t)(subs[0].rm_eo - subs[0].rm_so);
106 if (subs[0].rm_so != -1) { 110 if (subs[0].rm_so != -1) {
107 if (len != 0) 111 if (len != 0)
108 printf("match `%.*s'\n", len, 112 printf("match `%.*s'\n", (int)len,
109 argv[optind] + subs[0].rm_so); 113 argv[optind] + subs[0].rm_so);
110 else 114 else
111 printf("match `'@%.1s\n", 115 printf("match `'@%.1s\n",
@@ -501,7 +505,6 @@ efind(name)
501char *name; 505char *name;
502{ 506{
503 static char efbuf[100]; 507 static char efbuf[100];
504 size_t n;
505 regex_t re; 508 regex_t re;
506 509
507 sprintf(efbuf, "REG_%s", name); 510 sprintf(efbuf, "REG_%s", name);
diff --git a/src/regress/lib/libc/setjmp/jmptest.c b/src/regress/lib/libc/setjmp/jmptest.c
index f2cecc9178..16a482640a 100644
--- a/src/regress/lib/libc/setjmp/jmptest.c
+++ b/src/regress/lib/libc/setjmp/jmptest.c
@@ -32,6 +32,7 @@
32 */ 32 */
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35#include <err.h>
35#include <setjmp.h> 36#include <setjmp.h>
36#include <signal.h> 37#include <signal.h>
37#include <stdio.h> 38#include <stdio.h>
@@ -101,7 +102,7 @@ main(argc, argv)
101#endif 102#endif
102 103
103 sa.sa_handler = aborthandler; 104 sa.sa_handler = aborthandler;
104 sa.sa_mask = 0; 105 sigemptyset(&sa.sa_mask);
105 sa.sa_flags = 0; 106 sa.sa_flags = 0;
106 if (sigaction(SIGABRT, &sa, NULL) == -1) 107 if (sigaction(SIGABRT, &sa, NULL) == -1)
107 err(1, "sigaction failed"); 108 err(1, "sigaction failed");
diff --git a/src/usr.bin/nc/Makefile b/src/usr.bin/nc/Makefile
new file mode 100644
index 0000000000..086a9e5ee8
--- /dev/null
+++ b/src/usr.bin/nc/Makefile
@@ -0,0 +1,7 @@
1# $OpenBSD: Makefile,v 1.2 1997/09/21 11:50:13 deraadt Exp $
2
3CFLAGS+= -DTELNET
4PROG= nc
5SRCS= netcat.c
6
7.include <bsd.prog.mk>
diff --git a/src/usr.bin/nc/README b/src/usr.bin/nc/README
new file mode 100644
index 0000000000..4235bc41ac
--- /dev/null
+++ b/src/usr.bin/nc/README
@@ -0,0 +1,946 @@
1Netcat 1.10
2=========== /\_/\
3 / 0 0 \
4Netcat is a simple Unix utility which reads and writes data ====v====
5across network connections, using TCP or UDP protocol. \ W /
6It is designed to be a reliable "back-end" tool that can | | _
7be used directly or easily driven by other programs and / ___ \ /
8scripts. At the same time, it is a feature-rich network / / \ \ |
9debugging and exploration tool, since it can create almost (((-----)))-'
10any kind of connection you would need and has several /
11interesting built-in capabilities. Netcat, or "nc" as the ( ___
12actual program is named, should have been supplied long ago \__.=|___E
13as another one of those cryptic but standard Unix tools. /
14
15In the simplest usage, "nc host port" creates a TCP connection to the given
16port on the given target host. Your standard input is then sent to the host,
17and anything that comes back across the connection is sent to your standard
18output. This continues indefinitely, until the network side of the connection
19shuts down. Note that this behavior is different from most other applications
20which shut everything down and exit after an end-of-file on the standard input.
21
22Netcat can also function as a server, by listening for inbound connections
23on arbitrary ports and then doing the same reading and writing. With minor
24limitations, netcat doesn't really care if it runs in "client" or "server"
25mode -- it still shovels data back and forth until there isn't any more left.
26In either mode, shutdown can be forced after a configurable time of inactivity
27on the network side.
28
29And it can do this via UDP too, so netcat is possibly the "udp telnet-like"
30application you always wanted for testing your UDP-mode servers. UDP, as the
31"U" implies, gives less reliable data transmission than TCP connections and
32some systems may have trouble sending large amounts of data that way, but it's
33still a useful capability to have.
34
35You may be asking "why not just use telnet to connect to arbitrary ports?"
36Valid question, and here are some reasons. Telnet has the "standard input
37EOF" problem, so one must introduce calculated delays in driving scripts to
38allow network output to finish. This is the main reason netcat stays running
39until the *network* side closes. Telnet also will not transfer arbitrary
40binary data, because certain characters are interpreted as telnet options and
41are thus removed from the data stream. Telnet also emits some of its
42diagnostic messages to standard output, where netcat keeps such things
43religiously separated from its *output* and will never modify any of the real
44data in transit unless you *really* want it to. And of course telnet is
45incapable of listening for inbound connections, or using UDP instead. Netcat
46doesn't have any of these limitations, is much smaller and faster than telnet,
47and has many other advantages.
48
49Some of netcat's major features are:
50
51 Outbound or inbound connections, TCP or UDP, to or from any ports
52 Full DNS forward/reverse checking, with appropriate warnings
53 Ability to use any local source port
54 Ability to use any locally-configured network source address
55 Built-in port-scanning capabilities, with randomizer
56 Built-in loose source-routing capability
57 Can read command line arguments from standard input
58 Slow-send mode, one line every N seconds
59 Hex dump of transmitted and received data
60 Optional ability to let another program service established connections
61 Optional telnet-options responder
62
63Efforts have been made to have netcat "do the right thing" in all its various
64modes. If you believe that it is doing the wrong thing under whatever
65circumstances, please notify me and tell me how you think it should behave.
66If netcat is not able to do some task you think up, minor tweaks to the code
67will probably fix that. It provides a basic and easily-modified template for
68writing other network applications, and I certainly encourage people to make
69custom mods and send in any improvements they make to it. This is the second
70release; the overall differences from 1.00 are relatively minor and have mostly
71to do with portability and bugfixes. Many people provided greatly appreciated
72fixes and comments on the 1.00 release. Continued feedback from the Internet
73community is always welcome!
74
75Netcat is entirely my own creation, although plenty of other code was used as
76examples. It is freely given away to the Internet community in the hope that
77it will be useful, with no restrictions except giving credit where it is due.
78No GPLs, Berkeley copyrights or any of that nonsense. The author assumes NO
79responsibility for how anyone uses it. If netcat makes you rich somehow and
80you're feeling generous, mail me a check. If you are affiliated in any way
81with Microsoft Network, get a life. Always ski in control. Comments,
82questions, and patches to hobbit@avian.org.
83
84Building
85========
86
87Compiling is fairly straightforward. Examine the Makefile for a SYSTYPE that
88matches yours, and do "make <systype>". The executable "nc" should appear.
89If there is no relevant SYSTYPE section, try "generic". If you create new
90sections for generic.h and Makefile to support another platform, please follow
91the given format and mail back the diffs.
92
93There are a couple of other settable #defines in netcat.c, which you can
94include as DFLAGS="-DTHIS -DTHAT" to your "make" invocation without having to
95edit the Makefile. See the following discussions for what they are and do.
96
97If you want to link against the resolver library on SunOS [recommended] and
98you have BIND 4.9.x, you may need to change XLIBS=-lresolv in the Makefile to
99XLIBS="-lresolv -l44bsd".
100
101Linux sys/time.h does not really support presetting of FD_SETSIZE; a harmless
102warning is issued.
103
104Some systems may warn about pointer types for signal(). No problem, though.
105
106Exploration of features
107=======================
108
109Where to begin? Netcat is at the same time so simple and versatile, it's like
110trying to describe everything you can do with your Swiss Army knife. This will
111go over the basics; you should also read the usage examples and notes later on
112which may give you even more ideas about what this sort of tool is good for.
113
114If no command arguments are given at all, netcat asks for them, reads a line
115from standard input, and breaks it up into arguments internally. This can be
116useful when driving netcat from certain types of scripts, with the side effect
117of hiding your command line arguments from "ps" displays.
118
119The host argument can be a name or IP address. If -n is specified, netcat
120will only accept numeric IP addresses and do no DNS lookups for anything. If
121-n is not given and -v is turned on, netcat will do a full forward and reverse
122name and address lookup for the host, and warn you about the all-too-common
123problem of mismatched names in the DNS. This often takes a little longer for
124connection setup, but is useful to know about. There are circumstances under
125which this can *save* time, such as when you want to know the name for some IP
126address and also connect there. Netcat will just tell you all about it, saving
127the manual steps of looking up the hostname yourself. Normally mismatch-
128checking is case-insensitive per the DNS spec, but you can define ANAL at
129compile time to make it case-sensitive -- sometimes useful for uncovering minor
130errors in your own DNS files while poking around your networks.
131
132A port argument is required for outbound connections, and can be numeric or a
133name as listed in /etc/services. If -n is specified, only numeric arguments
134are valid. Special syntax and/or more than one port argument cause different
135behavior -- see details below about port-scanning.
136
137The -v switch controls the verbosity level of messages sent to standard error.
138You will probably want to run netcat most of the time with -v turned on, so you
139can see info about the connections it is trying to make. You will probably
140also want to give a smallish -w argument, which limits the time spent trying to
141make a connection. I usually alias "nc" to "nc -v -w 3", which makes it
142function just about the same for things I would otherwise use telnet to do.
143The timeout is easily changed by a subsequent -w argument which overrides the
144earlier one. Specifying -v more than once makes diagnostic output MORE
145verbose. If -v is not specified at all, netcat silently does its work unless
146some error happens, whereupon it describes the error and exits with a nonzero
147status. Refused network connections are generally NOT considered to be errors,
148unless you only asked for a single TCP port and it was refused.
149
150Note that -w also sets the network inactivity timeout. This does not have any
151effect until standard input closes, but then if nothing further arrives from
152the network in the next <timeout> seconds, netcat tries to read the net once
153more for good measure, and then closes and exits. There are a lot of network
154services now that accept a small amount of input and return a large amount of
155output, such as Gopher and Web servers, which is the main reason netcat was
156written to "block" on the network staying open rather than standard input.
157Handling the timeout this way gives uniform behavior with network servers that
158*don't* close by themselves until told to.
159
160UDP connections are opened instead of TCP when -u is specified. These aren't
161really "connections" per se since UDP is a connectionless protocol, although
162netcat does internally use the "connected UDP socket" mechanism that most
163kernels support. Although netcat claims that an outgoing UDP connection is
164"open" immediately, no data is sent until something is read from standard
165input. Only thereafter is it possible to determine whether there really is a
166UDP server on the other end, and often you just can't tell. Most UDP protocols
167use timeouts and retries to do their thing and in many cases won't bother
168answering at all, so you should specify a timeout and hope for the best. You
169will get more out of UDP connections if standard input is fed from a source
170of data that looks like various kinds of server requests.
171
172To obtain a hex dump file of the data sent either way, use "-o logfile". The
173dump lines begin with "<" or ">" to respectively indicate "from the net" or
174"to the net", and contain the total count per direction, and hex and ascii
175representations of the traffic. Capturing a hex dump naturally slows netcat
176down a bit, so don't use it where speed is critical.
177
178Netcat can bind to any local port, subject to privilege restrictions and ports
179that are already in use. It is also possible to use a specific local network
180source address if it is that of a network interface on your machine. [Note:
181this does not work correctly on all platforms.] Use "-p portarg" to grab a
182specific local port, and "-s ip-addr" or "-s name" to have that be your source
183IP address. This is often referred to as "anchoring the socket". Root users
184can grab any unused source port including the "reserved" ones less than 1024.
185Absence of -p will bind to whatever unused port the system gives you, just like
186any other normal client connection, unless you use -r [see below].
187
188Listen mode will cause netcat to wait for an inbound connection, and then the
189same data transfer happens. Thus, you can do "nc -l -p 1234 < filename" and
190when someone else connects to your port 1234, the file is sent to them whether
191they wanted it or not. Listen mode is generally used along with a local port
192argument -- this is required for UDP mode, while TCP mode can have the system
193assign one and tell you what it is if -v is turned on. If you specify a target
194host and optional port in listen mode, netcat will accept an inbound connection
195only from that host and if you specify one, only from that foreign source port.
196In verbose mode you'll be informed about the inbound connection, including what
197address and port it came from, and since listening on "any" applies to several
198possibilities, which address it came *to* on your end. If the system supports
199IP socket options, netcat will attempt to retrieve any such options from an
200inbound connection and print them out in hex.
201
202If netcat is compiled with -DGAPING_SECURITY_HOLE, the -e argument specifies
203a program to exec after making or receiving a successful connection. In the
204listening mode, this works similarly to "inetd" but only for a single instance.
205Use with GREAT CARE. This piece of the code is normally not enabled; if you
206know what you're doing, have fun. This hack also works in UDP mode. Note that
207you can only supply -e with the name of the program, but no arguments. If you
208want to launch something with an argument list, write a two-line wrapper script
209or just use inetd like always.
210
211If netcat is compiled with -DTELNET, the -t argument enables it to respond
212to telnet option negotiation [always in the negative, i.e. DONT or WONT].
213This allows it to connect to a telnetd and get past the initial negotiation
214far enough to get a login prompt from the server. Since this feature has
215the potential to modify the data stream, it is not enabled by default. You
216have to understand why you might need this and turn on the #define yourself.
217
218Data from the network connection is always delivered to standard output as
219efficiently as possible, using large 8K reads and writes. Standard input is
220normally sent to the net the same way, but the -i switch specifies an "interval
221time" which slows this down considerably. Standard input is still read in
222large batches, but netcat then tries to find where line breaks exist and sends
223one line every interval time. Note that if standard input is a terminal, data
224is already read line by line, so unless you make the -i interval rather long,
225what you type will go out at a fairly normal rate. -i is really designed
226for use when you want to "measure out" what is read from files or pipes.
227
228Port-scanning is a popular method for exploring what's out there. Netcat
229accepts its commands with options first, then the target host, and everything
230thereafter is interpreted as port names or numbers, or ranges of ports in M-N
231syntax. CAVEAT: some port names in /etc/services contain hyphens -- netcat
232currently will not correctly parse those, so specify ranges using numbers if
233you can. If more than one port is thus specified, netcat connects to *all* of
234them, sending the same batch of data from standard input [up to 8K worth] to
235each one that is successfully connected to. Specifying multiple ports also
236suppresses diagnostic messages about refused connections, unless -v is
237specified twice for "more verbosity". This way you normally get notified only
238about genuinely open connections. Example: "nc -v -w 2 -z target 20-30" will
239try connecting to every port between 20 and 30 [inclusive] at the target, and
240will likely inform you about an FTP server, telnet server, and mailer along the
241way. The -z switch prevents sending any data to a TCP connection and very
242limited probe data to a UDP connection, and is thus useful as a fast scanning
243mode just to see what ports the target is listening on. To limit scanning
244speed if desired, -i will insert a delay between each port probe. There are
245some pitfalls with regard to UDP scanning, described later, but in general it
246works well.
247
248For each range of ports specified, scanning is normally done downward within
249that range. If the -r switch is used, scanning hops randomly around within
250that range and reports open ports as it finds them. [If you want them listed
251in order regardless, pipe standard error through "sort"...] In addition, if
252random mode is in effect, the local source ports are also randomized. This
253prevents netcat from exhibiting any kind of regular pattern in its scanning.
254You can exert fairly fine control over your scan by judicious use of -r and
255selected port ranges to cover. If you use -r for a single connection, the
256source port will have a random value above 8192, rather than the next one the
257kernel would have assigned you. Note that selecting a specific local port
258with -p overrides any local-port randomization.
259
260Many people are interested in testing network connectivity using IP source
261routing, even if it's only to make sure their own firewalls are blocking
262source-routed packets. On systems that support it, the -g switch can be used
263multiple times [up to 8] to construct a loose-source-routed path for your
264connection, and the -G argument positions the "hop pointer" within the list.
265If your network allows source-routed traffic in and out, you can test
266connectivity to your own services via remote points in the internet. Note that
267although newer BSD-flavor telnets also have source-routing capability, it isn't
268clearly documented and the command syntax is somewhat clumsy. Netcat's
269handling of "-g" is modeled after "traceroute".
270
271Netcat tries its best to behave just like "cat". It currently does nothing to
272terminal input modes, and does no end-of-line conversion. Standard input from
273a terminal is read line by line with normal editing characters in effect. You
274can freely suspend out of an interactive connection and resume. ^C or whatever
275your interrupt character is will make netcat close the network connection and
276exit. A switch to place the terminal in raw mode has been considered, but so
277far has not been necessary. You can send raw binary data by reading it out of
278a file or piping from another program, so more meaningful effort would be spent
279writing an appropriate front-end driver.
280
281Netcat is not an "arbitrary packet generator", but the ability to talk to raw
282sockets and/or nit/bpf/dlpi may appear at some point. Such things are clearly
283useful; I refer you to Darren Reed's excellent ip_filter package, which now
284includes a tool to construct and send raw packets with any contents you want.
285
286Example uses -- the light side
287==============================
288
289Again, this is a very partial list of possibilities, but it may get you to
290think up more applications for netcat. Driving netcat with simple shell or
291expect scripts is an easy and flexible way to do fairly complex tasks,
292especially if you're not into coding network tools in C. My coding isn't
293particularly strong either [although undoubtedly better after writing this
294thing!], so I tend to construct bare-metal tools like this that I can trivially
295plug into other applications. Netcat doubles as a teaching tool -- one can
296learn a great deal about more complex network protocols by trying to simulate
297them through raw connections!
298
299An example of netcat as a backend for something else is the shell-script
300Web browser, which simply asks for the relevant parts of a URL and pipes
301"GET /what/ever" into a netcat connection to the server. I used to do this
302with telnet, and had to use calculated sleep times and other stupidity to
303kludge around telnet's limitations. Netcat guarantees that I get the whole
304page, and since it transfers all the data unmodified, I can even pull down
305binary image files and display them elsewhere later. Some folks may find the
306idea of a shell-script web browser silly and strange, but it starts up and
307gets me my info a hell of a lot faster than a GUI browser and doesn't hide
308any contents of links and forms and such. This is included, as scripts/web,
309along with several other web-related examples.
310
311Netcat is an obvious replacement for telnet as a tool for talking to daemons.
312For example, it is easier to type "nc host 25", talk to someone's mailer, and
313just ^C out than having to type ^]c or QUIT as telnet would require you to do.
314You can quickly catalog the services on your network by telling netcat to
315connect to well-known services and collect greetings, or at least scan for open
316ports. You'll probably want to collect netcat's diagnostic messages in your
317output files, so be sure to include standard error in the output using
318`>& file' in *csh or `> file 2>&1' in bourne shell.
319
320A scanning example: "echo QUIT | nc -v -w 5 target 20-250 500-600 5990-7000"
321will inform you about a target's various well-known TCP servers, including
322r-services, X, IRC, and maybe a few you didn't expect. Sending in QUIT and
323using the timeout will almost guarantee that you see some kind of greeting or
324error from each service, which usually indicates what it is and what version.
325[Beware of the "chargen" port, though...] SATAN uses exactly this technique to
326collect host information, and indeed some of the ideas herein were taken from
327the SATAN backend tools. If you script this up to try every host in your
328subnet space and just let it run, you will not only see all the services,
329you'll find out about hosts that aren't correctly listed in your DNS. Then you
330can compare new snapshots against old snapshots to see changes. For going
331after particular services, a more intrusive example is in scripts/probe.
332
333Netcat can be used as a simple data transfer agent, and it doesn't really
334matter which end is the listener and which end is the client -- input at one
335side arrives at the other side as output. It is helpful to start the listener
336at the receiving side with no timeout specified, and then give the sending side
337a small timeout. That way the listener stays listening until you contact it,
338and after data stops flowing the client will time out, shut down, and take the
339listener with it. Unless the intervening network is fraught with problems,
340this should be completely reliable, and you can always increase the timeout. A
341typical example of something "rsh" is often used for: on one side,
342
343 nc -l -p 1234 | uncompress -c | tar xvfp -
344
345and then on the other side
346
347 tar cfp - /some/dir | compress -c | nc -w 3 othermachine 1234
348
349will transfer the contents of a directory from one machine to another, without
350having to worry about .rhosts files, user accounts, or inetd configurations
351at either end. Again, it matters not which is the listener or receiver; the
352"tarring" machine could just as easily be running the listener instead. One
353could conceivably use a scheme like this for backups, by having cron-jobs fire
354up listeners and backup handlers [which can be restricted to specific addresses
355and ports between each other] and pipe "dump" or "tar" on one machine to "dd
356of=/dev/tapedrive" on another as usual. Since netcat returns a nonzero exit
357status for a denied listener connection, scripts to handle such tasks could
358easily log and reject connect attempts from third parties, and then retry.
359
360Another simple data-transfer example: shipping things to a PC that doesn't have
361any network applications yet except a TCP stack and a web browser. Point the
362browser at an arbitrary port on a Unix server by telling it to download
363something like http://unixbox:4444/foo, and have a listener on the Unix side
364ready to ship out a file when the connect comes in. The browser may pervert
365binary data when told to save the URL, but you can dig the raw data out of
366the on-disk cache.
367
368If you build netcat with GAPING_SECURITY_HOLE defined, you can use it as an
369"inetd" substitute to test experimental network servers that would otherwise
370run under "inetd". A script or program will have its input and output hooked
371to the network the same way, perhaps sans some fancier signal handling. Given
372that most network services do not bind to a particular local address, whether
373they are under "inetd" or not, it is possible for netcat avoid the "address
374already in use" error by binding to a specific address. This lets you [as
375root, for low ports] place netcat "in the way" of a standard service, since
376inbound connections are generally sent to such specifically-bound listeners
377first and fall back to the ones bound to "any". This allows for a one-off
378experimental simulation of some service, without having to screw around with
379inetd.conf. Running with -v turned on and collecting a connection log from
380standard error is recommended.
381
382Netcat as well can make an outbound connection and then run a program or script
383on the originating end, with input and output connected to the same network
384port. This "inverse inetd" capability could enhance the backup-server concept
385described above or help facilitate things such as a "network dialback" concept.
386The possibilities are many and varied here; if such things are intended as
387security mechanisms, it may be best to modify netcat specifically for the
388purpose instead of wrapping such functions in scripts.
389
390Speaking of inetd, netcat will function perfectly well *under* inetd as a TCP
391connection redirector for inbound services, like a "plug-gw" without the
392authentication step. This is very useful for doing stuff like redirecting
393traffic through your firewall out to other places like web servers and mail
394hubs, while posing no risk to the firewall machine itself. Put netcat behind
395inetd and tcp_wrappers, perhaps thusly:
396
397 www stream tcp nowait nobody /etc/tcpd /bin/nc -w 3 realwww 80
398
399and you have a simple and effective "application relay" with access control
400and logging. Note use of the wait time as a "safety" in case realwww isn't
401reachable or the calling user aborts the connection -- otherwise the relay may
402hang there forever.
403
404You can use netcat to generate huge amounts of useless network data for
405various performance testing. For example, doing
406
407 yes AAAAAAAAAAAAAAAAAAAAAA | nc -v -v -l -p 2222 > /dev/null
408
409on one side and then hitting it with
410
411 yes BBBBBBBBBBBBBBBBBBBBBB | nc othermachine 2222 > /dev/null
412
413from another host will saturate your wires with A's and B's. The "very
414verbose" switch usage will tell you how many of each were sent and received
415after you interrupt either side. Using UDP mode produces tremendously MORE
416trash per unit time in the form of fragmented 8 Kbyte mobygrams -- enough to
417stress-test kernels and network interfaces. Firing random binary data into
418various network servers may help expose bugs in their input handling, which
419nowadays is a popular thing to explore. A simple example data-generator is
420given in data/data.c included in this package, along with a small collection
421of canned input files to generate various packet contents. This program is
422documented in its beginning comments, but of interest here is using "%r" to
423generate random bytes at well-chosen points in a data stream. If you can
424crash your daemon, you likely have a security problem.
425
426The hex dump feature may be useful for debugging odd network protocols,
427especially if you don't have any network monitoring equipment handy or aren't
428root where you'd need to run "tcpdump" or something. Bind a listening netcat
429to a local port, and have it run a script which in turn runs another netcat
430to the real service and captures the hex dump to a log file. This sets up a
431transparent relay between your local port and wherever the real service is.
432Be sure that the script-run netcat does *not* use -v, or the extra info it
433sends to standard error may confuse the protocol. Note also that you cannot
434have the "listen/exec" netcat do the data capture, since once the connection
435arrives it is no longer netcat that is running.
436
437Binding to an arbitrary local port allows you to simulate things like r-service
438clients, if you are root locally. For example, feeding "^@root^@joe^@pwd^@"
439[where ^@ is a null, and root/joe could be any other local/remote username
440pair] into a "rsh" or "rlogin" server, FROM your port 1023 for example,
441duplicates what the server expects to receive. Thus, you can test for insecure
442.rhosts files around your network without having to create new user accounts on
443your client machine. The program data/rservice.c can aid this process by
444constructing the "rcmd" protocol bytes. Doing this also prevents "rshd" from
445trying to create that separate standard-error socket and still gives you an
446input path, as opposed to the usual action of "rsh -n". Using netcat for
447things like this can be really useful sometimes, because rsh and rlogin
448generally want a host *name* as an argument and won't accept IP addresses. If
449your client-end DNS is hosed, as may be true when you're trying to extract
450backup sets on to a dumb client, "netcat -n" wins where normal rsh/rlogin is
451useless.
452
453If you are unsure that a remote syslogger is working, test it with netcat.
454Make a UDP connection to port 514 and type in "<0>message", which should
455correspond to "kern.emerg" and cause syslogd to scream into every file it has
456open [and possibly all over users' terminals]. You can tame this down by
457using a different number and use netcat inside routine scripts to send syslog
458messages to places that aren't configured in syslog.conf. For example,
459"echo '<38>message' | nc -w 1 -u loggerhost 514" should send to auth.notice
460on loggerhost. The exact number may vary; check against your syslog.h first.
461
462Netcat provides several ways for you to test your own packet filters. If you
463bind to a port normally protected against outside access and make a connection
464to somewhere outside your own network, the return traffic will be coming to
465your chosen port from the "outside" and should be blocked. TCP may get through
466if your filter passes all "ack syn", but it shouldn't be even doing that to low
467ports on your network. Remember to test with UDP traffic as well! If your
468filter passes at least outbound source-routed IP packets, bouncing a connection
469back to yourself via some gateway outside your network will create "incoming"
470traffic with your source address, which should get dropped by a correctly
471configured anti-spoofing filter. This is a "non-test" if you're also dropping
472source-routing, but it's good to be able to test for that too. Any packet
473filter worth its salt will be blocking source-routed packets in both
474directions, but you never know what interesting quirks you might turn up by
475playing around with source ports and addresses and watching the wires with a
476network monitor.
477
478You can use netcat to protect your own workstation's X server against outside
479access. X is stupid enough to listen for connections on "any" and never tell
480you when new connections arrive, which is one reason it is so vulnerable. Once
481you have all your various X windows up and running you can use netcat to bind
482just to your ethernet address and listen to port 6000. Any new connections
483from outside the machine will hit netcat instead your X server, and you get a
484log of who's trying. You can either tell netcat to drop the connection, or
485perhaps run another copy of itself to relay to your actual X server on
486"localhost". This may not work for dedicated X terminals, but it may be
487possible to authorize your X terminal only for its boot server, and run a relay
488netcat over on the server that will in turn talk to your X terminal. Since
489netcat only handles one listening connection per run, make sure that whatever
490way you rig it causes another one to run and listen on 6000 soon afterward, or
491your real X server will be reachable once again. A very minimal script just
492to protect yourself could be
493
494 while true ; do
495 nc -v -l -s <your-addr> -p 6000 localhost 2
496 done
497
498which causes netcat to accept and then close any inbound connection to your
499workstation's normal ethernet address, and another copy is immediately run by
500the script. Send standard error to a file for a log of connection attempts.
501If your system can't do the "specific bind" thing all is not lost; run your
502X server on display ":1" or port 6001, and netcat can still function as a probe
503alarm by listening on 6000.
504
505Does your shell-account provider allow personal Web pages, but not CGI scripts?
506You can have netcat listen on a particular port to execute a program or script
507of your choosing, and then just point to the port with a URL in your homepage.
508The listener could even exist on a completely different machine, avoiding the
509potential ire of the homepage-host administrators. Since the script will get
510the raw browser query as input it won't look like a typical CGI script, and
511since it's running under your UID you need to write it carefully. You may want
512to write a netcat-based script as a wrapper that reads a query and sets up
513environment variables for a regular CGI script. The possibilities for using
514netcat and scripts to handle Web stuff are almost endless. Again, see the
515examples under scripts/.
516
517Example uses -- the dark side
518=============================
519
520Equal time is deserved here, since a versatile tool like this can be useful
521to any Shade of Hat. I could use my Victorinox to either fix your car or
522disassemble it, right? You can clearly use something like netcat to attack
523or defend -- I don't try to govern anyone's social outlook, I just build tools.
524Regardless of your intentions, you should still be aware of these threats to
525your own systems.
526
527The first obvious thing is scanning someone *else's* network for vulnerable
528services. Files containing preconstructed data, be it exploratory or
529exploitive, can be fed in as standard input, including command-line arguments
530to netcat itself to keep "ps" ignorant of your doings. The more random the
531scanning, the less likelihood of detection by humans, scan-detectors, or
532dynamic filtering, and with -i you'll wait longer but avoid loading down the
533target's network. Some examples for crafting various standard UDP probes are
534given in data/*.d.
535
536Some configurations of packet filters attempt to solve the FTP-data problem by
537just allowing such connections from the outside. These come FROM port 20, TO
538high TCP ports inside -- if you locally bind to port 20, you may find yourself
539able to bypass filtering in some cases. Maybe not to low ports "inside", but
540perhaps to TCP NFS servers, X servers, Prospero, ciscos that listen on 200x
541and 400x... Similar bypassing may be possible for UDP [and maybe TCP too] if a
542connection comes from port 53; a filter may assume it's a nameserver response.
543
544Using -e in conjunction with binding to a specific address can enable "server
545takeover" by getting in ahead of the real ones, whereupon you can snarf data
546sent in and feed your own back out. At the very least you can log a hex dump
547of someone else's session. If you are root, you can certainly use -s and -e to
548run various hacked daemons without having to touch inetd.conf or the real
549daemons themselves. You may not always have the root access to deal with low
550ports, but what if you are on a machine that also happens to be an NFS server?
551You might be able to collect some interesting things from port 2049, including
552local file handles. There are several other servers that run on high ports
553that are likely candidates for takeover, including many of the RPC services on
554some platforms [yppasswdd, anyone?]. Kerberos tickets, X cookies, and IRC
555traffic also come to mind. RADIUS-based terminal servers connect incoming
556users to shell-account machines on a high port, usually 1642 or thereabouts.
557SOCKS servers run on 1080. Do "netstat -a" and get creative.
558
559There are some daemons that are well-written enough to bind separately to all
560the local interfaces, possibly with an eye toward heading off this sort of
561problem. Named from recent BIND releases, and NTP, are two that come to mind.
562Netstat will show these listening on address.53 instead of *.53. You won't
563be able to get in front of these on any of the real interface addresses, which
564of course is especially interesting in the case of named, but these servers
565sometimes forget about things like "alias" interface addresses or interfaces
566that appear later on such as dynamic PPP links. There are some hacked web
567servers and versions of "inetd" floating around that specifically bind as well,
568based on a configuration file -- these generally *are* bound to alias addresses
569to offer several different address-based services from one machine.
570
571Using -e to start a remote backdoor shell is another obvious sort of thing,
572easier than constructing a file for inetd to listen on "ingreslock" or
573something, and you can access-control it against other people by specifying a
574client host and port. Experience with this truly demonstrates how fragile the
575barrier between being "logged in" or not really is, and is further expressed by
576scripts/bsh. If you're already behind a firewall, it may be easier to make an
577*outbound* connection and then run a shell; a small wrapper script can
578periodically try connecting to a known place and port, you can later listen
579there until the inbound connection arrives, and there's your shell. Running
580a shell via UDP has several interesting features, although be aware that once
581"connected", the UDP stub sockets tend to show up in "netstat" just like TCP
582connections and may not be quite as subtle as you wanted. Packets may also be
583lost, so use TCP if you need reliable connections. But since UDP is
584connectionless, a hookup of this sort will stick around almost forever, even if
585you ^C out of netcat or do a reboot on your side, and you only need to remember
586the ports you used on both ends to reestablish. And outbound UDP-plus-exec
587connection creates the connected socket and starts the program immediately. On
588a listening UDP connection, the socket is created once a first packet is
589received. In either case, though, such a "connection" has the interesting side
590effect that only your client-side IP address and [chosen?] source port will
591thereafter be able to talk to it. Instant access control! A non-local third
592party would have to do ALL of the following to take over such a session:
593
594 forge UDP with your source address [trivial to do; see below]
595 guess the port numbers of BOTH ends, or sniff the wire for them
596 arrange to block ICMP or UDP return traffic between it and your real
597 source, so the session doesn't die with a network write error.
598
599The companion program data/rservice.c is helpful in scripting up any sort of
600r-service username or password guessing attack. The arguments to "rservice"
601are simply the strings that get null-terminated and passed over an "rcmd"-style
602connection, with the assumption that the client does not need a separate
603standard-error port. Brute-force password banging is best done via "rexec" if
604it is available since it is less likely to log failed attempts. Thus, doing
605"rservice joe joespass pwd | nc target exec" should return joe's home dir if
606the password is right, or "Permission denied." Plug in a dictionary and go to
607town. If you're attacking rsh/rlogin, remember to be root and bind to a port
608between 512 and 1023 on your end, and pipe in "rservice joe joe pwd" and such.
609
610Netcat can prevent inadvertently sending extra information over a telnet
611connection. Use "nc -t" in place of telnet, and daemons that try to ask for
612things like USER and TERM environment variables will get no useful answers, as
613they otherwise would from a more recent telnet program. Some telnetds actually
614try to collect this stuff and then plug the USER variable into "login" so that
615the caller is then just asked for a password! This mechanism could cause a
616login attempt as YOUR real username to be logged over there if you use a
617Borman-based telnet instead of "nc -t".
618
619Got an unused network interface configured in your kernel [e.g. SLIP], or
620support for alias addresses? Ifconfig one to be any address you like, and bind
621to it with -s to enable all sorts of shenanigans with bogus source addresses.
622The interface probably has to be UP before this works; some SLIP versions
623need a far-end address before this is true. Hammering on UDP services is then
624a no-brainer. What you can do to an unfiltered syslog daemon should be fairly
625obvious; trimming the conf file can help protect against it. Many routers out
626there still blindly believe what they receive via RIP and other routing
627protocols. Although most UDP echo and chargen servers check if an incoming
628packet was sent from *another* "internal" UDP server, there are many that still
629do not, any two of which [or many, for that matter] could keep each other
630entertained for hours at the expense of bandwidth. And you can always make
631someone wonder why she's being probed by nsa.gov.
632
633Your TCP spoofing possibilities are mostly limited to destinations you can
634source-route to while locally bound to your phony address. Many sites block
635source-routed packets these days for precisely this reason. If your kernel
636does oddball things when sending source-routed packets, try moving the pointer
637around with -G. You may also have to fiddle with the routing on your own
638machine before you start receiving packets back. Warning: some machines still
639send out traffic using the source address of the outbound interface, regardless
640of your binding, especially in the case of localhost. Check first. If you can
641open a connection but then get no data back from it, the target host is
642probably killing the IP options on its end [this is an option inside TCP
643wrappers and several other packages], which happens after the 3-way handshake
644is completed. If you send some data and observe the "send-q" side of "netstat"
645for that connection increasing but never getting sent, that's another symptom.
646Beware: if Sendmail 8.7.x detects a source-routed SMTP connection, it extracts
647the hop list and sticks it in the Received: header!
648
649SYN bombing [sometimes called "hosing"] can disable many TCP servers, and if
650you hit one often enough, you can keep it unreachable for days. As is true of
651many other denial-of-service attacks, there is currently no defense against it
652except maybe at the human level. Making kernel SOMAXCONN considerably larger
653than the default and the half-open timeout smaller can help, and indeed some
654people running large high-performance web servers have *had* to do that just to
655handle normal traffic. Taking out mailers and web servers is sociopathic, but
656on the other hand it is sometimes useful to be able to, say, disable a site's
657identd daemon for a few minutes. If someone realizes what is going on,
658backtracing will still be difficult since the packets have a phony source
659address, but calls to enough ISP NOCs might eventually pinpoint the source.
660It is also trivial for a clueful ISP to watch for or even block outgoing
661packets with obviously fake source addresses, but as we know many of them are
662not clueful or willing to get involved in such hassles. Besides, outbound
663packets with an [otherwise unreachable] source address in one of their net
664blocks would look fairly legitimate.
665
666Notes
667=====
668
669A discussion of various caveats, subtleties, and the design of the innards.
670
671As of version 1.07 you can construct a single file containing command arguments
672and then some data to transfer. Netcat is now smart enough to pick out the
673first line and build the argument list, and send any remaining data across the
674net to one or multiple ports. The first release of netcat had trouble with
675this -- it called fgets() for the command line argument, which behind the
676scenes does a large read() from standard input, perhaps 4096 bytes or so, and
677feeds that out to the fgets() library routine. By the time netcat 1.00 started
678directly read()ing stdin for more data, 4096 bytes of it were gone. It now
679uses raw read() everywhere and does the right thing whether reading from files,
680pipes, or ttys. If you use this for multiple-port connections, the single
681block of data will now be a maximum of 8K minus the first line. Improvements
682have been made to the logic in sending the saved chunk to each new port. Note
683that any command-line arguments hidden using this mechanism could still be
684extracted from a core dump.
685
686When netcat receives an inbound UDP connection, it creates a "connected socket"
687back to the source of the connection so that it can also send out data using
688normal write(). Using this mechanism instead of recvfrom/sendto has several
689advantages -- the read/write select loop is simplified, and ICMP errors can in
690effect be received by non-root users. However, it has the subtle side effect
691that if further UDP packets arrive from the caller but from different source
692ports, the listener will not receive them. UDP listen mode on a multihomed
693machine may have similar quirks unless you specifically bind to one of its
694addresses. It is not clear that kernel support for UDP connected sockets
695and/or my understanding of it is entirely complete here, so experiment...
696
697You should be aware of some subtleties concerning UDP scanning. If -z is on,
698netcat attempts to send a single null byte to the target port, twice, with a
699small time in between. You can either use the -w timeout, or netcat will try
700to make a "sideline" TCP connection to the target to introduce a small time
701delay equal to the round-trip time between you and the target. Note that if
702you have a -w timeout and -i timeout set, BOTH take effect and you wait twice
703as long. The TCP connection is to a normally refused port to minimize traffic,
704but if you notice a UDP fast-scan taking somewhat longer than it should, it
705could be that the target is actually listening on the TCP port. Either way,
706any ICMP port-unreachable messages from the target should have arrived in the
707meantime. The second single-byte UDP probe is then sent. Under BSD kernels,
708the ICMP error is delivered to the "connected socket" and the second write
709returns an error, which tells netcat that there is NOT a UDP service there.
710While Linux seems to be a fortunate exception, under many SYSV derived kernels
711the ICMP is not delivered, and netcat starts reporting that *all* the ports are
712"open" -- clearly wrong. [Some systems may not even *have* the "udp connected
713socket" concept, and netcat in its current form will not work for UDP at all.]
714If -z is specified and only one UDP port is probed, netcat's exit status
715reflects whether the connection was "open" or "refused" as with TCP.
716
717It may also be that UDP packets are being blocked by filters with no ICMP error
718returns, in which case everything will time out and return "open". This all
719sounds backwards, but that's how UDP works. If you're not sure, try "echo
720w00gumz | nc -u -w 2 target 7" to see if you can reach its UDP echo port at
721all. You should have no trouble using a BSD-flavor system to scan for UDP
722around your own network, although flooding a target with the high activity that
723-z generates will cause it to occasionally drop packets and indicate false
724"opens". A more "correct" way to do this is collect and analyze the ICMP
725errors, as does SATAN's "udp_scan" backend, but then again there's no guarantee
726that the ICMP gets back to you either. Udp_scan also does the zero-byte
727probes but is excruciatingly careful to calculate its own round-trip timing
728average and dynamically set its own response timeouts along with decoding any
729ICMP received. Netcat uses a much sleazier method which is nonetheless quite
730effective. Cisco routers are known to have a "dead time" in between ICMP
731responses about unreachable UDP ports, so a fast scan of a cisco will show
732almost everything "open". If you are looking for a specific UDP service, you
733can construct a file containing the right bytes to trigger a response from the
734other end and send that as standard input. Netcat will read up to 8K of the
735file and send the same data to every UDP port given. Note that you must use a
736timeout in this case [as would any other UDP client application] since the
737two-write probe only happens if -z is specified.
738
739Many telnet servers insist on a specific set of option negotiations before
740presenting a login banner. On a raw connection you will see this as small
741amount of binary gook. My attempts to create fixed input bytes to make a
742telnetd happy worked some places but failed against newer BSD-flavor ones,
743possibly due to timing problems, but there are a couple of much better
744workarounds. First, compile with -DTELNET and use -t if you just want to get
745past the option negotiation and talk to something on a telnet port. You will
746still see the binary gook -- in fact you'll see a lot more of it as the options
747are responded to behind the scenes. The telnet responder does NOT update the
748total byte count, or show up in the hex dump -- it just responds negatively to
749any options read from the incoming data stream. If you want to use a normal
750full-blown telnet to get to something but also want some of netcat's features
751involved like settable ports or timeouts, construct a tiny "foo" script:
752
753 #! /bin/sh
754 exec nc -otheroptions targethost 23
755
756and then do
757
758 nc -l -p someport -e foo localhost &
759 telnet localhost someport
760
761and your telnet should connect transparently through the exec'ed netcat to
762the target, using whatever options you supplied in the "foo" script. Don't
763use -t inside the script, or you'll wind up sending *two* option responses.
764
765I've observed inconsistent behavior under some Linuxes [perhaps just older
766ones?] when binding in listen mode. Sometimes netcat binds only to "localhost"
767if invoked with no address or port arguments, and sometimes it is unable to
768bind to a specific address for listening if something else is already listening
769on "any". The former problem can be worked around by specifying "-s 0.0.0.0",
770which will do the right thing despite netcat claiming that it's listening on
771[127.0.0.1]. This is a known problem -- for example, there's a mention of it
772in the makefile for SOCKS. On the flip side, binding to localhost and sending
773packets to some other machine doesn't work as you'd expect -- they go out with
774the source address of the sending interface instead. The Linux kernel contains
775a specific check to ensure that packets from 127.0.0.1 are never sent to the
776wire; other kernels may contain similar code. Linux, of course, *still*
777doesn't support source-routing, but they claim that it and many other network
778improvements are at least breathing hard.
779
780There are several possible errors associated with making TCP connections, but
781to specifically see anything other than "refused", one must wait the full
782kernel-defined timeout for a connection to fail. Netcat's mechanism of
783wrapping an alarm timer around the connect prevents the *real* network error
784from being returned -- "errno" at that point indicates "interrupted system
785call" since the connect attempt was interrupted. Some old 4.3 BSD kernels
786would actually return things like "host unreachable" immediately if that was
787the case, but most newer kernels seem to wait the full timeout and *then* pass
788back the real error. Go figure. In this case, I'd argue that the old way was
789better, despite those same kernels generally being the ones that tear down
790*established* TCP connections when ICMP-bombed.
791
792Incoming socket options are passed to applications by the kernel in the
793kernel's own internal format. The socket-options structure for source-routing
794contains the "first-hop" IP address first, followed by the rest of the real
795options list. The kernel uses this as is when sending reply packets -- the
796structure is therefore designed to be more useful to the kernel than to humans,
797but the hex dump of it that netcat produces is still useful to have.
798
799Kernels treat source-routing options somewhat oddly, but it sort of makes sense
800once one understands what's going on internally. The options list of addresses
801must contain hop1, hop2, ..., destination. When a source-routed packet is sent
802by the kernel [at least BSD], the actual destination address becomes irrelevant
803because it is replaced with "hop1", "hop1" is removed from the options list,
804and all the other addresses in the list are shifted up to fill the hole. Thus
805the outbound packet is sent from your chosen source address to the first
806*gateway*, and the options list now contains hop2, ..., destination. During
807all this address shuffling, the kernel does NOT change the pointer value, which
808is why it is useful to be able to set the pointer yourself -- you can construct
809some really bizarre return paths, and send your traffic fairly directly to the
810target but around some larger loop on the way back. Some Sun kernels seem to
811never flip the source-route around if it contains less than three hops, never
812reset the pointer anyway, and tries to send the packet [with options containing
813a "completed" source route!!] directly back to the source. This is way broken,
814of course. [Maybe ipforwarding has to be on? I haven't had an opportunity to
815beat on it thoroughly yet.]
816
817"Credits" section: The original idea for netcat fell out of a long-standing
818desire and fruitless search for a tool resembling it and having the same
819features. After reading some other network code and realizing just how many
820cool things about sockets could be controlled by the calling user, I started
821on the basics and the rest fell together pretty quickly. Some port-scanning
822ideas were taken from Venema/Farmer's SATAN tool kit, and Pluvius' "pscan"
823utility. Healthy amounts of BSD kernel source were perused in an attempt to
824dope out socket options and source-route handling; additional help was obtained
825from Dave Borman's telnet sources. The select loop is loosely based on fairly
826well-known code from "rsh" and Richard Stevens' "sock" program [which itself is
827sort of a "netcat" with more obscure features], with some more paranoid
828sanity-checking thrown in to guard against the distinct likelihood that there
829are subtleties about such things I still don't understand. I found the
830argument-hiding method cleanly implemented in Barrett's "deslogin"; reading the
831line as input allows greater versatility and is much less prone to cause
832bizarre problems than the more common trick of overwriting the argv array.
833After the first release, several people contributed portability fixes; they are
834credited in generic.h and the Makefile. Lauren Burka inspired the ascii art
835for this revised document. Dean Gaudet at Wired supplied a precursor to
836the hex-dump code, and mudge@l0pht.com originally experimented with and
837supplied code for the telnet-options responder. Outbound "-e <prog>" resulted
838from a need to quietly bypass a firewall installation. Other suggestions and
839patches have rolled in for which I am always grateful, but there are only 26
840hours per day and a discussion of feature creep near the end of this document.
841
842Netcat was written with the Russian railroad in mind -- conservatively built
843and solid, but it *will* get you there. While the coding style is fairly
844"tight", I have attempted to present it cleanly [keeping *my* lines under 80
845characters, dammit] and put in plenty of comments as to why certain things
846are done. Items I know to be questionable are clearly marked with "XXX".
847Source code was made to be modified, but determining where to start is
848difficult with some of the tangles of spaghetti code that are out there.
849Here are some of the major points I feel are worth mentioning about netcat's
850internal design, whether or not you agree with my approach.
851
852Except for generic.h, which changes to adapt more platforms, netcat is a single
853source file. This has the distinct advantage of only having to include headers
854once and not having to re-declare all my functions in a billion different
855places. I have attempted to contain all the gross who's-got-what-.h-file
856things in one small dumping ground. Functions are placed "dependencies-first",
857such that when the compiler runs into the calls later, it already knows the
858type and arguments and won't complain. No function prototyping -- not even the
859__P(()) crock -- is used, since it is more portable and a file of this size is
860easy enough to check manually. Each function has a standard-format comment
861ahead of it, which is easily found using the regexp " :$". I freely use gotos.
862Loops and if-clauses are made as small and non-nested as possible, and the ends
863of same *marked* for clarity [I wish everyone would do this!!].
864
865Large structures and buffers are all malloc()ed up on the fly, slightly larger
866than the size asked for and zeroed out. This reduces the chances of damage
867from those "end of the buffer" fencepost errors or runaway pointers escaping
868off the end. These things are permanent per run, so nothing needs to be freed
869until the program exits.
870
871File descriptor zero is always expected to be standard input, even if it is
872closed. If a new network descriptor winds up being zero, a different one is
873asked for which will be nonzero, and fd zero is simply left kicking around
874for the rest of the run. Why? Because everything else assumes that stdin is
875always zero and "netfd" is always positive. This may seem silly, but it was a
876lot easier to code. The new fd is obtained directly as a new socket, because
877trying to simply dup() a new fd broke subsequent socket-style use of the new fd
878under Solaris' stupid streams handling in the socket library.
879
880The catch-all message and error handlers are implemented with an ample list of
881phoney arguments to get around various problems with varargs. Varargs seems
882like deliberate obfuscation in the first place, and using it would also
883require use of vfprintf() which not all platforms support. The trailing
884sleep in bail() is to allow output to flush, which is sometimes needed if
885netcat is already on the other end of a network connection.
886
887The reader may notice that the section that does DNS lookups seems much
888gnarlier and more confusing than other parts. This is NOT MY FAULT. The
889sockaddr and hostent abstractions are an abortion that forces the coder to
890deal with it. Then again, a lot of BSD kernel code looks like similar
891struct-pointer hell. I try to straighten it out somewhat by defining my own
892HINF structure, containing names, ascii-format IP addresses, and binary IP
893addresses. I fill this structure exactly once per host argument, and squirrel
894everything safely away and handy for whatever wants to reference it later.
895
896Where many other network apps use the FIONBIO ioctl to set non-blocking I/O
897on network sockets, netcat uses straightforward blocking I/O everywhere.
898This makes everything very lock-step, relying on the network and filesystem
899layers to feed in data when needed. Data read in is completely written out
900before any more is fetched. This may not be quite the right thing to do under
901some OSes that don't do timed select() right, but this remains to be seen.
902
903The hexdump routine is written to be as fast as possible, which is why it does
904so much work itself instead of just sprintf()ing everything together. Each
905dump line is built into a single buffer and atomically written out using the
906lowest level I/O calls. Further improvements could undoubtedly be made by
907using writev() and eliminating all sprintf()s, but it seems to fly right along
908as is. If both exec-a-prog mode and a hexdump file is asked for, the hexdump
909flag is deliberately turned off to avoid creating random zero-length files.
910Files are opened in "truncate" mode; if you want "append" mode instead, change
911the open flags in main().
912
913main() may look a bit hairy, but that's only because it has to go down the
914argv list and handle multiple ports, random mode, and exit status. Efforts
915have been made to place a minimum of code inside the getopt() loop. Any real
916work is sent off to functions in what is hopefully a straightforward way.
917
918Obligatory vendor-bash: If "nc" had become a standard utility years ago,
919the commercial vendors would have likely packaged it setuid root and with
920-DGAPING_SECURITY_HOLE turned on but not documented. It is hoped that netcat
921will aid people in finding and fixing the no-brainer holes of this sort that
922keep appearing, by allowing easier experimentation with the "bare metal" of
923the network layer.
924
925It could be argued that netcat already has too many features. I have tried
926to avoid "feature creep" by limiting netcat's base functionality only to those
927things which are truly relevant to making network connections and the everyday
928associated DNS lossage we're used to. Option switches already have slightly
929overloaded functionality. Random port mode is sort of pushing it. The
930hex-dump feature went in later because it *is* genuinely useful. The
931telnet-responder code *almost* verges on the gratuitous, especially since it
932mucks with the data stream, and is left as an optional piece. Many people have
933asked for example "how 'bout adding encryption?" and my response is that such
934things should be separate entities that could pipe their data *through* netcat
935instead of having their own networking code. I am therefore not completely
936enthusiastic about adding any more features to this thing, although you are
937still free to send along any mods you think are useful.
938
939Nonetheless, at this point I think of netcat as my tcp/ip swiss army knife,
940and the numerous companion programs and scripts to go with it as duct tape.
941Duct tape of course has a light side and a dark side and binds the universe
942together, and if I wrap enough of it around what I'm trying to accomplish,
943it *will* work. Alternatively, if netcat is a large hammer, there are many
944network protocols that are increasingly looking like nails by now...
945
946_H* 960320 v1.10 RELEASE -- happy spring!
diff --git a/src/usr.bin/nc/data/Makefile b/src/usr.bin/nc/data/Makefile
new file mode 100644
index 0000000000..65cf185358
--- /dev/null
+++ b/src/usr.bin/nc/data/Makefile
@@ -0,0 +1,10 @@
1all: data rservice xor
2
3data: data.c
4 cc -s -O -o data data.c
5rservice: rservice.c
6 cc -s -O -o rservice rservice.c
7xor: xor.c
8 cc -s -O -o xor xor.c
9clean:
10 rm -f *.o data rservice xor
diff --git a/src/usr.bin/nc/data/README b/src/usr.bin/nc/data/README
new file mode 100644
index 0000000000..7e4b9fbf63
--- /dev/null
+++ b/src/usr.bin/nc/data/README
@@ -0,0 +1,9 @@
1For now, read the header comments inside each of these for documentation.
2The programs are simple enough that they don't really need a Makefile any more
3complex than the one given; ymmv. Data and xor may also be useful on DOS,
4which is why there are hooks for it in the code.
5
6data.c a primitive atob / btoa byte generator
7*.d example input to "data -g"
8rservice.c a utility for scripting up rsh/rexec attacks
9xor.c generic xor handler
diff --git a/src/usr.bin/nc/data/data.c b/src/usr.bin/nc/data/data.c
new file mode 100644
index 0000000000..56c167fd96
--- /dev/null
+++ b/src/usr.bin/nc/data/data.c
@@ -0,0 +1,274 @@
1/* primitive arbitrary-data frontend for netcat. 0.9 960226
2 only handles one value per ascii line, but at least parses 0xNN too
3 an input line containing "%r" during "-g" generates a random byte
4
5 todo:
6 make work on msloss jus' for kicks [workin' on it...]
7
8 syntax: data -X [limit]
9 where X is one of
10 d: dump raw bytes to ascii format
11 g: generate raw bytes from ascii input
12 c: generate ??? of value -- NOTYET
13 r: generate all random bytes
14 and limit is how many bytes to generate or dump [unspecified = infinite]
15
16 *Hobbit*, started 951004 or so and randomly screwed around with since */
17
18#include <stdio.h>
19
20#ifdef MSDOS /* for MSC only at the moment... */
21#include <fcntl.h>
22#else /* MSDOS */
23#include <sys/file.h>
24#define HAVE_RANDOM /* XXX: might have to change */
25#endif /* MSDOS */
26
27static char buf_in [128];
28static char buf_raw [8192];
29static char surveysez[] = "survey sez... XXX\n";
30
31/* fgetss :
32 wrapper for fgets, that yanks trailing newlines. Doing the work ourselves
33 instead of calling strchr/strlen/whatever */
34char * fgetss (buf, len, from)
35 char * buf;
36 size_t len;
37 FILE * from;
38{
39 register int x;
40 register char * p, * q;
41 p = fgets (buf, len, from); /* returns ptr to buf */
42 if (! p)
43 return (NULL);
44 q = p;
45 for (x = 0; x < len; x++) {
46 *p = (*p & 0x7f); /* rip parity, just in case */
47 switch (*p) {
48 case '\n':
49 case '\r':
50 case '\0':
51 *p = '\0';
52 return (q);
53 } /* switch */
54 p++;
55 } /* for */
56} /* fgetss */
57
58/* randint:
59 swiped from rndb.c. Generates an INT, you have to mask down to char. */
60int randint()
61{
62 register int q;
63 register int x;
64
65#ifndef HAVE_RANDOM
66 q = rand();
67#else
68 q = random();
69#endif
70 x = ((q >> 8) & 0xff); /* perturb low byte using some higher bits */
71 x = q ^ x;
72 return (x);
73}
74
75main (argc, argv)
76 int argc;
77 char ** argv;
78{
79 register unsigned char * p;
80 register char * q;
81 register int x;
82 int bc = 0;
83 int limit = 0; /* num to gen, or 0 = infinite */
84 register int xlimit; /* running limit */
85 FILE * txt; /* line-by-line ascii file */
86 int raw; /* raw bytes fd */
87 int dumping = 0; /* cmd flags ... */
88 int genning = 0;
89 int randing = 0;
90
91 memset (buf_in, 0, sizeof (buf_in));
92 memset (buf_raw, 0, sizeof (buf_raw));
93
94 xlimit = 1; /* doubles as "exit flag" */
95 bc = 1; /* preload, assuming "dump" */
96 x = getpid() + 687319;
97/* if your library doesnt have srandom/random, use srand/rand. [from rnd.c] */
98#ifndef HAVE_RANDOM
99 srand (time(0) + x);
100#else
101 srandom (time(0) + x);
102#endif
103
104#ifdef O_BINARY
105/* DOS stupidity */
106/* Aha: *here's* where that setmode() lib call conflict in ?BSD came from */
107 x = setmode (0, O_BINARY); /* make stdin raw */
108 if (x < 0) {
109 fprintf (stderr, "stdin binary setmode oops: %d\n", x);
110 exit (1);
111 }
112 x = setmode (1, O_BINARY); /* make stdout raw */
113 if (x < 0) {
114 fprintf (stderr, "stdout binary setmode oops: %d\n", x);
115 exit (1);
116 }
117#endif /* O_BINARY */
118
119 if (argv[1]) {
120 p = argv[1]; /* shit-simple single arg parser... */
121 if (*p == '-') /* dash is optional, we'll deal */
122 p++;
123 if (*p == 'd')
124 dumping++;
125 if (*p == 'g')
126 genning++;
127 if (*p == 'r')
128 randing++;
129 } /* if argv 1 */
130
131/* optional second argument: limit # of bytes shoveled either way */
132 if (argv[2]) {
133 x = atoi (argv[2]);
134 if (x)
135 limit = x;
136 else
137 goto wrong;
138 xlimit = limit;
139 }
140
141/* Since this prog would likely best be written in assmbler, I'm gonna
142 write it *like* assembler. So there. */
143
144 if (randing)
145 goto do_rand;
146
147nextbuf: /* loop sleaze */
148
149 if (dumping) { /* switch off to wherever */
150 if (genning)
151 goto wrong;
152 goto do_dump;
153 }
154 if (genning)
155 goto do_gen;
156wrong:
157 fprintf (stderr, surveysez); /* if both or neither */
158 exit (1);
159
160do_gen:
161/* here if genning -- original functionality */
162 q = buf_raw;
163 bc = 0;
164/* suck up lines until eof or buf_raw is full */
165 while (1) {
166 p = fgetss (buf_in, 120, stdin);
167 if (! p)
168 break; /* EOF */
169/* super-primitive version first: one thingie per line */
170 if (*p == '#') /* comment */
171 continue;
172 if (*p == '\0') /* blank line */
173 continue;
174 if (*p == '%') { /* escape char? */
175 p++;
176 if (*p == 'r') { /* random byte */
177 x = randint();
178 goto stuff;
179 } /* %r */
180 } /* if "%" escape */
181 if (*p == '0')
182 if (*(p+1) == 'x') /* 0x?? */
183 goto hex;
184 x = atoi (p); /* reg'lar decimal number */
185 goto stuff;
186
187hex:
188/* A 65 a 97 */
189/* xxx: use a conversion table for this or something. Since we ripped the
190 parity bit, we only need a preset array of 128 with downconversion factors
191 loaded in *once*. maybe look at scanf... */
192 p++; p++; /* point at hex-chars */
193 x = 0;
194 if ((*p > 96) && (*p < 123)) /* a-z */
195 *p = (*p - 32); /* this is massively clumsy */
196 if ((*p > 64) && (*p < 71)) /* A-F */
197 x = (*p - 55);
198 if ((*p > 47) && (*p < 58)) /* digits */
199 x = (*p - 48);
200 p++;
201 if (*p) /* another digit? */
202 x = (x << 4); /* shift to hi half */
203 if ((*p > 96) && (*p < 123)) /* a-z */
204 *p = (*p - 32);
205 if ((*p > 64) && (*p < 71)) /* A-F */
206 x = (x | (*p - 55)); /* lo half */
207 if ((*p > 47) && (*p < 58)) /* digits */
208 x = (x | (*p - 48));
209
210/* fall thru */
211stuff: /* cvt to byte and add to buffer */
212 *q = (x & 0xff);
213 q++;
214 bc++;
215 if (limit) {
216 xlimit--;
217 if (xlimit == 0) /* max num reached */
218 break;
219 } /* limit */
220 if (bc >= sizeof (buf_raw)) /* buffer full */
221 break;
222 } /* while 1 */
223
224/* now in theory we have our buffer formed; shovel it out */
225 x = write (1, buf_raw, bc);
226 if (x <= 0) {
227 fprintf (stderr, "write oops: %d\n", x);
228 exit (1);
229 }
230 if (xlimit && p)
231 goto nextbuf; /* go get some more */
232 exit (0);
233
234do_dump:
235/* here if dumping raw stuff into an ascii file */
236/* gad, this is *so* much simpler! can we say "don't rewrite printf"? */
237 x = read (0, buf_raw, 8192);
238 if (x <= 0)
239 exit (0);
240 q = buf_raw;
241 for ( ; x > 0; x--) {
242 p = q;
243 printf ("%-3.3d # 0x%-2.2x # ", *p, *p);
244 if ((*p > 31) && (*p < 127))
245 printf ("%c %d\n", *p, bc);
246 else
247 printf (". %d\n", bc);
248 q++;
249 bc++;
250 if (limit) {
251 xlimit--;
252 if (xlimit == 0) {
253 fflush (stdout);
254 exit (0);
255 }
256 } /* limit */
257 } /* for */
258 goto nextbuf;
259
260do_rand:
261/* here if generating all-random bytes. Stays in this loop */
262 p = buf_raw;
263 while (1) {
264 *p = (randint() & 0xff);
265 write (1, p, 1); /* makes very slow! */
266 if (limit) {
267 xlimit--;
268 if (xlimit == 0)
269 break;
270 }
271 } /* while */
272 exit (0);
273
274} /* main */
diff --git a/src/usr.bin/nc/data/dns-any.d b/src/usr.bin/nc/data/dns-any.d
new file mode 100644
index 0000000000..77b014cf70
--- /dev/null
+++ b/src/usr.bin/nc/data/dns-any.d
@@ -0,0 +1,36 @@
1# dns "any for ." query, to udp 53
2# if tcp: precede with 2 bytes of len:
3# 0
4# 17
5# you should get at least *one* record back out
6
7# HEADER:
80 # query id = 2
92
10
111 # flags/opcodes = query, dorecurse
120
13
140 # qdcount, i.e. nqueries: 1
151
16
170 # ancount: answers, 0
180
19
200 # nscount: 0
210
22
230 # addl records: 0
240
25
26# end of fixed header
27
280 # name-len: 0 for ".", lenbyte plus name-bytes otherwise
29
300 # type: any, 255
310xff
32
330 # class: IN
341
35
36# i think that's it..
diff --git a/src/usr.bin/nc/data/nfs-0.d b/src/usr.bin/nc/data/nfs-0.d
new file mode 100644
index 0000000000..0360938227
--- /dev/null
+++ b/src/usr.bin/nc/data/nfs-0.d
@@ -0,0 +1,59 @@
1# UDP NFS null-proc call; finds active NFS listeners on port 2049.
2# If you get *something* back, there's an NFS server there.
3
4000 # XID: 4 trash bytes
5001
6002
7003
8
9000 # CALL: 0
10000
11000
12000
13
14000 # RPC version: 2
15000
16000
17002
18
19000 # nfs: 100003
20001
210x86
220xa3
23
24000 # version: 1
25000
26000
27001
28
29000 # procedure number: 0
30000
31000
32000
33
34000 # port: junk
35000
36000
37000
38
39000 # auth trash
40000
41000
42000
43
44000 # auth trash
45000
46000
47000
48
49000 # auth trash
50000
51000
52000
53
54000 # extra auth trash? probably not needed
55000
56000
57000
58
59# that's it!
diff --git a/src/usr.bin/nc/data/pm.d b/src/usr.bin/nc/data/pm.d
new file mode 100644
index 0000000000..fe327693a9
--- /dev/null
+++ b/src/usr.bin/nc/data/pm.d
@@ -0,0 +1,8 @@
1# obligatory duplicate of dr delete's Livingston portmaster crash, aka
2# telnet break. Fire into its telnet listener. An *old* bug by now, but
3# consider the small window one might obtain from a slightly out-of-rev PM
4# used as a firewall, that starts routing IP traffic BEFORE its filter sets
5# are fully loaded...
6
7255 # 0xff # . 1
8243 # 0xf3 # . 2
diff --git a/src/usr.bin/nc/data/pmap-dump.d b/src/usr.bin/nc/data/pmap-dump.d
new file mode 100644
index 0000000000..bc6b63277d
--- /dev/null
+++ b/src/usr.bin/nc/data/pmap-dump.d
@@ -0,0 +1,60 @@
1# portmap dump request: like "rpcinfo -p" but via UDP instead
2# send to UDP 111 and hope it's not a logging portmapper!
3# split into longwords, since rpc apparently only deals with them
4
5001 # 0x01 # . # XID: 4 trash bytes
6002 # 0x02 # .
7003 # 0x03 # .
8004 # 0x04 # .
9
10000 # 0x00 # . # MSG: int 0=call, 1=reply
11000 # 0x00 # .
12000 # 0x00 # .
13000 # 0x00 # .
14
15000 # 0x00 # . # pmap call body: rpc version=2
16000 # 0x00 # .
17000 # 0x00 # .
18002 # 0x02 # .
19
20000 # 0x00 # . # pmap call body: prog=PMAP, 100000
21001 # 0x01 # .
22134 # 0x86 # .
23160 # 0xa0 # .
24
25000 # 0x00 # . # pmap call body: progversion=2
26000 # 0x00 # .
27000 # 0x00 # .
28002 # 0x02 # .
29
30000 # 0x00 # . # pmap call body: proc=DUMP, 4
31000 # 0x00 # .
32000 # 0x00 # .
33004 # 0x04 # .
34
35# with AUTH_NONE, there are 4 zero integers [16 bytes] here
36
37000 # 0x00 # . # auth junk: cb_cred: auth_unix = 1; NONE = 0
38000 # 0x00 # .
39000 # 0x00 # .
40000 # 0x00 # .
41
42000 # 0x00 # . # auth junk
43000 # 0x00 # .
44000 # 0x00 # .
45000 # 0x00 # .
46
47000 # 0x00 # . # auth junk
48000 # 0x00 # .
49000 # 0x00 # .
50000 # 0x00 # .
51
52000 # 0x00 # . # auth junk
53000 # 0x00 # .
54000 # 0x00 # .
55000 # 0x00 # .
56
57# The reply you get back contains your XID, int 1 if "accepted", and
58# a whole mess of gobbledygook containing program numbers, versions,
59# and ports that rpcinfo knows how to decode. For the moment, you get
60# to wade through it yourself...
diff --git a/src/usr.bin/nc/data/pmap-mnt.d b/src/usr.bin/nc/data/pmap-mnt.d
new file mode 100644
index 0000000000..00588ba41f
--- /dev/null
+++ b/src/usr.bin/nc/data/pmap-mnt.d
@@ -0,0 +1,78 @@
1# portmap request for mountd [or whatever; see where prog=MOUNT]
2# send to UDP 111 and hope it's not a logging portmapper!
3# split into longwords, since rpc apparently only deals with them
4
5001 # 0x01 # . # XID: 4 trash bytes
6002 # 0x02 # .
7003 # 0x03 # .
8004 # 0x04 # .
9
10000 # 0x00 # . # MSG: int 0=call, 1=reply
11000 # 0x00 # .
12000 # 0x00 # .
13000 # 0x00 # .
14
15000 # 0x00 # . # pmap call body: rpc version=2
16000 # 0x00 # .
17000 # 0x00 # .
18002 # 0x02 # .
19
20000 # 0x00 # . # pmap call body: prog=PMAP, 100000
21001 # 0x01 # .
22134 # 0x86 # .
23160 # 0xa0 # .
24
25000 # 0x00 # . # pmap call body: progversion=2
26000 # 0x00 # .
27000 # 0x00 # .
28002 # 0x02 # .
29
30000 # 0x00 # . # pmap call body: proc=GETPORT, 3
31000 # 0x00 # .
32000 # 0x00 # .
33003 # 0x03 # .
34
35# with AUTH_NONE, there are 4 zero integers [16 bytes] here
36
37000 # 0x00 # . # auth junk: cb_cred: auth_unix = 1; NONE = 0
38000 # 0x00 # .
39000 # 0x00 # .
40000 # 0x00 # .
41
42000 # 0x00 # . # auth junk
43000 # 0x00 # .
44000 # 0x00 # .
45000 # 0x00 # .
46
47000 # 0x00 # . # auth junk
48000 # 0x00 # .
49000 # 0x00 # .
50000 # 0x00 # .
51
52000 # 0x00 # . # auth junk
53000 # 0x00 # .
54000 # 0x00 # .
55000 # 0x00 # .
56
57000 # 0x00 # . # prog=MOUNT, 100005
58001 # 0x01 # .
59134 # 0x86 # .
60165 # 0xa5 # .
61
62000 # 0x00 # . # progversion=1
63000 # 0x00 # .
64000 # 0x00 # .
65001 # 0x01 # .
66
67000 # 0x00 # . # protocol=udp, 17
68000 # 0x00 # .
69000 # 0x00 # .
70017 # 0x11 # .
71
72000 # 0x00 # . # proc num = junk
73000 # 0x00 # .
74000 # 0x00 # .
75000 # 0x00 # .
76
77# The reply you get back contains your XID, int 1 if "accepted", and
78# mountd's port number at the end or 0 if not registered.
diff --git a/src/usr.bin/nc/data/rip.d b/src/usr.bin/nc/data/rip.d
new file mode 100644
index 0000000000..da505e2143
--- /dev/null
+++ b/src/usr.bin/nc/data/rip.d
@@ -0,0 +1,52 @@
1# struct netinfo {
2# struct sockaddr rip_dst; /* destination net/host */
3# int rip_metric; /* cost of route */
4# };
5# struct rip {
6# u_char rip_cmd; /* request/response */
7# u_char rip_vers; /* protocol version # */
8# u_char rip_res1[2]; /* pad to 32-bit boundary */
9# union {
10# struct netinfo ru_nets[1]; /* variable length... */
11# char ru_tracefile[1]; /* ditto ... */
12# } ripun;
13#define rip_nets ripun.ru_nets
14#define rip_tracefile ripun.ru_tracefile
15#define RIPCMD_REQUEST 1 /* want info */
16#define RIPCMD_RESPONSE 2 /* responding to request */
17#define RIPCMD_TRACEON 3 /* turn tracing on */
18#define RIPCMD_TRACEOFF 4 /* turn it off */
19#define HOPCNT_INFINITY 16 /* per Xerox NS */
20#define MAXPACKETSIZE 512 /* max broadcast size */
21
22### RIP packet redux
23### UDP send FROM clued-rtr/520 to target/520
242 # RIPCMD_RESPONSE
251 # version
260 # padding
270
28
29# sockaddr-plus-metric structs begin, as many as necessary...
300 # len
312 # AF_INET
320 # port
330
34# addr bytes:
35X
36Y
37Z
38Q
390 # filler, out to 16 bytes [sizeof (sockaddr)] ...
400
410
420
430
440
450
460
470 # metric: net-order integer
480
490
501
51
52## that's it
diff --git a/src/usr.bin/nc/data/rservice.c b/src/usr.bin/nc/data/rservice.c
new file mode 100644
index 0000000000..1085d9cb78
--- /dev/null
+++ b/src/usr.bin/nc/data/rservice.c
@@ -0,0 +1,68 @@
1/* generate ^@string1^@string2^@cmd^@ input to netcat, for scripting up
2 rsh/rexec attacks. Needs to be a prog because shells strip out nulls.
3
4 args:
5 locuser remuser [cmd]
6 remuser passwd [cmd]
7
8 cmd defaults to "pwd".
9
10 ... whatever. _H*/
11
12#include <stdio.h>
13
14/* change if you like; "id" is a good one for figuring out if you won too */
15static char cmd[] = "pwd";
16
17static char buf [256];
18
19main(argc, argv)
20 int argc;
21 char * argv[];
22{
23 register int x;
24 register int y;
25 char * p;
26 char * q;
27
28 p = buf;
29 memset (buf, 0, 256);
30
31 p++; /* first null */
32 y = 1;
33
34 if (! argv[1])
35 goto wrong;
36 x = strlen (argv[1]);
37 memcpy (p, argv[1], x); /* first arg plus another null */
38 x++;
39 p += x;
40 y += x;
41
42 if (! argv[2])
43 goto wrong;
44 x = strlen (argv[2]);
45 memcpy (p, argv[2], x); /* second arg plus null */
46 x++;
47 p += x;
48 y += x;
49
50 q = cmd;
51 if (argv[3])
52 q = argv[3];
53 x = strlen (q); /* not checked -- bfd */
54 memcpy (p, q, x); /* the command, plus final null */
55 x++;
56 p += x;
57 y += x;
58
59 memcpy (p, "\n", 1); /* and a newline, so it goes */
60 y++;
61
62 write (1, buf, y); /* zot! */
63 exit (0);
64
65wrong:
66 fprintf (stderr, "wrong! needs 2 or more args.\n");
67 exit (1);
68}
diff --git a/src/usr.bin/nc/data/showmount.d b/src/usr.bin/nc/data/showmount.d
new file mode 100644
index 0000000000..499794bc8a
--- /dev/null
+++ b/src/usr.bin/nc/data/showmount.d
@@ -0,0 +1,63 @@
1# UDP mountd call. Use as input to find mount daemons and avoid portmap.
2# Useful proc numbers are 2, 5, and 6.
3# UDP-scan around between 600-800 to find most mount daemons.
4# Using this with "2", plugged into "nc -u -v -w 2 victim X-Y" will
5# directly scan *and* dump the current exports when mountd is hit.
6# combine stdout *and* stderr thru "strings" or something to clean it up
7
8000 # XID: 4 trash bytes
9001
10002
11003
12
13000 # CALL: 0
14000
15000
16000
17
18000 # RPC version: 2
19000
20000
21002
22
23000 # mount: 100005
24001
250x86
260xa5
27
28000 # mount version: 1
29000
30000
31001
32
33000 # procedure number -- put what you need here:
34000 # 2 = dump [showmount -e]
35000 # 5 = exportlist [showmount -a]
36xxx # "sed s/xxx/$1/ | data -g | nc ..." or some such...
37
38000 # port: junk
39000
40000
41000
42
43000 # auth trash
44000
45000
46000
47
48000 # auth trash
49000
50000
51000
52
53000 # auth trash
54000
55000
56000
57
58000 # extra auth trash? probably not needed
59000
60000
61000
62
63# that's it!
diff --git a/src/usr.bin/nc/data/xor.c b/src/usr.bin/nc/data/xor.c
new file mode 100644
index 0000000000..9feead0cba
--- /dev/null
+++ b/src/usr.bin/nc/data/xor.c
@@ -0,0 +1,92 @@
1/* Generic xor handler.
2
3 With no args, xors stdin against 0xFF to stdout. A single argument is a
4 file to read xor-bytes out of. Any zero in the xor-bytes array is treated
5 as the end; if you need to xor against a string that *includes* zeros,
6 you're on your own.
7
8 The indirect file can be generated easily with data.c.
9
10 Written because there are so many lame schemes for "masking" plaintext
11 passwords and the like floating around, and it's handy to just run an
12 obscure binary-format configuration file through this and look for strings.
13
14 *Hobbit*, 960208 */
15
16#include <stdio.h>
17#include <fcntl.h>
18
19char buf[8192];
20char bytes[256];
21char * py;
22
23/* do the xor, in place. Uses global ptr "py" to maintain "bytes" state */
24xorb (buf, len)
25 char * buf;
26 int len;
27{
28 register int x;
29 register char * pb;
30
31 pb = buf;
32 x = len;
33 while (x > 0) {
34 *pb = (*pb ^ *py);
35 pb++;
36 py++;
37 if (! *py)
38 py = bytes;
39 x--;
40 }
41} /* xorb */
42
43/* blah */
44main (argc, argv)
45 int argc;
46 char ** argv;
47{
48 register int x = 0;
49 register int y;
50
51/* manually preload; xor-with-0xFF is all too common */
52 memset (bytes, 0, sizeof (bytes));
53 bytes[0] = 0xff;
54
55/* if file named in any arg, reload from that */
56#ifdef O_BINARY /* DOS shit... */
57 x = setmode (0, O_BINARY); /* make stdin raw */
58 if (x < 0) {
59 fprintf (stderr, "stdin binary setmode oops: %d\n", x);
60 exit (1);
61 }
62 x = setmode (1, O_BINARY); /* make stdout raw */
63 if (x < 0) {
64 fprintf (stderr, "stdout binary setmode oops: %d\n", x);
65 exit (1);
66 }
67#endif /* O_BINARY */
68
69 if (argv[1])
70#ifdef O_BINARY
71 x = open (argv[1], O_RDONLY | O_BINARY);
72#else
73 x = open (argv[1], O_RDONLY);
74#endif
75 if (x > 0) {
76 read (x, bytes, 250); /* nothin' fancy here */
77 close (x);
78 }
79 py = bytes;
80 x = 1;
81 while (x > 0) {
82 x = read (0, buf, sizeof (buf));
83 if (x <= 0)
84 break;
85 xorb (buf, x);
86 y = write (1, buf, x);
87 if (y <= 0)
88 exit (1);
89 }
90 exit (0);
91}
92
diff --git a/src/usr.bin/nc/generic.h b/src/usr.bin/nc/generic.h
new file mode 100644
index 0000000000..b3dd5f5dc6
--- /dev/null
+++ b/src/usr.bin/nc/generic.h
@@ -0,0 +1,377 @@
1/* generic.h -- anything you don't #undef at the end remains in effect.
2 The ONLY things that go in here are generic indicator flags; it's up
3 to your programs to declare and call things based on those flags.
4
5 You should only need to make changes via a minimal system-specific section
6 at the end of this file. To build a new section, rip through this and
7 check everything it mentions on your platform, and #undef that which needs
8 it. If you generate a system-specific section you didn't find in here,
9 please mail me a copy so I can update the "master".
10
11 I realize I'm probably inventing another pseudo-standard here, but
12 goddamnit, everybody ELSE has already, and I can't include all of their
13 hairball schemes too. HAVE_xx conforms to the gnu/autoconf usage and
14 seems to be the most common format. In fact, I dug a lot of these out
15 of autoconf and tried to common them all together using "stupidh" to
16 collect data from platforms.
17
18 In disgust... _H* 940910, 941115, 950511. Pseudo-version: 1.3
19
20 Updated 951104 with many patches from netcat feedback, and properly
21 closed a lot of slop in open-ended comments: version 1.4
22 960217 + nextstep: version 1.5
23*/
24
25#ifndef GENERIC_H /* only run through this once */
26#define GENERIC_H
27
28/* =============================== */
29/* System calls, lib routines, etc */
30/* =============================== */
31
32/* How does your system declare malloc, void or char? Usually void, but go
33 ask the SunOS people why they had to be different... */
34#define VOID_MALLOC
35
36/* notably from fwtk/firewall.h: posix locking? */
37#define HAVE_FLOCK /* otherwise it's lockf() */
38
39/* if you don't have setsid(), you might have setpgrp(). */
40#define HAVE_SETSID
41
42/* random() is generally considered better than rand() */
43#define HAVE_RANDOM
44
45/* the srand48/lrand48/etc family is s'posedly even better */
46#define HAVE_RAND48
47/* bmc@telebase and others have suggested these macros if a box *does* have
48 rand48. Will consider for later if we're doing something that really
49 requires stronger random numbers, but netcat and such certainly doesn't.
50#define srandom(seed) srand48((long) seed)
51#define random() lrand48() */
52
53/* if your machine doesn't have lstat(), it should have stat() [dos...] */
54#define HAVE_LSTAT
55
56/* different kinds of term ioctls. How to recognize them, very roughly:
57 sysv/POSIX_ME_HARDER: termio[s].h, struct termio[s], tty.c_*[]
58 bsd/old stuff: sgtty.h, ioctl(TIOCSETP), sgttyb.sg_*, tchars.t_* */
59#define HAVE_TERMIOS
60
61/* dbm vs ndbm */
62#define HAVE_NDBM
63
64/* extended utmp/wtmp stuff. MOST machines still do NOT have this SV-ism */
65#define UTMPX
66
67/* some systems have nice() which takes *relative* values... [resource.h] */
68#define HAVE_SETPRIORITY
69
70/* a sysvism, I think, but ... */
71#define HAVE_SYSINFO
72
73/* ============= */
74/* Include files */
75/* ============= */
76
77/* Presence of these can be determined via a script that sniffs them
78 out if you aren't sure. See "stupidh"... */
79
80/* stdlib comes with most modern compilers, but ya never know */
81#define HAVE_STDLIB_H
82
83/* not on a DOS box! */
84#define HAVE_UNISTD_H
85
86/* stdarg is a weird one */
87#define HAVE_STDARG_H
88
89/* dir.h or maybe ndir.h otherwise. */
90#define HAVE_DIRENT_H
91
92/* string or strings */
93#define HAVE_STRINGS_H
94
95/* if you don't have lastlog.h, what you want might be in login.h */
96#define HAVE_LASTLOG_H
97
98/* predefines for _PATH_various */
99#define HAVE_PATHS_H
100
101/* some SV-flavors break select stuff out separately */
102#define HAVE_SELECT_H
103
104/* assorted others */
105#define HAVE_PARAM_H /* in sys/ */
106#define HAVE_SYSMACROS_H /* in sys/ */
107#define HAVE_TTYENT_H /* securetty et al */
108
109/* ==================== */
110
111/* Still maybe have to do something about the following, if it's even
112 worth it. I just grepped a lot of these out of various code, without
113 looking them up yet:
114
115#define HAVE_EINPROGRESS
116#define HAVE_F_SETOWN
117HAVE_FILIO_H ... fionbio, fiosetown, etc... will need for hairier
118 select loops.
119#define HAVE_SETENV ... now *there's* a hairy one; **environ is portable
120#define BIG_ENDIAN/little_endian ... *please* try to avoid this stupidity
121 and LSBFIRST/MSBFIRST
122#define HAVE_GETUSERSHELL ... you could always pull it out of getpwent()
123#define HAVE_SETE[UG]ID ... lib or syscall, it varies on diff platforms
124#define HAVE_STRCHR ... should actually be handled by string/strings
125#define HAVE_PSTAT
126#define HAVE_ST_BLKSIZE ... a stat() thing?
127#define HAVE_IP_TOS
128#define HAVE_STRFTIME ... screw this, we'll just INCLUDE one for lame
129 old boxes that don't have it [sunos 3.x, early 4.x?]
130#define HAVE_VFPRINTF
131#define HAVE_SHADOW_PASSWD ... in its multitudinous schemes?? ... how
132 about sumpin' like #define SHADOW_PASSWD_TYPE ... could get grody.
133 ... looks like sysv /etc/shadow, getspent() family is common.
134#define SIG* ... what a swamp, punt for now; should all be in signal.h
135#define HAVE_STRCSPN ... see larry wall's comment in the fwtk regex code
136#define ULTRIX_AUTH ... bwahaha.
137#define HAVE_YP or NIS or whatever you wanna call it this week
138randomness about VARARGS??
139--- later stuff to be considered ---
140#define UINT4 ... u-int on alpha/osf, i.e. __alpha/__osf__, ulong elsewhere?
141 dont name it that, though, it'll conflict with extant .h files like md5
142randomness about machine/endian.h, machine/inline.h -- bsdi, net/2
143randomness about _PATH_WTMP vs WTMP_FILE and where they even live!!
144#define HAVE_SYS_ERRLIST ... whether it's in stdio.h or not [bsd 4.4]
145--- still more stuff
146#define HAVE_SETENV
147#define _PATH_UTMP vs UTMP_FILE, a la deslogind?!
148#define HAVE_DAEMON
149#define HAVE_INETADDR [vixie bind?]
150lseek: SEEK_SET vs L_SET and associated lossage [epi-notes, old 386Mach]
151bsdi: ioctl_compat.h ?
152--- takin' some ifdefs from CNS krb:
153F_GETOWN/F_SETOWN
154CRAY: long = 8 bytes, etc [class with alpha?]
155CGETENT
156SIGINFO
157SIGTSTP SIGTTOU SIGWINCH
158SPX?
159SYSV_TERMIO -- covered elsewhere, I hope
160TIOCEXT TIOCFLUSH TIOC[GS]WINSIZ
161NEWINIT: something about init cleaning up dead login processes [telnet?]
162PARENT_DOES_UTMP, too [telnet]
163VDISCARD
164VEOL/VEOL2/VLNEXT VREPRINT -- termios stuff?, and related...
165STREAMSPTY/STREAMSPTYEM
166AF_INET/AF_UNSPEC, PF_*
167ECHOCTL/ECHOKE
168F_ULOCK [?!]
169setpgrp/getpgrp() ONEARG business..
170HAVE_ALLOCA
171HAVE_GETUTENT
172HAVE_SYS_SELECT_H [irix!]
173HAVE_DIRENT [old 386mach has *direct.h*!]
174HAVE_SIGSET
175HAVE_VFORK_H and HAVE_VFORK
176HAVE_VHANGUP
177HAVE_VSPRINTF
178HAVE_IPTOS_*
179HAVE_STRCASECMP, STRNCASECMP
180HAVE_SYS_FCNTL_H
181HAVE_SYS_TIME_H
182HAVE_UTIMES
183NOTTYENT [?]
184HAVE_FCHMOD
185HAVE_GETUSERSHELL
186HAVE_SIGCONTEXT [stack hair, very machine-specific]
187YYLINENO?
188POSIX_SIGNALS
189POSIX_TERMIOS
190SETPROCTITLE -- breaks some places, like fbsd sendmail
191SIG* -- actual signal names? some are missing
192SIOCGIFCONF
193SO_BROADCAST
194SHMEM [krb tickets]
195VARARGS, or HAVE_VARARGS
196CBAUD
197... and B300, B9600, etc etc
198HAVE_BZERO vs memset/memcpy
199HAVE_SETVBUF
200HAVE_STRDUP
201HAVE_GETENV
202HAVE_STRSAVE
203HAVE_STBLKSIZE [stat?]
204HAVE_STREAM_H -- in sys/, ref sendmail 8.7 for IP_SRCROUTE
205FCHMOD
206INITGROUPS -- most machines seem to *have*
207SETREUID
208SNPRINTF
209SETPGRP semantics bsd vs. sys5 style
210
211There's also the issue about WHERE various .h files live, sys/ or otherwise.
212There's a BIG swamp lurking where network code of any sort lives.
213*/
214
215/* ======================== */
216/* System-specific sections */
217/* ======================== */
218
219/* By turning OFF various bits of the above, you can customize for
220 a given platform. Yes, we're ignoring the stock compiler predefines
221 and using our own plugged in via the Makefile. */
222
223/* DOS boxes, with MSC; you may need to adapt to a different compiler. */
224/* looks like later ones *do* have dirent.h, for example */
225#ifdef MSDOS
226#undef HAVE_FLOCK
227#undef HAVE_RANDOM
228#undef HAVE_LSTAT
229#undef HAVE_TERMIOS
230#undef UTMPX
231#undef HAVE_SYSINFO
232#undef HAVE_UNISTD_H
233#undef HAVE_DIRENT_H /* unless you have the k00l little wrapper from L5!! */
234#undef HAVE_STRINGS_H
235#undef HAVE_LASTLOG_H
236#undef HAVE_PATHS_H
237#undef HAVE_PARAM_H
238#undef HAVE_SYSMACROS_H
239#undef HAVE_SELECT_H
240#undef HAVE_TTYENT_H
241#endif /* MSDOS */
242
243/* buglix 4.x; dunno about 3.x on down. should be bsd4.2 */
244#ifdef ULTRIX
245#undef UTMPX
246#undef HAVE_PATHS_H
247#undef HAVE_SYSMACROS_H
248#undef HAVE_SELECT_H
249#endif /* buglix */
250
251/* some of this might still be broken on older sunoses */
252#ifdef SUNOS
253#undef VOID_MALLOC
254#undef UTMPX
255#undef HAVE_PATHS_H
256#undef HAVE_SELECT_H
257#endif /* sunos */
258
259/* "contact your vendor for a fix" */
260#ifdef SOLARIS
261/* has UTMPX */
262#undef HAVE_RANDOM
263#undef HAVE_SETPRIORITY
264#undef HAVE_STRINGS_H /* this is genuinely the case, go figure */
265#undef HAVE_PATHS_H
266#undef HAVE_SELECT_H
267#undef HAVE_TTYENT_H
268#endif /* SOLARIS */
269
270/* whatever aix variant MIT had at the time; 3.2.x?? */
271#ifdef AIX
272#undef UTMPX
273#undef HAVE_LASTLOG_H
274#define HAVE_LOGIN_H /* "special", in the educational sense */
275#endif /* aix */
276
277/* linux, which is trying as desperately as the gnu folks can to be
278 POSIXLY_CORRECT. I think I'm gonna hurl... */
279#ifdef LINUX
280#undef UTMPX
281#undef HAVE_SYSINFO
282#undef HAVE_SELECT_H
283#undef HAVE_TTYENT_H
284#endif /* linux */
285
286/* irix 5.x; may not be correct for earlier ones */
287#ifdef IRIX
288/* wow, does irix really have everything?! */
289#endif /* irix */
290
291/* osf on alphas */
292#ifdef OSF
293#undef UTMPX
294#undef HAVE_SELECT_H
295#endif /* osf */
296
297/* they's some FUCKED UP paths in this one! */
298#ifdef FREEBSD
299#undef UTMPX
300#undef HAVE_SYSINFO
301#undef HAVE_LASTLOG_H
302#undef HAVE_SYSMACROS_H
303#undef HAVE_SELECT_H /* actually a lie, but only for kernel */
304#endif /* freebsd */
305
306/* Originally from the sidewinder site, of all places, but subsequently
307 checked further under a more normal bsdi 2.0 */
308#ifdef BSDI
309#undef UTMPX
310#undef HAVE_LASTLOG_H
311#undef HAVE_SYSMACROS_H
312/* and their malloc.h was in sys/ ?! */
313#undef HAVE_SELECT_H
314#endif /* bsdi */
315
316/* netbsd/44lite, jives with amiga-netbsd from cactus */
317#ifdef NETBSD
318#undef UTMPX
319#undef HAVE_SYSINFO
320#undef HAVE_LASTLOG_H
321#undef HAVE_SELECT_H
322#endif /* netbsd */
323
324/* Hpux 9.0x, from BBN and various patches sent in */
325#ifdef HPUX
326#undef HAVE_RANDOM /* but *does* have ?rand48 -- need to consider.. */
327#undef HAVE_UTMPX
328#undef HAVE_LASTLOG_H /* has utmp/wtmp/btmp nonsense, and pututline() */
329#undef HAVE_PATHS_H
330#undef HAVE_SELECT_H
331#undef HAVE_TTYENT_H
332#endif /* hockeypux */
333
334/* Unixware [a loose definition of "unix", to be sure], 1.1.2 [at least]
335 from Brian Clapper. He wasn't sure about 2.0... */
336#ifdef UNIXWARE
337/* has UTMPX */
338#undef HAVE_SETPRIORITY
339/* NOTE: UnixWare does provide the BSD stuff, in "/usr/ucbinclude" (headers)
340 and "/usr/ucblib" (libraries). However, I've run into problems linking
341 stuff out of that version of the C library, when objects are also coming
342 out of the "regular" C library. My advice: Avoid the BSD compatibility
343 stuff wherever possible. Brian Clapper <bmc@telebase.com> */
344#undef HAVE_STRINGS_H
345#undef HAVE_PATHS_H
346#undef HAVE_TTYENT_H
347#endif /* UNIXWARE */
348
349/* A/UX 3.1.x from darieb@sandia.gov */
350#ifdef AUX
351#undef HAVE_RANDOM
352#undef HAVE_SELECT_H /* xxx: untested */
353#endif /* a/ux */
354
355/* NeXTSTEP 3.2 motorola mudge@l0pht.com xxx should also work with
356 white hardware and Sparc/HPPA. Should work with 3.3 too as it's
357 4.3 / 4.4 bsd wrapped around mach */
358#ifdef NEXT
359#undef UTMPX
360#undef HAVE_SELECT_X
361#endif /* NeXTSTEP 3.2 motorola */
362
363/* Make some "generic" assumptions if all else fails */
364#ifdef GENERIC
365#undef HAVE_FLOCK
366#if defined(SYSV) && (SYSV < 4) /* TW leftover: old SV doesnt have symlinks */
367#undef HAVE_LSTAT
368#endif /* old SYSV */
369#undef HAVE_TERMIOS
370#undef UTMPX
371#undef HAVE_PATHS_H
372#undef HAVE_SELECT_H
373#endif /* generic */
374
375/* ================ */
376#endif /* GENERIC_H */
377
diff --git a/src/usr.bin/nc/nc.1 b/src/usr.bin/nc/nc.1
new file mode 100644
index 0000000000..8d87051c73
--- /dev/null
+++ b/src/usr.bin/nc/nc.1
@@ -0,0 +1,215 @@
1.\" $OpenBSD: nc.1,v 1.5 1998/09/28 06:57:35 millert Exp $
2.\"
3.\" Copyright (c) 1996 David Sacerdote
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\" derived from this software without specific prior written permission
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd August 1, 1996
29.Dt NC 1
30.Os
31.Sh NAME
32.Nm nc
33.Nd "arbitrary TCP and UDP connections and listens"
34.Sh SYNOPSIS
35.Nm nc
36.Op Fl e Ar command
37.Op Fl g Ar intermediates
38.Op Fl G Ar hopcount
39.Op Fl i Ar interval
40.Op Fl lnrtuvz
41.Op Fl o Ar filename
42.Op Fl p Ar source port
43.Op Fl s Ar ip address
44.Op Fl w Ar timeout
45.Op Ar hostname
46.Op Ar port[s...]
47.Sh DESCRIPTION
48The
49.Nm nc
50(or
51.Nm netcat )
52utility is used for just about anything under the sun
53involving TCP or UDP. It can open TCP connections, send UDP packets,
54listen on arbitrary TCP and UDP ports, do port scanning, and source
55routing. Unlike
56.Xr telnet 1 ,
57.Nm nc
58scripts nicely, and separates error messages onto standard error instead
59of sending them to standard output, as
60.Xr telnet 1
61does with some.
62.Pp
63Destination ports can be single integers, names as listed in
64.Xr services 5 ,
65or ranges. Ranges are in the form nn-mm, and several separate ports and/or
66ranges may be specified on the command line.
67.Pp
68Common uses include:
69.Bl -bullet
70.It
71simple TCP proxies
72.It
73shell\-script based HTTP clients and servers
74.It
75network daemon testing
76.It
77source routing based connectivity testing
78.It
79and much, much more
80.El
81.Pp
82The options are as follows:
83.Bl -tag -width Ds
84.It Fl e Ar command
85Execute the specified command, using data from the network for stdin,
86and sending stdout and stderr to the network. This option is only present if
87.Nm nc
88was compiled with the GAPING_SECURITY_HOLE compile time option, since it
89allows users to make arbitrary programs available to anyone on the network.
90.It Fl g Ar intermediate-host
91Specifies a hop along a loose source routed path. Can be used more than
92once to build a chain of hop points.
93.It Fl G Ar pointer
94Positions the "hop counter" within the list of machines in the path of
95a source routed packet. Must be a multiple of 4.
96.It Fl i Ar seconds
97Specifies a delay time interval between lines of text sent and received.
98Also causes a delay time between connections to multiple ports.
99.It Fl l
100Is used to specify that
101.Nm nc
102should listen for an incoming connection, rather than initiate a
103connection to a remote host. Any hostname/IP address and port arguments
104restrict the source of inbound connections to only that address and
105source port.
106.It Fl n
107Do not do DNS lookups on any of the specified addresses or hostnames, or
108names of port numbers from /etc/services.
109.It Fl o Ar filename
110Create a hexadecimal log of data transferred in the specified file.
111Each line begins with ``<'' or ``>''. ``<'' means "from the net" and ``>''
112means "to the net".
113.It Fl p Ar port
114Specifies the source port
115.Nm nc
116should use, subject to privilege restrictions and availability.
117.It Fl r
118Specifies that source and/or destination ports should be chosen semi-randomly
119instead of sequentially within a range or in the order that the
120system assigns.
121.It Fl s Ar hostname/ip-address
122Specifies the IP of the interface which is used to send the packets.
123On some platforms, this can be used for UDP spoofing by using
124.Xr ifconfig 8
125to bring up a dummy interface with the desired source IP address.
126.It Fl t
127Causes
128.Nm nc
129to send RFC854 DON'T and WON'T responses to RFC854 DO
130and WILL requests. This makes it possible to use
131.Nm nc
132to script telnet sessions. The presence of this option can be
133enabled or disabled as a compile-time option.
134.It Fl u
135Use UDP instead of TCP.
136On most platforms,
137.Nm nc
138will behave as if a connection is established until it receives an
139ICMP packet indicating that there is no program listening to what it
140sends.
141.It Fl v
142Verbose. Cause
143.Nm nc
144to display connection information. Using
145.Fl v
146more than once will cause
147.Nm nc
148to become even more verbose.
149.It Fl w Ar timeout
150Specifies the number of seconds
151.Nm nc
152should wait before deciding that
153an attempt to establish a connection is hopeless.
154Also used to specify how long to wait for more network data after standard
155input closes.
156.It Fl z
157Specifies that
158.Nm nc
159should just scan for listening
160daemons, without sending any data to them. Diagnostic messages about refused
161connections will not be
162displayed unless
163.Fl v
164is specified twice.
165.Sh EXAMPLES
166.Bl -tag -width x
167.It Li "nc"
168Wait for the user to type what would normally be command-line
169arguments in at stdin.
170.It Li "nc example.host 42"
171Open a TCP connection to port 42 of example.host. If the connection
172fails, do not display any error messages, but simply exit.
173.It Li "nc -p 31337 example.host 42"
174Open a TCP connection to port 42 of example.host, and use port 31337
175as the source port.
176.It Li "nc -w 5 example.host 42"
177Open a TCP connection to port 42 of example.host, and time out after
178five seconds while attempting to connect.
179.It Li "nc -u example.host 53"
180Send any data from stdin
181to UDP port 53 of example.host, and display any data returned.
182.It Li "nc -s 10.1.2.3 example.host 42"
183Open a TCP connection to port 42 of example.host using 10.1.2.3 as the
184IP for the local end of the connection.
185.It Li "nc -v example.host 42"
186Open a TCP connection to port 42 of example.host, displaying some
187diagnostic messages on stderr.
188.It Li "nc -v -v example.host 42"
189Open a TCP connection to port 42 of example.host, displaying all
190diagnostic messages on stderr.
191.It Li "nc -v -z example.host 20-30"
192Attempt to open TCP connections to ports 20 through 30 of
193example.host, and report which ones
194.Nm nc
195was able to connect to.
196.It Li "nc -v -u -z -w 3 example.host 20-30"
197Send UDP packets to ports 20-30 of example.host, and report which ones
198did not respond with an ICMP packet after three seconds.
199.It Li "nc -l -p 3000"
200Listen on TCP port 3000, and once there is a connection, send stdin to
201the remote host, and send data from the remote host to stdout.
202.It Li "echo foobar | nc example.host 1000"
203Connect to port 1000 of example.host, send the string "foobar"
204followed by a newline, and move data from port 1000 of example.host to
205stdout until example.host closes the connection.
206.El
207.Sh SEE ALSO
208.Xr cat 1 ,
209.Xr telnet 1
210.Pp
211The
212.Nm netcat
213.Pa README .
214.Sh AUTHOR
215*Hobbit* [hobbit@avian.org]
diff --git a/src/usr.bin/nc/netcat.blurb b/src/usr.bin/nc/netcat.blurb
new file mode 100644
index 0000000000..2c540ad9dc
--- /dev/null
+++ b/src/usr.bin/nc/netcat.blurb
@@ -0,0 +1,61 @@
1Netcat 1.10 is an updated release of Netcat, a simple Unix utility which reads
2and writes data across network connections using TCP or UDP protocol. It is
3designed to be a reliable "back-end" tool that can be used directly or easily
4driven by other programs and scripts. At the same time it is a feature-rich
5network debugging and exploration tool, since it can create almost any kind of
6connection you would need and has several interesting built-in capabilities.
7
8Some of netcat's major features are:
9
10 Outbound or inbound connections, TCP or UDP, to or from any ports
11 Full DNS forward/reverse checking, with appropriate warnings
12 Ability to use any local source port
13 Ability to use any locally-configured network source address
14 Built-in port-scanning capabilities, with randomizer
15 Built-in loose source-routing capability
16 Can read command line arguments from standard input
17 Slow-send mode, one line every N seconds
18 Hex dump of transmitted and received data
19 Optional ability to let another program service established connections
20 Optional telnet-options responder
21
22A very short list of potential uses:
23
24 Script backends
25 Scanning ports and inventorying services, automated probes
26 Backup handlers
27 File transfers
28 Server testing, simulation, debugging, and hijacking
29 Firewall testing
30 Proxy gatewaying
31 Network performance testing
32 Address spoofing tests
33 Protecting X servers
34 1001 other uses you'll likely come up with
35
36Changes between the 1.00 release and this release:
37
38 Better portability -- updated generic.h and Makefile [thanx folks!]
39 Indication of local-end interface address on inbound connections
40 That's *Dave* Borman's telnet, not Paul Borman...
41 Better indication of DNS errors
42 Total byte counts printed if -v -v is used
43 A bunch of front-end driver companion programs and scripts
44 Better handling of stdin arguments-plus-data
45 Hex-dump feature
46 Telnet responder
47 Program exec works inbound or outbound now
48
49Netcat and the associated package is a product of Avian Research, and is freely
50available in full source form with no restrictions save an obligation to give
51credit where due. Get it via anonymous FTP at avian.org:/src/hacks/nc110.tgz
52which is a gzipped tar file and not to be confused with its version 1.00
53precursor, nc100.tgz. Other distribution formats can be accomodated upon
54request. Netcat is also mirrored at the following [faster] sites:
55
56 zippy.telcom.arizona.edu:/pub/mirrors/avian.org/hacks/nc110.tgz
57 ftp.sterling.com:/mirrors/avian.org/src/hacks/nc110.tgz
58 coast.cs.purdue.edu:/pub/tools/unix/netcat/nc110.tgz
59 ftp.rge.com:/pub/security/coast/mirrors/avian.org/netcat/nc110.tgz
60
61_H* 960320
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c
new file mode 100644
index 0000000000..bb0b30749d
--- /dev/null
+++ b/src/usr.bin/nc/netcat.c
@@ -0,0 +1,1670 @@
1/* Netcat 1.10 RELEASE 960320
2
3 A damn useful little "backend" utility begun 950915 or thereabouts,
4 as *Hobbit*'s first real stab at some sockets programming. Something that
5 should have and indeed may have existed ten years ago, but never became a
6 standard Unix utility. IMHO, "nc" could take its place right next to cat,
7 cp, rm, mv, dd, ls, and all those other cryptic and Unix-like things.
8
9 Read the README for the whole story, doc, applications, etc.
10
11 Layout:
12 conditional includes:
13 includes:
14 handy defines:
15 globals:
16 malloced globals:
17 cmd-flag globals:
18 support routines:
19 readwrite select loop:
20 main:
21
22 bluesky:
23 parse ranges of IP address as well as ports, perhaps
24 RAW mode!
25 backend progs to grab a pty and look like a real telnetd?!
26 backend progs to do various encryption modes??!?!
27*/
28
29#include "generic.h" /* same as with L5, skey, etc */
30
31/* conditional includes -- a very messy section which you may have to dink
32 for your own architecture [and please send diffs...]: */
33/* #undef _POSIX_SOURCE /* might need this for something? */
34#define HAVE_BIND /* ASSUMPTION -- seems to work everywhere! */
35#define HAVE_HELP /* undefine if you dont want the help text */
36/* #define ANAL /* if you want case-sensitive DNS matching */
37
38#ifdef HAVE_STDLIB_H
39#include <stdlib.h>
40#else
41#include <malloc.h>
42#endif
43#ifdef HAVE_SELECT_H /* random SV variants need this */
44#include <sys/select.h>
45#endif
46
47/* have to do this *before* including types.h. xxx: Linux still has it wrong */
48#ifdef FD_SETSIZE /* should be in types.h, butcha never know. */
49#undef FD_SETSIZE /* if we ever need more than 16 active */
50#endif /* fd's, something is horribly wrong! */
51#define FD_SETSIZE 16 /* <-- this'll give us a long anyways, wtf */
52#include <sys/types.h> /* *now* do it. Sigh, this is broken */
53
54#ifdef HAVE_RANDOM /* aficionados of ?rand48() should realize */
55#define SRAND srandom /* that this doesn't need *strong* random */
56#define RAND random /* numbers just to mix up port numbers!! */
57#else
58#define SRAND srand
59#define RAND rand
60#endif /* HAVE_RANDOM */
61
62/* includes: */
63#include <sys/time.h> /* timeval, time_t */
64#include <setjmp.h> /* jmp_buf et al */
65#include <sys/socket.h> /* basics, SO_ and AF_ defs, sockaddr, ... */
66#include <netinet/in.h> /* sockaddr_in, htons, in_addr */
67#include <netinet/in_systm.h> /* misc crud that netinet/ip.h references */
68#include <netinet/ip.h> /* IPOPT_LSRR, header stuff */
69#include <netdb.h> /* hostent, gethostby*, getservby* */
70#include <arpa/inet.h> /* inet_ntoa */
71#include <stdio.h>
72#include <string.h> /* strcpy, strchr, yadda yadda */
73#include <errno.h>
74#include <signal.h>
75#include <fcntl.h> /* O_WRONLY et al */
76
77/* handy stuff: */
78#define SA struct sockaddr /* socket overgeneralization braindeath */
79#define SAI struct sockaddr_in /* ... whoever came up with this model */
80#define IA struct in_addr /* ... should be taken out and shot, */
81 /* ... not that TLI is any better. sigh.. */
82#define SLEAZE_PORT 31337 /* for UDP-scan RTT trick, change if ya want */
83#define USHORT unsigned short /* use these for options an' stuff */
84#define BIGSIZ 8192 /* big buffers */
85
86#ifndef INADDR_NONE
87#define INADDR_NONE 0xffffffff
88#endif
89
90struct host_poop {
91 char name[MAXHOSTNAMELEN]; /* dns name */
92 char addrs[8][24]; /* ascii-format IP addresses */
93 struct in_addr iaddrs[8]; /* real addresses: in_addr.s_addr: ulong */
94};
95#define HINF struct host_poop
96
97struct port_poop {
98 char name [64]; /* name in /etc/services */
99 char anum [8]; /* ascii-format number */
100 USHORT num; /* real host-order number */
101};
102#define PINF struct port_poop
103
104/* globals: */
105jmp_buf jbuf; /* timer crud */
106int jval = 0; /* timer crud */
107int netfd = -1;
108int ofd = 0; /* hexdump output fd */
109static char unknown[] = "(UNKNOWN)";
110static char p_tcp[] = "tcp"; /* for getservby* */
111static char p_udp[] = "udp";
112#ifdef HAVE_BIND
113extern int h_errno;
114/* stolen almost wholesale from bsd herror.c */
115static char * h_errs[] = {
116 "Error 0", /* but we *don't* use this */
117 "Unknown host", /* 1 HOST_NOT_FOUND */
118 "Host name lookup failure", /* 2 TRY_AGAIN */
119 "Unknown server error", /* 3 NO_RECOVERY */
120 "No address associated with name", /* 4 NO_ADDRESS */
121};
122#else
123int h_errno; /* just so we *do* have it available */
124#endif /* HAVE_BIND */
125int gatesidx = 0; /* LSRR hop count */
126int gatesptr = 4; /* initial LSRR pointer, settable */
127USHORT Single = 1; /* zero if scanning */
128unsigned int insaved = 0; /* stdin-buffer size for multi-mode */
129unsigned int wrote_out = 0; /* total stdout bytes */
130unsigned int wrote_net = 0; /* total net bytes */
131static char wrote_txt[] = " sent %d, rcvd %d";
132static char hexnibs[20] = "0123456789abcdef ";
133
134/* will malloc up the following globals: */
135struct timeval * timer1 = NULL;
136struct timeval * timer2 = NULL;
137SAI * lclend = NULL; /* sockaddr_in structs */
138SAI * remend = NULL;
139HINF ** gates = NULL; /* LSRR hop hostpoop */
140char * optbuf = NULL; /* LSRR or sockopts */
141char * bigbuf_in; /* data buffers */
142char * bigbuf_net;
143fd_set * ding1; /* for select loop */
144fd_set * ding2;
145PINF * portpoop = NULL; /* for getportpoop / getservby* */
146unsigned char * stage = NULL; /* hexdump line buffer */
147
148/* global cmd flags: */
149USHORT o_alla = 0;
150unsigned int o_interval = 0;
151USHORT o_listen = 0;
152USHORT o_nflag = 0;
153USHORT o_wfile = 0;
154USHORT o_random = 0;
155USHORT o_udpmode = 0;
156USHORT o_verbose = 0;
157unsigned int o_wait = 0;
158USHORT o_zero = 0;
159/* o_tn in optional section */
160
161/* Debug macro: squirt whatever message and sleep a bit so we can see it go
162 by. need to call like Debug ((stuff)) [with no ; ] so macro args match!
163 Beware: writes to stdOUT... */
164#ifdef DEBUG
165#define Debug(x) printf x; printf ("\n"); fflush (stdout); sleep (1);
166#else
167#define Debug(x) /* nil... */
168#endif
169
170
171/* support routines -- the bulk of this thing. Placed in such an order that
172 we don't have to forward-declare anything: */
173
174/* holler :
175 fake varargs -- need to do this way because we wind up calling through
176 more levels of indirection than vanilla varargs can handle, and not all
177 machines have vfprintf/vsyslog/whatever! 6 params oughta be enough. */
178void holler (str, p1, p2, p3, p4, p5, p6)
179 char * str;
180 char * p1, * p2, * p3, * p4, * p5, * p6;
181{
182 if (o_verbose) {
183 fprintf (stderr, str, p1, p2, p3, p4, p5, p6);
184#ifdef HAVE_BIND
185 if (h_errno) { /* if host-lookup variety of error ... */
186 if (h_errno > 4) /* oh no you don't, either */
187 fprintf (stderr, "preposterous h_errno: %d", h_errno);
188 else
189 fprintf (stderr, h_errs[h_errno]); /* handle it here */
190 h_errno = 0; /* and reset for next call */
191 }
192#endif
193 if (errno) { /* this gives funny-looking messages, but */
194 perror (" "); /* it's more portable than sys_errlist[]... */
195 } else /* xxx: do something better? */
196 fprintf (stderr, "\n");
197 fflush (stderr);
198 }
199} /* holler */
200
201/* bail :
202 error-exit handler, callable from anywhere */
203void bail (str, p1, p2, p3, p4, p5, p6)
204 char * str;
205 char * p1, * p2, * p3, * p4, * p5, * p6;
206{
207 o_verbose = 1;
208 holler (str, p1, p2, p3, p4, p5, p6);
209 close (netfd);
210 sleep (1);
211 exit (1);
212} /* bail */
213
214/* catch :
215 no-brainer interrupt handler */
216void catch ()
217{
218 errno = 0;
219 if (o_verbose > 1) /* normally we don't care */
220 bail (wrote_txt, wrote_net, wrote_out);
221 bail (" punt!");
222}
223
224/* timeout and other signal handling cruft */
225void tmtravel ()
226{
227 signal (SIGALRM, SIG_IGN);
228 alarm (0);
229 if (jval == 0)
230 bail ("spurious timer interrupt!");
231 longjmp (jbuf, jval);
232}
233
234/* arm :
235 set the timer. Zero secs arg means unarm */
236void arm (num, secs)
237 unsigned int num;
238 unsigned int secs;
239{
240 if (secs == 0) { /* reset */
241 signal (SIGALRM, SIG_IGN);
242 alarm (0);
243 jval = 0;
244 } else { /* set */
245 signal (SIGALRM, tmtravel);
246 alarm (secs);
247 jval = num;
248 } /* if secs */
249} /* arm */
250
251/* Hmalloc :
252 malloc up what I want, rounded up to *4, and pre-zeroed. Either succeeds
253 or bails out on its own, so that callers don't have to worry about it. */
254char * Hmalloc (size)
255 unsigned int size;
256{
257 unsigned int s = (size + 4) & 0xfffffffc; /* 4GB?! */
258 char * p = malloc (s);
259 if (p != NULL)
260 memset (p, 0, s);
261 else
262 bail ("Hmalloc %d failed", s);
263 return (p);
264} /* Hmalloc */
265
266/* findline :
267 find the next newline in a buffer; return inclusive size of that "line",
268 or the entire buffer size, so the caller knows how much to then write().
269 Not distinguishing \n vs \r\n for the nonce; it just works as is... */
270unsigned int findline (buf, siz)
271 char * buf;
272 unsigned int siz;
273{
274 register char * p;
275 register int x;
276 if (! buf) /* various sanity checks... */
277 return (0);
278 if (siz > BIGSIZ)
279 return (0);
280 x = siz;
281 for (p = buf; x > 0; x--) {
282 if (*p == '\n') {
283 x = (int) (p - buf);
284 x++; /* 'sokay if it points just past the end! */
285Debug (("findline returning %d", x))
286 return (x);
287 }
288 p++;
289 } /* for */
290Debug (("findline returning whole thing: %d", siz))
291 return (siz);
292} /* findline */
293
294/* comparehosts :
295 cross-check the host_poop we have so far against new gethostby*() info,
296 and holler about mismatches. Perhaps gratuitous, but it can't hurt to
297 point out when someone's DNS is fukt. Returns 1 if mismatch, in case
298 someone else wants to do something about it. */
299int comparehosts (poop, hp)
300 HINF * poop;
301 struct hostent * hp;
302{
303 errno = 0;
304 h_errno = 0;
305/* The DNS spec is officially case-insensitive, but for those times when you
306 *really* wanna see any and all discrepancies, by all means define this. */
307#ifdef ANAL
308 if (strcmp (poop->name, hp->h_name) != 0) { /* case-sensitive */
309#else
310 if (strcasecmp (poop->name, hp->h_name) != 0) { /* normal */
311#endif
312 holler ("DNS fwd/rev mismatch: %s != %s", poop->name, hp->h_name);
313 return (1);
314 }
315 return (0);
316/* ... do we need to do anything over and above that?? */
317} /* comparehosts */
318
319/* gethostpoop :
320 resolve a host 8 ways from sunday; return a new host_poop struct with its
321 info. The argument can be a name or [ascii] IP address; it will try its
322 damndest to deal with it. "numeric" governs whether we do any DNS at all,
323 and we also check o_verbose for what's appropriate work to do. */
324HINF * gethostpoop (name, numeric)
325 char * name;
326 USHORT numeric;
327{
328 struct hostent * hostent;
329 struct in_addr iaddr;
330 register HINF * poop = NULL;
331 register int x;
332
333/* I really want to strangle the twit who dreamed up all these sockaddr and
334 hostent abstractions, and then forced them all to be incompatible with
335 each other so you *HAVE* to do all this ridiculous casting back and forth.
336 If that wasn't bad enough, all the doc insists on referring to local ports
337 and addresses as "names", which makes NO sense down at the bare metal.
338
339 What an absolutely horrid paradigm, and to think of all the people who
340 have been wasting significant amounts of time fighting with this stupid
341 deliberate obfuscation over the last 10 years... then again, I like
342 languages wherein a pointer is a pointer, what you put there is your own
343 business, the compiler stays out of your face, and sheep are nervous.
344 Maybe that's why my C code reads like assembler half the time... */
345
346/* If we want to see all the DNS stuff, do the following hair --
347 if inet_addr, do reverse and forward with any warnings; otherwise try
348 to do forward and reverse with any warnings. In other words, as long
349 as we're here, do a complete DNS check on these clowns. Yes, it slows
350 things down a bit for a first run, but once it's cached, who cares? */
351
352 errno = 0;
353 h_errno = 0;
354 if (name)
355 poop = (HINF *) Hmalloc (sizeof (HINF));
356 if (! poop)
357 bail ("gethostpoop fuxored");
358 strcpy (poop->name, unknown); /* preload it */
359/* see wzv:workarounds.c for dg/ux return-a-struct inet_addr lossage */
360 iaddr.s_addr = inet_addr (name);
361
362 if (iaddr.s_addr == INADDR_NONE) { /* here's the great split: names... */
363 if (numeric)
364 bail ("Can't parse %s as an IP address", name);
365 hostent = gethostbyname (name);
366 if (! hostent)
367/* failure to look up a name is fatal, since we can't do anything with it */
368 bail ("%s: forward host lookup failed: ", name);
369 strncpy (poop->name, hostent->h_name, MAXHOSTNAMELEN - 1);
370 poop->name[MAXHOSTNAMELEN - 1] = '\0';
371 for (x = 0; hostent->h_addr_list[x] && (x < 8); x++) {
372 memcpy (&poop->iaddrs[x], hostent->h_addr_list[x], sizeof (IA));
373 strncpy (poop->addrs[x], inet_ntoa (poop->iaddrs[x]),
374 sizeof (poop->addrs[0])-1);
375 poop->addrs[x][sizeof (poop->addrs[0]) - 1] = '\0';
376 } /* for x -> addrs, part A */
377 if (! o_verbose) /* if we didn't want to see the */
378 return (poop); /* inverse stuff, we're done. */
379/* do inverse lookups in separate loop based on our collected forward addrs,
380 since gethostby* tends to crap into the same buffer over and over */
381 for (x = 0; poop->iaddrs[x].s_addr && (x < 8); x++) {
382 hostent = gethostbyaddr ((char *)&poop->iaddrs[x],
383 sizeof (IA), AF_INET);
384 if ((! hostent) || (! hostent-> h_name))
385 holler ("Warning: inverse host lookup failed for %s: ",
386 poop->addrs[x]);
387 else
388 (void) comparehosts (poop, hostent);
389 } /* for x -> addrs, part B */
390
391 } else { /* not INADDR_NONE: numeric addresses... */
392 memcpy (poop->iaddrs, &iaddr, sizeof (IA));
393 strncpy (poop->addrs[0], inet_ntoa (iaddr), sizeof (poop->addrs)-1);
394 poop->addrs[0][sizeof (poop->addrs)-1] = '\0';
395 if (numeric) /* if numeric-only, we're done */
396 return (poop);
397 if (! o_verbose) /* likewise if we don't want */
398 return (poop); /* the full DNS hair */
399 hostent = gethostbyaddr ((char *) &iaddr, sizeof (IA), AF_INET);
400/* numeric or not, failure to look up a PTR is *not* considered fatal */
401 if (! hostent)
402 holler ("%s: inverse host lookup failed: ", name);
403 else {
404 strncpy (poop->name, hostent->h_name, MAXHOSTNAMELEN - 1);
405 poop->name[MAXHOSTNAMELEN-1] = '\0';
406 hostent = gethostbyname (poop->name);
407 if ((! hostent) || (! hostent->h_addr_list[0]))
408 holler ("Warning: forward host lookup failed for %s: ",
409 poop->name);
410 else
411 (void) comparehosts (poop, hostent);
412 } /* if hostent */
413 } /* INADDR_NONE Great Split */
414
415/* whatever-all went down previously, we should now have a host_poop struct
416 with at least one IP address in it. */
417 h_errno = 0;
418 return (poop);
419} /* gethostpoop */
420
421/* getportpoop :
422 Same general idea as gethostpoop -- look up a port in /etc/services, fill
423 in global port_poop, but return the actual port *number*. Pass ONE of:
424 pstring to resolve stuff like "23" or "exec";
425 pnum to reverse-resolve something that's already a number.
426 If o_nflag is on, fill in what we can but skip the getservby??? stuff.
427 Might as well have consistent behavior here, and it *is* faster. */
428USHORT getportpoop (pstring, pnum)
429 char * pstring;
430 unsigned int pnum;
431{
432 struct servent * servent;
433 register int x;
434 register int y;
435 char * whichp = p_tcp;
436 if (o_udpmode)
437 whichp = p_udp;
438 portpoop->name[0] = '?'; /* fast preload */
439 portpoop->name[1] = '\0';
440
441/* case 1: reverse-lookup of a number; placed first since this case is much
442 more frequent if we're scanning */
443 if (pnum) {
444 if (pstring) /* one or the other, pleeze */
445 return (0);
446 x = pnum;
447 if (o_nflag) /* go faster, skip getservbyblah */
448 goto gp_finish;
449 y = htons (x); /* gotta do this -- see Fig.1 below */
450 servent = getservbyport (y, whichp);
451 if (servent) {
452 y = ntohs (servent->s_port);
453 if (x != y) /* "never happen" */
454 holler ("Warning: port-bynum mismatch, %d != %d", x, y);
455 strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name)-1);
456 portpoop->name[sizeof (portpoop->name)-1] = '\0';
457 } /* if servent */
458 goto gp_finish;
459 } /* if pnum */
460
461/* case 2: resolve a string, but we still give preference to numbers instead
462 of trying to resolve conflicts. None of the entries in *my* extensive
463 /etc/services begins with a digit, so this should "always work" unless
464 you're at 3com and have some company-internal services defined... */
465 if (pstring) {
466 if (pnum) /* one or the other, pleeze */
467 return (0);
468 x = atoi (pstring);
469 if (x)
470 return (getportpoop (NULL, x)); /* recurse for numeric-string-arg */
471 if (o_nflag) /* can't use names! */
472 return (0);
473 servent = getservbyname (pstring, whichp);
474 if (servent) {
475 strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name)-1);
476 portpoop->name[sizeof (portpoop->name)-1] = '\0';
477 x = ntohs (servent->s_port);
478 goto gp_finish;
479 } /* if servent */
480 } /* if pstring */
481
482 return (0); /* catches any problems so far */
483
484/* Obligatory netdb.h-inspired rant: servent.s_port is supposed to be an int.
485 Despite this, we still have to treat it as a short when copying it around.
486 Not only that, but we have to convert it *back* into net order for
487 getservbyport to work. Manpages generally aren't clear on all this, but
488 there are plenty of examples in which it is just quietly done. More BSD
489 lossage... since everything getserv* ever deals with is local to our own
490 host, why bother with all this network-order/host-order crap at all?!
491 That should be saved for when we want to actually plug the port[s] into
492 some real network calls -- and guess what, we have to *re*-convert at that
493 point as well. Fuckheads. */
494
495gp_finish:
496/* Fall here whether or not we have a valid servent at this point, with
497 x containing our [host-order and therefore useful, dammit] port number */
498 sprintf (portpoop->anum, "%d", x); /* always load any numeric specs! */
499 portpoop->num = (x & 0xffff); /* ushort, remember... */
500 return (portpoop->num);
501} /* getportpoop */
502
503/* nextport :
504 Come up with the next port to try, be it random or whatever. "block" is
505 a ptr to randports array, whose bytes [so far] carry these meanings:
506 0 ignore
507 1 to be tested
508 2 tested [which is set as we find them here]
509 returns a USHORT random port, or 0 if all the t-b-t ones are used up. */
510USHORT nextport (block)
511 char * block;
512{
513 register unsigned int x;
514 register unsigned int y;
515
516 y = 70000; /* high safety count for rnd-tries */
517 while (y > 0) {
518 x = (RAND() & 0xffff);
519 if (block[x] == 1) { /* try to find a not-done one... */
520 block[x] = 2;
521 break;
522 }
523 x = 0; /* bummer. */
524 y--;
525 } /* while y */
526 if (x)
527 return (x);
528
529 y = 65535; /* no random one, try linear downsearch */
530 while (y > 0) { /* if they're all used, we *must* be sure! */
531 if (block[y] == 1) {
532 block[y] = 2;
533 break;
534 }
535 y--;
536 } /* while y */
537 if (y)
538 return (y); /* at least one left */
539
540 return (0); /* no more left! */
541} /* nextport */
542
543/* loadports :
544 set "to be tested" indications in BLOCK, from LO to HI. Almost too small
545 to be a separate routine, but makes main() a little cleaner... */
546void loadports (block, lo, hi)
547 char * block;
548 USHORT lo;
549 USHORT hi;
550{
551 USHORT x;
552
553 if (! block)
554 bail ("loadports: no block?!");
555 if ((! lo) || (! hi))
556 bail ("loadports: bogus values %d, %d", lo, hi);
557 x = hi;
558 while (lo <= x) {
559 block[x] = 1;
560 x--;
561 }
562} /* loadports */
563
564#ifdef GAPING_SECURITY_HOLE
565char * pr00gie = NULL; /* global ptr to -e arg */
566
567/* doexec :
568 fiddle all the file descriptors around, and hand off to another prog. Sort
569 of like a one-off "poor man's inetd". This is the only section of code
570 that would be security-critical, which is why it's ifdefed out by default.
571 Use at your own hairy risk; if you leave shells lying around behind open
572 listening ports you deserve to lose!! */
573doexec (fd)
574 int fd;
575{
576 register char * p;
577
578 dup2 (fd, 0); /* the precise order of fiddlage */
579 close (fd); /* is apparently crucial; this is */
580 dup2 (0, 1); /* swiped directly out of "inetd". */
581 dup2 (0, 2);
582 p = strrchr (pr00gie, '/'); /* shorter argv[0] */
583 if (p)
584 p++;
585 else
586 p = pr00gie;
587Debug (("gonna exec %s as %s...", pr00gie, p))
588 execl (pr00gie, p, NULL);
589 bail ("exec %s failed", pr00gie); /* this gets sent out. Hmm... */
590} /* doexec */
591#endif /* GAPING_SECURITY_HOLE */
592
593/* doconnect :
594 do all the socket stuff, and return an fd for one of
595 an open outbound TCP connection
596 a UDP stub-socket thingie
597 with appropriate socket options set up if we wanted source-routing, or
598 an unconnected TCP or UDP socket to listen on.
599 Examines various global o_blah flags to figure out what-all to do. */
600int doconnect (rad, rp, lad, lp)
601 IA * rad;
602 USHORT rp;
603 IA * lad;
604 USHORT lp;
605{
606 register int nnetfd;
607 register int rr;
608 int x, y;
609 errno = 0;
610
611/* grab a socket; set opts */
612newskt:
613 if (o_udpmode)
614 nnetfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
615 else
616 nnetfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
617 if (nnetfd < 0)
618 bail ("Can't get socket");
619 if (nnetfd == 0) /* if stdin was closed this might *be* 0, */
620 goto newskt; /* so grab another. See text for why... */
621 x = 1;
622 rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x));
623 if (rr == -1)
624 holler ("nnetfd reuseaddr failed"); /* ??? */
625#ifdef SO_REUSEPORT /* doesnt exist everywhere... */
626 rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
627 if (rr == -1)
628 holler ("nnetfd reuseport failed"); /* ??? */
629#endif
630#if 0
631/* If you want to screw with RCVBUF/SNDBUF, do it here. Liudvikas Bukys at
632 Rochester sent this example, which would involve YET MORE options and is
633 just archived here in case you want to mess with it. o_xxxbuf are global
634 integers set in main() getopt loop, and check for rr == 0 afterward. */
635 rr = setsockopt(nnetfd, SOL_SOCKET, SO_RCVBUF, &o_rcvbuf, sizeof o_rcvbuf);
636 rr = setsockopt(nnetfd, SOL_SOCKET, SO_SNDBUF, &o_sndbuf, sizeof o_sndbuf);
637#endif
638
639 /* fill in all the right sockaddr crud */
640 lclend->sin_family = AF_INET;
641
642/* fill in all the right sockaddr crud */
643 lclend->sin_family = AF_INET;
644 remend->sin_family = AF_INET;
645
646/* if lad/lp, do appropriate binding */
647 if (lad)
648 memcpy (&lclend->sin_addr.s_addr, lad, sizeof (IA));
649 if (lp)
650 lclend->sin_port = htons (lp);
651 rr = 0;
652 if (lad || lp) {
653 x = (int) lp;
654/* try a few times for the local bind, a la ftp-data-port... */
655 for (y = 4; y > 0; y--) {
656 rr = bind (nnetfd, (SA *)lclend, sizeof (SA));
657 if (rr == 0)
658 break;
659 if (errno != EADDRINUSE)
660 break;
661 else {
662 holler ("retrying local %s:%d", inet_ntoa (lclend->sin_addr), lp);
663 sleep (2);
664 errno = 0; /* clear from sleep */
665 } /* if EADDRINUSE */
666 } /* for y counter */
667 } /* if lad or lp */
668 if (rr)
669 bail ("Can't grab %s:%d with bind",
670 inet_ntoa(lclend->sin_addr), lp);
671
672 if (o_listen)
673 return (nnetfd); /* thanks, that's all for today */
674
675 memcpy (&remend->sin_addr.s_addr, rad, sizeof (IA));
676 remend->sin_port = htons (rp);
677
678/* rough format of LSRR option and explanation of weirdness.
679Option comes after IP-hdr dest addr in packet, padded to *4, and ihl > 5.
680IHL is multiples of 4, i.e. real len = ip_hl << 2.
681 type 131 1 ; 0x83: copied, option class 0, number 3
682 len 1 ; of *whole* option!
683 pointer 1 ; nxt-hop-addr; 1-relative, not 0-relative
684 addrlist... var ; 4 bytes per hop-addr
685 pad-to-32 var ; ones, i.e. "NOP"
686
687If we want to route A -> B via hops C and D, we must add C, D, *and* B to the
688options list. Why? Because when we hand the kernel A -> B with list C, D, B
689the "send shuffle" inside the kernel changes it into A -> C with list D, B and
690the outbound packet gets sent to C. If B wasn't also in the hops list, the
691final destination would have been lost at this point.
692
693When C gets the packet, it changes it to A -> D with list C', B where C' is
694the interface address that C used to forward the packet. This "records" the
695route hop from B's point of view, i.e. which address points "toward" B. This
696is to make B better able to return the packets. The pointer gets bumped by 4,
697so that D does the right thing instead of trying to forward back to C.
698
699When B finally gets the packet, it sees that the pointer is at the end of the
700LSRR list and is thus "completed". B will then try to use the packet instead
701of forwarding it, i.e. deliver it up to some application.
702
703Note that by moving the pointer yourself, you could send the traffic directly
704to B but have it return via your preconstructed source-route. Playing with
705this and watching "tcpdump -v" is the best way to understand what's going on.
706
707Only works for TCP in BSD-flavor kernels. UDP is a loss; udp_input calls
708stripoptions() early on, and the code to save the srcrt is notdef'ed.
709Linux is also still a loss at 1.3.x it looks like; the lsrr code is { }...
710*/
711
712/* if any -g arguments were given, set up source-routing. We hit this after
713 the gates are all looked up and ready to rock, any -G pointer is set,
714 and gatesidx is now the *number* of hops */
715 if (gatesidx) { /* if we wanted any srcrt hops ... */
716/* don't even bother compiling if we can't do IP options here! */
717#ifdef IP_OPTIONS
718 if (! optbuf) { /* and don't already *have* a srcrt set */
719 char * opp; /* then do all this setup hair */
720 optbuf = Hmalloc (48);
721 opp = optbuf;
722 *opp++ = IPOPT_LSRR; /* option */
723 *opp++ = (char)
724 (((gatesidx + 1) * sizeof (IA)) + 3) & 0xff; /* length */
725 *opp++ = gatesptr; /* pointer */
726/* opp now points at first hop addr -- insert the intermediate gateways */
727 for ( x = 0; x < gatesidx; x++) {
728 memcpy (opp, gates[x]->iaddrs, sizeof (IA));
729 opp += sizeof (IA);
730 }
731/* and tack the final destination on the end [needed!] */
732 memcpy (opp, rad, sizeof (IA));
733 opp += sizeof (IA);
734 *opp = IPOPT_NOP; /* alignment filler */
735 } /* if empty optbuf */
736/* calculate length of whole option mess, which is (3 + [hops] + [final] + 1),
737 and apply it [have to do this every time through, of course] */
738 x = ((gatesidx + 1) * sizeof (IA)) + 4;
739 rr = setsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, x);
740 if (rr == -1)
741 bail ("srcrt setsockopt fuxored");
742#else /* IP_OPTIONS */
743 holler ("Warning: source routing unavailable on this machine, ignoring");
744#endif /* IP_OPTIONS*/
745 } /* if gatesidx */
746
747/* wrap connect inside a timer, and hit it */
748 arm (1, o_wait);
749 if (setjmp (jbuf) == 0) {
750 rr = connect (nnetfd, (SA *)remend, sizeof (SA));
751 } else { /* setjmp: connect failed... */
752 rr = -1;
753 errno = ETIMEDOUT; /* fake it */
754 }
755 arm (0, 0);
756 if (rr == 0)
757 return (nnetfd);
758 close (nnetfd); /* clean up junked socket FD!! */
759 return (-1);
760} /* doconnect */
761
762/* dolisten :
763 just like doconnect, and in fact calls a hunk of doconnect, but listens for
764 incoming and returns an open connection *from* someplace. If we were
765 given host/port args, any connections from elsewhere are rejected. This
766 in conjunction with local-address binding should limit things nicely... */
767int dolisten (rad, rp, lad, lp)
768 IA * rad;
769 USHORT rp;
770 IA * lad;
771 USHORT lp;
772{
773 register int nnetfd;
774 register int rr;
775 HINF * whozis = NULL;
776 int x;
777 char * cp;
778 USHORT z;
779 errno = 0;
780
781/* Pass everything off to doconnect, who in o_listen mode just gets a socket */
782 nnetfd = doconnect (rad, rp, lad, lp);
783 if (nnetfd <= 0)
784 return (-1);
785 if (o_udpmode) { /* apparently UDP can listen ON */
786 if (! lp) /* "port 0", but that's not useful */
787 bail ("UDP listen needs -p arg");
788 } else {
789 rr = listen (nnetfd, 1); /* gotta listen() before we can get */
790 if (rr < 0) /* our local random port. sheesh. */
791 bail ("local listen fuxored");
792 }
793
794/* Various things that follow temporarily trash bigbuf_net, which might contain
795 a copy of any recvfrom()ed packet, but we'll read() another copy later. */
796
797/* I can't believe I have to do all this to get my own goddamn bound address
798 and port number. It should just get filled in during bind() or something.
799 All this is only useful if we didn't say -p for listening, since if we
800 said -p we *know* what port we're listening on. At any rate we won't bother
801 with it all unless we wanted to see it, although listening quietly on a
802 random unknown port is probably not very useful without "netstat". */
803 if (o_verbose) {
804 x = sizeof (SA); /* how 'bout getsockNUM instead, pinheads?! */
805 rr = getsockname (nnetfd, (SA *) lclend, &x);
806 if (rr < 0)
807 holler ("local getsockname failed");
808 strcpy (bigbuf_net, "listening on ["); /* buffer reuse... */
809 if (lclend->sin_addr.s_addr)
810 strcat (bigbuf_net, inet_ntoa (lclend->sin_addr));
811 else
812 strcat (bigbuf_net, "any");
813 strcat (bigbuf_net, "] %d ...");
814 z = ntohs (lclend->sin_port);
815 holler (bigbuf_net, z);
816 } /* verbose -- whew!! */
817
818/* UDP is a speeeeecial case -- we have to do I/O *and* get the calling
819 party's particulars all at once, listen() and accept() don't apply.
820 At least in the BSD universe, however, recvfrom/PEEK is enough to tell
821 us something came in, and we can set things up so straight read/write
822 actually does work after all. Yow. YMMV on strange platforms! */
823 if (o_udpmode) {
824 x = sizeof (SA); /* retval for recvfrom */
825 arm (2, o_wait); /* might as well timeout this, too */
826 if (setjmp (jbuf) == 0) { /* do timeout for initial connect */
827 rr = recvfrom /* and here we block... */
828 (nnetfd, bigbuf_net, BIGSIZ, MSG_PEEK, (SA *) remend, &x);
829Debug (("dolisten/recvfrom ding, rr = %d, netbuf %s ", rr, bigbuf_net))
830 } else
831 goto dol_tmo; /* timeout */
832 arm (0, 0);
833/* I'm not completely clear on how this works -- BSD seems to make UDP
834 just magically work in a connect()ed context, but we'll undoubtedly run
835 into systems this deal doesn't work on. For now, we apparently have to
836 issue a connect() on our just-tickled socket so we can write() back.
837 Again, why the fuck doesn't it just get filled in and taken care of?!
838 This hack is anything but optimal. Basically, if you want your listener
839 to also be able to send data back, you need this connect() line, which
840 also has the side effect that now anything from a different source or even a
841 different port on the other end won't show up and will cause ICMP errors.
842 I guess that's what they meant by "connect".
843 Let's try to remember what the "U" is *really* for, eh? */
844 rr = connect (nnetfd, (SA *)remend, sizeof (SA));
845 goto whoisit;
846 } /* o_udpmode */
847
848/* fall here for TCP */
849 x = sizeof (SA); /* retval for accept */
850 arm (2, o_wait); /* wrap this in a timer, too; 0 = forever */
851 if (setjmp (jbuf) == 0) {
852 rr = accept (nnetfd, (SA *)remend, &x);
853 } else
854 goto dol_tmo; /* timeout */
855 arm (0, 0);
856 close (nnetfd); /* dump the old socket */
857 nnetfd = rr; /* here's our new one */
858
859whoisit:
860 if (rr < 0)
861 goto dol_err; /* bail out if any errors so far */
862
863/* If we can, look for any IP options. Useful for testing the receiving end of
864 such things, and is a good exercise in dealing with it. We do this before
865 the connect message, to ensure that the connect msg is uniformly the LAST
866 thing to emerge after all the intervening crud. Doesn't work for UDP on
867 any machines I've tested, but feel free to surprise me. */
868#ifdef IP_OPTIONS
869 if (! o_verbose) /* if we wont see it, we dont care */
870 goto dol_noop;
871 optbuf = Hmalloc (40);
872 x = 40;
873 rr = getsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x);
874 if (rr < 0)
875 holler ("getsockopt failed");
876Debug (("ipoptions ret len %d", x))
877 if (x) { /* we've got options, lessee em... */
878 unsigned char * q = (unsigned char *) optbuf;
879 char * p = bigbuf_net; /* local variables, yuk! */
880 char * pp = &bigbuf_net[128]; /* get random space farther out... */
881 memset (bigbuf_net, 0, 256); /* clear it all first */
882 while (x > 0) {
883 sprintf (pp, "%2.2x ", *q); /* clumsy, but works: turn into hex */
884 strcat (p, pp); /* and build the final string */
885 q++; p++;
886 x--;
887 }
888 holler ("IP options: %s", bigbuf_net);
889 } /* if x, i.e. any options */
890dol_noop:
891#endif /* IP_OPTIONS */
892
893/* find out what address the connection was *to* on our end, in case we're
894 doing a listen-on-any on a multihomed machine. This allows one to
895 offer different services via different alias addresses, such as the
896 "virtual web site" hack. */
897 memset (bigbuf_net, 0, 64);
898 cp = &bigbuf_net[32];
899 x = sizeof (SA);
900 rr = getsockname (nnetfd, (SA *) lclend, &x);
901 if (rr < 0)
902 holler ("post-rcv getsockname failed");
903 strcpy (cp, inet_ntoa (lclend->sin_addr));
904
905/* now check out who it is. We don't care about mismatched DNS names here,
906 but any ADDR and PORT we specified had better fucking well match the caller.
907 Converting from addr to inet_ntoa and back again is a bit of a kludge, but
908 gethostpoop wants a string and there's much gnarlier code out there already,
909 so I don't feel bad.
910 The *real* question is why BFD sockets wasn't designed to allow listens for
911 connections *from* specific hosts/ports, instead of requiring the caller to
912 accept the connection and then reject undesireable ones by closing. In
913 other words, we need a TCP MSG_PEEK. */
914 z = ntohs (remend->sin_port);
915 strcpy (bigbuf_net, inet_ntoa (remend->sin_addr));
916 whozis = gethostpoop (bigbuf_net, o_nflag);
917 errno = 0;
918 x = 0; /* use as a flag... */
919 if (rad) /* xxx: fix to go down the *list* if we have one? */
920 if (memcmp (rad, whozis->iaddrs, sizeof (SA)))
921 x = 1;
922 if (rp)
923 if (z != rp)
924 x = 1;
925 if (x) /* guilty! */
926 bail ("invalid connection to [%s] from %s [%s] %d",
927 cp, whozis->name, whozis->addrs[0], z);
928 holler ("connect to [%s] from %s [%s] %d", /* oh, you're okay.. */
929 cp, whozis->name, whozis->addrs[0], z);
930 return (nnetfd); /* open! */
931
932dol_tmo:
933 errno = ETIMEDOUT; /* fake it */
934dol_err:
935 close (nnetfd);
936 return (-1);
937} /* dolisten */
938
939/* udptest :
940 fire a couple of packets at a UDP target port, just to see if it's really
941 there. On BSD kernels, ICMP host/port-unreachable errors get delivered to
942 our socket as ECONNREFUSED write errors. On SV kernels, we lose; we'll have
943 to collect and analyze raw ICMP ourselves a la satan's probe_udp_ports
944 backend. Guess where one could swipe the appropriate code from...
945
946 Use the time delay between writes if given, otherwise use the "tcp ping"
947 trick for getting the RTT. [I got that idea from pluvius, and warped it.]
948 Return either the original fd, or clean up and return -1. */
949udptest (fd, where)
950 int fd;
951 IA * where;
952{
953 register int rr;
954
955 rr = write (fd, bigbuf_in, 1);
956 if (rr != 1)
957 holler ("udptest first write failed?! errno %d", errno);
958 if (o_wait)
959 sleep (o_wait);
960 else {
961/* use the tcp-ping trick: try connecting to a normally refused port, which
962 causes us to block for the time that SYN gets there and RST gets back.
963 Not completely reliable, but it *does* mostly work. */
964 o_udpmode = 0; /* so doconnect does TCP this time */
965/* Set a temporary connect timeout, so packet filtration doesnt cause
966 us to hang forever, and hit it */
967 o_wait = 5; /* enough that we'll notice?? */
968 rr = doconnect (where, SLEAZE_PORT, 0, 0);
969 if (rr > 0)
970 close (rr); /* in case it *did* open */
971 o_wait = 0; /* reset it */
972 o_udpmode++; /* we *are* still doing UDP, right? */
973 } /* if o_wait */
974 errno = 0; /* clear from sleep */
975 rr = write (fd, bigbuf_in, 1);
976 if (rr == 1) /* if write error, no UDP listener */
977 return (fd);
978 close (fd); /* use it or lose it! */
979 return (-1);
980} /* udptest */
981
982/* oprint :
983 Hexdump bytes shoveled either way to a running logfile, in the format:
984D offset - - - - --- 16 bytes --- - - - - # .... ascii .....
985 where "which" sets the direction indicator, D:
986 0 -- sent to network, or ">"
987 1 -- rcvd and printed to stdout, or "<"
988 and "buf" and "n" are data-block and length. If the current block generates
989 a partial line, so be it; we *want* that lockstep indication of who sent
990 what when. Adapted from dgaudet's original example -- but must be ripping
991 *fast*, since we don't want to be too disk-bound... */
992void oprint (which, buf, n)
993 int which;
994 char * buf;
995 int n;
996{
997 int bc; /* in buffer count */
998 int obc; /* current "global" offset */
999 int soc; /* stage write count */
1000 register unsigned char * p; /* main buf ptr; m.b. unsigned here */
1001 register unsigned char * op; /* out hexdump ptr */
1002 register unsigned char * a; /* out asc-dump ptr */
1003 register int x;
1004 register unsigned int y;
1005
1006 if (! ofd)
1007 bail ("oprint called with no open fd?!");
1008 if (n == 0)
1009 return;
1010
1011 op = stage;
1012 if (which) {
1013 *op = '<';
1014 obc = wrote_out; /* use the globals! */
1015 } else {
1016 *op = '>';
1017 obc = wrote_net;
1018 }
1019 op++; /* preload "direction" */
1020 *op = ' ';
1021 p = (unsigned char *) buf;
1022 bc = n;
1023 stage[59] = '#'; /* preload separator */
1024 stage[60] = ' ';
1025
1026 while (bc) { /* for chunk-o-data ... */
1027 x = 16;
1028 soc = 78; /* len of whole formatted line */
1029 if (bc < x) {
1030 soc = soc - 16 + bc; /* fiddle for however much is left */
1031 x = (bc * 3) + 11; /* 2 digits + space per, after D & offset */
1032 op = &stage[x];
1033 x = 16 - bc;
1034 while (x) {
1035 *op++ = ' '; /* preload filler spaces */
1036 *op++ = ' ';
1037 *op++ = ' ';
1038 x--;
1039 }
1040 x = bc; /* re-fix current linecount */
1041 } /* if bc < x */
1042
1043 bc -= x; /* fix wrt current line size */
1044 sprintf (&stage[2], "%8.8x ", obc); /* xxx: still slow? */
1045 obc += x; /* fix current offset */
1046 op = &stage[11]; /* where hex starts */
1047 a = &stage[61]; /* where ascii starts */
1048
1049 while (x) { /* for line of dump, however long ... */
1050 y = (int)(*p >> 4); /* hi half */
1051 *op = hexnibs[y];
1052 op++;
1053 y = (int)(*p & 0x0f); /* lo half */
1054 *op = hexnibs[y];
1055 op++;
1056 *op = ' ';
1057 op++;
1058 if ((*p > 31) && (*p < 127))
1059 *a = *p; /* printing */
1060 else
1061 *a = '.'; /* nonprinting, loose def */
1062 a++;
1063 p++;
1064 x--;
1065 } /* while x */
1066 *a = '\n'; /* finish the line */
1067 x = write (ofd, stage, soc);
1068 if (x < 0)
1069 bail ("ofd write err");
1070 } /* while bc */
1071} /* oprint */
1072
1073#ifdef TELNET
1074USHORT o_tn = 0; /* global -t option */
1075
1076/* atelnet :
1077 Answer anything that looks like telnet negotiation with don't/won't.
1078 This doesn't modify any data buffers, update the global output count,
1079 or show up in a hexdump -- it just shits into the outgoing stream.
1080 Idea and codebase from Mudge@l0pht.com. */
1081void atelnet (buf, size)
1082 unsigned char * buf; /* has to be unsigned here! */
1083 unsigned int size;
1084{
1085 static unsigned char obuf [4]; /* tiny thing to build responses into */
1086 register int x;
1087 register unsigned char y;
1088 register unsigned char * p;
1089
1090 y = 0;
1091 p = buf;
1092 x = size;
1093 while (x > 0) {
1094 if (*p != 255) /* IAC? */
1095 goto notiac;
1096 obuf[0] = 255;
1097 p++; x--;
1098 if ((*p == 251) || (*p == 252)) /* WILL or WONT */
1099 y = 254; /* -> DONT */
1100 if ((*p == 253) || (*p == 254)) /* DO or DONT */
1101 y = 252; /* -> WONT */
1102 if (y) {
1103 obuf[1] = y;
1104 p++; x--;
1105 obuf[2] = *p; /* copy actual option byte */
1106 (void) write (netfd, obuf, 3);
1107/* if one wanted to bump wrote_net or do a hexdump line, here's the place */
1108 y = 0;
1109 } /* if y */
1110notiac:
1111 p++; x--;
1112 } /* while x */
1113} /* atelnet */
1114#endif /* TELNET */
1115
1116/* readwrite :
1117 handle stdin/stdout/network I/O. Bwahaha!! -- the select loop from hell.
1118 In this instance, return what might become our exit status. */
1119int readwrite (fd)
1120 int fd;
1121{
1122 register int rr;
1123 register char * zp; /* stdin buf ptr */
1124 register char * np; /* net-in buf ptr */
1125 unsigned int rzleft;
1126 unsigned int rnleft;
1127 USHORT netretry; /* net-read retry counter */
1128 USHORT wretry; /* net-write sanity counter */
1129 USHORT wfirst; /* one-shot flag to skip first net read */
1130
1131/* if you don't have all this FD_* macro hair in sys/types.h, you'll have to
1132 either find it or do your own bit-bashing: *ding1 |= (1 << fd), etc... */
1133 if (fd > FD_SETSIZE) {
1134 holler ("Preposterous fd value %d", fd);
1135 return (1);
1136 }
1137 FD_SET (fd, ding1); /* global: the net is open */
1138 netretry = 2;
1139 wfirst = 0;
1140 rzleft = rnleft = 0;
1141 if (insaved) {
1142 rzleft = insaved; /* preload multi-mode fakeouts */
1143 zp = bigbuf_in;
1144 wfirst = 1;
1145 if (Single) /* if not scanning, this is a one-off first */
1146 insaved = 0; /* buffer left over from argv construction, */
1147 else {
1148 FD_CLR (0, ding1); /* OR we've already got our repeat chunk, */
1149 close (0); /* so we won't need any more stdin */
1150 } /* Single */
1151 } /* insaved */
1152 if (o_interval)
1153 sleep (o_interval); /* pause *before* sending stuff, too */
1154 errno = 0; /* clear from sleep, close, whatever */
1155
1156/* and now the big ol' select shoveling loop ... */
1157 while (FD_ISSET (fd, ding1)) { /* i.e. till the *net* closes! */
1158 wretry = 8200; /* more than we'll ever hafta write */
1159 if (wfirst) { /* any saved stdin buffer? */
1160 wfirst = 0; /* clear flag for the duration */
1161 goto shovel; /* and go handle it first */
1162 }
1163 *ding2 = *ding1; /* FD_COPY ain't portable... */
1164/* some systems, notably linux, crap into their select timers on return, so
1165 we create a expendable copy and give *that* to select. *Fuck* me ... */
1166 if (timer1)
1167 memcpy (timer2, timer1, sizeof (struct timeval));
1168 rr = select (16, ding2, 0, 0, timer2); /* here it is, kiddies */
1169 if (rr < 0) {
1170 if (errno != EINTR) { /* might have gotten ^Zed, etc ?*/
1171 holler ("select fuxored");
1172 close (fd);
1173 return (1);
1174 }
1175 } /* select fuckup */
1176/* if we have a timeout AND stdin is closed AND we haven't heard anything
1177 from the net during that time, assume it's dead and close it too. */
1178 if (rr == 0) {
1179 if (! FD_ISSET (0, ding1))
1180 netretry--; /* we actually try a coupla times. */
1181 if (! netretry) {
1182 if (o_verbose > 1) /* normally we don't care */
1183 holler ("net timeout");
1184 close (fd);
1185 return (0); /* not an error! */
1186 }
1187 } /* select timeout */
1188/* xxx: should we check the exception fds too? The read fds seem to give
1189 us the right info, and none of the examples I found bothered. */
1190
1191/* Ding!! Something arrived, go check all the incoming hoppers, net first */
1192 if (FD_ISSET (fd, ding2)) { /* net: ding! */
1193 rr = read (fd, bigbuf_net, BIGSIZ);
1194 if (rr <= 0) {
1195 FD_CLR (fd, ding1); /* net closed, we'll finish up... */
1196 rzleft = 0; /* can't write anymore: broken pipe */
1197 } else {
1198 rnleft = rr;
1199 np = bigbuf_net;
1200#ifdef TELNET
1201 if (o_tn)
1202 atelnet (np, rr); /* fake out telnet stuff */
1203#endif /* TELNET */
1204 } /* if rr */
1205Debug (("got %d from the net, errno %d", rr, errno))
1206 } /* net:ding */
1207
1208/* if we're in "slowly" mode there's probably still stuff in the stdin
1209 buffer, so don't read unless we really need MORE INPUT! MORE INPUT! */
1210 if (rzleft)
1211 goto shovel;
1212
1213/* okay, suck more stdin */
1214 if (FD_ISSET (0, ding2)) { /* stdin: ding! */
1215 rr = read (0, bigbuf_in, BIGSIZ);
1216/* Considered making reads here smaller for UDP mode, but 8192-byte
1217 mobygrams are kinda fun and exercise the reassembler. */
1218 if (rr <= 0) { /* at end, or fukt, or ... */
1219 FD_CLR (0, ding1); /* disable and close stdin */
1220 close (0);
1221 } else {
1222 rzleft = rr;
1223 zp = bigbuf_in;
1224/* special case for multi-mode -- we'll want to send this one buffer to every
1225 open TCP port or every UDP attempt, so save its size and clean up stdin */
1226 if (! Single) { /* we might be scanning... */
1227 insaved = rr; /* save len */
1228 FD_CLR (0, ding1); /* disable further junk from stdin */
1229 close (0); /* really, I mean it */
1230 } /* Single */
1231 } /* if rr/read */
1232 } /* stdin:ding */
1233
1234shovel:
1235/* now that we've dingdonged all our thingdings, send off the results.
1236 Geez, why does this look an awful lot like the big loop in "rsh"? ...
1237 not sure if the order of this matters, but write net -> stdout first. */
1238
1239/* sanity check. Works because they're both unsigned... */
1240 if ((rzleft > 8200) || (rnleft > 8200)) {
1241 holler ("Bogus buffers: %d, %d", rzleft, rnleft);
1242 rzleft = rnleft = 0;
1243 }
1244/* net write retries sometimes happen on UDP connections */
1245 if (! wretry) { /* is something hung? */
1246 holler ("too many output retries");
1247 return (1);
1248 }
1249 if (rnleft) {
1250 rr = write (1, np, rnleft);
1251 if (rr > 0) {
1252 if (o_wfile)
1253 oprint (1, np, rr); /* log the stdout */
1254 np += rr; /* fix up ptrs and whatnot */
1255 rnleft -= rr; /* will get sanity-checked above */
1256 wrote_out += rr; /* global count */
1257 }
1258Debug (("wrote %d to stdout, errno %d", rr, errno))
1259 } /* rnleft */
1260 if (rzleft) {
1261 if (o_interval) /* in "slowly" mode ?? */
1262 rr = findline (zp, rzleft);
1263 else
1264 rr = rzleft;
1265 rr = write (fd, zp, rr); /* one line, or the whole buffer */
1266 if (rr > 0) {
1267 if (o_wfile)
1268 oprint (0, zp, rr); /* log what got sent */
1269 zp += rr;
1270 rzleft -= rr;
1271 wrote_net += rr; /* global count */
1272 }
1273Debug (("wrote %d to net, errno %d", rr, errno))
1274 } /* rzleft */
1275 if (o_interval) { /* cycle between slow lines, or ... */
1276 sleep (o_interval);
1277 errno = 0; /* clear from sleep */
1278 continue; /* ...with hairy select loop... */
1279 }
1280 if ((rzleft) || (rnleft)) { /* shovel that shit till they ain't */
1281 wretry--; /* none left, and get another load */
1282 goto shovel;
1283 }
1284 } /* while ding1:netfd is open */
1285
1286/* XXX: maybe want a more graceful shutdown() here, or screw around with
1287 linger times?? I suspect that I don't need to since I'm always doing
1288 blocking reads and writes and my own manual "last ditch" efforts to read
1289 the net again after a timeout. I haven't seen any screwups yet, but it's
1290 not like my test network is particularly busy... */
1291 close (fd);
1292 return (0);
1293} /* readwrite */
1294
1295/* main :
1296 now we pull it all together... */
1297main (argc, argv)
1298 int argc;
1299 char ** argv;
1300{
1301#ifndef HAVE_GETOPT
1302 extern char * optarg;
1303 extern int optind, optopt;
1304#endif
1305 register int x;
1306 register char *cp;
1307 HINF * gp;
1308 HINF * whereto = NULL;
1309 HINF * wherefrom = NULL;
1310 IA * ouraddr = NULL;
1311 IA * themaddr = NULL;
1312 USHORT o_lport = 0;
1313 USHORT ourport = 0;
1314 USHORT loport = 0; /* for scanning stuff */
1315 USHORT hiport = 0;
1316 USHORT curport = 0;
1317 char * randports = NULL;
1318
1319#ifdef HAVE_BIND
1320/* can *you* say "cc -yaddayadda netcat.c -lresolv -l44bsd" on SunLOSs? */
1321 res_init();
1322#endif
1323/* I was in this barbershop quartet in Skokie IL ... */
1324/* round up the usual suspects, i.e. malloc up all the stuff we need */
1325 lclend = (SAI *) Hmalloc (sizeof (SA));
1326 remend = (SAI *) Hmalloc (sizeof (SA));
1327 bigbuf_in = Hmalloc (BIGSIZ);
1328 bigbuf_net = Hmalloc (BIGSIZ);
1329 ding1 = (fd_set *) Hmalloc (sizeof (fd_set));
1330 ding2 = (fd_set *) Hmalloc (sizeof (fd_set));
1331 portpoop = (PINF *) Hmalloc (sizeof (PINF));
1332
1333 errno = 0;
1334 gatesptr = 4;
1335 h_errno = 0;
1336
1337/* catch a signal or two for cleanup */
1338 signal (SIGINT, catch);
1339 signal (SIGQUIT, catch);
1340 signal (SIGTERM, catch);
1341/* and suppress others... */
1342#ifdef SIGURG
1343 signal (SIGURG, SIG_IGN);
1344#endif
1345#ifdef SIGPIPE
1346 signal (SIGPIPE, SIG_IGN); /* important! */
1347#endif
1348
1349/* if no args given at all, get 'em from stdin, construct an argv, and hand
1350 anything left over to readwrite(). */
1351 if (argc == 1) {
1352 cp = argv[0];
1353 argv = (char **) Hmalloc (128 * sizeof (char *)); /* XXX: 128? */
1354 argv[0] = cp; /* leave old prog name intact */
1355 cp = Hmalloc (BIGSIZ);
1356 argv[1] = cp; /* head of new arg block */
1357 fprintf (stderr, "Cmd line: ");
1358 fflush (stderr); /* I dont care if it's unbuffered or not! */
1359 insaved = read (0, cp, BIGSIZ); /* we're gonna fake fgets() here */
1360 if (insaved <= 0)
1361 bail ("wrong");
1362 x = findline (cp, insaved);
1363 if (x)
1364 insaved -= x; /* remaining chunk size to be sent */
1365 if (insaved) /* which might be zero... */
1366 memcpy (bigbuf_in, &cp[x], insaved);
1367 cp = strchr (argv[1], '\n');
1368 if (cp)
1369 *cp = '\0';
1370 cp = strchr (argv[1], '\r'); /* look for ^M too */
1371 if (cp)
1372 *cp = '\0';
1373
1374/* find and stash pointers to remaining new "args" */
1375 cp = argv[1];
1376 cp++; /* skip past first char */
1377 x = 2; /* we know argv 0 and 1 already */
1378 for (; *cp != '\0'; cp++) {
1379 if (*cp == ' ') {
1380 *cp = '\0'; /* smash all spaces */
1381 continue;
1382 } else {
1383 if (*(cp-1) == '\0') {
1384 argv[x] = cp;
1385 x++;
1386 }
1387 } /* if space */
1388 } /* for cp */
1389 argc = x;
1390 } /* if no args given */
1391
1392/* If your shitbox doesn't have getopt, step into the nineties already. */
1393/* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */
1394 while ((x = getopt (argc, argv, "ae:g:G:hi:lno:p:rs:tuvw:z")) != -1) {
1395/* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */
1396 switch (x) {
1397 case 'a':
1398 bail ("all-A-records NIY");
1399 o_alla++; break;
1400#ifdef GAPING_SECURITY_HOLE
1401 case 'e': /* prog to exec */
1402 pr00gie = optarg;
1403 break;
1404#endif
1405 case 'G': /* srcrt gateways pointer val */
1406 x = atoi (optarg);
1407 if ((x) && (x == (x & 0x1c))) /* mask off bits of fukt values */
1408 gatesptr = x;
1409 else
1410 bail ("invalid hop pointer %d, must be multiple of 4 <= 28", x);
1411 break;
1412 case 'g': /* srcroute hop[s] */
1413 if (gatesidx > 8)
1414 bail ("too many -g hops");
1415 if (gates == NULL) /* eat this, Billy-boy */
1416 gates = (HINF **) Hmalloc (sizeof (HINF *) * 10);
1417 gp = gethostpoop (optarg, o_nflag);
1418 if (gp)
1419 gates[gatesidx] = gp;
1420 gatesidx++;
1421 break;
1422 case 'h':
1423 errno = 0;
1424#ifdef HAVE_HELP
1425 helpme(); /* exits by itself */
1426#else
1427 bail ("no help available, dork -- RTFS");
1428#endif
1429 case 'i': /* line-interval time */
1430 o_interval = atoi (optarg) & 0xffff;
1431 if (! o_interval)
1432 bail ("invalid interval time %s", optarg);
1433 break;
1434 case 'l': /* listen mode */
1435 o_listen++; break;
1436 case 'n': /* numeric-only, no DNS lookups */
1437 o_nflag++; break;
1438 case 'o': /* hexdump log */
1439 stage = (unsigned char *) optarg;
1440 o_wfile++; break;
1441 case 'p': /* local source port */
1442 o_lport = getportpoop (optarg, 0);
1443 if (o_lport == 0)
1444 bail ("invalid local port %s", optarg);
1445 break;
1446 case 'r': /* randomize various things */
1447 o_random++; break;
1448 case 's': /* local source address */
1449/* do a full lookup [since everything else goes through the same mill],
1450 unless -n was previously specified. In fact, careful placement of -n can
1451 be useful, so we'll still pass o_nflag here instead of forcing numeric. */
1452 wherefrom = gethostpoop (optarg, o_nflag);
1453 ouraddr = &wherefrom->iaddrs[0];
1454 break;
1455#ifdef TELNET
1456 case 't': /* do telnet fakeout */
1457 o_tn++; break;
1458#endif /* TELNET */
1459 case 'u': /* use UDP */
1460 o_udpmode++; break;
1461 case 'v': /* verbose */
1462 o_verbose++; break;
1463 case 'w': /* wait time */
1464 o_wait = atoi (optarg);
1465 if (o_wait <= 0)
1466 bail ("invalid wait-time %s", optarg);
1467 timer1 = (struct timeval *) Hmalloc (sizeof (struct timeval));
1468 timer2 = (struct timeval *) Hmalloc (sizeof (struct timeval));
1469 timer1->tv_sec = o_wait; /* we need two. see readwrite()... */
1470 break;
1471 case 'z': /* little or no data xfer */
1472 o_zero++;
1473 break;
1474 default:
1475 errno = 0;
1476 bail ("nc -h for help");
1477 } /* switch x */
1478 } /* while getopt */
1479
1480/* other misc initialization */
1481Debug (("fd_set size %d", sizeof (*ding1))) /* how big *is* it? */
1482 FD_SET (0, ding1); /* stdin *is* initially open */
1483 if (o_random) {
1484 SRAND (time (0));
1485 randports = Hmalloc (65536); /* big flag array for ports */
1486 }
1487#ifdef GAPING_SECURITY_HOLE
1488 if (pr00gie) {
1489 close (0); /* won't need stdin */
1490 o_wfile = 0; /* -o with -e is meaningless! */
1491 ofd = 0;
1492 }
1493#endif /* G_S_H */
1494 if (o_wfile) {
1495 ofd = open (stage, O_WRONLY | O_CREAT | O_TRUNC, 0664);
1496 if (ofd <= 0) /* must be > extant 0/1/2 */
1497 bail ("can't open %s", stage);
1498 stage = (unsigned char *) Hmalloc (100);
1499 }
1500
1501/* optind is now index of first non -x arg */
1502Debug (("after go: x now %c, optarg %x optind %d", x, optarg, optind))
1503/* Debug (("optind up to %d at host-arg %s", optind, argv[optind])) */
1504/* gonna only use first addr of host-list, like our IQ was normal; if you wanna
1505 get fancy with addresses, look up the list yourself and plug 'em in for now.
1506 unless we finally implement -a, that is. */
1507 if (argv[optind])
1508 whereto = gethostpoop (argv[optind], o_nflag);
1509 if (whereto && whereto->iaddrs)
1510 themaddr = &whereto->iaddrs[0];
1511 if (themaddr)
1512 optind++; /* skip past valid host lookup */
1513 errno = 0;
1514 h_errno = 0;
1515
1516/* Handle listen mode here, and exit afterward. Only does one connect;
1517 this is arguably the right thing to do. A "persistent listen-and-fork"
1518 mode a la inetd has been thought about, but not implemented. A tiny
1519 wrapper script can handle such things... */
1520 if (o_listen) {
1521 curport = 0; /* rem port *can* be zero here... */
1522 if (argv[optind]) { /* any rem-port-arg? */
1523 curport = getportpoop (argv[optind], 0);
1524 if (curport == 0) /* if given, demand correctness */
1525 bail ("invalid port %s", argv[optind]);
1526 } /* if port-arg */
1527 netfd = dolisten (themaddr, curport, ouraddr, o_lport);
1528/* dolisten does its own connect reporting, so we don't holler anything here */
1529 if (netfd > 0) {
1530#ifdef GAPING_SECURITY_HOLE
1531 if (pr00gie) /* -e given? */
1532 doexec (netfd);
1533#endif /* GAPING_SECURITY_HOLE */
1534 x = readwrite (netfd); /* it even works with UDP! */
1535 if (o_verbose > 1) /* normally we don't care */
1536 holler (wrote_txt, wrote_net, wrote_out);
1537 exit (x); /* "pack out yer trash" */
1538 } else /* if no netfd */
1539 bail ("no connection");
1540 } /* o_listen */
1541
1542/* fall thru to outbound connects. Now we're more picky about args... */
1543 if (! themaddr)
1544 bail ("no destination");
1545 if (argv[optind] == NULL)
1546 bail ("no port[s] to connect to");
1547 if (argv[optind + 1]) /* look ahead: any more port args given? */
1548 Single = 0; /* multi-mode, case A */
1549 ourport = o_lport; /* which can be 0 */
1550
1551/* everything from here down is treated as as ports and/or ranges thereof, so
1552 it's all enclosed in this big ol' argv-parsin' loop. Any randomization is
1553 done within each given *range*, but in separate chunks per each succeeding
1554 argument, so we can control the pattern somewhat. */
1555 while (argv[optind]) {
1556 hiport = loport = 0;
1557 cp = strchr (argv[optind], '-'); /* nn-mm range? */
1558 if (cp) {
1559 *cp = '\0';
1560 cp++;
1561 hiport = getportpoop (cp, 0);
1562 if (hiport == 0)
1563 bail ("invalid port %s", cp);
1564 } /* if found a dash */
1565 loport = getportpoop (argv[optind], 0);
1566 if (loport == 0)
1567 bail ("invalid port %s", argv[optind]);
1568 if (hiport > loport) { /* was it genuinely a range? */
1569 Single = 0; /* multi-mode, case B */
1570 curport = hiport; /* start high by default */
1571 if (o_random) { /* maybe populate the random array */
1572 loadports (randports, loport, hiport);
1573 curport = nextport (randports);
1574 }
1575 } else /* not a range, including args like "25-25" */
1576 curport = loport;
1577Debug (("Single %d, curport %d", Single, curport))
1578
1579/* Now start connecting to these things. curport is already preloaded. */
1580 while (loport <= curport) {
1581 if ((! o_lport) && (o_random)) { /* -p overrides random local-port */
1582 ourport = (RAND() & 0xffff); /* random local-bind -- well above */
1583 if (ourport < 8192) /* resv and any likely listeners??? */
1584 ourport += 8192; /* if it *still* conflicts, use -s. */
1585 }
1586 curport = getportpoop (NULL, curport);
1587 netfd = doconnect (themaddr, curport, ouraddr, ourport);
1588Debug (("netfd %d from port %d to port %d", netfd, ourport, curport))
1589 if (netfd > 0)
1590 if (o_zero && o_udpmode) /* if UDP scanning... */
1591 netfd = udptest (netfd, themaddr);
1592 if (netfd > 0) { /* Yow, are we OPEN YET?! */
1593 x = 0; /* pre-exit status */
1594 holler ("%s [%s] %d (%s) open",
1595 whereto->name, whereto->addrs[0], curport, portpoop->name);
1596#ifdef GAPING_SECURITY_HOLE
1597 if (pr00gie) /* exec is valid for outbound, too */
1598 doexec (netfd);
1599#endif /* GAPING_SECURITY_HOLE */
1600 if (! o_zero)
1601 x = readwrite (netfd); /* go shovel shit */
1602 } else { /* no netfd... */
1603 x = 1; /* preload exit status for later */
1604/* if we're scanning at a "one -v" verbosity level, don't print refusals.
1605 Give it another -v if you want to see everything. */
1606 if ((Single || (o_verbose > 1)) || (errno != ECONNREFUSED))
1607 holler ("%s [%s] %d (%s)",
1608 whereto->name, whereto->addrs[0], curport, portpoop->name);
1609 } /* if netfd */
1610 close (netfd); /* just in case we didn't already */
1611 if (o_interval)
1612 sleep (o_interval); /* if -i, delay between ports too */
1613 if (o_random)
1614 curport = nextport (randports);
1615 else
1616 curport--; /* just decrement... */
1617 } /* while curport within current range */
1618 optind++;
1619 } /* while remaining port-args -- end of big argv-ports loop*/
1620
1621 errno = 0;
1622 if (o_verbose > 1) /* normally we don't care */
1623 holler (wrote_txt, wrote_net, wrote_out);
1624 if (Single)
1625 exit (x); /* give us status on one connection */
1626 exit (0); /* otherwise, we're just done */
1627} /* main */
1628
1629#ifdef HAVE_HELP /* unless we wanna be *really* cryptic */
1630/* helpme :
1631 the obvious */
1632helpme()
1633{
1634 o_verbose = 1;
1635 holler ("[v1.10]\n\
1636connect to somewhere: nc [-options] hostname port[s] [ports] ... \n\
1637listen for inbound: nc -l -p port [-options] [hostname] [port]\n\
1638options:");
1639/* sigh, this necessarily gets messy. And the trailing \ characters may be
1640 interpreted oddly by some compilers, generating or not generating extra
1641 newlines as they bloody please. u-fix... */
1642#ifdef GAPING_SECURITY_HOLE /* needs to be separate holler() */
1643 holler ("\
1644 -e prog program to exec after connect [dangerous!!]");
1645#endif
1646 holler ("\
1647 -g gateway source-routing hop point[s], up to 8\n\
1648 -G num source-routing pointer: 4, 8, 12, ...\n\
1649 -h this cruft\n\
1650 -i secs delay interval for lines sent, ports scanned\n\
1651 -l listen mode, for inbound connects\n\
1652 -n numeric-only IP addresses, no DNS\n\
1653 -o file hex dump of traffic\n\
1654 -p port local port number\n\
1655 -r randomize local and remote ports\n\
1656 -s addr local source address");
1657#ifdef TELNET
1658 holler ("\
1659 -t answer TELNET negotiation");
1660#endif
1661 holler ("\
1662 -u UDP mode\n\
1663 -v verbose [use twice to be more verbose]\n\
1664 -w secs timeout for connects and final net reads\n\
1665 -z zero-I/O mode [used for scanning]");
1666 bail ("port numbers can be individual or ranges: lo-hi [inclusive]");
1667} /* helpme */
1668#endif /* HAVE_HELP */
1669
1670/* None genuine without this seal! _H*/
diff --git a/src/usr.bin/nc/scripts/README b/src/usr.bin/nc/scripts/README
new file mode 100644
index 0000000000..07aee0c8ea
--- /dev/null
+++ b/src/usr.bin/nc/scripts/README
@@ -0,0 +1,5 @@
1A collection of example scripts that use netcat as a backend, each
2documented by its own internal comments.
3
4I'll be the first to admit that some of these are seriously *sick*,
5but they do work and are quite useful to me on a daily basis.
diff --git a/src/usr.bin/nc/scripts/alta b/src/usr.bin/nc/scripts/alta
new file mode 100644
index 0000000000..7a091767e8
--- /dev/null
+++ b/src/usr.bin/nc/scripts/alta
@@ -0,0 +1,33 @@
1#! /bin/sh
2## special handler for altavista, since they only hand out chunks of 10 at
3## a time. Tries to isolate out results without the leading/trailing trash.
4## multiword arguments are foo+bar, as usual.
5## Second optional arg switches the "what" field, to e.g. "news"
6
7test "${1}" = "" && echo 'Needs an argument to search for!' && exit 1
8WHAT="web"
9test "${2}" && WHAT="${2}"
10
11# convert multiple args
12PLUSARG="`echo $* | sed 's/ /+/g'`"
13
14# Plug in arg. only doing simple-q for now; pg=aq for advanced-query
15# embedded quotes define phrases; otherwise it goes wild on multi-words
16QB="GET /cgi-bin/query?pg=q&what=${WHAT}&fmt=c&q=\"${PLUSARG}\""
17
18# ping 'em once, to get the routing warm
19nc -z -w 8 www.altavista.digital.com 24015 2> /dev/null
20echo "=== Altavista ==="
21
22for xx in 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 \
23 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 ; do
24 echo "${QB}&stq=${xx}" | nc -w 15 www.altavista.digital.com 80 | \
25 egrep '^<a href="http://'
26done
27
28exit 0
29
30# old filter stuff
31 sed -e '/Documents .* matching .* query /,/query?.*stq=.* Document/p' \
32 -e d
33
diff --git a/src/usr.bin/nc/scripts/bsh b/src/usr.bin/nc/scripts/bsh
new file mode 100644
index 0000000000..796e480354
--- /dev/null
+++ b/src/usr.bin/nc/scripts/bsh
@@ -0,0 +1,29 @@
1#! /bin/sh
2## a little wrapper to "password" and re-launch a shell-listener.
3## Arg is taken as the port to listen on. Define "NC" to point wherever.
4
5NC=nc
6
7case "$1" in
8 ?* )
9 LPN="$1"
10 export LPN
11 sleep 1
12 echo "-l -p $LPN -e $0" | $NC > /dev/null 2>&1 &
13 echo "launched on port $LPN"
14 exit 0
15 ;;
16esac
17
18# here we play inetd
19echo "-l -p $LPN -e $0" | $NC > /dev/null 2>&1 &
20
21while read qq ; do
22case "$qq" in
23# here's yer password
24 gimme )
25 cd /
26 exec csh -i
27 ;;
28esac
29done
diff --git a/src/usr.bin/nc/scripts/dist.sh b/src/usr.bin/nc/scripts/dist.sh
new file mode 100644
index 0000000000..4d2534a0e3
--- /dev/null
+++ b/src/usr.bin/nc/scripts/dist.sh
@@ -0,0 +1,23 @@
1#! /bin/sh
2## This is a quick example listen-exec server, which was used for a while to
3## distribute netcat prereleases. It illustrates use of netcat both as a
4## "fake inetd" and a syslogger, and how easy it then is to crock up a fairly
5## functional server that restarts its own listener and does full connection
6## logging. In a half-screen of shell script!!
7
8PORT=31337
9
10sleep 1
11SRC=`tail -1 dist.log`
12echo "<36>elite: ${SRC}" | ./nc -u -w 1 localhost 514 > /dev/null 2>&1
13echo ";;; Hi, ${SRC}..."
14echo ";;; This is a PRERELEASE version of 'netcat', tar/gzip/uuencoded."
15echo ";;; Unless you are capturing this somehow, it won't do you much good."
16echo ";;; Ready?? Here it comes! Have phun ..."
17sleep 8
18cat dist.file
19sleep 1
20./nc -v -l -p ${PORT} -e dist.sh < /dev/null >> dist.log 2>&1 &
21sleep 1
22echo "<36>elite: done" | ./nc -u -w 1 localhost 514 > /dev/null 2>&1
23exit 0
diff --git a/src/usr.bin/nc/scripts/irc b/src/usr.bin/nc/scripts/irc
new file mode 100644
index 0000000000..3557d7a0c6
--- /dev/null
+++ b/src/usr.bin/nc/scripts/irc
@@ -0,0 +1,79 @@
1#! /bin/sh
2## Shit-simple script to supply the "privmsg <recipient>" of IRC typein, and
3## keep the connection alive. Pipe this thru "nc -v -w 5 irc-server port".
4## Note that this mechanism makes the script easy to debug without being live,
5## since it just echoes everything bound for the server.
6## if you want autologin-type stuff, construct some appropriate files and
7## shovel them in using the "<" mechanism.
8
9# magic arg: if "tick", do keepalive process instead of main loop
10if test "$1" = "tick" ; then
11# ignore most signals; the parent will nuke the kid
12# doesn't stop ^Z, of course.
13 trap '' 1 2 3 13 14 15 16
14 while true ; do
15 sleep 60
16 echo "PONG !"
17 done
18fi
19
20# top level: fire ourselves off as the keepalive process, and keep track of it
21sh $0 tick &
22ircpp=$!
23echo "[Keepalive: $ircpp]" >&2
24# catch our own batch of signals: hup int quit pipe alrm term urg
25trap 'kill -9 $ircpp ; exit 0' 1 2 3 13 14 15 16
26sleep 2
27
28sender=''
29savecmd=''
30
31# the big honkin' loop...
32while read xx yy ; do
33 case "${xx}" in
34# blank line: do nothing
35 "")
36 continue
37 ;;
38# new channel or recipient; if bare ">", we're back to raw literal mode.
39 ">")
40 if test "${yy}" ; then
41 sender="privmsg ${yy} :"
42 else
43 sender=''
44 fi
45 continue
46 ;;
47# send crud from a file, one line per second. Can you say "skr1pt kidz"??
48# *Note: uses current "recipient" if set.
49 "<")
50 if test -f "${yy}" ; then
51 ( while read zz ; do
52 sleep 1
53 echo "${sender}${zz}"
54 done ) < "$yy"
55 echo "[done]" >&2
56 else
57 echo "[File $yy not found]" >&2
58 fi
59 continue
60 ;;
61# do and save a single command, for quick repeat
62 "/")
63 if test "${yy}" ; then
64 savecmd="${yy}"
65 fi
66 echo "${savecmd}"
67 ;;
68# default case goes to recipient, just like always
69 *)
70 echo "${sender}${xx} ${yy}"
71 continue
72 ;;
73 esac
74done
75
76# parting shot, if you want it
77echo "quit :Bye all!"
78kill -9 $ircpp
79exit 0
diff --git a/src/usr.bin/nc/scripts/iscan b/src/usr.bin/nc/scripts/iscan
new file mode 100644
index 0000000000..6279bc817f
--- /dev/null
+++ b/src/usr.bin/nc/scripts/iscan
@@ -0,0 +1,35 @@
1#! /bin/sh
2## duplicate DaveG's ident-scan thingie using netcat. Oooh, he'll be pissed.
3## args: target port [port port port ...]
4## hose stdout *and* stderr together.
5##
6## advantages: runs slower than ident-scan, giving remote inetd less cause
7## for alarm, and only hits the few known daemon ports you specify.
8## disadvantages: requires numeric-only port args, the output sleazitude,
9## and won't work for r-services when coming from high source ports.
10
11case "${2}" in
12 "" ) echo needs HOST and at least one PORT ; exit 1 ;;
13esac
14
15# ping 'em once and see if they *are* running identd
16nc -z -w 9 "$1" 113 || { echo "oops, $1 isn't running identd" ; exit 0 ; }
17
18# generate a randomish base port
19RP=`expr $$ % 999 + 31337`
20
21TRG="$1"
22shift
23
24while test "$1" ; do
25 nc -v -w 8 -p ${RP} "$TRG" ${1} < /dev/null > /dev/null &
26 PROC=$!
27 sleep 3
28 echo "${1},${RP}" | nc -w 4 -r "$TRG" 113 2>&1
29 sleep 2
30# does this look like a lamer script or what...
31 kill -HUP $PROC
32 RP=`expr ${RP} + 1`
33 shift
34done
35
diff --git a/src/usr.bin/nc/scripts/ncp b/src/usr.bin/nc/scripts/ncp
new file mode 100644
index 0000000000..1931b03385
--- /dev/null
+++ b/src/usr.bin/nc/scripts/ncp
@@ -0,0 +1,46 @@
1#! /bin/sh
2## Like "rcp" but uses netcat on a high port.
3## do "ncp targetfile" on the RECEIVING machine
4## then do "ncp sourcefile receivinghost" on the SENDING machine
5## if invoked as "nzp" instead, compresses transit data.
6
7## pick your own personal favorite port, which will be used on both ends.
8## You should probably change this for your own uses.
9MYPORT=23456
10
11## if "nc" isn't systemwide or in your PATH, add the right place
12# PATH=${HOME}:${PATH} ; export PATH
13
14test "$3" && echo "too many args" && exit 1
15test ! "$1" && echo "no args?" && exit 1
16me=`echo $0 | sed 's+.*/++'`
17test "$me" = "nzp" && echo '[compressed mode]'
18
19# if second arg, it's a host to send an [extant] file to.
20if test "$2" ; then
21 test ! -f "$1" && echo "can't find $1" && exit 1
22 if test "$me" = "nzp" ; then
23 compress -c < "$1" | nc -v -w 2 $2 $MYPORT && exit 0
24 else
25 nc -v -w 2 $2 $MYPORT < "$1" && exit 0
26 fi
27 echo "transfer FAILED!"
28 exit 1
29fi
30
31# fall here for receiver. Ask before trashing existing files
32if test -f "$1" ; then
33 echo -n "Overwrite $1? "
34 read aa
35 test ! "$aa" = "y" && echo "[punted!]" && exit 1
36fi
37# 30 seconds oughta be pleeeeenty of time, but change if you want.
38if test "$me" = "nzp" ; then
39 nc -v -w 30 -p $MYPORT -l < /dev/null | uncompress -c > "$1" && exit 0
40else
41 nc -v -w 30 -p $MYPORT -l < /dev/null > "$1" && exit 0
42fi
43echo "transfer FAILED!"
44# clean up, since even if the transfer failed, $1 is already trashed
45rm -f "$1"
46exit 1
diff --git a/src/usr.bin/nc/scripts/probe b/src/usr.bin/nc/scripts/probe
new file mode 100644
index 0000000000..c47dc3f495
--- /dev/null
+++ b/src/usr.bin/nc/scripts/probe
@@ -0,0 +1,50 @@
1#! /bin/sh
2## launch a whole buncha shit at yon victim in no particular order; capture
3## stderr+stdout in one place. Run as root for rservice and low -p to work.
4## Fairly thorough example of using netcat to collect a lot of host info.
5## Will set off every intrusion alarm in existence on a paranoid machine!
6
7# where .d files are kept; "." if nothing else
8DDIR=../data
9# address of some well-connected router that groks LSRR
10GATE=192.157.69.11
11
12# might conceivably wanna change this for different run styles
13UCMD='nc -v -w 8'
14
15test ! "$1" && echo Needs victim arg && exit 1
16
17echo '' | $UCMD -w 9 -r "$1" 13 79 6667 2>&1
18echo '0' | $UCMD "$1" 79 2>&1
19# if LSRR was passed thru, should get refusal here:
20$UCMD -z -r -g $GATE "$1" 6473 2>&1
21$UCMD -r -z "$1" 6000 4000-4004 111 53 2105 137-140 1-20 540-550 95 87 2>&1
22# -s `hostname` may be wrong for some multihomed machines
23echo 'UDP echoecho!' | nc -u -p 7 -s `hostname` -w 3 "$1" 7 19 2>&1
24echo '113,10158' | $UCMD -p 10158 "$1" 113 2>&1
25rservice bin bin | $UCMD -p 1019 "$1" shell 2>&1
26echo QUIT | $UCMD -w 8 -r "$1" 25 158 159 119 110 109 1109 142-144 220 23 2>&1
27# newline after any telnet trash
28echo ''
29echo PASV | $UCMD -r "$1" 21 2>&1
30echo 'GET /' | $UCMD -w 10 "$1" 80 81 210 70 2>&1
31# sometimes contains useful directory info:
32echo 'GET /robots.txt' | $UCMD -w 10 "$1" 80 2>&1
33# now the big red lights go on
34rservice bin bin 9600/9600 | $UCMD -p 1020 "$1" login 2>&1
35rservice root root | $UCMD -r "$1" exec 2>&1
36echo 'BEGIN big udp -- everything may look "open" if packet-filtered'
37data -g < ${DDIR}/nfs-0.d | $UCMD -i 1 -u "$1" 2049 | od -x 2>&1
38# no wait-time, uses RTT hack
39nc -v -z -u -r "$1" 111 66-70 88 53 87 161-164 121-123 213 49 2>&1
40nc -v -z -u -r "$1" 137-140 694-712 747-770 175-180 2103 510-530 2>&1
41echo 'END big udp'
42$UCMD -r -z "$1" 175-180 2000-2003 530-533 1524 1525 666 213 8000 6250 2>&1
43# Use our identd-sniffer!
44iscan "$1" 21 25 79 80 111 53 6667 6000 2049 119 2>&1
45# this gets pretty intrusive, but what the fuck. Probe for portmap first
46if nc -w 5 -z -u "$1" 111 ; then
47 showmount -e "$1" 2>&1
48 rpcinfo -p "$1" 2>&1
49fi
50exit 0
diff --git a/src/usr.bin/nc/scripts/web b/src/usr.bin/nc/scripts/web
new file mode 100644
index 0000000000..382b18e1e3
--- /dev/null
+++ b/src/usr.bin/nc/scripts/web
@@ -0,0 +1,148 @@
1#! /bin/sh
2## The web sucks. It is a mighty dismal kludge built out of a thousand
3## tiny dismal kludges all band-aided together, and now these bottom-line
4## clueless pinheads who never heard of "TCP handshake" want to run
5## *commerce* over the damn thing. Ye godz. Welcome to TV of the next
6## century -- six million channels of worthless shit to choose from, and
7## about as much security as today's cable industry!
8##
9## Having grown mightily tired of pain in the ass browsers, I decided
10## to build the minimalist client. It doesn't handle POST, just GETs, but
11## the majority of cgi forms handlers apparently ignore the method anyway.
12## A distinct advantage is that it *doesn't* pass on any other information
13## to the server, like Referer: or info about your local machine such as
14## Netscum tries to!
15##
16## Since the first version, this has become the *almost*-minimalist client,
17## but it saves a lot of typing now. And with netcat as its backend, it's
18## totally the balls. Don't have netcat? Get it here in /src/hacks!
19## _H* 950824, updated 951009 et seq.
20##
21## args: hostname [port]. You feed it the filename-parts of URLs.
22## In the loop, HOST, PORT, and SAVE do the right things; a null line
23## gets the previous spec again [useful for initial timeouts]; EOF to exit.
24## Relative URLs behave like a "cd" to wherever the last slash appears, or
25## just use the last component with the saved preceding "directory" part.
26## "\" clears the "filename" part and asks for just the "directory", and
27## ".." goes up one "directory" level while retaining the "filename" part.
28## Play around; you'll get used to it.
29
30if test "$1" = "" ; then
31 echo Needs hostname arg.
32 exit 1
33fi
34umask 022
35
36# optional PATH fixup
37# PATH=${HOME}:${PATH} ; export PATH
38
39test "${PAGER}" || PAGER=more
40BACKEND="nc -v -w 15"
41TMPAGE=/tmp/web$$
42host="$1"
43port="80"
44if test "$2" != "" ; then
45 port="$2"
46fi
47
48spec="/"
49specD="/"
50specF=''
51saving=''
52
53# be vaguely smart about temp file usage. Use your own homedir if you're
54# paranoid about someone symlink-racing your shell script, jeez.
55rm -f ${TMPAGE}
56test -f ${TMPAGE} && echo "Can't use ${TMPAGE}" && exit 1
57
58# get loopy. Yes, I know "echo -n" aint portable. Everything echoed would
59# need "\c" tacked onto the end in an SV universe, which you can fix yourself.
60while echo -n "${specD}${specF} " && read spec ; do
61 case $spec in
62 HOST)
63 echo -n 'New host: '
64 read host
65 continue
66 ;;
67 PORT)
68 echo -n 'New port: '
69 read port
70 continue
71 ;;
72 SAVE)
73 echo -n 'Save file: '
74 read saving
75# if we've already got a page, save it
76 test "${saving}" && test -f ${TMPAGE} &&
77 echo "=== ${host}:${specD}${specF} ===" >> $saving &&
78 cat ${TMPAGE} >> $saving && echo '' >> $saving
79 continue
80 ;;
81# changing the logic a bit here. Keep a state-concept of "current dir"
82# and "current file". Dir is /foo/bar/ ; file is "baz" or null.
83# leading slash: create whole new state.
84 /*)
85 specF=`echo "${spec}" | sed 's|.*/||'`
86 specD=`echo "${spec}" | sed 's|\(.*/\).*|\1|'`
87 spec="${specD}${specF}"
88 ;;
89# embedded slash: adding to the path. "file" part can be blank, too
90 */*)
91 specF=`echo "${spec}" | sed 's|.*/||'`
92 specD=`echo "${specD}${spec}" | sed 's|\(.*/\).*|\1|'`
93 ;;
94# dotdot: jump "up" one level and just reprompt [confirms what it did...]
95 ..)
96 specD=`echo "${specD}" | sed 's|\(.*/\)..*/|\1|'`
97 continue
98 ;;
99# blank line: do nothing, which will re-get the current one
100 '')
101 ;;
102# hack-quoted blank line: "\" means just zero out "file" part
103 '\')
104 specF=''
105 ;;
106# sigh
107 '?')
108 echo Help yourself. Read the script fer krissake.
109 continue
110 ;;
111# anything else is taken as a "file" part
112 *)
113 specF=${spec}
114 ;;
115 esac
116
117# now put it together and stuff it down a connection. Some lame non-unix
118# http servers assume they'll never get simple-query format, and wait till
119# an extra newline arrives. If you're up against one of these, change
120# below to (echo GET "$spec" ; echo '') | $BACKEND ...
121 spec="${specD}${specF}"
122 echo GET "${spec}" | $BACKEND $host $port > ${TMPAGE}
123 ${PAGER} ${TMPAGE}
124
125# save in a format that still shows the URLs we hit after a de-html run
126 if test "${saving}" ; then
127 echo "=== ${host}:${spec} ===" >> $saving
128 cat ${TMPAGE} >> $saving
129 echo '' >> $saving
130 fi
131done
132rm -f ${TMPAGE}
133exit 0
134
135#######
136# Encoding notes, finally from RFC 1738:
137# %XX -- hex-encode of special chars
138# allowed alphas in a URL: $_-.+!*'(),
139# relative names *not* described, but obviously used all over the place
140# transport://user:pass@host:port/path/name?query-string
141# wais: port 210, //host:port/database?search or /database/type/file?
142# cgi-bin/script?arg1=foo&arg2=bar&... scripts have to parse xxx&yyy&zzz
143# ISMAP imagemap stuff: /bin/foobar.map?xxx,yyy -- have to guess at coords!
144# local access-ctl files: ncsa: .htaccess ; cern: .www_acl
145#######
146# SEARCH ENGINES: fortunately, all are GET forms or at least work that way...
147# multi-word args for most cases: foo+bar
148# See 'websearch' for concise results of this research...
diff --git a/src/usr.bin/nc/scripts/webproxy b/src/usr.bin/nc/scripts/webproxy
new file mode 100644
index 0000000000..cee2d29fd1
--- /dev/null
+++ b/src/usr.bin/nc/scripts/webproxy
@@ -0,0 +1,138 @@
1#! /bin/sh
2## Web proxy, following the grand tradition of Web things being handled by
3## gross scripts. Uses netcat to listen on a high port [default 8000],
4## picks apart requests and sends them on to the right place. Point this
5## at the browser client machine you'll be coming from [to limit access to
6## only it], and point the browser's concept of an HTTP proxy to the
7## machine running this. Takes a single argument of the client that will
8## be using it, and rejects connections from elsewhere. LOGS the queries
9## to a configurable logfile, which can be an interesting read later on!
10## If the argument is "reset", the listener and logfile are cleaned up.
11##
12## This works surprisingly fast and well, for a shell script, although may
13## randomly fail when hammered by a browser that tries to open several
14## connections at once. Drop the "maximum connections" in your browser if
15## this is a problem.
16##
17## A more degenerate case of this, or preferably a small C program that
18## does the same thing under inetd, could handle a small site's worth of
19## proxy queries. Given the way browsers are evolving, proxies like this
20## can play an important role in protecting your own privacy.
21##
22## If you grabbed this in ASCII mode, search down for "eew" and make sure
23## the embedded-CR check is intact, or requests might hang.
24##
25## Doesn't handle POST forms. Who cares, if you're just watching HTTV?
26## Dumbness here has a highly desirable side effect: it only sends the first
27## GET line, since that's all you really ever need to send, and suppresses
28## the other somewhat revealing trash that most browsers insist on sending.
29
30# set these as you wish: proxy port...
31PORT=8000
32# logfile spec: a real file or /dev/null if you don't care
33LFILE=${0}.log
34# optional: where to dump connect info, so you can see if anything went wrong
35# CFILE=${0}.conn
36# optional extra args to the listener "nc", for instance "-s inside-net-addr"
37# XNC=''
38
39# functionality switch has to be done fast, so the next listener can start
40# prelaunch check: if no current client and no args, bail.
41case "${1}${CLIENT}" in
42 "")
43 echo needs client hostname
44 exit 1
45 ;;
46esac
47
48case "${1}" in
49 "")
50# Make like inetd, and run the next relayer process NOW. All the redirection
51# is necessary so this shell has NO remaining channel open to the net.
52# This will hang around for 10 minutes, and exit if no new connections arrive.
53# Using -n for speed, avoiding any DNS/port lookups.
54 nc -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" < /dev/null > /dev/null \
55 2> $CFILE &
56 ;;
57esac
58
59# no client yet and had an arg, this checking can be much slower now
60umask 077
61
62if test "$1" ; then
63# if magic arg, just clean up and then hit our own port to cause server exit
64 if test "$1" = "reset" ; then
65 rm -f $LFILE
66 test -f "$CFILE" && rm -f $CFILE
67 nc -w 1 -n 127.0.0.1 $PORT < /dev/null > /dev/null 2>&1
68 exit 0
69 fi
70# find our ass with both hands
71 test ! -f "$0" && echo "Oops, cannot find my own corporeal being" && exit 1
72# correct launch: set up client access control, passed along thru environment.
73 CLIENT="$1"
74 export CLIENT
75 test "$CFILE" || CFILE=/dev/null
76 export CFILE
77 touch "$CFILE"
78# tell us what happened during the last run, if possible
79 if test -f "$CFILE" ; then
80 echo "Last connection results:"
81 cat $CFILE
82 fi
83
84# ping client machine and get its bare IP address
85 CLIENT=`nc -z -v -w 8 "$1" 22000 2>&1 | sed 's/.*\[\(..*\)\].*/\1/'`
86 test ! "$CLIENT" && echo "Can't find address of $1" && exit 1
87
88# if this was an initial launch, be informative about it
89 echo "=== Launch: $CLIENT" >> $LFILE
90 echo "Proxy running -- will accept connections on $PORT from $CLIENT"
91 echo " Logging queries to $LFILE"
92 test -f "$CFILE" && echo " and connection fuckups to $CFILE"
93
94# and run the first listener, showing us output just for the first hit
95 nc -v -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" &
96 exit 0
97fi
98
99# Fall here to handle a page.
100# GET type://host.name:80/file/path HTTP/1.0
101# Additional: trash
102# More: trash
103# <newline>
104
105read x1 x2 x3 x4
106echo "=== query: $x1 $x2 $x3 $x4" >> $LFILE
107test "$x4" && echo "extra junk after request: $x4" && exit 0
108# nuke questionable characters and split up the request
109hurl=`echo "$x2" | sed -e "s+.*//++" -e 's+[\`'\''|$;<>{}\\!*()"]++g'`
110# echo massaged hurl: $hurl >> $LFILE
111hh=`echo "$hurl" | sed -e "s+/.*++" -e "s+:.*++"`
112hp=`echo "$hurl" | sed -e "s+.*:++" -e "s+/.*++"`
113test "$hp" = "$hh" && hp=80
114hf=`echo "$hurl" | sed -e "s+[^/]*++"`
115# echo total split: $hh : $hp : $hf >> $LFILE
116# suck in and log the entire request, because we're curious
117# Fails on multipart stuff like forms; oh well...
118if test "$x3" ; then
119 while read xx ; do
120 echo "${xx}" >> $LFILE
121 test "${xx}" || break
122# eew, buried returns, gross but necessary for DOS stupidity:
123 test "${xx}" = " " && break
124 done
125fi
126# check for non-GET *after* we log the query...
127test "$x1" != "GET" && echo "sorry, this proxy only does GETs" && exit 0
128# no, you can *not* phone home, you miserable piece of shit
129test "`echo $hh | fgrep -i netscap`" && \
130 echo "access to Netscam's servers <b>DENIED.</b>" && exit 0
131# Do it. 30 sec net-wait time oughta be *plenty*...
132# Some braindead servers have forgotten how to handle the simple-query syntax.
133# If necessary, replace below with (echo "$x1 $hf" ; echo '') | nc...
134echo "$x1 $hf" | nc -w 30 "$hh" "$hp" 2> /dev/null || \
135 echo "oops, can't get to $hh : $hp".
136echo "sent \"$x1 $hf\" to $hh : $hp" >> $LFILE
137exit 0
138
diff --git a/src/usr.bin/nc/scripts/webrelay b/src/usr.bin/nc/scripts/webrelay
new file mode 100644
index 0000000000..829a8b0708
--- /dev/null
+++ b/src/usr.bin/nc/scripts/webrelay
@@ -0,0 +1,44 @@
1#! /bin/sh
2## web relay -- a degenerate version of webproxy, usable with browsers that
3## don't understand proxies. This just forwards connections to a given server.
4## No query logging, no access control [although you can add it to XNC for
5## your own run], and full-URL links will undoubtedly confuse the browser
6## if it can't reach the server directly. This was actually written before
7## the full proxy was, and it shows.
8## The arguments in this case are the destination server and optional port.
9## Please flame pinheads who use self-referential absolute links.
10
11# set these as you wish: proxy port...
12PORT=8000
13# any extra args to the listening "nc", for instance "-s inside-net-addr"
14XNC=''
15
16# functionality switch, which has to be done fast to start the next listener
17case "${1}${RDEST}" in
18 "")
19 echo needs hostname
20 exit 1
21 ;;
22esac
23
24case "${1}" in
25 "")
26# no args: fire off new relayer process NOW. Will hang around for 10 minutes
27 nc -w 600 -l -n -p $PORT -e "$0" $XNC < /dev/null > /dev/null 2>&1 &
28# and handle this request, which will simply fail if vars not set yet.
29 exec nc -w 15 $RDEST $RPORT
30 ;;
31esac
32
33# Fall here for setup; this can now be slower.
34RDEST="$1"
35RPORT="$2"
36test "$RPORT" || RPORT=80
37export RDEST RPORT
38
39# Launch the first relayer same as above, but let its error msgs show up
40# will hang around for a minute, and exit if no new connections arrive.
41nc -v -w 600 -l -p $PORT -e "$0" $XNC < /dev/null > /dev/null &
42echo \
43 "Relay to ${RDEST}:${RPORT} running -- point your browser here on port $PORT"
44exit 0
diff --git a/src/usr.bin/nc/scripts/websearch b/src/usr.bin/nc/scripts/websearch
new file mode 100644
index 0000000000..60c3a3356a
--- /dev/null
+++ b/src/usr.bin/nc/scripts/websearch
@@ -0,0 +1,77 @@
1#! /bin/sh
2## Hit the major search engines. Hose the [large] output to a file!
3## autoconverts multiple arguments into the right format for given servers --
4## usually worda+wordb, with certain lame exceptions like dejanews.
5## Extracting and post-sorting the URLs is highly recommended...
6##
7## Altavista currently handled by a separate script; may merge at some point.
8##
9## _H* original 950824, updated 951218 and 960209
10
11test "${1}" = "" && echo 'Needs argument[s] to search for!' && exit 1
12PLUSARG="`echo $* | sed 's/ /+/g'`"
13PIPEARG="`echo ${PLUSARG} | sed 's/+/|/g'`"
14IFILE=/tmp/.webq.$$
15
16# Don't have "nc"? Get "netcat" from avian.org and add it to your toolkit.
17doquery () {
18 echo GET "$1" | nc -v -i 1 -w 30 "$2" "$3"
19}
20
21# changed since original: now supplying port numbers and separator lines...
22
23echo "=== Yahoo ==="
24doquery "/bin/search?p=${PLUSARG}&n=300&w=w&s=a" search.yahoo.com 80
25
26echo '' ; echo "=== Webcrawler ==="
27doquery "/cgi-bin/WebQuery?searchText=${PLUSARG}&maxHits=300" webcrawler.com 80
28
29# the infoseek lamers want "registration" before they do a real search, but...
30echo '' ; echo "=== Infoseek ==="
31echo " is broken."
32# doquery "WW/IS/Titles?qt=${PLUSARG}" www2.infoseek.com 80
33# ... which doesn't work cuz their lame server wants the extra newlines, WITH
34# CRLF pairs ferkrissake. Fuck 'em for now, they're hopelessly broken. If
35# you want to play, the basic idea and query formats follow.
36# echo "GET /WW/IS/Titles?qt=${PLUSARG}" > $IFILE
37# echo "" >> $IFILE
38# nc -v -w 30 guide-p.infoseek.com 80 < $IFILE
39
40# this is kinda flakey; might have to do twice??
41echo '' ; echo "=== Opentext ==="
42doquery "/omw/simplesearch?SearchFor=${PLUSARG}&mode=phrase" \
43 search.opentext.com 80
44
45# looks like inktomi will only take hits=100, or defaults back to 30
46# we try to suppress all the stupid rating dots here, too
47echo '' ; echo "=== Inktomi ==="
48doquery "/query/?query=${PLUSARG}&hits=100" ink3.cs.berkeley.edu 1234 | \
49 sed '/^<IMG ALT.*inktomi.*\.gif">$/d'
50
51#djnews lame shit limits hits to 120 and has nonstandard format
52echo '' ; echo "=== Dejanews ==="
53doquery "/cgi-bin/nph-dnquery?query=${PIPEARG}+maxhits=110+format=terse+defaultOp=AND" \
54 smithers.dejanews.com 80
55
56# OLD lycos: used to work until they fucking BROKE it...
57# doquery "/cgi-bin/pursuit?query=${PLUSARG}&maxhits=300&terse=1" \
58# query5.lycos.cs.cmu.edu 80
59# NEW lycos: wants the User-agent field present in query or it returns nothing
60# 960206: webmaster@lycos duly bitched at
61# 960208: reply received; here's how we will now handle it:
62echo \
63"GET /cgi-bin/pursuit?query=${PLUSARG}&maxhits=300&terse=terse&matchmode=and&minscore=.5 HTTP/1.x" \
64 > $IFILE
65echo "User-agent: *FUCK OFF*" >> $IFILE
66echo "Why: go ask todd@pointcom.com (Todd Whitney)" >> $IFILE
67echo '' >> $IFILE
68echo '' ; echo "=== Lycos ==="
69nc -v -i 1 -w 30 twelve.srv.lycos.com 80 < $IFILE
70
71rm -f $IFILE
72exit 0
73
74# CURRENTLY BROKEN [?]
75# infoseek
76
77# some args need to be redone to ensure whatever "and" mode applies