Skip to content
Browse files

Allow customization of user completion. * Show/hide root account * De…

…fine min/max uid

Signed-off-by: Michał Siejak <michal@siejak.pl>
  • Loading branch information...
1 parent 6cea03b commit 4e8cccb3088651ea6fd14d9b362b8098a4ec026c Mateusz Hromada committed with Oct 15, 2011
Showing with 62 additions and 1 deletion.
  1. +4 −0 include/config.h
  2. +6 −0 include/libxlsh.h
  3. +51 −0 src/libxlsh.c
  4. +1 −1 src/xlsh.c
View
4 include/config.h
@@ -33,4 +33,8 @@
#define XLSHD_XWAIT 1
#define XLSHD_XRETRY 2
+#define XLSHD_COMPLETION_SHOWROOT 1
+#define XLSHD_COMPLETION_MINUID 1000
+#define XLSHD_COMPLETION_MAXUID 65534
+
#endif
View
6 include/libxlsh.h
@@ -22,10 +22,16 @@
#define XLSH_OVERWRITE 0x01
#define XLSH_DETACH 0x02
+#if !defined (STREQN)
+#define STREQN(a, b, n) (((n) == 0) ? (1) \
+ : ((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0))
+#endif
+
void libxlsh_proc_sigmask(void);
pid_t libxlsh_proc_exec(const char* cmdline, int flags);
pid_t libxlsh_pid_read(const char* filename);
int libxlsh_pid_lock(const char* filename, pid_t pid, int flags);
int libxlsh_file_read(const char* filename, char** buffer, size_t* bufsize);
+char* libxlsh_username_completion_function(const char* text, int state);
#endif
View
51 src/libxlsh.c
@@ -11,6 +11,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <pwd.h>
#include <config.h>
#include <libxlsh.h>
@@ -107,3 +108,53 @@ int libxlsh_file_read(const char* filename, char** buffer, size_t* bufsize)
}
return XLSH_EOK;
}
+
+
+/* A completion function for usernames. */
+char* libxlsh_username_completion_function(const char* text, int state)
+{
+ static char* username = (char*) NULL;
+ static struct passwd* entry;
+ static int namelen;
+ char* value;
+
+ if(state == 0)
+ {
+ if(username)
+ free(username);
+ namelen = strlen(text);
+ username = malloc(namelen + 1);
+ strcpy(username, text);
+ setpwent();
+ }
+
+ while((entry = getpwent()))
+ {
+#if XLSHD_COMPLETION_SHOWROOT == 1
+ if(entry->pw_uid != 0
+ && (entry->pw_uid < XLSHD_COMPLETION_MINUID
+ || entry->pw_uid >= XLSHD_COMPLETION_MAXUID))
+#else
+ if(entry->pw_uid == 0
+ || entry->pw_uid < XLSHD_COMPLETION_MINUID
+ || entry->pw_uid >= XLSHD_COMPLETION_MAXUID)
+#endif
+ continue;
+ /* Null usernames should result in all users as possible completions. */
+ if(namelen == 0 || (STREQN(username, entry->pw_name, namelen)))
+ break;
+ }
+
+ if(entry == 0)
+ {
+ endpwent();
+ return ((char*) NULL);
+ }
+ else
+ {
+ value = malloc(2 + strlen(entry->pw_name));
+ *value = *text;
+ strcpy(value, entry->pw_name);
+ return (value);
+ }
+}
View
2 src/xlsh.c
@@ -521,7 +521,7 @@ static char** xlsh_cmd_complete(const char* text, int start, int end)
return rl_completion_matches((char*)text, xlsh_cmd_match);
else
return rl_completion_matches((char*)text,
- rl_username_completion_function);
+ libxlsh_username_completion_function);
}
int xlsh_cmd_loop(void)

0 comments on commit 4e8cccb

Please sign in to comment.
Something went wrong with that request. Please try again.