📄 krb_dbm.c
字号:
for (try = 0; try < KERB_DB_MAX_RETRY; try++) { trans = kerb_start_read(); if ((code = kerb_dbl_lock(KERB_DBL_SHARED)) != 0) return -1; db = dbm_open(current_db_name, O_RDONLY, 0600); *more = 0;#ifdef DEBUG if (kerb_debug & 2) fprintf(stderr, "%s: db_get_principal for %s %s max = %d", progname, name, inst, max);#endif wildp = !strcmp(name, "*"); wildi = !strcmp(inst, "*"); if (!wildi && !wildp) { /* nothing's wild */ encode_princ_key(&key, name, inst); contents = dbm_fetch(db, key); if (contents.dptr == NULL) { found = 0; goto done; } decode_princ_contents(&contents, principal);#ifdef DEBUG if (kerb_debug & 1) { fprintf(stderr, "\t found %s %s p_n length %d t_n length %d\n", principal->name, principal->instance, strlen(principal->name), strlen(principal->instance)); }#endif found = 1; goto done; } /* process wild cards by looping through entire database */ for (key = dbm_firstkey(db); key.dptr != NULL; key = dbm_next(db, key)) { decode_princ_key(&key, testname, testinst); if ((wildp || !strcmp(testname, name)) && (wildi || !strcmp(testinst, inst))) { /* have a match */ if (found >= max) { *more = 1; goto done; } else { found++; contents = dbm_fetch(db, key); decode_princ_contents(&contents, principal);#ifdef DEBUG if (kerb_debug & 1) { fprintf(stderr, "\tfound %s %s p_n length %d t_n length %d\n", principal->name, principal->instance, strlen(principal->name), strlen(principal->instance)); }#endif principal++; /* point to next */ } } } done: kerb_dbl_unlock(); /* unlock read lock */ dbm_close(db); if (kerb_end_read(trans) == 0) break; found = -1; if (!non_blocking) sleep(1); } return (found);}/* * Update a name in the data base. Returns number of names * successfully updated. */kerb_db_put_principal(principal, max) Principal *principal; unsigned int max; /* number of principal structs to * update */{ int found = 0, code; u_long i; extern int errorproc(); datum key, contents; DBM *db; gettimeofday(×tamp, NULL); if (!init) kerb_db_init(); if ((code = kerb_dbl_lock(KERB_DBL_EXCLUSIVE)) != 0) return -1; db = dbm_open(current_db_name, O_RDWR, 0600);#ifdef DEBUG if (kerb_debug & 2) fprintf(stderr, "%s: kerb_db_put_principal max = %d", progname, max);#endif /* for each one, stuff temps, and do replace/append */ for (i = 0; i < max; i++) { encode_princ_contents(&contents, principal); encode_princ_key(&key, principal->name, principal->instance); if (dbm_store(db, key, contents, DBM_REPLACE) == -1) { found = -1; perror("dbm_store"); break; }#ifdef DEBUG if (kerb_debug & 1) { fprintf(stderr, "\n put %s %s\n", principal->name, principal->instance); }#endif found++; principal++; /* bump to next struct */ } dbm_close(db); kerb_dbl_unlock(); /* unlock database */ return (found);}static voidencode_princ_key(key, name, instance) datum *key; char *name, *instance;{ static char keystring[ANAME_SZ + INST_SZ]; bzero(keystring, ANAME_SZ + INST_SZ); strncpy(keystring, name, ANAME_SZ); strncpy(&keystring[ANAME_SZ], instance, INST_SZ); key->dptr = keystring; key->dsize = ANAME_SZ + INST_SZ;}static voiddecode_princ_key(key, name, instance) datum *key; char *name, *instance;{ strncpy(name, key->dptr, ANAME_SZ); strncpy(instance, key->dptr + ANAME_SZ, INST_SZ); name[ANAME_SZ - 1] = '\0'; instance[INST_SZ - 1] = '\0';}static voidencode_princ_contents(contents, principal) datum *contents; Principal *principal;{ contents->dsize = sizeof(*principal); contents->dptr = (char *) principal;}static voiddecode_princ_contents(contents, principal) datum *contents; Principal *principal;{ bcopy(contents->dptr, (char *) principal, sizeof(*principal));}kerb_db_get_stat(s) DB_stat *s;{ gettimeofday(×tamp, NULL); s->cpu = 0; s->elapsed = 0; s->dio = 0; s->pfault = 0; s->t_stamp = timestamp.tv_sec; s->n_retrieve = 0; s->n_replace = 0; s->n_append = 0; s->n_get_stat = 0; s->n_put_stat = 0; /* update local copy too */}kerb_db_put_stat(s) DB_stat *s;{}delta_stat(a, b, c) DB_stat *a, *b, *c;{ /* c = a - b then b = a for the next time */ c->cpu = a->cpu - b->cpu; c->elapsed = a->elapsed - b->elapsed; c->dio = a->dio - b->dio; c->pfault = a->pfault - b->pfault; c->t_stamp = a->t_stamp - b->t_stamp; c->n_retrieve = a->n_retrieve - b->n_retrieve; c->n_replace = a->n_replace - b->n_replace; c->n_append = a->n_append - b->n_append; c->n_get_stat = a->n_get_stat - b->n_get_stat; c->n_put_stat = a->n_put_stat - b->n_put_stat; bcopy(a, b, sizeof(DB_stat)); return;}/* * look up a dba in the data base returns number of dbas found , and * whether there were more than requested. */kerb_db_get_dba(dba_name, dba_inst, dba, max, more) char *dba_name; /* could have wild card */ char *dba_inst; /* could have wild card */ Dba *dba; unsigned int max; /* max number of name structs to return */ int *more; /* where there more than 'max' tuples? */{ *more = 0; return (0);}kerb_db_iterate (func, arg) int (*func)(); char *arg; /* void *, really */{ datum key, contents; Principal *principal; int code; DBM *db; kerb_db_init(); /* initialize and open the database */ if ((code = kerb_dbl_lock(KERB_DBL_SHARED)) != 0) return code; db = dbm_open(current_db_name, O_RDONLY, 0600); for (key = dbm_firstkey (db); key.dptr != NULL; key = dbm_next(db, key)) { contents = dbm_fetch (db, key); /* XXX may not be properly aligned */ principal = (Principal *) contents.dptr; if ((code = (*func)(arg, principal)) != 0) return code; } dbm_close(db); kerb_dbl_unlock(); return 0;}static int dblfd = -1;static int mylock = 0;static int inited = 0;static kerb_dbl_init(){ if (!inited) { char *filename = gen_dbsuffix (current_db_name, ".ok"); if ((dblfd = open(filename, 0)) < 0) { fprintf(stderr, "kerb_dbl_init: couldn't open %s\n", filename); fflush(stderr); perror("open"); exit(1); } free(filename); inited++; } return (0);}static void kerb_dbl_fini(){ close(dblfd); dblfd = -1; inited = 0; mylock = 0;}static int kerb_dbl_lock(mode) int mode;{ int flock_mode; if (!inited) kerb_dbl_init(); if (mylock) { /* Detect lock call when lock already * locked */ fprintf(stderr, "Kerberos locking error (mylock)\n"); fflush(stderr); exit(1); } switch (mode) { case KERB_DBL_EXCLUSIVE: flock_mode = LOCK_EX; break; case KERB_DBL_SHARED: flock_mode = LOCK_SH; break; default: fprintf(stderr, "invalid lock mode %d\n", mode); abort(); } if (non_blocking) flock_mode |= LOCK_NB; if (flock(dblfd, flock_mode) < 0) return errno; mylock++; return 0;}static void kerb_dbl_unlock(){ if (!mylock) { /* lock already unlocked */ fprintf(stderr, "Kerberos database lock not locked when unlocking.\n"); fflush(stderr); exit(1); } if (flock(dblfd, LOCK_UN) < 0) { fprintf(stderr, "Kerberos database lock error. (unlocking)\n"); fflush(stderr); perror("flock"); exit(1); } mylock = 0;}int kerb_db_set_lockmode(mode) int mode;{ int old = non_blocking; non_blocking = mode; return old;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -