1 /*
2  * ----------------------------------------------------------------
3  * syscalls - System Functions
4  * ----------------------------------------------------------------
5  * Copyright (C) 1997-2009 Jonas Kvinge
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * $Id: syscalls.c 54 2009-03-18 18:23:29Z jonasio $
21  *
22  */
23 
24 #define SYSCALLS_C
25 
26 #define NEED_SYS_TYPES_H 1		/* Extra types */
27 #define NEED_SYS_PARAM_H 1		/* Some systems need this */
28 #define NEED_LIMITS_H 1			/* Kernel limits */
29 #define NEED_STDARG_H 1			/* va_list, etc */
30 #define NEED_ERRNO_H 1			/* errno */
31 #define NEED_CTYPE_H 1			/* isdigit(), etc */
32 #define NEED_NETINET_IN_H 1		/* in_addr, sockaddr_in, etc */
33 #define NEED_ARPA_INET_H 1		/* inet_ntoa(), inet_aton(), etc */
34 #define NEED_STDIO_H 1			/* Standard C UNIX functions */
35 #define NEED_STDLIB_H 1			/* malloc(), exit(), atoi(), etc */
36 #define NEED_TIME_H 1			/* time(), etc */
37 #define NEED_SYSCTL_H 0			/* sysctl(), etc */
38 #define NEED_SYS_STAT_H 1		/* chmod(), mkdir(), etc */
39 #define NEED_SYS_UIO_H 0		/* iovec, etc */
40 #define NEED_FCNTL_H 1			/* open(), creat(), fcntl(), etc */
41 #define NEED_SYS_IOCTL_H 1		/* ioctl(), etc */
42 #define NEED_SYS_FILIO_H 1		/* Solaris need this for ioctl(), etc */
43 #define NEED_UNISTD_H 1			/* Unix standard functions */
44 #define NEED_STRING_H 1			/* C string functions */
45 #define NEED_SIGNAL_H 1			/* Signal functions */
46 #define NEED_SYS_SOCKET_H 1		/* Socket functions */
47 #define NEED_NETDB_H 0			/* Network database functions */
48 #define NEED_ARPA_NAMESER_H 0		/* Nameserver definitions */
49 #define NEED_GETUSERPW_HEADERS 1 	/* Functions to retrive system passwords */
50 
51 #include "includes.h"
52 
53 #include "nlstrsignal.h"
54 #include "main.h"
55 #include "conf.h"
56 
57 #if DCC
58   #include "dcc_conn.h"
59 #endif
60 
61 /* VARIABLES - JONAS (23.08.2001) */
62 
63 unsigned short int ScreenMode = TRUE;
64 unsigned short int ScreenPrint = TRUE;
65 unsigned short int ScreenDTS = TRUE;
66 unsigned short int Run = TRUE;
67 unsigned short int Exit = FALSE;
68 unsigned short int Root = FALSE;
69 
70 static pid_t PID = 0;
71 
72 time_t StartTime = 0;
73 time_t FlushTime = 0;
74 time_t ExitTime = 0;
75 
76 #if !WIN32
77 
78 uid_t UID_Current = 0;
79 uid_t EUID_Current = 0;
80 uid_t UID_Normal = 0;
81 uid_t EUID_Normal = 0;
82 
83 gid_t GID_Current = 0;
84 gid_t EGID_Current = 0;
85 gid_t GID_Normal = 0;
86 gid_t EGID_Normal = 0;
87 
88 #endif
89 
90 unsigned short int ExitSignals = 0;
91 
92 char *TMPSTRPT = NULL;
93 
94 extern unsigned long int ScreenBitmask;
95 
96 extern struct PrintFile_Struct PrintFileS[];
97 extern unsigned short int PrintFileS_Len;
98 
99 extern struct Conf_Struct ConfS;
100 
101 /* SYSINIT FUNCTION - JONAS (27.02.2002) */
102 
sysinit(void)103 void sysinit(void) {
104 
105   signed long int Flags = 0;
106   signed long int Result = 0;
107 
108   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "System initialization.");
109 
110   PID = getpid();
111   NOW = time(NULL);
112   srand(NOW);
113   StartTime = NOW;
114 
115 #if defined(NBLOCK_SYSV)
116   Flags = 1;
117   Result = ioctl(STDIN_FILENO, FIONBIO, &Flags);
118 #else
119   Flags = fcntl(STDIN_FILENO, F_GETFL);
120   if (Flags <= ERROR) { Flags = 0; }
121 #if defined(NBLOCK_BSD)
122   Flags |= O_NDELAY;
123 #elif defined(NBLOCK_POSIX)
124   Flags |= O_NONBLOCK;
125 #else
126   #warning "This system does not support non-blocking sockets?"
127   Flags |= O_NONBLOCK;
128 #endif
129   Result = fcntl(STDIN_FILENO, F_SETFL, Flags);
130 #endif
131 
132   strgetunameinfo();
133   strgetplatform();
134   strgetosname();
135   strgetosrelease();
136   strgethostname();
137 
138 }
139 
140 #if !WIN32
141 /* SYSINITID FUNCTION - JONAS (18.07.2001) */
142 
sysinitid(void)143 void sysinitid(void) {
144 
145   signed long int Result = 0;
146 
147   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Initializing ID.");
148 
149   UID_Current = getuid();
150   EUID_Current = geteuid();
151 
152   GID_Current = getgid();
153   EGID_Current = getegid();
154 
155   UID_Normal = UID_Current;
156   GID_Normal = GID_Current;
157 
158   if (UID_Current == 0) {
159     Root = TRUE;
160     assert(ConfS.EUser != NULL);
161     EUID_Normal = sysgetuidfromuser(ConfS.EUser);
162     if (aerrno != AESUCCESS) {
163       printf("%s: No such user.\n", ConfS.EUser);
164       printf("Cannot start %s from root without a vaild EUSER.\n", PACKAGE);
165       exit(1);
166     }
167     if (EUID_Normal == 0) {
168       printf("You must set EUSER to something else than root!\n");
169       exit(1);
170     }
171     EGID_Normal = sysgetgidfromgroup(ConfS.EGroup);
172     if (aerrno != AESUCCESS) {
173       printf("%s: No such group.\n", ConfS.EGroup);
174       printf("Cannot start %s from root without a vaild EGROUP.\n", PACKAGE);
175       exit(1);
176     }
177     if (EGID_Normal == 0) {
178       printf("You must set EGROUP to something else than (root/system/wheel)!\n");
179       exit(1);
180     }
181   }
182   else {
183     Root = FALSE;
184     EUID_Normal = EUID_Current;
185     EGID_Normal = EGID_Current;
186   }
187 
188   if (ConfS.ChDir == TRUE) {
189     Result = chdir(ConfS.ChDirPath);
190     if (Result == ERROR) {
191       printf("Unable to change working directory to %s (CHDIRPATH): [%d] %s\n", ConfS.ChDirPath, errno, strerror(errno));
192       exit(1);
193     }
194   }
195 
196   if (GID_Current == 0) {
197     Result = setegid(EGID_Normal);
198     if (Result <= ERROR) { printf("Unable to change effective group ID to %ld: [%d] %s\n", (PRINT_GID_T) EGID_Normal, errno, strerror(errno)); exit(1); }
199     EGID_Current = EGID_Normal;
200   }
201 
202   if (UID_Current == 0) {
203     Result = seteuid(EUID_Normal);
204     if (Result <= ERROR) { printf("Unable to change effective user ID to %ld: [%d] %s\n", (PRINT_UID_T) EUID_Normal, errno, strerror(errno)); exit(1); }
205     EUID_Current = EUID_Normal;
206   }
207 
208   assert(EUID_Current == EUID_Normal);
209   assert(EGID_Current == EGID_Normal);
210 
211   assert(EUID_Current == geteuid());
212   assert(EGID_Current == getegid());
213 
214   assert(EUID_Current != 0);
215   assert(EGID_Current != 0);
216 
217 }
218 #endif
219 
220 #if !WIN32
221 /* SYSSETEUIDNORMAL FUNCTION - JONAS (01.07.2000) */
222 
sysseteuidnormal(void)223 signed long int sysseteuidnormal(void) {
224 
225   signed long int Result = 0;
226 
227   if (EUID_Current == EUID_Normal) { return(SUCCESS); }
228 
229   if (EUID_Current != 0) {
230     Result = seteuid(0);
231     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to root (0): [%d] %s", errno, strerror(errno)); exit(1); }
232     EUID_Current = 0;
233   }
234 
235   Result = seteuid(EUID_Normal);
236   if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to normal (%ld): [%d] %s", (PRINT_UID_T) EUID_Normal, errno, strerror(errno)); exit(1); }
237   EUID_Current = EUID_Normal;
238 
239   return(SUCCESS);
240 
241 }
242 #endif
243 
244 #if !WIN32
245 /* SYSSETUID FUNCTION - JONAS (01.07.2000) */
246 
sysseteuid(const uid_t UID)247 signed long int sysseteuid(const uid_t UID) {
248 
249   signed long int Result = 0;
250 
251   if (UID_Current != 0) { return(ERROR); }
252   if (EUID_Current == UID) { return(SUCCESS); }
253 
254   Result = seteuid(0);
255   if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to root (0): [%d] %s", errno, strerror(errno)); return(ERROR); }
256   EUID_Current = 0;
257   if (UID == 0) { return(SUCCESS); }
258 
259   Result = seteuid(UID);
260   if (Result <= ERROR) {
261     sysprint(BITMASK_ERROR, "Unable to change effective user ID to %ld: [%d] %s", (PRINT_UID_T) UID, errno, strerror(errno));
262     Result = seteuid(EUID_Normal);
263     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to normal (%ld): [%d] %s", (PRINT_UID_T) EUID_Normal, errno, strerror(errno)); exit(1); }
264     return(ERROR);
265   }
266   EUID_Current = UID;
267 
268   return(SUCCESS);
269 
270 }
271 #endif
272 
273 #if !WIN32
274 /* SYSSETUIDBYUSER FUNCTION - JONAS (01.07.2000) */
275 
sysseteuidbyuser(const char * const UserPT)276 signed long int sysseteuidbyuser(const char *const UserPT) {
277 
278   signed long int Result = 0;
279   uid_t UID = 0;
280 
281   assert(UserPT != NULL);
282 
283   if (UID_Current != 0) { return(ERROR); }
284 
285   UID = sysgetuidfromuser(UserPT);
286   if (aerrno != AESUCCESS) { return(ERROR); }
287 
288   if (EUID_Current == UID) { return(SUCCESS); }
289 
290   Result = sysseteuid(UID);
291 
292   return(Result);
293 
294 }
295 #endif
296 
297 /* SYSINITFILES FUNCTION - JONAS (27.02.2002) */
298 
sysinitfiles(void)299 void sysinitfiles(void) {
300 
301   unsigned short int Index = 0;
302   signed long int Result = 0;
303 
304   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Initializing all files.");
305 
306   for (Index = 0 ; Index < PrintFileS_Len ; Index++) {
307 
308     char File[FILELEN+1] = "";
309 
310     assert(PrintFileS[Index].NamePTR != NULL);
311     assert(PrintFileS[Index].Name == NULL);
312 
313     PrintFileS[Index].Name = strdup(*PrintFileS[Index].NamePTR);
314 
315     if (Index == 0) { PrintFileS[Index].FilePT = stdin; }
316     else if (Index == 2) { PrintFileS[Index].FilePT = stderr; }
317     else { PrintFileS[Index].FilePT = stdout; }
318 
319     if ((strncmp(PrintFileS[Index].Name, ConfS.LogPath, strlen(ConfS.LogPath)) != FALSE) && (PrintFileS[Index].Name[0] != '/')) {
320       memset(File, 0, FILELEN+1);
321       strncat(File, ConfS.LogPath, (FILELEN - strlen(File)));
322       if (File[strlen(File)] != '/') { strncat(File, "/", (FILELEN - strlen(File))); }
323       strncat(File, PrintFileS[Index].Name, (FILELEN - strlen(File)));
324       PrintFileS[Index].Name = strrealloc(PrintFileS[Index].Name, File);
325     }
326     assert(PrintFileS[Index].Name != NULL);
327   }
328 
329   /* Check to see if we can read/write to all logfiles */
330 
331   for (Index = 0 ; Index < PrintFileS_Len ; ++Index) {
332     Result = open(PrintFileS[Index].Name, (O_CREAT|O_NONBLOCK|O_WRONLY|O_APPEND), (S_IRUSR|S_IWUSR|S_IRGRP));
333     if (Result <= ERROR) {
334       printf("Unable to open %s: [%d] %s\n", PrintFileS[Index].Name, errno, strerror(errno));
335       if (Root == TRUE) { printf("User %s needs read/write permissions to this file!\n", ConfS.EUser); }
336       exit(1);
337     }
338     close(Result);
339   }
340 
341 }
342 
343 /* SYSCLOSEALLFILES FUNCTION - JONAS (25.06.2000) */
344 
syscloseallfiles(void)345 void syscloseallfiles(void) {
346 
347   unsigned long int FD = 0;
348   unsigned long int FDL = sysconf(_SC_OPEN_MAX);
349 
350   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Closing all files.");
351 
352   for (FD = 0 ; FD <= FDL ; FD++) { close(FD); }
353 
354 }
355 
356 /* SYSOPENFILES_FG FUNCTION - JONAS (18.07.2001) */
357 
sysopenfiles_fg(void)358 void sysopenfiles_fg(void) {
359 
360   unsigned short int Index = 0;
361 
362   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Opening files (foreground).");
363 
364   for (Index = 0 ; Index < PrintFileS_Len ; ++Index) {
365     assert(PrintFileS[Index].Name != NULL);
366     PrintFileS[Index].FilePT = fopen(PrintFileS[Index].Name, "a");
367     if (PrintFileS[Index].FilePT == NULL) {
368       printf("Unable to open %s: [%d] %s\n", PrintFileS[Index].Name, errno, strerror(errno));
369       if (Root == TRUE) { printf("User %s needs read/write permissions to this file!\n", ConfS.EUser); }
370       exit(1);
371     }
372   }
373 
374 }
375 
376 /* SYSCLOSEFILES FUNCTION - JONAS (18.07.2001) */
377 
sysclosefiles(void)378 void sysclosefiles(void) {
379 
380   signed long int Result = 0;
381   unsigned short int Index = 0;
382 
383   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Closing files.");
384 
385   for (Index = 0 ; Index < PrintFileS_Len ; ++Index) {
386     if ((PrintFileS[Index].FilePT == NULL) || (PrintFileS[Index].FilePT == stdin) || (PrintFileS[Index].FilePT == stdout) || (PrintFileS[Index].FilePT == stderr)) { continue; }
387     Result = fclose(PrintFileS[Index].FilePT);
388     if (Result == EOF) {
389       sysprint(BITMASK_ERROR, "Unable to close %s: [%d] %s", PrintFileS[Index].Name, errno, strerror(errno));
390       exit(1);
391     }
392     PrintFileS[Index].FilePT = NULL;
393   }
394 
395 }
396 
397 #if !WIN32
398 /* SYSOPENFILES_BG FUNCTION - JONAS (18.07.2001) */
399 
sysopenfiles_bg(void)400 void sysopenfiles_bg(void) {
401 
402   signed long int Result = 0;
403   unsigned short int Index = 0;
404 
405   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Opening files (background).");
406 
407   close(0);
408   close(1);
409   close(2);
410 
411 #if 0
412   FD_CLR(0, &ReadFDS);
413   FD_CLR(0, &WriteFDS);
414   FD_CLR(1, &ReadFDS);
415   FD_CLR(1, &WriteFDS);
416   FD_CLR(2, &ReadFDS);
417   FD_CLR(2, &WriteFDS);
418 #endif
419 
420   for (Index = 0 ; Index < PrintFileS_Len ; ++Index) {
421     switch (Index) {
422       case 0:
423       case 1:
424       case 2:
425         assert(PrintFileS[Index].FilePT == NULL);
426         Result = open(PrintFileS[Index].Name, (O_CREAT|O_NONBLOCK|O_WRONLY|O_APPEND), (S_IRUSR|S_IWUSR|S_IRGRP));
427         if (Result <= ERROR) {
428           sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", PrintFileS[Index].Name, errno, strerror(errno));
429           exit(1);
430         }
431         assert(Result == Index);
432         if (Result != Index) { abort(); }
433         if (Index == 0) { PrintFileS[Index].FilePT = NULL; }
434         else if (Index == 1) { PrintFileS[Index].FilePT = stdout; }
435         else if (Index == 2) { PrintFileS[Index].FilePT = stderr; }
436         break;
437       default:
438         PrintFileS[Index].FilePT = fopen(PrintFileS[Index].Name, "a");
439         if (PrintFileS[Index].FilePT == NULL) {
440           sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", PrintFileS[Index].Name, errno, strerror(errno));
441           exit(1);
442         }
443     }
444   }
445 
446 }
447 #endif
448 
449 /* SYSFLUSHFILES FUNCTION - JONAS (18.07.2001) */
450 
sysflushfiles(void)451 void sysflushfiles(void) {
452 
453   signed long int Result = 0;
454   unsigned long int Index = 0;
455 
456   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Flushing all file descriptors...");
457 
458   FlushTime = NOW;
459 
460   for (Index = 1 ; Index < PrintFileS_Len ; Index++) {
461     if (PrintFileS[Index].Name == NULL) { continue; }
462     if (PrintFileS[Index].FilePT == NULL) { continue; }
463     Result = fflush(PrintFileS[Index].FilePT);
464     if (Result <= ERROR) {
465       sysprint(BITMASK_ERROR, "Unable to flush %s: [%d] %s", PrintFileS[Index].Name, errno, strerror(errno));
466       exit(1);
467     }
468   }
469 
470 }
471 
472 #if !WIN32
473 /* SYSCHECKPID FUNCTION - JONAS (11.07.2001) */
474 
syscheckpid(void)475 void syscheckpid(void) {
476 
477   FILE *FilePT = NULL;
478   char Line[LINELEN+1] = "";
479   char *LinePT = NULL;
480   unsigned long int pid = 0;
481   signed long int Result = 0;
482 
483   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Checking PID file.");
484 
485   if ((UID_Current == 0) && (EUID_Current != 0)) {
486     Result = seteuid(0);
487     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to root (0): [%d] %s", errno, strerror(errno)); }
488     else { EUID_Current = 0; }
489   }
490 
491   FilePT = fopen(ConfS.PIDFile, "r");
492   if (FilePT == NULL) {
493     if (errno != ENOENT) { sysprint(BITMASK_ERROR, "Unable to open %s for reading: [%d] %s", ConfS.PIDFile, errno, strerror(errno)); }
494   }
495   else {
496     LinePT = fgets(Line, LINELEN, FilePT);
497     if (LinePT != NULL) pid = atoi(LinePT);
498     if (pid <= 1) { fclose(FilePT); }
499     else {
500       Result = kill(pid, SIGUSR1);
501       if (Result != ERROR) {
502         fclose(FilePT);
503         printf("%s already running, PID: %ld\n", PACKAGE, (PRINT_PID_T) pid);
504         exit(1);
505       }
506       fclose(FilePT);
507     }
508   }
509 
510   if (EUID_Current != EUID_Normal) {
511     Result = seteuid(EUID_Normal);
512     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to %ld: [%d] %s", (PRINT_UID_T) EUID_Normal, errno, strerror(errno)); exit(1); }
513     EUID_Current = EUID_Normal;
514   }
515 
516 }
517 #endif
518 
519 #if !WIN32
520 /* SYSWRITEPID FUNCTION - JONAS (25.06.2000) */
521 
syswritepid(void)522 void syswritepid(void) {
523 
524   signed long int Result = 0;
525   FILE *FilePT = NULL;
526 
527   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Writing PID file, current PID: %ld.", (PRINT_PID_T) PID);
528 
529   if ((UID_Current == 0) && (EUID_Current != 0)) {
530     Result = seteuid(0);
531     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to root (0): [%d] %s", errno, strerror(errno)); }
532     else { EUID_Current = 0; }
533   }
534 
535   FilePT = fopen(ConfS.PIDFile, "w");
536   if (FilePT == NULL) {
537     sysprint(BITMASK_ERROR, "Unable to open %s for writing: [%d] %s", ConfS.PIDFile, errno, strerror(errno));
538     exit(1);
539   }
540 
541   fprintf(FilePT, "%ld%s", (PRINT_PID_T) PID, LINEFEED);
542   fclose(FilePT);
543 
544   if (EUID_Current != EUID_Normal) {
545     Result = seteuid(EUID_Normal);
546     if (Result <= ERROR) { sysprint(BITMASK_ERROR, "Unable to change effective user ID to %ld: [%d] %s", (PRINT_UID_T) EUID_Normal, errno, strerror(errno)); exit(1); }
547     EUID_Current = EUID_Normal;
548   }
549 
550 }
551 #endif
552 
553 /* SYSSETSIGNALS FUNCTION - JONAS (25.06.2000) */
554 
syssetsignals(void)555 void syssetsignals(void) {
556 
557   void *ResultPT = NULL;
558 
559   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Setting signals.");
560 
561 #ifdef SIGHUP
562   ResultPT = signal(SIGHUP, syshandlesignal);
563   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Hangup\"."); }
564 #endif
565 
566 #ifdef SIGINT
567   ResultPT = signal(SIGINT, syshandlesignal);
568   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Interrupt\"."); }
569 #endif
570 
571 #ifdef SIGQUIT
572   ResultPT = signal(SIGQUIT, syshandlesignal);
573   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Quit\"."); }
574 #endif
575 
576 #ifdef SIGILL
577   ResultPT = signal(SIGILL, syshandlesignal);
578   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Illegal instruction\"."); }
579 #endif
580 
581 #ifdef SIGTRAP
582   ResultPT = signal(SIGTRAP, syshandlesignal);
583   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Trace trap\"."); }
584 #endif
585 
586 #ifdef SIGABRT
587   ResultPT = signal(SIGABRT, syshandlesignal);
588   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Abort\"."); }
589 #endif
590 
591 #ifdef SIGIOT
592   ResultPT = signal(SIGIOT, syshandlesignal);
593   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Abort\"."); }
594 #endif
595 
596 #ifdef SIGEMT
597   ResultPT = signal(SIGEMT, syshandlesignal);
598   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"EMT instruction\"."); }
599 #endif
600 
601 #ifdef SIGFPE
602   ResultPT = signal(SIGFPE, syshandlesignal);
603   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Floating-point exception\"."); }
604 #endif
605 
606 #ifdef SIGBUS
607   ResultPT = signal(SIGBUS, syshandlesignal);
608   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Bus error\"."); }
609 #endif
610 
611 #ifdef SIGSEGV
612   ResultPT = signal(SIGSEGV, syshandlesignal);
613   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Segmentation violation\"."); }
614 #endif
615 
616 #ifdef SIGSYS
617   ResultPT = signal(SIGSYS, syshandlesignal);
618   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Non-existent system call invoked\"."); }
619 #endif
620 
621 #ifdef SIGPIPE
622   ResultPT = signal(SIGPIPE, SIG_IGN);
623   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Broken pipe\"."); }
624 #endif
625 
626 #ifdef SIGALRM
627   ResultPT = signal(SIGALRM, SIG_IGN);
628   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Alarm clock\"."); }
629 #endif
630 
631 #ifdef SIGTERM
632   ResultPT = signal(SIGTERM, syshandlesignal);
633   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Termination\"."); }
634 #endif
635 
636 #ifdef SIGURG
637   ResultPT = signal(SIGURG, syshandlesignal);
638   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Urgent condition on IO channel\"."); }
639 #endif
640 
641 #ifdef SIGTSTP
642   ResultPT = signal(SIGTSTP, syshandlesignal);
643   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Stop signal from tty\"."); }
644 #endif
645 
646 #ifdef SIGCONT
647   ResultPT = signal(SIGCONT, syshandlesignal);
648   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Continue a stopped process\"."); }
649 #endif
650 
651 #ifdef SIGCHLD
652   ResultPT = signal(SIGCHLD, SIG_IGN);
653   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Signal to parent on child stop or exit\"."); }
654 #endif
655 
656 #ifdef SIGTTIN
657   ResultPT = signal(SIGTTIN, SIG_IGN);
658   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Background read from tty\"."); }
659 #endif
660 
661 #ifdef SIGTTOU
662   ResultPT = signal(SIGTTOU, SIG_IGN);
663   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Background write to tty\"."); }
664 #endif
665 
666 #ifdef SIGIO
667   ResultPT = signal(SIGIO, SIG_IGN);
668   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"I/O possible\"."); }
669 #endif
670 
671 #ifdef SIGXCPU
672   ResultPT = signal(SIGXCPU, syshandlesignal);
673   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Exceeded CPU time limit\"."); }
674 #endif
675 
676 #ifdef SIGXFSZ
677   ResultPT = signal(SIGXFSZ, syshandlesignal);
678   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Exceeded file size limit\"."); }
679 #endif
680 
681 #ifdef SIGVTALRM
682   ResultPT = signal(SIGVTALRM, SIG_IGN);
683   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Virtual alarm clock\"."); }
684 #endif
685 
686 #ifdef SIGPROF
687   ResultPT = signal(SIGPROF, SIG_IGN);
688   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Profiling alarm clock\"."); }
689 #endif
690 
691 #ifdef SIGWINCH
692   ResultPT = signal(SIGWINCH, SIG_IGN);
693   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Window size change\"."); }
694 #endif
695 
696 #if 0
697   ResultPT = signal(SIGINFO, SIG_IGN);
698   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Information request\"."); }
699 #endif
700 
701 #ifdef SIGUSR1
702   ResultPT = signal(SIGUSR1, SIG_IGN);
703   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"User-defined signal 1\"."); }
704 #endif
705 
706 #ifdef SIGUSR2
707   ResultPT = signal(SIGUSR2, SIG_IGN);
708   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"User-defined signal 1\"."); }
709 #endif
710 
711 #ifdef SIGPWR
712   ResultPT = signal(SIGPWR, syshandlesignal);
713   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Power failure restart\"."); }
714 #endif
715 
716 #ifdef SIGSTKFLT
717   ResultPT = signal(SIGSTKFLT, syshandlesignal);
718   if (ResultPT == SIG_ERR) { sysprint(BITMASK_ERROR, "Unable to install signal handler for \"Stack fault\"."); }
719 #endif
720 
721 }
722 
723 /* SYSHANDLESIGNAL FUNCTION - JONAS (26.03.2002) */
724 
syshandlesignal(int Signal)725 void syshandlesignal(int Signal) {
726 
727   signal(Signal, syshandlesignal);
728 
729 
730   /* Signals telling us to rehash the configuration */
731 
732 #ifdef SIGHUP
733   if (Signal == SIGHUP) { /* Hangup */
734     sysprint(BITMASK_MAIN, "Caught signal %d, %s.", Signal, nlstrsignal(Signal));
735     main_rehash();
736     return;
737   }
738 #endif
739 
740 
741   /* Signals telling us to exit normally */
742 
743   if
744     (
745     (1 == 2)
746 #ifdef SIGINT
747     ||
748     (Signal == SIGINT) /* Interrupt */
749 #endif
750 #ifdef SIGTERM
751     ||
752     (Signal == SIGTERM)	/* Termination */
753 #endif
754   ) {
755     ExitSignals++;
756     if (ExitSignals >= 3) { exit(0); }
757     sysprint(BITMASK_MAIN, "Caught signal %d, %s.", Signal, nlstrsignal(Signal));
758     sysflushfiles();
759     main_exit("Caught signal %d, %s", Signal, nlstrsignal(Signal));
760     return;
761   }
762 
763 
764   /* Signals telling us something went wrong */
765 
766   if
767     (
768     (1 == 2)
769 #ifdef SIGQUIT
770     ||
771     (Signal == SIGQUIT) /* Quit */
772 #endif
773 #ifdef SIGILL
774     ||
775     (Signal == SIGILL) /* Illegal instruction */
776 #endif
777 #ifdef SIGTRAP
778     ||
779     (Signal == SIGTRAP) /* Trace trap */
780 #endif
781 #ifdef SIGABRT
782     ||
783     (Signal == SIGABRT) /* Abort */
784 #endif
785 #ifdef SIGIOT
786     ||
787     (Signal == SIGIOT) /* Abort? */
788 #endif
789 #ifdef SIGEMT
790     ||
791     (Signal == SIGEMT) /* EMT instruction */
792 #endif
793 #ifdef SIGFPE
794     ||
795     (Signal == SIGFPE) /* Floating-point exception */
796 #endif
797 #ifdef SIGBUS
798     ||
799     (Signal == SIGBUS) /* BUS error */
800 #endif
801 #ifdef SIGSEGV
802     ||
803     (Signal == SIGSEGV) /* Segmentation violation */
804 #endif
805 #ifdef SIGSYS
806     ||
807     (Signal == SIGSYS) /* Non-existent system call invoked */
808 #endif
809 #ifdef SIGSTKFLT
810     ||
811     (Signal == SIGSTKFLT) /* Stack fault */
812 #endif
813 #ifdef SIGPWR
814     ||
815     (Signal == SIGPWR) /* Power failure restart */
816 #endif
817 #ifdef SIGTSTP
818     ||
819     (Signal == SIGTSTP) /* Stop signal from tty */
820 #endif
821 #ifdef SIGXCPU
822     ||
823     (Signal == SIGXCPU) /* Exceeded CPU time limit */
824 #endif
825 #ifdef SIGXFSZ
826     ||
827     (Signal == SIGXFSZ) /* Exceeded file size limit */
828 #endif
829   ) {
830     ExitSignals++;
831     if (ExitSignals >= 3) { exit(1); }
832     sysprint(BITMASK_ERROR, "Caught signal %d, %s.", Signal, nlstrsignal(Signal));
833     sysflushfiles();
834     main_exit("Caught signal %d, %s", Signal, nlstrsignal(Signal));
835     main_loop();
836     return;
837   }
838 
839 
840   /* And basically just print anything else */
841 
842   sysprint(BITMASK_MAIN, "Caught signal %d, %s.", Signal, nlstrsignal(Signal));
843 
844 }
845 
846 #if !WIN32
847 /* SYSFORK FUNCTION - JONAS (25.06.2000) */
848 
sysfork(void)849 void sysfork(void) {
850 
851   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Going into the background...");
852 
853   sysclosefiles();
854 
855   PID = fork();
856   switch(PID) {
857     case ERROR:
858       sysprint(BITMASK_ERROR, "Unable to create a child process: [%d] %s", errno, strerror(errno));
859       exit(1);
860     case SUCCESS:
861       if (setsid() == ERROR) {
862         sysprint(BITMASK_ERROR, "Unable to request a new session: [%d] %s", errno, strerror(errno));
863         exit(1);
864       }
865       break;
866     default:
867       exit(0);
868   }
869 
870   sysopenfiles_bg();
871 
872   PID = getpid();
873   syswritepid();
874 
875   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Successfully went into the background, PID: %ld", (PRINT_PID_T) PID);
876   ScreenMode = FALSE;
877 
878 }
879 #endif
880 
881 /* SYSPRINT FUNCTION - JONAS (25.06.2000) */
882 
sysprint(const unsigned long int Bitmask,const char * const LinePT,...)883 void sysprint(const unsigned long int Bitmask, const char *const LinePT, ...) {
884 
885   char Line[LINELEN+1] = "";
886   va_list Args;
887   unsigned short int Index = 0;
888   signed long int Result = 0;
889 
890   assert(LinePT != NULL);
891 
892   va_start(Args, LinePT);
893   vsnprintf(Line, LINELEN+1, LinePT, Args);
894   va_end(Args);
895 
896   for (Index = 1 ; Index < PrintFileS_Len ; Index++) {
897     if (PrintFileS[Index].FilePT == NULL) { continue; }
898     if ((ScreenMode == TRUE) && (ScreenPrint == TRUE) && ((PrintFileS[Index].FilePT == stdout) || (PrintFileS[Index].FilePT == stderr))) { continue; }
899     if (PrintFileS[Index].Bitmask & Bitmask) {
900       Result = fprintf(PrintFileS[Index].FilePT, "%s *** %s%s", dtstamp(), Line, LINEFEED);
901       Result = fflush(PrintFileS[Index].FilePT);
902     }
903   }
904 
905   if ((ScreenMode == TRUE) && (ScreenPrint == TRUE) && (ScreenBitmask & Bitmask)) {
906     if (ScreenDTS == TRUE) { fprintf(stdout, "%s *** %s\n", dtstamp(), Line); }
907     else { fprintf(stdout, "%s\n", Line); }
908     fflush(stdin);
909     fflush(stdout);
910     fflush(stderr);
911   }
912 
913 #if DCC
914   dcc_conn_sendall(Bitmask, "*** %s\r\n", Line);
915 #endif
916 
917 }
918 
919 /* DEBUGPRINT FUNCTION - JONAS (01.12.2007) */
920 
debugprint(const char * const FilePT,const unsigned short int Line,const char * const FunctionPT,const char * const TextLinePT,...)921 void debugprint(const char *const FilePT, const unsigned short int Line, const char *const FunctionPT, const char *const TextLinePT, ...) {
922 
923   char TextLine[LINELEN+1] = "";
924   va_list Args;
925   unsigned short int Index = 0;
926 
927   assert(FilePT != NULL);
928   assert(FunctionPT != NULL);
929   assert(TextLinePT != NULL);
930 
931   va_start(Args, TextLinePT);
932   vsnprintf(TextLine, LINELEN+1, TextLinePT, Args);
933   va_end(Args);
934 
935   for (Index = 1 ; Index < PrintFileS_Len ; Index++) {
936     if ((PrintFileS[Index].Bitmask & BITMASK_DEBUG) && (PrintFileS[Index].FilePT != NULL)) {
937       fprintf(PrintFileS[Index].FilePT, "%s *** DEBUG *** FILE: %s LINE: %d FUNCTION: %s *** %s%s", dtstamp(), FilePT, Line, FunctionPT, TextLine, LINEFEED);
938       fflush(PrintFileS[Index].FilePT);
939     }
940   }
941 
942   if ((ScreenMode == TRUE) && (ScreenPrint == TRUE) && (ScreenBitmask & BITMASK_DEBUG)) {
943     if (ScreenDTS == TRUE) { fprintf(stdout, "%s *** DEBUG *** FILE: %s LINE: %d FUNCTION: %s *** %s%s", dtstamp(), FilePT, Line, FunctionPT, TextLine, LINEFEED); }
944     else { fprintf(stdout, "*** DEBUG *** FILE: %s LINE: %d FUNCTION: %s *** %s%s", FilePT, Line, FunctionPT, TextLine, LINEFEED); }
945     fflush(stdin);
946     fflush(stdout);
947     fflush(stderr);
948   }
949 
950 #if DCC
951   dcc_conn_sendall(BITMASK_DEBUG, "*** %s\r\n", TextLine);
952 #endif
953 
954 }
955 
956 /* SYSREHASH FUNCTION - JONAS (01.07.2000) */
957 
sysrehash(void)958 void sysrehash(void) {
959 
960   FILE *FilePT = fopen(ConfS.PIDFile, "r");
961   char Line[LINELEN+1] = "";
962   char *LinePT = NULL;
963   pid_t pid = 0;
964   signed long int Result = 0;
965 
966   if (FilePT == NULL) {
967     printf("Cannot rehash %s: Unable to open PID file %s: [%d] %s\n", PACKAGE, ConfS.PIDFile, errno, strerror(errno));
968     return;
969   }
970   LinePT = fgets(Line, LINELEN, FilePT);
971   if (LinePT != NULL) pid = atoi(LinePT);
972   if (pid <= 1) {
973     printf("Cannot rehash %s: Invalid PID: %ld.\n", PACKAGE, (PRINT_PID_T) pid);
974     return;
975   }
976   Result = kill(pid, SIGHUP);
977   if (Result == ERROR) {
978     printf("Cannot rehash %s, PID: %ld: [%d] %s\n", PACKAGE, (PRINT_PID_T) pid, errno, strerror(errno));
979     fclose(FilePT);
980     return;
981   }
982 
983   printf("Sent HUP signal to %s, PID: %ld.\n", PACKAGE, (PRINT_PID_T) pid);
984   fclose(FilePT);
985 
986 }
987 
988 /* SYSTERM FUNCTION - JONAS (01.07.2000) */
989 
systerm(void)990 void systerm(void) {
991 
992   FILE *FilePT = fopen(ConfS.PIDFile, "r");
993   char Line[LINELEN+1] = "";
994   char *LinePT = NULL;
995   pid_t pid = 0;
996   signed long int Result = 0;
997 
998   if (FilePT == NULL) {
999     printf("Cannot terminate %s: Unable to open PID file %s: [%d] %s\n", PACKAGE, ConfS.PIDFile, errno, strerror(errno));
1000     return;
1001   }
1002   LinePT = fgets(Line, LINELEN, FilePT);
1003   if (LinePT != NULL) pid = atoi(Line);
1004   if (pid <= 1) {
1005     printf("Cannot terminate %s: Invalid PID: %ld.\n", PACKAGE, (PRINT_PID_T) pid);
1006     return;
1007   }
1008   Result = kill(pid, SIGTERM);
1009   if (Result == ERROR) {
1010     printf("Cannot terminate %s, PID: %ld: [%d] %s\n", PACKAGE, (PRINT_PID_T) pid, errno, strerror(errno));
1011     fclose(FilePT);
1012     return;
1013   }
1014 
1015   printf("Sent TERM signal to %s, PID: %ld.\n", PACKAGE, (PRINT_PID_T) pid);
1016   fclose(FilePT);
1017 
1018 }
1019 
1020 /* SYSRUN FUNCTION - JONAS (01.07.2000) */
1021 
sysrun(const char * const CmdPT,char * ResultPT,unsigned long int Len)1022 signed short int sysrun(const char *const CmdPT, char *ResultPT, unsigned long int Len) {
1023 
1024   FILE *FilePT = NULL;
1025   signed long int Result = 0;
1026 
1027   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Running shell command: %s", CmdPT);
1028 
1029   FilePT = popen(CmdPT, "r");
1030   if (FilePT == NULL) { return(ERROR); }
1031   Result = fread(ResultPT, sizeof(char), Len, FilePT);
1032   Result = ferror(FilePT);
1033   pclose(FilePT);
1034   if (Result != 0) { return(ERROR); }
1035   return(Result);
1036 
1037 }
1038 
1039 #if !WIN32
1040 /* SYSGETUIDFROMUSER FUNCTION - JONAS (01.07.2000) */
1041 
1042 #if HAVE_GETPWNAM
sysgetuidfromuser(const char * const UserPT)1043 uid_t sysgetuidfromuser(const char *const UserPT) {
1044 
1045   struct passwd *PasswdPT = NULL;
1046 
1047   assert(UserPT != NULL);
1048 
1049   PasswdPT = getpwnam(UserPT);
1050   if (PasswdPT == NULL) {
1051     aerrno = AENOMATCH;
1052     return(0);
1053   }
1054   aerrno = AESUCCESS;
1055   return(PasswdPT->pw_uid);
1056 }
1057 #else
sysgetuidfromuser(const char * const UserPT)1058 uid_t sysgetuidfromuser(const char *const UserPT) {
1059 
1060   FILE *FilePT = NULL;
1061   char *TempPT = NULL;
1062   char Line[LINELEN+1] = "";
1063   uid_t UID = 0;
1064 
1065   assert(UserPT != NULL);
1066 
1067   FilePT = fopen(PASSWD_FILE, "r");
1068   if (FilePT == NULL) {
1069     sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", PASSWD_FILE, errno, strerror(errno));
1070     aerrno = AENORESOURCE;
1071     return(0);
1072   }
1073 
1074   FOREVERLOOP {
1075 
1076     TempPT = fgets(Line, LINELEN, FilePT);
1077     if (TempPT == NULL) { break; }
1078 
1079     while ((TempPT = strchr(Line, '\r')) != NULL) { *TempPT = '\0'; }
1080     while ((TempPT = strchr(Line, '\n')) != NULL) { *TempPT = '\0'; }
1081     if ((Line[0] == '#') || (Line[0] == ';') || (Line[0] == '\0')) { continue; }
1082 
1083     TempPT = strtok(Line, ":");
1084     if (TempPT == NULL) { continue; }
1085     if (strcmp(TempPT, UserPT) != FALSE) { continue; }
1086 
1087     TempPT = strtok(NULL, ":");
1088     if (TempPT == NULL) { continue; }
1089 
1090     TempPT = strtok(NULL, ":");
1091     if (TempPT == NULL) { continue; }
1092 
1093     UID = strtoul(TempPT, NULL, 0);
1094     fclose(FilePT);
1095     aerrno = AESUCCESS;
1096     return(UID);
1097   }
1098   fclose(FilePT);
1099   aerrno = AENOMATCH;
1100   return(0);
1101 }
1102 #endif /* HAVE_GETPWNAM */
1103 #endif /* !WIN32 */
1104 
1105 #if !WIN32
1106 
1107 /* SYSGETNAMEFROMUSER FUNCTION - JONAS (01.07.2000) */
1108 
1109 #if HAVE_GETPWNAM
sysgetnamefromuser(const char * const UserPT)1110 char *sysgetnamefromuser(const char *const UserPT) {
1111 
1112   struct passwd *PasswdPT = NULL;
1113 
1114   assert(UserPT != NULL);
1115 
1116   PasswdPT = getpwnam(UserPT);
1117   if (PasswdPT == NULL) { return(NULL); }
1118   TMPSTRPT = strrealloc(TMPSTRPT, PasswdPT->pw_gecos);
1119   return(TMPSTRPT);
1120 
1121 }
1122 #else
sysgetnamefromuser(const char * const UserPT)1123 char *sysgetnamefromuser(const char *const UserPT) {
1124 
1125   FILE *FilePT = NULL;
1126   char *TempPT = NULL;
1127   char Line[LINELEN+1] = "";
1128 
1129   assert(UserPT != NULL);
1130 
1131   FilePT = fopen(PASSWD_FILE, "r");
1132   if (FilePT == NULL) {
1133     sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", PASSWD_FILE, errno, strerror(errno));
1134     return(NULL);
1135   }
1136 
1137   FOREVERLOOP {
1138 
1139     TempPT = fgets(Line, LINELEN, FilePT);
1140     if (TempPT == NULL) { break; }
1141 
1142     while ((TempPT = strchr(Line, '\r')) != NULL) { *TempPT = '\0'; }
1143     while ((TempPT = strchr(Line, '\n')) != NULL) { *TempPT = '\0'; }
1144     if ((Line[0] == '#') || (Line[0] == ';') || (Line[0] == '\0')) { continue; }
1145 
1146     TempPT = strtok(Line, ":");
1147     if (TempPT == NULL) { continue; }
1148     if (strcmp(TempPT, UserPT) != FALSE) { continue; }
1149 
1150     TempPT = strtok(NULL, ":");
1151     if (TempPT == NULL) { continue; }
1152 
1153     TempPT = strtok(NULL, ":");
1154     if (TempPT == NULL) { continue; }
1155 
1156     TempPT = strtok(NULL, ":");
1157     if (TempPT == NULL) { continue; }
1158 
1159     TempPT = strtok(NULL, ":");
1160     if (TempPT == NULL) { continue; }
1161 
1162     fclose(FilePT);
1163 
1164     TMPSTRPT = strrealloc(TMPSTRPT, TempPT);
1165     return(TMPSTRPT);
1166 
1167   }
1168   fclose(FilePT);
1169 
1170   return(NULL);
1171 
1172 }
1173 #endif
1174 #endif
1175 
1176 #if !WIN32
1177 
1178 /* SYSGETHOMEDIRFROMUSER FUNCTION - JONAS (01.07.2000) */
1179 
1180 #if HAVE_GETPWNAM
sysgethomedirfromuser(const char * const UserPT)1181 char *sysgethomedirfromuser(const char *const UserPT) {
1182 
1183   struct passwd *PasswdPT = NULL;
1184 
1185   assert(UserPT != NULL);
1186 
1187   PasswdPT = getpwnam(UserPT);
1188   if (PasswdPT == NULL) { return(NULL); }
1189   TMPSTRPT = strrealloc(TMPSTRPT, PasswdPT->pw_dir);
1190   return(TMPSTRPT);
1191 
1192 }
1193 #else
sysgethomedirfromuser(const char * const UserPT)1194 char *sysgethomedirfromuser(const char *const UserPT) {
1195 
1196   FILE *FilePT = NULL;
1197   char *TempPT = NULL;
1198   char Line[LINELEN+1] = "";
1199 
1200   assert(UserPT != NULL);
1201 
1202   FilePT = fopen(PASSWD_FILE, "r");
1203   if (FilePT == NULL) {
1204     sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", PASSWD_FILE, errno, strerror(errno));
1205     return(NULL);
1206   }
1207 
1208   FOREVERLOOP {
1209 
1210     TempPT = fgets(Line, LINELEN, FilePT);
1211     if (TempPT == NULL) { break; }
1212 
1213     while ((TempPT = strchr(Line, '\r')) != NULL) { *TempPT = '\0'; }
1214     while ((TempPT = strchr(Line, '\n')) != NULL) { *TempPT = '\0'; }
1215     if ((Line[0] == '#') || (Line[0] == ';') || (Line[0] == '\0')) { continue; }
1216 
1217     TempPT = strtok(Line, ":");
1218     if (TempPT == NULL) { continue; }
1219     if (strcmp(TempPT, UserPT) != FALSE) { continue; }
1220 
1221     TempPT = strtok(NULL, ":");
1222     if (TempPT == NULL) { continue; }
1223 
1224     TempPT = strtok(NULL, ":");
1225     if (TempPT == NULL) { continue; }
1226 
1227     TempPT = strtok(NULL, ":");
1228     if (TempPT == NULL) { continue; }
1229 
1230     TempPT = strtok(NULL, ":");
1231     if (TempPT == NULL) { continue; }
1232 
1233     TempPT = strtok(NULL, ":");
1234     if (TempPT == NULL) { continue; }
1235 
1236     fclose(FilePT);
1237 
1238     TMPSTRPT = strrealloc(TMPSTRPT, TempPT);
1239     return(TMPSTRPT);
1240 
1241   }
1242   fclose(FilePT);
1243 
1244   return(NULL);
1245 
1246 }
1247 #endif
1248 #endif
1249 
1250 #if !WIN32
1251 
1252 /* SYSGETGIDFROMGROUP FUNCTION - JONAS (01.07.2000) */
1253 
sysgetgidfromgroup(const char * const GroupPT)1254 gid_t sysgetgidfromgroup(const char *const GroupPT) {
1255 
1256   FILE *FilePT = NULL;
1257   char *TempPT = NULL;
1258   char Line[LINELEN+1] = "";
1259   gid_t GID = 0;
1260 
1261   assert(GroupPT != NULL);
1262 
1263   FilePT = fopen(GROUP_FILE, "r");
1264   if (FilePT == NULL) {
1265     sysprint(BITMASK_ERROR, "Unable to open %s: [%d] %s", GROUP_FILE, errno, strerror(errno));
1266     aerrno = AENORESOURCE;
1267     return(0);
1268   }
1269 
1270   FOREVERLOOP {
1271 
1272     TempPT = fgets(Line, LINELEN, FilePT);
1273     if (TempPT == NULL) { break; }
1274 
1275     while ((TempPT = strchr(Line, '\r')) != NULL) { *TempPT = '\0'; }
1276     while ((TempPT = strchr(Line, '\n')) != NULL) { *TempPT = '\0'; }
1277     if ((Line[0] == '#') || (Line[0] == ';') || (Line[0] == '\0')) { continue; }
1278 
1279     TempPT = strtok(Line, ":");
1280     if (TempPT == NULL) { continue; }
1281     if (strcmp(TempPT, GroupPT) != FALSE) { continue; }
1282 
1283     TempPT = strtok(NULL, ":");
1284     if (TempPT == NULL) { continue; }
1285 
1286     TempPT = strtok(NULL, ":");
1287     if (TempPT == NULL) { continue; }
1288 
1289     GID = strtoul(TempPT, NULL, 0);
1290     fclose(FilePT);
1291     aerrno = AESUCCESS;
1292     return(GID);
1293 
1294   }
1295   aerrno = AENOMATCH;
1296   fclose(FilePT);
1297   return(0);
1298 
1299 }
1300 #endif
1301 
1302 /* SYSCLEANUP FUNCTION - JONAS (01.07.2000) */
1303 
syscleanup(void)1304 void syscleanup(void) {
1305 
1306   unsigned short int Index = 0;
1307 
1308   DEBUGPRINT(BITMASK_DEBUG_SYSCALLS, "Freeing %s allocated memory.", __FILE__);
1309 
1310   FREE(TMPSTRPT);
1311   for (Index = 0 ; Index < PrintFileS_Len ; ++Index) {
1312     FREE(PrintFileS[Index].Name);
1313   }
1314 
1315 }
1316 
1317 /* NL_NET_ATON FUNCTION - JONAS (01.07.2000) */
1318 
nl_net_aton(const char * HostIPS,struct in_addr * InAddr)1319 int nl_net_aton(const char *HostIPS, struct in_addr *InAddr) {
1320 
1321   signed long int Result = INADDR_NONE;
1322 
1323   Result = inet_addr(HostIPS);
1324   if (Result == INADDR_NONE) {
1325     return(0);
1326   }
1327   InAddr->s_addr = Result;
1328   return(1);
1329 
1330 }
1331 
1332