Add usernames and session IDs to readline cache.

This allows auto-completion to show user, and show id,
after list users is executed.
This commit is contained in:
Nikos Mavrogiannopoulos
2014-01-10 10:48:55 +01:00
parent b8cd530506
commit 503cd9576d
4 changed files with 140 additions and 4 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ ocpasswd-args.c: $(srcdir)/ocpasswd-args.def
$(AUTOGEN) $<
ocpasswd-args.h: ocpasswd-args.c
occtl_SOURCES = occtl.c occtl-pager.c occtl.h occtl-time.c
occtl_SOURCES = occtl.c occtl-pager.c occtl.h occtl-time.c occtl-cache.c
occtl_LDADD = ../gl/libgnu.a $(LIBDBUS_LIBS) $(LIBREADLINE_LIBS)
ipc.pb-c.c: ipc.proto
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2013 Red Hat
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of ocserv.
*
* ocserv is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* GnuTLS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <occtl.h>
#include <c-strcase.h>
#include <minmax.h>
typedef struct uid_entries_st {
char* user;
unsigned user_size;
char id[8];
unsigned id_size;
} uid_entries_st;
static uid_entries_st *entries = NULL;
static unsigned entries_size = 0;
static unsigned max_entries_size = 0;
void entries_clear(void)
{
unsigned i;
for (i=0;i<entries_size;i++) {
free(entries[i].user);
entries[i].user = 0;
}
entries_size = 0;
}
void entries_add(const char* user, unsigned user_size, unsigned id)
{
if (entries_size+1 > max_entries_size) {
max_entries_size += 128;
entries = realloc(entries, sizeof(uid_entries_st)*max_entries_size);
}
entries[entries_size].user = strdup(user);
entries[entries_size].user_size = user_size;
entries[entries_size].id_size =
snprintf(entries[entries_size].id, sizeof(entries[entries_size].id), "%u", id);
entries_size++;
return;
}
char* search_for_user(unsigned idx, const char* match, int match_size)
{
unsigned i;
if (idx >= entries_size)
return NULL;
for (i=idx;i<entries_size;i++) {
if (match_size <= entries[i].user_size) {
if (c_strncasecmp(match, entries[i].user, match_size) == 0)
return strdup(entries[i].user);
}
}
return NULL;
}
char* search_for_id(unsigned idx, const char* match, int match_size)
{
unsigned i;
if (idx >= entries_size)
return NULL;
for (i=idx;i<entries_size;i++) {
if (match_size <= entries[i].id_size) {
if (c_strncasecmp(match, entries[i].id, match_size) == 0) {
return strdup(entries[i].id);
}
}
}
return NULL;
}
+32 -3
View File
@@ -514,6 +514,8 @@ void handle_list_users_cmd(DBusConnection * conn, const char *arg)
unsigned iteration = 0;
const char* dtls_ciphersuite, *tls_ciphersuite;
entries_clear();
out = pager_start();
msg = send_dbus_cmd(conn, "org.infradead.ocserv",
@@ -656,6 +658,8 @@ void handle_list_users_cmd(DBusConnection * conn, const char *arg)
else
fprintf(out, " %9s %9s\n", "(no dtls)", auth);
entries_add(username, strlen(username), id);
if (!dbus_message_iter_next(&suba))
goto cleanup;
}
@@ -1107,24 +1111,50 @@ static unsigned int cmd_start = 0;
static char *command_generator(const char *text, int state)
{
static int list_index, len;
static int entries_idx;
unsigned name_size;
char *name;
char *name, *arg;
char *ret;
/* If this is a new word to complete, initialize now. This includes
saving the length of TEXT for efficiency, and initializing the index
variable to 0. */
if (!state) {
list_index = 0;
entries_idx = 0;
len = strlen(text);
}
/* Return the next name which partially matches from the command list. */
while ((name = commands[list_index].name)) {
name_size = commands[list_index].name_size;
arg = commands[list_index].arg;
list_index++;
if (name_size < cmd_start)
if (cmd_start > name_size) {
/* check for user or ID options */
if (rl_line_buffer != NULL &&
c_strncasecmp(rl_line_buffer, name, name_size) == 0 &&
/* make sure only one argument is appended */
rl_line_buffer[name_size] != 0 &&
strchr(&rl_line_buffer[name_size+1], ' ') == NULL) {
if (arg != NULL) {
ret = NULL;
if (strcmp(arg, "[NAME]") == 0)
ret = search_for_user(entries_idx, text, len);
else if (strcmp(arg, "[ID]") == 0)
ret = search_for_id(entries_idx, text, len);
if (ret != NULL) {
entries_idx++;
}
list_index--; /* restart at the same cmd */
return ret;
}
}
continue;
}
if (cmd_start > 0 && name[cmd_start - 1] != ' ')
continue;
@@ -1134,7 +1164,6 @@ static char *command_generator(const char *text, int state)
continue;
name += cmd_start;
if (c_strncasecmp(name, text, len) == 0) {
return (strdup(name));
}
+5
View File
@@ -8,4 +8,9 @@ FILE* pager_start(void);
void pager_stop(FILE* fp);
void print_time_ival7(time_t t, FILE * fout);
char* search_for_id(unsigned idx, const char* match, int match_size);
char* search_for_user(unsigned idx, const char* match, int match_size);
void entries_add(const char* user, unsigned user_size, unsigned id);
void entries_clear(void);
#endif