1 /* @(#)bcrypt.c	1.17 09/07/11 Copyright 1988-2009 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)bcrypt.c	1.17 09/07/11 Copyright 1988-2009 J. Schilling";
6 #endif
7 /*
8  *	Copyright (c) 1988-2009 J. Schilling
9  */
10 /*
11  * The contents of this file are subject to the terms of the
12  * Common Development and Distribution License, Version 1.0 only
13  * (the "License").  You may not use this file except in compliance
14  * with the License.
15  *
16  * See the file CDDL.Schily.txt in this distribution for details.
17  * A copy of the CDDL is also available via the Internet at
18  * http://www.opensource.org/licenses/cddl1.txt
19  *
20  * When distributing Covered Code, include this CDDL HEADER in each
21  * file and include the License file CDDL.Schily.txt from this distribution.
22  */
23 
24 #include <schily/mconfig.h>
25 #include <schily/standard.h>
26 #include <schily/unistd.h>
27 #include <schily/stdlib.h>
28 #include <schily/string.h>
29 #include "fmt.h"
30 #include <schily/schily.h>
31 #include <schily/libport.h>
32 
33 typedef	unsigned long	Ulong;
34 
35 EXPORT	char	*getnenv	 __PR((const char *, int));
36 EXPORT	Ulong	my_gethostid	__PR((void));
37 EXPORT	BOOL	bsecurity	__PR((int));
38 EXPORT	Ulong	bcrypt		__PR((Ulong));
39 EXPORT	char	*bmap		__PR((Ulong));
40 EXPORT	Ulong	bunmap		__PR((const char *));
41 
42 
43 
44 /*---------------------------------------------------------------------------
45 |
46 | Get n'th value from colon separated list in environment
47 |
48 +---------------------------------------------------------------------------*/
49 
50 EXPORT char *
getnenv(name,idx)51 getnenv(name, idx)
52 	const char	*name;
53 	int		idx;
54 {
55 	static	char rbuf[10];
56 	char	*ep = getenv(name);
57 	char	*xp;
58 	int	i = 0;
59 
60 	if (!ep)
61 		return (NULL);
62 
63 	while (i++ < idx) {
64 		if ((xp = strchr(ep, ':')) != NULL)
65 			ep = &xp[1];
66 		else
67 			return (NULL);
68 	}
69 
70 	strncpy(rbuf, ep, sizeof (rbuf));
71 	rbuf[sizeof (rbuf)-1] = '\0';
72 
73 	if ((xp = strchr(rbuf, ':')) != NULL)
74 		*xp = 0;
75 	return (rbuf);
76 }
77 
78 EXPORT Ulong
my_gethostid()79 my_gethostid()
80 {
81 	Ulong	id;
82 
83 	id = gethostid();
84 	return (id);
85 }
86 
87 EXPORT BOOL
bsecurity(idx)88 bsecurity(idx)
89 	int	idx;
90 {
91 	Ulong	id;
92 	char	*sp;
93 
94 	id = my_gethostid();
95 	sp = getnenv("SFORMAT_SECURITY", idx);
96 	while (idx-- >= 0)
97 		id = bcrypt(id);
98 	if (!sp || id != bunmap(sp))
99 		return (FALSE);
100 	return (TRUE);
101 }
102 
103 
104 EXPORT Ulong
bcrypt(i)105 bcrypt(i)
106 	Ulong	i;
107 {
108 	register Ulong	k;
109 	register Ulong	erg;
110 
111 	k = i + 19991;
112 	erg = 0;
113 	do {
114 		erg += 1 + k / 19;
115 		erg *= 1 + k % 19;
116 		k /= 11;
117 	} while (k != 0);
118 	return (erg);
119 }
120 
121 
122 /*---------------------------------------------------------------------------
123 |
124 | Convert unsigned long to string similar to l64a()
125 |
126 +---------------------------------------------------------------------------*/
127 
128 EXPORT char *
bmap(i)129 bmap(i)
130 	register Ulong	i;
131 {
132 	register int	c;
133 	static	char	buf[8];
134 	register char	*bp;
135 
136 	bp = &buf[7];
137 	*bp = '\0';
138 	do {
139 		c = i % 64;
140 		i /= 64;
141 		c += '.';
142 		if (c > '9')
143 			c += 7;
144 		if (c > 'Z')
145 			c += 6;
146 		*--bp = c;
147 	} while (i);
148 	return (bp);
149 }
150 
151 
152 /*---------------------------------------------------------------------------
153 |
154 | Convert string to unsigned long similar to a64l()
155 |
156 +---------------------------------------------------------------------------*/
157 
158 EXPORT Ulong
bunmap(s)159 bunmap(s)
160 	register const char	*s;
161 {
162 	register Ulong	l;
163 	register int	c;
164 
165 	l = 0L;
166 	while (*s) {
167 		c = *s++;
168 		if (c > 'Z')
169 			c -= 6;
170 		if (c > '9')
171 			c -= 7;
172 		c -= '.';
173 		l *= 64;
174 		l += c;
175 	}
176 	return (l);
177 }
178