summaryrefslogtreecommitdiff
path: root/support/dktools/dkwt-ac.ctr
blob: 710076e19e7a3d03d06dd7d5e537bdd2ed469de4 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2011-xxxx
license		=	bsd



%%	module

#include "dk3all.h"
#include "dkt.h"
#include "dkwt.h"



$!trace-include



/**	Job structure for dkwt accounts.
*/
typedef struct {
  dk3_app_t		*app;		/**< Application structure. */
  dkChar const * const	*msg;		/**< Localized message texts. */
  dkChar const * const	*kwnl;		/**< Keywords, not localized. */
  dk3_option_set_t	*opt;		/**< Options set. */
  int			 f_long;	/**< Flag: Long user listing. */
  int			 exval;		/**< Exit status code. */
} DKWT_ACC_J;



/**	Account flag description.
*/
typedef struct {
  DWORD		dw;	/**< Binary flag. */
  size_t	msgi;	/**< Index of description text. */
} DKWT_ACC_FLAG;



/**	Data for the option set.
*/
static dk3_option_t const dkwt_accounts_options[] = {
  { dkT('l'), dkT("long"), 0 },
  { dkT('s'), dkT("short"), 0 }
};

/**	Number of options in the dkt_sort_options array.
*/
static size_t const dkwt_accounts_szoptions =
sizeof(dkwt_accounts_options)/sizeof(dk3_option_t);



/**	Connection of account flags and text array indices.
*/
static const DKWT_ACC_FLAG dkwt_accounts_flags[] = {
  { UF_NORMAL_ACCOUNT, 26 },
  { UF_TEMP_DUPLICATE_ACCOUNT, 27 },
  { UF_WORKSTATION_TRUST_ACCOUNT, 28 },
  { UF_SERVER_TRUST_ACCOUNT, 29 },
  { UF_INTERDOMAIN_TRUST_ACCOUNT, 30 },
  { UF_TRUSTED_FOR_DELEGATION, 23 },
  { UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION, 25 },
  { UF_ACCOUNTDISABLE, 12 },
  { UF_LOCKOUT, 16 },
  { UF_PASSWD_NOTREQD, 14 },
  { UF_PASSWD_CANT_CHANGE, 15 },
  { UF_PASSWORD_EXPIRED, 24 },
  { UF_DONT_EXPIRE_PASSWD, 17 },
  { UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED, 18 },
  { UF_NOT_DELEGATED, 19 },
  { UF_SMARTCARD_REQUIRED, 20 },
  { UF_USE_DES_KEY_ONLY, 21 },
  { UF_DONT_REQUIRE_PREAUTH, 22 },
  { UF_SCRIPT, 11 },
  { UF_HOMEDIR_REQUIRED, 13 },
};



/**	Connection of operator account flags and text array indices.
*/
static const DKWT_ACC_FLAG dkwt_accounts_operator[] = {
  { AF_OP_ACCOUNTS, 33 },
  { AF_OP_SERVER, 34 },
  { AF_OP_COMM, 35 },
  { AF_OP_PRINT, 36 }
};


/**	Initialize job structure.
	@param	j	Job structure.
*/
static
void
dkwt_accounts_job_init(DKWT_ACC_J *j)
{
  j->app = NULL; j->msg = NULL; j->kwnl = NULL; j->opt = NULL;
  j->f_long = 1; j->exval = DKT_RESULT_ERR_UNSPECIFIC;
}




/**	Clean up job structure after use.
	@param	j	Job structure.
*/
static
void
dkwt_accounts_job_cleanup(DKWT_ACC_J *j)
{
  if(j->opt) {
    dk3opt_close(j->opt);
  } j->opt = NULL;
}



/**	Process command line arguments.
	@param	j	Job structure.
	@return	1 on success, 0 on error.
*/
static
int
dkwt_accounts_process_argv(DKWT_ACC_J *j)
{
  int			 back = 0;
  int			 xargc;
  dkChar const * const	*xargv;
  xargc = dk3app_get_argc(j->app);
  xargv = dk3app_get_argv(j->app);
  xargv++; xargv++; xargc--; xargc--;
  j->opt = dk3opt_open_app(
    dkwt_accounts_options,
    dkwt_accounts_szoptions,
    dkT('\0'),
    NULL,
    xargc,
    xargv,
    j->app
  );
  if(j->opt) {
    if(0 == dk3opt_get_error_code(j->opt)) {
      back = 1;
      if(dk3opt_is_set(j->opt, dkT('l'))) {
        j->f_long = 2; 
	if(dk3opt_is_set(j->opt, dkT('s'))) {
	  back = 0;
	  /* ERROR: -l and -s are exclusive. */
	  dk3app_log_1(j->app, DK3_LL_ERROR, j->msg, 75);
	}
      } else {
        if(dk3opt_is_set(j->opt, dkT('s'))) {
	  j->f_long = 0;
	}
      }
      if(dk3opt_get_num_args(j->opt) > 0) {
        /* Warning: Arguments ignored. */
	dk3app_log_1(j->app, DK3_LL_ERROR, j->msg, 76);
      }
    }
  }
  return back;
}



/**	Report one detail about an account.
	@param	msg	Array containing localized texts.
	@param	imsg	Index of attribute name.
	@param	dettxt	Description text.
*/
static
void
dkwt_ac_detail(dkChar const * const *msg, size_t imsg, dkChar const *dettxt)
{
  dk3sf_fputs(msg[imsg], stdout);
  dk3sf_fputc(dkT(' '), stdout);
  dk3sf_fputs(dettxt, stdout);
  dk3sf_fputc(dkT('\n'), stdout);
}



/**	Report user type for account.
	@param	j	Job structure.
	@param	n	Index of user type name in message text array.
*/
static
void
dkwt_accounts_user_type(DKWT_ACC_J *j, size_t n)
{
  dk3sf_fputs((j->msg)[6], stdout);
  dk3sf_fputc(dkT(' '), stdout);
  dk3sf_fputs((j->msg)[n], stdout);
  dk3sf_fputc(dkT('\n'), stdout);
}



/**	Show flags for an account.
	@param	j	Job structure.
	@param	dw	Flag set for account.
	@param	jarr	Private account flag set.
	@param	sarr	Number of inspections.
	@param	itext	Index in text array.
*/
static
void
dkwt_accounts_show_flags(
  DKWT_ACC_J		*j,
  DWORD			 dw,
  DKWT_ACC_FLAG	const	*jarr,
  size_t		 sarr,
  size_t		 itext
)
{
  int			 is_first = 1;
  DKWT_ACC_FLAG const	*fptr;
  size_t		 i;

  fptr = jarr;
  for(i = 0; i < sarr; i++) {
    if((dw) & (fptr->dw)) {
      dk3sf_fputs((j->msg)[(is_first) ? itext : 10], stdout);
      dk3sf_fputc(dkT(' '), stdout);
      dk3sf_fputs((j->msg)[fptr->msgi], stdout);
      dk3sf_fputc(dkT('\n'), stdout);
      is_first = 0;
    }
    fptr++;
  }
}



/**	Show a numeric value.
	@param	j	Job structure.
	@param	imsg	Index in message text array.
	@param	val	Value to show.
*/
static
void
dkwt_ac_numeric(DKWT_ACC_J *j, size_t imsg, DWORD val)
{
  dkChar		b1[64];
  dkChar		b2[DK3_SIZEOF(b1,dkChar)];

#if VERSION_BEFORE_20140716
  dk3sf_sprintf3(b1, dkT("%lu"), (unsigned long)val);
  dk3sf_sprintf3(b2, dkT("(%lx)"), (unsigned long)val);
#else
  if (0 == dk3ma_um_to_string(b1, DK3_SIZEOF(b1,dkChar), (dk3_um_t)val)) {
    b1[0] = dkT('\0');
  }
  if (0 == dk3ma_um_to_hex_string(b2, DK3_SIZEOF(b2,dkChar),(dk3_um_t)val,0)) {
    b2[0] = dkT('\0');
  }
#endif
  dk3sf_fputs((j->msg)[imsg], stdout);
  dk3sf_fputc(dkT(' '), stdout);
  dk3sf_fputs(b1, stdout);
  dk3sf_fputc(dkT(' '), stdout);
  dk3sf_fputs(b2, stdout);
  dk3sf_fputc(dkT('\n'), stdout);
}



/**	Do the accounts listing.
	@param	j	Job structure.
*/
static
void
dkwt_accounts_run(DKWT_ACC_J *j)
{
  dkwt_account_list_t	*al;
  dkwt_account_t	*ac;
  int			 found = 0;
  size_t		 namelgt;
  size_t		 i;
  al = dkwt_tool_open_account_list(j->app, j->f_long);
  if(al) {
    j->exval = DKT_RESULT_OK;
    dk3sf_initialize_stdout();
    dkwt_tool_reset_account_list(al);
    while((ac = dkwt_tool_get_account_from_list(al)) != NULL) {
      if((found) && (j->f_long)) {
        dk3sf_fputc(dkT('\n'), stdout);
      } found = 1;
      if(ac->logname) {
        dk3sf_fputs(ac->logname, stdout);
	dk3sf_fputc(dkT('\n'), stdout);
	if(j->f_long) {
	  namelgt = dk3str_len(ac->logname);
	  for(i = 0; i < namelgt; i++) dk3sf_fputc(dkT('-'), stdout);
	  dk3sf_fputc(dkT('\n'), stdout);
	  if(ac->textsid) {
	    dkwt_ac_detail(j->msg, 5, ac->textsid);
	  }
	  if((j->f_long > 1) && (ac->f_long)) {
	    switch(ac->priv) {
	      case USER_PRIV_GUEST: {
	        dkwt_accounts_user_type(j, 7);
	      } break;
	      case USER_PRIV_USER: {
	        dkwt_accounts_user_type(j, 8);
	      } break;
	      case USER_PRIV_ADMIN: {
	        dkwt_accounts_user_type(j, 9);
	      } break;
	    }
	    dkwt_accounts_show_flags(
	      j,
	      ac->flags,
	      dkwt_accounts_flags,
	      sizeof(dkwt_accounts_flags)/sizeof(DKWT_ACC_FLAG),
	      10
	    );
	    dkwt_accounts_show_flags(
	      j,
	      ac->a_flags,
	      dkwt_accounts_operator,
	      sizeof(dkwt_accounts_operator)/sizeof(DKWT_ACC_FLAG),
	      32
	    );
	  }
	  if(ac->fullname) {
	    dkwt_ac_detail(j->msg, 0, ac->fullname);
	  }
	  if(ac->comment) {
	    dkwt_ac_detail(j->msg, 1, ac->comment);
	  }
	  if(ac->usrcomment) {
	    dkwt_ac_detail(j->msg, 2, ac->usrcomment);
	  }
	  if(ac->homedir) {
	    dkwt_ac_detail(j->msg, 3, ac->homedir);
	  }
	  if(ac->profile) {
	    dkwt_ac_detail(j->msg, 4, ac->profile);
	  }
	  if((j->f_long > 1) && (ac->f_long)) {
	    dkwt_ac_numeric(j, 37, ac->coco);
	    dkwt_ac_numeric(j, 38, ac->codepage);
	  }
	}
      }
    }
    dkwt_tool_close_account_list(al);
  }
}



int
dkwt_accounts(
  dk3_app_t		*app,
  dkChar const * const	*msg,
  dkChar const * const	*kwnl
)
{
  int back = DKT_RESULT_ERR_UNSPECIFIC;
  DKWT_ACC_J j;
  dkwt_accounts_job_init(&j);
  j.app = app; j.msg = msg; j.kwnl = kwnl;
  if(dkwt_accounts_process_argv(&j)) {
    back = j.exval = DKT_RESULT_OK;
    dkwt_accounts_run(&j);
  }
  dkwt_accounts_job_cleanup(&j);
  return back;
}