| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <time.h> |
| 5 | #include <stdlib.h> |
| 6 | #if defined( unix ) || defined(WIN32 ) |
| 7 | #include <unistd.h> |
| 8 | #elif defined( VMS ) |
| 9 | #include <unixio.h> |
| 10 | #endif |
| 11 | #include <string.h> |
| 12 | #include <errno.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <fcntl.h> |
| 16 | #include "types.h" |
| 17 | #include "lock.h" |
| 18 | |
| 19 | #define BUILD_DLL |
| 20 | #include "sdb.h" |
| 21 | |
| 22 | #include "utct.h" |
| 23 | |
| 24 | #if defined( VMS ) |
| 25 | #define unlink(x) delete(x) |
| 26 | #endif |
| 27 | |
| 28 | #if defined( WIN32 ) |
| 29 | #define ftruncate chsize |
| 30 | #endif |
| 31 | |
| 32 | #define MAX_UTC 13 |
| 33 | |
| 34 | /* simple though inefficient implementation of a database function */ |
| 35 | |
| 36 | static int sdb_insert( DB*, const char* key, const char* val, int* err ); |
| 37 | |
| 38 | int sdb_open( DB* h, const char* filename, int* err ) |
| 39 | { |
| 40 | int fd = 0 ; |
| 41 | FILE* fp = NULL ; |
| 42 | |
| 43 | *err = 0; |
| 44 | h->file = NULL; |
| 45 | if ( filename == NULL ) { return 0; } |
| 46 | fd = open( filename, O_RDWR | O_CREAT, S_IREAD | S_IWRITE ); |
| 47 | if ( fd == -1 ) { goto fail; } |
| 48 | fp = fdopen( fd, "w+" ); |
| 49 | if ( fp == NULL ) { goto fail; } |
| 50 | h->file = fp; |
| 51 | if ( !lock_write( h->file ) ) { goto fail; } |
| 52 | strncpy( h->filename, filename, PATH_MAX ); h->filename[PATH_MAX] = '\0'; |
| 53 | h->write_pos = 0; |
| 54 | return 1; |
| 55 | fail: |
| 56 | *err = errno; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int sdb_close( DB* h, int* err ) |
| 61 | { |
| 62 | if ( h == NULL || h->file == NULL ) { return 0; } |
| 63 | if ( fclose( h->file ) == EOF ) { *err = errno; return 0; } |
| 64 | *err = 0; return 1; |
| 65 | } |
| 66 | |
| 67 | int sdb_add( DB* h, const char* key, const char* val, int* err ) |
| 68 | { |
| 69 | *err = 0; |
| 70 | |
| 71 | if ( h == NULL || h->file == NULL ) { return 0; } |
| 72 | if ( strlen( key ) > MAX_KEY ) { return 0; } |
| 73 | if ( strlen( val ) > MAX_VAL ) { return 0; } |
| 74 | if ( fseek( h->file, 0, SEEK_END ) == -1 ) { goto fail; } |
| 75 | if ( fprintf( h->file, "%s %s\n", key, val ) == 0 ) { goto fail; } |
| 76 | return 1; |
| 77 | fail: |
| 78 | *err = errno; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | #define MAX_LINE (MAX_KEY+MAX_VAL) |
| 83 | |
| 84 | static void sdb_rewind( DB* h ) |
| 85 | { |
| 86 | rewind( h->file ); |
| 87 | h->write_pos = 0; |
| 88 | } |
| 89 | |
| 90 | int sdb_findfirst( DB* h, char* key, int klen, char* val, int vlen, int* err ) |
| 91 | { |
| 92 | sdb_rewind( h ); |
| 93 | return sdb_findnext( h, key, klen, val, vlen, err ); |
| 94 | } |
| 95 | |
| 96 | int sdb_findnext( DB* h, char* key, int klen, char* val, int vlen, |
| 97 | int* err ) |
| 98 | { |
| 99 | char line[MAX_LINE+1] = {0}; |
| 100 | int line_len = 0 ; |
| 101 | char* fkey = line; |
| 102 | char* fval = NULL ; |
| 103 | |
| 104 | *err = 0; |
| 105 | if ( h->file == NULL ) { return 0; } |
| 106 | if ( feof( h->file ) ) { return 0; } |
| 107 | if ( fgets( line, MAX_LINE, h->file ) == NULL ) { return 0; } |
| 108 | line_len = strlen( line ); |
| 109 | if ( line_len == 0 ) { return 0; } |
| 110 | |
| 111 | /* remove unix, DOS, and MAC linefeeds */ |
| 112 | |
| 113 | if ( line[line_len-1] == '\n' ) { line[--line_len] = '\0'; } |
| 114 | if ( line[line_len-1] == '\r' ) { line[--line_len] = '\0'; } |
| 115 | if ( line[line_len-1] == '\n' ) { line[--line_len] = '\0'; } |
| 116 | |
| 117 | fval = strchr( line, ' ' ); |
| 118 | if ( fval != NULL ) { *fval = '\0'; fval++; } |
| 119 | else { fval = ""; } /* empty */ |
| 120 | strncpy( key, fkey, klen ); key[klen] = '\0'; |
| 121 | strncpy( val, fval, vlen ); val[vlen] = '\0'; |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | static int sdb_cb_notkeymatch( const char* key, const char* val, |
| 126 | void* arg, int* err ) |
| 127 | { |
| 128 | *err = 0; |
| 129 | if ( strncmp( key, arg, MAX_KEY ) != 0 ) { return 1; } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int sdb_cb_keymatch( const char* key, const char* val, |
| 134 | void* arg, int* err ) |
| 135 | { |
| 136 | *err = 0; |
| 137 | if ( strncmp( key, arg, MAX_KEY ) == 0 ) { return 1; } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int sdb_del( DB* h, const char* key, int* err ) |
| 142 | { |
| 143 | return sdb_updateiterate( h, (sdb_wcallback)sdb_cb_notkeymatch, |
| 144 | (void*)key, err ); |
| 145 | } |
| 146 | |
| 147 | int sdb_lookup( DB* h, const char* key, char* val, int vlen, int* err ) |
| 148 | { |
| 149 | char fkey[MAX_KEY+1] = {0}; |
| 150 | return sdb_callbacklookup( h, sdb_cb_keymatch, (void*)key, |
| 151 | fkey, MAX_KEY, val, vlen, err ); |
| 152 | } |
| 153 | |
| 154 | int sdb_lookupnext( DB* h, const char* key, char* val, int vlen, int* err ) |
| 155 | { |
| 156 | char fkey[MAX_KEY+1] = {0}; |
| 157 | return sdb_callbacklookupnext( h, sdb_cb_keymatch, (void*)key, |
| 158 | fkey, MAX_KEY, val, vlen, err ); |
| 159 | } |
| 160 | |
| 161 | int sdb_callbacklookup( DB* h, sdb_rcallback cb, void* arg, char* key, |
| 162 | int klen, char* val, int vlen, int* err ) |
| 163 | { |
| 164 | rewind( h->file ); |
| 165 | return sdb_callbacklookupnext( h, cb, arg, key, klen, val, vlen, err ); |
| 166 | } |
| 167 | |
| 168 | int sdb_callbacklookupnext( DB* h, sdb_rcallback cb, void* arg, char* key, |
| 169 | int klen, char* val, int vlen, int* err ) |
| 170 | { |
| 171 | char fkey[MAX_KEY+1] = {0}; |
| 172 | |
| 173 | while ( sdb_findnext( h, fkey, MAX_KEY, val, vlen, err ) ) { |
| 174 | if ( cb( fkey, val, arg, err ) ) { |
| 175 | strncpy( key, fkey, klen ); key[klen] = '\0'; |
| 176 | return 1; |
| 177 | } |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | int sdb_insert( DB* db, const char* key, const char* val, int* err ) |
| 183 | { |
| 184 | int res = 0 ; |
| 185 | *err = 0; |
| 186 | |
| 187 | res = ftell( db->file ); |
| 188 | if ( res < 0 ) { goto fail; } |
| 189 | db->read_pos = res; |
| 190 | |
| 191 | if ( fseek( db->file, db->write_pos, SEEK_SET ) == -1 ) { goto fail; } |
| 192 | if ( fprintf( db->file, "%s %s\n", key, val ) == 0 ) { goto fail; } |
| 193 | |
| 194 | res = ftell( db->file ); |
| 195 | if ( res < 0 ) { goto fail; } |
| 196 | db->write_pos = res; |
| 197 | |
| 198 | if ( fseek( db->file, db->read_pos, SEEK_SET ) == -1 ) { goto fail; } |
| 199 | return 1; |
| 200 | fail: |
| 201 | *err = errno; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int sdb_updateiterate( DB* h, sdb_wcallback cb, void* arg, int* err ) |
| 206 | { |
| 207 | int found = 0, res = 0; |
| 208 | char fkey[MAX_KEY+1] = {0}; |
| 209 | char fval[MAX_VAL+1] = {0}; |
| 210 | |
| 211 | for ( found = sdb_findfirst( h, fkey, MAX_KEY, fval, MAX_VAL, err ); |
| 212 | found; |
| 213 | found = sdb_findnext( h, fkey, MAX_KEY, fval, MAX_VAL, err ) ) { |
| 214 | if ( cb( fkey, fval, arg, err ) ) { |
| 215 | if ( *err ) { goto fail; } |
| 216 | if ( !sdb_insert( h, fkey, fval, err ) ) { goto fail; } |
| 217 | } |
| 218 | else if ( *err ) { goto fail; } |
| 219 | } |
| 220 | |
| 221 | res = ftruncate( fileno( h->file ), h->write_pos ); |
| 222 | return 1; |
| 223 | fail: |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* higher level functions */ |
| 228 | |
| 229 | int hashcash_db_open( DB* db, const char* db_filename, int* err ) { |
| 230 | int my_err; |
| 231 | |
| 232 | if ( !err ) { err = &my_err; } |
| 233 | if ( !sdb_open( db, db_filename, err ) ) { return 0; } |
| 234 | fgetc( db->file ); /* try read to trigger EOF */ |
| 235 | if ( feof( db->file ) ) { |
| 236 | if ( !sdb_add( db, PURGED_KEY, "700101000000", err ) ) { |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | rewind( db->file ); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int hashcash_db_in( DB* db, char* token, char *period, int* err ) { |
| 245 | int in_db = 0; |
| 246 | int my_err; |
| 247 | |
| 248 | if ( !err ) { err = &my_err; } |
| 249 | *err = 0; |
| 250 | |
| 251 | in_db = sdb_lookup( db, token, period, MAX_UTC, err ); |
| 252 | if ( *err ) { return 0; } |
| 253 | return in_db; |
| 254 | } |
| 255 | |
| 256 | int hashcash_db_add( DB* db, char* token, char *period, int* err ) { |
| 257 | if ( !sdb_add( db, token, period, err ) ) { |
| 258 | return 0; |
| 259 | } |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | int hashcash_db_close( DB* db, int* err ) { |
| 264 | if ( !sdb_close( db, err ) ) { return 0; } |
| 265 | return 1; |
| 266 | } |