summaryrefslogtreecommitdiffstats
path: root/crypto/heimdal/lib/hdb/print.c
diff options
context:
space:
mode:
authornectar <nectar@FreeBSD.org>2002-02-19 15:46:56 +0000
committernectar <nectar@FreeBSD.org>2002-02-19 15:46:56 +0000
commit69a91bec14ec3ad49d1c8a82c40a796755f9e4a3 (patch)
tree85ecf91fd00875cec4b93111d3a8ed9eec9cddfe /crypto/heimdal/lib/hdb/print.c
parent8db4cdb3da4228a5d93635e43825e2e8a2f66db7 (diff)
downloadFreeBSD-src-69a91bec14ec3ad49d1c8a82c40a796755f9e4a3.zip
FreeBSD-src-69a91bec14ec3ad49d1c8a82c40a796755f9e4a3.tar.gz
Import of Heimdal Kerberos from KTH repository circa 2002/02/17.
Diffstat (limited to 'crypto/heimdal/lib/hdb/print.c')
-rw-r--r--crypto/heimdal/lib/hdb/print.c220
1 files changed, 123 insertions, 97 deletions
diff --git a/crypto/heimdal/lib/hdb/print.c b/crypto/heimdal/lib/hdb/print.c
index 903e78b..548a47a 100644
--- a/crypto/heimdal/lib/hdb/print.c
+++ b/crypto/heimdal/lib/hdb/print.c
@@ -33,7 +33,7 @@
#include "hdb_locl.h"
#include <ctype.h>
-RCSID("$Id: print.c,v 1.5 2001/01/26 15:08:36 joda Exp $");
+RCSID("$Id: print.c,v 1.7 2001/07/13 06:30:42 assar Exp $");
/*
This is the present contents of a dump line. This might change at
@@ -55,37 +55,45 @@ RCSID("$Id: print.c,v 1.5 2001/01/26 15:08:36 joda Exp $");
max ticket life
max renewable life
flags
+ generation number
*/
-static void
-append_hex(char *str, krb5_data *data)
+static krb5_error_code
+append_string(krb5_context context, krb5_storage *sp, const char *fmt, ...)
+{
+ krb5_error_code ret;
+ char *s;
+ va_list ap;
+ va_start(ap, fmt);
+ vasprintf(&s, fmt, ap);
+ va_end(ap);
+ if(s == NULL) {
+ krb5_set_error_string(context, "malloc: out of memory");
+ return ENOMEM;
+ }
+ ret = sp->store(sp, s, strlen(s));
+ free(s);
+ return ret;
+}
+
+static krb5_error_code
+append_hex(krb5_context context, krb5_storage *sp, krb5_data *data)
{
- int i, s = 1;
+ int i, printable = 1;
char *p;
p = data->data;
for(i = 0; i < data->length; i++)
if(!isalnum((unsigned char)p[i]) && p[i] != '.'){
- s = 0;
+ printable = 0;
break;
}
- if(s){
- p = calloc(1, data->length + 2 + 1);
- p[0] = '\"';
- p[data->length + 1] = '\"';
- memcpy(p + 1, data->data, data->length);
- }else{
- const char *xchars = "0123456789abcdef";
- char *q = p = malloc(data->length * 2 + 1);
- for(i = 0; i < data->length; i++) {
- unsigned char c = ((u_char*)data->data)[i];
- *q++ = xchars[(c & 0xf0) >> 4];
- *q++ = xchars[(c & 0xf)];
- }
- *q = '\0';
- }
- strcat(str, p);
- free(p);
+ if(printable)
+ return append_string(context, sp, "\"%.*s\"",
+ data->length, data->data);
+ for(i = 0; i < data->length; i++)
+ append_string(context, sp, "%02x", ((unsigned char*)data->data)[i]);
+ return 0;
}
static char *
@@ -97,38 +105,27 @@ time2str(time_t t)
}
static krb5_error_code
-event2string(krb5_context context, Event *ev, char **str)
+append_event(krb5_context context, krb5_storage *sp, Event *ev)
{
- char *p;
- char *pr;
+ char *pr = NULL;
krb5_error_code ret;
- if(ev == NULL){
- *str = strdup("-");
- return (*str == NULL) ? ENOMEM : 0;
- }
- if (ev->principal == NULL) {
- pr = strdup("UNKNOWN");
- if (pr == NULL)
- return ENOMEM;
- } else {
+ if(ev == NULL)
+ return append_string(context, sp, "- ");
+ if (ev->principal != NULL) {
ret = krb5_unparse_name(context, ev->principal, &pr);
if(ret)
return ret;
}
- ret = asprintf(&p, "%s:%s", time2str(ev->time), pr);
+ ret = append_string(context, sp, "%s:%s ",
+ time2str(ev->time), pr ? pr : "UNKNOWN");
free(pr);
- if(ret < 0)
- return ENOMEM;
- *str = p;
- return 0;
+ return ret;
}
-krb5_error_code
-hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
+static krb5_error_code
+entry2string_int (krb5_context context, krb5_storage *sp, hdb_entry *ent)
{
char *p;
- char buf[1024] = "";
- char tmp[32];
int i;
krb5_error_code ret;
@@ -136,90 +133,103 @@ hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
ret = krb5_unparse_name(context, ent->principal, &p);
if(ret)
return ret;
- strlcat(buf, p, sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
+ append_string(context, sp, "%s ", p);
free(p);
/* --- kvno */
- snprintf(tmp, sizeof(tmp), "%d", ent->kvno);
- strlcat(buf, tmp, sizeof(buf));
+ append_string(context, sp, "%d", ent->kvno);
/* --- keys */
for(i = 0; i < ent->keys.len; i++){
/* --- mkvno, keytype */
if(ent->keys.val[i].mkvno)
- snprintf(tmp, sizeof(tmp), ":%d:%d:",
- *ent->keys.val[i].mkvno,
- ent->keys.val[i].key.keytype);
+ append_string(context, sp, ":%d:%d:",
+ *ent->keys.val[i].mkvno,
+ ent->keys.val[i].key.keytype);
else
- snprintf(tmp, sizeof(tmp), "::%d:",
- ent->keys.val[i].key.keytype);
- strlcat(buf, tmp, sizeof(buf));
+ append_string(context, sp, "::%d:",
+ ent->keys.val[i].key.keytype);
/* --- keydata */
- append_hex(buf, &ent->keys.val[i].key.keyvalue);
- strlcat(buf, ":", sizeof(buf));
+ append_hex(context, sp, &ent->keys.val[i].key.keyvalue);
+ append_string(context, sp, ":");
/* --- salt */
if(ent->keys.val[i].salt){
- snprintf(tmp, sizeof(tmp), "%u/", ent->keys.val[i].salt->type);
- strlcat(buf, tmp, sizeof(buf));
- append_hex(buf, &ent->keys.val[i].salt->salt);
+ append_string(context, sp, "%u/", ent->keys.val[i].salt->type);
+ append_hex(context, sp, &ent->keys.val[i].salt->salt);
}else
- strlcat(buf, "-", sizeof(buf));
+ append_string(context, sp, "-");
}
- strlcat(buf, " ", sizeof(buf));
+ append_string(context, sp, " ");
/* --- created by */
- event2string(context, &ent->created_by, &p);
- strlcat(buf, p, sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
- free(p);
+ append_event(context, sp, &ent->created_by);
/* --- modified by */
- event2string(context, ent->modified_by, &p);
- strlcat(buf, p, sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
- free(p);
+ append_event(context, sp, ent->modified_by);
/* --- valid start */
if(ent->valid_start)
- strlcat(buf, time2str(*ent->valid_start), sizeof(buf));
+ append_string(context, sp, "%s ", time2str(*ent->valid_start));
else
- strlcat(buf, "-", sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
+ append_string(context, sp, "- ");
/* --- valid end */
if(ent->valid_end)
- strlcat(buf, time2str(*ent->valid_end), sizeof(buf));
+ append_string(context, sp, "%s ", time2str(*ent->valid_end));
else
- strlcat(buf, "-", sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
+ append_string(context, sp, "- ");
/* --- password ends */
if(ent->pw_end)
- strlcat(buf, time2str(*ent->pw_end), sizeof(buf));
+ append_string(context, sp, "%s ", time2str(*ent->pw_end));
else
- strlcat(buf, "-", sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
+ append_string(context, sp, "- ");
/* --- max life */
- if(ent->max_life){
- snprintf(tmp, sizeof(tmp), "%d", *ent->max_life);
- strlcat(buf, tmp, sizeof(buf));
- }else
- strlcat(buf, "-", sizeof(buf));
- strlcat(buf, " ", sizeof(buf));
+ if(ent->max_life)
+ append_string(context, sp, "%d ", *ent->max_life);
+ else
+ append_string(context, sp, "- ");
/* --- max renewable life */
- if(ent->max_renew){
- snprintf(tmp, sizeof(tmp), "%d", *ent->max_renew);
- strlcat(buf, tmp, sizeof(buf));
- }else
- strlcat(buf, "-", sizeof(buf));
+ if(ent->max_renew)
+ append_string(context, sp, "%d ", *ent->max_renew);
+ else
+ append_string(context, sp, "- ");
- strlcat(buf, " ", sizeof(buf));
-
/* --- flags */
- snprintf(tmp, sizeof(tmp), "%d", HDBFlags2int(ent->flags));
- strlcat(buf, tmp, sizeof(buf));
+ append_string(context, sp, "%d ", HDBFlags2int(ent->flags));
+
+ /* --- generation number */
+ if(ent->generation) {
+ append_string(context, sp, "%s:%d:%d", time2str(ent->generation->time),
+ ent->generation->usec,
+ ent->generation->gen);
+ } else
+ append_string(context, sp, "-");
- *str = strdup(buf);
+ return 0;
+}
+
+krb5_error_code
+hdb_entry2string (krb5_context context, hdb_entry *ent, char **str)
+{
+ krb5_error_code ret;
+ krb5_data data;
+ krb5_storage *sp;
+
+ sp = krb5_storage_emem();
+ if(sp == NULL) {
+ krb5_set_error_string(context, "malloc: out of memory");
+ return ENOMEM;
+ }
+ ret = entry2string_int(context, sp, ent);
+ if(ret) {
+ krb5_storage_free(sp);
+ return ret;
+ }
+
+ sp->store(sp, "\0", 1);
+ krb5_storage_to_data(sp, &data);
+ krb5_storage_free(sp);
+ *str = data.data;
return 0;
}
@@ -228,9 +238,25 @@ hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
krb5_error_code
hdb_print_entry(krb5_context context, HDB *db, hdb_entry *entry, void *data)
{
- char *p;
- hdb_entry2string(context, entry, &p);
- fprintf((FILE*)data, "%s\n", p);
- free(p);
+ krb5_error_code ret;
+ krb5_storage *sp;
+
+ FILE *f = data;
+
+ fflush(f);
+ sp = krb5_storage_from_fd(fileno(f));
+ if(sp == NULL) {
+ krb5_set_error_string(context, "malloc: out of memory");
+ return ENOMEM;
+ }
+
+ ret = entry2string_int(context, sp, entry);
+ if(ret) {
+ krb5_storage_free(sp);
+ return ret;
+ }
+
+ sp->store(sp, "\n", 1);
+ krb5_storage_free(sp);
return 0;
}
OpenPOWER on IntegriCloud