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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <bsm/audit.h>
#include <bsm/audit_record.h>
#include <bsm/audit_uevents.h>
#include <bsm/libbsm.h>
#include "generic.h"
#include <netinet/in.h>
#include <netdb.h>
#include <pwd.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#include <syslog.h>
#ifdef C2_DEBUG
#define dprintf(x) { printf x; }
#else
#define dprintf(x)
#endif
/* constant for user account enable/disable state change */
#define AC_STATE_UNCHANGED -99
/* Constants used for password type interpretation in BSM auditing */
#define PWD_NONE_CODE 0
#define PWD_CLEARED_CODE 1
#define PWD_LOCKED_CODE 2
#define PWD_NORMAL_CODE 3
#define PWD_UNCHANGED_CODE 4
#define PWD_NONE_TEXT "No password active"
#define PWD_CLEARED_TEXT "Cleared until first login"
#define PWD_LOCKED_TEXT "Account is locked"
#define PWD_NORMAL_TEXT "Normal password active"
static int save_afunc();
static char *saved_uid_p;
static char *saved_username_p;
static char *saved_gid_p;
static char *saved_groups_p;
static char *saved_shell_p;
static char *saved_min_p;
static char *saved_max_p;
static char *saved_inactive_p;
static char *saved_expire_p;
static char *saved_warn_p;
static char *saved_home_path_p;
static char *saved_home_server_p;
static char *saved_home_mode_p;
static int saved_passwd_type_code;
#define String_max 511
static char adm_name[String_max + 1];
static uid_t adm_uid;
static uid_t adm_euid;
static char adm_host[String_max + 1];
static uint32_t adm_session_id;
static int taudit_user_dde_event_setup(au_event_t, char *);
static int audit_user_generic(int);
static int audit_users_modified_by_group_generic(char *, char *, int);
static void admin_auth_init(char *, char *);
static void admin_record(int, char *, au_event_t);
static int admin_selected(int, au_event_t);
/*
* Save user information to audit log as text tokens
*/
static int
save_afunc(int ad)
{
char *local_passwd_type_string;
/* Work out the password type display string */
switch (saved_passwd_type_code) {
case PWD_CLEARED_CODE:
local_passwd_type_string = PWD_CLEARED_TEXT;
break;
case PWD_LOCKED_CODE:
local_passwd_type_string = PWD_LOCKED_TEXT;
break;
case PWD_NORMAL_CODE:
local_passwd_type_string = PWD_NORMAL_TEXT;
break;
case PWD_NONE_CODE:
local_passwd_type_string = PWD_NONE_TEXT;
break;
case PWD_UNCHANGED_CODE:
local_passwd_type_string = NULL;
break;
default:
/* Never reached, but if it is report as if none */
/* to flag a potential hole in security */
local_passwd_type_string = PWD_NONE_TEXT;
break;
}
if (saved_uid_p != NULL) {
(void) au_write(ad, au_to_text(saved_uid_p));
}
if (saved_username_p != NULL) {
(void) au_write(ad, au_to_text(saved_username_p));
}
if (saved_gid_p != NULL) {
(void) au_write(ad, au_to_text(saved_gid_p));
}
if (saved_groups_p != NULL) {
(void) au_write(ad, au_to_text(saved_groups_p));
}
if (saved_shell_p != NULL) {
(void) au_write(ad, au_to_text(saved_shell_p));
}
if (local_passwd_type_string != NULL) {
(void) au_write(ad, au_to_text(local_passwd_type_string));
}
if (saved_min_p != NULL) {
(void) au_write(ad, au_to_text(saved_min_p));
}
if (saved_max_p != NULL) {
(void) au_write(ad, au_to_text(saved_max_p));
}
if (saved_inactive_p != NULL) {
(void) au_write(ad, au_to_text(saved_inactive_p));
}
if (saved_expire_p != NULL) {
(void) au_write(ad, au_to_text(saved_expire_p));
}
if (saved_warn_p != NULL) {
(void) au_write(ad, au_to_text(saved_warn_p));
}
if (saved_home_path_p != NULL) {
(void) au_write(ad, au_to_text(saved_home_path_p));
}
if (saved_home_server_p != NULL) {
(void) au_write(ad, au_to_text(saved_home_server_p));
}
if (saved_home_mode_p != NULL) {
(void) au_write(ad, au_to_text(saved_home_mode_p));
}
return (0);
}
/*
* Set up data for audit of user Delete/Disable or Enable Event
*/
int
audit_user_dde_event_setup(char *uid_p)
{
return (taudit_user_dde_event_setup(AUE_delete_user, uid_p));
}
static int
taudit_user_dde_event_setup(au_event_t id, char *uid_p)
{
dprintf(("taudit_user_dde_event_setup()\n"));
if (cannot_audit(0)) {
return (0);
}
(void) aug_init();
aug_save_event(id);
aug_save_text(uid_p);
(void) aug_save_me();
return (0);
}
/*
* Audit successful or failed user create
*/
int
audit_user_create_event(char *uid_p,
char *username_p,
char *gid_p,
char *groups_p,
char *shell_p,
char *min_p,
char *max_p,
char *inactive_p,
char *expire_p,
char *warn_p,
char *home_path_p,
char *home_server_p,
char *home_mode_p,
int passwd_type_code,
int ac_disabled,
int status)
{
dprintf(("audit_user_create_event()\n"));
if (cannot_audit(0)) {
return (0);
}
saved_uid_p = uid_p;
saved_username_p = username_p;
saved_gid_p = gid_p;
saved_groups_p = groups_p;
saved_shell_p = shell_p;
saved_min_p = min_p;
saved_max_p = max_p;
saved_inactive_p = inactive_p;
saved_expire_p = expire_p;
saved_warn_p = warn_p;
saved_home_path_p = home_path_p;
saved_home_server_p = home_server_p;
saved_home_mode_p = home_mode_p;
saved_passwd_type_code = passwd_type_code;
(void) aug_init();
aug_save_event(AUE_create_user);
(void) aug_save_me();
aug_save_afunc(save_afunc);
if (status != 0) {
(void) audit_user_generic(-1);
} else {
(void) audit_user_generic(0);
}
if (ac_disabled != AC_STATE_UNCHANGED) {
if (ac_disabled) {
(void) taudit_user_dde_event_setup(AUE_disable_user,
saved_uid_p);
} else {
(void) taudit_user_dde_event_setup(AUE_enable_user,
saved_uid_p);
}
if (status != 0) {
(void) audit_user_generic(-1);
} else {
(void) audit_user_generic(0);
}
}
return (0);
}
/*
* Audit user modification
*/
int
audit_user_modify_event(char *uid_p,
char *username_p,
char *gid_p,
char *groups_p,
char *shell_p,
char *min_p,
char *max_p,
char *inactive_p,
char *expire_p,
char *warn_p,
char *home_path_p,
char *home_server_p,
int passwd_type_code,
int ac_disabled,
int status)
{
dprintf(("audit_user_modify_event()\n"));
if (cannot_audit(0)) {
return (0);
}
saved_uid_p = uid_p;
saved_username_p = username_p;
saved_gid_p = gid_p;
saved_groups_p = groups_p;
saved_shell_p = shell_p;
saved_min_p = min_p;
saved_max_p = max_p;
saved_inactive_p = inactive_p;
saved_expire_p = expire_p;
saved_warn_p = warn_p;
saved_home_path_p = home_path_p;
saved_home_server_p = home_server_p;
saved_home_mode_p = NULL;
saved_passwd_type_code = passwd_type_code;
(void) aug_init();
aug_save_event(AUE_modify_user);
(void) aug_save_me();
aug_save_afunc(save_afunc);
if (status != 0) {
(void) audit_user_generic(-1);
} else {
(void) audit_user_generic(0);
}
if (ac_disabled != AC_STATE_UNCHANGED) {
if (ac_disabled) {
(void) taudit_user_dde_event_setup(AUE_disable_user,
saved_uid_p);
} else {
(void) taudit_user_dde_event_setup(AUE_enable_user,
saved_uid_p);
}
if (status != 0) {
(void) audit_user_generic(-1);
} else {
(void) audit_user_generic(0);
}
}
return (0);
}
int
audit_delete_user_fail()
{
return (audit_user_generic(-1));
}
int
audit_delete_user_success()
{
return (audit_user_generic(0));
}
static int
audit_user_generic(int sorf)
{
dprintf(("audit_user_generic(%d)\n", sorf));
if (cannot_audit(0)) {
return (0);
}
aug_save_sorf(sorf);
(void) aug_audit();
return (0);
}
int
audit_users_modified_by_group_success(char *unique_members, char *ID)
{
return (audit_users_modified_by_group_generic(unique_members, ID, 0));
}
int
audit_users_modified_by_group_fail(char *members, char *ID)
{
return (audit_users_modified_by_group_generic(members, ID, -1));
}
static int
audit_users_modified_by_group_generic(char *member_list, char *ID, int sorf)
{
char *member_start;
char *member_finish;
int member_len;
char *member;
member_start = member_list;
member_finish = member_list;
while (member_finish != NULL) {
member_finish = strchr(member_start, ',');
if (member_finish == NULL) {
(void) audit_user_modify_event(NULL,
member_start,
ID,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
PWD_UNCHANGED_CODE,
AC_STATE_UNCHANGED,
sorf);
}
else
{
member_len = member_finish - member_start;
member = (char *)malloc(member_len + 1);
if (member != NULL) {
(void) strncpy(member, member_start,
member_len);
member[member_len] = '\0';
(void) audit_user_modify_event(NULL,
member,
ID,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
PWD_UNCHANGED_CODE,
AC_STATE_UNCHANGED,
sorf);
free(member);
}
member_start = member_finish + 1;
}
}
return (0);
}
/*
* Record result of administrator authentication
*/
int
audit_admin_auth_fail(char *user_name, char *host, int fail_status)
{
if (cannot_audit(0)) {
return (0);
}
adm_session_id = 0;
admin_auth_init(user_name, host);
if (fail_status == -1) {
admin_record(1, dgettext(bsm_dom, "invalid user name"),
AUE_admin_authenticate);
} else {
admin_record(2, dgettext(bsm_dom, "invalid password"),
AUE_admin_authenticate);
}
return (0);
}
int
audit_admin_auth_success(char *user_name, char *host, uint32_t session_id)
{
if (cannot_audit(0)) {
return (0);
}
admin_auth_init(user_name, host);
adm_session_id = session_id;
admin_record(0, dgettext(bsm_dom, "successful authentication"),
AUE_admin_authenticate);
return (0);
}
static
void
admin_auth_init(char *user_name, char *host)
{
struct passwd *pwd;
adm_uid = getuid();
(void) strncpy(adm_name, user_name, sizeof (adm_name) - 1);
pwd = getpwnam(user_name);
if (pwd == NULL) {
adm_name[0] = '\0';
adm_euid = -1;
} else {
adm_name[String_max] = '\0';
adm_euid = pwd->pw_uid;
}
(void) strncpy(adm_host, host, sizeof (adm_host) - 1);
adm_host[String_max] = '\0';
}
static void
admin_record(int type, char *string, au_event_t event)
{
int ad, rc;
pid_t pid;
au_tid_addr_t tid;
rc = (type == 0) ? 0 : -1;
if (!admin_selected(rc, event))
return;
pid = getpid();
(void) aug_get_machine(adm_host, &(tid.at_addr[0]), &(tid.at_type));
tid.at_port = 0; /* not known */
ad = au_open();
/*
* to be consistent with admin_login, use uid, not gid...
*/
(void) au_write(ad, au_to_subject_ex(adm_euid, adm_uid,
adm_uid, adm_uid, adm_uid,
pid, adm_session_id, &tid));
(void) au_write(ad, au_to_text(string));
/*
* rc and type are reversed from how login works, but
* the output from praudit is correct for this code
* and wrong for login.
*/
#ifdef _LP64
(void) au_write(ad, au_to_return64((int64_t)rc, type));
#else
(void) au_write(ad, au_to_return32((int32_t)rc, type));
#endif
rc = au_close(ad, AU_TO_WRITE, event);
if (rc < 0) {
openlog("BSM-adminsuite", LOG_PID | LOG_CONS, LOG_AUTH);
(void) setlogmask(LOG_UPTO(LOG_ALERT));
syslog(LOG_ALERT, "au_close call failed: %m");
closelog();
}
}
static
int
admin_selected(int sf, au_event_t event)
{
int rc, sorf;
char naflags[String_max + 1];
struct au_mask mask;
mask.am_success = mask.am_failure = 0;
if (adm_euid < 0) { /* get non-attrib flags */
rc = getacna(naflags, sizeof (naflags) - 1);
if (rc) {
return (rc); /* don't audit if error */
}
rc = getauditflagsbin(naflags, &mask);
} else {
rc = au_user_mask(adm_name, &mask);
}
if (rc != 0) {
return (0); /* audit if error */
}
if (sf == 0) {
sorf = AU_PRS_SUCCESS;
} else {
sorf = AU_PRS_FAILURE;
}
rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
return (rc);
}
|