]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/util.c
Server address binding + bscan updates -- see kes25Sep02
[bacula/bacula] / bacula / src / lib / util.c
1 /*
2  *   util.c  miscellaneous utility subroutines for Bacula
3  * 
4  *    Kern Sibbald, MM
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "jcr.h"
31 #include "findlib/find.h"
32
33 /*
34  * Various Bacula Utility subroutines
35  *
36  */
37
38 /*
39  * Convert a string to btime_t (64 bit seconds)
40  * Returns 0: if error
41            1: if OK, and value stored in value
42  */
43 int string_to_btime(char *str, btime_t *value)
44 {
45    int i, ch, len;
46    double val;
47    static int  mod[] = {'*', 's', 'n', 'h', 'd', 'w', 'm', 'q', 'y', 0};
48    static int mult[] = {1,    1,  60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
49                   60*60*24*91, 60*60*24*365};
50
51    /* Look for modifier */
52    len = strlen(str);
53    ch = str[len - 1];
54    i = 0;
55    if (ISALPHA(ch)) {
56       if (ISUPPER(ch)) {
57          ch = tolower(ch);
58       }
59       while (mod[++i] != 0) {
60          if (ch == mod[i]) {
61             len--;
62             str[len] = 0; /* strip modifier */
63             break;
64          }
65       }
66    }
67    if (mod[i] == 0 || !is_a_number(str)) {
68       return 0;
69    }
70    val = strtod(str, NULL);
71    if (errno != 0 || val < 0) {
72       return 0;
73    }
74    *value = (btime_t)(val * mult[i]);
75    return 1;
76
77 }
78
79 char *edit_btime(btime_t val, char *buf)
80 {
81    char mybuf[30];
82    static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
83    static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
84    int i;
85    uint32_t times;
86
87    *buf = 0;
88    for (i=0; i<5; i++) {
89       times = val / mult[i];
90       if (times > 0) {
91          val = val - (btime_t)times * mult[i];
92          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
93          strcat(buf, mybuf);
94       }
95    }
96    if (val == 0 && strlen(buf) == 0) {     
97       strcat(buf, "0 secs");
98    } else if (val != 0) {
99       sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
100       strcat(buf, mybuf);
101    }
102    return buf;
103 }
104
105 /*
106  * Check if specified string is a number or not.
107  *  Taken from SQLite, cool, thanks.
108  */
109 int is_a_number(const char *n)
110 {
111    int digit_seen = 0;
112
113    if( *n == '-' || *n == '+' ) {
114       n++;
115    }
116    while (ISDIGIT(*n)) {
117       digit_seen = 1;
118       n++;
119    }
120    if (digit_seen && *n == '.') {
121       n++;
122       while (ISDIGIT(*n)) { n++; }
123    }
124    if (digit_seen && (*n == 'e' || *n == 'E')
125        && (ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && ISDIGIT(n[2])))) {
126       n += 2;                         /* skip e- or e+ or e digit */
127       while (ISDIGIT(*n)) { n++; }
128    }
129    return digit_seen && *n==0;
130 }
131
132
133 /*
134  * Edit an integer number with commas, the supplied buffer
135  * must be at least 27 bytes long.  The incoming number
136  * is always widened to 64 bits.
137  */
138 char *edit_uint64_with_commas(uint64_t val, char *buf)
139 {
140    sprintf(buf, "%" lld, val);
141    return add_commas(buf, buf);
142 }
143
144 /*
145  * Edit an integer number, the supplied buffer
146  * must be at least 27 bytes long.  The incoming number
147  * is always widened to 64 bits.
148  */
149 char *edit_uint64(uint64_t val, char *buf)
150 {
151    sprintf(buf, "%" lld, val);
152    return buf;
153 }
154
155
156 /*
157  * Add commas to a string, which is presumably
158  * a number.  
159  */
160 char *add_commas(char *val, char *buf)
161 {
162    int len, nc;
163    char *p, *q;
164    int i;
165
166    if (val != buf) {
167       strcpy(buf, val);
168    }
169    len = strlen(buf);
170    if (len < 1) {
171       len = 1;
172    }
173    nc = (len - 1) / 3;
174    p = buf+len;
175    q = p + nc;
176    *q-- = *p--;
177    for ( ; nc; nc--) {
178       for (i=0; i < 3; i++) {
179           *q-- = *p--;
180       }
181       *q-- = ',';
182    }   
183    return buf;
184 }
185
186
187 /* Convert a string in place to lower case */
188 void lcase(char *str)
189 {
190    while (*str) {
191       if (ISUPPER(*str))
192          *str = tolower((int)(*str));
193        str++;
194    }
195 }
196
197 /* Convert spaces to non-space character. 
198  * This makes scanf of fields containing spaces easier.
199  */
200 void
201 bash_spaces(char *str)
202 {
203    while (*str) {
204       if (*str == ' ')
205          *str = 0x1;
206       str++;
207    }
208 }
209
210 /* Convert non-space characters (0x1) back into spaces */
211 void
212 unbash_spaces(char *str)
213 {
214    while (*str) {
215      if (*str == 0x1)
216         *str = ' ';
217      str++;
218    }
219 }
220
221 /* Strip any trailing junk from the command */
222 void strip_trailing_junk(char *cmd)
223 {
224    char *p;
225    p = cmd + strlen(cmd) - 1;
226
227    /* strip trailing junk from command */
228    while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
229       *p-- = 0;
230 }
231
232 /* Strip any trailing slashes from a directory path */
233 void strip_trailing_slashes(char *dir)
234 {
235    char *p;
236    p = dir + strlen(dir) - 1;
237
238    /* strip trailing slashes */
239    while ((p >= dir) && (*p == '/'))
240       *p-- = 0;
241 }
242
243 /*
244  * Skip spaces
245  *  Returns: 0 on failure (EOF)             
246  *           1 on success
247  *           new address in passed parameter 
248  */
249 int skip_spaces(char **msg)
250 {
251    char *p = *msg;
252    if (!p) {
253       return 0;
254    }
255    while (*p && *p == ' ') {
256       p++;
257    }
258    *msg = p;
259    return *p ? 1 : 0;
260 }
261
262 /*
263  * Skip nonspaces
264  *  Returns: 0 on failure (EOF)             
265  *           1 on success
266  *           new address in passed parameter 
267  */
268 int skip_nonspaces(char **msg)
269 {
270    char *p = *msg;
271
272    if (!p) {
273       return 0;
274    }
275    while (*p && *p != ' ') {
276       p++;
277    }
278    *msg = p;
279    return *p ? 1 : 0;
280 }
281
282 /* folded search for string - case insensitive */
283 int
284 fstrsch(char *a, char *b)   /* folded case search */
285 {
286    register char *s1,*s2;
287    register char c1, c2;
288
289    s1=a;
290    s2=b;
291    while (*s1) {                      /* do it the fast way */
292       if ((*s1++ | 0x20) != (*s2++ | 0x20))
293          return 0;                    /* failed */
294    }
295    while (*a) {                       /* do it over the correct slow way */
296       if (ISUPPER(c1 = *a)) {
297          c1 = tolower((int)c1);
298       }
299       if (ISUPPER(c2 = *b)) {
300          c2 = tolower((int)c2);
301       }
302       if (c1 != c2) {
303          return 0;
304       }
305       a++;
306       b++;
307    }
308    return 1;
309 }
310
311
312 char *encode_time(time_t time, char *buf)
313 {
314    struct tm tm;
315    int n = 0;
316
317    if (localtime_r(&time, &tm)) {
318       n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
319                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
320                    tm.tm_hour, tm.tm_min, tm.tm_sec);
321    }
322    return buf+n;
323 }
324
325 /*
326  * Concatenate a string (str) onto a pool memory buffer pm
327  */
328 void pm_strcat(POOLMEM **pm, char *str)
329 {
330    int pmlen = strlen(*pm);
331    int len = strlen(str) + 1;
332
333    *pm = check_pool_memory_size(*pm, pmlen + len);
334    memcpy(*pm+pmlen, str, len);
335 }
336
337
338 /*
339  * Copy a string (str) into a pool memory buffer pm
340  */
341 void pm_strcpy(POOLMEM **pm, char *str)
342 {
343    int len = strlen(str) + 1;
344
345    *pm = check_pool_memory_size(*pm, len);
346    memcpy(*pm, str, len);
347 }
348
349
350 /*
351  * Convert a JobStatus code into a human readable form
352  */
353 void jobstatus_to_ascii(int JobStatus, char *msg, int maxlen)
354 {
355    char *termstat, jstat[2];
356
357    switch (JobStatus) {
358       case JS_Terminated:
359          termstat = _("OK");
360          break;
361      case JS_FatalError:
362      case JS_ErrorTerminated:
363          termstat = _("Error");
364          break;
365      case JS_Error:
366          termstat = _("Non-fatal error");
367          break;
368      case JS_Cancelled:
369          termstat = _("Cancelled");
370          break;
371      case JS_Differences:
372          termstat = _("Verify differences");
373          break;
374      default:
375          jstat[0] = last_job.JobStatus;
376          jstat[1] = 0;
377          termstat = jstat;
378          break;
379    }
380    strncpy(msg, termstat, maxlen);
381    msg[maxlen-1] = 0;
382 }
383
384 /*
385  * Convert Job Termination Status into a string
386  */
387 char *job_status_to_str(int stat) 
388 {
389    char *str;
390
391    switch (stat) {
392    case JS_Terminated:
393       str = _("OK");
394       break;
395    case JS_ErrorTerminated:
396    case JS_Error:
397       str = _("Error");
398       break;
399    case JS_FatalError:
400       str = _("Fatal Error");
401       break;
402    case JS_Cancelled:
403       str = _("Cancelled");
404       break;
405    case JS_Differences:
406       str = _("Differences");
407       break;
408    default:
409       str = _("Unknown term code");
410       break;
411    }
412    return str;
413 }
414
415
416 /*
417  * Convert Job Type into a string
418  */
419 char *job_type_to_str(int type) 
420 {
421    char *str;
422
423    switch (type) {
424    case JT_BACKUP:
425       str = _("Backup");
426       break;
427    case JT_VERIFY:
428       str = _("Verify");
429       break;
430    case JT_RESTORE:
431       str = _("Restore");
432       break;
433    case JT_ADMIN:
434       str = _("Admin");
435       break;
436    default:
437       str = _("Unknown Type");
438       break;
439    }
440    return str;
441 }
442
443 /*
444  * Convert Job Level into a string
445  */
446 char *job_level_to_str(int level) 
447 {
448    char *str;
449
450    switch (level) {
451    case L_FULL:
452       str = _("Full");
453       break;
454    case L_INCREMENTAL:
455       str = _("Incremental");
456       break;
457    case L_DIFFERENTIAL:
458       str = _("Differential");
459       break;
460    case L_LEVEL:
461       str = _("Level");
462       break;
463    case L_SINCE:
464       str = _("Since");
465       break;
466    case L_VERIFY_CATALOG:
467       str = _("Verify Catalog");
468       break;
469    case L_VERIFY_INIT:
470       str = _("Verify Init Catalog");
471       break;
472    case L_VERIFY_VOLUME_TO_CATALOG:
473       str = _("Verify Volume to Catalog");
474       break;
475    case L_VERIFY_DATA:
476       str = _("Verify Data");
477       break;
478    default:
479       str = _("Unknown Job Level");
480       break;
481    }
482    return str;
483 }
484
485
486 /***********************************************************************
487  * Encode the mode bits into a 10 character string like LS does
488  ***********************************************************************/
489
490 char *encode_mode(mode_t mode, char *buf)
491 {
492   char *cp = buf;  
493
494   *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
495           S_ISLNK(mode) ? 'l' : '-';
496   *cp++ = mode & S_IRUSR ? 'r' : '-';
497   *cp++ = mode & S_IWUSR ? 'w' : '-';
498   *cp++ = (mode & S_ISUID
499                ? (mode & S_IXUSR ? 's' : 'S')
500                : (mode & S_IXUSR ? 'x' : '-'));
501   *cp++ = mode & S_IRGRP ? 'r' : '-';
502   *cp++ = mode & S_IWGRP ? 'w' : '-';
503   *cp++ = (mode & S_ISGID
504                ? (mode & S_IXGRP ? 's' : 'S')
505                : (mode & S_IXGRP ? 'x' : '-'));
506   *cp++ = mode & S_IROTH ? 'r' : '-';
507   *cp++ = mode & S_IWOTH ? 'w' : '-';
508   *cp++ = (mode & S_ISVTX
509                ? (mode & S_IXOTH ? 't' : 'T')
510                : (mode & S_IXOTH ? 'x' : '-'));
511   *cp = '\0';
512   return cp;
513 }
514
515
516 int do_shell_expansion(char *name)
517 {
518 /*  ****FIXME***** this should work for Win32 too */
519 #define UNIX
520 #ifdef UNIX
521 #ifndef PATH_MAX
522 #define PATH_MAX 512
523 #endif
524
525    int pid, wpid, stat;
526    int waitstatus;
527    char *shellcmd;
528    void (*istat)(int), (*qstat)(int);
529    int i;
530    char echout[PATH_MAX + 256];
531    int pfd[2];
532    static char meta[] = "~\\$[]*?`'<>\"";
533    int found = FALSE;
534    int len;
535
536    /* Check if any meta characters are present */
537    len = strlen(meta);
538    for (i = 0; i < len; i++) {
539       if (strchr(name, meta[i])) {
540          found = TRUE;
541          break;
542       }
543    }
544    stat = 0;
545    if (found) {
546 #ifdef nt
547        /* If the filename appears to be a DOS filename,
548           convert all backward slashes \ to Unix path
549           separators / and insert a \ infront of spaces. */
550        len = strlen(name);
551        if (len >= 3 && name[1] == ':' && name[2] == '\\') {
552           for (i=2; i<len; i++)
553              if (name[i] == '\\')
554                 name[i] = '/';
555        }
556 #else
557        /* Pass string off to the shell for interpretation */
558        if (pipe(pfd) == -1)
559           return 0;
560        switch(pid = fork()) {
561        case -1:
562           break;
563
564        case 0:                            /* child */
565           /* look for shell */
566           if ((shellcmd = getenv("SHELL")) == NULL)
567              shellcmd = "/bin/sh";
568           close(1); dup(pfd[1]);          /* attach pipes to stdin and stdout */
569           close(2); dup(pfd[1]);
570           for (i = 3; i < 32; i++)        /* close everything else */
571              close(i);
572           strcpy(echout, "echo ");        /* form echo command */
573           strcat(echout, name);
574           execl(shellcmd, shellcmd, "-c", echout, NULL); /* give to shell */
575           exit(127);                      /* shouldn't get here */
576
577        default:                           /* parent */
578           /* read output from child */
579           i = read(pfd[0], echout, sizeof echout);
580           echout[--i] = 0;                /* set end of string */
581           /* look for first word or first line. */
582           while (--i >= 0) {
583              if (echout[i] == ' ' || echout[i] == '\n')
584                 echout[i] = 0;            /* keep only first one */
585           }
586           istat = signal(SIGINT, SIG_IGN);
587           qstat = signal(SIGQUIT, SIG_IGN);
588           /* wait for child to exit */
589           while ((wpid = wait(&waitstatus)) != pid && wpid != -1)
590              { ; }
591           signal(SIGINT, istat);
592           signal(SIGQUIT, qstat);
593           strcpy(name, echout);
594           stat = 1;
595           break;
596        }
597        close(pfd[0]);                     /* close pipe */
598        close(pfd[1]);
599 #endif /* nt */
600    }
601    return stat;
602
603 #endif /* UNIX */
604
605 #if  MSC | MSDOS | __WATCOMC__
606
607    char prefix[100], *env, *getenv();
608
609    /* Home directory reference? */
610    if (*name == '~' && (env=getenv("HOME"))) {
611       strcpy(prefix, env);            /* copy HOME directory name */
612       name++;                         /* skip over ~ in name */
613       strcat(prefix, name);
614       name--;                         /* get back to beginning */
615       strcpy(name, prefix);           /* move back into name */
616    }
617    return 1;
618 #endif
619
620 }
621
622 #define MAX_ARGV 100
623 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_arg);
624
625 /*
626  * Run an external program. Optionally wait a specified number
627  *   of seconds. Program killed if wait exceeded. Optionally
628  *   return the output from the program (normally a single line).
629  */
630 int run_program(char *prog, int wait, POOLMEM *results)
631 {
632    int stat = ETIME;
633    int chldstatus = 0;
634    pid_t pid1, pid2 = 0;
635    int pfd[2];
636    char *bargv[MAX_ARGV];
637    int bargc;
638
639    
640    build_argc_argv(prog, &bargc, bargv, MAX_ARGV);
641 #ifdef xxxxxxxxxx
642    printf("argc=%d\n", bargc);
643    int i;
644    for (i=0; i<bargc; i++) {
645       printf("argc=%d argv=%s\n", i, bargv[i]);
646    }
647 #endif
648
649    if (results && pipe(pfd) == -1) {
650       return errno;
651    }
652    /* Start worker process */
653    switch (pid1 = fork()) {
654    case -1:
655       break;
656
657    case 0:                            /* child */
658 //    printf("execl of %s\n", prog);
659       if (results) {
660          close(1); dup(pfd[1]);       /* attach pipes to stdin and stdout */
661          close(2); dup(pfd[1]);
662       }
663       execvp(bargv[0], bargv);
664       exit(errno);                   /* shouldn't get here */
665
666    default:                           /* parent */
667       /* start timer process */
668       if (wait > 0) {
669          switch (pid2=fork()) {
670          case -1:
671             break;
672          case 0:                         /* child 2 */
673             /* Time the worker process */  
674             sleep(wait);
675             if (kill(pid1, SIGTERM) == 0) { /* time expired kill it */
676                exit(0);
677             }
678             sleep(3);
679             kill(pid1, SIGKILL);
680             exit(0);
681          default:                        /* parent */
682             break;
683          }
684       }
685
686       /* Parent continues here */
687       int i;
688       if (results) {
689          i = read(pfd[0], results, sizeof_pool_memory(results) - 1);
690          if (--i < 0) {
691             i = 0;
692          }
693          results[i] = 0;                /* set end of string */
694       }
695       /* wait for worker child to exit */
696       for ( ;; ) {
697          pid_t wpid;
698          wpid = waitpid(pid1, &chldstatus, 0);         
699          if (wpid == pid1 || (errno != EINTR)) {
700             break;
701          }
702       }
703       if (WIFEXITED(chldstatus))
704          stat = WEXITSTATUS(chldstatus);
705
706       if (wait > 0) {
707          kill(pid2, SIGKILL);           /* kill off timer process */
708          waitpid(pid2, &chldstatus, 0); /* reap timer process */
709       }
710       if (results) { 
711          close(pfd[0]);              /* close pipe */
712          close(pfd[1]);
713       }
714       break;
715    }
716    return stat;
717 }
718
719 /*
720  * Build argc and argv from a string
721  */
722 static void build_argc_argv(char *cmd, int *bargc, char *bargv[], int max_argv)
723 {
724    int i, quote;
725    char *p, *q;
726    int argc = 0;
727
728    argc = 0;
729    for (i=0; i<max_argv; i++)
730       bargv[i] = NULL;
731
732    p = cmd;
733    quote = 0;
734    while  (*p && (*p == ' ' || *p == '\t'))
735       p++;
736    if (*p == '\"') {
737       quote = 1;
738       p++;
739    }
740    if (*p) {
741       while (*p && argc < MAX_ARGV) {
742          q = p;
743          if (quote) {
744             while (*q && *q != '\"')
745             q++;
746             quote = 0;
747          } else {
748             while (*q && *q != ' ')
749             q++;
750          }
751          if (*q)
752             *(q++) = '\0';
753          bargv[argc++] = p;
754          p = q;
755          while (*p && (*p == ' ' || *p == '\t'))
756             p++;
757          if (*p == '\"') {
758             quote = 1;
759             p++;
760          }
761       }
762    }
763    *bargc = argc;
764 }
765
766 /*  MAKESESSIONKEY  --  Generate session key with optional start
767                         key.  If mode is TRUE, the key will be
768                         translated to a string, otherwise it is
769                         returned as 16 binary bytes.
770
771     from SpeakFreely by John Walker */
772
773 void makeSessionKey(char *key, char *seed, int mode)
774 {
775      int j, k;
776      struct MD5Context md5c;
777      unsigned char md5key[16], md5key1[16];
778      char s[1024];
779
780      s[0] = 0;
781      if (seed != NULL) {
782         strcat(s, seed);
783      }
784
785      /* The following creates a seed for the session key generator
786         based on a collection of volatile and environment-specific
787         information unlikely to be vulnerable (as a whole) to an
788         exhaustive search attack.  If one of these items isn't
789         available on your machine, replace it with something
790         equivalent or, if you like, just delete it. */
791
792      sprintf(s + strlen(s), "%lu", (unsigned long) getpid());
793      sprintf(s + strlen(s), "%lu", (unsigned long) getppid());
794      getcwd(s + strlen(s), 256);
795      sprintf(s + strlen(s), "%lu", (unsigned long) clock());
796      sprintf(s + strlen(s), "%lu", (unsigned long) time(NULL));
797 #ifdef Solaris
798      sysinfo(SI_HW_SERIAL,s + strlen(s), 12);
799 #endif
800 #ifdef HAVE_GETHOSTID
801      sprintf(s + strlen(s), "%lu", (unsigned long) gethostid());
802 #endif
803 #ifdef HAVE_GETDOMAINNAME
804      getdomainname(s + strlen(s), 256);
805 #endif
806      gethostname(s + strlen(s), 256);
807      sprintf(s + strlen(s), "%u", (unsigned)getuid());
808      sprintf(s + strlen(s), "%u", (unsigned)getgid());
809      MD5Init(&md5c);
810      MD5Update(&md5c, (unsigned char *)s, strlen(s));
811      MD5Final(md5key, &md5c);
812      sprintf(s + strlen(s), "%lu", (unsigned long) ((time(NULL) + 65121) ^ 0x375F));
813      MD5Init(&md5c);
814      MD5Update(&md5c, (unsigned char *)s, strlen(s));
815      MD5Final(md5key1, &md5c);
816 #define nextrand    (md5key[j] ^ md5key1[j])
817      if (mode) {
818         for (j = k = 0; j < 16; j++) {
819             unsigned char rb = nextrand;
820
821 #define Rad16(x) ((x) + 'A')
822             key[k++] = Rad16((rb >> 4) & 0xF);
823             key[k++] = Rad16(rb & 0xF);
824 #undef Rad16
825             if (j & 1) {
826                  key[k++] = '-';
827             }
828         }
829         key[--k] = 0;
830      } else {
831         for (j = 0; j < 16; j++) {
832             key[j] = nextrand;
833         }
834      }
835 }
836 #undef nextrand