static PyObject * dbm_setdefault(register dbmobject *dp, PyObject *args) { datum key, val; PyObject *defvalue = NULL; int tmp_size; if (!PyArg_ParseTuple(args, "s#|S:setdefault", &key.dptr, &tmp_size, &defvalue)) return NULL; key.dsize = tmp_size; check_dbmobject_open(dp); val = dbm_fetch(dp->di_dbm, key); if (val.dptr != NULL) return PyString_FromStringAndSize(val.dptr, val.dsize); if (defvalue == NULL) { defvalue = PyString_FromStringAndSize(NULL, 0); if (defvalue == NULL) return NULL; } else Py_INCREF(defvalue); val.dptr = PyString_AS_STRING(defvalue); val.dsize = PyString_GET_SIZE(defvalue); if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); return NULL; } return defvalue; }
static int dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) { datum krec, drec; int tmp_size; if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have string indices only"); return -1; } krec.dsize = tmp_size; if (dp->di_dbm == NULL) { PyErr_SetString(DbmError, "DBM object has already been closed"); return -1; } dp->di_size = -1; if (w == NULL) { if ( dbm_delete(dp->di_dbm, krec) < 0 ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(PyExc_KeyError, PyString_AS_STRING((PyStringObject *)v)); return -1; } } else { if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have string elements only"); return -1; } drec.dsize = tmp_size; if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); return -1; } } if ( dbm_error(dp->di_dbm) ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, ""); return -1; } return 0; }
static apr_status_t set_error(apr_dbm_t *dbm, apr_status_t dbm_said) { apr_status_t rv = APR_SUCCESS; /* ### ignore whatever the DBM said (dbm_said); ask it explicitly */ dbm->errmsg = NULL; if (dbm_error((DBM*)dbm->file)) { dbm->errmsg = NULL; rv = APR_EGENERAL; /* ### need something better */ } /* captured it. clear it now. */ dbm_clearerr((DBM*)dbm->file); return rv; }
static PyObject * dbm_setdefault(dbmobject *dp, PyObject *args) { datum key, val; PyObject *defvalue = NULL; char *tmp_ptr; Py_ssize_t tmp_size; if (!PyArg_ParseTuple(args, "s#|O:setdefault", &tmp_ptr, &tmp_size, &defvalue)) return NULL; key.dptr = tmp_ptr; key.dsize = tmp_size; check_dbmobject_open(dp); val = dbm_fetch(dp->di_dbm, key); if (val.dptr != NULL) return PyBytes_FromStringAndSize(val.dptr, val.dsize); if (defvalue == NULL) { defvalue = PyBytes_FromStringAndSize(NULL, 0); if (defvalue == NULL) return NULL; val.dptr = NULL; val.dsize = 0; } else { if ( !PyArg_Parse(defvalue, "s#", &val.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have byte string elements only"); return NULL; } val.dsize = tmp_size; Py_INCREF(defvalue); } if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); Py_DECREF(defvalue); return NULL; } return defvalue; }
static PyObject * _dbm_dbm_setdefault_impl(dbmobject *self, const char *key, Py_ssize_clean_t key_length, PyObject *default_value) /*[clinic end generated code: output=52545886cf272161 input=6a3b99ae91f20203]*/ { datum dbm_key, val; Py_ssize_t tmp_size; dbm_key.dptr = (char *)key; dbm_key.dsize = key_length; check_dbmobject_open(self); val = dbm_fetch(self->di_dbm, dbm_key); if (val.dptr != NULL) return PyBytes_FromStringAndSize(val.dptr, val.dsize); if (default_value == NULL) { default_value = PyBytes_FromStringAndSize(NULL, 0); if (default_value == NULL) return NULL; val.dptr = NULL; val.dsize = 0; } else { if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have byte string elements only"); return NULL; } val.dsize = tmp_size; Py_INCREF(default_value); } if (dbm_store(self->di_dbm, dbm_key, val, DBM_INSERT) < 0) { dbm_clearerr(self->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); Py_DECREF(default_value); return NULL; } return default_value; }
static PyObject * dbm_subscript(dbmobject *dp, PyObject *key) { datum drec, krec; Py_ssize_t tmp_size; if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) ) return NULL; krec.dsize = tmp_size; check_dbmobject_open(dp); drec = dbm_fetch(dp->di_dbm, krec); if ( drec.dptr == 0 ) { PyErr_SetObject(PyExc_KeyError, key); return NULL; } if ( dbm_error(dp->di_dbm) ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, ""); return NULL; } return PyBytes_FromStringAndSize(drec.dptr, drec.dsize); }
int main(int argc, char **argv) { typedef enum { YOW, FETCH, STORE, DELETE, SCAN, REGEXP } commands; char opt; int flags; int giveusage = 0; int verbose = 0; commands what = YOW; char *comarg[3]; int st_flag = DBM_INSERT; int argn; DBM *db; datum key; datum content; flags = O_RDWR; argn = 0; while ((opt = getopt(argc, argv, "acdfFm:rstvx")) != ':') { switch (opt) { case 'a': what = SCAN; break; case 'c': flags |= O_CREAT; break; case 'd': what = DELETE; break; case 'f': what = FETCH; break; case 'F': what = REGEXP; break; case 'm': flags &= ~(000007); if (strcmp(optarg, "r") == 0) flags |= O_RDONLY; else if (strcmp(optarg, "w") == 0) flags |= O_WRONLY; else if (strcmp(optarg, "rw") == 0) flags |= O_RDWR; else { fprintf(stderr, "Invalid mode: \"%s\"\n", optarg); giveusage = 1; } break; case 'r': st_flag = DBM_REPLACE; break; case 's': what = STORE; break; case 't': flags |= O_TRUNC; break; case 'v': verbose = 1; break; case 'x': flags |= O_EXCL; break; case '!': giveusage = 1; break; case '?': if (argn < 3) comarg[argn++] = optarg; else { fprintf(stderr, "Too many arguments.\n"); giveusage = 1; } break; } } if (giveusage || what == YOW || argn < 1) { fprintf(stderr, "Usage: %s database [-m r|w|rw] [-crtx] -a|-d|-f|-F|-s [key [content]]\n", argv[0]); exit(-1); } if ((db = dbm_open(comarg[0], flags, 0777)) == NULL) { fprintf(stderr, "Error opening database \"%s\"\n", comarg[0]); exit(-1); } if (argn > 1) key = read_datum(comarg[1]); if (argn > 2) content = read_datum(comarg[2]); switch (what) { case SCAN: key = dbm_firstkey(db); if (dbm_error(db)) { fprintf(stderr, "Error when fetching first key\n"); goto db_exit; } while (key.dptr != NULL) { content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); if (dbm_error(db)) { fprintf(stderr, "Error when fetching next key\n"); goto db_exit; } key = dbm_nextkey(db); } break; case REGEXP: if (argn < 2) { fprintf(stderr, "Missing regular expression.\n"); goto db_exit; } if (re_comp(comarg[1])) { fprintf(stderr, "Invalid regular expression\n"); goto db_exit; } key = dbm_firstkey(db); if (dbm_error(db)) { fprintf(stderr, "Error when fetching first key\n"); goto db_exit; } while (key.dptr != NULL) { if (re_exec(key2s(key))) { content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); if (dbm_error(db)) { fprintf(stderr, "Error when fetching next key\n"); goto db_exit; } } key = dbm_nextkey(db); } break; case FETCH: if (argn < 2) { fprintf(stderr, "Missing fetch key.\n"); goto db_exit; } content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } if (content.dptr == NULL) { fprintf(stderr, "Cannot find "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); break; case DELETE: if (argn < 2) { fprintf(stderr, "Missing delete key.\n"); goto db_exit; } if (dbm_delete(db, key) || dbm_error(db)) { fprintf(stderr, "Error when deleting "); print_datum(key); printf("\n"); goto db_exit; } if (verbose) { print_datum(key); printf(": DELETED\n"); } break; case STORE: if (argn < 3) { fprintf(stderr, "Missing key and/or content.\n"); goto db_exit; } if (dbm_store(db, key, content, st_flag) || dbm_error(db)) { fprintf(stderr, "Error when storing "); print_datum(key); printf("\n"); goto db_exit; } if (verbose) { print_datum(key); printf(": "); print_datum(content); printf(" STORED\n"); } break; } db_exit: dbm_clearerr(db); dbm_close(db); if (dbm_error(db)) { fprintf(stderr, "Error closing database \"%s\"\n", comarg[0]); exit(-1); } }
/* * FUNCTION: update_map_from_dit() * * DESCRIPTION: Core code called to update an entire map. * Information is recovered from LDAP and used to build a duplicate * copy of the live maps. When this is complete the maps are * locked and then overwritten by the new copy. * * INPUTS: map_ctrl containing lots of information about the map and a * pointer to it's lock which will be required. * Flag indicating if progress logging is required. * * OUTPUTS: SUCCESS = Map updated * FAILURE = Map not updated */ suc_code update_map_from_dit(map_ctrl *map, bool_t log_flag) { __nis_table_mapping_t *t; __nis_rule_value_t *rv; __nis_ldap_search_t *ls; __nis_object_dn_t *objectDN = NULL; datum *datval, *datkey; int nr = 0, i, j, nv, numDNs; int statP = SUCCESS, flag; char *objname, **dn; /* Name of temporary entries DBM file */ char *temp_entries; /* Name of temporary TTL DBM file */ char *temp_ttl; /* Temporary DBM handles */ DBM *temp_entries_db; DBM *temp_ttl_db; map_ctrl temp_map; datum key; char *myself = "update_map_from_dit"; bool_t secure_flag; int entry_count = 1; int next_print = PRINT_FREQ; int search_flag = SUCCESS; int m; /* list of maps whose keys will be transliterated to lowercase */ char *xlate_to_lcase_maps[] = { "hosts.byname", "ipnodes.byname", NULL }; bool_t xlate_to_lcase = FALSE; if (!map || !map->map_name || !map->domain) { return (FAILURE); } __nisdb_get_tsd()->escapeFlag = '\0'; /* * netgroup.byxxx maps are a special case. They are regenerated from * the netgroup map, not the DIT, so handle special case. */ if ((0 == strcmp(map->map_name, NETGROUP_BYHOST)) || 0 == (strcmp(map->map_name, NETGROUP_BYUSER))) { return (update_netgroup_byxxx(map)); } /* Get the mapping information for the map */ if ((t = mappingFromMap(map->map_name, map->domain, &statP)) == 0) { if (statP == MAP_NO_MAPPING_EXISTS) logmsg(MSG_NOTIMECHECK, LOG_WARNING, "%s: No mapping information available for %s,%s", myself, map->map_name, map->domain); return (FAILURE); } /* Allocate and set up names */ if (SUCCESS != alloc_temp_names(map->map_path, &temp_entries, &temp_ttl)) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Unable to create map names for %s", myself, map->map_path); return (FAILURE); } /* Create temp entry and TTL file */ if ((temp_entries_db = dbm_open(temp_entries, O_RDWR | O_CREAT, 0644)) == NULL) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Could not open %s", myself, temp_entries); sfree(temp_entries); sfree(temp_ttl); return (FAILURE); } if ((temp_ttl_db = dbm_open(temp_ttl, O_RDWR | O_CREAT, 0644)) == NULL) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Could not open %s", myself, temp_ttl); dbm_close(temp_entries_db); delete_map(temp_entries); sfree(temp_entries); sfree(temp_ttl); return (FAILURE); } /* Initialize domainContext tsd */ __nisdb_get_tsd()->domainContext = 0; for (i = 0; i < ypDomains.numDomains; i++) { if (0 == ypDomains.domainLabels[i]) continue; if (0 == strcasecmp(map->domain, ypDomains.domainLabels[i])) { __nisdb_get_tsd()->domainContext = ypDomains.domains[i]; break; } } if (!(objname = getFullMapName(map->map_name, map->domain))) { if (temp_entries_db) dbm_close(temp_entries_db); if (temp_ttl_db) dbm_close(temp_ttl_db); delete_map(temp_entries); sfree(temp_entries); delete_map(temp_ttl); sfree(temp_ttl); return (FAILURE); } /* * set xlate_to_lcase to TRUE if map_name is found in * xlate_to_lcase_maps[] */ m = 0; while (xlate_to_lcase_maps[m] != NULL) { if (strncmp(map->map_name, xlate_to_lcase_maps[m], strlen(xlate_to_lcase_maps[m])) == 0) { xlate_to_lcase = TRUE; break; } ++m; } /* Try each mapping for the map */ for (flag = 0; t != 0 && search_flag != FAILURE; t = t->next) { /* Check if the mapping is the correct one */ if (strcmp(objname, t->objName) != 0) { continue; } /* Check if rulesFromLDAP are provided */ if (t->numRulesFromLDAP == 0) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: No rulesFromLDAP available for %s (%s)", myself, t->dbId, map->map_name); continue; } /* Set flag to indicate update is enabled */ flag = 1; /* Create ldap request for enumeration */ for (objectDN = t->objectDN; objectDN && objectDN->read.base; objectDN = objectDN->next) { if ((ls = createLdapRequest(t, 0, 0, 1, NULL, objectDN)) == 0) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Failed to create " "ldapSearch request for " "%s (%s) for base %s", myself, t->dbId, map->map_name, objectDN->read.base); statP = FAILURE; search_flag = FAILURE; break; } if (log_flag) { printf("Waiting for LDAP search results.\n"); } /* Query LDAP */ nr = (ls->isDN)?0:-1; rv = ldapSearch(ls, &nr, 0, &statP); freeLdapSearch(ls); if (rv == 0) { if (statP == LDAP_NO_SUCH_OBJECT) { /* * No Entry exists in the ldap server. Not * a problem. Maybe there are just no entries * in this map. */ continue; } logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: ldapSearch error %d " "(%s) for %s (%s) for base %s", myself, statP, ldap_err2string(statP), t->dbId, map->map_name, objectDN->read.base); statP = FAILURE; search_flag = FAILURE; break; } if (log_flag) { printf("Processing search results.\n"); } /* Obtain list of DNs for logging */ if ((dn = findDNs(myself, rv, nr, 0, &numDNs)) == 0) { statP = FAILURE; search_flag = FAILURE; break; } /* For each entry in the result do the following */ for (i = 0; i < nr; i++) { /* Convert LDAP data to NIS equivalents */ statP = buildNISRuleValue(t, &rv[i], map->domain); if (statP == MAP_INDEXLIST_ERROR) continue; if (statP != SUCCESS) { logmsg(MSG_NOTIMECHECK, LOG_WARNING, "%s: Conversion error %d (LDAP to " "name=value pairs) " "for (dn: %s) for " "%s (%s) for base %s", myself, statP, NIL(dn[i]), t->dbId, map->map_name, objectDN->read.base); continue; } /* Obtain the datum for value */ datval = ruleValueToDatum(t, &rv[i], &statP); if (datval == 0) { logmsg(MSG_NOTIMECHECK, LOG_WARNING, "%s: Conversion error %d " "(name=value pairs to NIS)" " for (dn: %s) for " "%s (%s) for base %s", myself, statP, NIL(dn[i]), t->dbId, map->map_name, objectDN->read.base); continue; } /* Obtain the datum for key */ datkey = getKeyFromRuleValue(t, &rv[i], &nv, &statP, xlate_to_lcase); if (datkey == 0) { logmsg(MSG_NOTIMECHECK, LOG_WARNING, "%s: Unable to obtain NIS " "key from LDAP data (dn:%s) " "for %s (%s) for base %s", myself, NIL(dn[i]), t->dbId, map->map_name, objectDN->read.base); sfree(datval->dptr); sfree(datval); continue; } /* Write to the temporary map */ for (j = 0; j < nv; j++, entry_count ++) { if (datkey[j].dsize == 0) continue; errno = 0; /* DBM_INSERT to match */ /* singleReadFromDIT */ if (dbm_store(temp_entries_db, datkey[j], *datval, DBM_INSERT) < 0) { /* * For some cases errno may * still be 0 but dbm_error * isn't informative at all. */ logmsg(MSG_NOTIMECHECK, LOG_WARNING, "%s: dbm store error " "(errno=%d) " "for (key=%s, value=%s) " "for %s (%s) for base %s", myself, errno, datkey[j].dptr, datval->dptr, t->dbId, map->map_name, objectDN->read.base); /* clear the error */ dbm_clearerr(temp_entries_db); } sfree(datkey[j].dptr); if (log_flag && (entry_count >= next_print)) { printf("%d entries processed\n", entry_count); next_print *= 2; } } sfree(datkey); sfree(datval->dptr); sfree(datval); } freeRuleValue(rv, nr); freeDNs(dn, numDNs); } /* End of for over objectDN */ } sfree(objname); if (t != 0 || flag == 0 || search_flag == FAILURE) { if (temp_entries_db) dbm_close(temp_entries_db); if (temp_ttl_db) dbm_close(temp_ttl_db); delete_map(temp_entries); sfree(temp_entries); delete_map(temp_ttl); sfree(temp_ttl); return (statP); } /* Set up enough of map_ctrl to call update_entry_ttl */ temp_map.map_name = map->map_name; temp_map.domain = map->domain; temp_map.ttl = temp_ttl_db; /* Generate new TTL file */ key = dbm_firstkey(temp_entries_db); while (key.dptr != 0) { if (!is_special_key(&key)) /* * We don't want all the entries to time out at the * same time so create random TTLs. */ if (FAILURE == update_entry_ttl(&temp_map, &key, TTL_RAND)) logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Could not update TTL for " "(key=%s) for map %s,%s", myself, NIL(key.dptr), map->map_name, map->domain); key = dbm_nextkey(temp_entries_db); } /* Update map TTL */ if (SUCCESS != update_map_ttl(&temp_map)) { logmsg(MSG_NOTIMECHECK, LOG_ERR, "%s: Could not update map TTL " "for %s,%s", myself, map->map_name, map->domain); } /* Set up 'special' nis entries */ add_special_entries(temp_entries_db, map, &secure_flag); /* Close temp DBM files */ dbm_close(temp_entries_db); dbm_close(temp_ttl_db); /* Lock access to the map for copy */ lock_map_ctrl(map); /* Move temp maps to real ones */ rename_map(temp_entries, map->map_path, secure_flag); rename_map(temp_ttl, map->ttl_path, secure_flag); /* Free file names */ sfree(temp_entries); sfree(temp_ttl); /* Unlock map */ unlock_map_ctrl(map); return (SUCCESS); }