diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-05-22 19:12:20 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-05-22 19:12:20 +0200 |
commit | ea2bfa175c9fcc4c0d4aa708fea6ce497b2c73d6 (patch) | |
tree | c01244a35117da75790cdd034c47daa38bd4fc1f | |
parent | 34ab7d2f73d351158875effa175d2daf6a4ed976 (diff) |
added line numbers feature to ecp util read key function
-rw-r--r-- | ecp/server/acl.c | 26 | ||||
-rw-r--r-- | ecp/util/util.c | 32 | ||||
-rw-r--r-- | ecp/util/util.h | 4 |
3 files changed, 39 insertions, 23 deletions
diff --git a/ecp/server/acl.c b/ecp/server/acl.c index 3341886..33b17ca 100644 --- a/ecp/server/acl.c +++ b/ecp/server/acl.c @@ -69,9 +69,9 @@ static int _add_key(ecp_ecdh_public_t *public, uint8_t roles) { return ECP_OK; } -static int _read_file(int fd, ACLItem *head) { +static int _read_file(int fd, ACLItem *head, int *line_cnt) { ecp_ecdh_public_t public; - int rv; + int _line_cnt, rv; if (head == NULL) return ECP_ERR_ALLOC; @@ -79,7 +79,7 @@ static int _read_file(int fd, ACLItem *head) { head = head->next; } - while(ecp_util_read_key(fd, &public, NULL) == ECP_OK) { + while((rv = ecp_util_read_key(fd, &public, NULL, &_line_cnt)) == ECP_OK) { if (head->key_cnt == ACL_MAX_KEY) { head->next = acl_create_item(); if (head->next == NULL) return ECP_ERR_ALLOC; @@ -87,9 +87,12 @@ static int _read_file(int fd, ACLItem *head) { } memcpy(&head->key[head->key_cnt], &public, sizeof(head->key[head->key_cnt])); head->key_cnt++; + *line_cnt += _line_cnt; } + *line_cnt += _line_cnt; - return ECP_OK; + if (rv == ECP_ERR_EOF) return ECP_OK; + return rv; } static int _li2ht(ACLItem *head, int is_dir) { @@ -194,6 +197,7 @@ int acl_load(void) { ACLItem *acl_dir_new = NULL; int fd = -1; int fd_dir = -1; + int line_cnt; int rv = ECP_OK; int _rv; @@ -202,7 +206,7 @@ int acl_load(void) { if (srv_config->acl_fn) { fd = open(srv_config->acl_fn, O_RDONLY); if (fd < 0) { - LOG(LOG_ERR, "acl_load: unable to open: %s\n", srv_config->acl_fn); + LOG(LOG_ERR, "acl_load: unable to open ACL file: %s\n", srv_config->acl_fn); return ECP_ERR; } } @@ -210,7 +214,7 @@ int acl_load(void) { if (srv_config->acl_fn_dir) { fd_dir = open(srv_config->acl_fn_dir, O_RDONLY); if (fd_dir < 0) { - LOG(LOG_ERR, "acl_load: unable to open: %s\n", srv_config->acl_fn_dir); + LOG(LOG_ERR, "acl_load: unable to open ACL file: %s\n", srv_config->acl_fn_dir); close(fd); return ECP_ERR; } @@ -218,20 +222,22 @@ int acl_load(void) { pthread_mutex_lock(&acl_li_mutex); if (fd >= 0) { + line_cnt = 0; acl_new = acl_create_item(); - rv = _read_file(fd, acl_new); + rv = _read_file(fd, acl_new, &line_cnt); if (rv) { - LOG(LOG_ERR, "acl_load: read from file: %s err:%d\n", srv_config->acl_fn, rv); + LOG(LOG_ERR, "acl_load: bad ACL file: %s line:%d\n", srv_config->acl_fn, line_cnt); acl_destroy_list(acl_new); goto load_fin; } } if (fd_dir >= 0) { + line_cnt = 0; acl_dir_new = acl_create_item(); - rv = _read_file(fd_dir, acl_dir_new); + rv = _read_file(fd_dir, acl_dir_new, &line_cnt); if (rv) { - LOG(LOG_ERR, "acl_load: read from file: %s err:%d\n", srv_config->acl_fn_dir, rv); + LOG(LOG_ERR, "acl_load: bad ACL file: %s line:%d\n", srv_config->acl_fn_dir, line_cnt); acl_destroy_list(acl_dir_new); acl_destroy_list(acl_new); goto load_fin; diff --git a/ecp/util/util.c b/ecp/util/util.c index 0f7f066..e11cab9 100644 --- a/ecp/util/util.c +++ b/ecp/util/util.c @@ -10,16 +10,18 @@ #include "util.h" -static int _read_key(int fd, uint8_t *key) { +static int _read_key(int fd, uint8_t *key, int *line_cnt) { char buffer[ECP_SIZE_ECDH_KEY_BUF]; char ch; int cmt; ssize_t rv; int _rv; + if (line_cnt) *line_cnt = 0; do { cmt = 0; rv = read(fd, &ch, 1); + if (rv == 0) return ECP_ERR_EOF; if (rv != 1) return ECP_ERR; switch (ch) { case '#': @@ -27,8 +29,10 @@ static int _read_key(int fd, uint8_t *key) { cmt = 1; while (ch != '\n') { rv = read(fd, &ch, 1); + if (rv == 0) return ECP_ERR_EOF; if (rv != 1) return ECP_ERR; } + if (line_cnt) *line_cnt += 1; break; } @@ -38,6 +42,7 @@ static int _read_key(int fd, uint8_t *key) { } } } while(cmt); + if (line_cnt) *line_cnt += 1; rv = read(fd, buffer + 1, sizeof(buffer) - 2); if (rv < (sizeof(buffer) - 2)) return ECP_ERR; @@ -51,29 +56,31 @@ static int _read_key(int fd, uint8_t *key) { return _rv; } -int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { +int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, int *line_cnt) { int rv; - rv = _read_key(fd, (uint8_t *)public); + rv = _read_key(fd, (uint8_t *)public, line_cnt); if (rv) return rv; if (private) { - rv = _read_key(fd, (uint8_t *)private); + rv = _read_key(fd, (uint8_t *)private, line_cnt); if (rv) return rv; } return ECP_OK; } -int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { +int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, int *line_cnt) { char buffer[ECP_SIZE_ECDH_KEY_BUF]; ssize_t rv; + if (line_cnt) *line_cnt = 0; ecp_key2str(buffer, (uint8_t *)public); buffer[ECP_SIZE_ECDH_KEY_BUF - 1] = '\n'; rv = write(fd, buffer, sizeof(buffer)); if (rv != sizeof(buffer)) return ECP_ERR; + if (line_cnt) *line_cnt += 1; if (private) { ecp_key2str(buffer, (uint8_t *)private); @@ -81,6 +88,7 @@ int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *pr rv = write(fd, buffer, sizeof(buffer)); if (rv != sizeof(buffer)) return ECP_ERR; + if (line_cnt) *line_cnt += 1; } return ECP_OK; @@ -89,21 +97,23 @@ int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *pr int ecp_util_load_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { int rv, fd; - if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR; + if ((fd = open(filename, O_RDONLY)) < 0) return ECP_ERR_OPEN; - rv = ecp_util_read_key(fd, public, private); + rv = ecp_util_read_key(fd, public, private, NULL); close(fd); - return rv; + if (rv < 0) return rv; + return ECP_OK; } int ecp_util_save_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { int rv, fd; - if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR; + if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) return ECP_ERR_OPEN; - rv = ecp_util_write_key(fd, public, private); + rv = ecp_util_write_key(fd, public, private, NULL); close(fd); - return rv; + if (rv < 0) return rv; + return ECP_OK; } diff --git a/ecp/util/util.h b/ecp/util/util.h index f9a6bb4..41de33b 100644 --- a/ecp/util/util.h +++ b/ecp/util/util.h @@ -1,5 +1,5 @@ -int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); -int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); +int ecp_util_read_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, int *line_cnt); +int ecp_util_write_key(int fd, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private, int *line_cnt); int ecp_util_load_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private); int ecp_util_save_key(char *filename, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private);
\ No newline at end of file |