summaryrefslogtreecommitdiff
path: root/src/users.c
blob: 445f0c34059f7fce1778312f15ebb509d91289ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#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>


char user_name[USER_NAME_SIZE];
char user_role[USER_ROLE_SIZE];

/* **************************************************************** */
/*                                                                  */
/*                       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];

/* Options: */
static int  opt_help = 0;
static char opt_name[USER_NAME_SIZE];
static char opt_role[USER_ROLE_SIZE];

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;
    }
  }
}

void get_args( int argc, char * const *argv )
{
  const char* short_options = "hn:r:";

  const struct option long_options[] =
  {
    { "help",        no_argument,       NULL, 'h' },
    { "name",        required_argument, NULL, 'n' },
    { "role",        required_argument, NULL, 'r' },
    { NULL,          0,                 NULL,  0  }
  };

  int ret;
  int option_index = 0;

  /* Reset options: */
  opt_help = 0;
  bzero( &opt_name[0], USER_NAME_SIZE );
  bzero( &opt_role[0], USER_ROLE_SIZE );

  while( (ret = getopt_long( argc, argv, short_options, long_options, &option_index )) != -1 )
  {
    switch( ret )
    {
      case 'h':
      {
        opt_help = 1;
        break;
      }
      case 'n':
      {
        if( optarg != NULL )
          strncpy( opt_name, (const char *)optarg, sizeof( opt_name ) - 1 );
        else
          opt_help = 1;
        break;
      }
      case 'r':
      {
        if( optarg != NULL )
          strncpy( opt_role, (const char *)optarg, sizeof( opt_role ) - 1 );
        else
          opt_help = 1;
        break;
      }
      case '?': default:
      {
        opt_help = 1;
        break;
      }
    }
  }

  /*
    Assume that the last argument is a user name:
   */
  if( optind < argc )
  {
    strncpy( opt_name, (const char *)argv[optind++], sizeof( opt_name ) - 1 );
  }

  /* This is important for call getopt_long() several times! */
  optind = 0;
}


/***************************************************************
  Users management commands:
 ***************************************************************/

static void passwd_usage( char *fname )
{
  fprintf( stdout, "\n" );
  fprintf( stdout, "Usage: %s [username]\n", fname );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Set or change user password.\n" );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Options:\n" );
  fprintf( stdout, "  -h,--help    Display this information.\n" );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Parameter:\n" );
  fprintf( stdout, "  [username]   User name (default: %s).\n", user_name );
  fprintf( stdout, "\n" );
}

int com_passwd( char *arg )
{
  split_args( "passwd", arg );

  get_args( argc, (char * const *)&argv[0] );

  if( opt_help )
  {
    passwd_usage( "passwd" );
  }
  else
  {
    /* check args */
    if( !opt_name[0] )
    {
      sprintf( syscom, "passwd %s", user_name );
    }
    else
    {
      if( !strncmp( user_role, ROLE_ADMIN, sizeof( ROLE_ADMIN ) ) )
        sprintf( syscom, "passwd %s", opt_name );
      else
        sprintf( syscom, "echo \"%s: Only users with role='%s' can change other user's password.\"",
                                "passwd",                  ROLE_ADMIN );
    }

    return( system( syscom ) );
  }

  return( 0 );
}


static void useradd_usage( char *fname )
{
  fprintf( stdout, "\n" );
  fprintf( stdout, "Usage: %s [options] [username]\n", fname );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Delete user.\n" );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Options:\n" );
  fprintf( stdout, "  -h,--help    Display this information.\n" );
  fprintf( stdout, "  -n,--name    User Name.\n" );
  fprintf( stdout, "  -r,--role    User Role (default: %s).\n", ROLE_USER );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Parameter:\n" );
  fprintf( stdout, "  [username]   User name (if not set by -n option).\n" );
  fprintf( stdout, "\n" );
}

int com_useradd( char *arg )
{
  split_args( "useradd", arg );

  get_args( argc, (char * const *)&argv[0] );

  if( opt_help )
  {
    useradd_usage( "useradd" );
  }
  else
  {
    /* check args */
    if( !opt_name[0] || !opt_role[0] )
    {
      useradd_usage( "useradd" );
    }
    else
    {
      /* temporary STUB. Should use busctl or REST */
      sprintf( syscom, "echo 'useradd --name %s --role %s (command is not implemented yet).'", opt_name, opt_role );
      return( system( syscom ) );
    }
  }

  return( 0 );

}


static void userdel_usage( char *fname )
{
  fprintf( stdout, "\n" );
  fprintf( stdout, "Usage: %s [options] [username]\n", fname );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Delete user.\n" );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Options:\n" );
  fprintf( stdout, "  -h,--help    Display this information.\n" );
  fprintf( stdout, "  -n,--name    User Name.\n" );
  fprintf( stdout, "\n" );
  fprintf( stdout, "Parameter:\n" );
  fprintf( stdout, "  [username]   User name (if not set by -n option).\n" );
  fprintf( stdout, "\n" );
}

int com_userdel( char *arg )
{
  split_args( "userdel", arg );

  get_args( argc, (char * const *)&argv[0] );

  if( opt_help )
  {
    userdel_usage( "userdel" );
  }
  else
  {
    /* check args */
    if( !opt_name[0] )
    {
      useradd_usage( "userdel" );
    }
    else
    {
      /* temporary STUB. Should use busctl or REST */
      sprintf( syscom, "echo 'userdel --name %s (command is not implemented yet).'", opt_name );
      return( system( syscom ) );
    }
  }

  return( 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" );
}

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;
    }
  }
 */

  get_args( argc, (char * const *)&argv[0] );

  if( 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 );
}