]> git.sur5r.net Git - openldap/blob - libraries/liblutil/uuid.c
0775edf00a40a59986b8e84bc444763247631835
[openldap] / libraries / liblutil / uuid.c
1 /*
2  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* Portions
6  * Copyright 2000, John E. Schimmel, All rights reserved.
7  * This software is not subject to any license of Mirapoint, Inc.
8  *
9  * This is free software; you can redistribute and use it
10  * under the same terms as OpenLDAP itself.
11  */
12 /* Adapted for incorporatation into OpenLDAP by Kurt Zeilenga */
13
14 /*
15  * Sorry this file is so scary, but it needs to run on a wide range of
16  * platforms.  The only exported routine is lutil_uuidstr() which is all
17  * that LDAP cares about.  It generates a new uuid and returns it in
18  * in string form.
19  */
20 #include "portable.h"
21
22 #include <stdio.h>
23 #include <sys/types.h>
24
25 #ifdef HAVE_SYS_UUID_H
26 #  include <sys/uuid.h>
27 #else
28 #  include <ac/socket.h>
29 #  include <ac/time.h>
30 #  include <net/if.h>
31 #  ifdef HAVE_SYS_SYSCTL_H
32 #    include <sys/sysctl.h>
33 #    include <net/route.h>
34 #  endif
35 #endif
36
37 #include <lutil.h>
38
39 /* 100 usec intervals from 10/10/1582 to 1/1/1970 */
40 #define UUID_TPLUS      0x01B21DD2138140LL
41
42 #ifndef HAVE_SYS_UUID_H
43 static unsigned char *
44 lutil_eaddr( void )
45 {
46         static unsigned char zero[6];
47         static unsigned char eaddr[6];
48
49 #ifdef HAVE_SYS_SYSCTL_H
50         size_t needed;
51         int mib[6];
52         char *buf, *next, *lim;
53         struct if_msghdr *ifm;
54         struct sockaddr_dl *sdl;
55
56         if (memcmp(eaddr, zero, sizeof(eaddr))) {
57                 return eaddr;
58         }
59
60         mib[0] = CTL_NET;
61         mib[1] = PF_ROUTE;
62         mib[3] = 0;
63         mib[3] = 0;
64         mib[4] = NET_RT_IFLIST;
65         mib[5] = 0;
66
67         if (sysctl(mib, sizeof(mib), NULL, &needed, NULL, 0) < 0) {
68                 return NULL;
69         }
70
71         buf = malloc(needed);
72         if( buf == NULL ) return NULL;
73
74         if (sysctl(mib, sizeof(mib), buf, &needed, NULL, 0) < 0) {
75                 free(buf);
76                 return NULL;
77         }
78
79         lim = buf + needed;
80         for (next = buf; next < lim; next += ifm->ifm_msglen) {
81                 ifm = (struct if_msghdr *)next;
82                 sdl = (struct sockaddr_dl *)(ifm + 1);
83
84                 if ( sdl->sdl_family != AF_LINK || sdl->sdl_alen == 6 ) {
85                         AC_MEMCPY(eaddr,
86                                 (unsigned char *)sdl->sdl_data + sdl->sdl_nlen,
87                                 sizeof(eaddr));
88                         free(buf);
89                         return eaddr;
90                 }
91         }
92
93         free(buf);
94         return NULL;
95
96 #elif defined (SIOCGIFADDR)
97         char buf[sizeof(struct ifreq) * 32];
98         struct ifconf ifc;
99         struct ifreq *ifr;
100         struct sockaddr *sa;
101         struct sockaddr_dl *sdl;
102         unsigned char *p;
103         int s, i;
104
105         if (memcmp(eaddr, zero, sizeof(eaddr))) {
106                 return eaddr;
107         }
108
109         s = socket( AF_INET, SOCK_DGRAM, 0 );
110         if ( s < 0 ) {
111                 return NULL;
112         }
113
114         ifc.ifc_len = sizeof( buf );
115         ifc.ifc_buf = buf;
116         memset( buf, 0, sizeof( buf ) );
117
118         i = ioctl( s, SIOCGIFCONF, (char *)&ifc );
119         close( s );
120
121         if( i < 0 ) {
122                 return NULL;
123         }
124
125         for ( i = 0; i < ifc.ifc_len; ) {
126                 ifr = (struct ifreq *)&ifc.ifc_buf[i];
127                 sa = &ifr->ifr_addr;
128
129                 if ( sa->sa_len > sizeof( ifr->ifr_addr ) ) {
130                         i += sizeof( ifr->ifr_name ) + sa->sa_len;
131                 } else {
132                         i += sizeof( *ifr );
133                 }
134
135                 if ( sa->sa_family != AF_LINK ) {
136                         continue;
137                 }
138
139                 sdl = (struct sockaddr_dl *)sa;
140
141                 if ( sdl->sdl_alen == 6 ) {
142                         AC_MEMCPY(eaddr,
143                                 (unsigned char *)sdl->sdl_data + sdl->sdl_nlen,
144                                 sizeof(eaddr));
145                         return eaddr;
146                 }
147         }
148
149         return NULL;
150
151 #else
152         if (memcmp(eaddr, zero, sizeof(eaddr)) == 0) {
153                 /* XXX - who knows? */
154                 lutil_entropy( eaddr, sizeof(eaddr) );
155                 eaddr[0] |= 0x80; /* turn it into a mutlicast address */
156         }
157
158         return eaddr;
159 #endif
160 }
161 #endif
162
163 /*
164 ** All we really care about is an ISO UUID string.  The format of a UUID is:
165 **      field                   octet           note
166 **      time_low                0-3             low field of the timestamp
167 **      time_mid                4-5             middle field of timestamp
168 **      time_hi_and_version     6-7             high field of timestamp and
169 **                                              version number
170 **      clock_seq_hi_and_resv   8               high field of clock sequence
171 **                                              and variant
172 **      clock_seq_low           9               low field of clock sequence
173 **      node                    10-15           spacially unique identifier
174 **
175 ** We use DCE version one, and the DCE variant.  Our unique identifier is
176 ** the first ethernet address on the system.
177 */
178 int
179 lutil_uuidstr( char *buf, size_t len )
180 {
181 #ifdef HAVE_SYS_UUID_H
182         uuid_t uu = {0};
183         unsigned rc;
184         char *s;
185         size_t l;
186
187         uuid_create( &uu, &rc );
188         if ( rc != uuid_s_ok ) {
189                 return 0;
190         }
191
192         uuid_to_str( &uu, &s, &rc );
193         if ( rc != uuid_s_ok ) {
194                 return 0;
195         }
196
197         l = strlen( s );
198         if ( l >= len ) {
199                 free( s );
200                 return 0;
201         }
202
203         strncpy( buf, s, l );
204         free( s );
205
206         return l;
207
208 #else
209         struct timeval tv;
210         unsigned long long tl;
211         unsigned char *nl;
212         unsigned short t2, t3, s1, n1, n2, n3;
213         unsigned int t1;
214
215         /*
216          * Theoretically we should delay if seq wraps within 100usec but for now
217          * systems are not fast enough to worry about it.
218          */
219         static int inited = 0;
220         static unsigned short seq;
221         
222         if (!inited) {
223                 lutil_entropy( (unsigned char *) &seq, sizeof(seq) );
224                 inited++;
225         }
226
227 #ifdef HAVE_GETTIMEOFDAY
228         gettimeofday( &tv, 0 );
229 #else
230         time( &tv.tv_sec );
231         tv.tv_usec = 0;
232 #endif
233
234         tl = ( tv.tv_sec * 10000000LL ) + ( tv.tv_usec * 10LL ) + UUID_TPLUS;
235         nl = lutil_eaddr();
236
237         t1 = tl & 0xffffffff;                                   /* time_low */
238         t2 = ( tl >> 32 ) & 0xffff;                             /* time_mid */
239         t3 = ( ( tl >> 48 ) & 0x0fff ) | 0x1000;        /* time_hi_and_version */
240         s1 = ( ++seq & 0x1fff ) | 0x8000;               /* clock_seq_and_reserved */
241
242         t1 = snprintf( buf, len,
243                 "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
244             t1, (unsigned) t2, (unsigned) t3, (unsigned) s1,
245                 (unsigned) nl[0], (unsigned) nl[1],
246                 (unsigned) nl[2], (unsigned) nl[3],
247                 (unsigned) nl[4], (unsigned) nl[5] );
248
249         return (t1 < len) ? t1 : 0;
250 #endif
251 }
252
253 #ifdef TEST
254 int
255 main(int argc, char **argv)
256 {
257         char buf1[8], buf2[64];
258
259 #ifndef HAVE_SYS_UUID_H
260         unsigned char *p = lutil_eaddr();
261
262         if( p ) {
263                 printf( "Ethernet Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
264                         (unsigned) p[0], (unsigned) p[1], (unsigned) p[2],
265                         (unsigned) p[3], (unsigned) p[4], (unsigned) p[5]);
266         }
267 #endif
268
269         if ( lutil_uuidstr( buf1, sizeof( buf1 ) ) ) {
270                 printf( "UUID: %s\n", buf1 );
271         } else {
272                 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf1 ) );
273         }
274
275         if ( lutil_uuidstr( buf2, sizeof( buf2 ) ) ) {
276                 printf( "UUID: %s\n", buf2 );
277         } else {
278                 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf2 ) );
279         }
280
281         if ( lutil_uuidstr( buf2, sizeof( buf2 ) ) ) {
282                 printf( "UUID: %s\n", buf2 );
283         } else {
284                 fprintf( stderr, "too short: %ld\n", (long) sizeof( buf2 ) );
285         }
286
287         return 0;
288 }
289 #endif