From 31dbf5249c74bc884a4f1c6fbb3f13ae58d67022 Mon Sep 17 00:00:00 2001 From: cvs2svn Date: Fri, 26 Mar 1999 18:24:02 +0000 Subject: This commit was manufactured by cvs2git to create tag 'OPENBSD_2_5_BASE'. --- src/lib/libc/crypt/Makefile.inc | 12 + src/lib/libc/crypt/arc4random.3 | 83 + src/lib/libc/crypt/arc4random.c | 176 ++ src/lib/libc/crypt/bcrypt.c | 362 +++ src/lib/libc/crypt/blowfish.3 | 104 + src/lib/libc/crypt/blowfish.c | 774 +++++ src/lib/libc/crypt/cast.c | 779 +++++ src/lib/libc/crypt/crypt.3 | 298 ++ src/lib/libc/crypt/crypt.c | 714 +++++ src/lib/libc/crypt/md5crypt.c | 157 + src/lib/libc/crypt/morecrypt.c | 628 ++++ src/lib/libc/crypt/skipjack.c | 258 ++ src/lib/libc/include/namespace.h | 18 + src/lib/libc/include/thread_private.h | 179 ++ src/lib/libc/net/Makefile.inc | 56 + src/lib/libc/net/base64.c | 317 ++ src/lib/libc/net/byteorder.3 | 155 + src/lib/libc/net/ethers.3 | 109 + src/lib/libc/net/ethers.c | 270 ++ src/lib/libc/net/gethostbyname.3 | 266 ++ src/lib/libc/net/gethostnamadr.c | 1147 +++++++ src/lib/libc/net/getnetbyaddr.c | 56 + src/lib/libc/net/getnetbyname.c | 62 + src/lib/libc/net/getnetent.3 | 148 + src/lib/libc/net/getnetent.c | 129 + src/lib/libc/net/getnetnamadr.c | 382 +++ src/lib/libc/net/getproto.c | 55 + src/lib/libc/net/getprotoent.3 | 144 + src/lib/libc/net/getprotoent.c | 123 + src/lib/libc/net/getprotoname.c | 62 + src/lib/libc/net/getservbyname.c | 87 + src/lib/libc/net/getservbyport.c | 60 + src/lib/libc/net/getservent.3 | 155 + src/lib/libc/net/getservent.c | 125 + src/lib/libc/net/herror.c | 121 + src/lib/libc/net/htonl.c | 25 + src/lib/libc/net/htons.c | 29 + src/lib/libc/net/inet.3 | 319 ++ src/lib/libc/net/inet_addr.c | 187 ++ src/lib/libc/net/inet_lnaof.c | 59 + src/lib/libc/net/inet_makeaddr.c | 62 + src/lib/libc/net/inet_net.3 | 149 + src/lib/libc/net/inet_net_ntop.c | 139 + src/lib/libc/net/inet_net_pton.c | 207 ++ src/lib/libc/net/inet_neta.c | 83 + src/lib/libc/net/inet_netof.c | 58 + src/lib/libc/net/inet_network.c | 92 + src/lib/libc/net/inet_ntoa.c | 59 + src/lib/libc/net/inet_ntop.c | 194 ++ src/lib/libc/net/inet_pton.c | 220 ++ src/lib/libc/net/ipx.3 | 126 + src/lib/libc/net/ipx_addr.c | 229 ++ src/lib/libc/net/ipx_ntoa.c | 58 + src/lib/libc/net/iso_addr.3 | 110 + src/lib/libc/net/iso_addr.c | 119 + src/lib/libc/net/link_addr.3 | 131 + src/lib/libc/net/linkaddr.c | 158 + src/lib/libc/net/ns.3 | 130 + src/lib/libc/net/ns_addr.c | 228 ++ src/lib/libc/net/ns_ntoa.c | 101 + src/lib/libc/net/nsap_addr.c | 109 + src/lib/libc/net/ntohl.c | 25 + src/lib/libc/net/ntohs.c | 29 + src/lib/libc/net/rcmd.3 | 227 ++ src/lib/libc/net/rcmd.c | 607 ++++ src/lib/libc/net/rcmdsh.3 | 108 + src/lib/libc/net/rcmdsh.c | 124 + src/lib/libc/net/recv.c | 50 + src/lib/libc/net/res_comp.c | 510 ++++ src/lib/libc/net/res_data.c | 117 + src/lib/libc/net/res_debug.c | 1518 ++++++++++ src/lib/libc/net/res_init.c | 518 ++++ src/lib/libc/net/res_mkquery.c | 195 ++ src/lib/libc/net/res_query.c | 398 +++ src/lib/libc/net/res_random.c | 233 ++ src/lib/libc/net/res_send.c | 780 +++++ src/lib/libc/net/resolver.3 | 345 +++ src/lib/libc/net/send.c | 50 + src/lib/libc/net/sethostent.c | 60 + src/lib/libc/stdlib/Makefile.inc | 54 + src/lib/libc/stdlib/_rand48.c | 50 + src/lib/libc/stdlib/a64l.3 | 122 + src/lib/libc/stdlib/a64l.c | 46 + src/lib/libc/stdlib/abort.3 | 70 + src/lib/libc/stdlib/abort.c | 86 + src/lib/libc/stdlib/abs.3 | 73 + src/lib/libc/stdlib/abs.c | 45 + src/lib/libc/stdlib/alloca.3 | 75 + src/lib/libc/stdlib/atexit.3 | 77 + src/lib/libc/stdlib/atexit.c | 67 + src/lib/libc/stdlib/atexit.h | 45 + src/lib/libc/stdlib/atof.3 | 73 + src/lib/libc/stdlib/atof.c | 45 + src/lib/libc/stdlib/atoi.3 | 73 + src/lib/libc/stdlib/atoi.c | 45 + src/lib/libc/stdlib/atol.3 | 74 + src/lib/libc/stdlib/atol.c | 45 + src/lib/libc/stdlib/bsearch.3 | 90 + src/lib/libc/stdlib/bsearch.c | 79 + src/lib/libc/stdlib/calloc.3 | 70 + src/lib/libc/stdlib/calloc.c | 53 + src/lib/libc/stdlib/cfree.c | 49 + src/lib/libc/stdlib/div.3 | 69 + src/lib/libc/stdlib/div.c | 79 + src/lib/libc/stdlib/drand48.c | 26 + src/lib/libc/stdlib/erand48.c | 27 + src/lib/libc/stdlib/exit.3 | 83 + src/lib/libc/stdlib/exit.c | 69 + src/lib/libc/stdlib/getenv.3 | 155 + src/lib/libc/stdlib/getenv.c | 89 + src/lib/libc/stdlib/getopt.3 | 261 ++ src/lib/libc/stdlib/getopt.c | 117 + src/lib/libc/stdlib/getsubopt.3 | 148 + src/lib/libc/stdlib/getsubopt.c | 106 + src/lib/libc/stdlib/heapsort.c | 183 ++ src/lib/libc/stdlib/jrand48.c | 25 + src/lib/libc/stdlib/l64a.c | 46 + src/lib/libc/stdlib/labs.3 | 66 + src/lib/libc/stdlib/labs.c | 45 + src/lib/libc/stdlib/lcong48.c | 34 + src/lib/libc/stdlib/ldiv.3 | 71 + src/lib/libc/stdlib/ldiv.c | 58 + src/lib/libc/stdlib/lrand48.c | 27 + src/lib/libc/stdlib/malloc.3 | 355 +++ src/lib/libc/stdlib/malloc.c | 1263 ++++++++ src/lib/libc/stdlib/memory.3 | 70 + src/lib/libc/stdlib/merge.c | 347 +++ src/lib/libc/stdlib/mrand48.c | 27 + src/lib/libc/stdlib/multibyte.c | 130 + src/lib/libc/stdlib/nrand48.c | 25 + src/lib/libc/stdlib/putenv.c | 58 + src/lib/libc/stdlib/qabs.3 | 61 + src/lib/libc/stdlib/qabs.c | 45 + src/lib/libc/stdlib/qdiv.3 | 65 + src/lib/libc/stdlib/qdiv.c | 58 + src/lib/libc/stdlib/qsort.3 | 233 ++ src/lib/libc/stdlib/qsort.c | 175 ++ src/lib/libc/stdlib/radixsort.3 | 161 + src/lib/libc/stdlib/radixsort.c | 317 ++ src/lib/libc/stdlib/rand.3 | 104 + src/lib/libc/stdlib/rand.c | 65 + src/lib/libc/stdlib/rand48.3 | 161 + src/lib/libc/stdlib/rand48.h | 32 + src/lib/libc/stdlib/random.3 | 183 ++ src/lib/libc/stdlib/random.c | 370 +++ src/lib/libc/stdlib/realpath.3 | 125 + src/lib/libc/stdlib/realpath.c | 163 + src/lib/libc/stdlib/seed48.c | 40 + src/lib/libc/stdlib/setenv.c | 120 + src/lib/libc/stdlib/srand48.c | 34 + src/lib/libc/stdlib/strtod.3 | 113 + src/lib/libc/stdlib/strtod.c | 2529 ++++++++++++++++ src/lib/libc/stdlib/strtol.3 | 168 + src/lib/libc/stdlib/strtol.c | 150 + src/lib/libc/stdlib/strtoq.c | 152 + src/lib/libc/stdlib/strtoul.3 | 163 + src/lib/libc/stdlib/strtoul.c | 112 + src/lib/libc/stdlib/strtouq.c | 114 + src/lib/libc/stdlib/system.3 | 98 + src/lib/libc/stdlib/system.c | 80 + src/lib/libc/stdlib/tfind.c | 41 + src/lib/libc/stdlib/tsearch.3 | 116 + src/lib/libc/stdlib/tsearch.c | 126 + src/lib/libc/string/Makefile.inc | 128 + src/lib/libc/string/__strerror.c | 111 + src/lib/libc/string/__strsignal.c | 104 + src/lib/libc/string/bcmp.3 | 71 + src/lib/libc/string/bcmp.c | 63 + src/lib/libc/string/bcopy.3 | 71 + src/lib/libc/string/bcopy.c | 138 + src/lib/libc/string/bm.3 | 113 + src/lib/libc/string/bm.c | 219 ++ src/lib/libc/string/bstring.3 | 109 + src/lib/libc/string/bzero.3 | 67 + src/lib/libc/string/bzero.c | 56 + src/lib/libc/string/ffs.3 | 62 + src/lib/libc/string/ffs.c | 58 + src/lib/libc/string/index.3 | 82 + src/lib/libc/string/index.c | 55 + src/lib/libc/string/memccpy.3 | 75 + src/lib/libc/string/memccpy.c | 64 + src/lib/libc/string/memchr.3 | 81 + src/lib/libc/string/memchr.c | 58 + src/lib/libc/string/memcmp.3 | 82 + src/lib/libc/string/memcmp.c | 60 + src/lib/libc/string/memcpy.3 | 83 + src/lib/libc/string/memmove.3 | 75 + src/lib/libc/string/memset.3 | 72 + src/lib/libc/string/memset.c | 58 + src/lib/libc/string/rindex.3 | 80 + src/lib/libc/string/rindex.c | 57 + src/lib/libc/string/strcasecmp.3 | 89 + src/lib/libc/string/strcasecmp.c | 120 + src/lib/libc/string/strcat.3 | 137 + src/lib/libc/string/strcat.c | 54 + src/lib/libc/string/strchr.3 | 87 + src/lib/libc/string/strcmp.3 | 93 + src/lib/libc/string/strcmp.c | 58 + src/lib/libc/string/strcoll.3 | 74 + src/lib/libc/string/strcoll.c | 52 + src/lib/libc/string/strcpy.3 | 154 + src/lib/libc/string/strcpy.c | 53 + src/lib/libc/string/strcspn.3 | 84 + src/lib/libc/string/strcspn.c | 67 + src/lib/libc/string/strdup.3 | 68 + src/lib/libc/string/strdup.c | 62 + src/lib/libc/string/strerror.3 | 65 + src/lib/libc/string/strerror.c | 55 + src/lib/libc/string/string.3 | 155 + src/lib/libc/string/strlcat.c | 71 + src/lib/libc/string/strlcpy.3 | 147 + src/lib/libc/string/strlcpy.c | 63 + src/lib/libc/string/strlen.3 | 70 + src/lib/libc/string/strlen.c | 53 + src/lib/libc/string/strmode.3 | 152 + src/lib/libc/string/strmode.c | 152 + src/lib/libc/string/strncat.c | 67 + src/lib/libc/string/strncmp.c | 59 + src/lib/libc/string/strncpy.c | 67 + src/lib/libc/string/strpbrk.3 | 79 + src/lib/libc/string/strpbrk.c | 56 + src/lib/libc/string/strrchr.3 | 90 + src/lib/libc/string/strsep.3 | 110 + src/lib/libc/string/strsep.c | 85 + src/lib/libc/string/strsignal.3 | 60 + src/lib/libc/string/strsignal.c | 49 + src/lib/libc/string/strspn.3 | 79 + src/lib/libc/string/strspn.c | 60 + src/lib/libc/string/strstr.3 | 88 + src/lib/libc/string/strstr.c | 64 + src/lib/libc/string/strtok.3 | 112 + src/lib/libc/string/strtok.c | 89 + src/lib/libc/string/strxfrm.3 | 68 + src/lib/libc/string/strxfrm.c | 73 + src/lib/libc/string/swab.3 | 65 + src/lib/libc/string/swab.c | 65 + src/lib/libcrypto/Makefile.ssl | 161 + src/lib/libcrypto/asn1/Makefile.ssl | 120 + src/lib/libcrypto/asn1/a_bmp.c | 90 + src/lib/libcrypto/asn1/asn1.err | 182 ++ src/lib/libcrypto/asn1/d2i_dhp.c | 108 + src/lib/libcrypto/asn1/d2i_dsap.c | 101 + src/lib/libcrypto/asn1/d2i_r_pr.c | 129 + src/lib/libcrypto/asn1/d2i_r_pu.c | 100 + src/lib/libcrypto/asn1/d2i_s_pr.c | 113 + src/lib/libcrypto/asn1/d2i_s_pu.c | 124 + src/lib/libcrypto/asn1/f.c | 80 + src/lib/libcrypto/asn1/i2d_dhp.c | 128 + src/lib/libcrypto/asn1/i2d_dsap.c | 121 + src/lib/libcrypto/asn1/i2d_r_pr.c | 132 + src/lib/libcrypto/asn1/i2d_r_pu.c | 118 + src/lib/libcrypto/asn1/i2d_s_pr.c | 128 + src/lib/libcrypto/asn1/i2d_s_pu.c | 133 + src/lib/libcrypto/asn1/p7_dgst.c | 130 + src/lib/libcrypto/asn1/p7_enc.c | 120 + src/lib/libcrypto/asn1/p7_enc_c.c | 127 + src/lib/libcrypto/asn1/p7_evp.c | 125 + src/lib/libcrypto/asn1/p7_i_s.c | 120 + src/lib/libcrypto/asn1/p7_lib.c | 299 ++ src/lib/libcrypto/asn1/p7_recip.c | 132 + src/lib/libcrypto/asn1/p7_s_e.c | 146 + src/lib/libcrypto/asn1/p7_signd.c | 140 + src/lib/libcrypto/asn1/p7_signi.c | 149 + src/lib/libcrypto/asn1/pkcs8.c | 142 + src/lib/libcrypto/asn1/x_cinf.c | 197 ++ src/lib/libcrypto/bf/Makefile.ssl | 107 + src/lib/libcrypto/bf/Makefile.uni | 169 ++ src/lib/libcrypto/bf/asm/b-win32.asm | 906 ++++++ src/lib/libcrypto/bf/asm/bx86unix.cpp | 976 ++++++ src/lib/libcrypto/bf/bf_locl.org | 242 ++ src/lib/libcrypto/bio/Makefile.ssl | 92 + src/lib/libcrypto/bio/bio.err | 46 + src/lib/libcrypto/bn/Makefile.ssl | 133 + src/lib/libcrypto/bn/asm/bn-win32.asm | 689 +++++ src/lib/libcrypto/bn/asm/bn86unix.cpp | 752 +++++ src/lib/libcrypto/bn/asm/sparc.s | 462 +++ src/lib/libcrypto/bn/asm/x86w16.asm | 297 ++ src/lib/libcrypto/bn/asm/x86w32.asm | 362 +++ src/lib/libcrypto/bn/bn.err | 27 + src/lib/libcrypto/bn/bn.org | 502 +++ src/lib/libcrypto/bn/bn_m.c | 169 ++ src/lib/libcrypto/bn/bn_mulw.c | 366 +++ src/lib/libcrypto/bn/bn_sub.c | 180 ++ src/lib/libcrypto/buffer/Makefile.ssl | 84 + src/lib/libcrypto/buffer/buffer.err | 9 + src/lib/libcrypto/cast/Makefile.ssl | 109 + src/lib/libcrypto/cast/Makefile.uni | 123 + src/lib/libcrypto/cast/asm/c-win32.asm | 940 ++++++ src/lib/libcrypto/cast/asm/cx86unix.cpp | 1010 ++++++ src/lib/libcrypto/conf/Makefile.ssl | 85 + src/lib/libcrypto/conf/conf.c | 727 +++++ src/lib/libcrypto/conf/conf.err | 12 + src/lib/libcrypto/conf/conf_lcl.h | 102 + src/lib/libcrypto/cryptall.h | 110 + src/lib/libcrypto/crypto.c | 575 ++++ src/lib/libcrypto/crypto.err | 8 + src/lib/libcrypto/date.h | 1 + src/lib/libcrypto/des/DES.pod | 16 + src/lib/libcrypto/des/FILES | 96 + src/lib/libcrypto/des/MODES.DES | 84 + src/lib/libcrypto/des/Makefile.PL | 14 + src/lib/libcrypto/des/Makefile.lit | 250 ++ src/lib/libcrypto/des/Makefile.ssl | 140 + src/lib/libcrypto/des/Makefile.uni | 263 ++ src/lib/libcrypto/des/PC1 | 28 + src/lib/libcrypto/des/PC2 | 57 + src/lib/libcrypto/des/asm/d-win32.asm | 3132 +++++++++++++++++++ src/lib/libcrypto/des/asm/dx86unix.cpp | 3202 ++++++++++++++++++++ src/lib/libcrypto/des/asm/y-win32.asm | 929 ++++++ src/lib/libcrypto/des/asm/yx86unix.cpp | 976 ++++++ src/lib/libcrypto/des/des.man | 186 ++ src/lib/libcrypto/des/des.org | 301 ++ src/lib/libcrypto/des/des.pl | 552 ++++ src/lib/libcrypto/des/des_crypt.man | 508 ++++ src/lib/libcrypto/des/des_locl.org | 516 ++++ src/lib/libcrypto/des/doIP | 46 + src/lib/libcrypto/des/doPC1 | 110 + src/lib/libcrypto/des/doPC2 | 94 + src/lib/libcrypto/des/ede_enc.c | 190 ++ src/lib/libcrypto/des/podd.h | 75 + src/lib/libcrypto/des/ranlib.sh | 23 + src/lib/libcrypto/des/shifts.pl | 198 ++ src/lib/libcrypto/des/sk.h | 204 ++ src/lib/libcrypto/des/supp.c | 109 + src/lib/libcrypto/des/testdes.pl | 167 + src/lib/libcrypto/des/vms.com | 90 + src/lib/libcrypto/dh/Makefile.ssl | 84 + src/lib/libcrypto/dh/dh.err | 12 + src/lib/libcrypto/dsa/Makefile.ssl | 84 + src/lib/libcrypto/dsa/dsa.err | 15 + src/lib/libcrypto/err/Makefile.ssl | 80 + src/lib/libcrypto/err/err_code.pl | 105 + src/lib/libcrypto/err/err_genc.pl | 198 ++ src/lib/libcrypto/err/error.err | 13 + src/lib/libcrypto/err/ssleay.ec | 57 + src/lib/libcrypto/evp/Makefile.ssl | 111 + src/lib/libcrypto/evp/e_cbc_3d.c | 163 + src/lib/libcrypto/evp/e_cbc_bf.c | 119 + src/lib/libcrypto/evp/e_cbc_c.c | 119 + src/lib/libcrypto/evp/e_cbc_d.c | 116 + src/lib/libcrypto/evp/e_cbc_i.c | 131 + src/lib/libcrypto/evp/e_cbc_r2.c | 136 + src/lib/libcrypto/evp/e_cbc_r5.c | 120 + src/lib/libcrypto/evp/e_cfb_3d.c | 166 + src/lib/libcrypto/evp/e_cfb_bf.c | 121 + src/lib/libcrypto/evp/e_cfb_c.c | 121 + src/lib/libcrypto/evp/e_cfb_d.c | 118 + src/lib/libcrypto/evp/e_cfb_i.c | 121 + src/lib/libcrypto/evp/e_cfb_r2.c | 122 + src/lib/libcrypto/evp/e_cfb_r5.c | 122 + src/lib/libcrypto/evp/e_ecb_3d.c | 161 + src/lib/libcrypto/evp/e_ecb_bf.c | 122 + src/lib/libcrypto/evp/e_ecb_c.c | 122 + src/lib/libcrypto/evp/e_ecb_d.c | 118 + src/lib/libcrypto/evp/e_ecb_i.c | 133 + src/lib/libcrypto/evp/e_ecb_r2.c | 123 + src/lib/libcrypto/evp/e_ecb_r5.c | 123 + src/lib/libcrypto/evp/e_ofb_3d.c | 165 + src/lib/libcrypto/evp/e_ofb_bf.c | 122 + src/lib/libcrypto/evp/e_ofb_c.c | 122 + src/lib/libcrypto/evp/e_ofb_d.c | 118 + src/lib/libcrypto/evp/e_ofb_i.c | 121 + src/lib/libcrypto/evp/e_ofb_r2.c | 123 + src/lib/libcrypto/evp/e_ofb_r5.c | 123 + src/lib/libcrypto/evp/evp.err | 24 + src/lib/libcrypto/evp/pk_lib.c | 82 + src/lib/libcrypto/hmac/Makefile.ssl | 80 + src/lib/libcrypto/idea/Makefile.ssl | 80 + src/lib/libcrypto/lhash/Makefile.ssl | 80 + src/lib/libcrypto/libvms.com | 31 + src/lib/libcrypto/md2/Makefile.ssl | 80 + src/lib/libcrypto/md2/md2.org | 106 + src/lib/libcrypto/md5/Makefile.ssl | 104 + src/lib/libcrypto/md5/Makefile.uni | 109 + src/lib/libcrypto/md5/asm/m5-win32.asm | 686 +++++ src/lib/libcrypto/md5/asm/mx86unix.cpp | 730 +++++ src/lib/libcrypto/mdc2/Makefile.ssl | 80 + src/lib/libcrypto/objects/Makefile.ssl | 87 + src/lib/libcrypto/objects/objects.err | 12 + src/lib/libcrypto/pem/Makefile.ssl | 96 + src/lib/libcrypto/pem/ctx_size.c | 122 + src/lib/libcrypto/pem/pem.err | 38 + src/lib/libcrypto/pem/pem.org | 562 ++++ src/lib/libcrypto/pkcs7/Makefile.ssl | 86 + src/lib/libcrypto/pkcs7/README | 5 + src/lib/libcrypto/pkcs7/mf.p7 | 18 + src/lib/libcrypto/pkcs7/p7.tst | 33 + src/lib/libcrypto/pkcs7/pkcs7.err | 26 + src/lib/libcrypto/rand/Makefile.ssl | 80 + src/lib/libcrypto/ranlib.sh | 23 + src/lib/libcrypto/rc2/Makefile.ssl | 80 + src/lib/libcrypto/rc2/Makefile.uni | 72 + src/lib/libcrypto/rc2/rc2.org | 118 + src/lib/libcrypto/rc4/Makefile.ssl | 108 + src/lib/libcrypto/rc4/Makefile.uni | 102 + src/lib/libcrypto/rc4/asm/r4-win32.asm | 314 ++ src/lib/libcrypto/rc4/asm/rx86unix.cpp | 358 +++ src/lib/libcrypto/rc4/rc4.org | 103 + src/lib/libcrypto/rc4/rc4_locl.org | 70 + src/lib/libcrypto/rc5/Makefile.ssl | 107 + src/lib/libcrypto/rc5/Makefile.uni | 72 + src/lib/libcrypto/rc5/asm/r5-win32.asm | 574 ++++ src/lib/libcrypto/rc5/asm/r586unix.cpp | 628 ++++ src/lib/libcrypto/rc5/asm/rc5-586.pl | 109 + src/lib/libcrypto/ripemd/Makefile.ssl | 104 + src/lib/libcrypto/ripemd/Makefile.uni | 109 + src/lib/libcrypto/ripemd/asm/rm-win32.asm | 1972 ++++++++++++ src/lib/libcrypto/ripemd/asm/rm86unix.cpp | 2016 ++++++++++++ src/lib/libcrypto/rsa/Makefile.ssl | 86 + src/lib/libcrypto/rsa/rsa.err | 45 + src/lib/libcrypto/sha/Makefile.ssl | 103 + src/lib/libcrypto/sha/Makefile.uni | 122 + src/lib/libcrypto/sha/asm/s1-win32.asm | 1664 ++++++++++ src/lib/libcrypto/sha/asm/sx86unix.cpp | 1948 ++++++++++++ src/lib/libcrypto/sha/sha_sgst.c | 246 ++ src/lib/libcrypto/stack/Makefile.ssl | 80 + src/lib/libcrypto/txt_db/Makefile.ssl | 80 + src/lib/libcrypto/util/mklink.sh | 35 + src/lib/libcrypto/util/ranlib.sh | 23 + src/lib/libcrypto/util/sep_lib.sh | 37 + src/lib/libcrypto/util/ssldir.pl | 52 + src/lib/libcrypto/util/up_ver.pl | 79 + src/lib/libcrypto/x509/Makefile.ssl | 96 + src/lib/libcrypto/x509/attrib | 38 + src/lib/libcrypto/x509/v3_net.c | 87 + src/lib/libcrypto/x509/v3_x509.c | 253 ++ src/lib/libcrypto/x509/x509.doc | 27 + src/lib/libcrypto/x509/x509.err | 46 + src/lib/libcrypto/x509/x509pack.c | 157 + src/lib/libcrypto/x509/x509v3.doc | 24 + src/lib/libcrypto/x509v3/format | 92 + src/lib/libcrypto/x509v3/header | 6 + src/lib/libcrypto/x509v3/v3_ku.c | 318 ++ src/lib/libssl/Makefile | 10 + src/lib/libssl/Makefile.bsd-wrapper | 100 + src/lib/libssl/README.OPENBSD | 66 + src/lib/libssl/crypto-patent/Makefile | 179 ++ src/lib/libssl/crypto-patent/shlib_version | 2 + src/lib/libssl/crypto/Makefile | 183 ++ src/lib/libssl/crypto/shlib_version | 2 + src/lib/libssl/src/COPYRIGHT | 65 + src/lib/libssl/src/Configure | 2 + src/lib/libssl/src/HISTORY | 316 ++ src/lib/libssl/src/HISTORY.066 | 443 +++ src/lib/libssl/src/MICROSOFT | 146 + src/lib/libssl/src/MINFO | 968 ++++++ src/lib/libssl/src/Makefile.ssl | 331 ++ src/lib/libssl/src/PATENTS | 9 + src/lib/libssl/src/README.066 | 27 + src/lib/libssl/src/README.080 | 147 + src/lib/libssl/src/README.090 | 71 + src/lib/libssl/src/TODO | 28 + src/lib/libssl/src/VERSION | 24 + src/lib/libssl/src/apps/Makefile.ssl | 144 + src/lib/libssl/src/apps/der_chop | 305 ++ src/lib/libssl/src/apps/eay.c | 130 + src/lib/libssl/src/apps/ext.v3 | 2 + src/lib/libssl/src/apps/g_ssleay.pl | 114 + src/lib/libssl/src/apps/mklinks | 7 + src/lib/libssl/src/apps/pem_mail.c | 170 ++ src/lib/libssl/src/apps/rmlinks | 6 + src/lib/libssl/src/apps/ssleay.c | 342 +++ src/lib/libssl/src/apps/ssleay.cnf | 116 + src/lib/libssl/src/apps/tkca | 66 + src/lib/libssl/src/certs/ICE-CA.pem | 59 + src/lib/libssl/src/certs/ICE-root.pem | 48 + src/lib/libssl/src/certs/ICE-user.pem | 63 + src/lib/libssl/src/certs/ICE.crl | 9 + src/lib/libssl/src/certs/ca-cert.pem | 31 + src/lib/libssl/src/certs/dsa-ca.pem | 43 + src/lib/libssl/src/certs/dsa-pca.pem | 49 + src/lib/libssl/src/certs/factory.pem | 15 + src/lib/libssl/src/certs/nortelCA.pem | 16 + src/lib/libssl/src/certs/pca-cert.pem | 31 + src/lib/libssl/src/certs/rsa-cca.pem | 19 + src/lib/libssl/src/certs/rsa-ssca.pem | 19 + src/lib/libssl/src/certs/timCA.pem | 16 + src/lib/libssl/src/certs/tjhCA.pem | 15 + src/lib/libssl/src/certs/vsign2.pem | 31 + src/lib/libssl/src/certs/vsign4.pem | 16 + src/lib/libssl/src/crypto/Makefile.ssl | 161 + src/lib/libssl/src/crypto/asn1/Makefile.ssl | 120 + src/lib/libssl/src/crypto/asn1/a_bmp.c | 90 + src/lib/libssl/src/crypto/asn1/asn1.err | 182 ++ src/lib/libssl/src/crypto/asn1/d2i_dhp.c | 108 + src/lib/libssl/src/crypto/asn1/d2i_dsap.c | 101 + src/lib/libssl/src/crypto/asn1/d2i_r_pr.c | 129 + src/lib/libssl/src/crypto/asn1/d2i_r_pu.c | 100 + src/lib/libssl/src/crypto/asn1/d2i_s_pr.c | 113 + src/lib/libssl/src/crypto/asn1/d2i_s_pu.c | 124 + src/lib/libssl/src/crypto/asn1/f.c | 80 + src/lib/libssl/src/crypto/asn1/i2d_dhp.c | 128 + src/lib/libssl/src/crypto/asn1/i2d_dsap.c | 121 + src/lib/libssl/src/crypto/asn1/i2d_r_pr.c | 132 + src/lib/libssl/src/crypto/asn1/i2d_r_pu.c | 118 + src/lib/libssl/src/crypto/asn1/i2d_s_pr.c | 128 + src/lib/libssl/src/crypto/asn1/i2d_s_pu.c | 133 + src/lib/libssl/src/crypto/asn1/p7_dgst.c | 130 + src/lib/libssl/src/crypto/asn1/p7_enc.c | 120 + src/lib/libssl/src/crypto/asn1/p7_enc_c.c | 127 + src/lib/libssl/src/crypto/asn1/p7_evp.c | 125 + src/lib/libssl/src/crypto/asn1/p7_i_s.c | 120 + src/lib/libssl/src/crypto/asn1/p7_lib.c | 299 ++ src/lib/libssl/src/crypto/asn1/p7_recip.c | 132 + src/lib/libssl/src/crypto/asn1/p7_s_e.c | 146 + src/lib/libssl/src/crypto/asn1/p7_signd.c | 140 + src/lib/libssl/src/crypto/asn1/p7_signi.c | 149 + src/lib/libssl/src/crypto/asn1/pkcs8.c | 142 + src/lib/libssl/src/crypto/asn1/x_cinf.c | 197 ++ src/lib/libssl/src/crypto/bf/Makefile.ssl | 107 + src/lib/libssl/src/crypto/bf/Makefile.uni | 169 ++ src/lib/libssl/src/crypto/bf/asm/b-win32.asm | 906 ++++++ src/lib/libssl/src/crypto/bf/asm/bx86unix.cpp | 976 ++++++ src/lib/libssl/src/crypto/bf/bf_locl.org | 242 ++ src/lib/libssl/src/crypto/bio/Makefile.ssl | 92 + src/lib/libssl/src/crypto/bio/bio.err | 46 + src/lib/libssl/src/crypto/bn/Makefile.ssl | 133 + src/lib/libssl/src/crypto/bn/asm/bn-win32.asm | 689 +++++ src/lib/libssl/src/crypto/bn/asm/bn86unix.cpp | 752 +++++ src/lib/libssl/src/crypto/bn/asm/sparc.s | 462 +++ src/lib/libssl/src/crypto/bn/asm/x86w16.asm | 297 ++ src/lib/libssl/src/crypto/bn/asm/x86w32.asm | 362 +++ src/lib/libssl/src/crypto/bn/bn.err | 27 + src/lib/libssl/src/crypto/bn/bn.org | 502 +++ src/lib/libssl/src/crypto/bn/bn_m.c | 169 ++ src/lib/libssl/src/crypto/bn/bn_mulw.c | 366 +++ src/lib/libssl/src/crypto/bn/bn_sub.c | 180 ++ src/lib/libssl/src/crypto/buffer/Makefile.ssl | 84 + src/lib/libssl/src/crypto/buffer/buffer.err | 9 + src/lib/libssl/src/crypto/cast/Makefile.ssl | 109 + src/lib/libssl/src/crypto/cast/Makefile.uni | 123 + src/lib/libssl/src/crypto/cast/asm/c-win32.asm | 940 ++++++ src/lib/libssl/src/crypto/cast/asm/cx86unix.cpp | 1010 ++++++ src/lib/libssl/src/crypto/conf/Makefile.ssl | 85 + src/lib/libssl/src/crypto/conf/conf.c | 727 +++++ src/lib/libssl/src/crypto/conf/conf.err | 12 + src/lib/libssl/src/crypto/conf/conf_lcl.h | 102 + src/lib/libssl/src/crypto/cryptall.h | 110 + src/lib/libssl/src/crypto/crypto.c | 575 ++++ src/lib/libssl/src/crypto/crypto.err | 8 + src/lib/libssl/src/crypto/date.h | 1 + src/lib/libssl/src/crypto/des/DES.pod | 16 + src/lib/libssl/src/crypto/des/FILES | 96 + src/lib/libssl/src/crypto/des/MODES.DES | 84 + src/lib/libssl/src/crypto/des/Makefile.PL | 14 + src/lib/libssl/src/crypto/des/Makefile.lit | 250 ++ src/lib/libssl/src/crypto/des/Makefile.ssl | 140 + src/lib/libssl/src/crypto/des/Makefile.uni | 263 ++ src/lib/libssl/src/crypto/des/PC1 | 28 + src/lib/libssl/src/crypto/des/PC2 | 57 + src/lib/libssl/src/crypto/des/asm/d-win32.asm | 3132 +++++++++++++++++++ src/lib/libssl/src/crypto/des/asm/dx86unix.cpp | 3202 ++++++++++++++++++++ src/lib/libssl/src/crypto/des/asm/y-win32.asm | 929 ++++++ src/lib/libssl/src/crypto/des/asm/yx86unix.cpp | 976 ++++++ src/lib/libssl/src/crypto/des/des.man | 186 ++ src/lib/libssl/src/crypto/des/des.org | 301 ++ src/lib/libssl/src/crypto/des/des.pl | 552 ++++ src/lib/libssl/src/crypto/des/des_crypt.man | 508 ++++ src/lib/libssl/src/crypto/des/des_locl.org | 516 ++++ src/lib/libssl/src/crypto/des/doIP | 46 + src/lib/libssl/src/crypto/des/doPC1 | 110 + src/lib/libssl/src/crypto/des/doPC2 | 94 + src/lib/libssl/src/crypto/des/ede_enc.c | 190 ++ src/lib/libssl/src/crypto/des/podd.h | 75 + src/lib/libssl/src/crypto/des/ranlib.sh | 23 + src/lib/libssl/src/crypto/des/shifts.pl | 198 ++ src/lib/libssl/src/crypto/des/sk.h | 204 ++ src/lib/libssl/src/crypto/des/supp.c | 109 + src/lib/libssl/src/crypto/des/testdes.pl | 167 + src/lib/libssl/src/crypto/des/vms.com | 90 + src/lib/libssl/src/crypto/dh/Makefile.ssl | 84 + src/lib/libssl/src/crypto/dh/dh.err | 12 + src/lib/libssl/src/crypto/dsa/Makefile.ssl | 84 + src/lib/libssl/src/crypto/dsa/dsa.err | 15 + src/lib/libssl/src/crypto/err/Makefile.ssl | 80 + src/lib/libssl/src/crypto/err/err_code.pl | 105 + src/lib/libssl/src/crypto/err/err_genc.pl | 198 ++ src/lib/libssl/src/crypto/err/error.err | 13 + src/lib/libssl/src/crypto/err/ssleay.ec | 57 + src/lib/libssl/src/crypto/evp/Makefile.ssl | 111 + src/lib/libssl/src/crypto/evp/e_cbc_3d.c | 163 + src/lib/libssl/src/crypto/evp/e_cbc_bf.c | 119 + src/lib/libssl/src/crypto/evp/e_cbc_c.c | 119 + src/lib/libssl/src/crypto/evp/e_cbc_d.c | 116 + src/lib/libssl/src/crypto/evp/e_cbc_i.c | 131 + src/lib/libssl/src/crypto/evp/e_cbc_r2.c | 136 + src/lib/libssl/src/crypto/evp/e_cbc_r5.c | 120 + src/lib/libssl/src/crypto/evp/e_cfb_3d.c | 166 + src/lib/libssl/src/crypto/evp/e_cfb_bf.c | 121 + src/lib/libssl/src/crypto/evp/e_cfb_c.c | 121 + src/lib/libssl/src/crypto/evp/e_cfb_d.c | 118 + src/lib/libssl/src/crypto/evp/e_cfb_i.c | 121 + src/lib/libssl/src/crypto/evp/e_cfb_r2.c | 122 + src/lib/libssl/src/crypto/evp/e_cfb_r5.c | 122 + src/lib/libssl/src/crypto/evp/e_ecb_3d.c | 161 + src/lib/libssl/src/crypto/evp/e_ecb_bf.c | 122 + src/lib/libssl/src/crypto/evp/e_ecb_c.c | 122 + src/lib/libssl/src/crypto/evp/e_ecb_d.c | 118 + src/lib/libssl/src/crypto/evp/e_ecb_i.c | 133 + src/lib/libssl/src/crypto/evp/e_ecb_r2.c | 123 + src/lib/libssl/src/crypto/evp/e_ecb_r5.c | 123 + src/lib/libssl/src/crypto/evp/e_ofb_3d.c | 165 + src/lib/libssl/src/crypto/evp/e_ofb_bf.c | 122 + src/lib/libssl/src/crypto/evp/e_ofb_c.c | 122 + src/lib/libssl/src/crypto/evp/e_ofb_d.c | 118 + src/lib/libssl/src/crypto/evp/e_ofb_i.c | 121 + src/lib/libssl/src/crypto/evp/e_ofb_r2.c | 123 + src/lib/libssl/src/crypto/evp/e_ofb_r5.c | 123 + src/lib/libssl/src/crypto/evp/evp.err | 24 + src/lib/libssl/src/crypto/evp/pk_lib.c | 82 + src/lib/libssl/src/crypto/hmac/Makefile.ssl | 80 + src/lib/libssl/src/crypto/idea/Makefile.ssl | 80 + src/lib/libssl/src/crypto/lhash/Makefile.ssl | 80 + src/lib/libssl/src/crypto/libvms.com | 31 + src/lib/libssl/src/crypto/md2/Makefile.ssl | 80 + src/lib/libssl/src/crypto/md2/md2.org | 106 + src/lib/libssl/src/crypto/md5/Makefile.ssl | 104 + src/lib/libssl/src/crypto/md5/Makefile.uni | 109 + src/lib/libssl/src/crypto/md5/asm/m5-win32.asm | 686 +++++ src/lib/libssl/src/crypto/md5/asm/mx86unix.cpp | 730 +++++ src/lib/libssl/src/crypto/mdc2/Makefile.ssl | 80 + src/lib/libssl/src/crypto/objects/Makefile.ssl | 87 + src/lib/libssl/src/crypto/objects/objects.err | 12 + src/lib/libssl/src/crypto/pem/Makefile.ssl | 96 + src/lib/libssl/src/crypto/pem/ctx_size.c | 122 + src/lib/libssl/src/crypto/pem/pem.err | 38 + src/lib/libssl/src/crypto/pem/pem.org | 562 ++++ src/lib/libssl/src/crypto/pkcs7/Makefile.ssl | 86 + src/lib/libssl/src/crypto/pkcs7/README | 5 + src/lib/libssl/src/crypto/pkcs7/mf.p7 | 18 + src/lib/libssl/src/crypto/pkcs7/p7.tst | 33 + src/lib/libssl/src/crypto/pkcs7/pkcs7.err | 26 + src/lib/libssl/src/crypto/rand/Makefile.ssl | 80 + src/lib/libssl/src/crypto/ranlib.sh | 23 + src/lib/libssl/src/crypto/rc2/Makefile.ssl | 80 + src/lib/libssl/src/crypto/rc2/Makefile.uni | 72 + src/lib/libssl/src/crypto/rc2/rc2.org | 118 + src/lib/libssl/src/crypto/rc4/Makefile.ssl | 108 + src/lib/libssl/src/crypto/rc4/Makefile.uni | 102 + src/lib/libssl/src/crypto/rc4/asm/r4-win32.asm | 314 ++ src/lib/libssl/src/crypto/rc4/asm/rx86unix.cpp | 358 +++ src/lib/libssl/src/crypto/rc4/rc4.org | 103 + src/lib/libssl/src/crypto/rc4/rc4_locl.org | 70 + src/lib/libssl/src/crypto/rc5/Makefile.ssl | 107 + src/lib/libssl/src/crypto/rc5/Makefile.uni | 72 + src/lib/libssl/src/crypto/rc5/asm/r5-win32.asm | 574 ++++ src/lib/libssl/src/crypto/rc5/asm/r586unix.cpp | 628 ++++ src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl | 109 + src/lib/libssl/src/crypto/ripemd/Makefile.ssl | 104 + src/lib/libssl/src/crypto/ripemd/Makefile.uni | 109 + src/lib/libssl/src/crypto/ripemd/asm/rm-win32.asm | 1972 ++++++++++++ src/lib/libssl/src/crypto/ripemd/asm/rm86unix.cpp | 2016 ++++++++++++ src/lib/libssl/src/crypto/rsa/Makefile.ssl | 86 + src/lib/libssl/src/crypto/rsa/rsa.err | 45 + src/lib/libssl/src/crypto/sha/Makefile.ssl | 103 + src/lib/libssl/src/crypto/sha/Makefile.uni | 122 + src/lib/libssl/src/crypto/sha/asm/s1-win32.asm | 1664 ++++++++++ src/lib/libssl/src/crypto/sha/asm/sx86unix.cpp | 1948 ++++++++++++ src/lib/libssl/src/crypto/sha/sha_sgst.c | 246 ++ src/lib/libssl/src/crypto/stack/Makefile.ssl | 80 + src/lib/libssl/src/crypto/txt_db/Makefile.ssl | 80 + src/lib/libssl/src/crypto/x509/Makefile.ssl | 96 + src/lib/libssl/src/crypto/x509/attrib | 38 + src/lib/libssl/src/crypto/x509/v3_net.c | 87 + src/lib/libssl/src/crypto/x509/v3_x509.c | 253 ++ src/lib/libssl/src/crypto/x509/x509.doc | 27 + src/lib/libssl/src/crypto/x509/x509.err | 46 + src/lib/libssl/src/crypto/x509/x509pack.c | 157 + src/lib/libssl/src/crypto/x509/x509v3.doc | 24 + src/lib/libssl/src/crypto/x509v3/format | 92 + src/lib/libssl/src/crypto/x509v3/header | 6 + src/lib/libssl/src/crypto/x509v3/v3_ku.c | 318 ++ src/lib/libssl/src/dep/crypto.txt | 1043 +++++++ src/lib/libssl/src/dep/files | 574 ++++ src/lib/libssl/src/dep/gen.pl | 113 + src/lib/libssl/src/dep/ssl.txt | 156 + src/lib/libssl/src/doc/API.doc | 24 + src/lib/libssl/src/doc/a_verify.doc | 85 + src/lib/libssl/src/doc/apps.doc | 53 + src/lib/libssl/src/doc/asn1.doc | 401 +++ src/lib/libssl/src/doc/bio.doc | 423 +++ src/lib/libssl/src/doc/blowfish.doc | 146 + src/lib/libssl/src/doc/bn.doc | 381 +++ src/lib/libssl/src/doc/ca.1 | 121 + src/lib/libssl/src/doc/callback.doc | 240 ++ src/lib/libssl/src/doc/cipher.doc | 345 +++ src/lib/libssl/src/doc/cipher.m | 128 + src/lib/libssl/src/doc/conf.doc | 89 + src/lib/libssl/src/doc/des.doc | 505 +++ src/lib/libssl/src/doc/digest.doc | 94 + src/lib/libssl/src/doc/encode.doc | 15 + src/lib/libssl/src/doc/envelope.doc | 67 + src/lib/libssl/src/doc/error.doc | 115 + src/lib/libssl/src/doc/legal.doc | 117 + src/lib/libssl/src/doc/lhash.doc | 151 + src/lib/libssl/src/doc/md2.doc | 49 + src/lib/libssl/src/doc/md5.doc | 50 + src/lib/libssl/src/doc/memory.doc | 27 + src/lib/libssl/src/doc/ms3-ca.doc | 398 +++ src/lib/libssl/src/doc/ns-ca.doc | 154 + src/lib/libssl/src/doc/obj.doc | 69 + src/lib/libssl/src/doc/rand.doc | 141 + src/lib/libssl/src/doc/rc2.doc | 165 + src/lib/libssl/src/doc/rc4.doc | 44 + src/lib/libssl/src/doc/readme | 6 + src/lib/libssl/src/doc/ref.doc | 48 + src/lib/libssl/src/doc/req.1 | 137 + src/lib/libssl/src/doc/rsa.doc | 135 + src/lib/libssl/src/doc/rsaref.doc | 35 + src/lib/libssl/src/doc/s_mult.doc | 17 + src/lib/libssl/src/doc/session.doc | 297 ++ src/lib/libssl/src/doc/sha.doc | 52 + src/lib/libssl/src/doc/speed.doc | 96 + src/lib/libssl/src/doc/ssl-ciph.doc | 84 + src/lib/libssl/src/doc/ssl.doc | 172 ++ src/lib/libssl/src/doc/ssl_ctx.doc | 68 + src/lib/libssl/src/doc/ssleay.doc | 213 ++ src/lib/libssl/src/doc/ssluse.doc | 45 + src/lib/libssl/src/doc/stack.doc | 96 + src/lib/libssl/src/doc/threads.doc | 90 + src/lib/libssl/src/doc/txt_db.doc | 4 + src/lib/libssl/src/doc/verify | 22 + src/lib/libssl/src/doc/why.doc | 79 + src/lib/libssl/src/makefile.one | 1781 +++++++++++ src/lib/libssl/src/ms/cipher.out | 93 + src/lib/libssl/src/ms/clear.out | 93 + src/lib/libssl/src/ms/libeay16.def | 987 ++++++ src/lib/libssl/src/ms/libeay32.def | 1035 +++++++ src/lib/libssl/src/ms/ntdll.mak | 1853 +++++++++++ src/lib/libssl/src/ms/ssleay16.def | 171 ++ src/lib/libssl/src/ms/ssleay32.def | 164 + src/lib/libssl/src/ms/w31dll.mak | 2295 ++++++++++++++ src/lib/libssl/src/mt/README | 14 + src/lib/libssl/src/mt/mttest.c | 1115 +++++++ src/lib/libssl/src/mt/profile.sh | 4 + src/lib/libssl/src/mt/ptest.bat | 4 + src/lib/libssl/src/mt/pthread.sh | 9 + src/lib/libssl/src/mt/purify.sh | 4 + src/lib/libssl/src/mt/solaris.sh | 4 + src/lib/libssl/src/mt/win32.bat | 4 + src/lib/libssl/src/perl/MANIFEST | 17 + src/lib/libssl/src/perl/Makefile.PL | 25 + src/lib/libssl/src/perl/SSLeay.pm | 78 + src/lib/libssl/src/perl/SSLeay.xs | 63 + src/lib/libssl/src/perl/b.pl | 21 + src/lib/libssl/src/perl/bio.pl | 28 + src/lib/libssl/src/perl/bio.txt | 36 + src/lib/libssl/src/perl/bio.xs | 448 +++ src/lib/libssl/src/perl/bn.pl | 23 + src/lib/libssl/src/perl/bn.txt | 38 + src/lib/libssl/src/perl/bn.xs | 589 ++++ src/lib/libssl/src/perl/callback.c | 103 + src/lib/libssl/src/perl/cipher.pl | 39 + src/lib/libssl/src/perl/cipher.txt | 10 + src/lib/libssl/src/perl/cipher.xs | 152 + src/lib/libssl/src/perl/dh.pl | 40 + src/lib/libssl/src/perl/digest.txt | 7 + src/lib/libssl/src/perl/digest.xs | 83 + src/lib/libssl/src/perl/err.txt | 2 + src/lib/libssl/src/perl/err.xs | 46 + src/lib/libssl/src/perl/f.pl | 25 + src/lib/libssl/src/perl/g.pl | 18 + src/lib/libssl/src/perl/gen_rsa.pl | 49 + src/lib/libssl/src/perl/mul.pl | 56 + src/lib/libssl/src/perl/p5SSLeay.h | 96 + src/lib/libssl/src/perl/r.pl | 56 + src/lib/libssl/src/perl/s.pl | 72 + src/lib/libssl/src/perl/s2.pl | 49 + src/lib/libssl/src/perl/server.pem | 369 +++ src/lib/libssl/src/perl/ss.pl | 64 + src/lib/libssl/src/perl/ssl.pl | 71 + src/lib/libssl/src/perl/ssl.txt | 43 + src/lib/libssl/src/perl/ssl.xs | 474 +++ src/lib/libssl/src/perl/ssl_srvr.pl | 35 + src/lib/libssl/src/perl/sslbio.pl | 40 + src/lib/libssl/src/perl/t.pl | 12 + src/lib/libssl/src/perl/test | 32 + src/lib/libssl/src/perl/test.pl | 30 + src/lib/libssl/src/perl/test.txt | 36 + src/lib/libssl/src/perl/test2.pl | 28 + src/lib/libssl/src/perl/test3.pl | 19 + src/lib/libssl/src/perl/test8.pl | 19 + src/lib/libssl/src/perl/test9.pl | 38 + src/lib/libssl/src/perl/testbn.pl | 23 + src/lib/libssl/src/perl/testdec.pl | 14 + src/lib/libssl/src/perl/testmd.pl | 26 + src/lib/libssl/src/perl/tt.pl | 15 + src/lib/libssl/src/perl/typemap | 96 + src/lib/libssl/src/perl/x509.txt | 6 + src/lib/libssl/src/perl/x509.xs | 74 + src/lib/libssl/src/perl/xstmp.c | 102 + src/lib/libssl/src/perl/y.pl | 7 + src/lib/libssl/src/perl/yy.pl | 19 + src/lib/libssl/src/perl/z.pl | 32 + src/lib/libssl/src/perl/zz.pl | 22 + src/lib/libssl/src/shlib/linux.sh | 76 + src/lib/libssl/src/ssl/Makefile.ssl | 100 + src/lib/libssl/src/ssl/readme | 277 ++ src/lib/libssl/src/ssl/ssl.c | 172 ++ src/lib/libssl/src/ssl/ssl.err | 290 ++ src/lib/libssl/src/test/.rnd | Bin 0 -> 1024 bytes src/lib/libssl/src/test/Makefile.ssl | 294 ++ src/lib/libssl/src/test/certCA.srl | 1 + src/lib/libssl/src/test/demoCA/index.txt | 0 src/lib/libssl/src/test/demoCA/private/cakey.pem | 2 + src/lib/libssl/src/test/demoCA/serial | 1 + src/lib/libssl/src/test/dsa-ca.pem | 43 + src/lib/libssl/src/test/dsa-pca.pem | 49 + src/lib/libssl/src/test/p | 294 ++ src/lib/libssl/src/test/riptest | Bin 0 -> 13325 bytes src/lib/libssl/src/test/test.txt | 31 + src/lib/libssl/src/test/testkey.pem | 2 + src/lib/libssl/src/test/testreq.pem | 9 + src/lib/libssl/src/tools/Makefile.ssl | 54 + src/lib/libssl/src/util/mklink.sh | 35 + src/lib/libssl/src/util/ranlib.sh | 23 + src/lib/libssl/src/util/sep_lib.sh | 37 + src/lib/libssl/src/util/ssldir.pl | 52 + src/lib/libssl/src/util/up_ver.pl | 79 + src/lib/libssl/ssl-patent/Makefile | 90 + src/lib/libssl/ssl-patent/shlib_version | 2 + src/lib/libssl/ssl/Makefile | 90 + src/lib/libssl/ssl/shlib_version | 2 + src/lib/libssl/ssleay.cnf | 65 + src/lib/libssl/test/.rnd | Bin 0 -> 1024 bytes src/lib/libssl/test/Makefile.ssl | 294 ++ src/lib/libssl/test/certCA.srl | 1 + src/lib/libssl/test/demoCA/index.txt | 0 src/lib/libssl/test/demoCA/private/cakey.pem | 2 + src/lib/libssl/test/demoCA/serial | 1 + src/lib/libssl/test/dsa-ca.pem | 43 + src/lib/libssl/test/dsa-pca.pem | 49 + src/lib/libssl/test/p | 294 ++ src/lib/libssl/test/riptest | Bin 0 -> 13325 bytes src/lib/libssl/test/test.txt | 31 + src/lib/libssl/test/testkey.pem | 2 + src/lib/libssl/test/testreq.pem | 9 + src/regress/lib/libc/Makefile | 16 + src/regress/lib/libc/_setjmp/Makefile | 16 + src/regress/lib/libc/arch/alpha/Makefile | 10 + .../lib/libc/arch/alpha/divremtest/Makefile | 29 + .../lib/libc/arch/alpha/divremtest/divremtest.c | 183 ++ .../lib/libc/arch/alpha/divremtest/mkcases.c | 63 + .../lib/libc/arch/alpha/divremtest/mktestcases.c | 67 + src/regress/lib/libc/db/Makefile | 17 + src/regress/lib/libc/db/README | 68 + src/regress/lib/libc/db/dbtest.c | 759 +++++ src/regress/lib/libc/db/run.test | 706 +++++ src/regress/lib/libc/ieeefp/Makefile | 9 + src/regress/lib/libc/ieeefp/except/Makefile | 12 + src/regress/lib/libc/ieeefp/except/except.c | 88 + src/regress/lib/libc/ieeefp/round/Makefile | 12 + src/regress/lib/libc/ieeefp/round/round.c | 44 + src/regress/lib/libc/regex/Makefile | 16 + src/regress/lib/libc/regex/debug.c | 244 ++ src/regress/lib/libc/regex/debug.ih | 16 + src/regress/lib/libc/regex/main.c | 515 ++++ src/regress/lib/libc/regex/main.ih | 21 + src/regress/lib/libc/regex/split.c | 318 ++ src/regress/lib/libc/regex/tests | 477 +++ src/regress/lib/libc/setjmp/Makefile | 16 + src/regress/lib/libc/setjmp/jmptest.c | 135 + src/regress/lib/libc/sigsetjmp/Makefile | 17 + src/regress/lib/libssl/Makefile | 11 + src/regress/lib/libssl/README | 8 + src/regress/lib/libssl/ssleay.cnf | 27 + src/regress/lib/libssl/test_client.sh | 11 + src/regress/lib/libssl/test_server.sh | 9 + src/regress/lib/libssl/testdsa.sh | 27 + src/regress/lib/libssl/testenc.sh | 63 + src/regress/lib/libssl/testrsa.sh | 36 + src/usr.bin/nc/Makefile | 7 + src/usr.bin/nc/README | 946 ++++++ src/usr.bin/nc/data/Makefile | 10 + src/usr.bin/nc/data/README | 9 + src/usr.bin/nc/data/data.c | 274 ++ src/usr.bin/nc/data/dns-any.d | 36 + src/usr.bin/nc/data/nfs-0.d | 59 + src/usr.bin/nc/data/pm.d | 8 + src/usr.bin/nc/data/pmap-dump.d | 60 + src/usr.bin/nc/data/pmap-mnt.d | 78 + src/usr.bin/nc/data/rip.d | 52 + src/usr.bin/nc/data/rservice.c | 68 + src/usr.bin/nc/data/showmount.d | 63 + src/usr.bin/nc/data/xor.c | 92 + src/usr.bin/nc/generic.h | 377 +++ src/usr.bin/nc/nc.1 | 215 ++ src/usr.bin/nc/netcat.blurb | 61 + src/usr.bin/nc/netcat.c | 1670 ++++++++++ src/usr.bin/nc/scripts/README | 5 + src/usr.bin/nc/scripts/alta | 33 + src/usr.bin/nc/scripts/bsh | 29 + src/usr.bin/nc/scripts/dist.sh | 23 + src/usr.bin/nc/scripts/irc | 79 + src/usr.bin/nc/scripts/iscan | 35 + src/usr.bin/nc/scripts/ncp | 46 + src/usr.bin/nc/scripts/probe | 50 + src/usr.bin/nc/scripts/web | 148 + src/usr.bin/nc/scripts/webproxy | 138 + src/usr.bin/nc/scripts/webrelay | 44 + src/usr.bin/nc/scripts/websearch | 77 + 901 files changed, 169096 insertions(+) create mode 100644 src/lib/libc/crypt/Makefile.inc create mode 100644 src/lib/libc/crypt/arc4random.3 create mode 100644 src/lib/libc/crypt/arc4random.c create mode 100644 src/lib/libc/crypt/bcrypt.c create mode 100644 src/lib/libc/crypt/blowfish.3 create mode 100644 src/lib/libc/crypt/blowfish.c create mode 100644 src/lib/libc/crypt/cast.c create mode 100644 src/lib/libc/crypt/crypt.3 create mode 100644 src/lib/libc/crypt/crypt.c create mode 100644 src/lib/libc/crypt/md5crypt.c create mode 100644 src/lib/libc/crypt/morecrypt.c create mode 100644 src/lib/libc/crypt/skipjack.c create mode 100644 src/lib/libc/include/namespace.h create mode 100644 src/lib/libc/include/thread_private.h create mode 100644 src/lib/libc/net/Makefile.inc create mode 100644 src/lib/libc/net/base64.c create mode 100644 src/lib/libc/net/byteorder.3 create mode 100644 src/lib/libc/net/ethers.3 create mode 100644 src/lib/libc/net/ethers.c create mode 100644 src/lib/libc/net/gethostbyname.3 create mode 100644 src/lib/libc/net/gethostnamadr.c create mode 100644 src/lib/libc/net/getnetbyaddr.c create mode 100644 src/lib/libc/net/getnetbyname.c create mode 100644 src/lib/libc/net/getnetent.3 create mode 100644 src/lib/libc/net/getnetent.c create mode 100644 src/lib/libc/net/getnetnamadr.c create mode 100644 src/lib/libc/net/getproto.c create mode 100644 src/lib/libc/net/getprotoent.3 create mode 100644 src/lib/libc/net/getprotoent.c create mode 100644 src/lib/libc/net/getprotoname.c create mode 100644 src/lib/libc/net/getservbyname.c create mode 100644 src/lib/libc/net/getservbyport.c create mode 100644 src/lib/libc/net/getservent.3 create mode 100644 src/lib/libc/net/getservent.c create mode 100644 src/lib/libc/net/herror.c create mode 100644 src/lib/libc/net/htonl.c create mode 100644 src/lib/libc/net/htons.c create mode 100644 src/lib/libc/net/inet.3 create mode 100644 src/lib/libc/net/inet_addr.c create mode 100644 src/lib/libc/net/inet_lnaof.c create mode 100644 src/lib/libc/net/inet_makeaddr.c create mode 100644 src/lib/libc/net/inet_net.3 create mode 100644 src/lib/libc/net/inet_net_ntop.c create mode 100644 src/lib/libc/net/inet_net_pton.c create mode 100644 src/lib/libc/net/inet_neta.c create mode 100644 src/lib/libc/net/inet_netof.c create mode 100644 src/lib/libc/net/inet_network.c create mode 100644 src/lib/libc/net/inet_ntoa.c create mode 100644 src/lib/libc/net/inet_ntop.c create mode 100644 src/lib/libc/net/inet_pton.c create mode 100644 src/lib/libc/net/ipx.3 create mode 100644 src/lib/libc/net/ipx_addr.c create mode 100644 src/lib/libc/net/ipx_ntoa.c create mode 100644 src/lib/libc/net/iso_addr.3 create mode 100644 src/lib/libc/net/iso_addr.c create mode 100644 src/lib/libc/net/link_addr.3 create mode 100644 src/lib/libc/net/linkaddr.c create mode 100644 src/lib/libc/net/ns.3 create mode 100644 src/lib/libc/net/ns_addr.c create mode 100644 src/lib/libc/net/ns_ntoa.c create mode 100644 src/lib/libc/net/nsap_addr.c create mode 100644 src/lib/libc/net/ntohl.c create mode 100644 src/lib/libc/net/ntohs.c create mode 100644 src/lib/libc/net/rcmd.3 create mode 100644 src/lib/libc/net/rcmd.c create mode 100644 src/lib/libc/net/rcmdsh.3 create mode 100644 src/lib/libc/net/rcmdsh.c create mode 100644 src/lib/libc/net/recv.c create mode 100644 src/lib/libc/net/res_comp.c create mode 100644 src/lib/libc/net/res_data.c create mode 100644 src/lib/libc/net/res_debug.c create mode 100644 src/lib/libc/net/res_init.c create mode 100644 src/lib/libc/net/res_mkquery.c create mode 100644 src/lib/libc/net/res_query.c create mode 100644 src/lib/libc/net/res_random.c create mode 100644 src/lib/libc/net/res_send.c create mode 100644 src/lib/libc/net/resolver.3 create mode 100644 src/lib/libc/net/send.c create mode 100644 src/lib/libc/net/sethostent.c create mode 100644 src/lib/libc/stdlib/Makefile.inc create mode 100644 src/lib/libc/stdlib/_rand48.c create mode 100644 src/lib/libc/stdlib/a64l.3 create mode 100644 src/lib/libc/stdlib/a64l.c create mode 100644 src/lib/libc/stdlib/abort.3 create mode 100644 src/lib/libc/stdlib/abort.c create mode 100644 src/lib/libc/stdlib/abs.3 create mode 100644 src/lib/libc/stdlib/abs.c create mode 100644 src/lib/libc/stdlib/alloca.3 create mode 100644 src/lib/libc/stdlib/atexit.3 create mode 100644 src/lib/libc/stdlib/atexit.c create mode 100644 src/lib/libc/stdlib/atexit.h create mode 100644 src/lib/libc/stdlib/atof.3 create mode 100644 src/lib/libc/stdlib/atof.c create mode 100644 src/lib/libc/stdlib/atoi.3 create mode 100644 src/lib/libc/stdlib/atoi.c create mode 100644 src/lib/libc/stdlib/atol.3 create mode 100644 src/lib/libc/stdlib/atol.c create mode 100644 src/lib/libc/stdlib/bsearch.3 create mode 100644 src/lib/libc/stdlib/bsearch.c create mode 100644 src/lib/libc/stdlib/calloc.3 create mode 100644 src/lib/libc/stdlib/calloc.c create mode 100644 src/lib/libc/stdlib/cfree.c create mode 100644 src/lib/libc/stdlib/div.3 create mode 100644 src/lib/libc/stdlib/div.c create mode 100644 src/lib/libc/stdlib/drand48.c create mode 100644 src/lib/libc/stdlib/erand48.c create mode 100644 src/lib/libc/stdlib/exit.3 create mode 100644 src/lib/libc/stdlib/exit.c create mode 100644 src/lib/libc/stdlib/getenv.3 create mode 100644 src/lib/libc/stdlib/getenv.c create mode 100644 src/lib/libc/stdlib/getopt.3 create mode 100644 src/lib/libc/stdlib/getopt.c create mode 100644 src/lib/libc/stdlib/getsubopt.3 create mode 100644 src/lib/libc/stdlib/getsubopt.c create mode 100644 src/lib/libc/stdlib/heapsort.c create mode 100644 src/lib/libc/stdlib/jrand48.c create mode 100644 src/lib/libc/stdlib/l64a.c create mode 100644 src/lib/libc/stdlib/labs.3 create mode 100644 src/lib/libc/stdlib/labs.c create mode 100644 src/lib/libc/stdlib/lcong48.c create mode 100644 src/lib/libc/stdlib/ldiv.3 create mode 100644 src/lib/libc/stdlib/ldiv.c create mode 100644 src/lib/libc/stdlib/lrand48.c create mode 100644 src/lib/libc/stdlib/malloc.3 create mode 100644 src/lib/libc/stdlib/malloc.c create mode 100644 src/lib/libc/stdlib/memory.3 create mode 100644 src/lib/libc/stdlib/merge.c create mode 100644 src/lib/libc/stdlib/mrand48.c create mode 100644 src/lib/libc/stdlib/multibyte.c create mode 100644 src/lib/libc/stdlib/nrand48.c create mode 100644 src/lib/libc/stdlib/putenv.c create mode 100644 src/lib/libc/stdlib/qabs.3 create mode 100644 src/lib/libc/stdlib/qabs.c create mode 100644 src/lib/libc/stdlib/qdiv.3 create mode 100644 src/lib/libc/stdlib/qdiv.c create mode 100644 src/lib/libc/stdlib/qsort.3 create mode 100644 src/lib/libc/stdlib/qsort.c create mode 100644 src/lib/libc/stdlib/radixsort.3 create mode 100644 src/lib/libc/stdlib/radixsort.c create mode 100644 src/lib/libc/stdlib/rand.3 create mode 100644 src/lib/libc/stdlib/rand.c create mode 100644 src/lib/libc/stdlib/rand48.3 create mode 100644 src/lib/libc/stdlib/rand48.h create mode 100644 src/lib/libc/stdlib/random.3 create mode 100644 src/lib/libc/stdlib/random.c create mode 100644 src/lib/libc/stdlib/realpath.3 create mode 100644 src/lib/libc/stdlib/realpath.c create mode 100644 src/lib/libc/stdlib/seed48.c create mode 100644 src/lib/libc/stdlib/setenv.c create mode 100644 src/lib/libc/stdlib/srand48.c create mode 100644 src/lib/libc/stdlib/strtod.3 create mode 100644 src/lib/libc/stdlib/strtod.c create mode 100644 src/lib/libc/stdlib/strtol.3 create mode 100644 src/lib/libc/stdlib/strtol.c create mode 100644 src/lib/libc/stdlib/strtoq.c create mode 100644 src/lib/libc/stdlib/strtoul.3 create mode 100644 src/lib/libc/stdlib/strtoul.c create mode 100644 src/lib/libc/stdlib/strtouq.c create mode 100644 src/lib/libc/stdlib/system.3 create mode 100644 src/lib/libc/stdlib/system.c create mode 100644 src/lib/libc/stdlib/tfind.c create mode 100644 src/lib/libc/stdlib/tsearch.3 create mode 100644 src/lib/libc/stdlib/tsearch.c create mode 100644 src/lib/libc/string/Makefile.inc create mode 100644 src/lib/libc/string/__strerror.c create mode 100644 src/lib/libc/string/__strsignal.c create mode 100644 src/lib/libc/string/bcmp.3 create mode 100644 src/lib/libc/string/bcmp.c create mode 100644 src/lib/libc/string/bcopy.3 create mode 100644 src/lib/libc/string/bcopy.c create mode 100644 src/lib/libc/string/bm.3 create mode 100644 src/lib/libc/string/bm.c create mode 100644 src/lib/libc/string/bstring.3 create mode 100644 src/lib/libc/string/bzero.3 create mode 100644 src/lib/libc/string/bzero.c create mode 100644 src/lib/libc/string/ffs.3 create mode 100644 src/lib/libc/string/ffs.c create mode 100644 src/lib/libc/string/index.3 create mode 100644 src/lib/libc/string/index.c create mode 100644 src/lib/libc/string/memccpy.3 create mode 100644 src/lib/libc/string/memccpy.c create mode 100644 src/lib/libc/string/memchr.3 create mode 100644 src/lib/libc/string/memchr.c create mode 100644 src/lib/libc/string/memcmp.3 create mode 100644 src/lib/libc/string/memcmp.c create mode 100644 src/lib/libc/string/memcpy.3 create mode 100644 src/lib/libc/string/memmove.3 create mode 100644 src/lib/libc/string/memset.3 create mode 100644 src/lib/libc/string/memset.c create mode 100644 src/lib/libc/string/rindex.3 create mode 100644 src/lib/libc/string/rindex.c create mode 100644 src/lib/libc/string/strcasecmp.3 create mode 100644 src/lib/libc/string/strcasecmp.c create mode 100644 src/lib/libc/string/strcat.3 create mode 100644 src/lib/libc/string/strcat.c create mode 100644 src/lib/libc/string/strchr.3 create mode 100644 src/lib/libc/string/strcmp.3 create mode 100644 src/lib/libc/string/strcmp.c create mode 100644 src/lib/libc/string/strcoll.3 create mode 100644 src/lib/libc/string/strcoll.c create mode 100644 src/lib/libc/string/strcpy.3 create mode 100644 src/lib/libc/string/strcpy.c create mode 100644 src/lib/libc/string/strcspn.3 create mode 100644 src/lib/libc/string/strcspn.c create mode 100644 src/lib/libc/string/strdup.3 create mode 100644 src/lib/libc/string/strdup.c create mode 100644 src/lib/libc/string/strerror.3 create mode 100644 src/lib/libc/string/strerror.c create mode 100644 src/lib/libc/string/string.3 create mode 100644 src/lib/libc/string/strlcat.c create mode 100644 src/lib/libc/string/strlcpy.3 create mode 100644 src/lib/libc/string/strlcpy.c create mode 100644 src/lib/libc/string/strlen.3 create mode 100644 src/lib/libc/string/strlen.c create mode 100644 src/lib/libc/string/strmode.3 create mode 100644 src/lib/libc/string/strmode.c create mode 100644 src/lib/libc/string/strncat.c create mode 100644 src/lib/libc/string/strncmp.c create mode 100644 src/lib/libc/string/strncpy.c create mode 100644 src/lib/libc/string/strpbrk.3 create mode 100644 src/lib/libc/string/strpbrk.c create mode 100644 src/lib/libc/string/strrchr.3 create mode 100644 src/lib/libc/string/strsep.3 create mode 100644 src/lib/libc/string/strsep.c create mode 100644 src/lib/libc/string/strsignal.3 create mode 100644 src/lib/libc/string/strsignal.c create mode 100644 src/lib/libc/string/strspn.3 create mode 100644 src/lib/libc/string/strspn.c create mode 100644 src/lib/libc/string/strstr.3 create mode 100644 src/lib/libc/string/strstr.c create mode 100644 src/lib/libc/string/strtok.3 create mode 100644 src/lib/libc/string/strtok.c create mode 100644 src/lib/libc/string/strxfrm.3 create mode 100644 src/lib/libc/string/strxfrm.c create mode 100644 src/lib/libc/string/swab.3 create mode 100644 src/lib/libc/string/swab.c create mode 100644 src/lib/libcrypto/Makefile.ssl create mode 100644 src/lib/libcrypto/asn1/Makefile.ssl create mode 100644 src/lib/libcrypto/asn1/a_bmp.c create mode 100644 src/lib/libcrypto/asn1/asn1.err create mode 100644 src/lib/libcrypto/asn1/d2i_dhp.c create mode 100644 src/lib/libcrypto/asn1/d2i_dsap.c create mode 100644 src/lib/libcrypto/asn1/d2i_r_pr.c create mode 100644 src/lib/libcrypto/asn1/d2i_r_pu.c create mode 100644 src/lib/libcrypto/asn1/d2i_s_pr.c create mode 100644 src/lib/libcrypto/asn1/d2i_s_pu.c create mode 100644 src/lib/libcrypto/asn1/f.c create mode 100644 src/lib/libcrypto/asn1/i2d_dhp.c create mode 100644 src/lib/libcrypto/asn1/i2d_dsap.c create mode 100644 src/lib/libcrypto/asn1/i2d_r_pr.c create mode 100644 src/lib/libcrypto/asn1/i2d_r_pu.c create mode 100644 src/lib/libcrypto/asn1/i2d_s_pr.c create mode 100644 src/lib/libcrypto/asn1/i2d_s_pu.c create mode 100644 src/lib/libcrypto/asn1/p7_dgst.c create mode 100644 src/lib/libcrypto/asn1/p7_enc.c create mode 100644 src/lib/libcrypto/asn1/p7_enc_c.c create mode 100644 src/lib/libcrypto/asn1/p7_evp.c create mode 100644 src/lib/libcrypto/asn1/p7_i_s.c create mode 100644 src/lib/libcrypto/asn1/p7_lib.c create mode 100644 src/lib/libcrypto/asn1/p7_recip.c create mode 100644 src/lib/libcrypto/asn1/p7_s_e.c create mode 100644 src/lib/libcrypto/asn1/p7_signd.c create mode 100644 src/lib/libcrypto/asn1/p7_signi.c create mode 100644 src/lib/libcrypto/asn1/pkcs8.c create mode 100644 src/lib/libcrypto/asn1/x_cinf.c create mode 100644 src/lib/libcrypto/bf/Makefile.ssl create mode 100644 src/lib/libcrypto/bf/Makefile.uni create mode 100644 src/lib/libcrypto/bf/asm/b-win32.asm create mode 100644 src/lib/libcrypto/bf/asm/bx86unix.cpp create mode 100644 src/lib/libcrypto/bf/bf_locl.org create mode 100644 src/lib/libcrypto/bio/Makefile.ssl create mode 100644 src/lib/libcrypto/bio/bio.err create mode 100644 src/lib/libcrypto/bn/Makefile.ssl create mode 100644 src/lib/libcrypto/bn/asm/bn-win32.asm create mode 100644 src/lib/libcrypto/bn/asm/bn86unix.cpp create mode 100644 src/lib/libcrypto/bn/asm/sparc.s create mode 100644 src/lib/libcrypto/bn/asm/x86w16.asm create mode 100644 src/lib/libcrypto/bn/asm/x86w32.asm create mode 100644 src/lib/libcrypto/bn/bn.err create mode 100644 src/lib/libcrypto/bn/bn.org create mode 100644 src/lib/libcrypto/bn/bn_m.c create mode 100644 src/lib/libcrypto/bn/bn_mulw.c create mode 100644 src/lib/libcrypto/bn/bn_sub.c create mode 100644 src/lib/libcrypto/buffer/Makefile.ssl create mode 100644 src/lib/libcrypto/buffer/buffer.err create mode 100644 src/lib/libcrypto/cast/Makefile.ssl create mode 100644 src/lib/libcrypto/cast/Makefile.uni create mode 100644 src/lib/libcrypto/cast/asm/c-win32.asm create mode 100644 src/lib/libcrypto/cast/asm/cx86unix.cpp create mode 100644 src/lib/libcrypto/conf/Makefile.ssl create mode 100644 src/lib/libcrypto/conf/conf.c create mode 100644 src/lib/libcrypto/conf/conf.err create mode 100644 src/lib/libcrypto/conf/conf_lcl.h create mode 100644 src/lib/libcrypto/cryptall.h create mode 100644 src/lib/libcrypto/crypto.c create mode 100644 src/lib/libcrypto/crypto.err create mode 100644 src/lib/libcrypto/date.h create mode 100644 src/lib/libcrypto/des/DES.pod create mode 100644 src/lib/libcrypto/des/FILES create mode 100644 src/lib/libcrypto/des/MODES.DES create mode 100644 src/lib/libcrypto/des/Makefile.PL create mode 100644 src/lib/libcrypto/des/Makefile.lit create mode 100644 src/lib/libcrypto/des/Makefile.ssl create mode 100644 src/lib/libcrypto/des/Makefile.uni create mode 100644 src/lib/libcrypto/des/PC1 create mode 100644 src/lib/libcrypto/des/PC2 create mode 100644 src/lib/libcrypto/des/asm/d-win32.asm create mode 100644 src/lib/libcrypto/des/asm/dx86unix.cpp create mode 100644 src/lib/libcrypto/des/asm/y-win32.asm create mode 100644 src/lib/libcrypto/des/asm/yx86unix.cpp create mode 100644 src/lib/libcrypto/des/des.man create mode 100644 src/lib/libcrypto/des/des.org create mode 100644 src/lib/libcrypto/des/des.pl create mode 100644 src/lib/libcrypto/des/des_crypt.man create mode 100644 src/lib/libcrypto/des/des_locl.org create mode 100644 src/lib/libcrypto/des/doIP create mode 100644 src/lib/libcrypto/des/doPC1 create mode 100644 src/lib/libcrypto/des/doPC2 create mode 100644 src/lib/libcrypto/des/ede_enc.c create mode 100644 src/lib/libcrypto/des/podd.h create mode 100644 src/lib/libcrypto/des/ranlib.sh create mode 100644 src/lib/libcrypto/des/shifts.pl create mode 100644 src/lib/libcrypto/des/sk.h create mode 100644 src/lib/libcrypto/des/supp.c create mode 100644 src/lib/libcrypto/des/testdes.pl create mode 100644 src/lib/libcrypto/des/vms.com create mode 100644 src/lib/libcrypto/dh/Makefile.ssl create mode 100644 src/lib/libcrypto/dh/dh.err create mode 100644 src/lib/libcrypto/dsa/Makefile.ssl create mode 100644 src/lib/libcrypto/dsa/dsa.err create mode 100644 src/lib/libcrypto/err/Makefile.ssl create mode 100644 src/lib/libcrypto/err/err_code.pl create mode 100644 src/lib/libcrypto/err/err_genc.pl create mode 100644 src/lib/libcrypto/err/error.err create mode 100644 src/lib/libcrypto/err/ssleay.ec create mode 100644 src/lib/libcrypto/evp/Makefile.ssl create mode 100644 src/lib/libcrypto/evp/e_cbc_3d.c create mode 100644 src/lib/libcrypto/evp/e_cbc_bf.c create mode 100644 src/lib/libcrypto/evp/e_cbc_c.c create mode 100644 src/lib/libcrypto/evp/e_cbc_d.c create mode 100644 src/lib/libcrypto/evp/e_cbc_i.c create mode 100644 src/lib/libcrypto/evp/e_cbc_r2.c create mode 100644 src/lib/libcrypto/evp/e_cbc_r5.c create mode 100644 src/lib/libcrypto/evp/e_cfb_3d.c create mode 100644 src/lib/libcrypto/evp/e_cfb_bf.c create mode 100644 src/lib/libcrypto/evp/e_cfb_c.c create mode 100644 src/lib/libcrypto/evp/e_cfb_d.c create mode 100644 src/lib/libcrypto/evp/e_cfb_i.c create mode 100644 src/lib/libcrypto/evp/e_cfb_r2.c create mode 100644 src/lib/libcrypto/evp/e_cfb_r5.c create mode 100644 src/lib/libcrypto/evp/e_ecb_3d.c create mode 100644 src/lib/libcrypto/evp/e_ecb_bf.c create mode 100644 src/lib/libcrypto/evp/e_ecb_c.c create mode 100644 src/lib/libcrypto/evp/e_ecb_d.c create mode 100644 src/lib/libcrypto/evp/e_ecb_i.c create mode 100644 src/lib/libcrypto/evp/e_ecb_r2.c create mode 100644 src/lib/libcrypto/evp/e_ecb_r5.c create mode 100644 src/lib/libcrypto/evp/e_ofb_3d.c create mode 100644 src/lib/libcrypto/evp/e_ofb_bf.c create mode 100644 src/lib/libcrypto/evp/e_ofb_c.c create mode 100644 src/lib/libcrypto/evp/e_ofb_d.c create mode 100644 src/lib/libcrypto/evp/e_ofb_i.c create mode 100644 src/lib/libcrypto/evp/e_ofb_r2.c create mode 100644 src/lib/libcrypto/evp/e_ofb_r5.c create mode 100644 src/lib/libcrypto/evp/evp.err create mode 100644 src/lib/libcrypto/evp/pk_lib.c create mode 100644 src/lib/libcrypto/hmac/Makefile.ssl create mode 100644 src/lib/libcrypto/idea/Makefile.ssl create mode 100644 src/lib/libcrypto/lhash/Makefile.ssl create mode 100644 src/lib/libcrypto/libvms.com create mode 100644 src/lib/libcrypto/md2/Makefile.ssl create mode 100644 src/lib/libcrypto/md2/md2.org create mode 100644 src/lib/libcrypto/md5/Makefile.ssl create mode 100644 src/lib/libcrypto/md5/Makefile.uni create mode 100644 src/lib/libcrypto/md5/asm/m5-win32.asm create mode 100644 src/lib/libcrypto/md5/asm/mx86unix.cpp create mode 100644 src/lib/libcrypto/mdc2/Makefile.ssl create mode 100644 src/lib/libcrypto/objects/Makefile.ssl create mode 100644 src/lib/libcrypto/objects/objects.err create mode 100644 src/lib/libcrypto/pem/Makefile.ssl create mode 100644 src/lib/libcrypto/pem/ctx_size.c create mode 100644 src/lib/libcrypto/pem/pem.err create mode 100644 src/lib/libcrypto/pem/pem.org create mode 100644 src/lib/libcrypto/pkcs7/Makefile.ssl create mode 100644 src/lib/libcrypto/pkcs7/README create mode 100644 src/lib/libcrypto/pkcs7/mf.p7 create mode 100644 src/lib/libcrypto/pkcs7/p7.tst create mode 100644 src/lib/libcrypto/pkcs7/pkcs7.err create mode 100644 src/lib/libcrypto/rand/Makefile.ssl create mode 100644 src/lib/libcrypto/ranlib.sh create mode 100644 src/lib/libcrypto/rc2/Makefile.ssl create mode 100644 src/lib/libcrypto/rc2/Makefile.uni create mode 100644 src/lib/libcrypto/rc2/rc2.org create mode 100644 src/lib/libcrypto/rc4/Makefile.ssl create mode 100644 src/lib/libcrypto/rc4/Makefile.uni create mode 100644 src/lib/libcrypto/rc4/asm/r4-win32.asm create mode 100644 src/lib/libcrypto/rc4/asm/rx86unix.cpp create mode 100644 src/lib/libcrypto/rc4/rc4.org create mode 100644 src/lib/libcrypto/rc4/rc4_locl.org create mode 100644 src/lib/libcrypto/rc5/Makefile.ssl create mode 100644 src/lib/libcrypto/rc5/Makefile.uni create mode 100644 src/lib/libcrypto/rc5/asm/r5-win32.asm create mode 100644 src/lib/libcrypto/rc5/asm/r586unix.cpp create mode 100644 src/lib/libcrypto/rc5/asm/rc5-586.pl create mode 100644 src/lib/libcrypto/ripemd/Makefile.ssl create mode 100644 src/lib/libcrypto/ripemd/Makefile.uni create mode 100644 src/lib/libcrypto/ripemd/asm/rm-win32.asm create mode 100644 src/lib/libcrypto/ripemd/asm/rm86unix.cpp create mode 100644 src/lib/libcrypto/rsa/Makefile.ssl create mode 100644 src/lib/libcrypto/rsa/rsa.err create mode 100644 src/lib/libcrypto/sha/Makefile.ssl create mode 100644 src/lib/libcrypto/sha/Makefile.uni create mode 100644 src/lib/libcrypto/sha/asm/s1-win32.asm create mode 100644 src/lib/libcrypto/sha/asm/sx86unix.cpp create mode 100644 src/lib/libcrypto/sha/sha_sgst.c create mode 100644 src/lib/libcrypto/stack/Makefile.ssl create mode 100644 src/lib/libcrypto/txt_db/Makefile.ssl create mode 100644 src/lib/libcrypto/util/mklink.sh create mode 100644 src/lib/libcrypto/util/ranlib.sh create mode 100644 src/lib/libcrypto/util/sep_lib.sh create mode 100644 src/lib/libcrypto/util/ssldir.pl create mode 100644 src/lib/libcrypto/util/up_ver.pl create mode 100644 src/lib/libcrypto/x509/Makefile.ssl create mode 100644 src/lib/libcrypto/x509/attrib create mode 100644 src/lib/libcrypto/x509/v3_net.c create mode 100644 src/lib/libcrypto/x509/v3_x509.c create mode 100644 src/lib/libcrypto/x509/x509.doc create mode 100644 src/lib/libcrypto/x509/x509.err create mode 100644 src/lib/libcrypto/x509/x509pack.c create mode 100644 src/lib/libcrypto/x509/x509v3.doc create mode 100644 src/lib/libcrypto/x509v3/format create mode 100644 src/lib/libcrypto/x509v3/header create mode 100644 src/lib/libcrypto/x509v3/v3_ku.c create mode 100644 src/lib/libssl/Makefile create mode 100644 src/lib/libssl/Makefile.bsd-wrapper create mode 100644 src/lib/libssl/README.OPENBSD create mode 100644 src/lib/libssl/crypto-patent/Makefile create mode 100644 src/lib/libssl/crypto-patent/shlib_version create mode 100644 src/lib/libssl/crypto/Makefile create mode 100644 src/lib/libssl/crypto/shlib_version create mode 100644 src/lib/libssl/src/COPYRIGHT create mode 100644 src/lib/libssl/src/HISTORY create mode 100644 src/lib/libssl/src/HISTORY.066 create mode 100644 src/lib/libssl/src/MICROSOFT create mode 100644 src/lib/libssl/src/MINFO create mode 100644 src/lib/libssl/src/Makefile.ssl create mode 100644 src/lib/libssl/src/PATENTS create mode 100644 src/lib/libssl/src/README.066 create mode 100644 src/lib/libssl/src/README.080 create mode 100644 src/lib/libssl/src/README.090 create mode 100644 src/lib/libssl/src/TODO create mode 100644 src/lib/libssl/src/VERSION create mode 100644 src/lib/libssl/src/apps/Makefile.ssl create mode 100644 src/lib/libssl/src/apps/der_chop create mode 100644 src/lib/libssl/src/apps/eay.c create mode 100644 src/lib/libssl/src/apps/ext.v3 create mode 100644 src/lib/libssl/src/apps/g_ssleay.pl create mode 100644 src/lib/libssl/src/apps/mklinks create mode 100644 src/lib/libssl/src/apps/pem_mail.c create mode 100644 src/lib/libssl/src/apps/rmlinks create mode 100644 src/lib/libssl/src/apps/ssleay.c create mode 100644 src/lib/libssl/src/apps/ssleay.cnf create mode 100644 src/lib/libssl/src/apps/tkca create mode 100644 src/lib/libssl/src/certs/ICE-CA.pem create mode 100644 src/lib/libssl/src/certs/ICE-root.pem create mode 100644 src/lib/libssl/src/certs/ICE-user.pem create mode 100644 src/lib/libssl/src/certs/ICE.crl create mode 100644 src/lib/libssl/src/certs/ca-cert.pem create mode 100644 src/lib/libssl/src/certs/dsa-ca.pem create mode 100644 src/lib/libssl/src/certs/dsa-pca.pem create mode 100644 src/lib/libssl/src/certs/factory.pem create mode 100644 src/lib/libssl/src/certs/nortelCA.pem create mode 100644 src/lib/libssl/src/certs/pca-cert.pem create mode 100644 src/lib/libssl/src/certs/rsa-cca.pem create mode 100644 src/lib/libssl/src/certs/rsa-ssca.pem create mode 100644 src/lib/libssl/src/certs/timCA.pem create mode 100644 src/lib/libssl/src/certs/tjhCA.pem create mode 100644 src/lib/libssl/src/certs/vsign2.pem create mode 100644 src/lib/libssl/src/certs/vsign4.pem create mode 100644 src/lib/libssl/src/crypto/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/asn1/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/asn1/a_bmp.c create mode 100644 src/lib/libssl/src/crypto/asn1/asn1.err create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_dhp.c create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_dsap.c create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_r_pr.c create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_r_pu.c create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_s_pr.c create mode 100644 src/lib/libssl/src/crypto/asn1/d2i_s_pu.c create mode 100644 src/lib/libssl/src/crypto/asn1/f.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_dhp.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_dsap.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_r_pr.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_r_pu.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_s_pr.c create mode 100644 src/lib/libssl/src/crypto/asn1/i2d_s_pu.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_dgst.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_enc.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_enc_c.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_evp.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_i_s.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_lib.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_recip.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_s_e.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_signd.c create mode 100644 src/lib/libssl/src/crypto/asn1/p7_signi.c create mode 100644 src/lib/libssl/src/crypto/asn1/pkcs8.c create mode 100644 src/lib/libssl/src/crypto/asn1/x_cinf.c create mode 100644 src/lib/libssl/src/crypto/bf/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/bf/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/bf/asm/b-win32.asm create mode 100644 src/lib/libssl/src/crypto/bf/asm/bx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/bf/bf_locl.org create mode 100644 src/lib/libssl/src/crypto/bio/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/bio/bio.err create mode 100644 src/lib/libssl/src/crypto/bn/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/bn/asm/bn-win32.asm create mode 100644 src/lib/libssl/src/crypto/bn/asm/bn86unix.cpp create mode 100644 src/lib/libssl/src/crypto/bn/asm/sparc.s create mode 100644 src/lib/libssl/src/crypto/bn/asm/x86w16.asm create mode 100644 src/lib/libssl/src/crypto/bn/asm/x86w32.asm create mode 100644 src/lib/libssl/src/crypto/bn/bn.err create mode 100644 src/lib/libssl/src/crypto/bn/bn.org create mode 100644 src/lib/libssl/src/crypto/bn/bn_m.c create mode 100644 src/lib/libssl/src/crypto/bn/bn_mulw.c create mode 100644 src/lib/libssl/src/crypto/bn/bn_sub.c create mode 100644 src/lib/libssl/src/crypto/buffer/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/buffer/buffer.err create mode 100644 src/lib/libssl/src/crypto/cast/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/cast/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/cast/asm/c-win32.asm create mode 100644 src/lib/libssl/src/crypto/cast/asm/cx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/conf/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/conf/conf.c create mode 100644 src/lib/libssl/src/crypto/conf/conf.err create mode 100644 src/lib/libssl/src/crypto/conf/conf_lcl.h create mode 100644 src/lib/libssl/src/crypto/cryptall.h create mode 100644 src/lib/libssl/src/crypto/crypto.c create mode 100644 src/lib/libssl/src/crypto/crypto.err create mode 100644 src/lib/libssl/src/crypto/date.h create mode 100644 src/lib/libssl/src/crypto/des/DES.pod create mode 100644 src/lib/libssl/src/crypto/des/FILES create mode 100644 src/lib/libssl/src/crypto/des/MODES.DES create mode 100644 src/lib/libssl/src/crypto/des/Makefile.PL create mode 100644 src/lib/libssl/src/crypto/des/Makefile.lit create mode 100644 src/lib/libssl/src/crypto/des/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/des/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/des/PC1 create mode 100644 src/lib/libssl/src/crypto/des/PC2 create mode 100644 src/lib/libssl/src/crypto/des/asm/d-win32.asm create mode 100644 src/lib/libssl/src/crypto/des/asm/dx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/des/asm/y-win32.asm create mode 100644 src/lib/libssl/src/crypto/des/asm/yx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/des/des.man create mode 100644 src/lib/libssl/src/crypto/des/des.org create mode 100644 src/lib/libssl/src/crypto/des/des.pl create mode 100644 src/lib/libssl/src/crypto/des/des_crypt.man create mode 100644 src/lib/libssl/src/crypto/des/des_locl.org create mode 100644 src/lib/libssl/src/crypto/des/doIP create mode 100644 src/lib/libssl/src/crypto/des/doPC1 create mode 100644 src/lib/libssl/src/crypto/des/doPC2 create mode 100644 src/lib/libssl/src/crypto/des/ede_enc.c create mode 100644 src/lib/libssl/src/crypto/des/podd.h create mode 100644 src/lib/libssl/src/crypto/des/ranlib.sh create mode 100644 src/lib/libssl/src/crypto/des/shifts.pl create mode 100644 src/lib/libssl/src/crypto/des/sk.h create mode 100644 src/lib/libssl/src/crypto/des/supp.c create mode 100644 src/lib/libssl/src/crypto/des/testdes.pl create mode 100644 src/lib/libssl/src/crypto/des/vms.com create mode 100644 src/lib/libssl/src/crypto/dh/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/dh/dh.err create mode 100644 src/lib/libssl/src/crypto/dsa/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/dsa/dsa.err create mode 100644 src/lib/libssl/src/crypto/err/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/err/err_code.pl create mode 100644 src/lib/libssl/src/crypto/err/err_genc.pl create mode 100644 src/lib/libssl/src/crypto/err/error.err create mode 100644 src/lib/libssl/src/crypto/err/ssleay.ec create mode 100644 src/lib/libssl/src/crypto/evp/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_3d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_bf.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_c.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_i.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_r2.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cbc_r5.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_3d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_bf.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_c.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_i.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_r2.c create mode 100644 src/lib/libssl/src/crypto/evp/e_cfb_r5.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_3d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_bf.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_c.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_i.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_r2.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ecb_r5.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_3d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_bf.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_c.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_d.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_i.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_r2.c create mode 100644 src/lib/libssl/src/crypto/evp/e_ofb_r5.c create mode 100644 src/lib/libssl/src/crypto/evp/evp.err create mode 100644 src/lib/libssl/src/crypto/evp/pk_lib.c create mode 100644 src/lib/libssl/src/crypto/hmac/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/idea/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/lhash/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/libvms.com create mode 100644 src/lib/libssl/src/crypto/md2/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/md2/md2.org create mode 100644 src/lib/libssl/src/crypto/md5/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/md5/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/md5/asm/m5-win32.asm create mode 100644 src/lib/libssl/src/crypto/md5/asm/mx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/mdc2/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/objects/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/objects/objects.err create mode 100644 src/lib/libssl/src/crypto/pem/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/pem/ctx_size.c create mode 100644 src/lib/libssl/src/crypto/pem/pem.err create mode 100644 src/lib/libssl/src/crypto/pem/pem.org create mode 100644 src/lib/libssl/src/crypto/pkcs7/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/pkcs7/README create mode 100644 src/lib/libssl/src/crypto/pkcs7/mf.p7 create mode 100644 src/lib/libssl/src/crypto/pkcs7/p7.tst create mode 100644 src/lib/libssl/src/crypto/pkcs7/pkcs7.err create mode 100644 src/lib/libssl/src/crypto/rand/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/ranlib.sh create mode 100644 src/lib/libssl/src/crypto/rc2/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/rc2/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/rc2/rc2.org create mode 100644 src/lib/libssl/src/crypto/rc4/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/rc4/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/rc4/asm/r4-win32.asm create mode 100644 src/lib/libssl/src/crypto/rc4/asm/rx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/rc4/rc4.org create mode 100644 src/lib/libssl/src/crypto/rc4/rc4_locl.org create mode 100644 src/lib/libssl/src/crypto/rc5/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/rc5/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/rc5/asm/r5-win32.asm create mode 100644 src/lib/libssl/src/crypto/rc5/asm/r586unix.cpp create mode 100644 src/lib/libssl/src/crypto/rc5/asm/rc5-586.pl create mode 100644 src/lib/libssl/src/crypto/ripemd/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/ripemd/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/ripemd/asm/rm-win32.asm create mode 100644 src/lib/libssl/src/crypto/ripemd/asm/rm86unix.cpp create mode 100644 src/lib/libssl/src/crypto/rsa/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/rsa/rsa.err create mode 100644 src/lib/libssl/src/crypto/sha/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/sha/Makefile.uni create mode 100644 src/lib/libssl/src/crypto/sha/asm/s1-win32.asm create mode 100644 src/lib/libssl/src/crypto/sha/asm/sx86unix.cpp create mode 100644 src/lib/libssl/src/crypto/sha/sha_sgst.c create mode 100644 src/lib/libssl/src/crypto/stack/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/txt_db/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/x509/Makefile.ssl create mode 100644 src/lib/libssl/src/crypto/x509/attrib create mode 100644 src/lib/libssl/src/crypto/x509/v3_net.c create mode 100644 src/lib/libssl/src/crypto/x509/v3_x509.c create mode 100644 src/lib/libssl/src/crypto/x509/x509.doc create mode 100644 src/lib/libssl/src/crypto/x509/x509.err create mode 100644 src/lib/libssl/src/crypto/x509/x509pack.c create mode 100644 src/lib/libssl/src/crypto/x509/x509v3.doc create mode 100644 src/lib/libssl/src/crypto/x509v3/format create mode 100644 src/lib/libssl/src/crypto/x509v3/header create mode 100644 src/lib/libssl/src/crypto/x509v3/v3_ku.c create mode 100644 src/lib/libssl/src/dep/crypto.txt create mode 100644 src/lib/libssl/src/dep/files create mode 100644 src/lib/libssl/src/dep/gen.pl create mode 100644 src/lib/libssl/src/dep/ssl.txt create mode 100644 src/lib/libssl/src/doc/API.doc create mode 100644 src/lib/libssl/src/doc/a_verify.doc create mode 100644 src/lib/libssl/src/doc/apps.doc create mode 100644 src/lib/libssl/src/doc/asn1.doc create mode 100644 src/lib/libssl/src/doc/bio.doc create mode 100644 src/lib/libssl/src/doc/blowfish.doc create mode 100644 src/lib/libssl/src/doc/bn.doc create mode 100644 src/lib/libssl/src/doc/ca.1 create mode 100644 src/lib/libssl/src/doc/callback.doc create mode 100644 src/lib/libssl/src/doc/cipher.doc create mode 100644 src/lib/libssl/src/doc/cipher.m create mode 100644 src/lib/libssl/src/doc/conf.doc create mode 100644 src/lib/libssl/src/doc/des.doc create mode 100644 src/lib/libssl/src/doc/digest.doc create mode 100644 src/lib/libssl/src/doc/encode.doc create mode 100644 src/lib/libssl/src/doc/envelope.doc create mode 100644 src/lib/libssl/src/doc/error.doc create mode 100644 src/lib/libssl/src/doc/legal.doc create mode 100644 src/lib/libssl/src/doc/lhash.doc create mode 100644 src/lib/libssl/src/doc/md2.doc create mode 100644 src/lib/libssl/src/doc/md5.doc create mode 100644 src/lib/libssl/src/doc/memory.doc create mode 100644 src/lib/libssl/src/doc/ms3-ca.doc create mode 100644 src/lib/libssl/src/doc/ns-ca.doc create mode 100644 src/lib/libssl/src/doc/obj.doc create mode 100644 src/lib/libssl/src/doc/rand.doc create mode 100644 src/lib/libssl/src/doc/rc2.doc create mode 100644 src/lib/libssl/src/doc/rc4.doc create mode 100644 src/lib/libssl/src/doc/readme create mode 100644 src/lib/libssl/src/doc/ref.doc create mode 100644 src/lib/libssl/src/doc/req.1 create mode 100644 src/lib/libssl/src/doc/rsa.doc create mode 100644 src/lib/libssl/src/doc/rsaref.doc create mode 100644 src/lib/libssl/src/doc/s_mult.doc create mode 100644 src/lib/libssl/src/doc/session.doc create mode 100644 src/lib/libssl/src/doc/sha.doc create mode 100644 src/lib/libssl/src/doc/speed.doc create mode 100644 src/lib/libssl/src/doc/ssl-ciph.doc create mode 100644 src/lib/libssl/src/doc/ssl.doc create mode 100644 src/lib/libssl/src/doc/ssl_ctx.doc create mode 100644 src/lib/libssl/src/doc/ssleay.doc create mode 100644 src/lib/libssl/src/doc/ssluse.doc create mode 100644 src/lib/libssl/src/doc/stack.doc create mode 100644 src/lib/libssl/src/doc/threads.doc create mode 100644 src/lib/libssl/src/doc/txt_db.doc create mode 100644 src/lib/libssl/src/doc/verify create mode 100644 src/lib/libssl/src/doc/why.doc create mode 100644 src/lib/libssl/src/makefile.one create mode 100644 src/lib/libssl/src/ms/cipher.out create mode 100644 src/lib/libssl/src/ms/clear.out create mode 100644 src/lib/libssl/src/ms/libeay16.def create mode 100644 src/lib/libssl/src/ms/libeay32.def create mode 100644 src/lib/libssl/src/ms/ntdll.mak create mode 100644 src/lib/libssl/src/ms/ssleay16.def create mode 100644 src/lib/libssl/src/ms/ssleay32.def create mode 100644 src/lib/libssl/src/ms/w31dll.mak create mode 100644 src/lib/libssl/src/mt/README create mode 100644 src/lib/libssl/src/mt/mttest.c create mode 100644 src/lib/libssl/src/mt/profile.sh create mode 100644 src/lib/libssl/src/mt/ptest.bat create mode 100644 src/lib/libssl/src/mt/pthread.sh create mode 100644 src/lib/libssl/src/mt/purify.sh create mode 100644 src/lib/libssl/src/mt/solaris.sh create mode 100644 src/lib/libssl/src/mt/win32.bat create mode 100644 src/lib/libssl/src/perl/MANIFEST create mode 100644 src/lib/libssl/src/perl/Makefile.PL create mode 100644 src/lib/libssl/src/perl/SSLeay.pm create mode 100644 src/lib/libssl/src/perl/SSLeay.xs create mode 100644 src/lib/libssl/src/perl/b.pl create mode 100644 src/lib/libssl/src/perl/bio.pl create mode 100644 src/lib/libssl/src/perl/bio.txt create mode 100644 src/lib/libssl/src/perl/bio.xs create mode 100644 src/lib/libssl/src/perl/bn.pl create mode 100644 src/lib/libssl/src/perl/bn.txt create mode 100644 src/lib/libssl/src/perl/bn.xs create mode 100644 src/lib/libssl/src/perl/callback.c create mode 100644 src/lib/libssl/src/perl/cipher.pl create mode 100644 src/lib/libssl/src/perl/cipher.txt create mode 100644 src/lib/libssl/src/perl/cipher.xs create mode 100644 src/lib/libssl/src/perl/dh.pl create mode 100644 src/lib/libssl/src/perl/digest.txt create mode 100644 src/lib/libssl/src/perl/digest.xs create mode 100644 src/lib/libssl/src/perl/err.txt create mode 100644 src/lib/libssl/src/perl/err.xs create mode 100644 src/lib/libssl/src/perl/f.pl create mode 100644 src/lib/libssl/src/perl/g.pl create mode 100644 src/lib/libssl/src/perl/gen_rsa.pl create mode 100644 src/lib/libssl/src/perl/mul.pl create mode 100644 src/lib/libssl/src/perl/p5SSLeay.h create mode 100644 src/lib/libssl/src/perl/r.pl create mode 100644 src/lib/libssl/src/perl/s.pl create mode 100644 src/lib/libssl/src/perl/s2.pl create mode 100644 src/lib/libssl/src/perl/server.pem create mode 100644 src/lib/libssl/src/perl/ss.pl create mode 100644 src/lib/libssl/src/perl/ssl.pl create mode 100644 src/lib/libssl/src/perl/ssl.txt create mode 100644 src/lib/libssl/src/perl/ssl.xs create mode 100644 src/lib/libssl/src/perl/ssl_srvr.pl create mode 100644 src/lib/libssl/src/perl/sslbio.pl create mode 100644 src/lib/libssl/src/perl/t.pl create mode 100644 src/lib/libssl/src/perl/test create mode 100644 src/lib/libssl/src/perl/test.pl create mode 100644 src/lib/libssl/src/perl/test.txt create mode 100644 src/lib/libssl/src/perl/test2.pl create mode 100644 src/lib/libssl/src/perl/test3.pl create mode 100644 src/lib/libssl/src/perl/test8.pl create mode 100644 src/lib/libssl/src/perl/test9.pl create mode 100644 src/lib/libssl/src/perl/testbn.pl create mode 100644 src/lib/libssl/src/perl/testdec.pl create mode 100644 src/lib/libssl/src/perl/testmd.pl create mode 100644 src/lib/libssl/src/perl/tt.pl create mode 100644 src/lib/libssl/src/perl/typemap create mode 100644 src/lib/libssl/src/perl/x509.txt create mode 100644 src/lib/libssl/src/perl/x509.xs create mode 100644 src/lib/libssl/src/perl/xstmp.c create mode 100644 src/lib/libssl/src/perl/y.pl create mode 100644 src/lib/libssl/src/perl/yy.pl create mode 100644 src/lib/libssl/src/perl/z.pl create mode 100644 src/lib/libssl/src/perl/zz.pl create mode 100644 src/lib/libssl/src/shlib/linux.sh create mode 100644 src/lib/libssl/src/ssl/Makefile.ssl create mode 100644 src/lib/libssl/src/ssl/readme create mode 100644 src/lib/libssl/src/ssl/ssl.c create mode 100644 src/lib/libssl/src/ssl/ssl.err create mode 100644 src/lib/libssl/src/test/.rnd create mode 100644 src/lib/libssl/src/test/Makefile.ssl create mode 100644 src/lib/libssl/src/test/certCA.srl create mode 100644 src/lib/libssl/src/test/demoCA/index.txt create mode 100644 src/lib/libssl/src/test/demoCA/private/cakey.pem create mode 100644 src/lib/libssl/src/test/demoCA/serial create mode 100644 src/lib/libssl/src/test/dsa-ca.pem create mode 100644 src/lib/libssl/src/test/dsa-pca.pem create mode 100644 src/lib/libssl/src/test/p create mode 100644 src/lib/libssl/src/test/riptest create mode 100644 src/lib/libssl/src/test/test.txt create mode 100644 src/lib/libssl/src/test/testkey.pem create mode 100644 src/lib/libssl/src/test/testreq.pem create mode 100644 src/lib/libssl/src/tools/Makefile.ssl create mode 100644 src/lib/libssl/src/util/mklink.sh create mode 100644 src/lib/libssl/src/util/ranlib.sh create mode 100644 src/lib/libssl/src/util/sep_lib.sh create mode 100644 src/lib/libssl/src/util/ssldir.pl create mode 100644 src/lib/libssl/src/util/up_ver.pl create mode 100644 src/lib/libssl/ssl-patent/Makefile create mode 100644 src/lib/libssl/ssl-patent/shlib_version create mode 100644 src/lib/libssl/ssl/Makefile create mode 100644 src/lib/libssl/ssl/shlib_version create mode 100644 src/lib/libssl/ssleay.cnf create mode 100644 src/lib/libssl/test/.rnd create mode 100644 src/lib/libssl/test/Makefile.ssl create mode 100644 src/lib/libssl/test/certCA.srl create mode 100644 src/lib/libssl/test/demoCA/index.txt create mode 100644 src/lib/libssl/test/demoCA/private/cakey.pem create mode 100644 src/lib/libssl/test/demoCA/serial create mode 100644 src/lib/libssl/test/dsa-ca.pem create mode 100644 src/lib/libssl/test/dsa-pca.pem create mode 100644 src/lib/libssl/test/p create mode 100644 src/lib/libssl/test/riptest create mode 100644 src/lib/libssl/test/test.txt create mode 100644 src/lib/libssl/test/testkey.pem create mode 100644 src/lib/libssl/test/testreq.pem create mode 100644 src/regress/lib/libc/Makefile create mode 100644 src/regress/lib/libc/_setjmp/Makefile create mode 100644 src/regress/lib/libc/arch/alpha/Makefile create mode 100644 src/regress/lib/libc/arch/alpha/divremtest/Makefile create mode 100644 src/regress/lib/libc/arch/alpha/divremtest/divremtest.c create mode 100644 src/regress/lib/libc/arch/alpha/divremtest/mkcases.c create mode 100644 src/regress/lib/libc/arch/alpha/divremtest/mktestcases.c create mode 100644 src/regress/lib/libc/db/Makefile create mode 100644 src/regress/lib/libc/db/README create mode 100644 src/regress/lib/libc/db/dbtest.c create mode 100644 src/regress/lib/libc/db/run.test create mode 100644 src/regress/lib/libc/ieeefp/Makefile create mode 100644 src/regress/lib/libc/ieeefp/except/Makefile create mode 100644 src/regress/lib/libc/ieeefp/except/except.c create mode 100644 src/regress/lib/libc/ieeefp/round/Makefile create mode 100644 src/regress/lib/libc/ieeefp/round/round.c create mode 100644 src/regress/lib/libc/regex/Makefile create mode 100644 src/regress/lib/libc/regex/debug.c create mode 100644 src/regress/lib/libc/regex/debug.ih create mode 100644 src/regress/lib/libc/regex/main.c create mode 100644 src/regress/lib/libc/regex/main.ih create mode 100644 src/regress/lib/libc/regex/split.c create mode 100644 src/regress/lib/libc/regex/tests create mode 100644 src/regress/lib/libc/setjmp/Makefile create mode 100644 src/regress/lib/libc/setjmp/jmptest.c create mode 100644 src/regress/lib/libc/sigsetjmp/Makefile create mode 100644 src/regress/lib/libssl/Makefile create mode 100644 src/regress/lib/libssl/README create mode 100644 src/regress/lib/libssl/ssleay.cnf create mode 100644 src/regress/lib/libssl/test_client.sh create mode 100644 src/regress/lib/libssl/test_server.sh create mode 100644 src/regress/lib/libssl/testdsa.sh create mode 100644 src/regress/lib/libssl/testenc.sh create mode 100644 src/regress/lib/libssl/testrsa.sh create mode 100644 src/usr.bin/nc/Makefile create mode 100644 src/usr.bin/nc/README create mode 100644 src/usr.bin/nc/data/Makefile create mode 100644 src/usr.bin/nc/data/README create mode 100644 src/usr.bin/nc/data/data.c create mode 100644 src/usr.bin/nc/data/dns-any.d create mode 100644 src/usr.bin/nc/data/nfs-0.d create mode 100644 src/usr.bin/nc/data/pm.d create mode 100644 src/usr.bin/nc/data/pmap-dump.d create mode 100644 src/usr.bin/nc/data/pmap-mnt.d create mode 100644 src/usr.bin/nc/data/rip.d create mode 100644 src/usr.bin/nc/data/rservice.c create mode 100644 src/usr.bin/nc/data/showmount.d create mode 100644 src/usr.bin/nc/data/xor.c create mode 100644 src/usr.bin/nc/generic.h create mode 100644 src/usr.bin/nc/nc.1 create mode 100644 src/usr.bin/nc/netcat.blurb create mode 100644 src/usr.bin/nc/netcat.c create mode 100644 src/usr.bin/nc/scripts/README create mode 100644 src/usr.bin/nc/scripts/alta create mode 100644 src/usr.bin/nc/scripts/bsh create mode 100644 src/usr.bin/nc/scripts/dist.sh create mode 100644 src/usr.bin/nc/scripts/irc create mode 100644 src/usr.bin/nc/scripts/iscan create mode 100644 src/usr.bin/nc/scripts/ncp create mode 100644 src/usr.bin/nc/scripts/probe create mode 100644 src/usr.bin/nc/scripts/web create mode 100644 src/usr.bin/nc/scripts/webproxy create mode 100644 src/usr.bin/nc/scripts/webrelay create mode 100644 src/usr.bin/nc/scripts/websearch diff --git a/src/lib/libc/crypt/Makefile.inc b/src/lib/libc/crypt/Makefile.inc new file mode 100644 index 0000000000..068172d8eb --- /dev/null +++ b/src/lib/libc/crypt/Makefile.inc @@ -0,0 +1,12 @@ +# $OpenBSD: Makefile.inc,v 1.12 1999/02/26 00:54:00 deraadt Exp $ + +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/crypt ${LIBCSRCDIR}/crypt + +SRCS+= cast.c crypt.c morecrypt.c md5crypt.c arc4random.c blowfish.c +SRCS+= bcrypt.c skipjack.c + +MAN+= crypt.3 blowfish.3 arc4random.3 +MLINKS+=crypt.3 encrypt.3 crypt.3 setkey.3 crypt.3 des_cipher.3 +MLINKS+=crypt.3 des_setkey.3 blowfish.3 blf_key.3 blowfish.3 blf_enc.3 +MLINKS+=blowfish.3 blf_dec.3 +MLINKS+=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 @@ +.\" $OpenBSD: arc4random.3,v 1.4 1998/09/07 16:44:34 aaron Exp $ +.\" Copyright 1997 Niels Provos +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Niels Provos. +.\" 4. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" Manual page, using -mandoc macros +.\" +.Dd April 15, 1997 +.Dt ARC4RANDOM 3 +.Os "OpenBSD 2.0" +.Sh NAME +.Nm arc4random, +.Nm arc4random_stir, +.Nm arc4random_addrandom +.Nd arc4 random number generator. +.Sh SYNOPSIS +.Fd #include +.Ft u_int32_t +.Fn arc4random "void" +.Ft void +.Fn arc4random_stir "void" +.Ft void +.Fn arc4random_addrandom "u_char *dat" "int datlen" +.Sh DESCRIPTION +The +.Fn arc4random +function uses the key stream generator employed by the +arc4 cipher, which uses 8*8 8 bit S-Boxes. The S-Boxes +can be in about +.if t 2\u\s71700\s10\d +.if n (2**1700) +states. +.Pp +The +.Fn arc4random_stir +function reads data from +.Pa /dev/arandom +and uses it to permutate the S-Boxes via +.Fn arc4random_addrandom . +.Pp +There is no need to call +.Fn arc4random_stir +before using +.Fn arc4random , +since +.Fn arc4random +automatically initalizes itself. +.Sh SEE ALSO +.Xr rand48 3 , +.Xr rand 3 , +.Xr random 3 +.Sh HISTORY +.Pa RC4 +has been designed by RSA Data Security, Inc. It was posted anonymously +to the USENET and was confirmed to be equivalent by several sources who +had access to the original cipher. Since +.Pa RC4 +used to be a trade secret, the cipher is now referred to as +.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 @@ +/* $OpenBSD: arc4random.c,v 1.3 1998/03/22 19:01:16 niklas Exp $ */ + +/* + * Arc4 random number generator for OpenBSD. + * Copyright 1996 David Mazieres . + * + * Modification and redistribution in source and binary forms is + * permitted provided that due credit is given to the author and the + * OpenBSD project (for instance by leaving this copyright notice + * intact). + */ + +/* + * This code is derived from section 17.1 of Applied Cryptography, + * second edition, which describes a stream cipher allegedly + * compatible with RSA Labs "RC4" cipher (the actual description of + * which is a trade secret). The same algorithm is used as a stream + * cipher called "arcfour" in Tatu Ylonen's ssh package. + * + * Here the stream cipher has been modified always to include the time + * when initializing the state. That makes it impossible to + * regenerate the same random sequence twice, so this can't be used + * for encryption, but will generate good random numbers. + * + * RC4 is a registered trademark of RSA Laboratories. + */ + +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +#define inline __inline +#else /* !__GNUC__ */ +#define inline +#endif /* !__GNUC__ */ + +struct arc4_stream { + u_int8_t i; + u_int8_t j; + u_int8_t s[256]; +}; + +int rs_initialized; +static struct arc4_stream rs; + +static inline void +arc4_init(as) + struct arc4_stream *as; +{ + int n; + + for (n = 0; n < 256; n++) + as->s[n] = n; + as->i = 0; + as->j = 0; +} + +static inline void +arc4_addrandom(as, dat, datlen) + struct arc4_stream *as; + u_char *dat; + int datlen; +{ + int n; + u_int8_t si; + + as->i--; + for (n = 0; n < 256; n++) { + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si + dat[n % datlen]); + as->s[as->i] = as->s[as->j]; + as->s[as->j] = si; + } +} + +static void +arc4_stir(as) + struct arc4_stream *as; +{ + int fd; + struct { + struct timeval tv; + u_int8_t rnd[128 - sizeof(struct timeval)]; + } rdat; + + gettimeofday(&rdat.tv, NULL); + fd = open("/dev/arandom", O_RDONLY); + if (fd >= 0) { + read(fd, rdat.rnd, sizeof(rdat.rnd)); + close(fd); + } + /* fd < 0? Ah, what the heck. We'll just take whatever was on the + * stack... */ + + arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); +} + +static inline u_int8_t +arc4_getbyte(as) + struct arc4_stream *as; +{ + u_int8_t si, sj; + + as->i = (as->i + 1); + si = as->s[as->i]; + as->j = (as->j + si); + sj = as->s[as->j]; + as->s[as->i] = sj; + as->s[as->j] = si; + return (as->s[(si + sj) & 0xff]); +} + +static inline u_int32_t +arc4_getword(as) + struct arc4_stream *as; +{ + u_int32_t val; + val = arc4_getbyte(as) << 24; + val |= arc4_getbyte(as) << 16; + val |= arc4_getbyte(as) << 8; + val |= arc4_getbyte(as); + return val; +} + +void +arc4random_stir() +{ + if (!rs_initialized) { + arc4_init(&rs); + rs_initialized = 1; + } + arc4_stir(&rs); +} + +void +arc4random_addrandom(dat, datlen) + u_char *dat; + int datlen; +{ + if (!rs_initialized) + arc4random_stir(); + arc4_addrandom(&rs, dat, datlen); +} + +u_int32_t +arc4random() +{ + if (!rs_initialized) + arc4random_stir(); + return arc4_getword(&rs); +} + +#if 0 +/*-------- Test code for i386 --------*/ +#include +#include +int +main(int argc, char **argv) +{ + const int iter = 1000000; + int i; + pctrval v; + + v = rdtsc(); + for (i = 0; i < iter; i++) + arc4random(); + v = rdtsc() - v; + v /= iter; + + printf("%qd cycles\n", v); +} +#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 @@ +/* $OpenBSD: bcrypt.c,v 1.12 1998/08/10 18:33:07 provos Exp $ */ + +/* + * Copyright 1997 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Niels Provos. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* This password hashing algorithm was designed by David Mazieres + * and works as follows: + * + * 1. state := InitState () + * 2. state := ExpandKey (state, salt, password) 3. + * REPEAT rounds: + * state := ExpandKey (state, 0, salt) + * state := ExpandKey(state, 0, password) + * 4. ctext := "OrpheanBeholderScryDoubt" + * 5. REPEAT 64: + * ctext := Encrypt_ECB (state, ctext); + * 6. RETURN Concatenate (salt, ctext); + * + */ + +#if 0 +#include +#endif + +#include +#include +#include +#include +#include +#include + +/* This implementation is adaptable to current computing power. + * You can have up to 2^31 rounds which should be enough for some + * time to come. + */ + +#define BCRYPT_VERSION '2' +#define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */ +#define BCRYPT_BLOCKS 6 /* Ciphertext blocks */ +#define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */ + +char *bcrypt_gensalt __P((u_int8_t)); + +static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t)); +static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t)); +static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *)); + +static char encrypted[_PASSWORD_LEN]; +static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1]; +static char error[] = ":"; + +const static u_int8_t Base64Code[] = +"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + +const static u_int8_t index_64[128] = +{ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 0, 1, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, 255, 255, + 255, 255, 255, 255, 255, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, + 255, 255, 255, 255, 255, 255, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, + 51, 52, 53, 255, 255, 255, 255, 255 +}; +#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) + +#ifdef __STDC__ +static void +decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) +#else +static void +decode_base64(buffer, len, data) + u_int8_t *buffer; + u_int16_t len; + u_int8_t *data; +#endif +{ + u_int8_t *bp = buffer; + u_int8_t *p = data; + u_int8_t c1, c2, c3, c4; + while (bp < buffer + len) { + c1 = CHAR64(*p); + c2 = CHAR64(*(p + 1)); + + /* Invalid data */ + if (c1 == 255 || c2 == 255) + break; + + *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4); + if (bp >= buffer + len) + break; + + c3 = CHAR64(*(p + 2)); + if (c3 == 255) + break; + + *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2); + if (bp >= buffer + len) + break; + + c4 = CHAR64(*(p + 3)); + if (c4 == 255) + break; + *bp++ = ((c3 & 0x03) << 6) | c4; + + p += 4; + } +} + +#ifdef __STDC__ +static void +encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) +#else +static void +encode_salt(salt, csalt, clen, logr) + char *salt; + u_int8_t *csalt; + u_int16_t clen; + u_int8_t logr; +#endif +{ + salt[0] = '$'; + salt[1] = BCRYPT_VERSION; + salt[2] = 'a'; + salt[3] = '$'; + + snprintf(salt + 4, 4, "%2.2u$", logr); + + encode_base64((u_int8_t *) salt + 7, csalt, clen); +} +/* Generates a salt for this version of crypt. + Since versions may change. Keeping this here + seems sensible. + */ + +#ifdef __STDC__ +char * +bcrypt_gensalt(u_int8_t log_rounds) +#else +char * +bcrypt_gensalt(log_rounds) + u_int8_t log_rounds; +#endif +{ + u_int8_t csalt[BCRYPT_MAXSALT]; + u_int16_t i; + u_int32_t seed = 0; + + for (i = 0; i < BCRYPT_MAXSALT; i++) { + if (i % 4 == 0) + seed = arc4random(); + csalt[i] = seed & 0xff; + seed = seed >> 8; + } + + if (log_rounds < 4) + log_rounds = 4; + + encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds); + return gsalt; +} +/* We handle $Vers$log2(NumRounds)$salt+passwd$ + i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */ + +char * +bcrypt(key, salt) + const char *key; + const char *salt; +{ + blf_ctx state; + u_int32_t rounds, i, k; + u_int16_t j; + u_int8_t key_len, salt_len, logr, minor; + u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; + u_int8_t csalt[BCRYPT_MAXSALT]; + u_int32_t cdata[BCRYPT_BLOCKS]; + + /* Discard "$" identifier */ + salt++; + + if (*salt > BCRYPT_VERSION) { + /* How do I handle errors ? Return ':' */ + return error; + } + + /* Check for minor versions */ + if (salt[1] != '$') { + switch (salt[1]) { + case 'a': + /* 'ab' should not yield the same as 'abab' */ + minor = salt[1]; + salt++; + break; + default: + return error; + } + } else + minor = 0; + + /* Discard version + "$" identifier */ + salt += 2; + + if (salt[2] != '$') + /* Out of sync with passwd entry */ + return error; + + /* Computer power doesnt increase linear, 2^x should be fine */ + if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS) + return error; + + /* Discard num rounds + "$" identifier */ + salt += 3; + + /* We dont want the base64 salt but the raw data */ + decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt); + salt_len = BCRYPT_MAXSALT; + key_len = strlen(key) + (minor >= 'a' ? 1 : 0); + + /* Setting up S-Boxes and Subkeys */ + Blowfish_initstate(&state); + Blowfish_expandstate(&state, csalt, salt_len, + (u_int8_t *) key, key_len); + for (k = 0; k < rounds; k++) { + Blowfish_expand0state(&state, (u_int8_t *) key, key_len); + Blowfish_expand0state(&state, csalt, salt_len); + } + + /* This can be precomputed later */ + j = 0; + for (i = 0; i < BCRYPT_BLOCKS; i++) + cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j); + + /* Now do the encryption */ + for (k = 0; k < 64; k++) + blf_enc(&state, cdata, BCRYPT_BLOCKS / 2); + + for (i = 0; i < BCRYPT_BLOCKS; i++) { + ciphertext[4 * i + 3] = cdata[i] & 0xff; + cdata[i] = cdata[i] >> 8; + ciphertext[4 * i + 2] = cdata[i] & 0xff; + cdata[i] = cdata[i] >> 8; + ciphertext[4 * i + 1] = cdata[i] & 0xff; + cdata[i] = cdata[i] >> 8; + ciphertext[4 * i + 0] = cdata[i] & 0xff; + } + + + i = 0; + encrypted[i++] = '$'; + encrypted[i++] = BCRYPT_VERSION; + if (minor) + encrypted[i++] = minor; + encrypted[i++] = '$'; + + snprintf(encrypted + i, 4, "%2.2u$", logr); + + encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT); + encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext, + 4 * BCRYPT_BLOCKS - 1); + return encrypted; +} + +#ifdef __STDC__ +static void +encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) +#else +static void +encode_base64(buffer, data, len) + u_int8_t *buffer; + u_int8_t *data; + u_int16_t len; +#endif +{ + u_int8_t *bp = buffer; + u_int8_t *p = data; + u_int8_t c1, c2; + while (p < data + len) { + c1 = *p++; + *bp++ = Base64Code[(c1 >> 2)]; + c1 = (c1 & 0x03) << 4; + if (p >= data + len) { + *bp++ = Base64Code[c1]; + break; + } + c2 = *p++; + c1 |= (c2 >> 4) & 0x0f; + *bp++ = Base64Code[c1]; + c1 = (c2 & 0x0f) << 2; + if (p >= data + len) { + *bp++ = Base64Code[c1]; + break; + } + c2 = *p++; + c1 |= (c2 >> 6) & 0x03; + *bp++ = Base64Code[c1]; + *bp++ = Base64Code[c2 & 0x3f]; + } + *bp = '\0'; +} +#if 0 +void +main() +{ + char blubber[73]; + char salt[100]; + char *p; + salt[0] = '$'; + salt[1] = BCRYPT_VERSION; + salt[2] = '$'; + + snprintf(salt + 3, 4, "%2.2u$", 5); + + printf("24 bytes of salt: "); + fgets(salt + 6, 94, stdin); + salt[99] = 0; + printf("72 bytes of password: "); + fpurge(stdin); + fgets(blubber, 73, stdin); + blubber[72] = 0; + + p = crypt(blubber, salt); + printf("Passwd entry: %s\n\n", p); + + p = bcrypt_gensalt(5); + printf("Generated salt: %s\n", p); + p = crypt(blubber, p); + printf("Passwd entry: %s\n", p); +} +#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 @@ +.\" $OpenBSD: blowfish.3,v 1.2 1998/08/10 18:40:58 provos Exp $ +.\" Copyright 1997 Niels Provos +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Niels Provos. +.\" 4. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.\" Manual page, using -mandoc macros +.\" +.Dd February 13, 1997 +.Dt BLOWFISH 3 +.Os "OpenBSD 2.0" +.Sh NAME +.Nm blf_key, +.Nm blf_enc, +.Nm blf_dec +.Nd Blowfish encryption +.Sh SYNOPSIS +.Fd #include +.Ft void +.Fn blf_key "blf_ctx *state" "const u_int8_t *key" "u_int16_t keylen" +.Ft void +.Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" +.Ft void +.Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" +.Ft void +.Fn blf_ecb_encrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" +.Ft void +.Fn blf_ecb_decrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" +.Ft void +.Fn blf_cbc_encrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" +.Ft void +.Fn blf_cbc_decrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" +.Sh DESCRIPTION +.Pa Blowfish +is a fast unpatented block cipher designed by Bruce Schneier. +It basically consists of a 16 times iterated Feistel network. +The block size is 64 bit and the key size is maximal 448 bit. +.Pp +The +.Fn blf_key +function initializes the 4 8bit S-boxes and the 18 Subkeys with +the hexadecimal digits of Pi. The key is used for further randomization. +The first argument to +.Fn blf_enc +is the initalized state derived from +.Fn blf_key . +The stream of 32-bit words is encrypted in Electronic Codebook +Mode (ECB) and +.Pa datalen +must be even. +.Fn blf_dec +is used for decrypting Blowfish encrypted blocks. +.Pp +The functions +.Fn blf_ecb_encrypt +and +.Fn blf_ecb_decrypt +are used for encrypting and decrypting octet streams in ECB mode. +The functions +.Fn blf_cbc_encrypt +and +.Fn blf_cbc_decrypt +are used for encrypting and decrypting octet streams in +Cipherblock Chaining Mode (CBC). +.Pp +The functions +.Fn Blowfish_initstate , +.Fn Blowfish_expand0state , +.Fn Blowfish_expandstate , +.Fn Blowfish_encipher +and +.Fn Blowfish_decipher +are used for customization of the +.Pa Blowfish +cipher, e.g. for the blowfish password hashing function. +.Sh SEE ALSO +.Xr crypt 3 , +.Xr passwd 1 , +.Xr passwd 5 +.Sh AUTHOR +Niels Provos 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 @@ +/* $OpenBSD: blowfish.c,v 1.12 1998/08/30 22:35:39 niklas Exp $ */ +/* + * Blowfish block cipher for OpenBSD + * Copyright 1997 Niels Provos + * All rights reserved. + * + * Implementation advice by David Mazieres . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Niels Provos. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * This code is derived from section 14.3 and the given source + * in section V of Applied Cryptography, second edition. + * Blowfish is an unpatented fast block cipher designed by + * Bruce Schneier. + */ + +#if 0 +#include /* used for debugging */ +#include +#endif + +#include +#include + +#undef inline +#ifdef __GNUC__ +#define inline __inline +#else /* !__GNUC__ */ +#define inline +#endif /* !__GNUC__ */ + +/* Function for Feistel Networks */ + +#define F(bc, x) ((((bc)->S[0][((x) & 0xFF000000) >> 24] \ + + (bc)->S[1][((x) &0xFF0000 ) >> 16]) \ + ^ (bc)->S[2][((x) & 0xFF00) >> 8]) \ + + (bc)->S[3][(x) & 0x00FF]) + +#define BLFRND(bc,i,j,n) (i ^= F(bc,j) ^ (bc)->P[n]) + +void +Blowfish_encipher(c, xl, xr) + blf_ctx *c; + u_int32_t *xl; + u_int32_t *xr; +{ + u_int32_t Xl; + u_int32_t Xr; + + Xl = *xl; + Xr = *xr; + + Xl ^= c->P[0]; + BLFRND(c, Xr, Xl, 1); BLFRND(c, Xl, Xr, 2); + BLFRND(c, Xr, Xl, 3); BLFRND(c, Xl, Xr, 4); + BLFRND(c, Xr, Xl, 5); BLFRND(c, Xl, Xr, 6); + BLFRND(c, Xr, Xl, 7); BLFRND(c, Xl, Xr, 8); + BLFRND(c, Xr, Xl, 9); BLFRND(c, Xl, Xr, 10); + BLFRND(c, Xr, Xl, 11); BLFRND(c, Xl, Xr, 12); + BLFRND(c, Xr, Xl, 13); BLFRND(c, Xl, Xr, 14); + BLFRND(c, Xr, Xl, 15); BLFRND(c, Xl, Xr, 16); + + *xl = Xr ^ c->P[17]; + *xr = Xl; +} + +void +Blowfish_decipher(c, xl, xr) + blf_ctx *c; + u_int32_t *xl; + u_int32_t *xr; +{ + u_int32_t Xl; + u_int32_t Xr; + + Xl = *xl; + Xr = *xr; + + Xl ^= c->P[17]; + BLFRND(c, Xr, Xl, 16); BLFRND(c, Xl, Xr, 15); + BLFRND(c, Xr, Xl, 14); BLFRND(c, Xl, Xr, 13); + BLFRND(c, Xr, Xl, 12); BLFRND(c, Xl, Xr, 11); + BLFRND(c, Xr, Xl, 10); BLFRND(c, Xl, Xr, 9); + BLFRND(c, Xr, Xl, 8); BLFRND(c, Xl, Xr, 7); + BLFRND(c, Xr, Xl, 6); BLFRND(c, Xl, Xr, 5); + BLFRND(c, Xr, Xl, 4); BLFRND(c, Xl, Xr, 3); + BLFRND(c, Xr, Xl, 2); BLFRND(c, Xl, Xr, 1); + + *xl = Xr ^ c->P[0]; + *xr = Xl; +} + +void +Blowfish_initstate(c) + blf_ctx *c; +{ + +/* P-box and S-box tables initialized with digits of Pi */ + + const blf_ctx initstate = + + { { + { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, + 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, + 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, + 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, + 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, + 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, + 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, + 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, + 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, + 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, + 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, + 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, + 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, + 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, + 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, + 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, + 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, + 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, + 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, + 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, + 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, + 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, + 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, + 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, + 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, + 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, + 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, + 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, + 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, + 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, + 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, + 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, + 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, + 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, + 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, + 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, + 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, + 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, + 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, + 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, + 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, + 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, + 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a}, + { + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, + 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, + 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, + 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, + 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, + 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, + 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, + 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, + 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, + 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, + 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, + 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, + 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, + 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, + 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, + 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, + 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, + 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, + 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, + 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, + 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, + 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, + 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, + 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, + 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, + 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, + 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, + 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, + 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, + 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, + 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, + 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, + 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, + 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, + 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, + 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, + 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, + 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, + 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, + 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, + 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, + 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, + 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7}, + { + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, + 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, + 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, + 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, + 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, + 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, + 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, + 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, + 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, + 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, + 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, + 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, + 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, + 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, + 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, + 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, + 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, + 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, + 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, + 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, + 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, + 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, + 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, + 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, + 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, + 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, + 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, + 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, + 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, + 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, + 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, + 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, + 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, + 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, + 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, + 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, + 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, + 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, + 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, + 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, + 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, + 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, + 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0}, + { + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, + 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, + 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, + 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, + 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, + 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, + 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, + 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, + 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, + 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, + 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, + 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, + 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, + 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, + 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, + 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, + 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, + 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, + 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, + 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, + 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, + 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, + 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, + 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, + 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, + 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, + 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, + 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, + 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, + 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, + 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, + 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, + 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, + 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, + 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, + 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, + 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, + 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, + 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, + 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, + 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, + 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, + 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6} + }, + { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, + 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, + 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, + 0x9216d5d9, 0x8979fb1b + } }; + + *c = initstate; + +} + +#ifdef __STDC__ +u_int32_t +Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, u_int16_t *current) +#else +u_int32_t +Blowfish_stream2word(data, databytes, current) + const u_int8_t *data; + u_int16_t databytes; + u_int16_t *current; +#endif +{ + u_int8_t i; + u_int16_t j; + u_int32_t temp; + + temp = 0x00000000; + j = *current; + + for (i = 0; i < 4; i++, j++) { + if (j >= databytes) + j = 0; + temp = (temp << 8) | data[j]; + } + + *current = j; + return temp; +} + +#if __STDC__ +void +Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes) +#else +void +Blowfish_expand0state(c, key, keybytes) + blf_ctx *c; + const u_int8_t *key; + u_int16_t keybytes; +#endif +{ + u_int16_t i; + u_int16_t j; + u_int16_t k; + u_int32_t temp; + u_int32_t datal; + u_int32_t datar; + + j = 0; + for (i = 0; i < BLF_N + 2; i++) { + /* Extract 4 int8 to 1 int32 from keystream */ + temp = Blowfish_stream2word(key, keybytes, &j); + c->P[i] = c->P[i] ^ temp; + } + + j = 0; + datal = 0x00000000; + datar = 0x00000000; + for (i = 0; i < BLF_N + 2; i += 2) { + Blowfish_encipher(c, &datal, &datar); + + c->P[i] = datal; + c->P[i + 1] = datar; + } + + for (i = 0; i < 4; i++) { + for (k = 0; k < 256; k += 2) { + Blowfish_encipher(c, &datal, &datar); + + c->S[i][k] = datal; + c->S[i][k + 1] = datar; + } + } +} + + +#if __STDC__ +void +Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes, + const u_int8_t *key, u_int16_t keybytes) +#else +void +Blowfish_expandstate(c, data, databytes, key, keybytes) + blf_ctx *c; + const u_int8_t *data; + u_int16_t databytes; + const u_int8_t *key; + u_int16_t keybytes; +#endif +{ + u_int16_t i; + u_int16_t j; + u_int16_t k; + u_int32_t temp; + u_int32_t datal; + u_int32_t datar; + + j = 0; + for (i = 0; i < BLF_N + 2; i++) { + /* Extract 4 int8 to 1 int32 from keystream */ + temp = Blowfish_stream2word(key, keybytes, &j); + c->P[i] = c->P[i] ^ temp; + } + + j = 0; + datal = 0x00000000; + datar = 0x00000000; + for (i = 0; i < BLF_N + 2; i += 2) { + datal ^= Blowfish_stream2word(data, databytes, &j); + datar ^= Blowfish_stream2word(data, databytes, &j); + Blowfish_encipher(c, &datal, &datar); + + c->P[i] = datal; + c->P[i + 1] = datar; + } + + for (i = 0; i < 4; i++) { + for (k = 0; k < 256; k += 2) { + datal ^= Blowfish_stream2word(data, databytes, &j); + datar ^= Blowfish_stream2word(data, databytes, &j); + Blowfish_encipher(c, &datal, &datar); + + c->S[i][k] = datal; + c->S[i][k + 1] = datar; + } + } + +} + +#if __STDC__ +void +blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len) +#else +void +blf_key(c, k, len) + blf_ctx *c; + const u_int8_t *k; + u_int16_t len; +#endif +{ + /* Initalize S-boxes and subkeys with Pi */ + Blowfish_initstate(c); + + /* Transform S-boxes and subkeys with key */ + Blowfish_expand0state(c, k, len); +} + +#if __STDC__ +void +blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks) +#else +void +blf_enc(c, data, blocks) + blf_ctx *c; + u_int32_t *data; + u_int16_t blocks; +#endif +{ + u_int32_t *d; + u_int16_t i; + + d = data; + for (i = 0; i < blocks; i++) { + Blowfish_encipher(c, d, d + 1); + d += 2; + } +} + +#if __STDC__ +void +blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) +#else +void +blf_dec(c, data, blocks) + blf_ctx *c; + u_int32_t *data; + u_int16_t blocks; +#endif +{ + u_int32_t *d; + u_int16_t i; + + d = data; + for (i = 0; i < blocks; i++) { + Blowfish_decipher(c, d, d + 1); + d += 2; + } +} + +#if __STDC__ +void +blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) +#else +void +blf_ecb_encrypt(c, data, len) + blf_ctx *c; + u_int8_t *data; + u_int32_t len; +#endif +{ + u_int32_t l, r; + u_int32_t i; + + for (i = 0; i < len; i += 8) { + l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; + Blowfish_encipher(c, &l, &r); + data[0] = l >> 24 & 0xff; + data[1] = l >> 16 & 0xff; + data[2] = l >> 8 & 0xff; + data[3] = l & 0xff; + data[4] = r >> 24 & 0xff; + data[5] = r >> 16 & 0xff; + data[6] = r >> 8 & 0xff; + data[7] = r & 0xff; + data += 8; + } +} + +#if __STDC__ +void +blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) +#else +void +blf_ecb_decrypt(c, data, len) + blf_ctx *c; + u_int8_t *data; + u_int32_t len; +#endif +{ + u_int32_t l, r; + u_int32_t i; + + for (i = 0; i < len; i += 8) { + l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; + Blowfish_decipher(c, &l, &r); + data[0] = l >> 24 & 0xff; + data[1] = l >> 16 & 0xff; + data[2] = l >> 8 & 0xff; + data[3] = l & 0xff; + data[4] = r >> 24 & 0xff; + data[5] = r >> 16 & 0xff; + data[6] = r >> 8 & 0xff; + data[7] = r & 0xff; + data += 8; + } +} + +#if __STDC__ +void +blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) +#else +void +blf_cbc_encrypt(c, iv, data, len) + blf_ctx *c; + u_int8_t *iv; + u_int8_t *data; + u_int32_t len; +#endif +{ + u_int32_t l, r; + u_int32_t i, j; + + for (i = 0; i < len; i += 8) { + for (j = 0; j < 8; j++) + data[j] ^= iv[j]; + l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; + Blowfish_encipher(c, &l, &r); + data[0] = l >> 24 & 0xff; + data[1] = l >> 16 & 0xff; + data[2] = l >> 8 & 0xff; + data[3] = l & 0xff; + data[4] = r >> 24 & 0xff; + data[5] = r >> 16 & 0xff; + data[6] = r >> 8 & 0xff; + data[7] = r & 0xff; + iv = data; + data += 8; + } +} + +#if __STDC__ +void +blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) +#else +void +blf_cbc_decrypt(c, iva, data, len) + blf_ctx *c; + u_int8_t *iva; + u_int8_t *data; + u_int32_t len; +#endif +{ + u_int32_t l, r; + u_int8_t *iv; + u_int32_t i, j; + + iv = data + len - 16; + data = data + len - 8; + for (i = len - 8; i >= 8; i -= 8) { + l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; + Blowfish_decipher(c, &l, &r); + data[0] = l >> 24 & 0xff; + data[1] = l >> 16 & 0xff; + data[2] = l >> 8 & 0xff; + data[3] = l & 0xff; + data[4] = r >> 24 & 0xff; + data[5] = r >> 16 & 0xff; + data[6] = r >> 8 & 0xff; + data[7] = r & 0xff; + for (j = 0; j < 8; j++) + data[j] ^= iv[j]; + iv = data; + data -= 8; + } + l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; + Blowfish_decipher(c, &l, &r); + data[0] = l >> 24 & 0xff; + data[1] = l >> 16 & 0xff; + data[2] = l >> 8 & 0xff; + data[3] = l & 0xff; + data[4] = r >> 24 & 0xff; + data[5] = r >> 16 & 0xff; + data[6] = r >> 8 & 0xff; + data[7] = r & 0xff; + for (j = 0; j < 8; j++) + data[j] ^= iva[j]; +} + +#if 0 +void +report(u_int32_t data[], u_int16_t len) +{ + u_int16_t i; + for (i = 0; i < len; i += 2) + printf("Block %0hd: %08lx %08lx.\n", + i / 2, data[i], data[i + 1]); +} +void +main(void) +{ + + blf_ctx c; + char key[] = "AAAAA"; + char key2[] = "abcdefghijklmnopqrstuvwxyz"; + + u_int32_t data[10]; + u_int32_t data2[] = + {0x424c4f57l, 0x46495348l}; + + u_int16_t i; + + /* First test */ + for (i = 0; i < 10; i++) + data[i] = i; + + blf_key(&c, (u_int8_t *) key, 5); + blf_enc(&c, data, 5); + blf_dec(&c, data, 1); + blf_dec(&c, data + 2, 4); + printf("Should read as 0 - 9.\n"); + report(data, 10); + + /* Second test */ + blf_key(&c, (u_int8_t *) key2, strlen(key2)); + blf_enc(&c, data2, 1); + printf("\nShould read as: 0x324ed0fe 0xf413a203.\n"); + report(data2, 2); + blf_dec(&c, data2, 1); + report(data2, 2); +} +#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 @@ +/* $OpenBSD: cast.c,v 1.2 1998/07/21 22:42:03 provos Exp $ */ +/* + * CAST-128 in C + * Written by Steve Reid + * 100% Public Domain - no warranty + * Released 1997.10.11 + */ + +#include + +#include + +/* CAST S-Boxes */ + +static const u_int32_t cast_sbox1[256] = { + 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, + 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, + 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, + 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, + 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, + 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, + 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, + 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, + 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, + 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, + 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, + 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, + 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, + 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, + 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, + 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, + 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, + 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, + 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, + 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, + 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, + 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, + 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, + 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, + 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, + 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, + 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, + 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, + 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, + 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, + 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, + 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, + 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, + 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, + 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, + 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, + 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, + 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, + 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, + 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, + 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, + 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, + 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, + 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, + 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, + 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, + 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, + 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, + 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, + 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, + 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, + 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, + 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, + 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, + 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, + 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, + 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, + 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, + 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, + 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, + 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, + 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, + 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, + 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF +}; + +static const u_int32_t cast_sbox2[256] = { + 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, + 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, + 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, + 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, + 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, + 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, + 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, + 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, + 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, + 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, + 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, + 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, + 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, + 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, + 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, + 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, + 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, + 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, + 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, + 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, + 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, + 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, + 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, + 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, + 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, + 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, + 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, + 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, + 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, + 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, + 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, + 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, + 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, + 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, + 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, + 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, + 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, + 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, + 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, + 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, + 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, + 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, + 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, + 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, + 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, + 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, + 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, + 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, + 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, + 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, + 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, + 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, + 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, + 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, + 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, + 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, + 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, + 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, + 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, + 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, + 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, + 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, + 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, + 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 +}; + +static const u_int32_t cast_sbox3[256] = { + 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, + 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, + 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, + 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, + 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, + 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, + 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, + 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, + 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, + 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, + 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, + 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, + 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, + 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, + 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, + 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, + 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, + 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, + 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, + 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, + 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, + 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, + 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, + 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, + 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, + 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, + 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, + 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, + 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, + 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, + 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, + 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, + 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, + 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, + 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, + 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, + 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, + 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, + 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, + 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, + 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, + 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, + 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, + 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, + 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, + 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, + 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, + 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, + 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, + 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, + 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, + 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, + 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, + 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, + 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, + 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, + 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, + 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, + 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, + 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, + 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, + 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, + 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, + 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 +}; + +static const u_int32_t cast_sbox4[256] = { + 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, + 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, + 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, + 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, + 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, + 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, + 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, + 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, + 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, + 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, + 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, + 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, + 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, + 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, + 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, + 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, + 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, + 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, + 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, + 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, + 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, + 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, + 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, + 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, + 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, + 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, + 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, + 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, + 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, + 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, + 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, + 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, + 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, + 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, + 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, + 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, + 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, + 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, + 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, + 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, + 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, + 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, + 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, + 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, + 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, + 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, + 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, + 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, + 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, + 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, + 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, + 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, + 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, + 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, + 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, + 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, + 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, + 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, + 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, + 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, + 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, + 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, + 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, + 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 +}; + +static const u_int32_t cast_sbox5[256] = { + 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, + 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, + 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, + 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, + 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, + 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, + 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, + 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, + 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, + 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, + 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, + 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, + 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, + 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, + 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, + 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, + 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, + 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, + 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, + 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, + 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, + 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, + 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, + 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, + 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, + 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, + 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, + 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, + 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, + 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, + 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, + 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, + 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, + 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, + 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, + 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, + 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, + 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, + 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, + 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, + 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, + 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, + 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, + 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, + 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, + 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, + 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, + 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, + 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, + 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, + 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, + 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, + 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, + 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, + 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, + 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, + 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, + 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, + 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, + 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, + 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, + 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, + 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, + 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 +}; + +static const u_int32_t cast_sbox6[256] = { + 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, + 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, + 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, + 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, + 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, + 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, + 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, + 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, + 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, + 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, + 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, + 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, + 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, + 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, + 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, + 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, + 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, + 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, + 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, + 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, + 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, + 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, + 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, + 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, + 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, + 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, + 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, + 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, + 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, + 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, + 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, + 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, + 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, + 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, + 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, + 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, + 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, + 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, + 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, + 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, + 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, + 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, + 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, + 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, + 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, + 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, + 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, + 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, + 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, + 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, + 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, + 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, + 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, + 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, + 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, + 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, + 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, + 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, + 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, + 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, + 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, + 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, + 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, + 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F +}; + +static const u_int32_t cast_sbox7[256] = { + 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, + 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, + 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, + 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, + 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, + 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, + 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, + 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, + 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, + 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, + 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, + 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, + 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, + 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, + 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, + 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, + 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, + 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, + 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, + 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, + 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, + 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, + 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, + 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, + 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, + 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, + 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, + 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, + 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, + 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, + 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, + 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, + 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, + 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, + 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, + 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, + 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, + 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, + 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, + 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, + 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, + 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, + 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, + 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, + 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, + 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, + 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, + 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, + 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, + 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, + 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, + 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, + 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, + 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, + 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, + 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, + 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, + 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, + 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, + 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, + 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, + 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, + 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, + 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 +}; + +static const u_int32_t cast_sbox8[256] = { + 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, + 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, + 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, + 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, + 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, + 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, + 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, + 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, + 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, + 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, + 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, + 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, + 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, + 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, + 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, + 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, + 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, + 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, + 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, + 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, + 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, + 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, + 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, + 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, + 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, + 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, + 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, + 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, + 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, + 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, + 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, + 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, + 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, + 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, + 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, + 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, + 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, + 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, + 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, + 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, + 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, + 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, + 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, + 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, + 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, + 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, + 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, + 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, + 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, + 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, + 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, + 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, + 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, + 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, + 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, + 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, + 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, + 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, + 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, + 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, + 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, + 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, + 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, + 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E +}; + +/* Macros to access 8-bit bytes out of a 32-bit word */ +#define U8a(x) ( (u_int8_t) (x>>24) ) +#define U8b(x) ( (u_int8_t) ((x>>16)&255) ) +#define U8c(x) ( (u_int8_t) ((x>>8)&255) ) +#define U8d(x) ( (u_int8_t) ((x)&255) ) + +/* Circular left shift */ +#define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) ) + +/* CAST-128 uses three different round functions */ +#define F1(l, r, i) \ + t = ROL(key->xkey[i] + r, key->xkey[i+16]); \ + l ^= ((cast_sbox1[U8a(t)] ^ cast_sbox2[U8b(t)]) - \ + cast_sbox3[U8c(t)]) + cast_sbox4[U8d(t)]; +#define F2(l, r, i) \ + t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); \ + l ^= ((cast_sbox1[U8a(t)] - cast_sbox2[U8b(t)]) + \ + cast_sbox3[U8c(t)]) ^ cast_sbox4[U8d(t)]; +#define F3(l, r, i) \ + t = ROL(key->xkey[i] - r, key->xkey[i+16]); \ + l ^= ((cast_sbox1[U8a(t)] + cast_sbox2[U8b(t)]) ^ \ + cast_sbox3[U8c(t)]) - cast_sbox4[U8d(t)]; + + +/***** Encryption Function *****/ + +void cast_encrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock) +{ +u_int32_t t, l, r; + + /* Get inblock into l,r */ + l = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) | + ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3]; + r = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) | + ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7]; + /* Do the work */ + F1(l, r, 0); + F2(r, l, 1); + F3(l, r, 2); + F1(r, l, 3); + F2(l, r, 4); + F3(r, l, 5); + F1(l, r, 6); + F2(r, l, 7); + F3(l, r, 8); + F1(r, l, 9); + F2(l, r, 10); + F3(r, l, 11); + /* Only do full 16 rounds if key length > 80 bits */ + if (key->rounds > 12) { + F1(l, r, 12); + F2(r, l, 13); + F3(l, r, 14); + F1(r, l, 15); + } + /* Put l,r into outblock */ + outblock[0] = U8a(r); + outblock[1] = U8b(r); + outblock[2] = U8c(r); + outblock[3] = U8d(r); + outblock[4] = U8a(l); + outblock[5] = U8b(l); + outblock[6] = U8c(l); + outblock[7] = U8d(l); + /* Wipe clean */ + t = l = r = 0; +} + + +/***** Decryption Function *****/ + +void cast_decrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock) +{ +u_int32_t t, l, r; + + /* Get inblock into l,r */ + r = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) | + ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3]; + l = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) | + ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7]; + /* Do the work */ + /* Only do full 16 rounds if key length > 80 bits */ + if (key->rounds > 12) { + F1(r, l, 15); + F3(l, r, 14); + F2(r, l, 13); + F1(l, r, 12); + } + F3(r, l, 11); + F2(l, r, 10); + F1(r, l, 9); + F3(l, r, 8); + F2(r, l, 7); + F1(l, r, 6); + F3(r, l, 5); + F2(l, r, 4); + F1(r, l, 3); + F3(l, r, 2); + F2(r, l, 1); + F1(l, r, 0); + /* Put l,r into outblock */ + outblock[0] = U8a(l); + outblock[1] = U8b(l); + outblock[2] = U8c(l); + outblock[3] = U8d(l); + outblock[4] = U8a(r); + outblock[5] = U8b(r); + outblock[6] = U8c(r); + outblock[7] = U8d(r); + /* Wipe clean */ + t = l = r = 0; +} + + +/***** Key Schedual *****/ + +void cast_setkey(cast_key* key, u_int8_t* rawkey, int keybytes) +{ +u_int32_t t[4], z[4], x[4]; +int i; + + /* Set number of rounds to 12 or 16, depending on key length */ + key->rounds = (keybytes <= 10 ? 12 : 16); + + /* Copy key to workspace x */ + for (i = 0; i < 4; i++) { + x[i] = 0; + if ((i*4+0) < keybytes) x[i] = (u_int32_t)rawkey[i*4+0] << 24; + if ((i*4+1) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+1] << 16; + if ((i*4+2) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+2] << 8; + if ((i*4+3) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+3]; + } + /* Generate 32 subkeys, four at a time */ + for (i = 0; i < 32; i+=4) { + switch (i & 4) { + case 0: + t[0] = z[0] = x[0] ^ cast_sbox5[U8b(x[3])] ^ + cast_sbox6[U8d(x[3])] ^ cast_sbox7[U8a(x[3])] ^ + cast_sbox8[U8c(x[3])] ^ cast_sbox7[U8a(x[2])]; + t[1] = z[1] = x[2] ^ cast_sbox5[U8a(z[0])] ^ + cast_sbox6[U8c(z[0])] ^ cast_sbox7[U8b(z[0])] ^ + cast_sbox8[U8d(z[0])] ^ cast_sbox8[U8c(x[2])]; + t[2] = z[2] = x[3] ^ cast_sbox5[U8d(z[1])] ^ + cast_sbox6[U8c(z[1])] ^ cast_sbox7[U8b(z[1])] ^ + cast_sbox8[U8a(z[1])] ^ cast_sbox5[U8b(x[2])]; + t[3] = z[3] = x[1] ^ cast_sbox5[U8c(z[2])] ^ + cast_sbox6[U8b(z[2])] ^ cast_sbox7[U8d(z[2])] ^ + cast_sbox8[U8a(z[2])] ^ cast_sbox6[U8d(x[2])]; + break; + case 4: + t[0] = x[0] = z[2] ^ cast_sbox5[U8b(z[1])] ^ + cast_sbox6[U8d(z[1])] ^ cast_sbox7[U8a(z[1])] ^ + cast_sbox8[U8c(z[1])] ^ cast_sbox7[U8a(z[0])]; + t[1] = x[1] = z[0] ^ cast_sbox5[U8a(x[0])] ^ + cast_sbox6[U8c(x[0])] ^ cast_sbox7[U8b(x[0])] ^ + cast_sbox8[U8d(x[0])] ^ cast_sbox8[U8c(z[0])]; + t[2] = x[2] = z[1] ^ cast_sbox5[U8d(x[1])] ^ + cast_sbox6[U8c(x[1])] ^ cast_sbox7[U8b(x[1])] ^ + cast_sbox8[U8a(x[1])] ^ cast_sbox5[U8b(z[0])]; + t[3] = x[3] = z[3] ^ cast_sbox5[U8c(x[2])] ^ + cast_sbox6[U8b(x[2])] ^ cast_sbox7[U8d(x[2])] ^ + cast_sbox8[U8a(x[2])] ^ cast_sbox6[U8d(z[0])]; + break; + } + switch (i & 12) { + case 0: + case 12: + key->xkey[i+0] = cast_sbox5[U8a(t[2])] ^ cast_sbox6[U8b(t[2])] ^ + cast_sbox7[U8d(t[1])] ^ cast_sbox8[U8c(t[1])]; + key->xkey[i+1] = cast_sbox5[U8c(t[2])] ^ cast_sbox6[U8d(t[2])] ^ + cast_sbox7[U8b(t[1])] ^ cast_sbox8[U8a(t[1])]; + key->xkey[i+2] = cast_sbox5[U8a(t[3])] ^ cast_sbox6[U8b(t[3])] ^ + cast_sbox7[U8d(t[0])] ^ cast_sbox8[U8c(t[0])]; + key->xkey[i+3] = cast_sbox5[U8c(t[3])] ^ cast_sbox6[U8d(t[3])] ^ + cast_sbox7[U8b(t[0])] ^ cast_sbox8[U8a(t[0])]; + break; + case 4: + case 8: + key->xkey[i+0] = cast_sbox5[U8d(t[0])] ^ cast_sbox6[U8c(t[0])] ^ + cast_sbox7[U8a(t[3])] ^ cast_sbox8[U8b(t[3])]; + key->xkey[i+1] = cast_sbox5[U8b(t[0])] ^ cast_sbox6[U8a(t[0])] ^ + cast_sbox7[U8c(t[3])] ^ cast_sbox8[U8d(t[3])]; + key->xkey[i+2] = cast_sbox5[U8d(t[1])] ^ cast_sbox6[U8c(t[1])] ^ + cast_sbox7[U8a(t[2])] ^ cast_sbox8[U8b(t[2])]; + key->xkey[i+3] = cast_sbox5[U8b(t[1])] ^ cast_sbox6[U8a(t[1])] ^ + cast_sbox7[U8c(t[2])] ^ cast_sbox8[U8d(t[2])]; + break; + } + switch (i & 12) { + case 0: + key->xkey[i+0] ^= cast_sbox5[U8c(z[0])]; + key->xkey[i+1] ^= cast_sbox6[U8c(z[1])]; + key->xkey[i+2] ^= cast_sbox7[U8b(z[2])]; + key->xkey[i+3] ^= cast_sbox8[U8a(z[3])]; + break; + case 4: + key->xkey[i+0] ^= cast_sbox5[U8a(x[2])]; + key->xkey[i+1] ^= cast_sbox6[U8b(x[3])]; + key->xkey[i+2] ^= cast_sbox7[U8d(x[0])]; + key->xkey[i+3] ^= cast_sbox8[U8d(x[1])]; + break; + case 8: + key->xkey[i+0] ^= cast_sbox5[U8b(z[2])]; + key->xkey[i+1] ^= cast_sbox6[U8a(z[3])]; + key->xkey[i+2] ^= cast_sbox7[U8c(z[0])]; + key->xkey[i+3] ^= cast_sbox8[U8c(z[1])]; + break; + case 12: + key->xkey[i+0] ^= cast_sbox5[U8d(x[0])]; + key->xkey[i+1] ^= cast_sbox6[U8d(x[1])]; + key->xkey[i+2] ^= cast_sbox7[U8a(x[2])]; + key->xkey[i+3] ^= cast_sbox8[U8b(x[3])]; + break; + } + if (i >= 16) { + key->xkey[i+0] &= 31; + key->xkey[i+1] &= 31; + key->xkey[i+2] &= 31; + key->xkey[i+3] &= 31; + } + } + /* Wipe clean */ + for (i = 0; i < 4; i++) { + t[i] = x[i] = z[i] = 0; + } +} + +/* Made in Canada */ + diff --git a/src/lib/libc/crypt/crypt.3 b/src/lib/libc/crypt/crypt.3 new file mode 100644 index 0000000000..239c9b3089 --- /dev/null +++ b/src/lib/libc/crypt/crypt.3 @@ -0,0 +1,298 @@ +.\" $OpenBSD: crypt.3,v 1.12 1999/03/18 11:08:33 aaron Exp $ +.\" +.\" FreeSec: libcrypt +.\" +.\" Copyright (c) 1994 David Burren +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 4. Neither the name of the author nor the names of other contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" Manual page, using -mandoc macros +.\" +.Dd March 9, 1994 +.Dt CRYPT 3 +.Os +.Sh NAME +.Nm crypt , +.Nm setkey , +.Nm encrypt , +.Nm des_setkey , +.Nm des_cipher +.Nd DES encryption +.Sh SYNOPSIS +.Fd #include +.Ft char +.Fn *crypt "const char *key" "const char *setting" +.Ft int +.Fn setkey "char *key" +.Ft int +.Fn encrypt "char *block" "int flag" +.Ft int +.Fn des_setkey "const char *key" +.Ft int +.Fn des_cipher "const char *in" "char *out" "int32_t salt" "int count" +.Sh DESCRIPTION +The +.Fn crypt +function performs password encryption, based on the +.Tn NBS +Data Encryption Standard (DES). +Additional code has been added to deter key search attempts and to use +stronger hashing algorithms. +The first argument to +.Fn crypt +is a +.Dv null Ns -terminated +string, typically a user's typed password. +The second is in one of three forms: +if it begins with an underscore (``_'') then an extended format is used +in interpreting both the key and the setting, as outlined below. If it begins +with an string character (``$'') and a number then a different algorithm +is used depending on the number. At the moment a ``$1'' chooses MD5 hashing +and a ``$2'' chooses Blowfish hashing, see below for more information. +.Ss Extended crypt: +.Pp +The +.Ar key +is divided into groups of 8 characters (the last group is null-padded) +and the low-order 7 bits of each character (56 bits per group) are +used to form the DES key as follows: +the first group of 56 bits becomes the initial DES key. +For each additional group, the XOR of the encryption of the current DES +key with itself and the group bits becomes the next DES key. +.Pp +The setting is a 9-character array consisting of an underscore followed +by 4 bytes of iteration count and 4 bytes of salt. +These are encoded as printable characters, 6 bits per character, +least significant character first. +The values 0 to 63 are encoded as ``./0-9A-Za-z''. +This allows 24 bits for both +.Fa count +and +.Fa salt . +.Ss "MD5" crypt: +.Pp +For +.Tn MD5 +crypt the version number, +.Fa salt +and the hashed password are separated +by the ``$'' character. The maximum length of a password is limited by +the length counter of the MD5 context, which is about +2**64. A valid MD5 password entry looks like this: +.Pp +``$1$caeiHQwX$hsKqOjrFRRN6K32OWkCBf1''. +.Pp +The whole MD5 password string is passed as +.Fa setting +for interpretation. +.Ss "Blowfish" crypt: +.Pp +The +.Tn Blowfish +version of crypt has 128 bits of +.Fa salt +in order to make building +dictionaries of common passwords space consuming. The initial state +of the +.Tn Blowfish +cipher is expanded using the +.Fa salt +and the +.Fa password +repeating the process a variable number of rounds, which is encoded in +the password string. The maximum password length is 72. The final Blowfish +password entry is created by encrypting +the string ``OrpheanBeholderScryDoubt'' with the +.Tn Blowfish +state 64 times. +.Pp +The version number, the logarithm of the number of rounds and +the concatenation of salt and +hashed password are separated by the ``$'' character. An encoded ``8'' +would specify 256 rounds. +A valid Blowfish password looks like this: +.Pp +``$2a$12$eIAq8PR8sIUnJ1HaohxX2O9x9Qlm2vK97LJ5dsXdmB.eXF42qjchC''. +.Pp +The whole Blowfish password string is passed as +.Fa setting +for interpretation. +.Ss "Traditional" crypt: +.Pp +The first 8 bytes of the key are null-padded, and the low-order 7 bits of +each character is used to form the 56-bit +.Tn DES +key. +.Pp +The setting is a 2-character array of the ASCII-encoded salt. +Thus only 12 bits of +.Fa salt +are used. +.Fa count +is set to 25. +.Ss DES Algorithm: +.Pp +The +.Fa salt +introduces disorder in the +.Tn DES +algorithm in one of 16777216 or 4096 possible ways +(ie. with 24 or 12 bits: if bit +.Em i +of the +.Ar salt +is set, then bits +.Em i +and +.Em i+24 +are swapped in the +.Tn DES +E-box output). +.Pp +The DES key is used to encrypt a 64-bit constant using +.Ar count +iterations of +.Tn DES . +The value returned is a +.Dv null Ns -terminated +string, 20 or 13 bytes (plus null) in length, consisting of the +.Ar setting +followed by the encoded 64-bit encryption. +.Pp +The functions, +.Fn encrypt , +.Fn setkey , +.Fn des_setkey +and +.Fn des_cipher +provide access to the +.Tn DES +algorithm itself. +.Fn setkey +is passed a 64-byte array of binary values (numeric 0 or 1). +A 56-bit key is extracted from this array by dividing the +array into groups of 8, and ignoring the last bit in each group. +That bit is reserved for a byte parity check by DES, but is ignored +by these functions. +.Pp +The +.Fa block +argument to +.Fn encrypt +is also a 64-byte array of binary values. +If the value of +.Fa flag +is 0, +.Fa block +is encrypted otherwise it is decrypted. +The result is returned in the original array +.Fa block +after using the key specified by +.Fn setkey +to process it. +.Pp +The argument to +.Fn des_setkey +is a character array of length 8. +The least significant bit (the parity bit) in each character is ignored, +and the remaining bits are concatenated to form a 56-bit key. +The function +.Fn des_cipher +encrypts (or decrypts if +.Fa count +is negative) the 64-bits stored in the 8 characters at +.Fa in +using +.Xr abs 3 +of +.Fa count +iterations of +.Tn DES +and stores the 64-bit result in the 8 characters at +.Fa out +(which may be the same as +.Fa in +). +The +.Fa salt +specifies perturbations to the +.Tn DES +E-box output as described above. +.Pp +The function +.Fn crypt +returns a pointer to the encrypted value on success, and NULL on failure. +The functions +.Fn setkey , +.Fn encrypt , +.Fn des_setkey , +and +.Fn des_cipher +return 0 on success and 1 on failure. +.Pp +The +.Fn crypt , +.Fn setkey +and +.Fn des_setkey +functions all manipulate the same key space. +.Sh SEE ALSO +.Xr login 1 , +.Xr passwd 1 , +.Xr blowfish 3 , +.Xr getpass 3 , +.Xr md5 3 , +.Xr passwd 5 +.Sh BUGS +The +.Fn crypt +function returns a pointer to static data, and subsequent calls to +.Fn crypt +will modify the same object. +.Sh HISTORY +A rotor-based +.Fn crypt +function appeared in +.At v6 . +The current style +.Fn crypt +first appeared in +.At v7 . +.Pp +This library (FreeSec 1.0) was developed outside the United States of America +as an unencumbered replacement for the U.S.-only libcrypt encryption +library. +Programs linked against the +.Fn crypt +interface may be exported from the U.S.A. only if they use +.Fn crypt +solely for authentication purposes and avoid use of +the other programmer interfaces listed above. Special care has been taken +in the library so that programs which only use the +.Fn crypt +interface do not pull in the other components. +.Sh AUTHOR +David Burren 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 @@ +/* $OpenBSD: crypt.c,v 1.13 1998/03/22 19:01:18 niklas Exp $ */ + +/* + * FreeSec: libcrypt + * + * Copyright (c) 1994 David Burren + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the author nor the names of other contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * This is an original implementation of the DES and the crypt(3) interfaces + * by David Burren . + * + * An excellent reference on the underlying algorithm (and related + * algorithms) is: + * + * B. Schneier, Applied Cryptography: protocols, algorithms, + * and source code in C, John Wiley & Sons, 1994. + * + * Note that in that book's description of DES the lookups for the initial, + * pbox, and final permutations are inverted (this has been brought to the + * attention of the author). A list of errata for this book has been + * posted to the sci.crypt newsgroup by the author and is available for FTP. + * + * NOTE: + * This file has a static version of des_setkey() so that crypt.o exports + * only the crypt() interface. This is required to make binaries linked + * against crypt.o exportable or re-exportable from the USA. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: crypt.c,v 1.13 1998/03/22 19:01:18 niklas Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#ifdef DEBUG +# include +#endif + +static u_char IP[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 +}; + +static u_char inv_key_perm[64]; +static u_char u_key_perm[56]; +static u_char key_perm[56] = { + 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 +}; + +static u_char key_shifts[16] = { + 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 +}; + +static u_char inv_comp_perm[56]; +static u_char comp_perm[48] = { + 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 +}; + +/* + * No E box is used, as it's replaced by some ANDs, shifts, and ORs. + */ + +static u_char u_sbox[8][64]; +static u_char sbox[8][64] = { + { + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 + }, + { + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 + }, + { + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 + }, + { + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 + }, + { + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 + }, + { + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 + }, + { + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 + }, + { + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 + } +}; + +static u_char un_pbox[32]; +static u_char pbox[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +}; + +static u_int32_t bits32[32] = +{ + 0x80000000, 0x40000000, 0x20000000, 0x10000000, + 0x08000000, 0x04000000, 0x02000000, 0x01000000, + 0x00800000, 0x00400000, 0x00200000, 0x00100000, + 0x00080000, 0x00040000, 0x00020000, 0x00010000, + 0x00008000, 0x00004000, 0x00002000, 0x00001000, + 0x00000800, 0x00000400, 0x00000200, 0x00000100, + 0x00000080, 0x00000040, 0x00000020, 0x00000010, + 0x00000008, 0x00000004, 0x00000002, 0x00000001 +}; + +static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + +static u_int32_t saltbits; +static int32_t old_salt; +static u_int32_t *bits28, *bits24; +static u_char init_perm[64], final_perm[64]; +static u_int32_t en_keysl[16], en_keysr[16]; +static u_int32_t de_keysl[16], de_keysr[16]; +static int des_initialised = 0; +static u_char m_sbox[4][4096]; +static u_int32_t psbox[4][256]; +static u_int32_t ip_maskl[8][256], ip_maskr[8][256]; +static u_int32_t fp_maskl[8][256], fp_maskr[8][256]; +static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; +static u_int32_t comp_maskl[8][128], comp_maskr[8][128]; +static u_int32_t old_rawkey0, old_rawkey1; + +static u_char ascii64[] = + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +/* 0000000000111111111122222222223333333333444444444455555555556666 */ +/* 0123456789012345678901234567890123456789012345678901234567890123 */ + +static __inline int +ascii_to_bin(ch) + char ch; +{ + if (ch > 'z') + return(0); + if (ch >= 'a') + return(ch - 'a' + 38); + if (ch > 'Z') + return(0); + if (ch >= 'A') + return(ch - 'A' + 12); + if (ch > '9') + return(0); + if (ch >= '.') + return(ch - '.'); + return(0); +} + +static void +des_init() +{ + int i, j, b, k, inbit, obit; + u_int32_t *p, *il, *ir, *fl, *fr; + + old_rawkey0 = old_rawkey1 = 0; + saltbits = 0; + old_salt = 0; + bits24 = (bits28 = bits32 + 4) + 4; + + /* + * Invert the S-boxes, reordering the input bits. + */ + for (i = 0; i < 8; i++) + for (j = 0; j < 64; j++) { + b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf); + u_sbox[i][j] = sbox[i][b]; + } + + /* + * Convert the inverted S-boxes into 4 arrays of 8 bits. + * Each will handle 12 bits of the S-box input. + */ + for (b = 0; b < 4; b++) + for (i = 0; i < 64; i++) + for (j = 0; j < 64; j++) + m_sbox[b][(i << 6) | j] = + (u_sbox[(b << 1)][i] << 4) | + u_sbox[(b << 1) + 1][j]; + + /* + * Set up the initial & final permutations into a useful form, and + * initialise the inverted key permutation. + */ + for (i = 0; i < 64; i++) { + init_perm[final_perm[i] = IP[i] - 1] = i; + inv_key_perm[i] = 255; + } + + /* + * Invert the key permutation and initialise the inverted key + * compression permutation. + */ + for (i = 0; i < 56; i++) { + u_key_perm[i] = key_perm[i] - 1; + inv_key_perm[key_perm[i] - 1] = i; + inv_comp_perm[i] = 255; + } + + /* + * Invert the key compression permutation. + */ + for (i = 0; i < 48; i++) { + inv_comp_perm[comp_perm[i] - 1] = i; + } + + /* + * Set up the OR-mask arrays for the initial and final permutations, + * and for the key initial and compression permutations. + */ + for (k = 0; k < 8; k++) { + for (i = 0; i < 256; i++) { + *(il = &ip_maskl[k][i]) = 0; + *(ir = &ip_maskr[k][i]) = 0; + *(fl = &fp_maskl[k][i]) = 0; + *(fr = &fp_maskr[k][i]) = 0; + for (j = 0; j < 8; j++) { + inbit = 8 * k + j; + if (i & bits8[j]) { + if ((obit = init_perm[inbit]) < 32) + *il |= bits32[obit]; + else + *ir |= bits32[obit-32]; + if ((obit = final_perm[inbit]) < 32) + *fl |= bits32[obit]; + else + *fr |= bits32[obit - 32]; + } + } + } + for (i = 0; i < 128; i++) { + *(il = &key_perm_maskl[k][i]) = 0; + *(ir = &key_perm_maskr[k][i]) = 0; + for (j = 0; j < 7; j++) { + inbit = 8 * k + j; + if (i & bits8[j + 1]) { + if ((obit = inv_key_perm[inbit]) == 255) + continue; + if (obit < 28) + *il |= bits28[obit]; + else + *ir |= bits28[obit - 28]; + } + } + *(il = &comp_maskl[k][i]) = 0; + *(ir = &comp_maskr[k][i]) = 0; + for (j = 0; j < 7; j++) { + inbit = 7 * k + j; + if (i & bits8[j + 1]) { + if ((obit=inv_comp_perm[inbit]) == 255) + continue; + if (obit < 24) + *il |= bits24[obit]; + else + *ir |= bits24[obit - 24]; + } + } + } + } + + /* + * Invert the P-box permutation, and convert into OR-masks for + * handling the output of the S-box arrays setup above. + */ + for (i = 0; i < 32; i++) + un_pbox[pbox[i] - 1] = i; + + for (b = 0; b < 4; b++) + for (i = 0; i < 256; i++) { + *(p = &psbox[b][i]) = 0; + for (j = 0; j < 8; j++) { + if (i & bits8[j]) + *p |= bits32[un_pbox[8 * b + j]]; + } + } + + des_initialised = 1; +} + +static void +setup_salt(salt) + int32_t salt; +{ + u_int32_t obit, saltbit; + int i; + + if (salt == old_salt) + return; + old_salt = salt; + + saltbits = 0; + saltbit = 1; + obit = 0x800000; + for (i = 0; i < 24; i++) { + if (salt & saltbit) + saltbits |= obit; + saltbit <<= 1; + obit >>= 1; + } +} + +static int +des_setkey(key) + const char *key; +{ + u_int32_t k0, k1, rawkey0, rawkey1; + int shifts, round; + + if (!des_initialised) + des_init(); + + rawkey0 = ntohl(*(u_int32_t *) key); + rawkey1 = ntohl(*(u_int32_t *) (key + 4)); + + if ((rawkey0 | rawkey1) + && rawkey0 == old_rawkey0 + && rawkey1 == old_rawkey1) { + /* + * Already setup for this key. + * This optimisation fails on a zero key (which is weak and + * has bad parity anyway) in order to simplify the starting + * conditions. + */ + return(0); + } + old_rawkey0 = rawkey0; + old_rawkey1 = rawkey1; + + /* + * Do key permutation and split into two 28-bit subkeys. + */ + k0 = key_perm_maskl[0][rawkey0 >> 25] + | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskl[4][rawkey1 >> 25] + | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; + k1 = key_perm_maskr[0][rawkey0 >> 25] + | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskr[4][rawkey1 >> 25] + | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; + /* + * Rotate subkeys and do compression permutation. + */ + shifts = 0; + for (round = 0; round < 16; round++) { + u_int32_t t0, t1; + + shifts += key_shifts[round]; + + t0 = (k0 << shifts) | (k0 >> (28 - shifts)); + t1 = (k1 << shifts) | (k1 >> (28 - shifts)); + + de_keysl[15 - round] = + en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] + | comp_maskl[1][(t0 >> 14) & 0x7f] + | comp_maskl[2][(t0 >> 7) & 0x7f] + | comp_maskl[3][t0 & 0x7f] + | comp_maskl[4][(t1 >> 21) & 0x7f] + | comp_maskl[5][(t1 >> 14) & 0x7f] + | comp_maskl[6][(t1 >> 7) & 0x7f] + | comp_maskl[7][t1 & 0x7f]; + + de_keysr[15 - round] = + en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] + | comp_maskr[1][(t0 >> 14) & 0x7f] + | comp_maskr[2][(t0 >> 7) & 0x7f] + | comp_maskr[3][t0 & 0x7f] + | comp_maskr[4][(t1 >> 21) & 0x7f] + | comp_maskr[5][(t1 >> 14) & 0x7f] + | comp_maskr[6][(t1 >> 7) & 0x7f] + | comp_maskr[7][t1 & 0x7f]; + } + return(0); +} + +static int +do_des(l_in, r_in, l_out, r_out, count) + u_int32_t l_in, r_in, *l_out, *r_out; + int count; +{ + /* + * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. + */ + u_int32_t l, r, *kl, *kr, *kl1, *kr1; + u_int32_t f, r48l, r48r; + int round; + + if (count == 0) { + return(1); + } else if (count > 0) { + /* + * Encrypting + */ + kl1 = en_keysl; + kr1 = en_keysr; + } else { + /* + * Decrypting + */ + count = -count; + kl1 = de_keysl; + kr1 = de_keysr; + } + + /* + * Do initial permutation (IP). + */ + l = ip_maskl[0][l_in >> 24] + | ip_maskl[1][(l_in >> 16) & 0xff] + | ip_maskl[2][(l_in >> 8) & 0xff] + | ip_maskl[3][l_in & 0xff] + | ip_maskl[4][r_in >> 24] + | ip_maskl[5][(r_in >> 16) & 0xff] + | ip_maskl[6][(r_in >> 8) & 0xff] + | ip_maskl[7][r_in & 0xff]; + r = ip_maskr[0][l_in >> 24] + | ip_maskr[1][(l_in >> 16) & 0xff] + | ip_maskr[2][(l_in >> 8) & 0xff] + | ip_maskr[3][l_in & 0xff] + | ip_maskr[4][r_in >> 24] + | ip_maskr[5][(r_in >> 16) & 0xff] + | ip_maskr[6][(r_in >> 8) & 0xff] + | ip_maskr[7][r_in & 0xff]; + + while (count--) { + /* + * Do each round. + */ + kl = kl1; + kr = kr1; + round = 16; + while (round--) { + /* + * Expand R to 48 bits (simulate the E-box). + */ + r48l = ((r & 0x00000001) << 23) + | ((r & 0xf8000000) >> 9) + | ((r & 0x1f800000) >> 11) + | ((r & 0x01f80000) >> 13) + | ((r & 0x001f8000) >> 15); + + r48r = ((r & 0x0001f800) << 7) + | ((r & 0x00001f80) << 5) + | ((r & 0x000001f8) << 3) + | ((r & 0x0000001f) << 1) + | ((r & 0x80000000) >> 31); + /* + * Do salting for crypt() and friends, and + * XOR with the permuted key. + */ + f = (r48l ^ r48r) & saltbits; + r48l ^= f ^ *kl++; + r48r ^= f ^ *kr++; + /* + * Do sbox lookups (which shrink it back to 32 bits) + * and do the pbox permutation at the same time. + */ + f = psbox[0][m_sbox[0][r48l >> 12]] + | psbox[1][m_sbox[1][r48l & 0xfff]] + | psbox[2][m_sbox[2][r48r >> 12]] + | psbox[3][m_sbox[3][r48r & 0xfff]]; + /* + * Now that we've permuted things, complete f(). + */ + f ^= l; + l = r; + r = f; + } + r = l; + l = f; + } + /* + * Do final permutation (inverse of IP). + */ + *l_out = fp_maskl[0][l >> 24] + | fp_maskl[1][(l >> 16) & 0xff] + | fp_maskl[2][(l >> 8) & 0xff] + | fp_maskl[3][l & 0xff] + | fp_maskl[4][r >> 24] + | fp_maskl[5][(r >> 16) & 0xff] + | fp_maskl[6][(r >> 8) & 0xff] + | fp_maskl[7][r & 0xff]; + *r_out = fp_maskr[0][l >> 24] + | fp_maskr[1][(l >> 16) & 0xff] + | fp_maskr[2][(l >> 8) & 0xff] + | fp_maskr[3][l & 0xff] + | fp_maskr[4][r >> 24] + | fp_maskr[5][(r >> 16) & 0xff] + | fp_maskr[6][(r >> 8) & 0xff] + | fp_maskr[7][r & 0xff]; + return(0); +} + +static int +des_cipher(in, out, salt, count) + const char *in; + char *out; + int32_t salt; + int count; +{ + u_int32_t l_out, r_out, rawl, rawr; + u_int32_t x[2]; + int retval; + + if (!des_initialised) + des_init(); + + setup_salt(salt); + + memcpy(x, in, sizeof x); + rawl = ntohl(x[0]); + rawr = ntohl(x[1]); + retval = do_des(rawl, rawr, &l_out, &r_out, count); + + x[0] = htonl(l_out); + x[1] = htonl(r_out); + memcpy(out, x, sizeof x); + return(retval); +} + +char * +crypt(key, setting) + const char *key; + const char *setting; +{ + int i; + u_int32_t count, salt, l, r0, r1, keybuf[2]; + u_char *p, *q; + static u_char output[21]; + extern char *md5crypt __P((const char *, const char *)); + extern char *bcrypt __P((const char *, const char *)); + + if (setting[0] == '$') { + switch (setting[1]) { + case '1': + return (md5crypt(key, setting)); + default: + return bcrypt(key, setting); + } + } + + if (!des_initialised) + des_init(); + + /* + * Copy the key, shifting each character up by one bit + * and padding with zeros. + */ + q = (u_char *) keybuf; + while ((q - (u_char *) keybuf) < sizeof(keybuf)) { + if ((*q++ = *key << 1)) + key++; + } + if (des_setkey((u_char *) keybuf)) + return(NULL); + + if (*setting == _PASSWORD_EFMT1) { + /* + * "new"-style: + * setting - underscore, 4 bytes of count, 4 bytes of salt + * key - unlimited characters + */ + for (i = 1, count = 0; i < 5; i++) + count |= ascii_to_bin(setting[i]) << (i - 1) * 6; + + for (i = 5, salt = 0; i < 9; i++) + salt |= ascii_to_bin(setting[i]) << (i - 5) * 6; + + while (*key) { + /* + * Encrypt the key with itself. + */ + if (des_cipher((u_char*)keybuf, (u_char*)keybuf, 0, 1)) + return(NULL); + /* + * And XOR with the next 8 characters of the key. + */ + q = (u_char *) keybuf; + while (((q - (u_char *) keybuf) < sizeof(keybuf)) && + *key) + *q++ ^= *key++ << 1; + + if (des_setkey((u_char *) keybuf)) + return(NULL); + } + strncpy((char *)output, setting, 9); + + /* + * Double check that we weren't given a short setting. + * If we were, the above code will probably have created + * wierd values for count and salt, but we don't really care. + * Just make sure the output string doesn't have an extra + * NUL in it. + */ + output[9] = '\0'; + p = output + strlen((const char *)output); + } else { + /* + * "old"-style: + * setting - 2 bytes of salt + * key - up to 8 characters + */ + count = 25; + + salt = (ascii_to_bin(setting[1]) << 6) + | ascii_to_bin(setting[0]); + + output[0] = setting[0]; + /* + * If the encrypted password that the salt was extracted from + * is only 1 character long, the salt will be corrupted. We + * need to ensure that the output string doesn't have an extra + * NUL in it! + */ + output[1] = setting[1] ? setting[1] : output[0]; + + p = output + 2; + } + setup_salt(salt); + /* + * Do it. + */ + if (do_des(0, 0, &r0, &r1, count)) + return(NULL); + /* + * Now encode the result... + */ + l = (r0 >> 8); + *p++ = ascii64[(l >> 18) & 0x3f]; + *p++ = ascii64[(l >> 12) & 0x3f]; + *p++ = ascii64[(l >> 6) & 0x3f]; + *p++ = ascii64[l & 0x3f]; + + l = (r0 << 16) | ((r1 >> 16) & 0xffff); + *p++ = ascii64[(l >> 18) & 0x3f]; + *p++ = ascii64[(l >> 12) & 0x3f]; + *p++ = ascii64[(l >> 6) & 0x3f]; + *p++ = ascii64[l & 0x3f]; + + l = r1 << 2; + *p++ = ascii64[(l >> 12) & 0x3f]; + *p++ = ascii64[(l >> 6) & 0x3f]; + *p++ = ascii64[l & 0x3f]; + *p = 0; + + return((char *)output); +} 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 @@ +/* $OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp $ */ + +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp + * ---------------------------------------------------------------------------- + * + * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $ + * + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +static void to64 __P((char *, u_int32_t, int)); + +static void +to64(s, v, n) + char *s; + u_int32_t v; + int n; +{ + while (--n >= 0) { + *s++ = itoa64[v&0x3f]; + v >>= 6; + } +} + +/* + * UNIX password + * + * Use MD5 for what it is best at... + */ + +char * +md5crypt(pw, salt) + register const char *pw; + register const char *salt; +{ + /* + * This string is magic for this algorithm. Having + * it this way, we can get get better later on + */ + static unsigned char *magic = (unsigned char *)"$1$"; + + static char passwd[120], *p; + static const unsigned char *sp,*ep; + unsigned char final[16]; + int sl,pl,i; + MD5_CTX ctx,ctx1; + u_int32_t l; + + /* Refine the Salt first */ + sp = (const unsigned char *)salt; + + /* If it starts with the magic string, then skip that */ + if(!strncmp((const char *)sp,(const char *)magic,strlen((const char *)magic))) + sp += strlen((const char *)magic); + + /* It stops at the first '$', max 8 chars */ + for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) + continue; + + /* get the length of the true salt */ + sl = ep - sp; + + MD5Init(&ctx); + + /* The password first, since that is what is most unknown */ + MD5Update(&ctx,(const unsigned char *)pw,strlen(pw)); + + /* Then our magic string */ + MD5Update(&ctx,magic,strlen((const char *)magic)); + + /* Then the raw salt */ + MD5Update(&ctx,sp,sl); + + /* Then just as many characters of the MD5(pw,salt,pw) */ + MD5Init(&ctx1); + MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); + MD5Update(&ctx1,sp,sl); + MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); + MD5Final(final,&ctx1); + for(pl = strlen(pw); pl > 0; pl -= 16) + MD5Update(&ctx,final,pl>16 ? 16 : pl); + + /* Don't leave anything around in vm they could use. */ + memset(final,0,sizeof final); + + /* Then something really weird... */ + for (i = strlen(pw); i ; i >>= 1) + if(i&1) + MD5Update(&ctx, final, 1); + else + MD5Update(&ctx, (const unsigned char *)pw, 1); + + /* Now make the output string */ + strcpy(passwd,(const char *)magic); + strncat(passwd,(const char *)sp,sl); + strcat(passwd,"$"); + + MD5Final(final,&ctx); + + /* + * and now, just to make sure things don't run too fast + * On a 60 Mhz Pentium this takes 34 msec, so you would + * need 30 seconds to build a 1000 entry dictionary... + */ + for(i=0;i<1000;i++) { + MD5Init(&ctx1); + if(i & 1) + MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); + else + MD5Update(&ctx1,final,16); + + if(i % 3) + MD5Update(&ctx1,sp,sl); + + if(i % 7) + MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); + + if(i & 1) + MD5Update(&ctx1,final,16); + else + MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); + MD5Final(final,&ctx1); + } + + p = passwd + strlen(passwd); + + l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4; + l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4; + l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4; + l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4; + l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4; + l = final[11] ; to64(p,l,2); p += 2; + *p = '\0'; + + /* Don't leave anything around in vm they could use. */ + memset(final,0,sizeof final); + + return passwd; +} + 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 @@ +/* $OpenBSD: morecrypt.c,v 1.9 1998/03/22 19:01:20 niklas Exp $ */ + +/* + * FreeSec: libcrypt + * + * Copyright (c) 1994 David Burren + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the author nor the names of other contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * This is an original implementation of the DES and the crypt(3) interfaces + * by David Burren . + * + * An excellent reference on the underlying algorithm (and related + * algorithms) is: + * + * B. Schneier, Applied Cryptography: protocols, algorithms, + * and source code in C, John Wiley & Sons, 1994. + * + * Note that in that book's description of DES the lookups for the initial, + * pbox, and final permutations are inverted (this has been brought to the + * attention of the author). A list of errata for this book has been + * posted to the sci.crypt newsgroup by the author and is available for FTP. + * + * NOTE: + * This file must copy certain chunks of crypt.c for legal reasons. + * crypt.c can only export the interface crypt(), to make binaries + * exportable from the USA. Hence, to also have the other crypto interfaces + * available we have to copy pieces... + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: morecrypt.c,v 1.9 1998/03/22 19:01:20 niklas Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#ifdef DEBUG +# include +#endif + +static u_char IP[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 +}; + +static u_char inv_key_perm[64]; +static u_char u_key_perm[56]; +static u_char key_perm[56] = { + 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 +}; + +static u_char key_shifts[16] = { + 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 +}; + +static u_char inv_comp_perm[56]; +static u_char comp_perm[48] = { + 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 +}; + +/* + * No E box is used, as it's replaced by some ANDs, shifts, and ORs. + */ + +static u_char u_sbox[8][64]; +static u_char sbox[8][64] = { + { + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 + }, + { + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 + }, + { + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 + }, + { + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 + }, + { + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 + }, + { + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 + }, + { + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 + }, + { + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 + } +}; + +static u_char un_pbox[32]; +static u_char pbox[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +}; + +static u_int32_t bits32[32] = +{ + 0x80000000, 0x40000000, 0x20000000, 0x10000000, + 0x08000000, 0x04000000, 0x02000000, 0x01000000, + 0x00800000, 0x00400000, 0x00200000, 0x00100000, + 0x00080000, 0x00040000, 0x00020000, 0x00010000, + 0x00008000, 0x00004000, 0x00002000, 0x00001000, + 0x00000800, 0x00000400, 0x00000200, 0x00000100, + 0x00000080, 0x00000040, 0x00000020, 0x00000010, + 0x00000008, 0x00000004, 0x00000002, 0x00000001 +}; + +static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + +static u_int32_t saltbits; +static int32_t old_salt; +static u_int32_t *bits28, *bits24; +static u_char init_perm[64], final_perm[64]; +static u_int32_t en_keysl[16], en_keysr[16]; +static u_int32_t de_keysl[16], de_keysr[16]; +static int des_initialised = 0; +static u_char m_sbox[4][4096]; +static u_int32_t psbox[4][256]; +static u_int32_t ip_maskl[8][256], ip_maskr[8][256]; +static u_int32_t fp_maskl[8][256], fp_maskr[8][256]; +static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; +static u_int32_t comp_maskl[8][128], comp_maskr[8][128]; +static u_int32_t old_rawkey0, old_rawkey1; + +static __inline int +ascii_to_bin(ch) + char ch; +{ + if (ch > 'z') + return(0); + if (ch >= 'a') + return(ch - 'a' + 38); + if (ch > 'Z') + return(0); + if (ch >= 'A') + return(ch - 'A' + 12); + if (ch > '9') + return(0); + if (ch >= '.') + return(ch - '.'); + return(0); +} + +void +des_init() +{ + int i, j, b, k, inbit, obit; + u_int32_t *p, *il, *ir, *fl, *fr; + + old_rawkey0 = old_rawkey1 = 0; + saltbits = 0; + old_salt = 0; + bits24 = (bits28 = bits32 + 4) + 4; + + /* + * Invert the S-boxes, reordering the input bits. + */ + for (i = 0; i < 8; i++) + for (j = 0; j < 64; j++) { + b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf); + u_sbox[i][j] = sbox[i][b]; + } + + /* + * Convert the inverted S-boxes into 4 arrays of 8 bits. + * Each will handle 12 bits of the S-box input. + */ + for (b = 0; b < 4; b++) + for (i = 0; i < 64; i++) + for (j = 0; j < 64; j++) + m_sbox[b][(i << 6) | j] = + (u_sbox[(b << 1)][i] << 4) | + u_sbox[(b << 1) + 1][j]; + + /* + * Set up the initial & final permutations into a useful form, and + * initialise the inverted key permutation. + */ + for (i = 0; i < 64; i++) { + init_perm[final_perm[i] = IP[i] - 1] = i; + inv_key_perm[i] = 255; + } + + /* + * Invert the key permutation and initialise the inverted key + * compression permutation. + */ + for (i = 0; i < 56; i++) { + u_key_perm[i] = key_perm[i] - 1; + inv_key_perm[key_perm[i] - 1] = i; + inv_comp_perm[i] = 255; + } + + /* + * Invert the key compression permutation. + */ + for (i = 0; i < 48; i++) { + inv_comp_perm[comp_perm[i] - 1] = i; + } + + /* + * Set up the OR-mask arrays for the initial and final permutations, + * and for the key initial and compression permutations. + */ + for (k = 0; k < 8; k++) { + for (i = 0; i < 256; i++) { + *(il = &ip_maskl[k][i]) = 0; + *(ir = &ip_maskr[k][i]) = 0; + *(fl = &fp_maskl[k][i]) = 0; + *(fr = &fp_maskr[k][i]) = 0; + for (j = 0; j < 8; j++) { + inbit = 8 * k + j; + if (i & bits8[j]) { + if ((obit = init_perm[inbit]) < 32) + *il |= bits32[obit]; + else + *ir |= bits32[obit-32]; + if ((obit = final_perm[inbit]) < 32) + *fl |= bits32[obit]; + else + *fr |= bits32[obit - 32]; + } + } + } + for (i = 0; i < 128; i++) { + *(il = &key_perm_maskl[k][i]) = 0; + *(ir = &key_perm_maskr[k][i]) = 0; + for (j = 0; j < 7; j++) { + inbit = 8 * k + j; + if (i & bits8[j + 1]) { + if ((obit = inv_key_perm[inbit]) == 255) + continue; + if (obit < 28) + *il |= bits28[obit]; + else + *ir |= bits28[obit - 28]; + } + } + *(il = &comp_maskl[k][i]) = 0; + *(ir = &comp_maskr[k][i]) = 0; + for (j = 0; j < 7; j++) { + inbit = 7 * k + j; + if (i & bits8[j + 1]) { + if ((obit=inv_comp_perm[inbit]) == 255) + continue; + if (obit < 24) + *il |= bits24[obit]; + else + *ir |= bits24[obit - 24]; + } + } + } + } + + /* + * Invert the P-box permutation, and convert into OR-masks for + * handling the output of the S-box arrays setup above. + */ + for (i = 0; i < 32; i++) + un_pbox[pbox[i] - 1] = i; + + for (b = 0; b < 4; b++) + for (i = 0; i < 256; i++) { + *(p = &psbox[b][i]) = 0; + for (j = 0; j < 8; j++) { + if (i & bits8[j]) + *p |= bits32[un_pbox[8 * b + j]]; + } + } + + des_initialised = 1; +} + +void +setup_salt(salt) + int32_t salt; +{ + u_int32_t obit, saltbit; + int i; + + if (salt == old_salt) + return; + old_salt = salt; + + saltbits = 0; + saltbit = 1; + obit = 0x800000; + for (i = 0; i < 24; i++) { + if (salt & saltbit) + saltbits |= obit; + saltbit <<= 1; + obit >>= 1; + } +} + +int +do_des(l_in, r_in, l_out, r_out, count) + u_int32_t l_in, r_in, *l_out, *r_out; + int count; +{ + /* + * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. + */ + u_int32_t l, r, *kl, *kr, *kl1, *kr1; + u_int32_t f, r48l, r48r; + int round; + + if (count == 0) { + return(1); + } else if (count > 0) { + /* + * Encrypting + */ + kl1 = en_keysl; + kr1 = en_keysr; + } else { + /* + * Decrypting + */ + count = -count; + kl1 = de_keysl; + kr1 = de_keysr; + } + + /* + * Do initial permutation (IP). + */ + l = ip_maskl[0][l_in >> 24] + | ip_maskl[1][(l_in >> 16) & 0xff] + | ip_maskl[2][(l_in >> 8) & 0xff] + | ip_maskl[3][l_in & 0xff] + | ip_maskl[4][r_in >> 24] + | ip_maskl[5][(r_in >> 16) & 0xff] + | ip_maskl[6][(r_in >> 8) & 0xff] + | ip_maskl[7][r_in & 0xff]; + r = ip_maskr[0][l_in >> 24] + | ip_maskr[1][(l_in >> 16) & 0xff] + | ip_maskr[2][(l_in >> 8) & 0xff] + | ip_maskr[3][l_in & 0xff] + | ip_maskr[4][r_in >> 24] + | ip_maskr[5][(r_in >> 16) & 0xff] + | ip_maskr[6][(r_in >> 8) & 0xff] + | ip_maskr[7][r_in & 0xff]; + + while (count--) { + /* + * Do each round. + */ + kl = kl1; + kr = kr1; + round = 16; + while (round--) { + /* + * Expand R to 48 bits (simulate the E-box). + */ + r48l = ((r & 0x00000001) << 23) + | ((r & 0xf8000000) >> 9) + | ((r & 0x1f800000) >> 11) + | ((r & 0x01f80000) >> 13) + | ((r & 0x001f8000) >> 15); + + r48r = ((r & 0x0001f800) << 7) + | ((r & 0x00001f80) << 5) + | ((r & 0x000001f8) << 3) + | ((r & 0x0000001f) << 1) + | ((r & 0x80000000) >> 31); + /* + * Do salting for crypt() and friends, and + * XOR with the permuted key. + */ + f = (r48l ^ r48r) & saltbits; + r48l ^= f ^ *kl++; + r48r ^= f ^ *kr++; + /* + * Do sbox lookups (which shrink it back to 32 bits) + * and do the pbox permutation at the same time. + */ + f = psbox[0][m_sbox[0][r48l >> 12]] + | psbox[1][m_sbox[1][r48l & 0xfff]] + | psbox[2][m_sbox[2][r48r >> 12]] + | psbox[3][m_sbox[3][r48r & 0xfff]]; + /* + * Now that we've permuted things, complete f(). + */ + f ^= l; + l = r; + r = f; + } + r = l; + l = f; + } + /* + * Do final permutation (inverse of IP). + */ + *l_out = fp_maskl[0][l >> 24] + | fp_maskl[1][(l >> 16) & 0xff] + | fp_maskl[2][(l >> 8) & 0xff] + | fp_maskl[3][l & 0xff] + | fp_maskl[4][r >> 24] + | fp_maskl[5][(r >> 16) & 0xff] + | fp_maskl[6][(r >> 8) & 0xff] + | fp_maskl[7][r & 0xff]; + *r_out = fp_maskr[0][l >> 24] + | fp_maskr[1][(l >> 16) & 0xff] + | fp_maskr[2][(l >> 8) & 0xff] + | fp_maskr[3][l & 0xff] + | fp_maskr[4][r >> 24] + | fp_maskr[5][(r >> 16) & 0xff] + | fp_maskr[6][(r >> 8) & 0xff] + | fp_maskr[7][r & 0xff]; + return(0); +} + +int +des_cipher(in, out, salt, count) + const char *in; + char *out; + long salt; + int count; +{ + u_int32_t l_out, r_out, rawl, rawr; + u_int32_t x[2]; + int retval; + + if (!des_initialised) + des_init(); + + setup_salt((int32_t)salt); + + memcpy(x, in, sizeof x); + rawl = ntohl(x[0]); + rawr = ntohl(x[1]); + retval = do_des(rawl, rawr, &l_out, &r_out, count); + + x[0] = htonl(l_out); + x[1] = htonl(r_out); + memcpy(out, x, sizeof x); + return(retval); +} + +int +des_setkey(key) + const char *key; +{ + u_int32_t k0, k1, rawkey0, rawkey1; + int shifts, round; + + if (!des_initialised) + des_init(); + + rawkey0 = ntohl(*(u_int32_t *) key); + rawkey1 = ntohl(*(u_int32_t *) (key + 4)); + + if ((rawkey0 | rawkey1) + && rawkey0 == old_rawkey0 + && rawkey1 == old_rawkey1) { + /* + * Already setup for this key. + * This optimisation fails on a zero key (which is weak and + * has bad parity anyway) in order to simplify the starting + * conditions. + */ + return(0); + } + old_rawkey0 = rawkey0; + old_rawkey1 = rawkey1; + + /* + * Do key permutation and split into two 28-bit subkeys. + */ + k0 = key_perm_maskl[0][rawkey0 >> 25] + | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskl[4][rawkey1 >> 25] + | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; + k1 = key_perm_maskr[0][rawkey0 >> 25] + | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] + | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] + | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] + | key_perm_maskr[4][rawkey1 >> 25] + | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] + | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] + | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; + /* + * Rotate subkeys and do compression permutation. + */ + shifts = 0; + for (round = 0; round < 16; round++) { + u_int32_t t0, t1; + + shifts += key_shifts[round]; + + t0 = (k0 << shifts) | (k0 >> (28 - shifts)); + t1 = (k1 << shifts) | (k1 >> (28 - shifts)); + + de_keysl[15 - round] = + en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] + | comp_maskl[1][(t0 >> 14) & 0x7f] + | comp_maskl[2][(t0 >> 7) & 0x7f] + | comp_maskl[3][t0 & 0x7f] + | comp_maskl[4][(t1 >> 21) & 0x7f] + | comp_maskl[5][(t1 >> 14) & 0x7f] + | comp_maskl[6][(t1 >> 7) & 0x7f] + | comp_maskl[7][t1 & 0x7f]; + + de_keysr[15 - round] = + en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] + | comp_maskr[1][(t0 >> 14) & 0x7f] + | comp_maskr[2][(t0 >> 7) & 0x7f] + | comp_maskr[3][t0 & 0x7f] + | comp_maskr[4][(t1 >> 21) & 0x7f] + | comp_maskr[5][(t1 >> 14) & 0x7f] + | comp_maskr[6][(t1 >> 7) & 0x7f] + | comp_maskr[7][t1 & 0x7f]; + } + return(0); +} + +int +setkey(key) + const char *key; +{ + int i, j; + u_int32_t packed_keys[2]; + u_char *p; + + p = (u_char *) packed_keys; + + for (i = 0; i < 8; i++) { + p[i] = 0; + for (j = 0; j < 8; j++) + if (*key++ & 1) + p[i] |= bits8[j]; + } + return(des_setkey(p)); +} + +int +encrypt(block, flag) + char *block; + int flag; +{ + u_int32_t io[2]; + u_char *p; + int i, j, retval; + + if (!des_initialised) + des_init(); + + setup_salt((int32_t)0); + p = (u_char *)block; + for (i = 0; i < 2; i++) { + io[i] = 0L; + for (j = 0; j < 32; j++) + if (*p++ & 1) + io[i] |= bits32[j]; + } + retval = do_des(io[0], io[1], io, io + 1, flag ? -1 : 1); + for (i = 0; i < 2; i++) + for (j = 0; j < 32; j++) + block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0; + return(retval); +} diff --git a/src/lib/libc/crypt/skipjack.c b/src/lib/libc/crypt/skipjack.c new file mode 100644 index 0000000000..e700f40c39 --- /dev/null +++ b/src/lib/libc/crypt/skipjack.c @@ -0,0 +1,258 @@ +/* + * Further optimized test implementation of SKIPJACK algorithm + * Mark Tillotson , 25 June 98 + * Optimizations suit RISC (lots of registers) machine best. + * + * based on unoptimized implementation of + * Panu Rissanen 960624 + * + * SKIPJACK and KEA Algorithm Specifications + * Version 2.0 + * 29 May 1998 +*/ + +#include +#include +#include + +static const u_int8_t ftable[0x100] = +{ + 0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, + 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, + 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, + 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, + 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, + 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, + 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, + 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, + 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, + 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, + 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, + 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, + 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, + 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, + 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, + 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, + 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, + 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, + 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, + 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, + 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, + 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, + 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, + 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, + 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, + 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, + 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, + 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, + 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, + 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, + 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, + 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46 +}; + +/* + * For each key byte generate a table to represent the function + * ftable [in ^ keybyte] + * + * These tables used to save an XOR in each stage of the G-function + * the tables are hopefully pointed to by register allocated variables + * k0, k1..k9 + */ +void +subkey_table_gen (u_int8_t *key, u_int8_t **key_tables) +{ + int i, k; + + for (k = 0; k < 10; k++) { + u_int8_t key_byte = key[k]; + u_int8_t * table = (u_int8_t *) malloc(0x100); + /* XXX */ + + key_tables[k] = table; + for (i = 0; i < 0x100; i++) + table[i] = ftable[i ^ key_byte]; + } +} + + +#define g(k0, k1, k2, k3, ih, il, oh, ol) \ +{ \ + oh = k##k0 [il] ^ ih; \ + ol = k##k1 [oh] ^ il; \ + oh = k##k2 [ol] ^ oh; \ + ol = k##k3 [oh] ^ ol; \ +} + +#define g0(ih, il, oh, ol) g(0, 1, 2, 3, ih, il, oh, ol) +#define g4(ih, il, oh, ol) g(4, 5, 6, 7, ih, il, oh, ol) +#define g8(ih, il, oh, ol) g(8, 9, 0, 1, ih, il, oh, ol) +#define g2(ih, il, oh, ol) g(2, 3, 4, 5, ih, il, oh, ol) +#define g6(ih, il, oh, ol) g(6, 7, 8, 9, ih, il, oh, ol) + + +#define g_inv(k0, k1, k2, k3, ih, il, oh, ol) \ +{ \ + ol = k##k3 [ih] ^ il; \ + oh = k##k2 [ol] ^ ih; \ + ol = k##k1 [oh] ^ ol; \ + oh = k##k0 [ol] ^ oh; \ +} + + +#define g0_inv(ih, il, oh, ol) g_inv(0, 1, 2, 3, ih, il, oh, ol) +#define g4_inv(ih, il, oh, ol) g_inv(4, 5, 6, 7, ih, il, oh, ol) +#define g8_inv(ih, il, oh, ol) g_inv(8, 9, 0, 1, ih, il, oh, ol) +#define g2_inv(ih, il, oh, ol) g_inv(2, 3, 4, 5, ih, il, oh, ol) +#define g6_inv(ih, il, oh, ol) g_inv(6, 7, 8, 9, ih, il, oh, ol) + +/* optimized version of Skipjack algorithm + * + * the appropriate g-function is inlined for each round + * + * the data movement is minimized by rotating the names of the + * variables w1..w4, not their contents (saves 3 moves per round) + * + * the loops are completely unrolled (needed to staticize choice of g) + * + * compiles to about 470 instructions on a Sparc (gcc -O) + * which is about 58 instructions per byte, 14 per round. + * gcc seems to leave in some unnecessary and with 0xFF operations + * but only in the latter part of the functions. Perhaps it + * runs out of resources to properly optimize long inlined function? + * in theory should get about 11 instructions per round, not 14 + */ + +void +skipjack_forwards(u_int8_t *plain, u_int8_t *cipher, u_int8_t **key_tables) +{ + u_int8_t wh1 = plain[0]; u_int8_t wl1 = plain[1]; + u_int8_t wh2 = plain[2]; u_int8_t wl2 = plain[3]; + u_int8_t wh3 = plain[4]; u_int8_t wl3 = plain[5]; + u_int8_t wh4 = plain[6]; u_int8_t wl4 = plain[7]; + + u_int8_t * k0 = key_tables [0]; + u_int8_t * k1 = key_tables [1]; + u_int8_t * k2 = key_tables [2]; + u_int8_t * k3 = key_tables [3]; + u_int8_t * k4 = key_tables [4]; + u_int8_t * k5 = key_tables [5]; + u_int8_t * k6 = key_tables [6]; + u_int8_t * k7 = key_tables [7]; + u_int8_t * k8 = key_tables [8]; + u_int8_t * k9 = key_tables [9]; + + /* first 8 rounds */ + g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 1; wh4 ^= wh1; + g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 2; wh3 ^= wh4; + g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 3; wh2 ^= wh3; + g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 4; wh1 ^= wh2; + g6 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 5; wh4 ^= wh1; + g0 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 6; wh3 ^= wh4; + g4 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 7; wh2 ^= wh3; + g8 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 8; wh1 ^= wh2; + + /* second 8 rounds */ + wh2 ^= wh1; wl2 ^= wl1 ^ 9 ; g2 (wh1,wl1, wh1,wl1); + wh1 ^= wh4; wl1 ^= wl4 ^ 10; g6 (wh4,wl4, wh4,wl4); + wh4 ^= wh3; wl4 ^= wl3 ^ 11; g0 (wh3,wl3, wh3,wl3); + wh3 ^= wh2; wl3 ^= wl2 ^ 12; g4 (wh2,wl2, wh2,wl2); + wh2 ^= wh1; wl2 ^= wl1 ^ 13; g8 (wh1,wl1, wh1,wl1); + wh1 ^= wh4; wl1 ^= wl4 ^ 14; g2 (wh4,wl4, wh4,wl4); + wh4 ^= wh3; wl4 ^= wl3 ^ 15; g6 (wh3,wl3, wh3,wl3); + wh3 ^= wh2; wl3 ^= wl2 ^ 16; g0 (wh2,wl2, wh2,wl2); + + /* third 8 rounds */ + g4 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 17; wh4 ^= wh1; + g8 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 18; wh3 ^= wh4; + g2 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 19; wh2 ^= wh3; + g6 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 20; wh1 ^= wh2; + g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 21; wh4 ^= wh1; + g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 22; wh3 ^= wh4; + g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 23; wh2 ^= wh3; + g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 24; wh1 ^= wh2; + + /* last 8 rounds */ + wh2 ^= wh1; wl2 ^= wl1 ^ 25; g6 (wh1,wl1, wh1,wl1); + wh1 ^= wh4; wl1 ^= wl4 ^ 26; g0 (wh4,wl4, wh4,wl4); + wh4 ^= wh3; wl4 ^= wl3 ^ 27; g4 (wh3,wl3, wh3,wl3); + wh3 ^= wh2; wl3 ^= wl2 ^ 28; g8 (wh2,wl2, wh2,wl2); + wh2 ^= wh1; wl2 ^= wl1 ^ 29; g2 (wh1,wl1, wh1,wl1); + wh1 ^= wh4; wl1 ^= wl4 ^ 30; g6 (wh4,wl4, wh4,wl4); + wh4 ^= wh3; wl4 ^= wl3 ^ 31; g0 (wh3,wl3, wh3,wl3); + wh3 ^= wh2; wl3 ^= wl2 ^ 32; g4 (wh2,wl2, wh2,wl2); + + /* pack into byte vector */ + cipher [0] = wh1; cipher [1] = wl1; + cipher [2] = wh2; cipher [3] = wl2; + cipher [4] = wh3; cipher [5] = wl3; + cipher [6] = wh4; cipher [7] = wl4; +} + + +void +skipjack_backwards (u_int8_t *cipher, u_int8_t *plain, u_int8_t **key_tables) +{ + /* setup 4 16-bit portions */ + u_int8_t wh1 = cipher[0]; u_int8_t wl1 = cipher[1]; + u_int8_t wh2 = cipher[2]; u_int8_t wl2 = cipher[3]; + u_int8_t wh3 = cipher[4]; u_int8_t wl3 = cipher[5]; + u_int8_t wh4 = cipher[6]; u_int8_t wl4 = cipher[7]; + + u_int8_t * k0 = key_tables [0]; + u_int8_t * k1 = key_tables [1]; + u_int8_t * k2 = key_tables [2]; + u_int8_t * k3 = key_tables [3]; + u_int8_t * k4 = key_tables [4]; + u_int8_t * k5 = key_tables [5]; + u_int8_t * k6 = key_tables [6]; + u_int8_t * k7 = key_tables [7]; + u_int8_t * k8 = key_tables [8]; + u_int8_t * k9 = key_tables [9]; + + /* first 8 rounds */ + g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 32; wh3 ^= wh2; + g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 31; wh4 ^= wh3; + g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 30; wh1 ^= wh4; + g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 29; wh2 ^= wh1; + g8_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 28; wh3 ^= wh2; + g4_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 27; wh4 ^= wh3; + g0_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 26; wh1 ^= wh4; + g6_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 25; wh2 ^= wh1; + + /* second 8 rounds */ + wh1 ^= wh2; wl1 ^= wl2 ^ 24; g2_inv (wh2,wl2, wh2,wl2); + wh2 ^= wh3; wl2 ^= wl3 ^ 23; g8_inv (wh3,wl3, wh3,wl3); + wh3 ^= wh4; wl3 ^= wl4 ^ 22; g4_inv (wh4,wl4, wh4,wl4); + wh4 ^= wh1; wl4 ^= wl1 ^ 21; g0_inv (wh1,wl1, wh1,wl1); + wh1 ^= wh2; wl1 ^= wl2 ^ 20; g6_inv (wh2,wl2, wh2,wl2); + wh2 ^= wh3; wl2 ^= wl3 ^ 19; g2_inv (wh3,wl3, wh3,wl3); + wh3 ^= wh4; wl3 ^= wl4 ^ 18; g8_inv (wh4,wl4, wh4,wl4); + wh4 ^= wh1; wl4 ^= wl1 ^ 17; g4_inv (wh1,wl1, wh1,wl1); + + /* third 8 rounds */ + g0_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 16; wh3 ^= wh2; + g6_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 15; wh4 ^= wh3; + g2_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 14; wh1 ^= wh4; + g8_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 13; wh2 ^= wh1; + g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 12; wh3 ^= wh2; + g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 11; wh4 ^= wh3; + g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 10; wh1 ^= wh4; + g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 9; wh2 ^= wh1; + + /* last 8 rounds */ + wh1 ^= wh2; wl1 ^= wl2 ^ 8; g8_inv (wh2,wl2, wh2,wl2); + wh2 ^= wh3; wl2 ^= wl3 ^ 7; g4_inv (wh3,wl3, wh3,wl3); + wh3 ^= wh4; wl3 ^= wl4 ^ 6; g0_inv (wh4,wl4, wh4,wl4); + wh4 ^= wh1; wl4 ^= wl1 ^ 5; g6_inv (wh1,wl1, wh1,wl1); + wh1 ^= wh2; wl1 ^= wl2 ^ 4; g2_inv (wh2,wl2, wh2,wl2); + wh2 ^= wh3; wl2 ^= wl3 ^ 3; g8_inv (wh3,wl3, wh3,wl3); + wh3 ^= wh4; wl3 ^= wl4 ^ 2; g4_inv (wh4,wl4, wh4,wl4); + wh4 ^= wh1; wl4 ^= wl1 ^ 1; g0_inv (wh1,wl1, wh1,wl1); + + /* pack into byte vector */ + plain [0] = wh1; plain [1] = wl1; + plain [2] = wh2; plain [3] = wl2; + plain [4] = wh3; plain [5] = wl3; + plain [6] = wh4; plain [7] = wl4; +} diff --git a/src/lib/libc/include/namespace.h b/src/lib/libc/include/namespace.h new file mode 100644 index 0000000000..4a51f15ddf --- /dev/null +++ b/src/lib/libc/include/namespace.h @@ -0,0 +1,18 @@ +/* $OpenBSD: namespace.h,v 1.2 1996/08/19 08:28:08 tholo Exp $ */ + +#define catclose _catclose +#define catgets _catgets +#define catopen _catopen +#define err _err +#define errx _errx +#define strtoq _strtoq +#define strtouq _strtouq +#define sys_errlist _sys_errlist +#define sys_nerr _sys_nerr +#define sys_siglist _sys_siglist +#define verr _verr +#define verrx _verrx +#define vwarn _vwarn +#define vwarnx _vwarnx +#define warn _warn +#define warnx _warnx diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h new file mode 100644 index 0000000000..0027468269 --- /dev/null +++ b/src/lib/libc/include/thread_private.h @@ -0,0 +1,179 @@ +/* + * + * Support for thread-safety in libc and libc_r common code using macros + * to declare thread-safe data structures. + * + * $OpenBSD: thread_private.h,v 1.2 1999/01/06 05:19:32 d Exp $ + */ + +#ifndef _THREAD_PRIVATE_H_ +#define _THREAD_PRIVATE_H_ + +/* + * Parts of this file are + * Copyright (c) 1998 John Birrell . + * All rights reserved. + * + * $Id: thread_private.h,v 1.2 1999/01/06 05:19:32 d Exp $ + * $OpenBSD: thread_private.h,v 1.2 1999/01/06 05:19:32 d Exp $ + */ + +/* + * This global flag is non-zero when a process has created one + * or more threads. It is used to avoid calling locking functions + * when they are not required. In libc, this is always assumed + * to be zero. + */ + +extern volatile int __isthreaded; + +#ifdef _THREAD_SAFE + +#include +#include "pthread_private.h" + +/* + * File lock contention is difficult to diagnose without knowing + * where locks were set. Allow a debug library to be built which + * records the source file and line number of each lock call. + */ +#ifdef _FLOCK_DEBUG +#define _FLOCKFILE(x) _flockfile_debug(x, __FILE__, __LINE__) +#else +#define _FLOCKFILE(x) flockfile(x) +#endif + +/* + * These macros help in making persistent storage thread-specific. + * Libc makes extensive use of private static data structures + * that hold state across function invocation, and these macros + * are no-ops when _THREAD_SAFE is not defined. + * In a thread-safe library, the static variables are used only for + * initialising the per-thread instances of the state variables. + */ + +/* + * Give names to the private variables used to hold per-thread + * data structures. + */ +#ifdef __STDC__ +#define __THREAD_MUTEXP_NAME(name) _thread_mutexp_inst__ ## name +#define __THREAD_MUTEX_NAME(name) _thread_mutex_inst__ ## name +#define __THREAD_KEY_NAME(name) _thread_key_inst__ ## name +#else +#define __THREAD_MUTEXP_NAME(name) _thread_mutexp_inst__/**/name +#define __THREAD_MUTEX_NAME(name) _thread_mutex_inst__/**/name +#define __THREAD_KEY_NAME(name) _thread_key_inst__/**/name +#endif + +/* + * Mutex declare, lock and unlock macros. + */ +#define _THREAD_PRIVATE_MUTEX(name) \ + static struct pthread_mutex __THREAD_MUTEXP_NAME(name) = \ + PTHREAD_MUTEX_STATIC_INITIALIZER; \ + static pthread_mutex_t __THREAD_MUTEX_NAME(name) = \ + &__THREAD_MUTEXP_NAME(name); + +#define _THREAD_PRIVATE_MUTEX_LOCK(name) \ + pthread_mutex_lock(&__THREAD_MUTEX_NAME(name)) + +#define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \ + pthread_mutex_unlock(&__THREAD_MUTEX_NAME(name)) + +/* + * A mutexed data structure used to hold the persistent state's key. + */ +struct _thread_private_key_struct { + struct pthread_mutex lockd; + pthread_mutex_t lock; + int init; + pthread_key_t key; +}; + +/* + * Declaration of a per-thread state key. + */ +#define _THREAD_PRIVATE_KEY(name) \ + static volatile struct _thread_private_key_struct \ + __THREAD_KEY_NAME(name) = { \ + PTHREAD_MUTEX_STATIC_INITIALIZER, \ + &__THREAD_KEY_NAME(name).lockd, \ + 0 \ + }; + +/* + * Initialisation of storage space for a per-thread state variable. + * A pointer to a per-thread *copy* of the _initv parameter is returned. + * It calls malloc the first time and the space is automatically free'd + * when the thread dies. If you need something a bit more complicated + * than free() you will need to roll-your-own. + */ +#define _THREAD_PRIVATE(keyname, _initv, _errv) \ + ({ \ + struct _thread_private_key_struct * __k = \ + &__THREAD_KEY_NAME(keyname); \ + void* __p; \ + extern void free __P((void*)); \ + extern void* malloc __P((size_t)); \ + \ + if (!__isthreaded) { \ + /* non-threaded behaviour */ \ + __p = &(_initv); \ + goto _ok; \ + } \ + \ + /* create key for first thread */ \ + pthread_mutex_lock(&__k->lock); \ + if (__k->init == 0) { \ + if (pthread_key_create(&__k->key, free)) { \ + pthread_mutex_unlock(&__k->lock); \ + goto _err; \ + } \ + __k->init = 1; \ + } \ + pthread_mutex_unlock(&__k->lock); \ + \ + if ((__p = pthread_getspecific(__k->key)) == NULL) { \ + /* alloc space on 1st call in this thread */ \ + if ((__p = malloc(sizeof(_initv))) == NULL) \ + goto _err; \ + if (pthread_setspecific(__k->key, __p) != 0) { \ + free(__p); \ + goto _err; \ + } \ + /* initialise with _initv */ \ + memcpy(__p, &_initv, sizeof(_initv)); \ + } \ + goto _ok; \ + _err: \ + __p = (_errv); \ + _ok: \ + __p; \ + }) + +/* + * Macros for locking and unlocking FILEs. These test if the + * process is threaded to avoid locking when not required. + */ +#define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp) +#define FUNLOCKFILE(fp) if (__isthreaded) funlockfile(fp) + +#else /* !_THREAD_SAFE */ + +/* + * Do-nothing macros for single-threaded case. + */ +#define _FD_LOCK(f,o,p) (0) +#define _FD_UNLOCK(f,o) /* nothing */ +#define _THREAD_PRIVATE_KEY(_key) /* nothing */ +#define _THREAD_PRIVATE(keyname, _initv, _errv) (&(_initv)) +#define _THREAD_PRIVATE_MUTEX(_name) /* nothing */ +#define _THREAD_PRIVATE_MUTEX_LOCK(_name) /* nothing */ +#define _THREAD_PRIVATE_MUTEX_UNLOCK(_name) /* nothing */ +#define FLOCKFILE(fp) /* nothing */ +#define FUNLOCKFILE(fp) /* nothing */ + +#endif /* !_THREAD_SAFE */ + +#endif _THREAD_PRIVATE_H_ diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc new file mode 100644 index 0000000000..35a9632a94 --- /dev/null +++ b/src/lib/libc/net/Makefile.inc @@ -0,0 +1,56 @@ +# $OpenBSD: Makefile.inc,v 1.17 1998/11/20 11:18:43 d Exp $ + +# net sources +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net + +CFLAGS+=-DRESOLVSORT + +SRCS+= base64.c gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \ + getnetnamadr.c getproto.c getprotoent.c getprotoname.c getservbyname.c \ + getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \ + inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \ + inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c \ + inet_pton.c ipx_addr.c ipx_ntoa.c \ + iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c nsap_addr.c rcmd.c recv.c \ + res_comp.c res_data.c res_debug.c res_init.c res_mkquery.c res_query.c \ + res_random.c res_send.c send.c sethostent.c ethers.c rcmdsh.c + +# machine-dependent net sources +# m-d Makefile.inc must include sources for: +# htonl() htons() ntohl() ntohs() + +.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" + +MAN+= byteorder.3 ethers.3 gethostbyname.3 getnetent.3 getprotoent.3 \ + getservent.3 inet.3 inet_net.3 iso_addr.3 link_addr.3 ns.3 ipx.3 \ + rcmd.3 rcmdsh.3 resolver.3 + +MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ + byteorder.3 ntohs.3 byteorder.3 htobe16.3 byteorder.3 htobe32.3 \ + byteorder.3 betoh16.3 byteorder.3 betoh32.3 byteorder.3 htole16.3 \ + byteorder.3 htole32.3 byteorder.3 letoh16.3 byteorder.3 letoh32.3 \ + byteorder.3 swap16.3 byteorder.3 swap32.3 +MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ + ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 ethers.3 ether_addr.3 +MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ + gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ + gethostbyname.3 herror.3 gethostbyname.3 gethostbyname2.3 \ + gethostbyname.3 hstrerror.3 +MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ + getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 +MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ + getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 +MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ + getservent.3 getservbyport.3 getservent.3 setservent.3 +MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ + inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ + inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ + inet.3 ntoa.3 inet.3 inet_ntop.3 inet.3 inet_pton.3 +MLINKS+=iso_addr.3 iso_ntoa.3 +MLINKS+=link_addr.3 link_ntoa.3 +MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3 +MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 +MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 +MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ + resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ + resolver.3 res_search.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 @@ +/* $OpenBSD: base64.c,v 1.3 1997/11/08 20:46:55 deraadt Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Portions Copyright (c) 1995 by International Business Machines, Inc. + * + * International Business Machines, Inc. (hereinafter called IBM) grants + * permission under its copyrights to use, copy, modify, and distribute this + * Software with or without fee, provided that the above copyright notice and + * all paragraphs of this notice appear in all copies, and that the name of IBM + * not be used in connection with the marketing of any product incorporating + * the Software or modifications thereof, without specific, written prior + * permission. + * + * To the extent it has a right to do so, IBM grants an immunity from suit + * under its patents, if any, for the use, sale or manufacture of products to + * the extent that such products are used for performing Domain Name System + * dynamic updates in TCP/IP networks by means of the Software. No immunity is + * granted for any product per se or for any other function of any product. + * + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#define Assert(Cond) if (!(Cond)) abort() + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) + The following encoding technique is taken from RFC 1521 by Borenstein + and Freed. It is reproduced here in a slightly edited form for + convenience. + + A 65-character subset of US-ASCII is used, enabling 6 bits to be + represented per printable character. (The extra 65th character, "=", + is used to signify a special processing function.) + + The encoding process represents 24-bit groups of input bits as output + strings of 4 encoded characters. Proceeding from left to right, a + 24-bit input group is formed by concatenating 3 8-bit input groups. + These 24 bits are then treated as 4 concatenated 6-bit groups, each + of which is translated into a single digit in the base64 alphabet. + + Each 6-bit group is used as an index into an array of 64 printable + characters. The character referenced by the index is placed in the + output string. + + Table 1: The Base64 Alphabet + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + + Special processing is performed if fewer than 24 bits are available + at the end of the data being encoded. A full encoding quantum is + always completed at the end of a quantity. When fewer than 24 input + bits are available in an input group, zero bits are added (on the + right) to form an integral number of 6-bit groups. Padding at the + end of the data is performed using the '=' character. + + Since all base64 input is an integral number of octets, only the + ------------------------------------------------- + following cases can arise: + + (1) the final quantum of encoding input is an integral + multiple of 24 bits; here, the final unit of encoded + output will be an integral multiple of 4 characters + with no "=" padding, + (2) the final quantum of encoding input is exactly 8 bits; + here, the final unit of encoded output will be two + characters followed by two "=" padding characters, or + (3) the final quantum of encoding input is exactly 16 bits; + here, the final unit of encoded output will be three + characters followed by one "=" padding character. + */ + +int +b64_ntop(src, srclength, target, targsize) + u_char const *src; + size_t srclength; + char *target; + size_t targsize; +{ + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + int i; + + while (2 < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0 != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /* Returned value doesn't count \0. */ + return (datalength); +} + +/* skips all whitespace anywhere. + converts characters, four at a time, starting at (or after) + src from base - 64 numbers into three 8 bit bytes in the target area. + it returns the number of data bytes stored at the target, or -1 on error. + */ + +int +b64_pton(src, target, targsize) + char const *src; + u_char *target; + size_t targsize; +{ + int tarindex, state, ch; + char *pos; + + state = 0; + tarindex = 0; + + while ((ch = *src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 4; + target[tarindex+1] = ((pos - Base64) & 0x0f) + << 4 ; + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if (tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 2; + target[tarindex+1] = ((pos - Base64) & 0x03) + << 6; + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /* We got a pad char. */ + ch = *src++; /* Skip it, get next. */ + switch (state) { + case 0: /* Invalid = in first position */ + case 1: /* Invalid = in second position */ + return (-1); + + case 2: /* Valid, means one byte of info */ + /* Skip any number of spaces. */ + for (; ch != '\0'; ch = *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /* Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /* Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for (; ch != '\0'; ch = *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + return (tarindex); +} diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3 new file mode 100644 index 0000000000..f2788b25dc --- /dev/null +++ b/src/lib/libc/net/byteorder.3 @@ -0,0 +1,155 @@ +.\" $OpenBSD: byteorder.3,v 1.5 1997/11/19 23:30:17 niklas Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt BYTEORDER 3 +.Os BSD 4.2 +.Sh NAME +.Nm htonl , +.Nm htons , +.Nm ntohl , +.Nm ntohs , +.Nm htobe32 , +.Nm htobe16 , +.Nm betoh32 , +.Nm betoh16 , +.Nm htole32 , +.Nm htole16 , +.Nm letoh32 , +.Nm letoh16 , +.Nm swap32 , +.Nm swap16 +.Nd convert values between different byte orderings +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Ft u_int32_t +.Fn htonl "u_int32_t host32" +.Ft u_int16_t +.Fn htons "u_int16_t host16" +.Ft u_int32_t +.Fn ntohl "u_int32_t net32" +.Ft u_int16_t +.Fn ntohs "u_int16_t net16" +.Ft u_int32_t +.Fn htobe32 "u_int32_t host32" +.Ft u_int16_t +.Fn htobe16 "u_int16_t host16" +.Ft u_int32_t +.Fn betoh32 "u_int32_t big32" +.Ft u_int16_t +.Fn betoh16 "u_int16_t big16" +.Ft u_int32_t +.Fn htole32 "u_int32_t host32" +.Ft u_int16_t +.Fn htole16 "u_int16_t host16" +.Ft u_int32_t +.Fn letoh32 "u_int32_t little32" +.Ft u_int16_t +.Fn letoh16 "u_int16_t little16" +.Ft u_int32_t +.Fn swap32 "u_int32_t val32" +.Ft u_int16_t +.Fn swap16 "u_int16_t val16" +.Sh DESCRIPTION +These routines convert 16 and 32 bit quantities between different +byte orderings. The "swap" functions reverse the byte ordering of +the given quantity, the others converts either from/to the native +byte order used by the host to/from either little- or big-endian (a.k.a +network) order. +.Pp +Apart from the "swap" functions, the names can be described by this form: +{src-order}to{dst-order}{size}. +Both {src-order} and {dst-order} can take the following forms: +.Bl -tag -width "be " +.It Em h +host order +.It Em n +network order (big-endian) +.It Em be +big-endian (Most significant byte first) +.It Em le +little-endian (Least significant byte first) +.El +.Pp +One of the specified orderings must be "h". +{Size} will take these forms: +.Bl -tag -width "32 " +.It Em l +long (32-bit, used in conjunction with forms involving "n") +.It Em s +short (16-bit, used in conjunction with forms involving "n") +.It Em 16 +16-bit +.It Em 32 +32-bit +.El +.Pp +The "swap" functions are of the form: swap{size}. +.Pp +Names involving "n" convert quantities between network +byte order and host byte order. The last letter (s/l) is a mnemonic +for the traditional names for such quantities, short and long, +respectively. Today, the C concept of "short"/"long" integers +need not coincide with this traditional misunderstanding. +On machines which have a byte order which is the same as the network +order, routines are defined as null macros. +.Pp +The functions involving either "be", "le" or "swap" use the numbers +(16/32) for specifying the bitwidth of the quantities they operate on. +Currently all supported architectures are either big- or little-endian +so either the "be" or the "le" variants are implemented as null macros. +.Pp +The routines mentioned above which have either {src-order} or {dst-order} +set to "n" are most often used in +conjunction with Internet addresses and ports as returned by +.Xr gethostbyname 3 +and +.Xr getservent 3 . +.Sh SEE ALSO +.Xr gethostbyname 3 , +.Xr getservent 3 +.Sh HISTORY +The +.Nm byteorder +functions appeared in +.Bx 4.2 . +.Sh BUGS +On the +.Tn vax , +.Tn alpha , +.Tn i386 , +and so far +.Tn mips +bytes are handled backwards from most everyone else in +the 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 new file mode 100644 index 0000000000..74d153fce6 --- /dev/null +++ b/src/lib/libc/net/ethers.3 @@ -0,0 +1,109 @@ +.\" $OpenBSD: ethers.3,v 1.10 1999/03/18 11:09:15 aaron Exp $ +.\" +.\" Written by roland@frob.com. Public domain. +.\" +.Dd December 16, 1993 +.Dt ETHERS 3 +.Os +.Sh NAME +.Nm ether_aton , +.Nm ether_ntoa , +.Nm ether_addr , +.Nm ether_ntohost , +.Nm ether_hostton , +.Nm ether_line +.Nd get ethers entry +.Sh SYNOPSIS +.Fd #include +.Ft char * +.Fn ether_ntoa "struct ether_addr *e" +.Ft struct ether_addr * +.Fn ether_aton "char *s" +.Ft int +.Fn ether_ntohost "char *hostname" "struct ether_addr *e" +.Ft int +.Fn ether_hostton "char *hostname" "struct ether_addr *e" +.Ft int +.Fn ether_line "char *l" "struct ether_addr *e" "char *hostname" +.Sh DESCRIPTION +Ethernet addresses are represented by the +following structure: +.Bd -literal -offset indent +struct ether_addr { + u_int8_t ether_addr_octet[6]; +}; +.Ed +.Pp +The +.Fn ether_ntoa +function converts this structure into an ASCII string of the form +``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated +by colons. It returns a pointer to a static buffer that is reused for +each call. +The +.Fn ether_aton +converts an ASCII string of the same form and to a structure +containing the 6 octets of the address. It returns a pointer to a +static structure that is reused for each call. +.Pp +The +.Fn ether_ntohost +and +.Fn ether_hostton +functions interrogate the data base mapping host names to Ethernet +addresses, +.Pa /etc/ethers . +The +.Fn ether_ntohost +function looks up the given Ethernet address and writes the associated +host name into the character buffer passed. This buffer should be +.Ev MAXHOSTNAMELEN +characters in size. +The +.Fn ether_hostton +function looks up the given host name and writes the associated +Ethernet address into the structure passed. Both functions return +zero if they find the requested host name or address, and -1 if not. +.Pp +Each call reads +.Pa /etc/ethers +from the beginning; if a + appears alone on a line in the file, then +.Fn ether_hostton +will consult the +.Pa ethers.byname +YP map, and +.Fn ether_ntohost +will consult the +.Pa ethers.byaddr +YP map. +.Pp +The +.Fn ether_line +function parses a line from the +.Pa /etc/ethers +file and fills in the passed ``struct ether_addr'' and character +buffer with the Ethernet address and host name on the line. It +returns zero if the line was successfully parsed and -1 if not. +The character buffer should be +.Ev MAXHOSTNAMELEN +characters in size. +.Sh FILES +.Bl -tag -width /etc/ethers -compact +.It Pa /etc/ethers +.El +.Sh SEE ALSO +.Xr ethers 5 +.Sh HISTORY +The +.Fn ether_ntoa , +.Fn ether_aton , +.Fn ether_ntohost , +.Fn ether_hostton , +and +.Fn ether_line +functions were adopted from SunOS and appeared in +NetBSD 0.9b. +.Sh BUGS +The data space used by these functions is static; if future use +requires the data, it should be copied before any subsequent calls to +these functions overwrite it. diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c new file mode 100644 index 0000000000..94ae5c996a --- /dev/null +++ b/src/lib/libc/net/ethers.c @@ -0,0 +1,270 @@ +/* $OpenBSD: ethers.c,v 1.10 1998/11/18 23:28:54 deraadt Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * ethers(3) a la Sun. + * Originally Written by Roland McGrath 10/14/93. + * Substantially modified by Todd C. Miller + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: ethers.c,v 1.10 1998/11/18 23:28:54 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef _PATH_ETHERS +#define _PATH_ETHERS "/etc/ethers" +#endif + +static char * _ether_aton __P((char *, struct ether_addr *)); + +char * +ether_ntoa(e) + struct ether_addr *e; +{ + static char a[] = "xx:xx:xx:xx:xx:xx"; + + if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || + e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || + e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { + errno = EINVAL; + return (NULL); + } + + (void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); + + return (a); +} + +static char * +_ether_aton(s, e) + char *s; + struct ether_addr *e; +{ + int i; + long l; + char *pp; + + while (isspace(*s)) + s++; + + /* expect 6 hex octets separated by ':' or space/NUL if last octet */ + for (i = 0; i < 6; i++) { + l = strtol(s, &pp, 16); + if (pp == s || l > 0xFF || l < 0) + return (NULL); + if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) + return (NULL); + e->ether_addr_octet[i] = (u_char)l; + s = pp + 1; + } + + /* return character after the octets ala strtol(3) */ + return (pp); +} + +struct ether_addr * +ether_aton(s) + char *s; +{ + static struct ether_addr n; + + return (_ether_aton(s, &n) ? &n : NULL); +} + +int +ether_ntohost(hostname, e) + char *hostname; + struct ether_addr *e; +{ + FILE *f; + char buf[BUFSIZ+1], *p; + size_t len; + struct ether_addr try; +#ifdef YP + char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; + int trylen; +#endif + + if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || + e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || + e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { + errno = EINVAL; + return (-1); + } + +#ifdef YP + sprintf(trybuf, "%x:%x:%x:%x:%x:%x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); + trylen = strlen(trybuf); +#endif + + f = fopen(_PATH_ETHERS, "r"); + if (f == NULL) + return (-1); + while ((p = fgetln(f, &len)) != NULL) { + if (p[len-1] == '\n') + len--; + if (len > sizeof(buf) - 2) + continue; + (void)memcpy(buf, p, len); + buf[len] = '\n'; /* code assumes newlines later on */ + buf[len+1] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (!strncmp(buf, "+\n", sizeof(buf))) { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byaddr", trybuf, + trylen, &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, &try, hostname) == 0) { + free(ypbuf); + (void)fclose(f); + return (0); + } + free(ypbuf); + continue; + } +#endif + if (ether_line(buf, &try, hostname) == 0 && + memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { + (void)fclose(f); + return (0); + } + } + (void)fclose(f); + errno = ENOENT; + return (-1); +} + +int +ether_hostton(hostname, e) + char *hostname; + struct ether_addr *e; +{ + FILE *f; + char buf[BUFSIZ+1], *p; + char try[MAXHOSTNAMELEN]; + size_t len; +#ifdef YP + int hostlen = strlen(hostname); +#endif + + f = fopen(_PATH_ETHERS, "r"); + if (f==NULL) + return (-1); + + while ((p = fgetln(f, &len)) != NULL) { + if (p[len-1] == '\n') + len--; + if (len > sizeof(buf) - 2) + continue; + memcpy(buf, p, len); + buf[len] = '\n'; /* code assumes newlines later on */ + buf[len+1] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (!strncmp(buf, "+\n", sizeof(buf))) { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byname", hostname, hostlen, + &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, e, try) == 0) { + free(ypbuf); + (void)fclose(f); + return (0); + } + free(ypbuf); + continue; + } +#endif + if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { + (void)fclose(f); + return (0); + } + } + (void)fclose(f); + errno = ENOENT; + return (-1); +} + +int +ether_line(line, e, hostname) + char *line; + struct ether_addr *e; + char *hostname; +{ + char *p; + size_t n; + + /* Parse "xx:xx:xx:xx:xx:xx" */ + if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) + goto bad; + + /* Now get the hostname */ + while (isspace(*p)) + p++; + if (*p == '\0') + goto bad; + n = strcspn(p, " \t\n"); + if (n >= MAXHOSTNAMELEN) + goto bad; + (void)strncpy(hostname, p, n); + hostname[n] = '\0'; + return (0); + +bad: + errno = EINVAL; + return (-1); +} diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3 new file mode 100644 index 0000000000..e28fed361c --- /dev/null +++ b/src/lib/libc/net/gethostbyname.3 @@ -0,0 +1,266 @@ +.\" $OpenBSD: gethostbyname.3,v 1.10 1999/02/27 21:55:27 deraadt Exp $ +.\" +.\" Copyright (c) 1983, 1987, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd March 13, 1997 +.Dt GETHOSTBYNAME 3 +.Os +.Sh NAME +.Nm gethostbyname , +.Nm gethostbyname2 , +.Nm gethostbyaddr , +.Nm gethostent , +.Nm sethostent , +.Nm endhostent , +.Nm hstrerror , +.Nm herror +.Nd get network host entry +.Sh SYNOPSIS +.Fd #include +.Fd extern int h_errno; +.Ft struct hostent * +.Fn gethostbyname "const char *name" +.Ft struct hostent * +.Fn gethostbyname2 "const char *name" "int af" +.Ft struct hostent * +.Fn gethostbyaddr "const char *addr" "int len" "int type" +.Ft struct hostent * +.Fn gethostent void +.Ft void +.Fn sethostent "int stayopen" +.Ft void +.Fn endhostent void +.Ft void +.Fn herror "const char *string" +.Ft const char * +.Fn hstrerror "int err" +.Sh DESCRIPTION +The +.Fn gethostbyname +and +.Fn gethostbyaddr +functions +each return a pointer to an object with the +following structure describing an internet host +referenced by name or by address, respectively. +This structure contains either the information obtained from the name server, +.Xr named 8 , +broken-out fields from a line in +.Pa /etc/hosts , +or database entries supplied by the +.Xr yp 8 +system . +If the local name server is not running these routines do a lookup in +.Pa /etc/hosts . +.Bd -literal +struct hostent { + char *h_name; /* official name of host */ + char **h_aliases; /* alias list */ + int h_addrtype; /* host address type */ + int h_length; /* length of address */ + char **h_addr_list; /* list of addresses from name server */ +}; +#define h_addr h_addr_list[0] /* address, for backward compatibility */ +.Ed +.Pp +The members of this structure are: +.Bl -tag -width h_addr_list +.It Fa h_name +Official name of the host. +.It Fa h_aliases +A zero terminated array of alternate names for the host. +.It Fa h_addrtype +The type of address being returned. +.It Fa h_length +The length, in bytes, of the address. +.It Fa h_addr_list +A zero terminated array of network addresses for the host. +Host addresses are returned in network byte order. +.It Fa h_addr +The first address in +.Fa h_addr_list ; +this is for backward compatibility. +.El +.Pp +When using the nameserver, +.Fn gethostbyname +will search for the named host in the current domain and its parents +unless the name ends in a dot. +If the name contains no dot, and if the environment variable +.Dq Ev HOSTALIASES +contains the name of an alias file, the alias file will first be searched +for an alias matching the input name. +See +.Xr hostname 7 +for the domain search procedure and the alias file format. +.Pp +.Fn gethostbyname2 +is an advanced form of +.Fn gethostbyname +which allows lookups in address families other than +.Dv AF_INET , +for example +.Dv AF_INET6 . +.Pp +The +.Fn sethostent +function +may be used to request the use of a connected +.Tn TCP +socket for queries. +If the +.Fa stayopen +flag is non-zero, +this sets the option to send all queries to the name server using +.Tn TCP +and to retain the connection after each call to +.Fn gethostbyname +or +.Fn gethostbyaddr . +Otherwise, queries are performed using +.Tn UDP +datagrams. +.Pp +The +.Fn endhostent +function +closes the +.Tn TCP +connection. +.Pp +The +.Fn herror +function prints an error message describing the failure. If its argument +.Fa string +is +.Pf non Dv -NULL , +it is prepended to the message string and separated from it by a colon +and a space. The error message is printed with a trailing newline. +The contents of the error message is the same as that returned by +.Fn hstrerror +with argument +.Fa h_errno . +.Sh FILES +.Bl -tag -width /etc/hosts -compact +.It Pa /etc/hosts +.El +.Sh DIAGNOSTICS +Error return status from +.Fn gethostbyname , +.Fn gethostbyname2 , +and +.Fn gethostbyaddr +is indicated by return of a null pointer. +The external integer +.Va h_errno +may then be checked to see whether this is a temporary failure +or an invalid or unknown host. +.Pp +The variable +.Va h_errno +can have the following values: +.Bl -tag -width HOST_NOT_FOUND +.It Dv HOST_NOT_FOUND +No such host is known. +.It Dv TRY_AGAIN +This is usually a temporary error +and means that the local server did not receive +a response from an authoritative server. +A retry at some later time may succeed. +.It Dv NO_RECOVERY +Some unexpected server failure was encountered. +This is a non-recoverable error. +.It Dv NO_DATA +The requested name is valid but does not have an IP address; +this is not a temporary error. +This means that the name is known to the name server but there is no address +associated with this name. +Another type of request to the name server using this domain name +will result in an answer; +for example, a mail-forwarder may be registered for this domain. +.El +.Sh SEE ALSO +.Xr resolver 3 , +.Xr hosts 5 , +.Xr hostname 7 , +.Xr named 8 +.Sh CAVEAT +The +.Fn gethostent +function +reads the next line of +.Pa /etc/hosts , +opening the file if necessary. +.Pp +The +.Fn sethostent +function +opens and/or rewinds the file +.Pa /etc/hosts . +If the +.Fa stayopen +argument is non-zero, +the file will not be closed after each call to +.Fn gethostbyname , +.Fn gethostbyname2 , +or +.Fn gethostbyaddr . +.Pp +The +.Fn endhostent +function +closes the file. +.Sh HISTORY +The +.Fn herror +function appeared in +.Bx 4.3 . +The +.Fn endhostent , +.Fn gethostbyaddr , +.Fn gethostbyname , +.Fn gethostent , +and +.Fn sethostent +functions appeared in +.Bx 4.2 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Only the Internet +address formats are currently understood. +.Pp +YP does not support any address families other than +.Dv AF_INET +and uses +the traditional database format. diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c new file mode 100644 index 0000000000..6ff456fb0c --- /dev/null +++ b/src/lib/libc/net/gethostnamadr.c @@ -0,0 +1,1147 @@ +/*- + * Copyright (c) 1985, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.31 1998/11/20 11:18:44 d Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef YP +#include +#include +#include +#include "ypinternal.h" +#endif +#include "thread_private.h" + +#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ + +#define MAXALIASES 35 +#define MAXADDRS 35 + +static char *h_addr_ptrs[MAXADDRS + 1]; + +#ifdef YP +static char *__ypdomain; +#endif + +static struct hostent host; +static char *host_aliases[MAXALIASES]; +static char hostbuf[BUFSIZ+1]; +static union { + struct in_addr _host_in_addr; + u_char _host_addr[16]; /* IPv4 or IPv6 */ +} _host_addr_u; +#define host_addr _host_addr_u._host_addr +static FILE *hostf = NULL; +static int stayopen = 0; + +static void map_v4v6_address __P((const char *src, char *dst)); +static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len)); + +#ifdef RESOLVSORT +static void addrsort __P((char **, int)); +#endif + +int _hokchar __P((const char *)); + +static const char AskedForGot[] = + "gethostby*.getanswer: asked for \"%s\", got \"%s\""; + +#if PACKETSZ > 1024 +#define MAXPACKET PACKETSZ +#else +#define MAXPACKET 1024 +#endif + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + int32_t al; + char ac; +} align; + +static struct hostent *getanswer __P((const querybuf *, int, const char *, + int)); + +extern int h_errno; + +int +_hokchar(p) + const char *p; +{ + char c; + + /* + * Many people do not obey RFC 822 and 1035. The valid + * characters are a-z, A-Z, 0-9, '-' and . But the others + * tested for below can happen, and we must be more permissive + * than the resolver until those idiots clean up their act. + * We let '/' through, but not '..' + */ + while ((c = *p++)) { + if (('a' <= c && c <= 'z') || + ('A' <= c && c <= 'Z') || + ('0' <= c && c <= '9')) + continue; + if (strchr("-_/", c)) + continue; + if (c == '.' && *p != '.') + continue; + return 0; + } + return 1; +} + +static struct hostent * +getanswer(answer, anslen, qname, qtype) + const querybuf *answer; + int anslen; + const char *qname; + int qtype; +{ + register const HEADER *hp; + register const u_char *cp; + register int n; + const u_char *eom; + char *bp, **ap, **hap; + int type, class, buflen, ancount, qdcount; + int haveanswer, had_error; + int toobig = 0; + char tbuf[MAXDNAME]; + const char *tname; + int (*name_ok) __P((const char *)); + + tname = qname; + host.h_name = NULL; + eom = answer->buf + anslen; + switch (qtype) { + case T_A: + case T_AAAA: +#ifdef USE_RESOLV_NAME_OK + name_ok = res_hnok; + break; +#endif + case T_PTR: +#ifdef USE_RESOLV_NAME_OK + name_ok = res_dnok; +#else + name_ok = _hokchar; +#endif + break; + default: + return (NULL); + } + /* + * find first satisfactory answer + */ + hp = &answer->hdr; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + bp = hostbuf; + buflen = sizeof hostbuf; + cp = answer->buf + HFIXEDSZ; + if (qdcount != 1) { + h_errno = NO_RECOVERY; + return (NULL); + } + n = dn_expand(answer->buf, eom, cp, bp, buflen); + if ((n < 0) || !(*name_ok)(bp)) { + h_errno = NO_RECOVERY; + return (NULL); + } + cp += n + QFIXEDSZ; + if (qtype == T_A || qtype == T_AAAA) { + /* res_send() has already verified that the query name is the + * same as the one we sent; this just gets the expanded name + * (i.e., with the succeeding search-domain tacked on). + */ + n = strlen(bp) + 1; /* for the \0 */ + host.h_name = bp; + bp += n; + buflen -= n; + /* The qname can be abbreviated, but h_name is now absolute. */ + qname = host.h_name; + } + ap = host_aliases; + *ap = NULL; + host.h_aliases = host_aliases; + hap = h_addr_ptrs; + *hap = NULL; + host.h_addr_list = h_addr_ptrs; + haveanswer = 0; + had_error = 0; + while (ancount-- > 0 && cp < eom && !had_error) { + n = dn_expand(answer->buf, eom, cp, bp, buflen); + if ((n < 0) || !(*name_ok)(bp)) { + had_error++; + continue; + } + cp += n; /* name */ + type = _getshort(cp); + cp += INT16SZ; /* type */ + class = _getshort(cp); + cp += INT16SZ + INT32SZ; /* class, TTL */ + n = _getshort(cp); + cp += INT16SZ; /* len */ + if (class != C_IN) { + /* XXX - debug? syslog? */ + cp += n; + continue; /* XXX - had_error++ ? */ + } + if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { + if (ap >= &host_aliases[MAXALIASES-1]) + continue; + n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); + if ((n < 0) || !(*name_ok)(tbuf)) { + had_error++; + continue; + } + cp += n; + /* Store alias. */ + *ap++ = bp; + n = strlen(bp) + 1; /* for the \0 */ + bp += n; + buflen -= n; + /* Get canonical name. */ + n = strlen(tbuf) + 1; /* for the \0 */ + if (n > buflen) { + had_error++; + continue; + } + strcpy(bp, tbuf); + host.h_name = bp; + bp += n; + buflen -= n; + continue; + } + if (qtype == T_PTR && type == T_CNAME) { + n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); + if ((n < 0) || !res_hnok(tbuf)) { + had_error++; + continue; + } + cp += n; + /* Get canonical name. */ + n = strlen(tbuf) + 1; /* for the \0 */ + if (n > buflen) { + had_error++; + continue; + } + strcpy(bp, tbuf); + tname = bp; + bp += n; + buflen -= n; + continue; + } + if (type != qtype) { + syslog(LOG_NOTICE|LOG_AUTH, + "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", + qname, p_class(C_IN), p_type(qtype), + p_type(type)); + cp += n; + continue; /* XXX - had_error++ ? */ + } + switch (type) { + case T_PTR: + if (strcasecmp(tname, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, qname, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + n = dn_expand(answer->buf, eom, cp, bp, buflen); + if ((n < 0) || !res_hnok(bp)) { + had_error++; + break; + } +#if MULTI_PTRS_ARE_ALIASES + cp += n; + if (!haveanswer) + host.h_name = bp; + else if (ap < &host_aliases[MAXALIASES-1]) + *ap++ = bp; + else + n = -1; + if (n != -1) { + n = strlen(bp) + 1; /* for the \0 */ + bp += n; + buflen -= n; + } + break; +#else + host.h_name = bp; + if (_res.options & RES_USE_INET6) { + n = strlen(bp) + 1; /* for the \0 */ + bp += n; + buflen -= n; + map_v4v6_hostent(&host, &bp, &buflen); + } + h_errno = NETDB_SUCCESS; + return (&host); +#endif + case T_A: + case T_AAAA: + if (strcasecmp(host.h_name, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, host.h_name, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + if (n != host.h_length) { + cp += n; + continue; + } + if (!haveanswer) { + register int nn; + + host.h_name = bp; + nn = strlen(bp) + 1; /* for the \0 */ + bp += nn; + buflen -= nn; + } + + bp += sizeof(align) - ((u_long)bp % sizeof(align)); + + if (bp + n >= &hostbuf[sizeof hostbuf]) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("size (%d) too big\n", n); +#endif + had_error++; + continue; + } + if (hap >= &h_addr_ptrs[MAXADDRS-1]) { + if (!toobig++) +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("Too many addresses (%d)\n", MAXADDRS); +#endif + cp += n; + continue; + } + bcopy(cp, *hap++ = bp, n); + bp += n; + buflen -= n; + cp += n; + break; + } + if (!had_error) + haveanswer++; + } + if (haveanswer) { + *ap = NULL; + *hap = NULL; +# if defined(RESOLVSORT) + /* + * Note: we sort even if host can take only one address + * in its return structures - should give it the "best" + * address in that case, not some random one + */ + if (_res.nsort && haveanswer > 1 && qtype == T_A) + addrsort(h_addr_ptrs, haveanswer); +# endif /*RESOLVSORT*/ + if (!host.h_name) { + n = strlen(qname) + 1; /* for the \0 */ + if (n > buflen) + goto try_again; + strcpy(bp, qname); + host.h_name = bp; + bp += n; + buflen -= n; + } + if (_res.options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, &buflen); + h_errno = NETDB_SUCCESS; + return (&host); + } + try_again: + h_errno = TRY_AGAIN; + return (NULL); +} + +#ifndef notyet +/* + * XXX This is an extremely bogus implementations. + * + * FreeBSD has this interface: + * int gethostbyaddr_r(const char *addr, int len, int type, + * struct hostent *result, struct hostent_data *buffer) + */ + +struct hostent * +gethostbyname_r(name, hp, buf, buflen, errorp) + const char * name; + struct hostent * hp; + char * buf; + int buflen; + int * errorp; +{ + struct hostent *res; + + res = gethostbyname(name); + *errorp = h_errno; + if (res == NULL) + return NULL; + memcpy(hp, res, sizeof *hp); /* XXX not sufficient */ + return hp; +} + +/* + * XXX This is an extremely bogus implementations. + */ +struct hostent * +gethostbyaddr_r(addr, len, af, he, buf, buflen, errorp) + const char *addr; /* XXX should have been def'd as u_char! */ + int len, af; + struct hostent * he; + char * buf; + int buflen; + int * errorp; +{ + struct hostent * res; + + res = gethostbyaddr(addr, len, af); + *errorp = h_errno; + if (res == NULL) + return NULL; + memcpy(he, res, sizeof *he); /* XXX not sufficient */ + return he; +} + +/* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */ +#endif + +_THREAD_PRIVATE_MUTEX(gethostnamadr) + +struct hostent * +gethostbyname(name) + const char *name; +{ + struct hostent *hp; + extern struct hostent *_gethtbyname2(); + + _THREAD_PRIVATE_MUTEX_LOCK(gethostnamadr); + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + hp = _gethtbyname2(name, AF_INET); + + else if (_res.options & RES_USE_INET6) { + hp = gethostbyname2(name, AF_INET6); + if (hp == NULL) + hp = gethostbyname2(name, AF_INET); + } + else + hp = gethostbyname2(name, AF_INET); + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return hp; +} + +struct hostent * +gethostbyname2(name, af) + const char *name; + int af; +{ + querybuf buf; + register const char *cp; + char *bp; + int n, size, type, len, i; + extern struct hostent *_gethtbyname2(), *_yp_gethtbyname(); + register struct hostent *hp; + char lookups[MAXDNSLUS]; + + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + return (_gethtbyname2(name, af)); + + switch (af) { + case AF_INET: + size = INADDRSZ; + type = T_A; + break; + case AF_INET6: + size = IN6ADDRSZ; + type = T_AAAA; + break; + default: + h_errno = NETDB_INTERNAL; + errno = EAFNOSUPPORT; + return (NULL); + } + + host.h_addrtype = af; + host.h_length = size; + + /* + * if there aren't any dots, it could be a user-level alias. + * this is also done in res_query() since we are not the only + * function that looks up host names. + */ + if (!strchr(name, '.') && (cp = __hostalias(name))) + name = cp; + + /* + * disallow names consisting only of digits/dots, unless + * they end in a dot. + */ + if (isdigit(name[0])) + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-numeric, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return (NULL); + } + strncpy(hostbuf, name, MAXHOSTNAMELEN-1); + hostbuf[MAXHOSTNAMELEN-1] = '\0'; + bp = hostbuf + MAXHOSTNAMELEN; + len = sizeof hostbuf - MAXHOSTNAMELEN; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + if (_res.options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, &len); + h_errno = NETDB_SUCCESS; + return (&host); + } + if (!isdigit(*cp) && *cp != '.') + break; + } + if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || + name[0] == ':') + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-IPv6-legal, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return (NULL); + } + strncpy(hostbuf, name, MAXHOSTNAMELEN-1); + hostbuf[MAXHOSTNAMELEN-1] = '\0'; + bp = hostbuf + MAXHOSTNAMELEN; + len = sizeof hostbuf - MAXHOSTNAMELEN; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + h_errno = NETDB_SUCCESS; + return (&host); + } + if (!isxdigit(*cp) && *cp != ':' && *cp != '.') + break; + } + + bcopy(_res.lookups, lookups, sizeof lookups); + if (lookups[0] == '\0') + strncpy(lookups, "bf", sizeof lookups); + + hp = (struct hostent *)NULL; + for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { + switch (lookups[i]) { +#ifdef YP + case 'y': + /* YP only supports AF_INET. */ + if (af == AF_INET) + hp = _yp_gethtbyname(name); + break; +#endif + case 'b': + if ((n = res_search(name, C_IN, type, buf.buf, + sizeof(buf))) < 0) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("res_search failed\n"); +#endif + break; + } + hp = getanswer(&buf, n, name, type); + break; + case 'f': + hp = _gethtbyname2(name, af); + break; + } + } + /* XXX h_errno not correct in all cases... */ + return (hp); +} + +struct hostent * +gethostbyaddr(addr, len, af) + const char *addr; /* XXX should have been def'd as u_char! */ + int len, af; +{ + const u_char *uaddr = (const u_char *)addr; + static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; + static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; + int n, size, i; + querybuf buf; + register struct hostent *hp; + char qbuf[MAXDNAME+1], *qp; + extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); + char lookups[MAXDNSLUS]; + struct hostent *res; + + _THREAD_PRIVATE_MUTEX_LOCK(gethostnamadr); + if ((_res.options & RES_INIT) == 0 && res_init() == -1) { + res = _gethtbyaddr(addr, len, af); + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return (res); + } + + if (af == AF_INET6 && len == IN6ADDRSZ && + (!bcmp(uaddr, mapped, sizeof mapped) || + !bcmp(uaddr, tunnelled, sizeof tunnelled))) { + /* Unmap. */ + addr += sizeof mapped; + uaddr += sizeof mapped; + af = AF_INET; + len = INADDRSZ; + } + switch (af) { + case AF_INET: + size = INADDRSZ; + break; + case AF_INET6: + size = IN6ADDRSZ; + break; + default: + errno = EAFNOSUPPORT; + h_errno = NETDB_INTERNAL; + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return (NULL); + } + if (size != len) { + errno = EINVAL; + h_errno = NETDB_INTERNAL; + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return (NULL); + } + switch (af) { + case AF_INET: + (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", + (uaddr[3] & 0xff), + (uaddr[2] & 0xff), + (uaddr[1] & 0xff), + (uaddr[0] & 0xff)); + break; + case AF_INET6: + qp = qbuf; + for (n = IN6ADDRSZ - 1; n >= 0; n--) { + qp += sprintf(qp, "%x.%x.", + uaddr[n] & 0xf, + (uaddr[n] >> 4) & 0xf); + } + strcpy(qp, "ip6.int"); + break; + } + + bcopy(_res.lookups, lookups, sizeof lookups); + if (lookups[0] == '\0') + strncpy(lookups, "bf", sizeof lookups); + + hp = (struct hostent *)NULL; + for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { + switch (lookups[i]) { +#ifdef YP + case 'y': + /* YP only supports AF_INET. */ + if (af == AF_INET) + hp = _yp_gethtbyaddr(addr); + break; +#endif + case 'b': + n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, + sizeof buf.buf); + if (n < 0) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("res_query failed\n"); +#endif + break; + } + if (!(hp = getanswer(&buf, n, qbuf, T_PTR))) + break; + hp->h_addrtype = af; + hp->h_length = len; + bcopy(addr, host_addr, len); + h_addr_ptrs[0] = (char *)host_addr; + h_addr_ptrs[1] = NULL; + if (af == AF_INET && (_res.options & RES_USE_INET6)) { + map_v4v6_address((char*)host_addr, + (char*)host_addr); + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + } + h_errno = NETDB_SUCCESS; + break; + case 'f': + hp = _gethtbyaddr(addr, len, af); + break; + } + } + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + /* XXX h_errno not correct in all cases... */ + return (hp); +} + +void +_sethtent(f) + int f; +{ + if (hostf == NULL) + hostf = fopen(_PATH_HOSTS, "r" ); + else + rewind(hostf); + stayopen = f; +} + +void +_endhtent() +{ + if (hostf && !stayopen) { + (void) fclose(hostf); + hostf = NULL; + } +} + +struct hostent * +_gethtent() +{ + char *p; + register char *cp, **q; + int af; + size_t len; + + if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { + h_errno = NETDB_INTERNAL; + return (NULL); + } + again: + if ((p = fgetln(hostf, &len)) == NULL) { + h_errno = HOST_NOT_FOUND; + return (NULL); + } + if (p[len-1] == '\n') + len--; + if (len >= sizeof(hostbuf) || len == 0) + goto again; + p = memcpy(hostbuf, p, len); + hostbuf[len] = '\0'; + if (*p == '#') + goto again; + if ((cp = strchr(p, '#'))) + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + if ((_res.options & RES_USE_INET6) && + inet_pton(AF_INET6, p, host_addr) > 0) { + af = AF_INET6; + len = IN6ADDRSZ; + } else if (inet_pton(AF_INET, p, host_addr) > 0) { + if (_res.options & RES_USE_INET6) { + map_v4v6_address((char*)host_addr, (char*)host_addr); + af = AF_INET6; + len = IN6ADDRSZ; + } else { + af = AF_INET; + len = INADDRSZ; + } + } else { + goto again; + } + h_addr_ptrs[0] = (char *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_length = len; + host.h_addrtype = af; + while (*cp == ' ' || *cp == '\t') + cp++; + host.h_name = cp; + q = host.h_aliases = host_aliases; + if ((cp = strpbrk(cp, " \t"))) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + if ((cp = strpbrk(cp, " \t"))) + *cp++ = '\0'; + } + *q = NULL; + if (_res.options & RES_USE_INET6) { + char *bp = hostbuf; + int buflen = sizeof hostbuf; + + map_v4v6_hostent(&host, &bp, &buflen); + } + h_errno = NETDB_SUCCESS; + return (&host); +} + +struct hostent * +_gethtbyname(name) + const char *name; +{ + extern struct hostent *_gethtbyname2(); + struct hostent *hp; + + if (_res.options & RES_USE_INET6) { + hp = _gethtbyname2(name, AF_INET6); + if (hp) + return (hp); + } + return (_gethtbyname2(name, AF_INET)); +} + +struct hostent * +_gethtbyname2(name, af) + const char *name; + int af; +{ + register struct hostent *p; + register char **cp; + + _sethtent(0); + while ((p = _gethtent())) { + if (p->h_addrtype != af) + continue; + if (strcasecmp(p->h_name, name) == 0) + break; + for (cp = p->h_aliases; *cp != 0; cp++) + if (strcasecmp(*cp, name) == 0) + goto found; + } + found: + _endhtent(); + return (p); +} + +struct hostent * +_gethtbyaddr(addr, len, af) + const char *addr; + int len, af; +{ + register struct hostent *p; + + _sethtent(0); + while ((p = _gethtent())) + if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) + break; + _endhtent(); + return (p); +} + +#ifdef YP +struct hostent * +_yphostent(line) + char *line; +{ + static struct in_addr host_addrs[MAXADDRS]; + char *p = line; + char *cp, **q; + char **hap; + struct in_addr *buf; + int more; + + host.h_name = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_length = INADDRSZ; + host.h_addrtype = AF_INET; + hap = h_addr_ptrs; + buf = host_addrs; + q = host.h_aliases = host_aliases; + +nextline: + more = 0; + cp = strpbrk(p, " \t"); + if (cp == NULL) { + if (host.h_name == NULL) + return (NULL); + else + goto done; + } + *cp++ = '\0'; + + *hap++ = (char *)buf; + (void) inet_aton(p, buf++); + + while (*cp == ' ' || *cp == '\t') + cp++; + p = cp; + cp = strpbrk(p, " \t\n"); + if (cp != NULL) { + if (*cp == '\n') + more = 1; + *cp++ = '\0'; + } + if (!host.h_name) + host.h_name = p; + else if (strcmp(host.h_name, p)==0) + ; + else if (q < &host_aliases[MAXALIASES - 1]) + *q++ = p; + p = cp; + if (more) + goto nextline; + + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (*cp == '\n') { + cp++; + goto nextline; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } +done: + *q = NULL; + *hap = NULL; + return (&host); +} + +struct hostent * +_yp_gethtbyaddr(addr) + const char *addr; +{ + struct hostent *hp = (struct hostent *)NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + char name[sizeof("xxx.xxx.xxx.xxx") + 1]; + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return (hp); + } + sprintf(name, "%u.%u.%u.%u", + ((unsigned)addr[0] & 0xff), + ((unsigned)addr[1] & 0xff), + ((unsigned)addr[2] & 0xff), + ((unsigned)addr[3] & 0xff)); + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + r = yp_match(__ypdomain, "hosts.byaddr", name, + strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r==0) + hp = _yphostent(__ypcurrent); + if (hp==NULL) + h_errno = HOST_NOT_FOUND; + return (hp); +} + +struct hostent * +_yp_gethtbyname(name) + const char *name; +{ + struct hostent *hp = (struct hostent *)NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + + if (strlen(name) >= MAXHOSTNAMELEN) + return (NULL); + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return (hp); + } + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + r = yp_match(__ypdomain, "hosts.byname", name, + strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r == 0) + hp = _yphostent(__ypcurrent); + if (hp == NULL) + h_errno = HOST_NOT_FOUND; + return (hp); +} +#endif + +static void +map_v4v6_address(src, dst) + const char *src; + char *dst; +{ + u_char *p = (u_char *)dst; + char tmp[INADDRSZ]; + int i; + + /* Stash a temporary copy so our caller can update in place. */ + bcopy(src, tmp, INADDRSZ); + /* Mark this ipv6 addr as a mapped ipv4. */ + for (i = 0; i < 10; i++) + *p++ = 0x00; + *p++ = 0xff; + *p++ = 0xff; + /* Retrieve the saved copy and we're done. */ + bcopy(tmp, (void*)p, INADDRSZ); +} + +static void +map_v4v6_hostent(hp, bpp, lenp) + struct hostent *hp; + char **bpp; + int *lenp; +{ + char **ap; + + if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) + return; + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + for (ap = hp->h_addr_list; *ap; ap++) { + int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); + + if (*lenp < (i + IN6ADDRSZ)) { + /* Out of memory. Truncate address list here. XXX */ + *ap = NULL; + return; + } + *bpp += i; + *lenp -= i; + map_v4v6_address(*ap, *bpp); + *ap = *bpp; + *bpp += IN6ADDRSZ; + *lenp -= IN6ADDRSZ; + } +} + +struct hostent * +gethostent() +{ + return (_gethtent()); +} + +#ifdef RESOLVSORT +static void +addrsort(ap, num) + char **ap; + int num; +{ + int i, j; + char **p; + short aval[MAXADDRS]; + int needsort = 0; + + p = ap; + for (i = 0; i < num; i++, p++) { + for (j = 0 ; (unsigned)j < _res.nsort; j++) + if (_res.sort_list[j].addr.s_addr == + (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) + break; + aval[i] = j; + if (needsort == 0 && i > 0 && j < aval[i-1]) + needsort = i; + } + if (!needsort) + return; + + while (needsort < num) { + for (j = needsort - 1; j >= 0; j--) { + if (aval[j] > aval[j+1]) { + char *hp; + + i = aval[j]; + aval[j] = aval[j+1]; + aval[j+1] = i; + + hp = ap[j]; + ap[j] = ap[j+1]; + ap[j+1] = hp; + + } else + break; + } + needsort++; + } +} +#endif diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c new file mode 100644 index 0000000000..925d1d5895 --- /dev/null +++ b/src/lib/libc/net/getnetbyaddr.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getnetbyaddr.c,v 1.5 1997/07/09 01:08:28 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include + +extern int _net_stayopen; + +struct netent * +_getnetbyaddr(net, type) + register in_addr_t net; + register int type; +{ + register struct netent *p; + + setnetent(_net_stayopen); + while ((p = getnetent())) + if (p->n_addrtype == type && p->n_net == net) + break; + if (!_net_stayopen) + endnetent(); + return (p); +} diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c new file mode 100644 index 0000000000..4e39cf6860 --- /dev/null +++ b/src/lib/libc/net/getnetbyname.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getnetbyname.c,v 1.5 1997/07/09 01:08:29 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +extern int _net_stayopen; + +struct netent * +_getnetbyname(name) + register const char *name; +{ + register struct netent *p; + register char **cp; + + setnetent(_net_stayopen); + while ((p = getnetent())) { + if (strcasecmp(p->n_name, name) == 0) + break; + for (cp = p->n_aliases; *cp != 0; cp++) + if (strcasecmp(*cp, name) == 0) + goto found; + } +found: + if (!_net_stayopen) + endnetent(); + return (p); +} diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3 new file mode 100644 index 0000000000..5864b75839 --- /dev/null +++ b/src/lib/libc/net/getnetent.3 @@ -0,0 +1,148 @@ +.\" $OpenBSD: getnetent.3,v 1.5 1998/03/16 05:06:56 millert Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd March 13, 1997 +.Dt GETNETENT 3 +.Os +.Sh NAME +.Nm getnetent , +.Nm getnetbyaddr , +.Nm getnetbyname , +.Nm setnetent , +.Nm endnetent +.Nd get network entry +.Sh SYNOPSIS +.Fd #include +.Ft struct netent * +.Fn getnetent +.Ft struct netent * +.Fn getnetbyname "char *name" +.Ft struct netent * +.Fn getnetbyaddr "in_addr_t net" "int type" +.Fn setnetent "int stayopen" +.Fn endnetent +.Sh DESCRIPTION +The +.Fn getnetent , +.Fn getnetbyname , +and +.Fn getnetbyaddr +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network data base, +.Pa /etc/networks . +.Bd -literal -offset indent +struct netent { + char *n_name; /* official name of net */ + char **n_aliases; /* alias list */ + int n_addrtype; /* net number type */ + in_addr_t n_net; /* net number */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width n_addrtype +.It Fa n_name +The official name of the network. +.It Fa n_aliases +A zero terminated list of alternate names for the network. +.It Fa n_addrtype +The type of the network number returned; currently only AF_INET. +.It Fa n_net +The network number. Network numbers are returned in machine byte +order. +.El +.Pp +The +.Fn getnetent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setnetent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getnetbyname +or +.Fn getnetbyaddr . +.Pp +The +.Fn endnetent +function +closes the file. +.Pp +The +.Fn getnetbyname +function +and +.Fn getnetbyaddr +search the domain name server if the system is configured to use one. +If the search fails, or no name server is configured, they sequentially +search from the beginning of the file until a matching net name or +net address and type is found, or until +.Dv EOF +is encountered. +Network numbers are supplied in host order. +.Sh FILES +.Bl -tag -width /etc/networks -compact +.It Pa /etc/networks +.El +.Sh DIAGNOSTICS +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh SEE ALSO +.Xr networks 5 , +.Xr resolver 3 +.Sh HISTORY +The +.Fn getnetent , +.Fn getnetbyaddr , +.Fn getnetbyname , +.Fn setnetent , +and +.Fn endnetent +functions appeared in +.Bx 4.2 . +.Sh BUGS +The data space used by these functions is static; if future use +requires the data, it should be copied before any subsequent calls +to these functions overwrite it. Only Internet network numbers +are currently understood. Expecting network numbers to fit in no +more than 32 bits is naive. diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..8f618a1d5e --- /dev/null +++ b/src/lib/libc/net/getnetent.c @@ -0,0 +1,129 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getnetent.c,v 1.8 1998/03/16 05:06:57 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +#define MAXALIASES 35 + +static FILE *netf; +static char line[BUFSIZ+1]; +static struct netent net; +static char *net_aliases[MAXALIASES]; +int _net_stayopen; + +void +setnetent(f) + int f; +{ + if (netf == NULL) + netf = fopen(_PATH_NETWORKS, "r" ); + else + rewind(netf); + _net_stayopen |= f; +} + +void +endnetent() +{ + if (netf) { + fclose(netf); + netf = NULL; + } + _net_stayopen = 0; +} + +struct netent * +getnetent() +{ + char *p, *cp, **q; + size_t len; + + if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) + return (NULL); +again: + if ((p = fgetln(netf, &len)) == NULL) + return (NULL); + if (p[len-1] == '\n') + len--; + if (len >= sizeof(line) || len == 0) + goto again; + p = memcpy(line, p, len); + line[len] = '\0'; + if (*p == '#') + goto again; + if ((cp = strchr(p, '#')) != NULL) + *cp = '\0'; + net.n_name = p; + if (strlen(net.n_name) >= MAXHOSTNAMELEN-1) + net.n_name[MAXHOSTNAMELEN-1] = '\0'; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net.n_net = inet_network(cp); + net.n_addrtype = AF_INET; + q = net.n_aliases = net_aliases; + if (p != NULL) + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) { + *q++ = cp; + if (strlen(cp) >= MAXHOSTNAMELEN-1) + cp[MAXHOSTNAMELEN-1] = '\0'; + } + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + *q = NULL; + return (&net); +} 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 @@ +/* $OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $ */ + +/* + * Copyright (c) 1997, Jason Downs. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Jason Downs for the + * OpenBSD system. + * 4. Neither the name(s) of the author(s) nor the name OpenBSD + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro + * Dep. Matematica Universidade de Coimbra, Portugal, Europe + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; +static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; +static char rcsid[] = "$From: getnetnamadr.c,v 8.7 1996/08/05 08:31:35 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +extern int h_errno; + +struct netent *_getnetbyaddr __P((in_addr_t net, int type)); +struct netent *_getnetbyname __P((const char *name)); + +int _hokchar __P((const char *)); + +#define BYADDR 0 +#define BYNAME 1 +#define MAXALIASES 35 + +#if PACKETSZ > 1024 +#define MAXPACKET PACKETSZ +#else +#define MAXPACKET 1024 +#endif + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + long al; + char ac; +} align; + +static struct netent * +getnetanswer(answer, anslen, net_i) + querybuf *answer; + int anslen; + int net_i; +{ + + register HEADER *hp; + register u_char *cp; + register int n; + u_char *eom; + int type, class, buflen, ancount, qdcount, haveanswer, i, nchar; + char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; + char *in, *st, *pauxt, *bp, **ap; + char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; + static struct netent net_entry; + static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1]; + + /* + * find first satisfactory answer + * + * answer --> +------------+ ( MESSAGE ) + * | Header | + * +------------+ + * | Question | the question for the name server + * +------------+ + * | Answer | RRs answering the question + * +------------+ + * | Authority | RRs pointing toward an authority + * | Additional | RRs holding additional information + * +------------+ + */ + eom = answer->buf + anslen; + hp = &answer->hdr; + ancount = ntohs(hp->ancount); /* #/records in the answer section */ + qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ + bp = netbuf; + buflen = sizeof(netbuf); + cp = answer->buf + HFIXEDSZ; + if (!qdcount) { + if (hp->aa) + h_errno = HOST_NOT_FOUND; + else + h_errno = TRY_AGAIN; + return (NULL); + } + while (qdcount-- > 0) + cp += __dn_skipname(cp, eom) + QFIXEDSZ; + ap = net_aliases; + *ap = NULL; + net_entry.n_aliases = net_aliases; + haveanswer = 0; + while (--ancount >= 0 && cp < eom) { + n = dn_expand(answer->buf, eom, cp, bp, buflen); +#ifdef USE_RESOLV_NAME_OK + if ((n < 0) || !res_dnok(bp)) +#else + if ((n < 0) || !_hokchar(bp)) +#endif + break; + cp += n; + ans[0] = '\0'; + (void)strncpy(&ans[0], bp, sizeof ans-1); + ans[sizeof ans-1] = '\0'; + GETSHORT(type, cp); + GETSHORT(class, cp); + cp += INT32SZ; /* TTL */ + GETSHORT(n, cp); + if (class == C_IN && type == T_PTR) { + n = dn_expand(answer->buf, eom, cp, bp, buflen); + if ((n < 0) || !res_hnok(bp)) { + cp += n; + return (NULL); + } + cp += n; + *ap++ = bp; + bp += strlen(bp) + 1; + net_entry.n_addrtype = + (class == C_IN) ? AF_INET : AF_UNSPEC; + haveanswer++; + } + } + if (haveanswer) { + *ap = NULL; + switch (net_i) { + case BYADDR: + net_entry.n_name = *net_entry.n_aliases; + net_entry.n_net = 0L; + break; + case BYNAME: + in = *net_entry.n_aliases; + net_entry.n_name = &ans[0]; + aux2[0] = '\0'; + for (i = 0; i < 4; i++) { + for (st = in, nchar = 0; + *st != '.'; + st++, nchar++) + ; + if (nchar != 1 || *in != '0' || flag) { + flag = 1; + (void)strncpy(paux1, + (i==0) ? in : in-1, + (i==0) ?nchar : nchar+1); + paux1[(i==0) ? nchar : nchar+1] = '\0'; + pauxt = paux2; + paux2 = strcat(paux1, paux2); + paux1 = pauxt; + } + in = ++st; + } + net_entry.n_net = inet_network(paux2); + break; + } + net_entry.n_aliases++; + return (&net_entry); + } + h_errno = TRY_AGAIN; + return (NULL); +} + +struct netent * +getnetbyaddr(net, net_type) + register in_addr_t net; + register int net_type; +{ + unsigned int netbr[4]; + int nn, anslen; + querybuf buf; + char qbuf[MAXDNAME]; + in_addr_t net2; + struct netent *net_entry = NULL; + char lookups[MAXDNSLUS]; + int i; + + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + return(_getnetbyaddr(net, net_type)); + + bcopy(_res.lookups, lookups, sizeof lookups); + if (lookups[0] == '\0') + strncpy(lookups, "bf", sizeof lookups); + + for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { + switch (lookups[i]) { +#ifdef YP + case 'y': + /* There is no YP support. */ + break; +#endif /* YP */ + case 'b': + if (net_type != AF_INET) + break; /* DNS only supports AF_INET? */ + + for (nn = 4, net2 = net; net2; net2 >>= 8) + netbr[--nn] = net2 & 0xff; + switch (nn) { + case 3: /* Class A */ + snprintf(qbuf, sizeof(qbuf), + "0.0.0.%u.in-addr.arpa", netbr[3]); + break; + case 2: /* Class B */ + snprintf(qbuf, sizeof(qbuf), + "0.0.%u.%u.in-addr.arpa", + netbr[3], netbr[2]); + break; + case 1: /* Class C */ + snprintf(qbuf, sizeof(qbuf), + "0.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1]); + break; + case 0: /* Class D - E */ + snprintf(qbuf, sizeof(qbuf), + "%u.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1], netbr[0]); + break; + } + anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, + sizeof(buf)); + if (anslen < 0) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("res_query failed\n"); +#endif + break; + } + net_entry = getnetanswer(&buf, anslen, BYADDR); + if (net_entry != NULL) { + unsigned u_net = net; /* maybe net should be unsigned ? */ + + /* Strip trailing zeros */ + while ((u_net & 0xff) == 0 && u_net != 0) + u_net >>= 8; + net_entry->n_net = u_net; + return (net_entry); + } + break; + case 'f': + net_entry = _getnetbyaddr(net, net_type); + if (net_entry != NULL) + return (net_entry); + } + } + + /* Nothing matched. */ + return (NULL); +} + +struct netent * +getnetbyname(net) + register const char *net; +{ + int anslen; + querybuf buf; + char qbuf[MAXDNAME]; + struct netent *net_entry = NULL; + char lookups[MAXDNSLUS]; + int i; + + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + return (_getnetbyname(net)); + + bcopy(_res.lookups, lookups, sizeof lookups); + if (lookups[0] == '\0') + strncpy(lookups, "bf", sizeof lookups); + + for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { + switch (lookups[i]) { +#ifdef YP + case 'y': + /* There is no YP support. */ + break; +#endif /* YP */ + case 'b': + strncpy(qbuf, net, sizeof qbuf-1); + qbuf[sizeof qbuf-1] = '\0'; + anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, + sizeof(buf)); + if (anslen < 0) { +#ifdef DEBUG + if (_res.options & RES_DEBUG) + printf("res_query failed\n"); +#endif + break; + } + net_entry = getnetanswer(&buf, anslen, BYNAME); + if (net_entry != NULL) + return (net_entry); + break; + case 'f': + net_entry = _getnetbyname(net); + if (net_entry != NULL) + return (net_entry); + break; + } + } + + /* Nothing matched. */ + return (NULL); +} diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c new file mode 100644 index 0000000000..474d8d9427 --- /dev/null +++ b/src/lib/libc/net/getproto.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getproto.c,v 1.3 1997/07/09 01:08:31 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include + +extern int _proto_stayopen; + +struct protoent * +getprotobynumber(proto) + register int proto; +{ + register struct protoent *p; + + setprotoent(_proto_stayopen); + while ((p = getprotoent())) + if (p->p_proto == proto) + break; + if (!_proto_stayopen) + endprotoent(); + return (p); +} diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 new file mode 100644 index 0000000000..f67987954f --- /dev/null +++ b/src/lib/libc/net/getprotoent.3 @@ -0,0 +1,144 @@ +.\" $OpenBSD: getprotoent.3,v 1.2 1996/08/19 08:28:50 tholo Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt GETPROTOENT 3 +.Os BSD 4.2 +.Sh NAME +.Nm getprotoent , +.Nm getprotobynumber , +.Nm getprotobyname , +.Nm setprotoent , +.Nm endprotoent +.Nd get protocol entry +.Sh SYNOPSIS +.Fd #include +.Ft struct protoent * +.Fn getprotoent +.Ft struct protoent * +.Fn getprotobyname "char *name" +.Ft struct protoent * +.Fn getprotobynumber "int proto" +.Fn setprotoent "int stayopen" +.Fn endprotoent +.Sh DESCRIPTION +The +.Fn getprotoent , +.Fn getprotobyname , +and +.Fn getprotobynumber +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network protocol data base, +.Pa /etc/protocols . +.Bd -literal -offset indent +.Pp +struct protoent { + char *p_name; /* official name of protocol */ + char **p_aliases; /* alias list */ + int p_proto; /* protocol number */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width p_aliases +.It Fa p_name +The official name of the protocol. +.It Fa p_aliases +A zero terminated list of alternate names for the protocol. +.It Fa p_proto +The protocol number. +.El +.Pp +The +.Fn getprotoent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setprotoent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getprotobyname +or +.Fn getprotobynumber . +.Pp +The +.Fn endprotoent +function +closes the file. +.Pp +The +.Fn getprotobyname +function +and +.Fn getprotobynumber +sequentially search from the beginning +of the file until a matching +protocol name or +protocol number is found, +or until +.Dv EOF +is encountered. +.Sh RETURN VALUES +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh FILES +.Bl -tag -width /etc/protocols -compact +.It Pa /etc/protocols +.El +.Sh SEE ALSO +.Xr protocols 5 +.Sh HISTORY +The +.Fn getprotoent , +.Fn getprotobynumber , +.Fn getprotobyname , +.Fn setprotoent , +and +.Fn endprotoent +functions appeared in +.Bx 4.2 . +.Sh BUGS +These functions use a static data space; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Only the Internet +protocols are currently understood. diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c new file mode 100644 index 0000000000..2bef526e7a --- /dev/null +++ b/src/lib/libc/net/getprotoent.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.3 1998/03/16 05:06:59 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#define MAXALIASES 35 + +static FILE *protof = NULL; +static char line[BUFSIZ+1]; +static struct protoent proto; +static char *proto_aliases[MAXALIASES]; +int _proto_stayopen; + +void +setprotoent(f) + int f; +{ + if (protof == NULL) + protof = fopen(_PATH_PROTOCOLS, "r" ); + else + rewind(protof); + _proto_stayopen |= f; +} + +void +endprotoent() +{ + if (protof) { + fclose(protof); + protof = NULL; + } + _proto_stayopen = 0; +} + +struct protoent * +getprotoent() +{ + char *p, *cp, **q; + size_t len; + + if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) + return (NULL); +again: + if ((p = fgetln(protof, &len)) == NULL) + return (NULL); + if (p[len-1] == '\n') + len--; + if (len >= sizeof(line) || len == 0) + goto again; + p = memcpy(line, p, len); + line[len] = '\0'; + if (*p == '#') + goto again; + if ((cp = strchr(p, '#')) != NULL) + *cp = '\0'; + proto.p_name = p; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + proto.p_proto = atoi(cp); + q = proto.p_aliases = proto_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &proto_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + } + *q = NULL; + return (&proto); +} diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c new file mode 100644 index 0000000000..7a4e5fede5 --- /dev/null +++ b/src/lib/libc/net/getprotoname.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getprotoname.c,v 1.3 1997/07/09 01:08:32 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +extern int _proto_stayopen; + +struct protoent * +getprotobyname(name) + register const char *name; +{ + register struct protoent *p; + register char **cp; + + setprotoent(_proto_stayopen); + while ((p = getprotoent())) { + if (strcmp(p->p_name, name) == 0) + break; + for (cp = p->p_aliases; *cp != 0; cp++) + if (strcmp(*cp, name) == 0) + goto found; + } +found: + if (!_proto_stayopen) + endprotoent(); + return (p); +} diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c new file mode 100644 index 0000000000..7375c89404 --- /dev/null +++ b/src/lib/libc/net/getservbyname.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.4 1998/11/20 11:18:44 d Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include "thread_private.h" + +extern int _serv_stayopen; + +_THREAD_PRIVATE_MUTEX(getservbyname_r) + +struct servent * +getservbyname_r(name, proto, se, buf, buflen) + const char *name, *proto; + struct servent *se; + char *buf; + int buflen; +{ + register struct servent *p; + register char **cp; + + _THREAD_PRIVATE_MUTEX_LOCK(getservbyname_r); + setservent(_serv_stayopen); + while ((p = getservent())) { + if (strcmp(name, p->s_name) == 0) + goto gotname; + for (cp = p->s_aliases; *cp; cp++) + if (strcmp(name, *cp) == 0) + goto gotname; + continue; +gotname: + if (proto == 0 || strcmp(p->s_proto, proto) == 0) + break; + } + if (!_serv_stayopen) + endservent(); + _THREAD_PRIVATE_MUTEX_UNLOCK(getservbyname_r); + return (p); +} + +struct servent *getservbyname(name, proto) + const char *name, *proto; +{ + _THREAD_PRIVATE_KEY(getservbyname) + static char buf[4096]; + char *bufp = (char*)_THREAD_PRIVATE(getservbyname, buf, NULL); + + if (bufp == NULL) + return (NULL); + return getservbyname_r(name, proto, (struct servent*) bufp, + bufp + sizeof(struct servent), + sizeof buf - sizeof(struct servent) ); +} diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c new file mode 100644 index 0000000000..4b063760d2 --- /dev/null +++ b/src/lib/libc/net/getservbyport.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getservbyport.c,v 1.3 1997/07/09 01:08:35 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +extern int _serv_stayopen; + +struct servent * +getservbyport(port, proto) + int port; + const char *proto; +{ + register struct servent *p; + + setservent(_serv_stayopen); + while ((p = getservent())) { + if (p->s_port != port) + continue; + if (proto == 0 || strcmp(p->s_proto, proto) == 0) + break; + } + if (!_serv_stayopen) + endservent(); + return (p); +} diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 new file mode 100644 index 0000000000..3bbcc38a6b --- /dev/null +++ b/src/lib/libc/net/getservent.3 @@ -0,0 +1,155 @@ +.\" $OpenBSD: getservent.3,v 1.4 1999/03/02 03:47:46 deraadt Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd January 12, 1994 +.Dt GETSERVENT 3 +.Os BSD 4.2 +.Sh NAME +.Nm getservent , +.Nm getservbyport , +.Nm getservbyname , +.Nm setservent , +.Nm endservent +.Nd get service entry +.Sh SYNOPSIS +.Fd #include +.Ft struct servent * +.Fn getservent +.Ft struct servent * +.Fn getservbyname "char *name" "char *proto" +.Ft struct servent * +.Fn getservbyport "int port" "char *proto" +.Ft void +.Fn setservent "int stayopen" +.Ft void +.Fn endservent void +.Sh DESCRIPTION +The +.Fn getservent , +.Fn getservbyname , +and +.Fn getservbyport +functions +each return a pointer to an object with the +following structure +containing the broken-out +fields of a line in the network services data base, +.Pa /etc/services . +.Bd -literal -offset indent +struct servent { + char *s_name; /* official name of service */ + char **s_aliases; /* alias list */ + int s_port; /* port service resides at */ + char *s_proto; /* protocol to use */ +}; +.Ed +.Pp +The members of this structure are: +.Bl -tag -width s_aliases +.It Fa s_name +The official name of the service. +.It Fa s_aliases +A zero terminated list of alternate names for the service. +.It Fa s_port +The port number at which the service resides. +Port numbers are returned in network byte order. +.It Fa s_proto +The name of the protocol to use when contacting the +service. +.El +.Pp +The +.Fn getservent +function +reads the next line of the file, opening the file if necessary. +.Pp +The +.Fn setservent +function +opens and rewinds the file. If the +.Fa stayopen +flag is non-zero, +the net data base will not be closed after each call to +.Fn getservbyname +or +.Fn getservbyport . +.Pp +The +.Fn endservent +function +closes the file. +.Pp +The +.Fn getservbyname +and +.Fn getservbyport +functions +sequentially search from the beginning +of the file until a matching +protocol name or +port number (specified in network byte order) is found, +or until +.Dv EOF +is encountered. +If a protocol name is also supplied (non- +.Dv NULL ) , +searches must also match the protocol. +.ne 1i +.Sh FILES +.Bl -tag -width /etc/services -compact +.It Pa /etc/services +.El +.Sh DIAGNOSTICS +Null pointer +(0) returned on +.Dv EOF +or error. +.Sh SEE ALSO +.Xr getprotoent 3 , +.Xr services 5 +.Sh HISTORY +The +.Fn getservent , +.Fn getservbyport , +.Fn getservbyname , +.Fn setservent , +and +.Fn endservent +functions appeared in +.Bx 4.2 . +.Sh BUGS +These functions use static data storage; +if the data is needed for future use, it should be +copied before any subsequent calls overwrite it. +Expecting port numbers to fit in a 32 bit +quantity is probably naive. diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..7d8cb6d8ca --- /dev/null +++ b/src/lib/libc/net/getservent.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: getservent.c,v 1.4 1998/03/16 05:07:00 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +#define MAXALIASES 35 + +static FILE *servf = NULL; +static char line[BUFSIZ+1]; +static struct servent serv; +static char *serv_aliases[MAXALIASES]; +int _serv_stayopen; + +void +setservent(f) + int f; +{ + if (servf == NULL) + servf = fopen(_PATH_SERVICES, "r" ); + else + rewind(servf); + _serv_stayopen |= f; +} + +void +endservent() +{ + if (servf) { + fclose(servf); + servf = NULL; + } + _serv_stayopen = 0; +} + +struct servent * +getservent() +{ + char *p, *cp, **q; + size_t len; + + if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) + return (NULL); +again: + if ((p = fgetln(servf, &len)) == NULL) + return (NULL); + if (p[len-1] == '\n') + len--; + if (len >= sizeof(line) || len == 0) + goto again; + p = memcpy(line, p, len); + line[len] = '\0'; + if (*p == '#') + goto again; + if ((cp = strchr(p, '#')) != NULL) + *cp = '\0'; + serv.s_name = p; + p = strpbrk(p, " \t"); + if (p == NULL) + goto again; + *p++ = '\0'; + while (*p == ' ' || *p == '\t') + p++; + cp = strpbrk(p, ",/"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + serv.s_port = htons((in_port_t)atoi(p)); + serv.s_proto = cp; + q = serv.s_aliases = serv_aliases; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &serv_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + *q = NULL; + return (&serv); +} diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c new file mode 100644 index 0000000000..737bb115a7 --- /dev/null +++ b/src/lib/libc/net/herror.c @@ -0,0 +1,121 @@ +/* $OpenBSD: herror.c,v 1.4 1997/03/13 19:07:28 downsj Exp $ */ + +/* + * ++Copyright++ 1987, 1993 + * - + * Copyright (c) 1987, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "$From: herror.c,v 8.3 1996/08/05 08:31:35 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: herror.c,v 1.4 1997/03/13 19:07:28 downsj Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include + +const char *h_errlist[] = { + "Resolver Error 0 (no error)", + "Unknown host", /* 1 HOST_NOT_FOUND */ + "Host name lookup failure", /* 2 TRY_AGAIN */ + "Unknown server error", /* 3 NO_RECOVERY */ + "No address associated with name", /* 4 NO_ADDRESS */ +}; +int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] }; + +extern int h_errno; + +/* + * herror -- + * print the error indicated by the h_errno value. + */ +void +herror(s) + const char *s; +{ + struct iovec iov[4]; + register struct iovec *v = iov; + + if (s && *s) { + v->iov_base = (char *)s; + v->iov_len = strlen(s); + v++; + v->iov_base = ": "; + v->iov_len = 2; + v++; + } + v->iov_base = (char *)hstrerror(h_errno); + v->iov_len = strlen(v->iov_base); + v++; + v->iov_base = "\n"; + v->iov_len = 1; + writev(STDERR_FILENO, iov, (v - iov) + 1); +} + +const char * +hstrerror(err) + int err; +{ + if (err < 0) + return ("Resolver internal error"); + else if (err < h_nerr) + return (h_errlist[err]); + return ("Unknown resolver error"); +} diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c new file mode 100644 index 0000000000..73b7432731 --- /dev/null +++ b/src/lib/libc/net/htonl.c @@ -0,0 +1,25 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: htonl.c,v 1.4 1996/12/12 03:19:55 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef htonl + +u_int32_t +htonl(x) + u_int32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c new file mode 100644 index 0000000000..47cf25952d --- /dev/null +++ b/src/lib/libc/net/htons.c @@ -0,0 +1,29 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: htons.c,v 1.6 1997/07/25 20:30:07 mickey Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef htons + +u_int16_t +#ifdef __STDC__ +htons(u_int16_t x) +#else +htons(x) + u_int16_t x; +#endif +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (u_int16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3 new file mode 100644 index 0000000000..2fb86cd927 --- /dev/null +++ b/src/lib/libc/net/inet.3 @@ -0,0 +1,319 @@ +.\" $OpenBSD: inet.3,v 1.4 1997/06/23 04:01:11 millert Exp $ +.\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $ +.\" +.\" Copyright (c) 1983, 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)inet.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd June 18, 1997 +.Dt INET 3 +.Os BSD 4.2 +.Sh NAME +.Nm inet_addr , +.Nm inet_aton , +.Nm inet_lnaof , +.Nm inet_makeaddr , +.Nm inet_netof , +.Nm inet_network , +.Nm inet_ntoa , +.Nm inet_ntop , +.Nm inet_pton +.Nd Internet address manipulation routines +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Fd #include +.Ft in_addr_t +.Fn inet_addr "const char *cp" +.Ft int +.Fn inet_aton "const char *cp" "struct in_addr *addr" +.Ft in_addr_t +.Fn inet_lnaof "struct in_addr in" +.Ft struct in_addr +.Fn inet_makeaddr "unsigned long net" "unsigned long lna" +.Ft in_addr_t +.Fn inet_netof "struct in_addr in" +.Ft in_addr_t +.Fn inet_network "const char *cp" +.Ft char * +.Fn inet_ntoa "struct in_addr in" +.Ft const char * +.Fn inet_ntop "int af" "const void *src" "char *dst" "size_t size" +.Ft int +.Fn inet_pton "int af" "const char *src" "void *dst" +.Sh DESCRIPTION +The routines +.Fn inet_aton , +.Fn inet_addr +and +.Fn inet_network +interpret character strings representing +numbers expressed in the Internet standard +.Ql \&. +notation. +The +.Fn inet_pton +function converts a presentation format address (that is, printable form +as held in a character string) to network format (usually a +.Ft struct in_addr +or some other internal binary representation, in network byte order). It +returns 1 if the address was valid for the specified address family, or +0 if the address wasn't parseable in the specified address family, or -1 +if some system error occurred (in which case +.Va errno +will have been set). This function is presently valid for AF_INET and +AF_INET6. The +.Fn inet_aton +routine interprets the specified character string as an Internet address, +placing the address into the structure provided. +It returns 1 if the string was successfully interpreted, +or 0 if the string is invalid. +The +.Fn inet_addr +and +.Fn inet_network +functions return numbers suitable for use +as Internet addresses and Internet network +numbers, respectively. +.Pp +The function +.Fn inet_ntop +converts an address from network format (usually a +.Ft struct in_addr +or some other binary form, in network byte order) to presentation format +(suitable for external display purposes). It returns NULL if a system +error occurs (in which case, +.Va errno +will have been set), or it returns a pointer to the destination string. +The routine +.Fn inet_ntoa +takes an Internet address and returns an +.Tn ASCII +string representing the address in +.Ql \&. +notation. The routine +.Fn inet_makeaddr +takes an Internet network number and a local +network address and constructs an Internet address +from it. The routines +.Fn inet_netof +and +.Fn inet_lnaof +break apart Internet host addresses, returning +the network number and local network address part, +respectively. +.Pp +All Internet addresses are returned in network +order (bytes ordered from left to right). +All network numbers and local address parts are +returned as machine format integer values. +.Sh INTERNET ADDRESSES (IP VERSION 4) +Values specified using the +.Ql \&. +notation take one +of the following forms: +.Bd -literal -offset indent +a.b.c.d +a.b.c +a.b +a +.Ed +.Pp +When four parts are specified, each is interpreted +as a byte of data and assigned, from left to right, +to the four bytes of an Internet address. Note +that when an Internet address is viewed as a 32-bit +integer quantity on a system that uses little-endian +byte order (such as the +.Tn Intel 386, 486 +and +.Tn Pentium +processors) the bytes referred to above appear as +.Dq Li d.c.b.a . +That is, little-endian bytes are ordered from right to left. +.Pp +When a three part address is specified, the last +part is interpreted as a 16-bit quantity and placed +in the right-most two bytes of the network address. +This makes the three part address format convenient +for specifying Class B network addresses as +.Dq Li 128.net.host . +.Pp +When a two part address is supplied, the last part +is interpreted as a 24-bit quantity and placed in +the right most three bytes of the network address. +This makes the two part address format convenient +for specifying Class A network addresses as +.Dq Li net.host . +.Pp +When only one part is given, the value is stored +directly in the network address without any byte +rearrangement. +.Pp +All numbers supplied as +.Dq parts +in a +.Ql \&. +notation +may be decimal, octal, or hexadecimal, as specified +in the C language (i.e., a leading 0x or 0X implies +hexadecimal; otherwise, a leading 0 implies octal; +otherwise, the number is interpreted as decimal). +.Sh INTERNET ADDRESSES (IP VERSION 6) +The presentation format of an IPv6 address is given in [RFC1884 2.2]: +.Pp +There are three conventional forms for representing IPv6 addresses as +text strings: +.Bl -enum +.It +The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the +hexadecimal values of the eight 16-bit pieces of the address. +Examples: +.Bd -literal -offset indent +FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 +1080:0:0:0:8:800:200C:417A +.Ed +.Pp +Note that it is not necessary to write the leading zeros in an +individual field, but there must be at least one numeral in +every field (except for the case described in 2.). +.It +Due to the method of allocating certain styles of IPv6 +addresses, it will be common for addresses to contain long +strings of zero bits. In order to make writing addresses +.Pp +containing zero bits easier a special syntax is available to +compress the zeros. The use of ``::'' indicates multiple groups +of 16-bits of zeros. The ``::'' can only appear once in an +address. The ``::'' can also be used to compress the leading +and/or trailing zeros in an address. +.Pp +For example the following addresses: +.Bd -literal -offset indent +1080:0:0:0:8:800:200C:417A a unicast address +FF01:0:0:0:0:0:0:43 a multicast address +0:0:0:0:0:0:0:1 the loopback address +0:0:0:0:0:0:0:0 the unspecified addresses +.Ed +.Pp +may be represented as: +.Bd -literal -offset indent +1080::8:800:200C:417A a unicast address +FF01::43 a multicast address +::1 the loopback address +:: the unspecified addresses +.Ed +.It +An alternative form that is sometimes more convenient when +dealing with a mixed environment of IPv4 and IPv6 nodes is +x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values +of the six high-order 16-bit pieces of the address, and the 'd's +are the decimal values of the four low-order 8-bit pieces of the +address (standard IPv4 representation). Examples: +.Bd -literal -offset indent +0:0:0:0:0:0:13.1.68.3 +0:0:0:0:0:FFFF:129.144.52.38 +.Ed +.Pp +or in compressed form: +.Bd -literal -offset indent +::13.1.68.3 +::FFFF:129.144.52.38 +.Ed +.El +.Sh DIAGNOSTICS +The constant +.Dv INADDR_NONE +is returned by +.Fn inet_addr +and +.Fn inet_network +for malformed requests. +.Sh SEE ALSO +.Xr byteorder 3 , +.Xr gethostbyname 3 , +.Xr getnetent 3 , +.Xr inet_net 3 , +.Xr hosts 5 , +.Xr networks 5 +.Sh STANDARDS +The +.Nm inet_ntop +and +.Nm inet_pton +functions conforms to the IETF IPng BSD API and address formatting +specifications. Note that +.Nm inet_pton +does not accept 1-, 2-, or 3-part dotted addresses; all four parts +must be specified. This is a narrower input set than that accepted by +.Nm inet_aton . +.Sh HISTORY +The +.Nm inet_addr , +.Nm inet_network , +.Nm inet_makeaddr , +.Nm inet_lnaof +and +.Nm inet_netof +functions appeared in +.Bx 4.2 . +The +.Nm inet_aton +and +.Nm inet_ntoa +functions appeared in +.Bx 4.3 . +The +.Nm inet_pton +and +.Nm inet_ntop +functions appeared in BIND 4.9.4. +.Sh BUGS +The value +.Dv INADDR_NONE +(0xffffffff) is a valid broadcast address, but +.Fn inet_addr +cannot return that value without indicating failure. +The newer +.Fn inet_aton +function does not share this problem. +.Pp +The problem of host byte ordering versus network byte ordering is +confusing. +.Pp +The string returned by +.Fn inet_ntoa +resides in a static memory area. +.Pp +.Fn inet_addr +should return a +.Fa "struct in_addr" . diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c new file mode 100644 index 0000000000..5e4dcdafb2 --- /dev/null +++ b/src/lib/libc/net/inet_addr.c @@ -0,0 +1,187 @@ +/* $OpenBSD: inet_addr.c,v 1.5 1997/04/05 21:13:10 millert Exp $ */ + +/* + * ++Copyright++ 1983, 1990, 1993 + * - + * Copyright (c) 1983, 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; +static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.5 1997/04/05 21:13:10 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include + +/* + * Ascii internet address interpretation routine. + * The value returned is in network order. + */ +in_addr_t +inet_addr(cp) + register const char *cp; +{ + struct in_addr val; + + if (inet_aton(cp, &val)) + return (val.s_addr); + return (INADDR_NONE); +} + +/* + * Check whether "cp" is a valid ascii representation + * of an Internet address and convert to a binary address. + * Returns 1 if the address is valid, 0 if not. + * This replaces inet_addr, the return value from which + * cannot distinguish between failure and a local broadcast address. + */ +int +inet_aton(cp, addr) + register const char *cp; + struct in_addr *addr; +{ + register in_addr_t val; + register int base, n; + register char c; + u_int parts[4]; + register u_int *pp = parts; + + c = *cp; + for (;;) { + /* + * Collect number up to ``.''. + * Values are specified as for C: + * 0x=hex, 0=octal, isdigit=decimal. + */ + if (!isdigit(c)) + return (0); + val = 0; base = 10; + if (c == '0') { + c = *++cp; + if (c == 'x' || c == 'X') + base = 16, c = *++cp; + else + base = 8; + } + for (;;) { + if (isascii(c) && isdigit(c)) { + val = (val * base) + (c - '0'); + c = *++cp; + } else if (base == 16 && isascii(c) && isxdigit(c)) { + val = (val << 4) | + (c + 10 - (islower(c) ? 'a' : 'A')); + c = *++cp; + } else + break; + } + if (c == '.') { + /* + * Internet format: + * a.b.c.d + * a.b.c (with c treated as 16 bits) + * a.b (with b treated as 24 bits) + */ + if (pp >= parts + 3) + return (0); + *pp++ = val; + c = *++cp; + } else + break; + } + /* + * Check for trailing characters. + */ + if (c != '\0' && (!isascii(c) || !isspace(c))) + return (0); + /* + * Concoct the address according to + * the number of parts specified. + */ + n = pp - parts + 1; + switch (n) { + + case 0: + return (0); /* initial nondigit */ + + case 1: /* a -- 32 bits */ + break; + + case 2: /* a.b -- 8.24 bits */ + if (val > 0xffffff) + return (0); + val |= parts[0] << 24; + break; + + case 3: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 4: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + if (addr) + addr->s_addr = htonl(val); + return (1); +} diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c new file mode 100644 index 0000000000..6aed18699b --- /dev/null +++ b/src/lib/libc/net/inet_lnaof.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: inet_lnaof.c,v 1.3 1997/04/05 21:13:11 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* + * Return the local network address portion of an + * internet address; handles class a/b/c network + * number formats. + */ +in_addr_t +inet_lnaof(in) + struct in_addr in; +{ + register in_addr_t i = ntohl(in.s_addr); + + if (IN_CLASSA(i)) + return ((i)&IN_CLASSA_HOST); + else if (IN_CLASSB(i)) + return ((i)&IN_CLASSB_HOST); + else + return ((i)&IN_CLASSC_HOST); +} diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c new file mode 100644 index 0000000000..196a589e4c --- /dev/null +++ b/src/lib/libc/net/inet_makeaddr.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: inet_makeaddr.c,v 1.3 1997/04/05 21:13:12 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* + * Formulate an Internet address from network + host. Used in + * building addresses stored in the ifnet structure. + */ +struct in_addr +inet_makeaddr(net, host) + in_addr_t net, host; +{ + in_addr_t addr; + + if (net < 128) + addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); + else if (net < 65536) + addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); + else if (net < 16777216L) + addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); + else + addr = net | host; + addr = htonl(addr); + return (*(struct in_addr *)&addr); +} 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 @@ +.\" $OpenBSD: inet_net.3,v 1.1 1997/06/23 03:37:26 millert Exp $ +.\" $NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $ +.\" +.\" Copyright (c) 1997 The NetBSD Foundation, Inc. +.\" All rights reserved. +.\" +.\" This code is derived from software contributed to The NetBSD Foundation +.\" by Luke Mewburn. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the NetBSD +.\" Foundation, Inc. and its contributors. +.\" 4. Neither the name of The NetBSD Foundation nor the names of its +.\" contributors may be used to endorse or promote products derived +.\" from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE +.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd June 18, 1997 +.Dt INET_NET 3 +.Os +.Sh NAME +.Nm inet_net_ntop , +.Nm inet_net_pton +.Nd Internet network number manipulation routines +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Fd #include +.Ft char * +.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size" +.Ft int +.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size" +.Sh DESCRIPTION +The +.Fn inet_net_ntop +function converts an Internet network number from network format (usually a +.Ft struct in_addr +or some other binary form, in network byte order) to CIDR presentation format +(suitable for external display purposes). +.Fa bits +is the number of bits in +.Fa src +that are the network number. +It returns NULL if a system error occurs (in which case, +.Va errno +will have been set), or it returns a pointer to the destination string. +.Pp +The +.Fn inet_net_pton +function converts a presentation format Internet network number (that is, +printable form as held in a character string) to network format (usually a +.Ft struct in_addr +or some other internal binary representation, in network byte order). +It returns the number of bits (either computed based on the class, or +specified with /CIDR), or -1 if a failure occurred +(in which case +.Va errno +will have been set. +It will be set to +.Er ENOENT +if the Internet network number was not valid). +.Pp +The currently supported value for +.Fa af +is: AF_INET. +.Fa size +is the size of the result buffer +.Fa dst . +.Pp +.Sh NETWORK NUMBERS (IP VERSION 4) +Internet network numbers may be specified in one of the following forms: +.Bd -literal -offset indent +a.b.c.d/bits +a.b.c.d +a.b.c +a.b +a +.Ed +.Pp +When four parts are specified, each is interpreted +as a byte of data and assigned, from left to right, +to the four bytes of an Internet network number. Note +that when an Internet network number is viewed as a 32-bit +integer quantity on a system that uses little-endian +byte order (such as the +.Tn Intel 386, 486 +and +.Tn Pentium +processors) the bytes referred to above appear as +.Dq Li d.c.b.a . +That is, little-endian bytes are ordered from right to left. +.Pp +When a three part number is specified, the last +part is interpreted as a 16-bit quantity and placed +in the right-most two bytes of the Internet network number. +This makes the three part number format convenient +for specifying Class B network numbers as +.Dq Li 128.net.host . +.Pp +When a two part number is supplied, the last part +is interpreted as a 24-bit quantity and placed in +the right most three bytes of the Internet network number. +This makes the two part number format convenient +for specifying Class A network numbers as +.Dq Li net.host . +.Pp +When only one part is given, the value is stored +directly in the Internet network number without any byte +rearrangement. +.Pp +All numbers supplied as +.Dq parts +in a +.Ql \&. +notation +may be decimal, octal, or hexadecimal, as specified +in the C language (i.e., a leading 0x or 0X implies +hexadecimal; otherwise, a leading 0 implies octal; +otherwise, the number is interpreted as decimal). +.Sh SEE ALSO +.Xr byteorder 3 , +.Xr inet 3 , +.Xr networks 5 +.Sh HISTORY +The +.Nm inet_net_ntop +and +.Nm inet_net_pton +functions 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 @@ +/* $OpenBSD: inet_net_ntop.c,v 1.1 1997/03/13 19:07:30 downsj Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "$From: inet_net_ntop.c,v 8.2 1996/08/08 06:54:44 vixie Exp $"; +#else +static const char rcsid[] = "$OpenBSD: inet_net_ntop.c,v 1.1 1997/03/13 19:07:30 downsj Exp $"; +#endif +#endif + +#include +#include +#include +#include + +#include +#include +#include +#include + +static char * inet_net_ntop_ipv4 __P((const u_char *src, int bits, + char *dst, size_t size)); + +/* + * char * + * inet_net_ntop(af, src, bits, dst, size) + * convert network number from network to presentation format. + * generates CIDR style result always. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * author: + * Paul Vixie (ISC), July 1996 + */ +char * +inet_net_ntop(af, src, bits, dst, size) + int af; + const void *src; + int bits; + char *dst; + size_t size; +{ + switch (af) { + case AF_INET: + return (inet_net_ntop_ipv4(src, bits, dst, size)); + default: + errno = EAFNOSUPPORT; + return (NULL); + } +} + +/* + * static char * + * inet_net_ntop_ipv4(src, bits, dst, size) + * convert IPv4 network number from network to presentation format. + * generates CIDR style result always. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0x11110000 in its fourth octet. + * author: + * Paul Vixie (ISC), July 1996 + */ +static char * +inet_net_ntop_ipv4(src, bits, dst, size) + const u_char *src; + int bits; + char *dst; + size_t size; +{ + char *odst = dst; + char *t; + u_int m; + int b; + + if (bits < 0 || bits > 32) { + errno = EINVAL; + return (NULL); + } + if (bits == 0) { + if (size < sizeof "0") + goto emsgsize; + *dst++ = '0'; + *dst = '\0'; + } + + /* Format whole octets. */ + for (b = bits / 8; b > 0; b--) { + if (size < sizeof "255.") + goto emsgsize; + t = dst; + dst += sprintf(dst, "%u", *src++); + if (b > 1) { + *dst++ = '.'; + *dst = '\0'; + } + size -= (size_t)(dst - t); + } + + /* Format partial octet. */ + b = bits % 8; + if (b > 0) { + if (size < sizeof ".255") + goto emsgsize; + t = dst; + if (dst != odst) + *dst++ = '.'; + m = ((1 << b) - 1) << (8 - b); + dst += sprintf(dst, "%u", *src & m); + size -= (size_t)(dst - t); + } + + /* Format CIDR /width. */ + if (size < sizeof "/32") + goto emsgsize; + dst += sprintf(dst, "/%u", bits); + return (odst); + + emsgsize: + errno = EMSGSIZE; + return (NULL); +} 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 @@ +/* $OpenBSD: inet_net_pton.c,v 1.1 1997/03/13 19:07:30 downsj Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "$From: inet_net_pton.c,v 8.3 1996/11/11 06:36:52 vixie Exp $"; +#else +static const char rcsid[] = "$OpenBSD: inet_net_pton.c,v 1.1 1997/03/13 19:07:30 downsj Exp $"; +#endif +#endif + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +static int inet_net_pton_ipv4 __P((const char *src, u_char *dst, + size_t size)); + +/* + * static int + * inet_net_pton(af, src, dst, size) + * convert network number from presentation to network format. + * accepts hex octets, hex strings, decimal octets, and /CIDR. + * "size" is in bytes and describes "dst". + * return: + * number of bits, either imputed classfully or specified with /CIDR, + * or -1 if some failure occurred (check errno). ENOENT means it was + * not a valid network specification. + * author: + * Paul Vixie (ISC), June 1996 + */ +int +inet_net_pton(af, src, dst, size) + int af; + const char *src; + void *dst; + size_t size; +{ + switch (af) { + case AF_INET: + return (inet_net_pton_ipv4(src, dst, size)); + default: + errno = EAFNOSUPPORT; + return (-1); + } +} + +/* + * static int + * inet_net_pton_ipv4(src, dst, size) + * convert IPv4 network number from presentation to network format. + * accepts hex octets, hex strings, decimal octets, and /CIDR. + * "size" is in bytes and describes "dst". + * return: + * number of bits, either imputed classfully or specified with /CIDR, + * or -1 if some failure occurred (check errno). ENOENT means it was + * not an IPv4 network specification. + * note: + * network byte order assumed. this means 192.5.5.240/28 has + * 0x11110000 in its fourth octet. + * author: + * Paul Vixie (ISC), June 1996 + */ +static int +inet_net_pton_ipv4(src, dst, size) + const char *src; + u_char *dst; + size_t size; +{ + static const char + xdigits[] = "0123456789abcdef", + digits[] = "0123456789"; + int n, ch, tmp, dirty, bits; + const u_char *odst = dst; + + ch = *src++; + if (ch == '0' && (src[0] == 'x' || src[0] == 'X') + && isascii(src[1]) && isxdigit(src[1])) { + /* Hexadecimal: Eat nybble string. */ + if (size <= 0) + goto emsgsize; + *dst = 0, dirty = 0; + src++; /* skip x or X. */ + while ((ch = *src++) != '\0' && + isascii(ch) && isxdigit(ch)) { + if (isupper(ch)) + ch = tolower(ch); + n = strchr(xdigits, ch) - xdigits; + assert(n >= 0 && n <= 15); + *dst |= n; + if (!dirty++) + *dst <<= 4; + else if (size-- > 0) + *++dst = 0, dirty = 0; + else + goto emsgsize; + } + if (dirty) + size--; + } else if (isascii(ch) && isdigit(ch)) { + /* Decimal: eat dotted digit string. */ + for (;;) { + tmp = 0; + do { + n = strchr(digits, ch) - digits; + assert(n >= 0 && n <= 9); + tmp *= 10; + tmp += n; + if (tmp > 255) + goto enoent; + } while ((ch = *src++) != '\0' && + isascii(ch) && isdigit(ch)); + if (size-- <= 0) + goto emsgsize; + *dst++ = (u_char) tmp; + if (ch == '\0' || ch == '/') + break; + if (ch != '.') + goto enoent; + ch = *src++; + if (!isascii(ch) || !isdigit(ch)) + goto enoent; + } + } else + goto enoent; + + bits = -1; + if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) { + /* CIDR width specifier. Nothing can follow it. */ + ch = *src++; /* Skip over the /. */ + bits = 0; + do { + n = strchr(digits, ch) - digits; + assert(n >= 0 && n <= 9); + bits *= 10; + bits += n; + } while ((ch = *src++) != '\0' && + isascii(ch) && isdigit(ch)); + if (ch != '\0') + goto enoent; + if (bits > 32) + goto emsgsize; + } + + /* Firey death and destruction unless we prefetched EOS. */ + if (ch != '\0') + goto enoent; + + /* If nothing was written to the destination, we found no address. */ + if (dst == odst) + goto enoent; + /* If no CIDR spec was given, infer width from net class. */ + if (bits == -1) { + if (*odst >= 240) /* Class E */ + bits = 32; + else if (*odst >= 224) /* Class D */ + bits = 4; + else if (*odst >= 192) /* Class C */ + bits = 24; + else if (*odst >= 128) /* Class B */ + bits = 16; + else /* Class A */ + bits = 8; + /* If imputed mask is narrower than specified octets, widen. */ + if (bits >= 8 && bits < ((dst - odst) * 8)) + bits = (dst - odst) * 8; + } + /* Extend network to cover the actual mask. */ + while (bits > ((dst - odst) * 8)) { + if (size-- <= 0) + goto emsgsize; + *dst++ = '\0'; + } + return (bits); + + enoent: + errno = ENOENT; + return (-1); + + emsgsize: + errno = EMSGSIZE; + return (-1); +} 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 @@ +/* $OpenBSD: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "$Id: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $"; +#else +static const char rcsid[] = "$OpenBSD: inet_neta.c,v 1.2 1997/04/05 21:13:12 millert Exp $"; +#endif +#endif + +#include +#include +#include +#include + +#include +#include +#include + +/* + * char * + * inet_neta(src, dst, size) + * format an in_addr_t network number into presentation format. + * return: + * pointer to dst, or NULL if an error occurred (check errno). + * note: + * format of ``src'' is as for inet_network(). + * author: + * Paul Vixie (ISC), July 1996 + */ +char * +inet_neta(src, dst, size) + in_addr_t src; + char *dst; + size_t size; +{ + char *odst = dst; + char *tp; + + while (src & 0xffffffff) { + u_char b = (src & 0xff000000) >> 24; + + src <<= 8; + if (b) { + if (size < sizeof "255.") + goto emsgsize; + tp = dst; + dst += sprintf(dst, "%u", b); + if (src != 0L) { + *dst++ = '.'; + *dst = '\0'; + } + size -= (size_t)(dst - tp); + } + } + if (dst == odst) { + if (size < sizeof "0.0.0.0") + goto emsgsize; + strcpy(dst, "0.0.0.0"); + } + return (odst); + + emsgsize: + errno = EMSGSIZE; + return (NULL); +} diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c new file mode 100644 index 0000000000..f3b9c01697 --- /dev/null +++ b/src/lib/libc/net/inet_netof.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: inet_netof.c,v 1.3 1997/04/05 21:13:13 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* + * Return the network number from an internet + * address; handles class a/b/c network #'s. + */ +in_addr_t +inet_netof(in) + struct in_addr in; +{ + register in_addr_t i = ntohl(in.s_addr); + + if (IN_CLASSA(i)) + return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); + else if (IN_CLASSB(i)) + return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); + else + return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); +} diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c new file mode 100644 index 0000000000..8a9a555d62 --- /dev/null +++ b/src/lib/libc/net/inet_network.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: inet_network.c,v 1.7 1997/07/09 01:08:37 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +/* + * Internet network address interpretation routine. + * The library routines call this routine to interpret + * network numbers. + */ +in_addr_t +inet_network(cp) + register const char *cp; +{ + register in_addr_t val, base, n; + register char c; + in_addr_t parts[4], *pp = parts; + register int i; + +again: + val = 0; base = 10; + if (*cp == '0') + base = 8, cp++; + if (*cp == 'x' || *cp == 'X') + base = 16, cp++; + while ((c = *cp)) { + if (isdigit(c)) { + val = (val * base) + (c - '0'); + cp++; + continue; + } + if (base == 16 && isxdigit(c)) { + val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); + cp++; + continue; + } + break; + } + if (*cp == '.') { + if (pp >= parts + 3) + return (INADDR_NONE); + *pp++ = val, cp++; + goto again; + } + if (*cp && !isspace(*cp)) + return (INADDR_NONE); + *pp++ = val; + n = pp - parts; + for (val = 0, i = 0; i < 4; i++) { + val <<= 8; + if (i < n) + val |= parts[i] & 0xff; + } + return (val); +} diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c new file mode 100644 index 0000000000..148732ba5a --- /dev/null +++ b/src/lib/libc/net/inet_ntoa.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: inet_ntoa.c,v 1.2 1996/08/19 08:29:16 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Convert network-format internet address + * to base 256 d.d.d.d representation. + */ +#include +#include +#include +#include + +char * +inet_ntoa(in) + struct in_addr in; +{ + static char b[18]; + register char *p; + + p = (char *)∈ +#define UC(b) (((int)b)&0xff) + (void)snprintf(b, sizeof(b), + "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); + return (b); +} 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 @@ +/* $OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $ */ + +/* Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size)); +static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size)); + +/* char * + * inet_ntop(af, src, dst, size) + * convert a network format address to presentation format. + * return: + * pointer to presentation format address (`dst'), or NULL (see errno). + * author: + * Paul Vixie, 1996. + */ +const char * +inet_ntop(af, src, dst, size) + int af; + const void *src; + char *dst; + size_t size; +{ + switch (af) { + case AF_INET: + return (inet_ntop4(src, dst, size)); + case AF_INET6: + return (inet_ntop6(src, dst, size)); + default: + errno = EAFNOSUPPORT; + return (NULL); + } + /* NOTREACHED */ +} + +/* const char * + * inet_ntop4(src, dst, size) + * format an IPv4 address, more or less like inet_ntoa() + * return: + * `dst' (as a const) + * notes: + * (1) uses no statics + * (2) takes a u_char* not an in_addr as input + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop4(src, dst, size) + const u_char *src; + char *dst; + size_t size; +{ + static const char fmt[] = "%u.%u.%u.%u"; + char tmp[sizeof "255.255.255.255"]; + + if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) { + errno = ENOSPC; + return (NULL); + } + strcpy(dst, tmp); + return (dst); +} + +/* const char * + * inet_ntop6(src, dst, size) + * convert IPv6 binary address into presentation (printable) format + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop6(src, dst, size) + const u_char *src; + char *dst; + size_t size; +{ + /* + * Note that int32_t and int16_t need only be "at least" large enough + * to contain a value of the specified size. On some systems, like + * Crays, there is no such thing as an integer variable with 16 bits. + * Keep this in mind if you think this function should have been coded + * to use pointer overlays. All the world's not a VAX. + */ + char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; + struct { int base, len; } best, cur; + u_int words[IN6ADDRSZ / INT16SZ]; + int i; + + /* + * Preprocess: + * Copy the input (bytewise) array into a wordwise array. + * Find the longest run of 0x00's in src[] for :: shorthanding. + */ + memset(words, '\0', sizeof words); + for (i = 0; i < IN6ADDRSZ; i++) + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); + best.base = -1; + cur.base = -1; + for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; + else + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } + } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } + if (best.base != -1 && best.len < 2) + best.base = -1; + + /* + * Format the result. + */ + tp = tmp; + for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) + *tp++ = ':'; + continue; + } + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) + *tp++ = ':'; + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && + (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { + if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) + return (NULL); + tp += strlen(tp); + break; + } + tp += sprintf(tp, "%x", words[i]); + } + /* Was it a trailing run of 0x00's? */ + if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) + *tp++ = ':'; + *tp++ = '\0'; + + /* + * Check for overflow, copy, and we're done. + */ + if ((size_t)(tp - tmp) > size) { + errno = ENOSPC; + return (NULL); + } + strcpy(dst, tmp); + return (dst); +} 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 @@ +/* $OpenBSD: inet_pton.c,v 1.2 1997/04/13 05:08:24 deraadt Exp $ */ + +/* Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char rcsid[] = "$From: inet_pton.c,v 8.7 1996/08/05 08:31:35 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: inet_pton.c,v 1.2 1997/04/13 05:08:24 deraadt Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static int inet_pton4 __P((const char *src, u_char *dst)); +static int inet_pton6 __P((const char *src, u_char *dst)); + +/* int + * inet_pton(af, src, dst) + * convert from presentation format (which usually means ASCII printable) + * to network format (which is usually some kind of binary format). + * return: + * 1 if the address was valid for the specified address family + * 0 if the address wasn't valid (`dst' is untouched in this case) + * -1 if some other error occurred (`dst' is untouched in this case, too) + * author: + * Paul Vixie, 1996. + */ +int +inet_pton(af, src, dst) + int af; + const char *src; + void *dst; +{ + switch (af) { + case AF_INET: + return (inet_pton4(src, dst)); + case AF_INET6: + return (inet_pton6(src, dst)); + default: + errno = EAFNOSUPPORT; + return (-1); + } + /* NOTREACHED */ +} + +/* int + * inet_pton4(src, dst) + * like inet_aton() but without all the hexadecimal and shorthand. + * return: + * 1 if `src' is a valid dotted quad, else 0. + * notice: + * does not touch `dst' unless it's returning 1. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton4(src, dst) + const char *src; + u_char *dst; +{ + static const char digits[] = "0123456789"; + int saw_digit, octets, ch; + u_char tmp[INADDRSZ], *tp; + + saw_digit = 0; + octets = 0; + *(tp = tmp) = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr(digits, ch)) != NULL) { + u_int new = *tp * 10 + (pch - digits); + + if (new > 255) + return (0); + if (! saw_digit) { + if (++octets > 4) + return (0); + saw_digit = 1; + } + *tp = new; + } else if (ch == '.' && saw_digit) { + if (octets == 4) + return (0); + *++tp = 0; + saw_digit = 0; + } else + return (0); + } + if (octets < 4) + return (0); + + memcpy(dst, tmp, INADDRSZ); + return (1); +} + +/* int + * inet_pton6(src, dst) + * convert presentation level address to network order binary form. + * return: + * 1 if `src' is a valid [RFC1884 2.2] address, else 0. + * notice: + * (1) does not touch `dst' unless it's returning 1. + * (2) :: in a full address is silently ignored. + * credit: + * inspired by Mark Andrews. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton6(src, dst) + const char *src; + u_char *dst; +{ + static const char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; + u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp; + const char *xdigits, *curtok; + int ch, saw_xdigit; + u_int val; + + memset((tp = tmp), '\0', IN6ADDRSZ); + endp = tp + IN6ADDRSZ; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + return (0); + curtok = src; + saw_xdigit = 0; + val = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (val > 0xffff) + return (0); + saw_xdigit = 1; + continue; + } + if (ch == ':') { + curtok = src; + if (!saw_xdigit) { + if (colonp) + return (0); + colonp = tp; + continue; + } + if (tp + INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + saw_xdigit = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + INADDRSZ) <= endp) && + inet_pton4(curtok, tp) > 0) { + tp += INADDRSZ; + saw_xdigit = 0; + break; /* '\0' was seen by inet_pton4(). */ + } + return (0); + } + if (saw_xdigit) { + if (tp + INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + } + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + int i; + + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + return (0); + memcpy(dst, tmp, IN6ADDRSZ); + return (1); +} 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 @@ +.\" $OpenBSD: ipx.3,v 1.3 1997/09/09 11:45:17 kstailey Exp $ +.\" +.\" Copyright (c) 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt IPX 3 +.Os OpenBSD 1.2 +.Sh NAME +.Nm ipx_addr , +.Nm ipx_ntoa +.Nd IPX address conversion routines +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Ft struct ipx_addr +.Fn ipx_addr "char *cp" +.Ft char * +.Fn ipx_ntoa "struct ipx_addr ipx" +.Sh DESCRIPTION +The routine +.Fn ipx_addr +interprets character strings representing +.Tn IPX +addresses, returning binary information suitable +for use in system calls. +The routine +.Fn ipx_ntoa +takes +.Tn IPX +addresses and returns +.Tn ASCII +strings representing the address in a +notation in common use: +.Bd -filled -offset indent +.. +.Ed +.Pp +Trailing zero fields are suppressed, and each number is printed in hexadecimal, +in a format suitable for input to +.Fn ipx_addr . +Any fields lacking super-decimal digits will have a +trailing +.Ql H +appended. +.Pp +An effort has been made to insure that +.Fn ipx_addr +be compatible with most formats in common use. +It will first separate an address into 1 to 3 fields using a single delimiter +chosen from +period +.Ql \&. , +colon +.Ql \&: +or pound-sign +.Ql \&# . +Each field is then examined for byte separators (colon or period). +If there are byte separators, each subfield separated is taken to be +a small hexadecimal number, and the entirety is taken as a network-byte-ordered +quantity to be zero extended in the high-network-order bytes. +Next, the field is inspected for hyphens, in which case +the field is assumed to be a number in decimal notation +with hyphens separating the millenia. +Next, the field is assumed to be a number: +It is interpreted +as hexadecimal if there is a leading +.Ql 0x +(as in C), +a trailing +.Ql H +(as in Mesa), or there are any super-decimal digits present. +It is interpreted as octal is there is a leading +.Ql 0 +and there are no super-octal digits. +Otherwise, it is converted as a decimal number. +.Sh RETURN VALUES +None. (See +.Sx BUGS . ) +.Sh SEE ALSO +.Xr ns 4 , +.Xr hosts 5 , +.Xr networks 5 +.Sh HISTORY +The precursor +.Fn ns_addr +and +.Fn ns_ntoa +functions appeared in +.Bx 4.3 . +.Sh BUGS +The string returned by +.Fn ipx_ntoa +resides in a static memory area. +The function +.Fn ipx_addr +should diagnose improperly formed input, and there should be an unambiguous +way 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 @@ +/* + * Copyright (c) 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * J.Q. Johnson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from @(#)ipx_addr.c + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: ipx_addr.c,v 1.3 1997/07/09 01:08:39 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +static struct ipx_addr addr, zero_addr; + +static void Field(), cvtbase(); + +struct ipx_addr +ipx_addr(name) + const char *name; +{ + char separator; + char *hostname, *socketname, *cp; + char buf[50]; + + (void)strncpy(buf, name, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; + + /* + * First, figure out what he intends as a field separtor. + * Despite the way this routine is written, the prefered + * form 2-272.AA001234H.01777, i.e. XDE standard. + * Great efforts are made to insure backward compatability. + */ + if ((hostname = strchr(buf, '#'))) + separator = '#'; + else { + hostname = strchr(buf, '.'); + if ((cp = strchr(buf, ':')) && + ((hostname && cp < hostname) || (hostname == 0))) { + hostname = cp; + separator = ':'; + } else + separator = '.'; + } + if (hostname) + *hostname++ = 0; + + addr = zero_addr; + Field(buf, addr.ipx_net.c_net, 4); + if (hostname == 0) + return (addr); /* No separator means net only */ + + socketname = strchr(hostname, separator); + if (socketname) { + *socketname++ = 0; + Field(socketname, (u_char *)&addr.ipx_port, 2); + } + + Field(hostname, addr.ipx_host.c_host, 6); + + return (addr); +} + +static void +Field(buf, out, len) + char *buf; + u_char *out; + int len; +{ + register char *bp = buf; + int i, ibase, base16 = 0, base10 = 0, clen = 0; + int hb[6], *hp; + char *fmt; + + /* + * first try 2-273#2-852-151-014#socket + */ + if ((*buf != '-') && + (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { + cvtbase(1000L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0.0.AA.0.5E.E6#socket + */ + if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0:0:AA:0:5E:E6#socket + */ + if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * This is REALLY stretching it but there was a + * comma notation separting shorts -- definitely non standard + */ + if (1 < (i = sscanf(buf,"%x,%x,%x", + &hb[0], &hb[1], &hb[2]))) { + hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); + hb[2] = htons(hb[2]); + cvtbase(65536L, 256, hb, i, out, len); + return; + } + + /* Need to decide if base 10, 16 or 8 */ + while (*bp) switch (*bp++) { + + case '0': case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '-': + break; + + case '8': case '9': + base10 = 1; + break; + + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + base16 = 1; + break; + + case 'x': case 'X': + *--bp = '0'; + base16 = 1; + break; + + case 'h': case 'H': + base16 = 1; + /* fall into */ + + default: + *--bp = 0; /* Ends Loop */ + } + if (base16) { + fmt = "%3x"; + ibase = 4096; + } else if (base10 == 0 && *buf == '0') { + fmt = "%3o"; + ibase = 512; + } else { + fmt = "%3d"; + ibase = 1000; + } + + for (bp = buf; *bp++; ) clen++; + if (clen == 0) clen++; + if (clen > 18) clen = 18; + i = ((clen - 1) / 3) + 1; + bp = clen + buf - 3; + hp = hb + i - 1; + + while (hp > hb) { + (void)sscanf(bp, fmt, hp); + bp[0] = 0; + hp--; + bp -= 3; + } + (void)sscanf(buf, fmt, hp); + cvtbase((long)ibase, 256, hb, i, out, len); +} + +static void +cvtbase(oldbase,newbase,input,inlen,result,reslen) + long oldbase; + int newbase; + int input[]; + int inlen; + unsigned char result[]; + int reslen; +{ + int d, e; + long sum; + + e = 1; + while (e > 0 && reslen > 0) { + d = 0; e = 0; sum = 0; + /* long division: input=input/newbase */ + while (d < inlen) { + sum = sum*oldbase + (long) input[d]; + e += (sum > 0); + input[d++] = sum / newbase; + sum %= newbase; + } + result[--reslen] = sum; /* accumulate remainder */ + } + for (d=0; d < reslen; d++) + result[d] = 0; +} 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 @@ +/* + * Copyright (c) 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: ipx_ntoa.c,v 1.2 1996/08/19 08:29:20 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +char * +ipx_ntoa(addr) + struct ipx_addr addr; +{ + static char obuf[] = "xxxx.xx:xx:xx:xx:xx:xx.uuuuu"; + + sprintf(obuf, "%8xH.%02x:%02x:%02x:%02x:%02x:%02x.%u", + ntohl(addr.ipx_net.l_net), + addr.ipx_host.c_host[0], + addr.ipx_host.c_host[1], + addr.ipx_host.c_host[2], + addr.ipx_host.c_host[3], + addr.ipx_host.c_host[4], + addr.ipx_host.c_host[5], + ntohs(addr.ipx_port)); + return (obuf); +} diff --git a/src/lib/libc/net/iso_addr.3 b/src/lib/libc/net/iso_addr.3 new file mode 100644 index 0000000000..d9bf9086be --- /dev/null +++ b/src/lib/libc/net/iso_addr.3 @@ -0,0 +1,110 @@ +.\" $OpenBSD: iso_addr.3,v 1.2 1996/08/19 08:29:22 tholo Exp $ +.\" +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt ISO_ADDR 3 +.Os +.Sh NAME +.Nm iso_addr , +.Nm iso_ntoa +.Nd "elementary network address conversion routines for Open System Interconnection +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Ft struct iso_addr * +.Fn iso_addr "char *cp" +.Ft char * +.Fn iso_ntoa "struct iso_addr *isoa" +.Sh DESCRIPTION +The routine +.Fn iso_addr +interprets character strings representing +.Tn OSI +addresses, returning binary information suitable +for use in system calls. +The routine +.Fn iso_ntoa +takes +.Tn OSI +addresses and returns +.Tn ASCII +strings representing NSAPs (network service +access points) in a +notation inverse to that accepted by +.Fn iso_addr . +.Pp +Unfortunately, no universal standard exists for representing +.Tn OSI +network addresses. +.Pp +The format employed by +.Fn iso_addr +is a sequence of hexadecimal +.Dq digits +(optionally separated by periods), +of the form: +.Bd -filled -offset indent +.. +.Ed +.Pp +Each pair of hexadecimal digits represents a byte +with the leading digit indicating the higher-ordered bits. +A period following an even number of bytes has no +effect (but may be used to increase legibility). +A period following an odd number of bytes has the +effect of causing the byte of address being translated +to have its higher order bits filled with zeros. +.Sh RETURN VALUES +.Fn iso_ntoa +always returns a null terminated string. +.Fn iso_addr +always returns a pointer to a struct iso_addr. +(See +.Sx BUGS . ) +.Sh SEE ALSO +.Xr iso 4 +.Sh HISTORY +The +.Fn iso_addr +and +.Fn iso_ntoa +functions appeared in +.Bx 4.3 Reno . +.Sh BUGS +The returned values +reside in a static memory area. +.Pp +The function +.Fn iso_addr +should diagnose improperly formed input, and there should be an unambiguous +way to recognize this. diff --git a/src/lib/libc/net/iso_addr.c b/src/lib/libc/net/iso_addr.c new file mode 100644 index 0000000000..01561e395b --- /dev/null +++ b/src/lib/libc/net/iso_addr.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: iso_addr.c,v 1.2 1996/08/19 08:29:23 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +/* States*/ +#define VIRGIN 0 +#define GOTONE 1 +#define GOTTWO 2 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) + +struct iso_addr * +iso_addr(addr) + register const char *addr; +{ + static struct iso_addr out_addr; + register char *cp = out_addr.isoa_genaddr; + char *cplim = cp + sizeof(out_addr.isoa_genaddr); + register int byte = 0, state = VIRGIN, new; + + bzero((char *)&out_addr, sizeof(out_addr)); + do { + if ((*addr >= '0') && (*addr <= '9')) { + new = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + new = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + new = *addr - 'A' + 10; + } else if (*addr == 0) + state |= END; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case GOTTWO | DIGIT: + *cp++ = byte; /*FALLTHROUGH*/ + case VIRGIN | DIGIT: + state = GOTONE; byte = new; continue; + case GOTONE | DIGIT: + state = GOTTWO; byte = new + (byte << 4); continue; + default: /* | DELIM */ + state = VIRGIN; *cp++ = byte; byte = 0; continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; /* FALLTHROUGH */ + case VIRGIN | END: + break; + } + break; + } while (cp < cplim); + out_addr.isoa_len = cp - out_addr.isoa_genaddr; + return (&out_addr); +} +static char hexlist[] = "0123456789abcdef"; + +char * +iso_ntoa(isoa) + const struct iso_addr *isoa; +{ + static char obuf[64]; + register char *out = obuf; + register int i; + register u_char *in = (u_char *)isoa->isoa_genaddr; + u_char *inlim = in + isoa->isoa_len; + + out[1] = 0; + while (in < inlim) { + i = *in++; + *out++ = '.'; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return(obuf + 1); +} diff --git a/src/lib/libc/net/link_addr.3 b/src/lib/libc/net/link_addr.3 new file mode 100644 index 0000000000..eb6c952177 --- /dev/null +++ b/src/lib/libc/net/link_addr.3 @@ -0,0 +1,131 @@ +.\" $OpenBSD: link_addr.3,v 1.2 1996/08/19 08:29:25 tholo Exp $ +.\" +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" Donn Seeley at BSDI. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd July 28, 1993 +.Dt LINK_ADDR 3 +.Os BSD 4.4 +.Sh NAME +.Nm link_addr , +.Nm link_ntoa +.Nd elementary address specification routines for link level access +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Fd #include +.Ft void +.Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" +.Ft char * +.Fn link_ntoa "const struct sockaddr_dl *sdl" +.Sh DESCRIPTION +The routine +.Fn link_addr +interprets character strings representing +link-level addresses, returning binary information suitable +for use in system calls. +The routine +.Fn link_ntoa +takes +a link-level +address and returns an +.Tn ASCII +string representing some of the information present, +including the link level address itself, and the interface name +or number, if present. +This facility is experimental and is +still subject to change. +.Pp +For +.Fn link_addr , +the string +.Fa addr +may contain +an optional network interface identifier of the form +.Dq "name unit-number" , +suitable for the first argument to +.Xr ifconfig 4 , +followed in all cases by a colon and +an interface address in the form of +groups of hexadecimal digits +separated by periods. +Each group represents a byte of address; +address bytes are filled left to right from +low order bytes through high order bytes. +.Pp +.\" A regular expression may make this format clearer: +.\" .Bd -literal -offset indent +.\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* +.\" .Ed +.\" .Pp +Thus +.Li le0:8.0.9.13.d.30 +represents an ethernet address +to be transmitted on the first Lance ethernet interface. +.Sh RETURN VALUES +.Fn link_ntoa +always returns a null terminated string. +.Fn link_addr +has no return value. +(See +.Sx BUGS . ) +.Sh SEE ALSO +.Xr iso 4 , +.Sh HISTORY +The +.Fn link_addr +and +.Fn link_ntoa +functions appeared in +.Bx 4.3 Reno . +.Sh BUGS +The returned values for link_ntoa +reside in a static memory area. +.Pp +The function +.Fn link_addr +should diagnose improperly formed input, and there should be an unambiguous +way to recognize this. +.Pp +If the +.Va sdl_len +field of the link socket address +.Fa sdl +is 0, +.Fn link_ntoa +will not insert a colon before the interface address bytes. +If this translated address is given to +.Fn link_addr +without inserting an initial colon, +the latter will not interpret it correctly. diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c new file mode 100644 index 0000000000..fb522f3233 --- /dev/null +++ b/src/lib/libc/net/linkaddr.c @@ -0,0 +1,158 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: linkaddr.c,v 1.2 1996/08/19 08:29:27 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +/* States*/ +#define NAMING 0 +#define GOTONE 1 +#define GOTTWO 2 +#define RESET 3 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) +#define LETTER (4*3) + +void +link_addr(addr, sdl) + register const char *addr; + register struct sockaddr_dl *sdl; +{ + register char *cp = sdl->sdl_data; + char *cplim = sdl->sdl_len + (char *)sdl; + register int byte = 0, state = NAMING, new; + + bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); + sdl->sdl_family = AF_LINK; + do { + state &= ~LETTER; + if ((*addr >= '0') && (*addr <= '9')) { + new = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + new = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + new = *addr - 'A' + 10; + } else if (*addr == 0) { + state |= END; + } else if (state == NAMING && + (((*addr >= 'A') && (*addr <= 'Z')) || + ((*addr >= 'a') && (*addr <= 'z')))) + state |= LETTER; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case NAMING | DIGIT: + case NAMING | LETTER: + *cp++ = addr[-1]; + continue; + case NAMING | DELIM: + state = RESET; + sdl->sdl_nlen = cp - sdl->sdl_data; + continue; + case GOTTWO | DIGIT: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | DIGIT: + state = GOTONE; + byte = new; + continue; + case GOTONE | DIGIT: + state = GOTTWO; + byte = new + (byte << 4); + continue; + default: /* | DELIM */ + state = RESET; + *cp++ = byte; + byte = 0; + continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | END: + break; + } + break; + } while (cp < cplim); + sdl->sdl_alen = cp - LLADDR(sdl); + new = cp - (char *)sdl; + if (new > sizeof(*sdl)) + sdl->sdl_len = new; + return; +} + +static char hexlist[] = "0123456789abcdef"; + +char * +link_ntoa(sdl) + register const struct sockaddr_dl *sdl; +{ + static char obuf[64]; + register char *out = obuf; + register int i; + register u_char *in = (u_char *)LLADDR(sdl); + u_char *inlim = in + sdl->sdl_alen; + int firsttime = 1; + + if (sdl->sdl_nlen) { + bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); + out += sdl->sdl_nlen; + if (sdl->sdl_alen) + *out++ = ':'; + } + while (in < inlim) { + if (firsttime) + firsttime = 0; + else + *out++ = '.'; + i = *in++; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return (obuf); +} diff --git a/src/lib/libc/net/ns.3 b/src/lib/libc/net/ns.3 new file mode 100644 index 0000000000..6e096d9f4b --- /dev/null +++ b/src/lib/libc/net/ns.3 @@ -0,0 +1,130 @@ +.\" $OpenBSD: ns.3,v 1.2 1996/08/19 08:29:29 tholo Exp $ +.\" +.\" Copyright (c) 1986, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt NS 3 +.Os BSD 4.3 +.Sh NAME +.Nm ns_addr , +.Nm ns_ntoa +.Nd Xerox +.Tn NS Ns (tm) +address conversion routines +.Sh SYNOPSIS +.Fd #include +.Fd #include +.Ft struct ns_addr +.Fn ns_addr "char *cp" +.Ft char * +.Fn ns_ntoa "struct ns_addr ns" +.Sh DESCRIPTION +The routine +.Fn ns_addr +interprets character strings representing +.Tn XNS +addresses, returning binary information suitable +for use in system calls. +The routine +.Fn ns_ntoa +takes +.Tn XNS +addresses and returns +.Tn ASCII +strings representing the address in a +notation in common use in the Xerox Development Environment: +.Bd -filled -offset indent +.. +.Ed +.Pp +Trailing zero fields are suppressed, and each number is printed in hexadecimal, +in a format suitable for input to +.Fn ns_addr . +Any fields lacking super-decimal digits will have a +trailing +.Ql H +appended. +.Pp +Unfortunately, no universal standard exists for representing +.Tn XNS +addresses. +An effort has been made to insure that +.Fn ns_addr +be compatible with most formats in common use. +It will first separate an address into 1 to 3 fields using a single delimiter +chosen from +period +.Ql \&. , +colon +.Ql \&: +or pound-sign +.Ql \&# . +Each field is then examined for byte separators (colon or period). +If there are byte separators, each subfield separated is taken to be +a small hexadecimal number, and the entirety is taken as a network-byte-ordered +quantity to be zero extended in the high-network-order bytes. +Next, the field is inspected for hyphens, in which case +the field is assumed to be a number in decimal notation +with hyphens separating the millenia. +Next, the field is assumed to be a number: +It is interpreted +as hexadecimal if there is a leading +.Ql 0x +(as in C), +a trailing +.Ql H +(as in Mesa), or there are any super-decimal digits present. +It is interpreted as octal is there is a leading +.Ql 0 +and there are no super-octal digits. +Otherwise, it is converted as a decimal number. +.Sh RETURN VALUES +None. (See +.Sx BUGS . ) +.Sh SEE ALSO +.Xr hosts 5 , +.Xr networks 5 , +.Sh HISTORY +The +.Fn ns_addr +and +.Fn ns_toa +functions appeared in +.Bx 4.3 . +.Sh BUGS +The string returned by +.Fn ns_ntoa +resides in a static memory area. +The function +.Fn ns_addr +should diagnose improperly formed input, and there should be an unambiguous +way to recognize this. diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c new file mode 100644 index 0000000000..8f2e4bc513 --- /dev/null +++ b/src/lib/libc/net/ns_addr.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * J.Q. Johnson. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: ns_addr.c,v 1.4 1997/07/21 20:31:05 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +static struct ns_addr addr, zero_addr; + +static void Field __P((char *, u_int8_t *, int)); +static void cvtbase __P((long, int, int[], int, u_int8_t[], int)); + +struct ns_addr +ns_addr(name) + const char *name; +{ + char separator; + char *hostname, *socketname, *cp; + char buf[50]; + + (void)strncpy(buf, name, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; + + /* + * First, figure out what he intends as a field separtor. + * Despite the way this routine is written, the prefered + * form 2-272.AA001234H.01777, i.e. XDE standard. + * Great efforts are made to insure backward compatability. + */ + if ((hostname = strchr(buf, '#'))) + separator = '#'; + else { + hostname = strchr(buf, '.'); + if ((cp = strchr(buf, ':')) && + ((hostname && cp < hostname) || (hostname == 0))) { + hostname = cp; + separator = ':'; + } else + separator = '.'; + } + if (hostname) + *hostname++ = 0; + + addr = zero_addr; + Field(buf, addr.x_net.c_net, 4); + if (hostname == 0) + return (addr); /* No separator means net only */ + + socketname = strchr(hostname, separator); + if (socketname) { + *socketname++ = 0; + Field(socketname, (u_char *)&addr.x_port, 2); + } + + Field(hostname, (u_char *)addr.x_host.c_host, 6); + + return (addr); +} + +static void +Field(buf, out, len) + char *buf; + u_char *out; + int len; +{ + register char *bp = buf; + int i, ibase, base16 = 0, base10 = 0, clen = 0; + int hb[6], *hp; + char *fmt; + + /* + * first try 2-273#2-852-151-014#socket + */ + if ((*buf != '-') && + (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { + cvtbase(1000L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0.0.AA.0.5E.E6#socket + */ + if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * try form 8E1#0:0:AA:0:5E:E6#socket + */ + if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", + &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { + cvtbase(256L, 256, hb, i, out, len); + return; + } + /* + * This is REALLY stretching it but there was a + * comma notation separting shorts -- definitely non standard + */ + if (1 < (i = sscanf(buf,"%x,%x,%x", + &hb[0], &hb[1], &hb[2]))) { + hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); + hb[2] = htons(hb[2]); + cvtbase(65536L, 256, hb, i, out, len); + return; + } + + /* Need to decide if base 10, 16 or 8 */ + while (*bp) switch (*bp++) { + + case '0': case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '-': + break; + + case '8': case '9': + base10 = 1; + break; + + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + base16 = 1; + break; + + case 'x': case 'X': + *--bp = '0'; + base16 = 1; + break; + + case 'h': case 'H': + base16 = 1; + /* fall into */ + + default: + *--bp = 0; /* Ends Loop */ + } + if (base16) { + fmt = "%3x"; + ibase = 4096; + } else if (base10 == 0 && *buf == '0') { + fmt = "%3o"; + ibase = 512; + } else { + fmt = "%3d"; + ibase = 1000; + } + + for (bp = buf; *bp++; ) clen++; + if (clen == 0) clen++; + if (clen > 18) clen = 18; + i = ((clen - 1) / 3) + 1; + bp = clen + buf - 3; + hp = hb + i - 1; + + while (hp > hb) { + (void)sscanf(bp, fmt, hp); + bp[0] = 0; + hp--; + bp -= 3; + } + (void)sscanf(buf, fmt, hp); + cvtbase((long)ibase, 256, hb, i, out, len); +} + +static void +cvtbase(oldbase,newbase,input,inlen,result,reslen) + long oldbase; + int newbase; + int input[]; + int inlen; + unsigned char result[]; + int reslen; +{ + int d, e; + long sum; + + e = 1; + while (e > 0 && reslen > 0) { + d = 0; e = 0; sum = 0; + /* long division: input=input/newbase */ + while (d < inlen) { + sum = sum*oldbase + (long) input[d]; + e += (sum > 0); + input[d++] = sum / newbase; + sum %= newbase; + } + result[--reslen] = sum; /* accumulate remainder */ + } + for (d=0; d < reslen; d++) + result[d] = 0; +} diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c new file mode 100644 index 0000000000..c33f710966 --- /dev/null +++ b/src/lib/libc/net/ns_ntoa.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 1986, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: ns_ntoa.c,v 1.7 1997/08/24 21:25:48 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +static char *spectHex __P((char *)); + +char * +ns_ntoa(addr) + struct ns_addr addr; +{ + static char obuf[40]; + union { union ns_net net_e; u_int32_t long_e; } net; + in_port_t port = htons(addr.x_port); + register char *cp; + char *cp2; + register u_char *up = addr.x_host.c_host; + u_char *uplim = up + 6; + + net.net_e = addr.x_net; + sprintf(obuf, "%x", ntohl(net.long_e)); + cp = spectHex(obuf); + cp2 = cp + 1; + while (*up==0 && up < uplim) up++; + if (up == uplim) { + if (port) { + sprintf(cp, ".0"); + cp += 2; + } + } else { + sprintf(cp, ".%x", *up++); + while (up < uplim) { + while (*cp) cp++; + sprintf(cp, "%02x", *up++); + } + cp = spectHex(cp2); + } + if (port) { + sprintf(cp, ".%x", port); + spectHex(cp + 1); + } + return (obuf); +} + +static char * +spectHex(p0) + char *p0; +{ + int ok = 0; + int nonzero = 0; + register char *p = p0; + for (; *p; p++) switch (*p) { + + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + *p += ('A' - 'a'); + /* fall into . . . */ + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + ok = 1; + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': + nonzero = 1; + } + if (nonzero && !ok) { *p++ = 'H'; *p = 0; } + return (p); +} 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 @@ +/* $OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $ */ + +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char rcsid[] = "$From: nsap_addr.c,v 8.3 1996/08/05 08:31:35 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include + +static char +xtob(c) + register int c; +{ + return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); +} + +u_int +inet_nsap_addr(ascii, binary, maxlen) + const char *ascii; + u_char *binary; + int maxlen; +{ + register u_char c, nib; + u_int len = 0; + + while ((c = *ascii++) != '\0' && len < maxlen) { + if (c == '.' || c == '+' || c == '/') + continue; + if (!isascii(c)) + return (0); + if (islower(c)) + c = toupper(c); + if (isxdigit(c)) { + nib = xtob(c); + if ((c = *ascii++)) { + c = toupper(c); + if (isxdigit(c)) { + *binary++ = (nib << 4) | xtob(c); + len++; + } else + return (0); + } + else + return (0); + } + else + return (0); + } + return (len); +} + +char * +inet_nsap_ntoa(binlen, binary, ascii) + int binlen; + register const u_char *binary; + register char *ascii; +{ + register int nib; + int i; + static char tmpbuf[255*3]; + char *start; + + if (ascii) + start = ascii; + else { + ascii = tmpbuf; + start = tmpbuf; + } + + if (binlen > 255) + binlen = 255; + + for (i = 0; i < binlen; i++) { + nib = *binary >> 4; + *ascii++ = nib + (nib < 10 ? '0' : '7'); + nib = *binary++ & 0x0f; + *ascii++ = nib + (nib < 10 ? '0' : '7'); + if (((i % 2) == 0 && (i + 1) < binlen)) + *ascii++ = '.'; + } + *ascii = '\0'; + return (start); +} diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c new file mode 100644 index 0000000000..7d3e227e60 --- /dev/null +++ b/src/lib/libc/net/ntohl.c @@ -0,0 +1,25 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: ntohl.c,v 1.4 1996/12/12 03:19:56 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef ntohl + +u_int32_t +ntohl(x) + u_int32_t x; +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *)&x; + return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); +#else + return x; +#endif +} diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c new file mode 100644 index 0000000000..cf6414d4a6 --- /dev/null +++ b/src/lib/libc/net/ntohs.c @@ -0,0 +1,29 @@ +/* + * Written by J.T. Conklin . + * Public domain. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: ntohs.c,v 1.6 1997/07/25 20:30:07 mickey Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#undef ntohs + +u_int16_t +#ifdef __STDC__ +ntohs(u_int16_t x) +#else +ntohs(x) + u_int16_t x; +#endif +{ +#if BYTE_ORDER == LITTLE_ENDIAN + u_char *s = (u_char *) &x; + return (u_int16_t)(s[0] << 8 | s[1]); +#else + return x; +#endif +} diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 new file mode 100644 index 0000000000..e69e822834 --- /dev/null +++ b/src/lib/libc/net/rcmd.3 @@ -0,0 +1,227 @@ +.\" $OpenBSD: rcmd.3,v 1.10 1997/09/29 18:25:47 deraadt Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd June 4, 1993 +.Dt RCMD 3 +.Os BSD 4.2 +.Sh NAME +.Nm rcmd , +.Nm rresvport , +.Nm iruserok , +.Nm ruserok +.Nd routines for returning a stream to a remote command +.Sh SYNOPSIS +.Fd #include +.Ft int +.Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" +.Ft int +.Fn rresvport "int *port" +.Ft int +.Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" +.Ft int +.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" +.Sh DESCRIPTION +The +.Fn rcmd +function +is used by the super-user to execute a command on a remote +machine using an authentication scheme based on reserved +port numbers. If the calling process is not setuid and the +.Li RSH +environment variable is set and +.Fa inport +is +.Li shell/tcp , +.Xr rcmdsh 3 +is called instead with the value of +.Li RSH . +Alternately, if the user is not the super-user, +.Fn rcmd +will invoke +.Xr rcmdsh 3 +to run the command via +.Xr rsh 1 . +The +.Fn rresvport +function +returns a descriptor to a socket +with an address in the privileged port space. +The +.Fn iruserok +and +.Fn ruserok +functions are used by servers +to authenticate clients requesting service with +.Fn rcmd . +All four functions are present in the same file and are used +by the +.Xr rshd 8 +server (among others). +.Pp +The +.Fn rcmd +function +looks up the host +.Fa *ahost +using +.Xr gethostbyname 3 , +returning \-1 if the host does not exist. +Otherwise +.Fa *ahost +is set to the standard name of the host +and a connection is established to a server +residing at the well-known Internet port +.Fa inport . +If the user is not the super-user, the only valid port is +.Li shell/tcp , +(usually port 514). +.Pp +If the connection succeeds, +a socket in the Internet domain of type +.Dv SOCK_STREAM +is returned to the caller, and given to the remote +command as +.Em stdin +and +.Em stdout . +If +.Fa fd2p +is non-zero, then an auxiliary channel to a control +process will be set up, and a descriptor for it will be placed +in +.Fa *fd2p . +The control process will return diagnostic +output from the command (unit 2) on this channel, and will also +accept bytes on this channel as being +.Tn UNIX +signal numbers, to be +forwarded to the process group of the command. +If +.Fa fd2p +is 0, then the +.Em stderr +(unit 2 of the remote +command) will be made the same as the +.Em stdout +and no +provision is made for sending arbitrary signals to the remote process, +although you may be able to get its attention by using out-of-band data. +Note that if the user is not the super-user, +.Fa fd2p +must be 0. +.Pp +The protocol is described in detail in +.Xr rshd 8 . +.Pp +The +.Fn rresvport +function is used to obtain a socket with a privileged +address bound to it. This socket is suitable for use +by +.Fn rcmd +and several other functions. Privileged Internet ports are those +in the range 0 to 1023. Only the super-user +is allowed to bind an address of this sort to a socket. +.Fn rresvport +needs to be seeded with a port number; if that port +is not available it will find another. +.Pp +The +.Fn iruserok +and +.Fn ruserok +functions take a remote host's IP address or name, respectively, +two user names and a flag indicating whether the local user's +name is that of the super-user. +Then, if the user is +.Em NOT +the super-user, it checks the +.Pa /etc/hosts.equiv +file. +If that lookup is not done, or is unsuccessful, the +.Pa .rhosts +in the local user's home directory is checked to see if the request for +service is allowed. +.Pp +If this file does not exist, is not a regular file, is owned by anyone +other than the user or the super-user, or is writeable by anyone other +than the owner, the check automatically fails. +Zero is returned if the machine name is listed in the +.Dq Pa hosts.equiv +file, or the host and remote user name are found in the +.Dq Pa .rhosts +file; otherwise +.Fn iruserok +and +.Fn ruserok +return \-1. +If the local domain (as obtained from +.Xr gethostname 3 ) +is the same as the remote domain, only the machine name need be specified. +.Pp +If the IP address of the remote host is known, +.Fn iruserok +should be used in preference to +.Fn ruserok , +as it does not require trusting the DNS server for the remote host's domain. +.Sh DIAGNOSTICS +The +.Fn rcmd +function +returns a valid socket descriptor on success. +It returns \-1 on error and prints a diagnostic message on the standard error. +.Pp +The +.Fn rresvport +function +returns a valid, bound socket descriptor on success. +It returns \-1 on error with the global value +.Va errno +set according to the reason for failure. +The error code +.Dv EAGAIN +is overloaded to mean ``All network ports in use.'' +.Sh SEE ALSO +.Xr rlogin 1 , +.Xr rsh 1 , +.Xr intro 2 , +.Xr rexec 3 , +.Xr rcmdsh 3 , +.Xr rexecd 8 , +.Xr rlogind 8 , +.Xr bindresvport 3 , +.Xr rshd 8 +.Sh HISTORY +These +functions appeared in +.Bx 4.2 . diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..c933f5b447 --- /dev/null +++ b/src/lib/libc/net/rcmd.c @@ -0,0 +1,607 @@ +/* + * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. + * Copyright (c) 1983, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * This product includes software developed by Theo de Raadt. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: rcmd.c,v 1.31 1998/03/19 00:30:05 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int __ivaliduser __P((FILE *, in_addr_t, const char *, const char *)); +static int __icheckhost __P((u_int32_t, const char *)); +static char *__gethostloop __P((u_int32_t)); + +int +rcmd(ahost, rport, locuser, remuser, cmd, fd2p) + char **ahost; + in_port_t rport; + const char *locuser, *remuser, *cmd; + int *fd2p; +{ + struct hostent *hp; + struct sockaddr_in sin, from; + fd_set *readsp = NULL; + int oldmask; + pid_t pid; + int s, lport, timo; + char c, *p; + + /* call rcmdsh() with specified remote shell if appropriate. */ + if (!issetugid() && (p = getenv("RSH"))) { + struct servent *sp = getservbyname("shell", "tcp"); + + if (sp && sp->s_port == rport) + return (rcmdsh(ahost, rport, locuser, remuser, + cmd, p)); + } + + /* use rsh(1) if non-root and remote port is shell. */ + if (geteuid()) { + struct servent *sp = getservbyname("shell", "tcp"); + + if (sp && sp->s_port == rport) + return (rcmdsh(ahost, rport, locuser, remuser, + cmd, NULL)); + } + + pid = getpid(); + hp = gethostbyname(*ahost); + if (hp == NULL) { + herror(*ahost); + return (-1); + } + *ahost = hp->h_name; + + oldmask = sigblock(sigmask(SIGURG)); + for (timo = 1, lport = IPPORT_RESERVED - 1;;) { + s = rresvport(&lport); + if (s < 0) { + if (errno == EAGAIN) + (void)fprintf(stderr, + "rcmd: socket: All ports in use\n"); + else + (void)fprintf(stderr, "rcmd: socket: %s\n", + strerror(errno)); + sigsetmask(oldmask); + return (-1); + } + fcntl(s, F_SETOWN, pid); + bzero(&sin, sizeof sin); + sin.sin_len = sizeof(struct sockaddr_in); + sin.sin_family = hp->h_addrtype; + sin.sin_port = rport; + bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); + if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) + break; + (void)close(s); + if (errno == EADDRINUSE) { + lport--; + continue; + } + if (errno == ECONNREFUSED && timo <= 16) { + (void)sleep(timo); + timo *= 2; + continue; + } + if (hp->h_addr_list[1] != NULL) { + int oerrno = errno; + + (void)fprintf(stderr, "connect to address %s: ", + inet_ntoa(sin.sin_addr)); + errno = oerrno; + perror(0); + hp->h_addr_list++; + bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); + (void)fprintf(stderr, "Trying %s...\n", + inet_ntoa(sin.sin_addr)); + continue; + } + (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); + sigsetmask(oldmask); + return (-1); + } +#if 0 + /* + * try to rresvport() to the same port. This will make rresvport() + * fail it's first bind, resulting in it choosing a random port. + */ + lport--; +#endif + if (fd2p == 0) { + write(s, "", 1); + lport = 0; + } else { + char num[8]; + int s2 = rresvport(&lport), s3; + int len = sizeof(from); + int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); + + if (s2 < 0) + goto bad; + readsp = (fd_set *)malloc(fdssize); + if (readsp == NULL) + goto bad; + listen(s2, 1); + (void)snprintf(num, sizeof(num), "%d", lport); + if (write(s, num, strlen(num)+1) != strlen(num)+1) { + (void)fprintf(stderr, + "rcmd: write (setting up stderr): %s\n", + strerror(errno)); + (void)close(s2); + goto bad; + } +again: + bzero(readsp, fdssize); + FD_SET(s, readsp); + FD_SET(s2, readsp); + errno = 0; + if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || + !FD_ISSET(s2, readsp)) { + if (errno != 0) + (void)fprintf(stderr, + "rcmd: select (setting up stderr): %s\n", + strerror(errno)); + else + (void)fprintf(stderr, + "select: protocol failure in circuit setup\n"); + (void)close(s2); + goto bad; + } + s3 = accept(s2, (struct sockaddr *)&from, &len); + /* + * XXX careful for ftp bounce attacks. If discovered, shut them + * down and check for the real auxiliary channel to connect. + */ + if (from.sin_family == AF_INET && from.sin_port == htons(20)) { + close(s3); + goto again; + } + (void)close(s2); + if (s3 < 0) { + (void)fprintf(stderr, + "rcmd: accept: %s\n", strerror(errno)); + lport = 0; + goto bad; + } + *fd2p = s3; + from.sin_port = ntohs(from.sin_port); + if (from.sin_family != AF_INET || + from.sin_port >= IPPORT_RESERVED || + from.sin_port < IPPORT_RESERVED / 2) { + (void)fprintf(stderr, + "socket: protocol failure in circuit setup.\n"); + goto bad2; + } + } + (void)write(s, locuser, strlen(locuser)+1); + (void)write(s, remuser, strlen(remuser)+1); + (void)write(s, cmd, strlen(cmd)+1); + if (read(s, &c, 1) != 1) { + (void)fprintf(stderr, + "rcmd: %s: %s\n", *ahost, strerror(errno)); + goto bad2; + } + if (c != 0) { + while (read(s, &c, 1) == 1) { + (void)write(STDERR_FILENO, &c, 1); + if (c == '\n') + break; + } + goto bad2; + } + sigsetmask(oldmask); + free(readsp); + return (s); +bad2: + if (lport) + (void)close(*fd2p); +bad: + if (readsp) + free(readsp); + (void)close(s); + sigsetmask(oldmask); + return (-1); +} + +int +rresvport(alport) + int *alport; +{ + struct sockaddr_in sin; + int s; + + bzero(&sin, sizeof sin); + sin.sin_len = sizeof(struct sockaddr_in); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = INADDR_ANY; + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) + return (-1); + sin.sin_port = htons((in_port_t)*alport); + if (*alport < IPPORT_RESERVED - 1) { + if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) + return (s); + if (errno != EADDRINUSE) { + (void)close(s); + return (-1); + } + } + sin.sin_port = 0; + if (bindresvport(s, &sin) == -1) { + (void)close(s); + return (-1); + } + *alport = (int)ntohs(sin.sin_port); + return (s); +} + +int __check_rhosts_file = 1; +char *__rcmd_errstr; + +int +ruserok(rhost, superuser, ruser, luser) + const char *rhost, *ruser, *luser; + int superuser; +{ + struct hostent *hp; + char **ap; + int i; +#define MAXADDRS 35 + u_int32_t addrs[MAXADDRS + 1]; + + if ((hp = gethostbyname(rhost)) == NULL) + return (-1); + for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) + bcopy(*ap, &addrs[i], sizeof(addrs[i])); + addrs[i] = 0; + + for (i = 0; i < MAXADDRS && addrs[i]; i++) + if (iruserok((in_addr_t)addrs[i], superuser, ruser, luser) == 0) + return (0); + return (-1); +} + +/* + * New .rhosts strategy: We are passed an ip address. We spin through + * hosts.equiv and .rhosts looking for a match. When the .rhosts only + * has ip addresses, we don't have to trust a nameserver. When it + * contains hostnames, we spin through the list of addresses the nameserver + * gives us and look for a match. + * + * Returns 0 if ok, -1 if not ok. + */ +int +iruserok(raddr, superuser, ruser, luser) + u_int32_t raddr; + int superuser; + const char *ruser, *luser; +{ + register char *cp; + struct stat sbuf; + struct passwd *pwd; + FILE *hostf; + uid_t uid; + int first; + char pbuf[MAXPATHLEN]; + + first = 1; + hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); +again: + if (hostf) { + if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { + (void)fclose(hostf); + return (0); + } + (void)fclose(hostf); + } + if (first == 1 && (__check_rhosts_file || superuser)) { + first = 0; + if ((pwd = getpwnam(luser)) == NULL) + return (-1); + (void)strcpy(pbuf, pwd->pw_dir); + (void)strcat(pbuf, "/.rhosts"); + + /* + * Change effective uid while opening .rhosts. If root and + * reading an NFS mounted file system, can't read files that + * are protected read/write owner only. + */ + uid = geteuid(); + (void)seteuid(pwd->pw_uid); + hostf = fopen(pbuf, "r"); + (void)seteuid(uid); + + if (hostf == NULL) + return (-1); + /* + * If not a regular file, or is owned by someone other than + * user or root or if writeable by anyone but the owner, quit. + */ + cp = NULL; + if (lstat(pbuf, &sbuf) < 0) + cp = ".rhosts lstat failed"; + else if (!S_ISREG(sbuf.st_mode)) + cp = ".rhosts not regular file"; + else if (fstat(fileno(hostf), &sbuf) < 0) + cp = ".rhosts fstat failed"; + else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) + cp = "bad .rhosts owner"; + else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) + cp = ".rhosts writeable by other than owner"; + /* If there were any problems, quit. */ + if (cp) { + __rcmd_errstr = cp; + (void)fclose(hostf); + return (-1); + } + goto again; + } + return (-1); +} + +/* + * XXX + * Don't make static, used by lpd(8). + * + * Returns 0 if ok, -1 if not ok. + */ +int +__ivaliduser(hostf, raddrl, luser, ruser) + FILE *hostf; + in_addr_t raddrl; + const char *luser, *ruser; +{ + register char *user, *p; + char *buf; + const char *auser, *ahost; + int hostok, userok; + char *rhost = (char *)-1; + char domain[MAXHOSTNAMELEN]; + u_int32_t raddr = (u_int32_t)raddrl; + size_t buflen; + + getdomainname(domain, sizeof(domain)); + + while ((buf = fgetln(hostf, &buflen))) { + p = buf; + if (*p == '#') + continue; + while (*p != '\n' && *p != ' ' && *p != '\t' && p < buf + buflen) { + if (!isprint(*p)) + goto bail; + *p = isupper(*p) ? tolower(*p) : *p; + p++; + } + if (p >= buf + buflen) + continue; + if (*p == ' ' || *p == '\t') { + *p++ = '\0'; + while ((*p == ' ' || *p == '\t') && p < buf + buflen) + p++; + if (p >= buf + buflen) + continue; + user = p; + while (*p != '\n' && *p != ' ' && + *p != '\t' && p < buf + buflen) { + if (!isprint(*p)) + goto bail; + p++; + } + } else + user = p; + *p = '\0'; + + if (p == buf) + continue; + + auser = *user ? user : luser; + ahost = buf; + + if (strlen(ahost) >= MAXHOSTNAMELEN) + continue; + + /* + * innetgr() must lookup a hostname (we do not attempt + * to change the semantics so that netgroups may have + * #.#.#.# addresses in the list.) + */ + if (ahost[0] == '+') + switch (ahost[1]) { + case '\0': + hostok = 1; + break; + case '@': + if (rhost == (char *)-1) + rhost = __gethostloop(raddr); + hostok = 0; + if (rhost) + hostok = innetgr(&ahost[2], rhost, + NULL, domain); + break; + default: + hostok = __icheckhost(raddr, &ahost[1]); + break; + } + else if (ahost[0] == '-') + switch (ahost[1]) { + case '\0': + hostok = -1; + break; + case '@': + if (rhost == (char *)-1) + rhost = __gethostloop(raddr); + hostok = 0; + if (rhost) + hostok = -innetgr(&ahost[2], rhost, + NULL, domain); + break; + default: + hostok = -__icheckhost(raddr, &ahost[1]); + break; + } + else + hostok = __icheckhost(raddr, ahost); + + + if (auser[0] == '+') + switch (auser[1]) { + case '\0': + userok = 1; + break; + case '@': + userok = innetgr(&auser[2], NULL, ruser, + domain); + break; + default: + userok = strcmp(ruser, &auser[1]) ? 0 : 1; + break; + } + else if (auser[0] == '-') + switch (auser[1]) { + case '\0': + userok = -1; + break; + case '@': + userok = -innetgr(&auser[2], NULL, ruser, + domain); + break; + default: + userok = strcmp(ruser, &auser[1]) ? 0 : -1; + break; + } + else + userok = strcmp(ruser, auser) ? 0 : 1; + + /* Check if one component did not match */ + if (hostok == 0 || userok == 0) + continue; + + /* Check if we got a forbidden pair */ + if (userok <= -1 || hostok <= -1) + return (-1); + + /* Check if we got a valid pair */ + if (hostok >= 1 && userok >= 1) + return (0); + } +bail: + return (-1); +} + +/* + * Returns "true" if match, 0 if no match. If we do not find any + * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. + */ +static int +__icheckhost(raddr, lhost) + u_int32_t raddr; + const char *lhost; +{ + register struct hostent *hp; + register char **pp; + struct in_addr in; + + hp = gethostbyname(lhost); + if (hp != NULL) { + /* Spin through ip addresses. */ + for (pp = hp->h_addr_list; *pp; ++pp) + if (!bcmp(&raddr, *pp, sizeof(raddr))) + return (1); + } + + in.s_addr = raddr; + if (strcmp(lhost, inet_ntoa(in)) == 0) + return (1); + return (0); +} + +/* + * Return the hostname associated with the supplied address. + * Do a reverse lookup as well for security. If a loop cannot + * be found, pack the result of inet_ntoa() into the string. + */ +static char * +__gethostloop(raddr) + u_int32_t raddr; +{ + static char remotehost[MAXHOSTNAMELEN]; + struct hostent *hp; + struct in_addr in; + + hp = gethostbyaddr((char *) &raddr, sizeof(raddr), AF_INET); + if (hp == NULL) + return (NULL); + + /* + * Look up the name and check that the supplied + * address is in the list + */ + strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); + remotehost[sizeof(remotehost) - 1] = '\0'; + hp = gethostbyname(remotehost); + if (hp == NULL) + return (NULL); + + for (; hp->h_addr_list[0] != NULL; hp->h_addr_list++) + if (!bcmp(hp->h_addr_list[0], (caddr_t)&raddr, sizeof(raddr))) + return (remotehost); + + /* + * either the DNS adminstrator has made a configuration + * mistake, or someone has attempted to spoof us + */ + in.s_addr = raddr; + syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", + inet_ntoa(in), hp->h_name); + return (NULL); +} diff --git a/src/lib/libc/net/rcmdsh.3 b/src/lib/libc/net/rcmdsh.3 new file mode 100644 index 0000000000..fd89acee8f --- /dev/null +++ b/src/lib/libc/net/rcmdsh.3 @@ -0,0 +1,108 @@ +.\" $OpenBSD: rcmdsh.3,v 1.3 1998/06/26 17:54:09 millert Exp $ +.\" +.\" Copyright (c) 1983, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd Sep 1, 1996 +.Dt RCMDSH 3 +.Os OpenBSD +.Sh NAME +.Nm rcmdsh +.Nd return a stream to a remote command without superuser +.Sh SYNOPSIS +.Fd #include +.Ft int +.Fn rcmdsh "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "char *rshprog" +.Sh DESCRIPTION +The +.Fn rcmdsh +function +is used by normal users to execute a command on +a remote machine using an authentication scheme based +on reserved port numbers using +.Xr rshd 8 +or the value of +.Fa rshprog +(if non-NULL). +.Pp +The +.Fn rcmdsh +function +looks up the host +.Fa *ahost +using +.Xr gethostbyname 3 , +returning \-1 if the host does not exist. +Otherwise +.Fa *ahost +is set to the standard name of the host +and a connection is established to a server +residing at the well-known Internet port +.Li shell/tcp +(or whatever port is used by +.Fa rshprog +). The parameter +.Fa inport +is ignored; it is only included to provide an interface similar to +.Xr rcmd 3 . +.Pp +If the connection succeeds, +a socket in the +.Tn UNIX +domain of type +.Dv SOCK_STREAM +is returned to the caller, and given to the remote +command as +.Em stdin +and +.Em stdout , +and +.Em stderr . +.Sh DIAGNOSTICS +The +.Fn rcmdsh +function +returns a valid socket descriptor on success. +It returns \-1 on error and prints a diagnostic message on the standard error. +.Sh SEE ALSO +.Xr rsh 1 , +.Xr socketpair 2 , +.Xr rcmd 3 , +.Xr rshd 8 +.Sh BUGS +If +.Xr rsh 1 +gets an error a file descriptor is still returned instead of \-1. +.Sh HISTORY +The +.Fn rcmdsh +function first appeared in +.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 @@ +/* $OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $ */ + +/* + * This is an rcmd() replacement originally by + * Chris Siebenmann . + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: rcmdsh.c,v 1.5 1998/04/25 16:23:58 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * This is a replacement rcmd() function that uses the rsh(1) + * program in place of a direct rcmd(3) function call so as to + * avoid having to be root. Note that rport is ignored. + */ +/* ARGSUSED */ +int +rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog) + char **ahost; + int rport; + const char *locuser, *remuser, *cmd; + char *rshprog; +{ + struct hostent *hp; + int cpid, sp[2]; + char *p; + struct passwd *pw; + + /* What rsh/shell to use. */ + if (rshprog == NULL) + rshprog = _PATH_RSH; + + /* locuser must exist on this host. */ + if ((pw = getpwnam(locuser)) == NULL) { + (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser); + return(-1); + } + + /* Validate remote hostname. */ + if (strcmp(*ahost, "localhost") != 0) { + if ((hp = gethostbyname(*ahost)) == NULL) { + herror(*ahost); + return(-1); + } + *ahost = hp->h_name; + } + + /* Get a socketpair we'll use for stdin and stdout. */ + if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) { + perror("rcmdsh: socketpair"); + return(-1); + } + + cpid = fork(); + if (cpid < 0) { + perror("rcmdsh: fork failed"); + return(-1); + } else if (cpid == 0) { + /* + * Child. We use sp[1] to be stdin/stdout, and close sp[0]. + */ + (void) close(sp[0]); + if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { + perror("rcmdsh: dup2 failed"); + _exit(255); + } + /* Fork again to lose parent. */ + cpid = fork(); + if (cpid < 0) { + perror("rcmdsh: fork to lose parent failed"); + _exit(255); + } + if (cpid > 0) + _exit(0); + + /* In grandchild here. Become local user for rshprog. */ + if (setuid(pw->pw_uid)) { + (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n", + pw->pw_uid, strerror(errno)); + _exit(255); + } + + /* + * If remote host is "localhost" and local and remote user + * are the same, avoid running remote shell for efficiency. + */ + if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) { + if (pw->pw_shell[0] == '\0') + rshprog = _PATH_BSHELL; + else + rshprog = pw->pw_shell; + p = strrchr(rshprog, '/'); + execlp(rshprog, p ? p+1 : rshprog, "-c", cmd, + (char *) NULL); + } else { + p = strrchr(rshprog, '/'); + execlp(rshprog, p ? p+1 : rshprog, *ahost, "-l", + remuser, cmd, (char *) NULL); + } + (void) fprintf(stderr, "rcmdsh: execlp %s failed: %s\n", + rshprog, strerror(errno)); + _exit(255); + } else { + /* Parent. close sp[1], return sp[0]. */ + (void) close(sp[1]); + /* Reap child. */ + (void) wait(NULL); + return(sp[0]); + } + /* NOTREACHED */ +} diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c new file mode 100644 index 0000000000..d209a07213 --- /dev/null +++ b/src/lib/libc/net/recv.c @@ -0,0 +1,50 @@ +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: recv.c,v 1.2 1996/08/19 08:29:40 tholo Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include + +ssize_t +recv(s, buf, len, flags) + int s, flags; + size_t len; + void *buf; +{ + return (recvfrom(s, buf, len, flags, NULL, 0)); +} diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..f7a0358967 --- /dev/null +++ b/src/lib/libc/net/res_comp.c @@ -0,0 +1,510 @@ +/* $OpenBSD: res_comp.c,v 1.8 1997/07/09 01:08:49 millert Exp $ */ + +/* + * ++Copyright++ 1985, 1993 + * - + * Copyright (c) 1985, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $"; +#else +static char rcsid[] = "$OpenBSD: res_comp.c,v 1.8 1997/07/09 01:08:49 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +static int dn_find __P((u_char *exp_dn, u_char *msg, + u_char **dnptrs, u_char **lastdnptr)); + +/* + * Expand compressed domain name 'comp_dn' to full domain name. + * 'msg' is a pointer to the begining of the message, + * 'eomorig' points to the first location after the message, + * 'exp_dn' is a pointer to a buffer of size 'length' for the result. + * Return size of compressed name or -1 if there was an error. + */ +int +dn_expand(msg, eomorig, comp_dn, exp_dn, length) + const u_char *msg, *eomorig, *comp_dn; + char *exp_dn; + int length; +{ + register const u_char *cp; + register char *dn; + register int n, c; + char *eom; + int len = -1, checked = 0; + + dn = exp_dn; + cp = comp_dn; + if (length > MAXHOSTNAMELEN-1) + length = MAXHOSTNAMELEN-1; + eom = exp_dn + length; + /* + * fetch next label in domain name + */ + while ((n = *cp++)) { + /* + * Check for indirection + */ + switch (n & INDIR_MASK) { + case 0: + if (dn != exp_dn) { + if (dn >= eom) + return (-1); + *dn++ = '.'; + } + if (dn+n >= eom) + return (-1); + checked += n + 1; + while (--n >= 0) { + if (((c = *cp++) == '.') || (c == '\\')) { + if (dn + n + 2 >= eom) + return (-1); + *dn++ = '\\'; + } + *dn++ = c; + if (cp >= eomorig) /* out of range */ + return (-1); + } + break; + + case INDIR_MASK: + if (len < 0) + len = cp - comp_dn + 1; + cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); + if (cp < msg || cp >= eomorig) /* out of range */ + return (-1); + checked += 2; + /* + * Check for loops in the compressed name; + * if we've looked at the whole message, + * there must be a loop. + */ + if (checked >= eomorig - msg) + return (-1); + break; + + default: + return (-1); /* flag error */ + } + } + *dn = '\0'; + if (len < 0) + len = cp - comp_dn; + return (len); +} + +/* + * Compress domain name 'exp_dn' into 'comp_dn'. + * Return the size of the compressed name or -1. + * 'length' is the size of the array pointed to by 'comp_dn'. + * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] + * is a pointer to the beginning of the message. The list ends with NULL. + * 'lastdnptr' is a pointer to the end of the arrary pointed to + * by 'dnptrs'. Side effect is to update the list of pointers for + * labels inserted into the message as we compress the name. + * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' + * is NULL, we don't update the list. + */ +int +dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) + const char *exp_dn; + u_char *comp_dn, **dnptrs, **lastdnptr; + int length; +{ + register u_char *cp, *dn; + register int c, l; + u_char **cpp, **lpp, *sp, *eob; + u_char *msg; + + dn = (u_char *)exp_dn; + cp = comp_dn; + eob = cp + length; + lpp = cpp = NULL; + if (dnptrs != NULL) { + if ((msg = *dnptrs++) != NULL) { + for (cpp = dnptrs; *cpp != NULL; cpp++) + ; + lpp = cpp; /* end of list to search */ + } + } else + msg = NULL; + for (c = *dn++; c != '\0'; ) { + /* look to see if we can use pointers */ + if (msg != NULL) { + if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { + if (cp+1 >= eob) + return (-1); + *cp++ = (l >> 8) | INDIR_MASK; + *cp++ = l % 256; + return (cp - comp_dn); + } + /* not found, save it */ + if (lastdnptr != NULL && cpp < lastdnptr-1) { + *cpp++ = cp; + *cpp = NULL; + } + } + sp = cp++; /* save ptr to length byte */ + do { + if (c == '.') { + c = *dn++; + break; + } + if (c == '\\') { + if ((c = *dn++) == '\0') + break; + } + if (cp >= eob) { + if (msg != NULL) + *lpp = NULL; + return (-1); + } + *cp++ = c; + } while ((c = *dn++) != '\0'); + /* catch trailing '.'s but not '..' */ + if ((l = cp - sp - 1) == 0 && c == '\0') { + cp--; + break; + } + if (l <= 0 || l > MAXLABEL) { + if (msg != NULL) + *lpp = NULL; + return (-1); + } + *sp = l; + } + if (cp >= eob) { + if (msg != NULL) + *lpp = NULL; + return (-1); + } + *cp++ = '\0'; + return (cp - comp_dn); +} + +/* + * Skip over a compressed domain name. Return the size or -1. + */ +int +__dn_skipname(comp_dn, eom) + const u_char *comp_dn, *eom; +{ + register const u_char *cp; + register int n; + + cp = comp_dn; + while (cp < eom && (n = *cp++)) { + /* + * check for indirection + */ + switch (n & INDIR_MASK) { + case 0: /* normal case, n == len */ + cp += n; + continue; + case INDIR_MASK: /* indirection */ + cp++; + break; + default: /* illegal type */ + return (-1); + } + break; + } + if (cp > eom) + return (-1); + return (cp - comp_dn); +} + +static int +mklower(ch) + register int ch; +{ + if (isascii(ch) && isupper(ch)) + return (tolower(ch)); + return (ch); +} + +/* + * Search for expanded name from a list of previously compressed names. + * Return the offset from msg if found or -1. + * dnptrs is the pointer to the first name on the list, + * not the pointer to the start of the message. + */ +static int +dn_find(exp_dn, msg, dnptrs, lastdnptr) + u_char *exp_dn, *msg; + u_char **dnptrs, **lastdnptr; +{ + register u_char *dn, *cp, **cpp; + register int n; + u_char *sp; + + for (cpp = dnptrs; cpp < lastdnptr; cpp++) { + dn = exp_dn; + sp = cp = *cpp; + while ((n = *cp++)) { + /* + * check for indirection + */ + switch (n & INDIR_MASK) { + case 0: /* normal case, n == len */ + while (--n >= 0) { + if (*dn == '.') + goto next; + if (*dn == '\\') + dn++; + if (mklower(*dn++) != mklower(*cp++)) + goto next; + } + if ((n = *dn++) == '\0' && *cp == '\0') + return (sp - msg); + if (n == '.') + continue; + goto next; + + case INDIR_MASK: /* indirection */ + cp = msg + (((n & 0x3f) << 8) | *cp); + break; + + default: /* illegal type */ + return (-1); + } + } + if (*dn == '\0') + return (sp - msg); + next: ; + } + return (-1); +} + +/* + * Verify that a domain name uses an acceptable character set. + */ + +/* + * Note the conspicuous absence of ctype macros in these definitions. On + * non-ASCII hosts, we can't depend on string literals or ctype macros to + * tell us anything about network-format data. The rest of the BIND system + * is not careful about this, but for some reason, we're doing it right here. + */ +#define PERIOD 0x2e +#define hyphenchar(c) ((c) == 0x2d) +#define bslashchar(c) ((c) == 0x5c) +#define periodchar(c) ((c) == PERIOD) +#define asterchar(c) ((c) == 0x2a) +#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ + || ((c) >= 0x61 && (c) <= 0x7a)) +#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) + +#define borderchar(c) (alphachar(c) || digitchar(c)) +#define middlechar(c) (borderchar(c) || hyphenchar(c)) +#define domainchar(c) ((c) > 0x20 && (c) < 0x7f) + +int +res_hnok(dn) + const char *dn; +{ + int pch = PERIOD, ch = *dn++; + + while (ch != '\0') { + int nch = *dn++; + + if (periodchar(ch)) { + ; + } else if (periodchar(pch)) { + if (!borderchar(ch)) + return (0); + } else if (periodchar(nch) || nch == '\0') { + if (!borderchar(ch)) + return (0); + } else { + if (!middlechar(ch)) + return (0); + } + pch = ch, ch = nch; + } + return (1); +} + +/* + * hostname-like (A, MX, WKS) owners can have "*" as their first label + * but must otherwise be as a host name. + */ +int +res_ownok(dn) + const char *dn; +{ + if (asterchar(dn[0])) { + if (periodchar(dn[1])) + return (res_hnok(dn+2)); + if (dn[1] == '\0') + return (1); + } + return (res_hnok(dn)); +} + +/* + * SOA RNAMEs and RP RNAMEs can have any printable character in their first + * label, but the rest of the name has to look like a host name. + */ +int +res_mailok(dn) + const char *dn; +{ + int ch, escaped = 0; + + /* "." is a valid missing representation */ + if (*dn == '\0') + return(1); + + /* otherwise