FOSSology  4.4.0
Open Source License Compliance by Open Source Software
nomos.h
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2006-2014 Hewlett-Packard Development Company, L.P.
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
66 #ifndef _NOMOS_H
67 #define _NOMOS_H 1
68 #ifndef _GNU_SOURCE
69 #define _GNU_SOURCE
70 #endif /* not defined _GNU_SOURCE */
71 #include <glib.h>
72 #include <stdio.h>
73 #include <assert.h>
74 #include <stdlib.h>
75 #include <search.h>
76 #include <unistd.h>
77 #include <fcntl.h>
78 #include <string.h>
79 #include <strings.h>
80 #include <ctype.h>
81 #include <dirent.h>
82 #include <ftw.h>
83 #include <regex.h>
84 #include <getopt.h>
85 #include <time.h>
86 #include <libgen.h>
87 #include <errno.h>
88 #include <sys/types.h>
89 #include <sys/stat.h>
90 #include <sys/wait.h>
91 #include <sys/mman.h>
92 #include <sys/time.h>
93 #include "nomos_gap.h"
94 #include <stdbool.h>
95 #include <semaphore.h>
96 #include <stdbool.h>
97 #include "json_writer.h"
98 
100 #ifdef STANDALONE
101 #include "standalone.h"
102 #else
103 #include <libfossology.h>
104 #include <libfossdbmanager.h>
105 #endif
106 
110 #ifdef MEMORY_TRACING
111 #include "DMalloc.h"
112 #endif /* MEMORY_TRACING */
113 
114 #define PRECHECK
115 #define GPLV2_BEATS_GPLV3
116 #define SAVE_UNCLASSIFIED_LICENSES
117 /*#define FLAG_NO_COPYRIGHT*/
118 
119 #ifdef PROC_TRACE_SWITCH
120 #define PROC_TRACE
121 #endif /* PROC_TRACE_SWITCH */
122 
123 #define myBUFSIZ 4096
124 #define MAX_RENAME 1000
125 #define TEMP_FILE_LEN 100
126 
131 #define MAX_SCANBYTES 1024*1024
132 
138 #define OPTS_DEBUG 0x1
139 #define OPTS_TRACE_SWITCH 0x2
140 #define OPTS_LONG_CMD_OUTPUT 0x4
141 #define OPTS_HIGHLIGHT_STDOUT 0x8
142 #define OPTS_NO_HIGHLIGHTINFO 0x10
143 #define OPTS_JSON_OUTPUT 0x20
144 #define OPTS_SCANNING_DIRECTORY 0x40
145 
146 extern char debugStr[myBUFSIZ];
147 extern char dbErrString[myBUFSIZ];
148 extern char saveLics[myBUFSIZ];
149 
150 extern size_t hashEntries;
151 
155 #define FL_SAVEBASE 0x20
156 #define FL_FRAGMENT 0x40
157 #define FL_SHOWMATCH 0x80
158 #define FL_NOCOPYRIGHT 0x100
159 
163 #define FILE_FOUND "Found.txt"
164 #define FILE_SCORES "_scores"
165 #define DEBUGLOG "/tmp/NomosDebugLog"
166 
167 
171 #define NO 0
175 #define YES 1
176 
177 /* List-sorting flags */
178 #define UNSORTED 0
179 #define SORT_BY_NAME 1
180 #define SORT_BY_NAME_ICASE 2
181 #define SORT_BY_COUNT_DSC 3
182 #define SORT_BY_COUNT_ASC 4
183 #define SORT_BY_ALIAS 5
184 #define SORT_BY_BASENAME 6
185 
186 /* Interest level (licenses) */
187 #define IL_HIGH 3
188 #define IL_MED 2
189 #define IL_LOW 1
190 #define IL_NONE 0
191 #define IL_INIT -1
192 
196 #define LTSR_RMASK ((char) 1)
197 #define LTSR_SMASK ((char) 2)
198 #define LTSR_YES ((char) 3)
199 #define LTSR_NO LTSR_SMASK
204 #define STR_NOTPKG "None (not an rpm-format package)"
205 
206 /*
207  * License-scanning limits
208  */
209 #define _scCOMFORT 9
210 #define _scINVALID 4
215 #define LS_NONE "None"
216 #define LS_UNLIKELY "LikelyNot"
217 #define LS_NOSUM "No_license_found"
218 #define LS_UNCL "UnclassifiedLicense"
219 #define LS_NOT_PD "NOT-public-domain"
220 #define LS_PD_CLM "Public-domain"
221 #define LS_PD_CPRT "Public-domain(C)"
222 #define LS_PD_ONLY "Public-domain-ref"
223 #define LS_CPRTONLY "Misc-Copyright"
224 #define LS_TDMKONLY "Trademark-ref"
225 #define LS_LICRONLY "License-ref"
226 #define LS_PATRONLY "Patent-ref"
227 
228 /*
229  * NULL values
230  */
231 #define NULL_ITEM (item_t *) NULL
232 #define NULL_LIST (list_t *) NULL
233 #define NULL_FH (fh_t *) NULL
234 #define NULL_CHAR '\0'
235 #define NULL_STR (char *) NULL
236 
240 #define isEOL(x) (((x == '\n') || (x == '\r') || (x == '\v')))
241 #define IS_HUGE(x) (x >= gl.blkUpperLimit)
242 
243 
244 
245 #define NOMOS_TEMP "/tmp/nomos.tempdir"
246 #define NOMOS_TLOCK "/tmp/nomos.tempdir/.lock.tmp,"
252 struct mm_cache {
253  int inUse;
254  int fd;
255  unsigned long size;
256  void *mmPtr;
257  char label[myBUFSIZ];
258 };
259 
260 
261 /*
262  CDB - This is kind of tricky, the way it uses the same fields for
263  different meanings. If we had objects, we could subclass. It works
264  okay, but is just a PITA for debugging.
265  */
266 
274 struct listitem {
275  int val;
276  int val2;
277  int val3;
278  char *str;
279  void *buf;
280 };
281 typedef struct listitem item_t;
282 
283 
287 #define seqNo val
288 #define foundTool val
289 #define refCount val
290 #define num val
291 #define bStart val
292 #define iFlag val
293 #define ssComp val2
294 #define isProcessed val2
295 #define iLevel val2
296 #define nMatch val2
297 #define bLen val2
298 #define bDocLen val3
299 #define bIndex val3
300 #define bList buf
301 
308 struct list {
309  char name[64];
310  int used;
311  int size;
312  int ix;
313  int sorted;
316  int desc;
318 };
319 typedef struct list list_t;
320 
324 struct searchString {
325  int csLen;
326  char *csData;
327 };
328 typedef struct searchString searchString_t;
329 
333 struct licenseSpec {
336 };
337 typedef struct licenseSpec licSpec_t;
338 
339 
344 struct globals {
345  char initwd[myBUFSIZ];
346  char progName[64];
347  int progOpts;
348  int flags;
349  int uPsize;
350 #ifdef GLOBAL_DEBUG
351  int DEEBUG;
352  int MEM_DEEBUG;
353 #endif /* GLOBAL_DEBUG */
354 #ifdef PROC_TRACE_SWITCH
355  int ptswitch;
356 #endif /* PROC_TRACE_SWITCH */
358  /* Agent-specific Things */
359  int agentPk;
360  long uploadFk;
361  int arsPk;
362  PGconn *pgConn;
364 };
365 
369 typedef struct {
370  int start;
371  int end;
372  int index;
374 
378 typedef struct {
379  GArray* matchPositions;
380  GArray* indexList;
381  char* licenceName;
384 
385 
386 
391 struct curScan {
392  char cwd[myBUFSIZ];
393  char targetDir[myBUFSIZ]; /* check */
394  char targetFile[myBUFSIZ]; /* check */
396  long pFileFk;
397  char pFile[myBUFSIZ];
398  char *licPara;
399  char *matchBase;
400  size_t targetLen;
401  size_t cwdLen;
402  struct stat stbuf;
403  regmatch_t regm;
404  list_t regfList;
405  list_t fLicFoundMap;
406  list_t parseList;
407  list_t offList;
408  list_t lList;
411  int nLines;
412  int cliMode;
413  char *tmpLics;
414  char *licenseList[512];
416  GArray* indexList;
417  GArray* theMatches;
419  GArray* docBufferPositionsAndOffsets;
420  int currentLicenceIndex;
421 };
422 
426 struct license {
427  int len;
428  char *patt;
429 };
430 
434 struct licensetext {
435  char *regex;
436  char *tseed;
437  int nAbove;
438  int nBelow;
439  int compiled;
440  int plain;
441 };
442 typedef struct licensetext licText_t;
443 
447 #define _REGEX(x) licText[x].regex
451 #define _SEED(x) licText[x].tseed
452 
456 struct scanResults {
457  int score;
458  int kwbm;
459  int size;
460  int flag;
461  int dataOffset;
462  char fullpath[myBUFSIZ];
463  char linkname[16];
464  char *licenses;
465  char *relpath;
466  size_t nameOffset;
467 };
468 typedef struct scanResults scanres_t;
469 
473 #define MTAG_UNSORTKEY "list/str (initially-UNsorted key)"
474 #define MTAG_SORTKEY "list/str (initially-sorted key)"
475 #define MTAG_LISTKEY "list/str (sorted/unsorted key)"
476 #define MTAG_REPLKEY "list/str (replaced primary key)"
477 #define MTAG_LISTBUF "list/buf (any data)"
478 #define MTAG_PATHBASE "list/buf (path basename)"
479 #define MTAG_PKGINFO "list/buf (pkg rname/type/name/vers/lic)"
480 #define MTAG_PKG_NV "list/buf (pkg name/vers)"
481 #define MTAG_MD5SUM "list/buf (distro-arch MD5SUM)"
482 #define MTAG_COUNTER "list/buf integer (counter)"
483 #define MTAG_PKGNAME "list/buf (package-name)"
484 #define MTAG_PKGVERS "list/buf (package-vers)"
485 #define MTAG_CLAIMLIC "list/buf (claimlic copy)"
486 #define MTAG_COMPLIC "list/buf (pkg compLic copy)"
487 #define MTAG_URLCOPY "list/buf (pkg URL copy)"
488 #define MTAG_FILELIC "list/buf (file-license copy)"
489 #define MTAG_FIXNAME "list/buf (fixed-package name)"
493 #define MTAG_SEEDTEXT "search-seed text"
494 #define MTAG_SRCHTEXT "license-search text"
495 #define MTAG_MMAPFILE "mmap-file data"
496 #define MTAG_MAGICDATA "file magic description"
497 #define MTAG_PATTRS "pkg-attr buffer"
498 #define MTAG_DOUBLED "doubled (reallocated) data"
499 #define MTAG_SEARCHBUF "initial search-data buffer"
500 #define MTAG_TOOSMALL "too-small half-size buffer"
501 #define MTAG_TEXTPARA "paragraph text"
502 #define MTAG_LIST "dynamically-allocated list"
503 #define MTAG_ENV "environment variable"
504 #define MTAG_SCANRES "scan-results list"
505 
506 
507 /*
508  Functions defined in nomos.c, used in other files
509  */
510 void Bail(int exitval);
511 int optionIsSet(int val);
512 
513 /*
514  Global Declarations
515  */
516 extern struct globals gl;
517 extern struct curScan cur;
518 extern licText_t licText[];
519 extern licSpec_t licSpec[];
520 extern int schedulerMode; /* Non-zero if being run by scheduler */
521 
525 #ifdef MEMORY_TRACING
526 char *memAllocTagged();
527 void memFreeTagged();
528 #define memFree(x,y) memFreeTagged(x, y)
529 #define memAlloc(x,y) memAllocTagged(x, y)
530 #else /* NOT MEMORY_TRACING */
531 #define memFree(x,/*notused*/y) free(x)
532 #define memAlloc(x,y) calloc(x, 1)
533 #endif /* NOT MEMORY_TRACING */
534 
535 /*
536  * Macros for timing - refer to findPhrase() for usage examples
537  */
538 /* need TIMING_DECL in the declarations section of function */
539 #define DECL_TIMER struct timeval bTV, eTV; float proctime
540 #define ZERO_TIMER memcpy((void *) &bTV, (void *) &eTV, sizeof(eTV))
541 #define RESET_TIMER END_TIMER; ZERO_TIMER
542 #define START_TIMER RECORD_TIMER(bTV)
543 #define END_TIMER RECORD_TIMER(eTV) ; \
544  proctime = (float) (eTV.tv_sec - bTV.tv_sec) + \
545  ((float) (eTV.tv_usec - bTV.tv_usec) * 0.000001)
546 #define RECORD_TIMER(x) (void) gettimeofday(&x, (struct timezone *) NULL)
547 #define PRINT_TIMER(x,y) printf("%11.6f seconds: %s\n", proctime, x); \
548  if (y) { DUMP_TIMERS; }
549 #define DUMP_TIMERS printf("[1]: %d.%06d\n", bTV.tv_sec, bTV.tv_usec); \
550  printf("[2]: %d.%06d\n", eTV.tv_sec, eTV.tv_usec)
551 
552 
553 /*
554  * Cut-and-paste this stuff to turn on timing
555  */
556 #if 0
557 #ifdef TIMING
558 DECL_TIMER; /* timer declaration */
559 #endif
560 /* */
561 #ifdef TIMING
562 START_TIMER; /* turn on the timer */
563 #endif /* TIMING */
564 /* */
565 #ifdef TIMING
566 END_TIMER; /* stop the timer */
567 PRINT_TIMER("unpack", 0); /* ... and report */
568 START_TIMER; /* optionally re-start timer */
569 #endif /* TIMING */
570 #endif
571 
572 #endif /* _NOMOS_H */
Handle JSON outputs.
The main FOSSology C library.
licText_t licText[]
#define myBUFSIZ
Buffer max length.
Definition: nomos.h:123
char debugStr[myBUFSIZ]
Debug string.
Definition: nomos.c:28
int schedulerMode
Definition: nomos.c:33
void Bail(int exitval)
Close connections and exit.
Definition: nomos_utils.c:533
char saveLics[myBUFSIZ]
License string.
Definition: nomos_utils.c:23
size_t hashEntries
Hash entries.
Definition: nomos.c:31
char dbErrString[myBUFSIZ]
DB error string.
Definition: nomos.c:29
int optionIsSet(int val)
Check if an CLI option is set.
Definition: nomos_utils.c:560
GArray * matchPositions
Match positions.
Definition: nomos.h:379
GArray * indexList
License indexes.
Definition: nomos.h:380
char * licenceName
License names.
Definition: nomos.h:381
int licenseFileId
PFile id.
Definition: nomos.h:382
int start
Start position of match.
Definition: nomos.h:370
int index
Enums from index (Entrynumber) in STRINGS.in.
Definition: nomos.h:372
int end
End position of match.
Definition: nomos.h:371
Struct that tracks state related to current file being scanned.
Definition: nomos.h:391
GArray * indexList
Definition: nomos.h:416
char cwd[myBUFSIZ]
Definition: nomos.h:392
char * tmpLics
Definition: nomos.h:413
char pFile[myBUFSIZ]
Definition: nomos.h:397
char filePath[myBUFSIZ]
Definition: nomos.h:395
long pFileFk
Definition: nomos.h:396
char targetDir[myBUFSIZ]
Definition: nomos.h:393
GArray * theMatches
Definition: nomos.h:417
GArray * keywordPositions
Definition: nomos.h:418
char compLic[myBUFSIZ]
Definition: nomos.h:409
char targetFile[myBUFSIZ]
Definition: nomos.h:394
char * licenseList[512]
Definition: nomos.h:414
int cliMode
Definition: nomos.h:412
Structure holding data truly global in that it remains consistent for each file scanned.
Definition: nomos.h:344
int arsPk
Agent ars id.
Definition: nomos.h:361
long uploadFk
Upload id.
Definition: nomos.h:360
PGconn * pgConn
DB Connection.
Definition: nomos.h:362
list_t sHash
Hashes.
Definition: nomos.h:357
int agentPk
Agent id.
Definition: nomos.h:359
fo_dbManager * dbManager
FOSSology DB manager.
Definition: nomos.h:363
int progOpts
CLI options.
Definition: nomos.h:347
int uPsize
Size.
Definition: nomos.h:349
char initwd[myBUFSIZ]
CDB, would like to workaround/eliminate.
Definition: nomos.h:345
int flags
Flags.
Definition: nomos.h:348
char progName[64]
Program name.
Definition: nomos.h:346
searchString_t text
License text.
Definition: nomos.h:335
searchString_t seed
License seed.
Definition: nomos.h:334
Definition: nomos.h:426
int len
Length of pattern.
Definition: nomos.h:427
char * patt
License pattern to use.
Definition: nomos.h:428
char * regex
License regex.
Definition: nomos.h:435
char * tseed
unencrypted license text
Definition: nomos.h:436
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308
int desc
Definition: nomos.h:316
int size
Definition: nomos.h:311
item_t * items
Definition: nomos.h:317
int used
Definition: nomos.h:310
int sorted
Definition: nomos.h:313
char name[64]
Definition: nomos.h:309
int ix
Definition: nomos.h:312
tricky data structure used for a list of 'items'
Definition: nomos.h:274
void * buf
Definition: nomos.h:279
char * str
Definition: nomos.h:278
int inUse
Cache in use.
Definition: nomos.h:253
int fd
File descriptor.
Definition: nomos.h:254
char label[myBUFSIZ]
Label.
Definition: nomos.h:257
void * mmPtr
Memory pointer.
Definition: nomos.h:256
unsigned long size
Size.
Definition: nomos.h:255
int flag
Flags.
Definition: nomos.h:460
int score
License match score.
Definition: nomos.h:457
int csLen
String length.
Definition: nomos.h:325
char * csData
String data.
Definition: nomos.h:326