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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
|
/*****************************************************************************
* Secure Locate v2.4 *
* November 28, 2000 *
* Programmed by: Kevin Lindsay *
* Copyright (c) 1994, 1999, 2000 Kevin Lindsay & Netnation Communications, *
* Inc. & Free Software Foundation, Inc. *
* *
* NetNation Communications http://www.netnation.com/ *
* Secure Locate: ftp://ftp.linux-geek.org/slocate/ *
* ftp://ftp.mkintraweb.com/pub/linux/slocate/ *
* *
* Web: http://www.linux-geek.org/slocate/ *
* *
* Patches: Sean Mcnulty <lazy@ccf.rutgers.edu> *
* * Fixed a bug which caused the decode function to fail. *
* Ulf Betlehem <flu@iki.fi> *
* * multiple databases *
* * LOCATE_PATH environment variable support *
* * -o, --output options *
* Jim Dougharty <james.dougharty@sabre.com> *
* * Recursive directory walk will not exit if a directory *
* * cannot be read. May happen on some NFS directories. *
* Glen Maynard <glennm@mediaone.net> *
* * Multiple search strings are now possible *
* * Uses access() instead of opendir() to see if a directory *
* is readable. *
* R.G. Wood <rgwood@debian.org> *
* * Made a Debian Package for Secure Locate *
* Alexander V. Lukyanov" <lav@long.yar.ru> *
* * Fixed some performance issues that I over looked. Thanx *
* To Alex, slocate -u is much faster! *
* Matt Heckaman <matt@MLINK.NET> *
* * Created a patch to make Secure Locate work with FreeBSD. *
* Luca Berra <bluca@vodka.it> *
* * Added case insensitive option and optimized code to make *
* searching faster. *
* Hans-Juergen Godau <godau@wi-inf.uni-essen.de> *
* * Fixed a segfault when searching through more than one *
* database. *
* *
* Report any Bugs to: klindsay@mkintraweb.com *
* *
* Even though Secure Locate was written from scratch, some recognition goes *
* to James A. Woods <jwoods@adobe.com> for writing the original GNU locate. *
* *
* This product includes software developed by the University of *
* California, Berkeley and its contributors. *
* *
*****************************************************************************/
/*****************************************************************************
*
* Secure Locate -- search database for filenames that match patterns without
* showing files that the user using slocate does not have
* access to.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <grp.h>
#include <ctype.h>
#include <fnmatch.h>
#include <regex.h>
#include <fts.h>
//#include "sl_fts.h"
#include "link.h"
#include "misc.h"
#ifdef __FreeBSD__
# include <sys/param.h>
# include <sys/ucred.h>
# include <sys/mount.h>
# undef FNM_CASEFOLD
#endif
/* GLOBALS */
#define VERSION "Secure Locate v2.4 - Released November 28, 2000\n"
#define GRPFILE "/etc/group"
#define SLOC_ESC 0x80
#define SLOC_GRP "slocate"
#define SLOC_UID 0
#define MIN_BLK 4096
#define MTAB_FILE "/etc/mtab"
/* Warn if a database is older than this. 8 days allows for a weekly
* update that takes up to a day to perform. */
#define WARN_SECONDS (60 * 60 * 24 * 8)
/* Printable version of WARN_SECONDS. */
#define WARN_MESSAGE "8 days"
char **SLOCATE_PATH = NULL;
/* More fitting paths for FreeBSD -matt */
#if defined(__FreeBSD__)
char *SLOCATEDB = "/var/db/slocate/slocate.db";
char *TMPSLOCATEDB = "/var/db/slocate/slocate.db.tmp";
char *SLOCATEDB_DIR = "/var/db/slocate/";
#else
/* These paths are good for Linux machines */
char *SLOCATEDB = "/var/lib/slocate/slocate.db";
char *TMPSLOCATEDB = "/var/lib/slocate/slocate.db.tmp";
char *SLOCATEDB_DIR = "/var/lib/slocate/";
# define UPDATEDB_CONF "/etc/updatedb.conf"
#endif
char *EXCLUDE_DIR=NULL;
int EXCLUDE=0;
int VERBOSE=0;
int QUIET=0;
int NOCASE=0;
int REGEXP=0;
int NEWOUTPUT=0;
char *regexp;
char *progname;
char *tmp_path=NULL;
char *cur_dir=NULL;
char prog_CWD[4096];
short fr_num=0;
int t_num=0;
uid_t UID;
gid_t GID;
int first=1;
char slevel='1';
unsigned short SLOC_GID=65534; /* Seems to be the default nobody gid */
int max_queries=0;
int decode_db(char *database, char *str);
/* Usage */
void
usage()
{
int i;
printf("%s\n"
"Copyright (c) 1994, 1999, 2000 Kevin Lindsay & Netnation Communications Inc. &\n"
" Free Software Foundation, Inc.\n\n"
"search usage: %s [-qi] [-d <path>] [--database=<path>] <search string>...\n"
" %s [-r <regexp>] [--regexp=<regexp>]\n"
"database usage: %s [-qv] [-o <file>] [--output=<file>]\n"
" %s [-e <dir1,dir2,...>] [-f <fs_type1,...> ] [-l <level>]\n"
,VERSION,progname,progname,progname,progname);
for (i = 0; i < strlen(progname)-1; i+=1)
printf(" ");
printf(
#ifndef __FreeBSD__
" [-c] <[-U <path>] [-u]>\n"
#else
" <[-U <path>] [-u]>\n"
#endif
"general usage: %s [-Vh] [--version] [--help]\n\n"
" Options:\n"
" -u - Create slocate database starting at path /.\n"
" -U <dir> - Create slocate database starting at path <dir>.\n",progname);
#ifndef __FreeBSD__
printf(" -c - Parse original GNU Locate's '/etc/updatedb.conf'\n"
" when using the -u or -U options. If 'updatedb' is\n"
" symbolically linked to the '%s' binary, the\n"
" original configuration file will automatically be\n"
" used.\n",progname);
#endif
printf(" -e <dir1,dir2,...> - Exclude directories from the slocate database when\n"
" using the -u or -U options.\n"
" -f <fs_type1,...> - Exclude file system types from the slocate database\n"
" when using the -u or -U options. (ie. NFS, etc).\n"
" -l <level> - Security level. \n"
" 0 turns security checks off. This will make\n"
" searchs faster.\n"
" 1 turns security checks on. This is the default.\n"
" -q - Quiet mode. Error messages are suppressed.\n"
" -n <num> - Limit the amount of results shown to <num>.\n"
" -i - Does a case insensitive search.\n"
" -r <regexp>\n"
" --regexp=<regexp> - Search the database using a basic POSIX regular\n"
" expression.\n"
" -o <file>\n"
" --output=<file> - Specfies the database to create.\n"
" -d <path>\n"
" --database=<path> - Specfies the path of databases to search in.\n"
" -h\n"
" --help - Display this help.\n"
" -v\n"
" --verbose - Verbose mode. Display files when creating database.\n"
" -V\n"
" --version - Display version.\n"
"\n"
"Author: Kevin Lindsay\n"
"Bugs: klindsay@mkintraweb.com\n"
"FTP: ftp://ftp.linux-geek.org/slocate/\n"
" ftp://ftp.mkintraweb.com/pub/linux/slocate/\n"
"HTTP: http://www.linux-geek.org/slocate/\n"
"\n");
exit(0);
}
static void
put_short (int c, FILE *fp)
{
putc (c >> 8, fp);
putc (c, fp);
}
/*static int
get_short (FILE *fp)
{
register short x;
x = fgetc (fp);
return (x << 8) | (fgetc (fp) & 0xff);
} */
static int
get_short (char **fp)
{
register short x;
x = **fp;
/* move pointer one byte ahead */
(*fp)++;
return (x << 8) | (*((*fp)++) & 0xff);
}
/* Validate database file */
int
validate_db(char *database)
{
int fd;
char buf[2];
fd = open(database,O_RDONLY);
if (fd == -1)
return 0;
if (read(fd,buf,2) < 2) {
close(fd);
return 0;
}
if ((buf[0] != '1' && buf[0] != '0') ||
buf[1] != 0) {
close(fd);
return 0;
}
close(fd);
return 1;
}
/* Parse Output Path */
void
parse_create_path(char *path)
{
int ret_val;
if (!path || strlen(path) == 0) return;
ret_val = creat(path, S_IRWXU|S_IRGRP);
if(ret_val==-1)
{
if (!QUIET)
fprintf(stderr,"%s: could not create database: %s: %s\n", progname,path,strerror(errno));
exit(1);
}
SLOCATEDB = malloc(strlen(path)+1);
SLOCATEDB[0] = '\0';
strcat(SLOCATEDB, path);
TMPSLOCATEDB = malloc(strlen(path)+5);
TMPSLOCATEDB[0] = '\0';
strcat(TMPSLOCATEDB, path);
strcat(TMPSLOCATEDB, ".tmp");
}
/* Parse Path */
void
parse_decode_path(char *path)
{
char *pathcopy;
char *part;
int i;
if (!path || strlen(path) == 0) return;
i = 1;
part = path;
while ((part = strchr(part+1, ':'))) i++;
if (!SLOCATE_PATH)
SLOCATE_PATH = malloc((i+1) * sizeof(char *));
else
SLOCATE_PATH = realloc(SLOCATE_PATH,(i+1) * sizeof(char *));
i = 0;
pathcopy = malloc(strlen(path)+1);
pathcopy[0] = '\0';
strcat(pathcopy, path);
part = strtok(pathcopy, ":");
while (part) {
if (validate_db(part))
SLOCATE_PATH[i++] = part;
else if (!QUIET)
fprintf(stderr,"%s: this is not a valid slocate database: %s\n",progname,part);
part = strtok(NULL, ":");
}
SLOCATE_PATH[i] = NULL;
}
/* Parse Dash */
void
parse_dash(char *option)
{
char *ptr;
int i=0;
char *database;
int res=1;
for (ptr = option;*ptr != 0 && *ptr != '=' ; ptr++)
*ptr = toupper(*ptr);
if (!strcmp(option,"HELP"))
usage();
if (!strcmp(option,"VERSION")) {
printf("%s",VERSION);
exit(0);
}
if (!strcmp(option,"VERBOSE"))
VERBOSE=1;
*ptr = '\0';
ptr++;
if (!strcmp(option,"OUTPUT")) {
parse_create_path(ptr);
NEWOUTPUT=1;
}
if (!strcmp(option,"DATABASE")) {
parse_decode_path(ptr);
}
if (!strcmp(option,"REGEXP")) {
REGEXP = 1;
regexp = malloc(strlen(ptr)+1);
*regexp = 0;
strcat(regexp,ptr);
while ((database = SLOCATE_PATH[i++])) res |= decode_db(database, ptr);
exit(!res);
}
}
/* Make sure that the DB Directory exists */
int
check_dir()
{
int res=1;
struct stat tmpstat;
if (lstat(SLOCATEDB_DIR,&tmpstat) == -1) {
if (!QUIET)
fprintf(stderr,"%s: error accessing DB Directory: %s : %s\n",progname,SLOCATEDB_DIR,strerror(errno));
res=0;
}
return(res);
}
/* Get the GID for group slocate */
unsigned short
get_gid()
{
unsigned short GGID=SLOC_GID;
struct group *grpres;
if ((grpres = getgrnam(SLOC_GRP)) == NULL) {
if (!QUIET) {
fprintf(stderr,"%s: Could not find the group: %s in the /etc/group file.\n",progname,SLOC_GRP);
fprintf(stderr,"This is a result of the group missing or a corrupted group file.\n");
}
exit(1);
}
GGID = grpres->gr_gid;
return GGID;
}
/* Parse Exclude Command */
int
parse_exclude(char *estr)
{
char *excludestr=NULL;
char *ptr1;
char *ptr2=NULL;
int grow=1;
int elen;
EXCLUDE=1;
excludestr = (char *)malloc(1);
excludestr[0] = '\0';
elen = strlen(estr);
ptr1 = estr;
ptr2 = ptr1;
while (ptr2[0] != '\0') {
if ((ptr2 = strchr(ptr1,',')) == NULL) {
ptr2 = strchr(ptr1,'\0');
}
grow += (ptr2-ptr1)+2;
if (*(ptr2-1) == '/' && (ptr2-1) != estr && *(ptr2-2) != ',' ) {
grow--;
ptr2--;
}
excludestr = realloc(excludestr,grow);
strcat(excludestr,"*");
strncat(excludestr,ptr1,ptr2-ptr1);
strcat(excludestr,"*");
if (*ptr2 == '/')
ptr2++;
if (ptr2[0] == ',')
ptr1 = ptr2+1;
}
if (!EXCLUDE_DIR) {
EXCLUDE_DIR = malloc(strlen(excludestr)+1);
*EXCLUDE_DIR = 0;
} else {
EXCLUDE_DIR = realloc(EXCLUDE_DIR,strlen(EXCLUDE_DIR)+strlen(excludestr)+1);
}
strcat(EXCLUDE_DIR,excludestr);
free(excludestr);
#ifdef DEBUG
printf("E: %s\n",EXCLUDE_DIR);
#endif
return 1;
}
#ifdef __FreeBSD__
/* Get File System type in the form of a string. "*fstype*" */
char *
get_fs_type(int fs_type)
{
if (fs_type == MOUNT_UFS)
return("*UFS*");
else if (fs_type == MOUNT_NFS)
return("*NFS*");
else if (fs_type == MOUNT_MFS)
return("*MFS*");
else if (fs_type == MOUNT_MSDOS)
return("*MSDOS*");
else if (fs_type == MOUNT_LFS)
return("*LFS*");
else if (fs_type == MOUNT_LOFS)
return("*LOFS*");
else if (fs_type == MOUNT_FDESC)
return("*FDESC*");
else if (fs_type == MOUNT_PORTAL)
return("*PORTAL*");
else if (fs_type == MOUNT_NULL)
return("*NULL*");
else if (fs_type == MOUNT_UMAP)
return("*UMAP*");
else if (fs_type == MOUNT_KERNFS)
return("*KERNFS*");
else if (fs_type == MOUNT_PROCFS)
return("*PROCFS*");
else if (fs_type == MOUNT_AFS)
return("*AFS*");
else if (fs_type == MOUNT_CD9660)
return("*CD9660*");
else if (fs_type == MOUNT_UNION)
return("*UNION*");
else if (fs_type == MOUNT_DEVFS)
return("*DEVFS*");
else if (fs_type == MOUNT_EXT2FS)
return("*EXT2FS*");
else if (fs_type == MOUNT_TFS)
return("*TFS*");
else
return("*NONE*");
}
#endif
/* Parse File System Type Exclusion */
int
parse_fs_exclude(char *estr)
{
char *ptr;
char *newestr=NULL;
int estr_size=0;
/* Uppercase all characters in estr to make parsing easier.
* Also add '*'s around each fs type */
if (estr) {
estr_size = strlen(estr)+2;
newestr = malloc(estr_size+1);
*newestr = 0;
strcat(newestr,"*");
for (ptr = estr; *ptr != 0; ptr += 1) {
*ptr = toupper(*ptr);
if (*ptr == ',') {
estr_size += 1;
newestr = realloc(newestr,estr_size+1);
strcat(newestr,"*");
} else {
strncat(newestr,ptr,1);
}
}
strcat(newestr,"*");
estr = newestr;
}
/* FreeBSD File System Status */
#ifdef __FreeBSD__
{
struct statfs *fs_stat;
long bufsize=4096;
int num_mounts=0;
int i;
char *exclude_str=NULL;
fs_stat = malloc(sizeof(struct statfs));
num_mounts = getfsstat(fs_stat,bufsize,MNT_WAIT);
for (i = 0; i < num_mounts; i+=1) {
if (strstr(estr,get_fs_type(fs_stat[i].f_type))) {
if (!exclude_str) {
exclude_str = malloc(strlen(fs_stat[i].f_mntonname)+1);
*exclude_str = 0;
} else {
exclude_str = realloc(exclude_str,strlen(exclude_str)+strlen(fs_stat[i].f_mntonname)+2);
strcat(exclude_str,",");
}
strcat(exclude_str,fs_stat[i].f_mntonname);
}
}
if (exclude_str)
parse_exclude(exclude_str);
}
#else
/* Linux File System Status */
{
char *fbuf=NULL;
char *head_ptr;
char *tail_ptr;
char *exclude_str=NULL;
char *match_str=NULL;
fbuf = load_file(MTAB_FILE);
if (*fbuf == 0) {
fbuf += 1;
if (!QUIET)
fprintf(stderr,"%s: File System Exclude: Could not open file %s: %s\n",progname,MTAB_FILE,fbuf);
exit(1);
}
head_ptr = fbuf;
while (head_ptr) {
/* find filesystem type */
if ((head_ptr = strchr(head_ptr,' '))) {
head_ptr += 1;
head_ptr = strchr(head_ptr,' ');
}
if (!head_ptr)
continue;
head_ptr += 1;
tail_ptr = strchr(head_ptr,' ');
if (!tail_ptr) {
head_ptr = NULL;
continue;
}
*tail_ptr = 0;
/* Check if file sytem type exists in exclude string */
match_str = realloc(match_str,strlen(head_ptr)+3);
match_str[0] = '*';
match_str[1] = 0;
strcat(match_str,head_ptr);
strcat(match_str,"*");
for (ptr = match_str; *ptr != 0; ptr += 1)
*ptr = toupper(*ptr);
*tail_ptr = ' ';
/* If a match is found, move back a few bytes to locate the path */
if (strstr(estr,match_str)) {
head_ptr -= 2;
while (*head_ptr != ' ' && head_ptr != fbuf)
head_ptr -= 1;
if (head_ptr == fbuf) {
if (!QUIET)
fprintf(stderr,"%s: File System Exclude: (1) corrupt mtab file: %s\n",progname,MTAB_FILE);
exit(1);
}
head_ptr += 1;
if (!(tail_ptr = strchr(head_ptr,' '))) {
if (!QUIET)
fprintf(stderr,"%s: File System Exclude: (2) corrupt mtab file: %s\n",progname,MTAB_FILE);
exit(1);
}
*tail_ptr = 0;
/* Add the path to the exclude string */
if (!exclude_str) {
exclude_str = malloc(strlen(head_ptr)+1);
*exclude_str = 0;
strcat(exclude_str,head_ptr);
} else {
exclude_str = realloc(exclude_str,strlen(exclude_str)+strlen(head_ptr)+2);
strcat(exclude_str,",");
strcat(exclude_str,head_ptr);
}
*tail_ptr = ' ';
}
head_ptr = strchr(head_ptr,'\n');
}
# ifdef DEBUG
printf("=-%s-=\n",exclude_str);
# endif
if (exclude_str)
parse_exclude(exclude_str);
}
#endif
return 1;
}
#ifndef __FreeBSD__
/* Parse updatedb.conf file */
void
parse_updatedb_conf()
{
char *fbuf;
char *head_ptr;
char *tail_ptr;
char *ptr;
char tmp_ch;
char *parse_str=NULL;
int times=0;
char var_type[11];
fbuf = load_file(UPDATEDB_CONF);
if (*fbuf == 0) {
fbuf += 1;
/* if (!QUIET) {
fprintf(stderr,"%s: could not access %s: %s\n",progname,UPDATEDB_CONF,fbuf);
fflush(stderr);
} */
return;
}
for (times = 0; times < 2; times += 1) {
head_ptr = fbuf;
var_type[0] = 0;
if (times == 0)
strcat(var_type,"PRUNEFS");
else
strcat(var_type,"PRUNEPATHS");
if ((head_ptr = strstr(fbuf,var_type))) {
ptr = head_ptr;
while (ptr != fbuf && *ptr != '\n' && *ptr != '#')
ptr -= 1;
if (*ptr != '#') {
while (*head_ptr != '"' && *head_ptr != '\n' && *head_ptr != '\0')
head_ptr += 1;
if (*head_ptr == '"') {
head_ptr += 1;
/* Get each file system from PRUNEFS variable */
while (*head_ptr != '"' && *head_ptr != 0) {
while (*head_ptr != 0 && isspace(*head_ptr))
head_ptr += 1;
if (*head_ptr == 0 || *head_ptr == '"')
break;
tail_ptr = head_ptr;
while (*tail_ptr != 0 && !isspace(*tail_ptr) && *tail_ptr != '"')
tail_ptr += 1;
if (*tail_ptr == 0)
break;
tmp_ch = *tail_ptr;
*tail_ptr = 0;
if (!parse_str) {
parse_str = malloc(strlen(head_ptr)+1);
*parse_str = 0;
} else {
parse_str = realloc(parse_str,strlen(parse_str)+strlen(head_ptr)+2);
strcat(parse_str,",");
}
strcat(parse_str,head_ptr);
*tail_ptr = tmp_ch;
head_ptr = tail_ptr;
}
}
}
}
if (parse_str) {
if (times == 0)
parse_fs_exclude(parse_str);
else
parse_exclude(parse_str);
}
if (parse_str)
free(parse_str);
parse_str = NULL;
}
# ifdef DEBUG
printf("Final Exclude: %s\n",EXCLUDE_DIR);
exit(0);
# endif
}
#endif
/* Check to see if a path matches an excluded one. */
int
match_exclude(char *path,char *filedir) {
int res=0;
char *newstr;
newstr = malloc(strlen(path)+strlen(filedir)+3);
newstr[0] = '\0';
strcat(newstr,"*");
strcat(newstr,path);
strcat(newstr,filedir);
strcat(newstr,"*");
if (strstr(EXCLUDE_DIR,newstr) != NULL)
res=1;
free(newstr);
return res;
}
/* FRCODE - Incremental Encoding algorithm */
int
frcode(FILE *fd,char *dir_path, char *filename)
{
int res=0;
char *cur_path;
char *new_path;
char *ptr1;
char *ptr2;
int i;
cur_path = malloc(strlen(dir_path)+strlen(filename)+1);
cur_path[0]='\0';
strcat(cur_path,dir_path);
strcat(cur_path,filename);
if (VERBOSE)
fprintf(stdout,"%s\n",cur_path);
if (tmp_path != NULL) {
ptr1=cur_path;
ptr2=tmp_path;
for (i=0; ptr1[0] == ptr2[0] && (ptr1[0] != '\0' && ptr2[0] != '\0') ; i++) {
ptr1++;
ptr2++;
}
fr_num = i - t_num;
t_num = t_num + fr_num;
ptr2 = strchr(ptr1,'\0');
new_path=malloc((ptr2-ptr1)+1);
new_path[0] = '\0';
strncat(new_path,ptr1,ptr2-ptr1);
} else {
new_path = malloc(strlen(cur_path)+1);
new_path[0] = '\0';
strcat(new_path,cur_path);
t_num = strlen(new_path);
}
if (tmp_path != NULL)
free(tmp_path);
tmp_path = malloc(strlen(cur_path)+1);
tmp_path[0] = '\0';
strcat(tmp_path,cur_path);
if (fr_num > 127 || fr_num < -127) {
putc (SLOC_ESC, fd);
put_short (fr_num, fd);
} else
putc(fr_num,fd);
fputs(new_path,fd);
putc('\0',fd);
if (first) {
chmod(TMPSLOCATEDB,00600);
// chown(TMPSLOCATEDB,UID,SLOC_GID);
first=0;
}
free(new_path);
free(cur_path);
return res;
}
/* Create Database */
int
create_db(char *dirstr)
{
int res=0;
struct stat statres;
FILE *fd;
FTS *dir;
FTSENT *file;
int i;
char **dirchk;
if (!NEWOUTPUT && UID != SLOC_UID) {
if (!QUIET)
fprintf(stderr,"%s: You are not authorized to create a default slocate database!\n",progname);
return(1);
}
fd = fopen(TMPSLOCATEDB,"w");
putc(slevel,fd);
if (dirstr) {
/* Make sure there are no problems accessing the source directory */
if (lstat(dirstr,&statres) == -1) {
if (!QUIET)
fprintf(stderr,"%s: lstat: d%s/: %s\n",progname,dirstr,strerror(errno));
fclose(fd);
return(1);
}
if (strlen(dirstr) > 1) {
if (dirstr[strlen(dirstr)-1] == '/')
dirstr[strlen(dirstr)-1] = 0;
}
}
else {
dirstr = malloc(2);
*dirstr = 0;
strcat(dirstr,"/");
}
dirchk = malloc(sizeof(char **)*2);
*dirchk = dirstr;
dirchk[1] = NULL;
dir = fts_open(dirchk,FTS_PHYSICAL,NULL);
/* The new FTS() funtionality */
for (i = 0; i > -1; i+=1) {
file = fts_read(dir);
if (!file)
break;
if (file->fts_info != FTS_DP && file->fts_info != FTS_NS) {
if ((EXCLUDE && !match_exclude(file->fts_path,"")) || !EXCLUDE)
frcode(fd,file->fts_path,"");
else {
fts_set(dir,file,FTS_SKIP);
}
}
}
fts_close(dir);
fclose(fd);
chdir(prog_CWD);
rename(TMPSLOCATEDB,SLOCATEDB);
chmod(SLOCATEDB,00640);
// chown(SLOCATEDB,UID,SLOC_GID);
return res;
}
/* Decode Database */
int
decode_db(char *database, char *str)
{
int res = 1;
int fd;
short code_num;
int pathlen=0;
register char ch;
int jump=0;
int first=1;
char *codedpath=NULL;
// char *tmpcodedpath;
char *code_ptr;
int printit=0;
int globflag=0;
char *globptr1;
struct stat statres;
time_t now;
// char *chk1;
// char tmpch;
regex_t *preg=NULL;
char *errbuf=NULL;
int nmatch=32;
regmatch_t pmatch[32];
int cur_queries = 0;
int reg_res;
int foundit = 0;
int bytes = -1;
int ptr_offset;
char one_char[1];
char *begin_ptr;
int begin_offset=0;
int tot_size = MIN_BLK;
int cur_size;
int code_tot_size = MIN_BLK;
#ifndef FNM_CASEFOLD
char *casestr=NULL;
char *casecodedpath=NULL;
char *cp=NULL;
#endif
char *bucket_of_holding=NULL;
if ((fd = open(database,O_RDONLY)) == -1) {
if (!QUIET)
fprintf(stderr,"%s: decode_db(): %s: %s\n",progname,database,strerror(errno));
return(0);
}
lstat(database,&statres);
if (S_ISDIR(statres.st_mode)) {
if (!QUIET)
fprintf(stderr,"%s: decode_db(): %s is a directory\n",progname,database);
return(0);
}
time(&now);
if (now - statres.st_mtime > WARN_SECONDS)
{
fprintf(stderr,"%s: warning: database %s' is more than %s old\n",progname,database,WARN_MESSAGE);
}
// slevel = getc(fd);
read(fd,one_char,1);
slevel = *one_char;
codedpath = malloc(MIN_BLK);
*codedpath = 0;
code_ptr = codedpath;
if ((globptr1 = strchr(str,'*')) != NULL ||
(globptr1 = strchr(str,'?')) != NULL ||
((globptr1 = strchr(str,'[')) != NULL && strchr(str,']') != NULL))
globflag = 1;
if (REGEXP) {
preg = malloc(sizeof(regex_t));
if ((reg_res = regcomp(preg,regexp,NOCASE?REG_ICASE:0)) != 0) {
regerror(reg_res, preg, errbuf,1024);
if (!QUIET)
fprintf(stderr,"error: %s: regular expression: %s\n",progname,errbuf);
exit(1);
}
#ifndef FNM_CASEFOLD
} else if (NOCASE) {
casestr=strdup(str);
for (cp = casestr; *cp; cp++)
*cp = tolower(*cp);
#endif /* FNM_CASEFOLD */
}
bucket_of_holding = malloc(MIN_BLK);
*bucket_of_holding = 0;
begin_ptr = bucket_of_holding;
tot_size = MIN_BLK;
cur_size = 0;
while (first || begin_ptr < bucket_of_holding+cur_size) {
/* No 1 byte reads! */
if (cur_size + MIN_BLK > tot_size) {
while (cur_size + MIN_BLK > tot_size)
tot_size <<= 1;
begin_offset = begin_ptr - bucket_of_holding;
bucket_of_holding = realloc(bucket_of_holding,tot_size);
begin_ptr = bucket_of_holding + begin_offset;
}
if (bytes != 0)
bytes = read(fd,bucket_of_holding+cur_size,MIN_BLK-1);
if (bytes == -1) {
fprintf(stderr,"%s: decode_db(): read: %s\n",progname,strerror(errno));
exit(1);
}
cur_size += bytes;
code_num = *begin_ptr;
begin_ptr += 1;
if (code_num == SLOC_ESC)
code_num = get_short(&begin_ptr);
else if (code_num > 127)
code_num = code_num - 256;
/* FIXME sometimes pathlen is < 0 but it shouldn't be.
* corrupt database file?
* This could be from a bug in frcode() or decode_db(). I
* am leaning toward frcode() at the moment */
code_ptr += code_num;
pathlen = code_ptr - codedpath;
if (pathlen < 0) {
fprintf(stderr,"%s: decode_db() aborted. Corrupt database?\n",progname);
exit(1);
}
jump = 0;
while (!jump) {
ch = *begin_ptr;
begin_ptr++;
pathlen++;
if (pathlen > code_tot_size) {
code_tot_size = pathlen * 2;
ptr_offset = code_ptr - codedpath;
codedpath = realloc(codedpath,code_tot_size);
code_ptr = codedpath+ptr_offset;
}
*(codedpath+(pathlen-1)) = ch;
if (!ch)
jump = 1;
/* FIXME: Handle if begin_ptr runs past buffer */
if (begin_ptr > bucket_of_holding+cur_size-1 && bytes) {
fprintf(stderr,"slocate fluky bug found!\n");
fprintf(stderr,"Ack! This shouldn't happen unless you have a path over 4096!\n");
fprintf(stderr,"Report this as a bug to klindsay@mkintraweb.com\n");
exit(1);
}
}
// printf("%s\n",codedpath);
if (first) {
code_ptr = code_ptr+strlen(codedpath);
first=0;
}
pathlen--;
printit=0;
if (REGEXP)
foundit = !regexec(preg,codedpath,nmatch,pmatch,0);
else if (NOCASE) {
#ifdef FNM_CASEFOLD /* i suppose i also have strcasestr */
if (globflag)
foundit =! fnmatch(str,codedpath,FNM_CASEFOLD);
else
foundit = (strcasestr(codedpath,str) != NULL);
#else /* FNM_CASEFOLD */
casecodedpath=strdup(codedpath);
for (cp = casecodedpath; *cp; cp++)
*cp = tolower(*cp);
if (globflag)
foundit =! fnmatch(casestr,casecodedpath,0);
else
foundit = (strstr(casecodedpath,casestr) != NULL);
free(casecodedpath);
#endif /* FNM_CASEFOLD */
} else {
if (globflag)
foundit =! fnmatch(str,codedpath,0);
else
foundit=(strstr(codedpath,str) != NULL);
}
if (foundit) {
if (slevel == '1') {
if (UID == 0 || access(codedpath, F_OK) == 0) {
//if (UID == 0 || lstat(codedpath,&statres) == 0) {
printit = 1;
/* chk1 = codedpath+1;
while ((chk1 = strchr(chk1,'/')) != NULL) {
chk1++;
tmpch=*chk1;
*chk1='\0';
if (access(codedpath, R_OK) == -1)
printit=0;
*chk1=tmpch;
} */
}
} else
printit=1;
}
if (printit) {
res = 0;
cur_queries += 1;
printf("%s\n",codedpath);
if (max_queries > 0 && cur_queries == max_queries)
exit(0);
}
}
if (REGEXP)
regfree(preg);
#ifndef FNM_CASEFOLD
else if (NOCASE)
free(casestr);
#endif /* FNM_CASEFOLD */
close(fd);
return(res);
}
/* Main Function */
int
main(int args, char **argv)
{
int res=0;
int ch;
extern char *optarg;
extern int optind, opterr, optopt;
char *p;
int SPECDIR=0;
int ROOTDIR=0;
char *spec_dir=NULL;
char *database;
int i=0;
progname = ((p = strrchr(argv[0],'/')) ? p+1 : *argv);
if (!strcmp(progname,"updatedb")) {
ROOTDIR=1;
#ifndef __FreeBSD__
parse_updatedb_conf();
#endif
}
/* Get Current Working Directory */
getcwd(prog_CWD, 4095);
if (strlen(prog_CWD) == 4095) {
fprintf(stderr,"%s: Current Working Directory is too large!",progname);
exit(1);
}
if (args < 2 && !ROOTDIR)
usage();
if (!check_dir())
return(1);
UID = getuid();
GID = getgid();
parse_decode_path(SLOCATEDB);
parse_decode_path(getenv("LOCATE_PATH"));
while ((ch = getopt(args,argv,"VvuhqU:r:o:e:l:d:-:n:f:ci")) != EOF) {
switch(ch) {
case 'h':
usage();
break;
case 'q':
QUIET=1;
break;
case 'V':
printf("%s",VERSION);
exit(0);
break;
case 'v':
VERBOSE=1;
break;
case 'e':
parse_exclude(optarg);
break;
case 'f':
parse_fs_exclude(optarg);
break;
case 'c':
#ifndef __FreeBSD__
parse_updatedb_conf();
#endif
break;
case 'l':
slevel = optarg[0];
if (slevel != '0' && slevel != '1') {
if (!QUIET)
fprintf(stderr,"%s: ERROR: Security level must be 0 or 1.\n",progname);
exit(1);
}
break;
case 'u':
ROOTDIR=1;
break;
case 'U':
ROOTDIR=0;
SPECDIR=1;
spec_dir = malloc(strlen(optarg)+1);
*spec_dir = 0;
strcat(spec_dir,optarg);
break;
case 'r':
REGEXP = 1;
regexp = malloc(strlen(optarg)+1);
*regexp = 0;
strcat(regexp,optarg);
while ((database = SLOCATE_PATH[i++])) res |= decode_db(database, optarg);
return(res);
break;
case 'o':
parse_create_path(optarg);
NEWOUTPUT=1;
break;
case 'd':
parse_decode_path(optarg);
break;
case '-':
parse_dash(optarg);
break;
case 'n':
for (i=0; i < strlen(optarg); i+=1) {
if (!isdigit(optarg[i])) {
fprintf(stderr,"%s: ERROR: Invalid argument for option -n\n",progname);
exit(1);
}
}
max_queries = atoi(optarg);
break;
case 'i':
NOCASE=1;
break;
default:
return(1);
break;
}
}
SLOC_GID = get_gid(GRPFILE);
if (SPECDIR) {
res = create_db(spec_dir);
} else if (ROOTDIR)
res = create_db((char *)NULL);
else if (argv[optind]) {
int j = optind;
int i = 0;
while (j < args) {
/* while ((database = SLOCATE_PATH[i++])) res |= decode_db(database, argv[j++]);
i = 0;
* A Bug fix by Hans-Juergen Godau <godau@wi-inf.uni-essen.de>
* Prevents segfault when using multiple databases */
while ((database = SLOCATE_PATH[i++]))
res |= decode_db(database, argv[j]);
i = 0; j += 1;
}
} else
usage();
return(res);
}
|