2 * util.c miscellaneous utility subroutines for Bacula
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
31 #include "findlib/find.h"
34 * Various Bacula Utility subroutines
38 /* Return true of buffer has all zero bytes */
39 int is_buf_zero(char *buf, int len)
43 int i, len64, done, rem;
49 /* Optimize by checking uint64_t for zero */
50 len64 = len / sizeof(uint64_t);
51 for (i=0; i < len64; i++) {
56 done = len64 * sizeof(uint64_t); /* bytes already checked */
59 for (i = 0; i < rem; i++) {
68 /* Convert a string in place to lower case */
73 *str = tolower((int)(*str));
78 /* Convert spaces to non-space character.
79 * This makes scanf of fields containing spaces easier.
82 bash_spaces(char *str)
91 /* Convert non-space characters (0x1) back into spaces */
93 unbash_spaces(char *str)
103 char *encode_time(time_t time, char *buf)
108 if (localtime_r(&time, &tm)) {
109 n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
110 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
111 tm.tm_hour, tm.tm_min, tm.tm_sec);
117 * Concatenate a string (str) onto a pool memory buffer pm
119 int pm_strcat(POOLMEM **pm, char *str)
121 int pmlen = strlen(*pm);
122 int len = strlen(str) + 1;
124 *pm = check_pool_memory_size(*pm, pmlen + len);
125 memcpy(*pm+pmlen, str, len);
126 return pmlen + len - 1;
131 * Copy a string (str) into a pool memory buffer pm
133 int pm_strcpy(POOLMEM **pm, char *str)
135 int len = strlen(str) + 1;
137 *pm = check_pool_memory_size(*pm, len);
138 memcpy(*pm, str, len);
144 * Convert a JobStatus code into a human readable form
146 void jobstatus_to_ascii(int JobStatus, char *msg, int maxlen)
153 jobstat = _("Created");
156 jobstat = _("Running");
159 jobstat = _("Blocked");
165 case JS_ErrorTerminated:
166 jobstat = _("Error");
169 jobstat = _("Non-fatal error");
172 jobstat = _("Canceled");
175 jobstat = _("Verify differences");
178 jobstat = _("Waiting on FD");
181 jobstat = _("Wait on SD");
184 jobstat = _("Wait for new Volume");
187 jobstat = _("Waiting for mount");
189 case JS_WaitStoreRes:
190 jobstat = _("Waiting for Storage resource");
193 jobstat = _("Waiting for Job resource");
195 case JS_WaitClientRes:
196 jobstat = _("Waiting for Client resource");
199 jobstat = _("Waiting on Max Jobs");
201 case JS_WaitStartTime:
202 jobstat = _("Waiting for Start Time");
204 case JS_WaitPriority:
205 jobstat = _("Waiting on Priority");
209 if (JobStatus == 0) {
212 bsnprintf(buf, sizeof(buf), _("Unknown Job termination status=%d"), JobStatus);
217 bstrncpy(msg, jobstat, maxlen);
221 * Convert Job Termination Status into a string
223 char *job_status_to_str(int stat)
231 case JS_ErrorTerminated:
236 str = _("Fatal Error");
242 str = _("Differences");
245 str = _("Unknown term code");
253 * Convert Job Type into a string
255 char *job_type_to_str(int type)
273 str = _("Unknown Type");
280 * Convert Job Level into a string
282 char *job_level_to_str(int level)
293 str = _("Incremental");
296 str = _("Differential");
301 case L_VERIFY_CATALOG:
302 str = _("Verify Catalog");
305 str = _("Verify Init Catalog");
307 case L_VERIFY_VOLUME_TO_CATALOG:
308 str = _("Verify Volume to Catalog");
310 case L_VERIFY_DISK_TO_CATALOG:
311 str = _("Verify Disk to Catalog");
314 str = _("Verify Data");
317 str = _("Unknown Job Level");
324 /***********************************************************************
325 * Encode the mode bits into a 10 character string like LS does
326 ***********************************************************************/
328 char *encode_mode(mode_t mode, char *buf)
332 *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
333 S_ISLNK(mode) ? 'l' : S_ISFIFO(mode) ? 'f' : S_ISSOCK(mode) ? 's' : '-';
334 *cp++ = mode & S_IRUSR ? 'r' : '-';
335 *cp++ = mode & S_IWUSR ? 'w' : '-';
336 *cp++ = (mode & S_ISUID
337 ? (mode & S_IXUSR ? 's' : 'S')
338 : (mode & S_IXUSR ? 'x' : '-'));
339 *cp++ = mode & S_IRGRP ? 'r' : '-';
340 *cp++ = mode & S_IWGRP ? 'w' : '-';
341 *cp++ = (mode & S_ISGID
342 ? (mode & S_IXGRP ? 's' : 'S')
343 : (mode & S_IXGRP ? 'x' : '-'));
344 *cp++ = mode & S_IROTH ? 'r' : '-';
345 *cp++ = mode & S_IWOTH ? 'w' : '-';
346 *cp++ = (mode & S_ISVTX
347 ? (mode & S_IXOTH ? 't' : 'T')
348 : (mode & S_IXOTH ? 'x' : '-'));
354 int do_shell_expansion(char *name, int name_len)
356 static char meta[] = "~\\$[]*?`'<>\"";
361 char line[MAXSTRING];
364 /* Check if any meta characters are present */
366 for (i = 0; i < len; i++) {
367 if (strchr(name, meta[i])) {
373 cmd = get_pool_memory(PM_FNAME);
375 if ((shellcmd = getenv("SHELL")) == NULL) {
376 shellcmd = "/bin/sh";
378 pm_strcpy(&cmd, shellcmd);
379 pm_strcat(&cmd, " -c \"echo ");
380 pm_strcat(&cmd, name);
381 pm_strcat(&cmd, "\"");
382 Dmsg1(400, "Send: %s\n", cmd);
383 bpipe = open_bpipe(cmd, 0, "r");
385 fgets(line, sizeof(line), bpipe->rfd);
386 strip_trailing_junk(line);
387 stat = close_bpipe(bpipe);
388 Dmsg2(400, "stat=%d got: %s\n", stat, line);
389 free_pool_memory(cmd);
391 bstrncpy(name, line, name_len);
398 /* MAKESESSIONKEY -- Generate session key with optional start
399 key. If mode is TRUE, the key will be
400 translated to a string, otherwise it is
401 returned as 16 binary bytes.
403 from SpeakFreely by John Walker */
405 void make_session_key(char *key, char *seed, int mode)
408 struct MD5Context md5c;
409 unsigned char md5key[16], md5key1[16];
417 /* The following creates a seed for the session key generator
418 based on a collection of volatile and environment-specific
419 information unlikely to be vulnerable (as a whole) to an
420 exhaustive search attack. If one of these items isn't
421 available on your machine, replace it with something
422 equivalent or, if you like, just delete it. */
424 sprintf(s + strlen(s), "%lu", (unsigned long) getpid());
425 sprintf(s + strlen(s), "%lu", (unsigned long) getppid());
426 getcwd(s + strlen(s), 256);
427 sprintf(s + strlen(s), "%lu", (unsigned long) clock());
428 sprintf(s + strlen(s), "%lu", (unsigned long) time(NULL));
430 sysinfo(SI_HW_SERIAL,s + strlen(s), 12);
432 #ifdef HAVE_GETHOSTID
433 sprintf(s + strlen(s), "%lu", (unsigned long) gethostid());
435 #ifdef HAVE_GETDOMAINNAME
436 getdomainname(s + strlen(s), 256);
438 gethostname(s + strlen(s), 256);
439 sprintf(s + strlen(s), "%u", (unsigned)getuid());
440 sprintf(s + strlen(s), "%u", (unsigned)getgid());
442 MD5Update(&md5c, (unsigned char *)s, strlen(s));
443 MD5Final(md5key, &md5c);
444 sprintf(s + strlen(s), "%lu", (unsigned long) ((time(NULL) + 65121) ^ 0x375F));
446 MD5Update(&md5c, (unsigned char *)s, strlen(s));
447 MD5Final(md5key1, &md5c);
448 #define nextrand (md5key[j] ^ md5key1[j])
450 for (j = k = 0; j < 16; j++) {
451 unsigned char rb = nextrand;
453 #define Rad16(x) ((x) + 'A')
454 key[k++] = Rad16((rb >> 4) & 0xF);
455 key[k++] = Rad16(rb & 0xF);
463 for (j = 0; j < 16; j++) {
473 * Edit job codes into main command line
476 * %d = Director's name
479 * %j = Unique Job name
481 * %n = Unadorned Job name
482 * %t = Job type (Backup, ...)
485 * omsg = edited output message
486 * imsg = input string containing edit codes (%x)
487 * to = recepients list
490 POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
494 char name[MAX_NAME_LENGTH];
497 Dmsg1(200, "edit_job_codes: %s\n", imsg);
498 for (p=imsg; *p; p++) {
505 str = jcr->client_name;
511 str = my_name; /* Director's name */
514 str = job_status_to_str(jcr->JobStatus);
517 bsnprintf(add, sizeof(add), "%d", jcr->JobId);
520 case 'j': /* Job name */
524 str = job_level_to_str(jcr->JobLevel);
527 bstrncpy(name, jcr->Job, sizeof(name));
528 /* There are three periods after the Job name */
529 for (int i=0; i<3; i++) {
530 if ((str=strrchr(name, '.')) != NULL) {
540 str = job_type_to_str(jcr->JobType);
554 Dmsg1(1200, "add_str %s\n", str);
555 pm_strcat(&omsg, str);
556 Dmsg1(1200, "omsg=%s\n", omsg);
561 void set_working_directory(char *wd)
563 struct stat stat_buf;
566 Emsg0(M_ERROR_TERM, 0, _("Working directory not defined. Cannot continue.\n"));
568 if (stat(wd, &stat_buf) != 0) {
569 Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" not found. Cannot continue.\n"),
572 if (!S_ISDIR(stat_buf.st_mode)) {
573 Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" is not a directory. Cannot continue.\n"),
576 working_directory = wd; /* set global */