xref: /illumos-gate/usr/src/lib/libsasl/lib/saslutil.c (revision e44e85a7)
17c478bd9Sstevel@tonic-gate /*
2*e44e85a7SRitwik Ghoshal  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /* saslutil.c
77c478bd9Sstevel@tonic-gate  * Rob Siemborski
87c478bd9Sstevel@tonic-gate  * Tim Martin
97c478bd9Sstevel@tonic-gate  * $Id: saslutil.c,v 1.41 2003/03/19 18:25:28 rjs3 Exp $
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
157c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
167c478bd9Sstevel@tonic-gate  * are met:
177c478bd9Sstevel@tonic-gate  *
187c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
197c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
227c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
237c478bd9Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
247c478bd9Sstevel@tonic-gate  *    distribution.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * 3. The name "Carnegie Mellon University" must not be used to
277c478bd9Sstevel@tonic-gate  *    endorse or promote products derived from this software without
287c478bd9Sstevel@tonic-gate  *    prior written permission. For permission or any other legal
297c478bd9Sstevel@tonic-gate  *    details, please contact
307c478bd9Sstevel@tonic-gate  *      Office of Technology Transfer
317c478bd9Sstevel@tonic-gate  *      Carnegie Mellon University
327c478bd9Sstevel@tonic-gate  *      5000 Forbes Avenue
337c478bd9Sstevel@tonic-gate  *      Pittsburgh, PA  15213-3890
347c478bd9Sstevel@tonic-gate  *      (412) 268-4387, fax: (412) 268-7395
357c478bd9Sstevel@tonic-gate  *      tech-transfer@andrew.cmu.edu
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * 4. Redistributions of any form whatsoever must retain the following
387c478bd9Sstevel@tonic-gate  *    acknowledgment:
397c478bd9Sstevel@tonic-gate  *    "This product includes software developed by Computing Services
407c478bd9Sstevel@tonic-gate  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
437c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
447c478bd9Sstevel@tonic-gate  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
457c478bd9Sstevel@tonic-gate  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
467c478bd9Sstevel@tonic-gate  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
477c478bd9Sstevel@tonic-gate  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
487c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <config.h>
527c478bd9Sstevel@tonic-gate #include <stdio.h>
537c478bd9Sstevel@tonic-gate #include <stdlib.h>
547c478bd9Sstevel@tonic-gate #include <string.h>
557c478bd9Sstevel@tonic-gate #include <assert.h>
567c478bd9Sstevel@tonic-gate #include <ctype.h>
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <errno.h>
617c478bd9Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
627c478bd9Sstevel@tonic-gate #include <unistd.h>
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate #ifdef HAVE_TIME_H
657c478bd9Sstevel@tonic-gate #include <time.h>
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate #include "saslint.h"
687c478bd9Sstevel@tonic-gate #include <saslutil.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*  Contains:
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * sasl_decode64
737c478bd9Sstevel@tonic-gate  * sasl_encode64
747c478bd9Sstevel@tonic-gate  * sasl_mkchal
757c478bd9Sstevel@tonic-gate  * sasl_utf8verify
767c478bd9Sstevel@tonic-gate  * sasl_randcreate
777c478bd9Sstevel@tonic-gate  * sasl_randfree
787c478bd9Sstevel@tonic-gate  * sasl_randseed
797c478bd9Sstevel@tonic-gate  * sasl_rand
807c478bd9Sstevel@tonic-gate  * sasl_churn
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
847c478bd9Sstevel@tonic-gate char *encode_table;
857c478bd9Sstevel@tonic-gate char *decode_table;
867c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define RPOOL_SIZE 3
897c478bd9Sstevel@tonic-gate struct sasl_rand_s {
907c478bd9Sstevel@tonic-gate     unsigned short pool[RPOOL_SIZE];
917c478bd9Sstevel@tonic-gate     /* since the init time might be really bad let's make this lazy */
927c478bd9Sstevel@tonic-gate     int initialized;
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static char basis_64[] =
987c478bd9Sstevel@tonic-gate    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????";
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static char index_64[128] = {
1017c478bd9Sstevel@tonic-gate     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1027c478bd9Sstevel@tonic-gate     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1037c478bd9Sstevel@tonic-gate     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1047c478bd9Sstevel@tonic-gate     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1057c478bd9Sstevel@tonic-gate     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
1067c478bd9Sstevel@tonic-gate     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1077c478bd9Sstevel@tonic-gate     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1087c478bd9Sstevel@tonic-gate     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* base64 encode
1127c478bd9Sstevel@tonic-gate  *  in      -- input data
1137c478bd9Sstevel@tonic-gate  *  inlen   -- input data length
1147c478bd9Sstevel@tonic-gate  *  out     -- output buffer (will be NUL terminated)
1157c478bd9Sstevel@tonic-gate  *  outmax  -- max size of output buffer
1167c478bd9Sstevel@tonic-gate  * result:
1177c478bd9Sstevel@tonic-gate  *  outlen  -- gets actual length of output buffer (optional)
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * Returns SASL_OK on success, SASL_BUFOVER if result won't fit
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
sasl_encode64(const char * _in,unsigned inlen,char * _out,unsigned outmax,unsigned * outlen)1227c478bd9Sstevel@tonic-gate int sasl_encode64(const char *_in, unsigned inlen,
1237c478bd9Sstevel@tonic-gate 		  char *_out, unsigned outmax, unsigned *outlen)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate     const unsigned char *in = (const unsigned char *)_in;
1267c478bd9Sstevel@tonic-gate     unsigned char *out = (unsigned char *)_out;
1277c478bd9Sstevel@tonic-gate     unsigned char oval;
1287c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
1297c478bd9Sstevel@tonic-gate     char *blah;
1307c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
1317c478bd9Sstevel@tonic-gate     unsigned olen;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate     /* check params */
1347c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1357c478bd9Sstevel@tonic-gate     if (((inlen >0) && (in == NULL)) || _out == NULL) return SASL_BADPARAM;
1367c478bd9Sstevel@tonic-gate #else
1377c478bd9Sstevel@tonic-gate     if ((inlen >0) && (in == NULL)) return SASL_BADPARAM;
1387c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate     /* Will it fit? */
1417c478bd9Sstevel@tonic-gate     olen = (inlen + 2) / 3 * 4;
1427c478bd9Sstevel@tonic-gate     if (outlen)
1437c478bd9Sstevel@tonic-gate       *outlen = olen;
144*e44e85a7SRitwik Ghoshal     if (outmax <= olen)
1457c478bd9Sstevel@tonic-gate       return SASL_BUFOVER;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate     /* Do the work... */
1487c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
1497c478bd9Sstevel@tonic-gate     blah=(char *) out;
1507c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
1517c478bd9Sstevel@tonic-gate     while (inlen >= 3) {
1527c478bd9Sstevel@tonic-gate       /* user provided max buffer size; make sure we don't go over it */
1537c478bd9Sstevel@tonic-gate         *out++ = basis_64[in[0] >> 2];
1547c478bd9Sstevel@tonic-gate         *out++ = basis_64[((in[0] << 4) & 0x30) | (in[1] >> 4)];
1557c478bd9Sstevel@tonic-gate         *out++ = basis_64[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
1567c478bd9Sstevel@tonic-gate         *out++ = basis_64[in[2] & 0x3f];
1577c478bd9Sstevel@tonic-gate         in += 3;
1587c478bd9Sstevel@tonic-gate         inlen -= 3;
1597c478bd9Sstevel@tonic-gate     }
1607c478bd9Sstevel@tonic-gate     if (inlen > 0) {
1617c478bd9Sstevel@tonic-gate       /* user provided max buffer size; make sure we don't go over it */
1627c478bd9Sstevel@tonic-gate         *out++ = basis_64[in[0] >> 2];
1637c478bd9Sstevel@tonic-gate         oval = (in[0] << 4) & 0x30;
1647c478bd9Sstevel@tonic-gate         if (inlen > 1) oval |= in[1] >> 4;
1657c478bd9Sstevel@tonic-gate         *out++ = basis_64[oval];
1667c478bd9Sstevel@tonic-gate         *out++ = (inlen < 2) ? '=' : basis_64[(in[1] << 2) & 0x3c];
1677c478bd9Sstevel@tonic-gate         *out++ = '=';
1687c478bd9Sstevel@tonic-gate     }
1697c478bd9Sstevel@tonic-gate 
170*e44e85a7SRitwik Ghoshal     *out = '\0';
171*e44e85a7SRitwik Ghoshal 
1727c478bd9Sstevel@tonic-gate     return SASL_OK;
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /* base64 decode
1767c478bd9Sstevel@tonic-gate  *  in     -- input data
1777c478bd9Sstevel@tonic-gate  *  inlen  -- length of input data
1787c478bd9Sstevel@tonic-gate  *  out    -- output data (may be same as in, must have enough space)
1797c478bd9Sstevel@tonic-gate  *  outmax  -- max size of output buffer
1807c478bd9Sstevel@tonic-gate  * result:
1817c478bd9Sstevel@tonic-gate  *  outlen -- actual output length
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * returns:
1847c478bd9Sstevel@tonic-gate  * SASL_BADPROT on bad base64,
1857c478bd9Sstevel@tonic-gate  * SASL_BUFOVER if result won't fit,
1867c478bd9Sstevel@tonic-gate  * SASL_OK on success
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate 
sasl_decode64(const char * in,unsigned inlen,char * out,unsigned outmax,unsigned * outlen)1897c478bd9Sstevel@tonic-gate int sasl_decode64(const char *in, unsigned inlen,
1907c478bd9Sstevel@tonic-gate 		  char *out, unsigned outmax, unsigned *outlen)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate     unsigned len = 0,lup;
1937c478bd9Sstevel@tonic-gate     int c1, c2, c3, c4;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate     /* check parameters */
1967c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1977c478bd9Sstevel@tonic-gate     if (out==NULL || in == NULL) return SASL_FAIL;
1987c478bd9Sstevel@tonic-gate #else
1997c478bd9Sstevel@tonic-gate     if (out==NULL) return SASL_FAIL;
2007c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate     /* xxx these necessary? */
2037c478bd9Sstevel@tonic-gate     if (in[0] == '+' && in[1] == ' ') in += 2;
2047c478bd9Sstevel@tonic-gate     if (*in == '\r') return SASL_FAIL;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate     for (lup=0;lup<inlen/4;lup++)
2077c478bd9Sstevel@tonic-gate     {
2087c478bd9Sstevel@tonic-gate         c1 = in[0];
2097c478bd9Sstevel@tonic-gate         if (CHAR64(c1) == -1) return SASL_BADPROT;
2107c478bd9Sstevel@tonic-gate         c2 = in[1];
2117c478bd9Sstevel@tonic-gate         if (CHAR64(c2) == -1) return SASL_BADPROT;
2127c478bd9Sstevel@tonic-gate         c3 = in[2];
2137c478bd9Sstevel@tonic-gate         if (c3 != '=' && CHAR64(c3) == -1) return SASL_BADPROT;
2147c478bd9Sstevel@tonic-gate         c4 = in[3];
2157c478bd9Sstevel@tonic-gate         if (c4 != '=' && CHAR64(c4) == -1) return SASL_BADPROT;
2167c478bd9Sstevel@tonic-gate         in += 4;
2177c478bd9Sstevel@tonic-gate         *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
2187c478bd9Sstevel@tonic-gate         if(++len >= outmax) return SASL_BUFOVER;
2197c478bd9Sstevel@tonic-gate         if (c3 != '=') {
2207c478bd9Sstevel@tonic-gate             *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
2217c478bd9Sstevel@tonic-gate             if(++len >= outmax) return SASL_BUFOVER;
2227c478bd9Sstevel@tonic-gate             if (c4 != '=') {
2237c478bd9Sstevel@tonic-gate                 *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
2247c478bd9Sstevel@tonic-gate                 if(++len >= outmax) return SASL_BUFOVER;
2257c478bd9Sstevel@tonic-gate             }
2267c478bd9Sstevel@tonic-gate         }
2277c478bd9Sstevel@tonic-gate     }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate     *out=0; /* terminate string */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate     if(outlen) *outlen=len;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate     return SASL_OK;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* make a challenge string (NUL terminated)
2377c478bd9Sstevel@tonic-gate  *  buf      -- buffer for result
2387c478bd9Sstevel@tonic-gate  *  maxlen   -- max length of result
2397c478bd9Sstevel@tonic-gate  *  hostflag -- 0 = don't include hostname, 1 = include hostname
2407c478bd9Sstevel@tonic-gate  * returns final length or 0 if not enough space
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate 
sasl_mkchal(sasl_conn_t * conn,char * buf,unsigned maxlen,unsigned hostflag)2437c478bd9Sstevel@tonic-gate int sasl_mkchal(sasl_conn_t *conn,
2447c478bd9Sstevel@tonic-gate 		char *buf,
2457c478bd9Sstevel@tonic-gate 		unsigned maxlen,
2467c478bd9Sstevel@tonic-gate 		unsigned hostflag)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
2497c478bd9Sstevel@tonic-gate   sasl_rand_t *pool = NULL;
2507c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
2517c478bd9Sstevel@tonic-gate   unsigned long randnum;
2527c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
2537c478bd9Sstevel@tonic-gate   int ret;
2547c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
2557c478bd9Sstevel@tonic-gate   time_t now;
2567c478bd9Sstevel@tonic-gate   unsigned len;
2577c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2587c478bd9Sstevel@tonic-gate   const sasl_utils_t *utils;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate   if (conn->type == SASL_CONN_SERVER)
2617c478bd9Sstevel@tonic-gate     utils = ((sasl_server_conn_t *)conn)->sparams->utils;
2627c478bd9Sstevel@tonic-gate   else if (conn->type == SASL_CONN_CLIENT)
2637c478bd9Sstevel@tonic-gate     utils = ((sasl_client_conn_t *)conn)->cparams->utils;
2647c478bd9Sstevel@tonic-gate   else
2657c478bd9Sstevel@tonic-gate     return 0;
2667c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate   len = 4			/* <.>\0 */
2697c478bd9Sstevel@tonic-gate     + (2 * 20);			/* 2 numbers, 20 => max size of 64bit
2707c478bd9Sstevel@tonic-gate 				 * ulong in base 10 */
2717c478bd9Sstevel@tonic-gate   if (hostflag && conn->serverFQDN)
2727c478bd9Sstevel@tonic-gate     len += strlen(conn->serverFQDN) + 1 /* for the @ */;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate   if (maxlen < len)
2757c478bd9Sstevel@tonic-gate     return 0;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2787c478bd9Sstevel@tonic-gate   utils->rand(utils->rpool, (char *)&randnum, sizeof (randnum));
2797c478bd9Sstevel@tonic-gate #else
2807c478bd9Sstevel@tonic-gate   ret = sasl_randcreate(&pool);
2817c478bd9Sstevel@tonic-gate   if(ret != SASL_OK) return 0; /* xxx sasl return code? */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate   sasl_rand(pool, (char *)&randnum, sizeof(randnum));
2847c478bd9Sstevel@tonic-gate   sasl_randfree(&pool);
2857c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate   time(&now);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate   if (hostflag && conn->serverFQDN)
2907c478bd9Sstevel@tonic-gate     snprintf(buf,maxlen, "<%lu.%lu@%s>", randnum, now, conn->serverFQDN);
2917c478bd9Sstevel@tonic-gate   else
2927c478bd9Sstevel@tonic-gate     snprintf(buf,maxlen, "<%lu.%lu>", randnum, now);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate   return strlen(buf);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate   /* borrowed from larry. probably works :)
2987c478bd9Sstevel@tonic-gate    * probably is also in acap server somewhere
2997c478bd9Sstevel@tonic-gate    */
sasl_utf8verify(const char * str,unsigned len)3007c478bd9Sstevel@tonic-gate int sasl_utf8verify(const char *str, unsigned len)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate   unsigned i;
3037c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3047c478bd9Sstevel@tonic-gate   if (str == NULL)
3057c478bd9Sstevel@tonic-gate     return len == 0 ? SASL_OK : SASL_BADPARAM;
3067c478bd9Sstevel@tonic-gate   if (len == 0) len = strlen(str);
3077c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3087c478bd9Sstevel@tonic-gate   for (i = 0; i < len; i++) {
3097c478bd9Sstevel@tonic-gate     /* how many octets? */
3107c478bd9Sstevel@tonic-gate     int seqlen = 0;
3117c478bd9Sstevel@tonic-gate     while (str[i] & (0x80 >> seqlen)) ++seqlen;
3127c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3137c478bd9Sstevel@tonic-gate     if (seqlen == 0) {
3147c478bd9Sstevel@tonic-gate 	if (str[i] == '\0' || str[i] == '\n' || str[i] == '\r')
3157c478bd9Sstevel@tonic-gate 	   return SASL_BADPROT;
3167c478bd9Sstevel@tonic-gate 	continue; /* this is a valid US-ASCII char */
3177c478bd9Sstevel@tonic-gate     }
3187c478bd9Sstevel@tonic-gate #else
3197c478bd9Sstevel@tonic-gate     if (seqlen == 0) continue; /* this is a valid US-ASCII char */
3207c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3217c478bd9Sstevel@tonic-gate     if (seqlen == 1) return SASL_BADPROT; /* this shouldn't happen here */
3227c478bd9Sstevel@tonic-gate     if (seqlen > 6) return SASL_BADPROT; /* illegal */
3237c478bd9Sstevel@tonic-gate     while (--seqlen)
3247c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3257c478bd9Sstevel@tonic-gate       if ((str[++i] & 0xC0) != 0x80)
3267c478bd9Sstevel@tonic-gate 	return SASL_BADPROT; /* needed an appropriate octet */
3277c478bd9Sstevel@tonic-gate #else
3287c478bd9Sstevel@tonic-gate       if ((str[++i] & 0xC0) != 0xF0) return SASL_BADPROT; /* needed a 10 octet */
3297c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3307c478bd9Sstevel@tonic-gate   }
3317c478bd9Sstevel@tonic-gate   return SASL_OK;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * To see why this is really bad see RFC 1750
3367c478bd9Sstevel@tonic-gate  *
3377c478bd9Sstevel@tonic-gate  * unfortunatly there currently is no way to make
3387c478bd9Sstevel@tonic-gate  * cryptographically secure pseudo random numbers
3397c478bd9Sstevel@tonic-gate  * without specialized hardware etc...
3407c478bd9Sstevel@tonic-gate  * thus, this is for nonce use only
3417c478bd9Sstevel@tonic-gate  */
getranddata(unsigned short ret[RPOOL_SIZE])3427c478bd9Sstevel@tonic-gate void getranddata(unsigned short ret[RPOOL_SIZE])
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate     long curtime;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate     memset(ret, 0, RPOOL_SIZE*sizeof(unsigned short));
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate #ifdef DEV_RANDOM
3497c478bd9Sstevel@tonic-gate     {
3507c478bd9Sstevel@tonic-gate 	int fd;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	fd = open(DEV_RANDOM, O_RDONLY);
3537c478bd9Sstevel@tonic-gate 	if(fd != -1) {
3547c478bd9Sstevel@tonic-gate 	    unsigned char *buf = (unsigned char *)ret;
3557c478bd9Sstevel@tonic-gate 	    ssize_t bytesread = 0;
3567c478bd9Sstevel@tonic-gate 	    size_t bytesleft = RPOOL_SIZE*sizeof(unsigned short);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	    do {
3597c478bd9Sstevel@tonic-gate 		bytesread = read(fd, buf, bytesleft);
3607c478bd9Sstevel@tonic-gate 		if(bytesread == -1 && errno == EINTR) continue;
3617c478bd9Sstevel@tonic-gate 		else if(bytesread <= 0) break;
3627c478bd9Sstevel@tonic-gate 		bytesleft -= bytesread;
3637c478bd9Sstevel@tonic-gate 		buf += bytesread;
3647c478bd9Sstevel@tonic-gate 	    } while(bytesleft != 0);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	    close(fd);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate     }
3697c478bd9Sstevel@tonic-gate #endif
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate #ifdef HAVE_GETPID
3727c478bd9Sstevel@tonic-gate     ret[0] ^= (unsigned short) getpid();
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #ifdef HAVE_GETTIMEOFDAY
3767c478bd9Sstevel@tonic-gate     {
3777c478bd9Sstevel@tonic-gate 	struct timeval tv;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* xxx autoconf macro */
3807c478bd9Sstevel@tonic-gate #ifdef _SVID_GETTOD
3817c478bd9Sstevel@tonic-gate 	if (!gettimeofday(&tv))
3827c478bd9Sstevel@tonic-gate #else
3837c478bd9Sstevel@tonic-gate 	if (!gettimeofday(&tv, NULL))
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 	{
3867c478bd9Sstevel@tonic-gate 	    /* longs are guaranteed to be at least 32 bits; we need
3877c478bd9Sstevel@tonic-gate 	       16 bits in each short */
3887c478bd9Sstevel@tonic-gate 	    ret[0] ^= (unsigned short) (tv.tv_sec & 0xFFFF);
3897c478bd9Sstevel@tonic-gate 	    ret[1] ^= (unsigned short) (clock() & 0xFFFF);
3907c478bd9Sstevel@tonic-gate 	    ret[1] ^= (unsigned short) (tv.tv_usec >> 16);
3917c478bd9Sstevel@tonic-gate 	    ret[2] ^= (unsigned short) (tv.tv_usec & 0xFFFF);
3927c478bd9Sstevel@tonic-gate 	    return;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate     }
3957c478bd9Sstevel@tonic-gate #endif /* HAVE_GETTIMEOFDAY */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate     /* if all else fails just use time() */
3987c478bd9Sstevel@tonic-gate     curtime = (long) time(NULL); /* better be at least 32 bits */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate     ret[0] ^= (unsigned short) (curtime >> 16);
4017c478bd9Sstevel@tonic-gate     ret[1] ^= (unsigned short) (curtime & 0xFFFF);
4027c478bd9Sstevel@tonic-gate     ret[2] ^= (unsigned short) (clock() & 0xFFFF);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate     return;
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
sasl_randcreate(sasl_rand_t ** rpool)4077c478bd9Sstevel@tonic-gate int sasl_randcreate(sasl_rand_t **rpool)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
4107c478bd9Sstevel@tonic-gate   (*rpool)=sasl_sun_ALLOC(sizeof(sasl_rand_t));
4117c478bd9Sstevel@tonic-gate #else
4127c478bd9Sstevel@tonic-gate   (*rpool)=sasl_ALLOC(sizeof(sasl_rand_t));
4137c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
4147c478bd9Sstevel@tonic-gate   if ((*rpool) == NULL) return SASL_NOMEM;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate   /* init is lazy */
4177c478bd9Sstevel@tonic-gate   (*rpool)->initialized = 0;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate   return SASL_OK;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
sasl_randfree(sasl_rand_t ** rpool)4227c478bd9Sstevel@tonic-gate void sasl_randfree(sasl_rand_t **rpool)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
4257c478bd9Sstevel@tonic-gate     sasl_sun_FREE(*rpool);
4267c478bd9Sstevel@tonic-gate #else
4277c478bd9Sstevel@tonic-gate     sasl_FREE(*rpool);
4287c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
sasl_randseed(sasl_rand_t * rpool,const char * seed,unsigned len)4317c478bd9Sstevel@tonic-gate void sasl_randseed (sasl_rand_t *rpool, const char *seed, unsigned len)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate     /* is it acceptable to just use the 1st 3 char's given??? */
4347c478bd9Sstevel@tonic-gate     unsigned int lup;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate     /* check params */
4377c478bd9Sstevel@tonic-gate     if (seed == NULL) return;
4387c478bd9Sstevel@tonic-gate     if (rpool == NULL) return;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate     rpool->initialized = 1;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate     if (len > sizeof(unsigned short)*RPOOL_SIZE)
4437c478bd9Sstevel@tonic-gate       len = sizeof(unsigned short)*RPOOL_SIZE;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate     for (lup = 0; lup < len; lup += 2)
4467c478bd9Sstevel@tonic-gate 	rpool->pool[lup/2] = (seed[lup] << 8) + seed[lup + 1];
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
randinit(sasl_rand_t * rpool)4497c478bd9Sstevel@tonic-gate static void randinit(sasl_rand_t *rpool)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate     assert(rpool);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate     if (!rpool->initialized) {
4547c478bd9Sstevel@tonic-gate 	getranddata(rpool->pool);
4557c478bd9Sstevel@tonic-gate 	rpool->initialized = 1;
4567c478bd9Sstevel@tonic-gate #if !(defined(WIN32)||defined(macintosh))
4577c478bd9Sstevel@tonic-gate #ifndef HAVE_JRAND48
4587c478bd9Sstevel@tonic-gate     {
4597c478bd9Sstevel@tonic-gate       /* xxx varies by platform */
4607c478bd9Sstevel@tonic-gate 	unsigned int *foo = (unsigned int *)rpool->pool;
4617c478bd9Sstevel@tonic-gate 	srandom(*foo);
4627c478bd9Sstevel@tonic-gate     }
4637c478bd9Sstevel@tonic-gate #endif /* HAVE_JRAND48 */
4647c478bd9Sstevel@tonic-gate #endif /* WIN32 */
4657c478bd9Sstevel@tonic-gate     }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
sasl_rand(sasl_rand_t * rpool,char * buf,unsigned len)4697c478bd9Sstevel@tonic-gate void sasl_rand (sasl_rand_t *rpool, char *buf, unsigned len)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate     unsigned int lup;
4727c478bd9Sstevel@tonic-gate     /* check params */
4737c478bd9Sstevel@tonic-gate     if (!rpool || !buf) return;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate     /* init if necessary */
4767c478bd9Sstevel@tonic-gate     randinit(rpool);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #if (defined(WIN32)||defined(macintosh))
4797c478bd9Sstevel@tonic-gate     for (lup=0;lup<len;lup++)
4807c478bd9Sstevel@tonic-gate 	buf[lup] = (char) (rand() >> 8);
4817c478bd9Sstevel@tonic-gate #else /* WIN32 */
4827c478bd9Sstevel@tonic-gate #ifdef HAVE_JRAND48
4837c478bd9Sstevel@tonic-gate     for (lup=0; lup<len; lup++)
4847c478bd9Sstevel@tonic-gate 	buf[lup] = (char) (jrand48(rpool->pool) >> 8);
4857c478bd9Sstevel@tonic-gate #else
4867c478bd9Sstevel@tonic-gate     for (lup=0;lup<len;lup++)
4877c478bd9Sstevel@tonic-gate 	buf[lup] = (char) (random() >> 8);
4887c478bd9Sstevel@tonic-gate #endif /* HAVE_JRAND48 */
4897c478bd9Sstevel@tonic-gate #endif /* WIN32 */
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /* this function is just a bad idea all around, since we're not trying to
4937c478bd9Sstevel@tonic-gate    implement a true random number generator */
sasl_churn(sasl_rand_t * rpool,const char * data,unsigned len)4947c478bd9Sstevel@tonic-gate void sasl_churn (sasl_rand_t *rpool, const char *data, unsigned len)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate     unsigned int lup;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate     /* check params */
4997c478bd9Sstevel@tonic-gate     if (!rpool || !data) return;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate     /* init if necessary */
5027c478bd9Sstevel@tonic-gate     randinit(rpool);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate     for (lup=0; lup<len; lup++)
5057c478bd9Sstevel@tonic-gate 	rpool->pool[lup % RPOOL_SIZE] ^= data[lup];
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
sasl_erasebuffer(char * buf,unsigned len)5087c478bd9Sstevel@tonic-gate void sasl_erasebuffer(char *buf, unsigned len) {
5097c478bd9Sstevel@tonic-gate     memset(buf, 0, len);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate #ifndef _SUN_SDK_
5137c478bd9Sstevel@tonic-gate #ifdef WIN32
5147c478bd9Sstevel@tonic-gate /*****************************************************************************
5157c478bd9Sstevel@tonic-gate  *
5167c478bd9Sstevel@tonic-gate  *  MODULE NAME : GETOPT.C
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  *  COPYRIGHTS:
5197c478bd9Sstevel@tonic-gate  *             This module contains code made available by IBM
5207c478bd9Sstevel@tonic-gate  *             Corporation on an AS IS basis.  Any one receiving the
5217c478bd9Sstevel@tonic-gate  *             module is considered to be licensed under IBM copyrights
5227c478bd9Sstevel@tonic-gate  *             to use the IBM-provided source code in any way he or she
5237c478bd9Sstevel@tonic-gate  *             deems fit, including copying it, compiling it, modifying
5247c478bd9Sstevel@tonic-gate  *             it, and redistributing it, with or without
5257c478bd9Sstevel@tonic-gate  *             modifications.  No license under any IBM patents or
5267c478bd9Sstevel@tonic-gate  *             patent applications is to be implied from this copyright
5277c478bd9Sstevel@tonic-gate  *             license.
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  *             A user of the module should understand that IBM cannot
5307c478bd9Sstevel@tonic-gate  *             provide technical support for the module and will not be
5317c478bd9Sstevel@tonic-gate  *             responsible for any consequences of use of the program.
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  *             Any notices, including this one, are not to be removed
5347c478bd9Sstevel@tonic-gate  *             from the module without the prior written consent of
5357c478bd9Sstevel@tonic-gate  *             IBM.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  *  AUTHOR:   Original author:
5387c478bd9Sstevel@tonic-gate  *                 G. R. Blair (BOBBLAIR at AUSVM1)
5397c478bd9Sstevel@tonic-gate  *                 Internet: bobblair@bobblair.austin.ibm.com
5407c478bd9Sstevel@tonic-gate  *
5417c478bd9Sstevel@tonic-gate  *            Extensively revised by:
5427c478bd9Sstevel@tonic-gate  *                 John Q. Walker II, Ph.D. (JOHHQ at RALVM6)
5437c478bd9Sstevel@tonic-gate  *                 Internet: johnq@ralvm6.vnet.ibm.com
5447c478bd9Sstevel@tonic-gate  *
5457c478bd9Sstevel@tonic-gate  *****************************************************************************/
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /******************************************************************************
5487c478bd9Sstevel@tonic-gate  * getopt()
5497c478bd9Sstevel@tonic-gate  *
5507c478bd9Sstevel@tonic-gate  * The getopt() function is a command line parser.  It returns the next
5517c478bd9Sstevel@tonic-gate  * option character in argv that matches an option character in opstring.
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  * The argv argument points to an array of argc+1 elements containing argc
5547c478bd9Sstevel@tonic-gate  * pointers to character strings followed by a null pointer.
5557c478bd9Sstevel@tonic-gate  *
5567c478bd9Sstevel@tonic-gate  * The opstring argument points to a string of option characters; if an
5577c478bd9Sstevel@tonic-gate  * option character is followed by a colon, the option is expected to have
5587c478bd9Sstevel@tonic-gate  * an argument that may or may not be separated from it by white space.
5597c478bd9Sstevel@tonic-gate  * The external variable optarg is set to point to the start of the option
5607c478bd9Sstevel@tonic-gate  * argument on return from getopt().
5617c478bd9Sstevel@tonic-gate  *
5627c478bd9Sstevel@tonic-gate  * The getopt() function places in optind the argv index of the next argument
5637c478bd9Sstevel@tonic-gate  * to be processed.  The system initializes the external variable optind to
5647c478bd9Sstevel@tonic-gate  * 1 before the first call to getopt().
5657c478bd9Sstevel@tonic-gate  *
5667c478bd9Sstevel@tonic-gate  * When all options have been processed (that is, up to the first nonoption
5677c478bd9Sstevel@tonic-gate  * argument), getopt() returns EOF.  The special option "--" may be used to
5687c478bd9Sstevel@tonic-gate  * delimit the end of the options; EOF will be returned, and "--" will be
5697c478bd9Sstevel@tonic-gate  * skipped.
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * The getopt() function returns a question mark (?) when it encounters an
5727c478bd9Sstevel@tonic-gate  * option character not included in opstring.  This error message can be
5737c478bd9Sstevel@tonic-gate  * disabled by setting opterr to zero.  Otherwise, it returns the option
5747c478bd9Sstevel@tonic-gate  * character that was detected.
5757c478bd9Sstevel@tonic-gate  *
5767c478bd9Sstevel@tonic-gate  * If the special option "--" is detected, or all options have been
5777c478bd9Sstevel@tonic-gate  * processed, EOF is returned.
5787c478bd9Sstevel@tonic-gate  *
5797c478bd9Sstevel@tonic-gate  * Options are marked by either a minus sign (-) or a slash (/).
5807c478bd9Sstevel@tonic-gate  *
5817c478bd9Sstevel@tonic-gate  * No errors are defined.
5827c478bd9Sstevel@tonic-gate  *****************************************************************************/
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate #include <string.h>                 /* for strchr() */
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /* static (global) variables that are specified as exported by getopt() */
5877c478bd9Sstevel@tonic-gate __declspec(dllexport) char *optarg = NULL;    /* pointer to the start of the option argument  */
5887c478bd9Sstevel@tonic-gate __declspec(dllexport) int   optind = 1;       /* number of the next argv[] to be evaluated    */
5897c478bd9Sstevel@tonic-gate __declspec(dllexport) int   opterr = 1;       /* non-zero if a question mark should be returned */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /* handle possible future character set concerns by putting this in a macro */
5937c478bd9Sstevel@tonic-gate #define _next_char(string)  (char)(*(string+1))
5947c478bd9Sstevel@tonic-gate 
getopt(int argc,char * argv[],char * opstring)5957c478bd9Sstevel@tonic-gate int getopt(int argc, char *argv[], char *opstring)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate     static char *pIndexPosition = NULL; /* place inside current argv string */
5987c478bd9Sstevel@tonic-gate     char *pArgString = NULL;        /* where to start from next */
5997c478bd9Sstevel@tonic-gate     char *pOptString;               /* the string in our program */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate     if (pIndexPosition != NULL) {
6037c478bd9Sstevel@tonic-gate         /* we last left off inside an argv string */
6047c478bd9Sstevel@tonic-gate         if (*(++pIndexPosition)) {
6057c478bd9Sstevel@tonic-gate             /* there is more to come in the most recent argv */
6067c478bd9Sstevel@tonic-gate             pArgString = pIndexPosition;
6077c478bd9Sstevel@tonic-gate         }
6087c478bd9Sstevel@tonic-gate     }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate     if (pArgString == NULL) {
6117c478bd9Sstevel@tonic-gate         /* we didn't leave off in the middle of an argv string */
6127c478bd9Sstevel@tonic-gate         if (optind >= argc) {
6137c478bd9Sstevel@tonic-gate             /* more command-line arguments than the argument count */
6147c478bd9Sstevel@tonic-gate             pIndexPosition = NULL;  /* not in the middle of anything */
6157c478bd9Sstevel@tonic-gate             return EOF;             /* used up all command-line arguments */
6167c478bd9Sstevel@tonic-gate         }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate         /*---------------------------------------------------------------------
6197c478bd9Sstevel@tonic-gate          * If the next argv[] is not an option, there can be no more options.
6207c478bd9Sstevel@tonic-gate          *-------------------------------------------------------------------*/
6217c478bd9Sstevel@tonic-gate         pArgString = argv[optind++]; /* set this to the next argument ptr */
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate         if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */
6247c478bd9Sstevel@tonic-gate             ('-' != *pArgString)) {
6257c478bd9Sstevel@tonic-gate             --optind;               /* point to current arg once we're done */
6267c478bd9Sstevel@tonic-gate             optarg = NULL;          /* no argument follows the option */
6277c478bd9Sstevel@tonic-gate             pIndexPosition = NULL;  /* not in the middle of anything */
6287c478bd9Sstevel@tonic-gate             return EOF;             /* used up all the command-line flags */
6297c478bd9Sstevel@tonic-gate         }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate         /* check for special end-of-flags markers */
6327c478bd9Sstevel@tonic-gate         if ((strcmp(pArgString, "-") == 0) ||
6337c478bd9Sstevel@tonic-gate             (strcmp(pArgString, "--") == 0)) {
6347c478bd9Sstevel@tonic-gate             optarg = NULL;          /* no argument follows the option */
6357c478bd9Sstevel@tonic-gate             pIndexPosition = NULL;  /* not in the middle of anything */
6367c478bd9Sstevel@tonic-gate             return EOF;             /* encountered the special flag */
6377c478bd9Sstevel@tonic-gate         }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate         pArgString++;               /* look past the / or - */
6407c478bd9Sstevel@tonic-gate     }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate     if (':' == *pArgString) {       /* is it a colon? */
6437c478bd9Sstevel@tonic-gate         /*---------------------------------------------------------------------
6447c478bd9Sstevel@tonic-gate          * Rare case: if opterr is non-zero, return a question mark;
6457c478bd9Sstevel@tonic-gate          * otherwise, just return the colon we're on.
6467c478bd9Sstevel@tonic-gate          *-------------------------------------------------------------------*/
6477c478bd9Sstevel@tonic-gate         return (opterr ? (int)'?' : (int)':');
6487c478bd9Sstevel@tonic-gate     }
6497c478bd9Sstevel@tonic-gate     else if ((pOptString = strchr(opstring, *pArgString)) == 0) {
6507c478bd9Sstevel@tonic-gate         /*---------------------------------------------------------------------
6517c478bd9Sstevel@tonic-gate          * The letter on the command-line wasn't any good.
6527c478bd9Sstevel@tonic-gate          *-------------------------------------------------------------------*/
6537c478bd9Sstevel@tonic-gate         optarg = NULL;              /* no argument follows the option */
6547c478bd9Sstevel@tonic-gate         pIndexPosition = NULL;      /* not in the middle of anything */
6557c478bd9Sstevel@tonic-gate         return (opterr ? (int)'?' : (int)*pArgString);
6567c478bd9Sstevel@tonic-gate     }
6577c478bd9Sstevel@tonic-gate     else {
6587c478bd9Sstevel@tonic-gate         /*---------------------------------------------------------------------
6597c478bd9Sstevel@tonic-gate          * The letter on the command-line matches one we expect to see
6607c478bd9Sstevel@tonic-gate          *-------------------------------------------------------------------*/
6617c478bd9Sstevel@tonic-gate         if (':' == _next_char(pOptString)) { /* is the next letter a colon? */
6627c478bd9Sstevel@tonic-gate             /* It is a colon.  Look for an argument string. */
6637c478bd9Sstevel@tonic-gate             if ('\0' != _next_char(pArgString)) {  /* argument in this argv? */
6647c478bd9Sstevel@tonic-gate                 optarg = &pArgString[1];   /* Yes, it is */
6657c478bd9Sstevel@tonic-gate             }
6667c478bd9Sstevel@tonic-gate             else {
6677c478bd9Sstevel@tonic-gate                 /*-------------------------------------------------------------
6687c478bd9Sstevel@tonic-gate                  * The argument string must be in the next argv.
6697c478bd9Sstevel@tonic-gate                  * But, what if there is none (bad input from the user)?
6707c478bd9Sstevel@tonic-gate                  * In that case, return the letter, and optarg as NULL.
6717c478bd9Sstevel@tonic-gate                  *-----------------------------------------------------------*/
6727c478bd9Sstevel@tonic-gate                 if (optind < argc)
6737c478bd9Sstevel@tonic-gate                     optarg = argv[optind++];
6747c478bd9Sstevel@tonic-gate                 else {
6757c478bd9Sstevel@tonic-gate                     optarg = NULL;
6767c478bd9Sstevel@tonic-gate                     return (opterr ? (int)'?' : (int)*pArgString);
6777c478bd9Sstevel@tonic-gate                 }
6787c478bd9Sstevel@tonic-gate             }
6797c478bd9Sstevel@tonic-gate             pIndexPosition = NULL;  /* not in the middle of anything */
6807c478bd9Sstevel@tonic-gate         }
6817c478bd9Sstevel@tonic-gate         else {
6827c478bd9Sstevel@tonic-gate             /* it's not a colon, so just return the letter */
6837c478bd9Sstevel@tonic-gate             optarg = NULL;          /* no argument follows the option */
6847c478bd9Sstevel@tonic-gate             pIndexPosition = pArgString;    /* point to the letter we're on */
6857c478bd9Sstevel@tonic-gate         }
6867c478bd9Sstevel@tonic-gate         return (int)*pArgString;    /* return the letter that matched */
6877c478bd9Sstevel@tonic-gate     }
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate #ifndef PASSWORD_MAX
6917c478bd9Sstevel@tonic-gate #  define PASSWORD_MAX 255
6927c478bd9Sstevel@tonic-gate #endif
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate #include <conio.h>
6957c478bd9Sstevel@tonic-gate char *
getpass(prompt)6967c478bd9Sstevel@tonic-gate getpass(prompt)
6977c478bd9Sstevel@tonic-gate const char *prompt;
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	register char *p;
7007c478bd9Sstevel@tonic-gate 	register c;
7017c478bd9Sstevel@tonic-gate 	static char pbuf[PASSWORD_MAX];
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s", prompt); (void) fflush(stderr);
7047c478bd9Sstevel@tonic-gate 	for (p=pbuf; (c = _getch())!=13 && c!=EOF;) {
7057c478bd9Sstevel@tonic-gate 		if (p < &pbuf[sizeof(pbuf)-1])
7067c478bd9Sstevel@tonic-gate 			*p++ = c;
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 	*p = '\0';
7097c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n"); (void) fflush(stderr);
7107c478bd9Sstevel@tonic-gate 	return(pbuf);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate #endif /* WIN32 */
7167c478bd9Sstevel@tonic-gate #endif /* !_SUN_SDK_ */
717