summaryrefslogtreecommitdiff
path: root/support/dktools/dkwt-ac.c
blob: cf5c13f4b3391b82bc1fb44e8b1d2b89d479ea0e (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dkwt-ac.ctr
*/

/*
Copyright (C) 2011-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dkwt-ac.c The dkwt-ac module.
*/


#line 10 "dkwt-ac.ctr"

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





#line 18 "dkwt-ac.ctr"



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