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)
41 uint64_t *ip = (uint64_t *)buf;
43 int i, len64, done, rem;
45 /* Optimize by checking uint64_t for zero */
46 len64 = len >> sizeof(uint64_t);
47 for (i=0; i < len64; i++) {
52 done = len64 << sizeof(uint64_t); /* bytes already checked */
55 for (i = 0; i < rem; i++) {
64 /* Convert a string in place to lower case */
69 *str = tolower((int)(*str));
74 /* Convert spaces to non-space character.
75 * This makes scanf of fields containing spaces easier.
78 bash_spaces(char *str)
87 /* Convert non-space characters (0x1) back into spaces */
89 unbash_spaces(char *str)
99 char *encode_time(time_t time, char *buf)
104 if (localtime_r(&time, &tm)) {
105 n = sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
106 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
107 tm.tm_hour, tm.tm_min, tm.tm_sec);
113 * Concatenate a string (str) onto a pool memory buffer pm
115 int pm_strcat(POOLMEM **pm, char *str)
117 int pmlen = strlen(*pm);
118 int len = strlen(str) + 1;
120 *pm = check_pool_memory_size(*pm, pmlen + len);
121 memcpy(*pm+pmlen, str, len);
122 return pmlen + len - 1;
127 * Copy a string (str) into a pool memory buffer pm
129 int pm_strcpy(POOLMEM **pm, char *str)
131 int len = strlen(str) + 1;
133 *pm = check_pool_memory_size(*pm, len);
134 memcpy(*pm, str, len);
140 * Convert a JobStatus code into a human readable form
142 void jobstatus_to_ascii(int JobStatus, char *msg, int maxlen)
144 char *termstat, jstat[2];
151 case JS_ErrorTerminated:
152 termstat = _("Error");
155 termstat = _("Non-fatal error");
158 termstat = _("Canceled");
161 termstat = _("Verify differences");
164 jstat[0] = last_job.JobStatus;
169 bstrncpy(msg, termstat, maxlen);
173 * Convert Job Termination Status into a string
175 char *job_status_to_str(int stat)
183 case JS_ErrorTerminated:
188 str = _("Fatal Error");
194 str = _("Differences");
197 str = _("Unknown term code");
205 * Convert Job Type into a string
207 char *job_type_to_str(int type)
225 str = _("Unknown Type");
232 * Convert Job Level into a string
234 char *job_level_to_str(int level)
245 str = _("Incremental");
248 str = _("Differential");
253 case L_VERIFY_CATALOG:
254 str = _("Verify Catalog");
257 str = _("Verify Init Catalog");
259 case L_VERIFY_VOLUME_TO_CATALOG:
260 str = _("Verify Volume to Catalog");
263 str = _("Verify Data");
266 str = _("Unknown Job Level");
273 /***********************************************************************
274 * Encode the mode bits into a 10 character string like LS does
275 ***********************************************************************/
277 char *encode_mode(mode_t mode, char *buf)
281 *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' :
282 S_ISLNK(mode) ? 'l' : S_ISFIFO(mode) ? 'f' : S_ISSOCK(mode) ? 's' : '-';
283 *cp++ = mode & S_IRUSR ? 'r' : '-';
284 *cp++ = mode & S_IWUSR ? 'w' : '-';
285 *cp++ = (mode & S_ISUID
286 ? (mode & S_IXUSR ? 's' : 'S')
287 : (mode & S_IXUSR ? 'x' : '-'));
288 *cp++ = mode & S_IRGRP ? 'r' : '-';
289 *cp++ = mode & S_IWGRP ? 'w' : '-';
290 *cp++ = (mode & S_ISGID
291 ? (mode & S_IXGRP ? 's' : 'S')
292 : (mode & S_IXGRP ? 'x' : '-'));
293 *cp++ = mode & S_IROTH ? 'r' : '-';
294 *cp++ = mode & S_IWOTH ? 'w' : '-';
295 *cp++ = (mode & S_ISVTX
296 ? (mode & S_IXOTH ? 't' : 'T')
297 : (mode & S_IXOTH ? 'x' : '-'));
303 int do_shell_expansion(char *name, int name_len)
305 static char meta[] = "~\\$[]*?`'<>\"";
310 char line[MAXSTRING];
313 /* Check if any meta characters are present */
315 for (i = 0; i < len; i++) {
316 if (strchr(name, meta[i])) {
322 cmd = get_pool_memory(PM_FNAME);
324 if ((shellcmd = getenv("SHELL")) == NULL) {
325 shellcmd = "/bin/sh";
327 pm_strcpy(&cmd, shellcmd);
328 pm_strcat(&cmd, " -c \"echo ");
329 pm_strcat(&cmd, name);
330 pm_strcat(&cmd, "\"");
331 Dmsg1(400, "Send: %s\n", cmd);
332 bpipe = open_bpipe(cmd, 0, "r");
334 fgets(line, sizeof(line), bpipe->rfd);
335 strip_trailing_junk(line);
336 stat = close_bpipe(bpipe);
337 Dmsg2(400, "stat=%d got: %s\n", stat, line);
338 free_pool_memory(cmd);
340 bstrncpy(name, line, name_len);
347 /* MAKESESSIONKEY -- Generate session key with optional start
348 key. If mode is TRUE, the key will be
349 translated to a string, otherwise it is
350 returned as 16 binary bytes.
352 from SpeakFreely by John Walker */
354 void make_session_key(char *key, char *seed, int mode)
357 struct MD5Context md5c;
358 unsigned char md5key[16], md5key1[16];
366 /* The following creates a seed for the session key generator
367 based on a collection of volatile and environment-specific
368 information unlikely to be vulnerable (as a whole) to an
369 exhaustive search attack. If one of these items isn't
370 available on your machine, replace it with something
371 equivalent or, if you like, just delete it. */
373 sprintf(s + strlen(s), "%lu", (unsigned long) getpid());
374 sprintf(s + strlen(s), "%lu", (unsigned long) getppid());
375 getcwd(s + strlen(s), 256);
376 sprintf(s + strlen(s), "%lu", (unsigned long) clock());
377 sprintf(s + strlen(s), "%lu", (unsigned long) time(NULL));
379 sysinfo(SI_HW_SERIAL,s + strlen(s), 12);
381 #ifdef HAVE_GETHOSTID
382 sprintf(s + strlen(s), "%lu", (unsigned long) gethostid());
384 #ifdef HAVE_GETDOMAINNAME
385 getdomainname(s + strlen(s), 256);
387 gethostname(s + strlen(s), 256);
388 sprintf(s + strlen(s), "%u", (unsigned)getuid());
389 sprintf(s + strlen(s), "%u", (unsigned)getgid());
391 MD5Update(&md5c, (unsigned char *)s, strlen(s));
392 MD5Final(md5key, &md5c);
393 sprintf(s + strlen(s), "%lu", (unsigned long) ((time(NULL) + 65121) ^ 0x375F));
395 MD5Update(&md5c, (unsigned char *)s, strlen(s));
396 MD5Final(md5key1, &md5c);
397 #define nextrand (md5key[j] ^ md5key1[j])
399 for (j = k = 0; j < 16; j++) {
400 unsigned char rb = nextrand;
402 #define Rad16(x) ((x) + 'A')
403 key[k++] = Rad16((rb >> 4) & 0xF);
404 key[k++] = Rad16(rb & 0xF);
412 for (j = 0; j < 16; j++) {
422 * Edit job codes into main command line
425 * %d = Director's name
428 * %j = Unique Job name
430 * %n = Unadorned Job name
431 * %t = Job type (Backup, ...)
434 * omsg = edited output message
435 * imsg = input string containing edit codes (%x)
436 * to = recepients list
439 POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
443 char name[MAX_NAME_LENGTH];
446 Dmsg1(200, "edit_job_codes: %s\n", imsg);
447 for (p=imsg; *p; p++) {
454 str = jcr->client_name;
460 str = my_name; /* Director's name */
463 str = job_status_to_str(jcr->JobStatus);
466 bsnprintf(add, sizeof(add), "%d", jcr->JobId);
469 case 'j': /* Job name */
473 str = job_level_to_str(jcr->JobLevel);
476 bstrncpy(name, jcr->Job, sizeof(name));
477 /* There are three periods after the Job name */
478 for (int i=0; i<3; i++) {
479 if ((str=strrchr(name, '.')) != NULL) {
489 str = job_type_to_str(jcr->JobType);
503 Dmsg1(1200, "add_str %s\n", str);
504 pm_strcat(&omsg, str);
505 Dmsg1(1200, "omsg=%s\n", omsg);
510 void set_working_directory(char *wd)
512 struct stat stat_buf;
515 Emsg0(M_ERROR_TERM, 0, _("Working directory not defined. Cannot continue.\n"));
517 if (stat(wd, &stat_buf) != 0) {
518 Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" not found. Cannot continue.\n"),
521 if (!S_ISDIR(stat_buf.st_mode)) {
522 Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" is not a directory. Cannot continue.\n"),
525 working_directory = wd; /* set global */