summaryrefslogtreecommitdiff
path: root/src/users.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/users.c')
-rw-r--r--src/users.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/src/users.c b/src/users.c
new file mode 100644
index 0000000..292f9ad
--- /dev/null
+++ b/src/users.c
@@ -0,0 +1,196 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#define _GNU_SOURCE
+#include <getopt.h>
+
+#include <main.h>
+#include <commands.h>
+#include <completion.h>
+#include <utils.h>
+
+#include <shell.h>
+#include <users.h>
+
+/* **************************************************************** */
+/* */
+/* SILA Users Commands */
+/* */
+/* **************************************************************** */
+
+/* String to pass to system (). This is for the LIST, VIEW and RENAME
+ commands. */
+static char syscom[PATH_MAX];
+
+static int argc;
+static char args[PATH_MAX];
+static const char *argv[PATH_MAX];
+
+void split_args( const char *name, char *arg )
+{
+ char *p = NULL;
+ bzero( &args[0], PATH_MAX );
+ bzero( (void *)argv, sizeof( argv ) );
+ argv[0] = name;
+ argc = 1;
+
+ if( !arg )
+ {
+ return;
+ }
+
+ strncpy( args, arg, PATH_MAX - 1 );
+ p = &args[0];
+ while( *p )
+ {
+ while( *p && (*p == '\t' || *p == ' ') )
+ ++p;
+
+ if( *p )
+ {
+ argv[argc] = p;
+ while( *p && *p != '\t' && *p != ' ' )
+ ++p;
+ *p = '\0';
+ ++p;
+
+ ++argc;
+ }
+ }
+}
+
+int com_useradd( char *arg )
+{
+ /* temporary STUB. Should use busctl or REST */
+ if( !valid_argument( "useradd", arg ) )
+ return( 1 );
+
+ sprintf (syscom, "useradd %s", arg);
+ return( system( syscom ) );
+}
+
+int com_userdel( char *arg )
+{
+ /* temporary STUB. Should use busctl or REST */
+ if( !valid_argument( "userdel", arg ) )
+ return( 1 );
+
+ sprintf( syscom, "userdel %s", arg );
+ return( system( syscom ) );
+}
+
+
+
+/***************************************************************
+ Users list functions:
+ ***************************************************************/
+
+/* Options: */
+static int userlist_opt_help = 0;
+
+static void userlist_usage( char *fname )
+{
+ fprintf( stdout, "\n" );
+ fprintf( stdout, "Usage: %s [options]\n", fname );
+ fprintf( stdout, "\n" );
+ fprintf( stdout, "List users.\n" );
+ fprintf( stdout, "\n" );
+ fprintf( stdout, "Options:\n" );
+ fprintf( stdout, " -h,--help Display this information.\n" );
+ fprintf( stdout, "\n" );
+}
+
+
+void userlist_get_args( int argc, char * const *argv )
+{
+ const char* short_options = "h";
+
+ const struct option long_options[] =
+ {
+ { "help", no_argument, NULL, 'h' },
+ { NULL, 0, NULL, 0 }
+ };
+
+ int ret;
+ int option_index = 0;
+
+ /* Reset options: */
+ userlist_opt_help = 0;
+
+ while( (ret = getopt_long( argc, argv, short_options, long_options, &option_index )) != -1 )
+ {
+ switch( ret )
+ {
+ case 'h':
+ {
+ userlist_opt_help = 1;
+ break;
+ }
+ case '?': default:
+ {
+ userlist_opt_help = 1;
+ break;
+ }
+ }
+ }
+
+ if( optind < argc )
+ {
+ userlist_opt_help = 1;
+ }
+
+ /* This is important for call getopt_long() several times! */
+ optind = 0;
+}
+
+
+
+int com_userlist( char *arg )
+{
+ split_args( "list", arg );
+
+/* test:
+ printf( "argc: %d\n", argc );
+ if( argc > 0 )
+ {
+ int i = 0;
+ while( i < argc )
+ {
+ printf( "arg[%d] = '%s';\n", i, argv[i] );
+ ++i;
+ }
+ }
+ */
+
+ userlist_get_args( argc, (char * const *)&argv[0] );
+
+ if( userlist_opt_help )
+ {
+ userlist_usage( "list" );
+ }
+ else
+ {
+ /*
+ list command:
+ */
+ sprintf( syscom, "busctl --list tree %s | grep %s/ | sed 's,%s/, ,'",
+ USERS_DBUS_SERVICE,
+ USERS_DBUS_ROOT_PATH,
+ USERS_DBUS_ROOT_PATH );
+ return( system( syscom ) );
+ }
+
+ return( 0 );
+}