]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/nextid.c
Import nextid safety checks from -devel.
[openldap] / servers / slapd / back-ldbm / nextid.c
1 /* id.c - keep track of the next id to be given out */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
11 #endif
12
13 #include "slap.h"
14 #include "back-ldbm.h"
15
16 ID
17 next_id( Backend *be )
18 {
19         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
20         char            buf[MAXPATHLEN];
21         char            buf2[20];
22         FILE            *fp;
23         ID              id;
24
25         sprintf( buf, "%s/NEXTID", li->li_directory );
26
27         pthread_mutex_lock( &li->li_nextid_mutex );
28
29         /* first time in here since startup - try to read the nexid */
30         if ( li->li_nextid == -1 ) {
31                 if ( (fp = fopen( buf, "r" )) == NULL ) {
32                         Debug( LDAP_DEBUG_ANY,
33                             "next_id %lu: could not open \"%s\"\n",
34                             li->li_nextid, buf, 0 );
35                         li->li_nextid = 1;
36
37                 } else {
38                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
39                                 li->li_nextid = atol( buf2 );
40
41                                 if(li->li_nextid < 1) {
42                                         /* protect against bad data */
43                                         Debug( LDAP_DEBUG_ANY,
44                                         "next_id %lu: atol(%s) return non-positive integer\n",
45                                                 li->li_nextid, buf2, 0 );
46                                         li->li_nextid = 1;
47                                 }
48
49                         } else {
50                                 Debug( LDAP_DEBUG_ANY,
51                            "next_id %lu: could not fgets nextid from \"%s\"\n",
52                                     li->li_nextid, buf2, 0 );
53                                 li->li_nextid = 1;
54                         }
55
56                         fclose( fp );
57                 }
58         }
59
60         id = li->li_nextid++;
61
62         if ( (fp = fopen( buf, "w" )) == NULL ) {
63                 Debug( LDAP_DEBUG_ANY, "next_id %lu: could not open \"%s\"\n",
64                     li->li_nextid, buf, 0 );
65         } else {
66                 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
67                         Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fprintf\n",
68                             li->li_nextid, 0, 0 );
69                 }
70                 if( fclose( fp ) != 0 ) {
71                         Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
72                             li->li_nextid, 0, 0 );
73                 }
74         }
75
76         pthread_mutex_unlock( &li->li_nextid_mutex );
77         return( id );
78 }
79
80 void
81 next_id_return( Backend *be, ID id )
82 {
83         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
84         char            buf[MAXPATHLEN];
85         FILE            *fp;
86
87         pthread_mutex_lock( &li->li_nextid_mutex );
88
89         if ( id != li->li_nextid - 1 ) {
90                 pthread_mutex_unlock( &li->li_nextid_mutex );
91                 return;
92         }
93
94         sprintf( buf, "%s/NEXTID", li->li_directory );
95
96         li->li_nextid--;
97         if ( (fp = fopen( buf, "w" )) == NULL ) {
98                 Debug( LDAP_DEBUG_ANY,
99                   "next_id_return of %lu: could not open \"%s\" next id %lu\n",
100                     id, buf, li->li_nextid );
101         } else {
102                 if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
103                         Debug( LDAP_DEBUG_ANY,
104                   "next_id_return of %lu: cannot fprintf \"%s\" next id %lu\n",
105                             id, buf, li->li_nextid );
106                 }
107                 if( fclose( fp ) != 0 ) {
108                         Debug( LDAP_DEBUG_ANY,
109                   "next_id_return of %lu: cannot fclose \"%s\" next id %lu\n",
110                             id, buf, li->li_nextid );
111                 }
112         }
113         pthread_mutex_unlock( &li->li_nextid_mutex );
114 }
115
116 ID
117 next_id_get( Backend *be )
118 {
119         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
120         char            buf[MAXPATHLEN];
121         char            buf2[20];
122         FILE            *fp;
123         ID              id;
124
125         sprintf( buf, "%s/NEXTID", li->li_directory );
126
127         pthread_mutex_lock( &li->li_nextid_mutex );
128
129         /* first time in here since startup - try to read the nexid */
130         if ( li->li_nextid == -1 ) {
131                 if ( (fp = fopen( buf, "r" )) == NULL ) {
132                         Debug( LDAP_DEBUG_ANY,
133                             "next_id_get %lu: could not open \"%s\"\n",
134                             li->li_nextid, buf, 0 );
135                         li->li_nextid = 1;
136
137                 } else {
138                         if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
139                                 li->li_nextid = atol( buf2 );
140
141                                 if(li->li_nextid < 1) {
142                                         /* protect against bad data */
143                                         Debug( LDAP_DEBUG_ANY,
144                                         "next_id_get %lu: atol(%s) return non-positive integer\n",
145                                                 li->li_nextid, buf2, 0 );
146                                         li->li_nextid = 1;
147                                 }
148
149                         } else {
150                                 Debug( LDAP_DEBUG_ANY,
151                             "next_id_get %lu: cannot fgets nextid from \"%s\"\n",
152                                     li->li_nextid, buf2, 0 );
153                                 li->li_nextid = 1;
154                         }
155                         fclose( fp );
156                 }
157         }
158
159         id = li->li_nextid;
160
161         pthread_mutex_unlock( &li->li_nextid_mutex );
162
163         return( id );
164 }