[1] | 1 | /*
|
---|
| 2 | * File: sh_userfiles.c
|
---|
| 3 | * Desc: A module for Samhain; adds files in user directories to the check list
|
---|
| 4 | * Auth: Jerry Connolly <jerry.connolly@eircom.net>
|
---|
| 5 | */
|
---|
| 6 | /* This program is free software; you can redistribute it */
|
---|
| 7 | /* and/or modify */
|
---|
| 8 | /* it under the terms of the GNU General Public License as */
|
---|
| 9 | /* published by */
|
---|
| 10 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 11 | /* (at your option) any later version. */
|
---|
| 12 | /* */
|
---|
| 13 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 14 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 15 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 16 | /* GNU General Public License for more details. */
|
---|
| 17 | /* */
|
---|
| 18 | /* You should have received a copy of the GNU General Public License */
|
---|
| 19 | /* along with this program; if not, write to the Free Software */
|
---|
| 20 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 21 |
|
---|
| 22 | #include "config_xor.h"
|
---|
| 23 |
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 | #include <ctype.h>
|
---|
| 27 | #include <limits.h>
|
---|
| 28 | #include <sys/types.h>
|
---|
| 29 | #include <sys/stat.h>
|
---|
| 30 | #include <pwd.h>
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | #include "samhain.h"
|
---|
| 34 | #include "sh_modules.h"
|
---|
| 35 | #include "sh_userfiles.h"
|
---|
| 36 | #include "sh_utils.h"
|
---|
| 37 | #include "sh_schedule.h"
|
---|
| 38 | #include "sh_error.h"
|
---|
| 39 | #include "sh_hash.h"
|
---|
| 40 | #include "sh_files.h"
|
---|
[111] | 41 | #define SH_NEED_PWD_GRP 1
|
---|
| 42 | #include "sh_static.h"
|
---|
[1] | 43 |
|
---|
| 44 | #ifdef SH_USE_USERFILES
|
---|
| 45 |
|
---|
| 46 | #define FIL__ _("sh_userfiles.c")
|
---|
| 47 |
|
---|
| 48 | /* We won't want to build this into yule */
|
---|
| 49 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
| 50 |
|
---|
| 51 | static int ShUserfilesActive = S_TRUE;
|
---|
| 52 |
|
---|
| 53 | struct userfileslist {
|
---|
| 54 | char filename[PATH_MAX];
|
---|
| 55 | int level;
|
---|
| 56 |
|
---|
| 57 | struct userfileslist *next;
|
---|
| 58 | };
|
---|
| 59 |
|
---|
| 60 | struct userhomeslist {
|
---|
| 61 | char *pw_dir;
|
---|
| 62 |
|
---|
| 63 | struct userhomeslist *next;
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | struct useruidlist {
|
---|
| 67 | unsigned long lower;
|
---|
| 68 | unsigned long upper;
|
---|
| 69 | struct useruidlist *next;
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | static struct userfileslist *userFiles = NULL;
|
---|
| 73 | static struct userhomeslist *userHomes = NULL;
|
---|
| 74 | static struct useruidlist *userUids = NULL;
|
---|
| 75 |
|
---|
| 76 | static void sh_userfiles_free_fileslist(struct userfileslist *head);
|
---|
| 77 | static void sh_userfiles_free_homeslist(struct userhomeslist *head);
|
---|
| 78 | static void sh_userfiles_free_uidslist (struct useruidlist *head);
|
---|
| 79 |
|
---|
| 80 | sh_rconf sh_userfiles_table[] = {
|
---|
| 81 | {
|
---|
| 82 | N_("userfilesname"),
|
---|
| 83 | sh_userfiles_add_file,
|
---|
| 84 | },
|
---|
| 85 | {
|
---|
| 86 | N_("userfilesactive"),
|
---|
| 87 | sh_userfiles_set_active,
|
---|
| 88 | },
|
---|
| 89 | {
|
---|
| 90 | N_("userfilescheckuids"),
|
---|
| 91 | sh_userfiles_set_uid,
|
---|
| 92 | },
|
---|
| 93 | {
|
---|
| 94 | NULL,
|
---|
| 95 | NULL
|
---|
| 96 | }
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | static int sh_userfiles_check_uid (unsigned long uid)
|
---|
| 100 | {
|
---|
| 101 | struct useruidlist * uids = userUids;
|
---|
| 102 |
|
---|
| 103 | /* default is to include all
|
---|
| 104 | */
|
---|
| 105 | if (userUids == NULL)
|
---|
| 106 | return 1;
|
---|
| 107 |
|
---|
| 108 | while (uids)
|
---|
| 109 | {
|
---|
| 110 | if ((uids->upper != 0) && (uid >= uids->lower) && (uid <= uids->upper))
|
---|
| 111 | return 1;
|
---|
| 112 | if ((uids->upper == 0) && (uid == uids->lower))
|
---|
| 113 | return 1;
|
---|
| 114 | uids = uids->next;
|
---|
| 115 | }
|
---|
| 116 | return 0;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[68] | 119 | int sh_userfiles_set_uid (const char * str)
|
---|
[1] | 120 | {
|
---|
| 121 | char * end;
|
---|
[68] | 122 | const char * p = str;
|
---|
[1] | 123 | unsigned long lower;
|
---|
| 124 | unsigned long upper = 0;
|
---|
| 125 | struct useruidlist * uids;
|
---|
| 126 |
|
---|
| 127 | while ((p != NULL) && (*p != '\0'))
|
---|
| 128 | {
|
---|
| 129 | lower = strtoul(p, &end, 10);
|
---|
| 130 | if ( (lower == ULONG_MAX) || (end == p))
|
---|
| 131 | return -1;
|
---|
| 132 | p = end;
|
---|
| 133 | if (*p == '-')
|
---|
| 134 | {
|
---|
| 135 | ++p;
|
---|
| 136 | if (*p == '\0')
|
---|
| 137 | {
|
---|
| 138 | upper = ULONG_MAX;
|
---|
| 139 | p = NULL;
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
| 143 | upper = strtoul(p, &end, 10);
|
---|
| 144 | if ( (upper == ULONG_MAX) || (end == p))
|
---|
| 145 | return -1;
|
---|
| 146 | p = end;
|
---|
| 147 | if ( (*p != ',') && (*p != '\0'))
|
---|
| 148 | return -1;
|
---|
| 149 | if (*p != '\0')
|
---|
| 150 | ++p;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | else if (*p == '\0')
|
---|
| 154 | {
|
---|
| 155 | upper = 0;
|
---|
| 156 | p = NULL;
|
---|
| 157 | }
|
---|
| 158 | else if ((*p == ',') || (*p == ' ') || (*p == '\t'))
|
---|
| 159 | {
|
---|
| 160 | upper = 0;
|
---|
| 161 | ++p;
|
---|
| 162 | }
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | upper = strtoul(p, &end, 10);
|
---|
| 166 | if ( (upper == ULONG_MAX) || (end == p))
|
---|
| 167 | return -1;
|
---|
| 168 | p = end;
|
---|
| 169 | if ( (*p != ',') && (*p != ' ') && (*p != '\t') && (*p != '\0') )
|
---|
| 170 | return -1;
|
---|
| 171 | if (*p != '\0')
|
---|
| 172 | ++p;
|
---|
| 173 | }
|
---|
| 174 | uids = SH_ALLOC(sizeof(struct useruidlist));
|
---|
| 175 | uids->lower = lower;
|
---|
| 176 | uids->upper = upper;
|
---|
| 177 | uids->next = userUids;
|
---|
| 178 | userUids = uids;
|
---|
| 179 | /* fprintf(stderr, "range %lu %lu\n", lower, upper); */
|
---|
| 180 | }
|
---|
| 181 | return 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /* Add 'c' to the list of files (userFiles) relative to any given HOME
|
---|
| 185 | * directory that should be checked. */
|
---|
| 186 |
|
---|
[68] | 187 | int sh_userfiles_add_file(const char *c) {
|
---|
[1] | 188 | struct userfileslist *new;
|
---|
| 189 | char *s, *orig;
|
---|
| 190 | char *user_filename;
|
---|
| 191 |
|
---|
| 192 | int default_level = SH_LEVEL_NOIGNORE;
|
---|
| 193 | char *separator = " ";
|
---|
| 194 |
|
---|
| 195 | SL_ENTER(_("sh_userfiles_add_file"));
|
---|
| 196 |
|
---|
| 197 | if( c == NULL )
|
---|
| 198 | SL_RETURN(-1, _("sh_userfiles_add_file") );
|
---|
| 199 |
|
---|
| 200 | s = sh_util_strdup(c); /* Maybe c is needed elsewhere */
|
---|
| 201 | orig = s;
|
---|
| 202 |
|
---|
| 203 | user_filename = sh_util_strsep(&s, separator);
|
---|
| 204 |
|
---|
| 205 | if( user_filename == NULL || strlen(user_filename) > PATH_MAX )
|
---|
| 206 | SL_RETURN(-1, _("sh_userfiles_add_file") );
|
---|
| 207 |
|
---|
| 208 | new = SH_ALLOC(sizeof(struct userfileslist));
|
---|
| 209 |
|
---|
| 210 | (void) sl_strlcpy(new->filename, user_filename, PATH_MAX);
|
---|
| 211 | new->next = userFiles;
|
---|
| 212 | userFiles = new;
|
---|
| 213 |
|
---|
| 214 | /* order is important here, since 'log' would match on 'glog'
|
---|
| 215 | * So, compare longest strings first */
|
---|
| 216 | if( s == NULL ) /* The default */ new->level = default_level;
|
---|
| 217 | else if ( strstr(s, "attributes")!= NULL ) new->level = SH_LEVEL_ATTRIBUTES;
|
---|
| 218 | else if ( strstr(s, "allignore") != NULL ) new->level = SH_LEVEL_ALLIGNORE;
|
---|
| 219 | else if ( strstr(s, "noignore") != NULL ) new->level = SH_LEVEL_NOIGNORE;
|
---|
| 220 | else if ( strstr(s, "logfiles") != NULL ) new->level = SH_LEVEL_LOGFILES;
|
---|
| 221 | else if ( strstr(s, "readonly") != NULL ) new->level = SH_LEVEL_READONLY;
|
---|
| 222 | else if ( strstr(s, "loggrow") != NULL ) new->level = SH_LEVEL_LOGGROW;
|
---|
| 223 | else if ( strstr(s, "user0") != NULL ) new->level = SH_LEVEL_USER0;
|
---|
| 224 | else if ( strstr(s, "user1") != NULL ) new->level = SH_LEVEL_USER1;
|
---|
[27] | 225 | else if ( strstr(s, "user2") != NULL ) new->level = SH_LEVEL_USER2;
|
---|
| 226 | else if ( strstr(s, "user3") != NULL ) new->level = SH_LEVEL_USER3;
|
---|
| 227 | else if ( strstr(s, "user4") != NULL ) new->level = SH_LEVEL_USER4;
|
---|
[1] | 228 | else if ( strstr(s, "prelink") != NULL ) new->level = SH_LEVEL_PRELINK;
|
---|
| 229 | else /* The default */ new->level = default_level;
|
---|
| 230 |
|
---|
| 231 | SH_FREE(orig);
|
---|
| 232 |
|
---|
| 233 | SL_RETURN(0, _("sh_userfiles_add_file") );
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[68] | 236 | /* Decide if we're active.
|
---|
| 237 | */
|
---|
| 238 | int sh_userfiles_set_active(const char *c) {
|
---|
[1] | 239 | int value;
|
---|
| 240 |
|
---|
| 241 | SL_ENTER(_("sh_userfiles_set_active"));
|
---|
| 242 | value = sh_util_flagval(c, &ShUserfilesActive);
|
---|
| 243 | SL_RETURN((value), _("sh_userfiles_set_active"));
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /* Build the list of users, then use this to construct the filenames to
|
---|
| 247 | * be checked. */
|
---|
| 248 | int sh_userfiles_init(void) {
|
---|
| 249 | struct passwd *cur_user;
|
---|
| 250 | struct userhomeslist *end;
|
---|
| 251 | struct userhomeslist *new;
|
---|
| 252 | struct userhomeslist *homes;
|
---|
| 253 |
|
---|
| 254 | SL_ENTER(_("sh_userfiles_init"));
|
---|
| 255 |
|
---|
| 256 | /* We need to free anything allocated by the configuration functions if
|
---|
| 257 | * we find that the module is to be left inactive - otherwise _reconf()
|
---|
| 258 | * won't quite work. */
|
---|
| 259 | if( ShUserfilesActive == S_FALSE ) {
|
---|
| 260 | sh_userfiles_free_homeslist(userHomes);
|
---|
| 261 | sh_userfiles_free_fileslist(userFiles);
|
---|
| 262 | userHomes = NULL;
|
---|
| 263 | userFiles = NULL;
|
---|
| 264 | SL_RETURN(-1, _("sh_userfiles_init"));
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /* We build a list in here because the samhain internals want to use
|
---|
| 268 | * getpwent() too */
|
---|
[134] | 269 | SH_MUTEX_LOCK(mutex_pwent);
|
---|
[1] | 270 | /*@-unrecog@*/
|
---|
[111] | 271 | sh_setpwent();
|
---|
[1] | 272 | /*@+unrecog@*/
|
---|
[111] | 273 | while( ( cur_user = /*@-unrecog@*/sh_getpwent()/*@+unrecog@*/ ) != NULL ) {
|
---|
[1] | 274 | int found = 0;
|
---|
| 275 |
|
---|
| 276 | if (0 == sh_userfiles_check_uid( (unsigned long) cur_user->pw_uid))
|
---|
| 277 | continue;
|
---|
| 278 |
|
---|
| 279 | for( end = userHomes; end != NULL; end = end->next ) {
|
---|
| 280 | if( sl_strcmp( end->pw_dir, cur_user->pw_dir) == 0 ) {
|
---|
| 281 | found = 1; /* Found a match, so flag it and stop searching */
|
---|
| 282 | break;
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | if( found == 0 ) {
|
---|
| 287 | /* Didn't find it, so add to the front of the list */
|
---|
| 288 | new = SH_ALLOC(sizeof(struct userhomeslist) );
|
---|
| 289 | new->next = userHomes;
|
---|
| 290 | new->pw_dir = sh_util_strdup(cur_user->pw_dir);
|
---|
| 291 |
|
---|
| 292 | userHomes = new;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
[134] | 295 | sh_endpwent();
|
---|
| 296 | SH_MUTEX_UNLOCK(mutex_pwent);
|
---|
[1] | 297 |
|
---|
| 298 | for (homes = userHomes; homes != NULL; homes = homes->next ) {
|
---|
| 299 | struct userfileslist *file_ptr;
|
---|
| 300 | char filepath[PATH_MAX];
|
---|
| 301 |
|
---|
| 302 | for (file_ptr = userFiles; file_ptr != NULL; file_ptr = file_ptr->next) {
|
---|
| 303 | (void) sl_strncpy(filepath, homes->pw_dir, PATH_MAX);
|
---|
| 304 | (void) sl_strncat(filepath, "/", PATH_MAX);
|
---|
| 305 | (void) sl_strncat(filepath, file_ptr->filename, PATH_MAX);
|
---|
| 306 |
|
---|
| 307 | switch(file_ptr->level) {
|
---|
| 308 | case SH_LEVEL_READONLY:
|
---|
| 309 | (void) sh_files_pushfile_ro(filepath);
|
---|
| 310 | break;
|
---|
| 311 | case SH_LEVEL_LOGFILES:
|
---|
| 312 | (void) sh_files_pushfile_log(filepath);
|
---|
| 313 | break;
|
---|
| 314 | case SH_LEVEL_LOGGROW:
|
---|
| 315 | (void) sh_files_pushfile_glog(filepath);
|
---|
| 316 | break;
|
---|
| 317 | case SH_LEVEL_NOIGNORE:
|
---|
| 318 | (void) sh_files_pushfile_noig(filepath);
|
---|
| 319 | break;
|
---|
| 320 | case SH_LEVEL_ALLIGNORE:
|
---|
| 321 | (void) sh_files_pushfile_allig(filepath);
|
---|
| 322 | break;
|
---|
| 323 | case SH_LEVEL_ATTRIBUTES:
|
---|
| 324 | (void) sh_files_pushfile_attr(filepath);
|
---|
| 325 | break;
|
---|
| 326 | case SH_LEVEL_USER0:
|
---|
| 327 | (void) sh_files_pushfile_user0(filepath);
|
---|
| 328 | break;
|
---|
| 329 | case SH_LEVEL_USER1:
|
---|
| 330 | (void) sh_files_pushfile_user1(filepath);
|
---|
| 331 | break;
|
---|
[27] | 332 | case SH_LEVEL_USER2:
|
---|
| 333 | (void) sh_files_pushfile_user2(filepath);
|
---|
| 334 | break;
|
---|
| 335 | case SH_LEVEL_USER3:
|
---|
| 336 | (void) sh_files_pushfile_user3(filepath);
|
---|
| 337 | break;
|
---|
| 338 | case SH_LEVEL_USER4:
|
---|
| 339 | (void) sh_files_pushfile_user4(filepath);
|
---|
| 340 | break;
|
---|
[1] | 341 | case SH_LEVEL_PRELINK:
|
---|
| 342 | (void) sh_files_pushfile_prelink(filepath);
|
---|
| 343 | break;
|
---|
| 344 | default: /* Should not reach here */
|
---|
| 345 | break;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | SL_RETURN(0, _("sh_userfiles_init"));
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /* This is pretty much NULL; we don't do anything in our checking routine,
|
---|
| 354 | * so we never need to run it. Just use tcurrent to avoid compiler warnings. */
|
---|
| 355 | int sh_userfiles_timer(time_t tcurrent) {
|
---|
| 356 | SL_ENTER(_("sh_userfiles_timer"));
|
---|
| 357 | tcurrent = 0;
|
---|
| 358 | SL_RETURN((int)tcurrent, _("sh_userfiles_timer"));
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | int sh_userfiles_check(void) {
|
---|
| 362 | SL_ENTER(_("sh_userfiles_check"));
|
---|
| 363 | SL_RETURN(0, _("sh_userfiles_check"));
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /* Free our lists and the associated memory */
|
---|
| 367 |
|
---|
| 368 | int sh_userfiles_cleanup(void) {
|
---|
| 369 | SL_ENTER(_("sh_userfiles_cleanup"));
|
---|
| 370 |
|
---|
| 371 | sh_userfiles_free_homeslist(userHomes);
|
---|
| 372 | sh_userfiles_free_fileslist(userFiles);
|
---|
| 373 | sh_userfiles_free_uidslist (userUids);
|
---|
| 374 |
|
---|
| 375 | SL_RETURN(0, _("sh_userfiles_cleanup"));
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /* As with sh_userfiles_cleanup, but in preparation for re-reading the
|
---|
| 379 | * configuration files */
|
---|
| 380 |
|
---|
| 381 | int sh_userfiles_reconf(void) {
|
---|
| 382 | SL_ENTER(_("sh_userfiles_reconf"));
|
---|
| 383 |
|
---|
| 384 | sh_userfiles_free_homeslist(userHomes);
|
---|
| 385 | sh_userfiles_free_fileslist(userFiles);
|
---|
| 386 | sh_userfiles_free_uidslist (userUids);
|
---|
| 387 |
|
---|
| 388 | userHomes = NULL;
|
---|
| 389 | userFiles = NULL;
|
---|
| 390 |
|
---|
| 391 | SL_RETURN(0, _("sh_userfiles_reconf"));
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | /* Recurse to the end of the list and then free the data as we return
|
---|
| 395 | * back up towards the start, making sure to free any strdupped strings
|
---|
| 396 | */
|
---|
| 397 |
|
---|
| 398 | static void sh_userfiles_free_homeslist(struct userhomeslist *head) {
|
---|
| 399 | if( head != NULL ) {
|
---|
| 400 | sh_userfiles_free_homeslist(head->next);
|
---|
| 401 | SH_FREE(head->pw_dir);
|
---|
| 402 | SH_FREE(head);
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /* Recurse to the end of the list and then free the data as we return
|
---|
| 407 | * back up towards the start */
|
---|
| 408 |
|
---|
| 409 | static void sh_userfiles_free_fileslist(struct userfileslist *head) {
|
---|
| 410 | if( head != NULL ) {
|
---|
| 411 | sh_userfiles_free_fileslist(head->next);
|
---|
| 412 | SH_FREE(head);
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /* Recurse to the end of the list and then free the data as we return
|
---|
| 417 | * back up towards the start */
|
---|
| 418 |
|
---|
| 419 | static void sh_userfiles_free_uidslist(struct useruidlist *head) {
|
---|
| 420 | if( head != NULL ) {
|
---|
| 421 | sh_userfiles_free_uidslist(head->next);
|
---|
| 422 | SH_FREE(head);
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | /* #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE) */
|
---|
| 427 | #endif
|
---|
| 428 |
|
---|
| 429 | /* #ifdef SH_USE_USERFILES */
|
---|
| 430 | #endif
|
---|