+
+
+/* MAKESESSIONKEY -- Generate session key with optional start
+ key. If mode is TRUE, the key will be
+ translated to a string, otherwise it is
+ returned as 16 binary bytes.
+
+ from SpeakFreely by John Walker */
+
+void make_session_key(char *key, char *seed, int mode)
+{
+ int j, k;
+ struct MD5Context md5c;
+ unsigned char md5key[16], md5key1[16];
+ char s[1024];
+
+ s[0] = 0;
+ if (seed != NULL) {
+ strcat(s, seed);
+ }
+
+ /* The following creates a seed for the session key generator
+ based on a collection of volatile and environment-specific
+ information unlikely to be vulnerable (as a whole) to an
+ exhaustive search attack. If one of these items isn't
+ available on your machine, replace it with something
+ equivalent or, if you like, just delete it. */
+
+ sprintf(s + strlen(s), "%lu", (unsigned long) getpid());
+ sprintf(s + strlen(s), "%lu", (unsigned long) getppid());
+ getcwd(s + strlen(s), 256);
+ sprintf(s + strlen(s), "%lu", (unsigned long) clock());
+ sprintf(s + strlen(s), "%lu", (unsigned long) time(NULL));
+#ifdef Solaris
+ sysinfo(SI_HW_SERIAL,s + strlen(s), 12);
+#endif
+#ifdef HAVE_GETHOSTID
+ sprintf(s + strlen(s), "%lu", (unsigned long) gethostid());
+#endif
+#ifdef HAVE_GETDOMAINNAME
+ getdomainname(s + strlen(s), 256);
+#endif
+ gethostname(s + strlen(s), 256);
+ sprintf(s + strlen(s), "%u", (unsigned)getuid());
+ sprintf(s + strlen(s), "%u", (unsigned)getgid());
+ MD5Init(&md5c);
+ MD5Update(&md5c, (unsigned char *)s, strlen(s));
+ MD5Final(md5key, &md5c);
+ sprintf(s + strlen(s), "%lu", (unsigned long) ((time(NULL) + 65121) ^ 0x375F));
+ MD5Init(&md5c);
+ MD5Update(&md5c, (unsigned char *)s, strlen(s));
+ MD5Final(md5key1, &md5c);
+#define nextrand (md5key[j] ^ md5key1[j])
+ if (mode) {
+ for (j = k = 0; j < 16; j++) {
+ unsigned char rb = nextrand;
+
+#define Rad16(x) ((x) + 'A')
+ key[k++] = Rad16((rb >> 4) & 0xF);
+ key[k++] = Rad16(rb & 0xF);
+#undef Rad16
+ if (j & 1) {
+ key[k++] = '-';
+ }
+ }
+ key[--k] = 0;
+ } else {
+ for (j = 0; j < 16; j++) {
+ key[j] = nextrand;
+ }
+ }
+}
+#undef nextrand
+
+
+
+/*
+ * Edit job codes into main command line
+ * %% = %
+ * %j = Job name
+ * %t = Job type (Backup, ...)
+ * %e = Job Exit code
+ * %i = JobId
+ * %l = job level
+ * %c = Client's name
+ * %r = Recipients
+ * %d = Director's name
+ *
+ * omsg = edited output message
+ * imsg = input string containing edit codes (%x)
+ * to = recepients list
+ *
+ */
+POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, char *to)
+{
+ char *p, *str;
+ char add[20];
+
+ *omsg = 0;
+ Dmsg1(200, "edit_job_codes: %s\n", imsg);
+ for (p=imsg; *p; p++) {
+ if (*p == '%') {
+ switch (*++p) {
+ case '%':
+ str = "%";
+ break;
+ case 'c':
+ str = jcr->client_name;
+ if (!str) {
+ str = "";
+ }
+ break;
+ case 'd':
+ str = my_name; /* Director's name */
+ break;
+ case 'e':
+ str = job_status_to_str(jcr->JobStatus);
+ break;
+ case 'i':
+ bsnprintf(add, sizeof(add), "%d", jcr->JobId);
+ str = add;
+ break;
+ case 'j': /* Job name */
+ str = jcr->Job;
+ break;
+ case 'l':
+ str = job_level_to_str(jcr->JobLevel);
+ break;
+ case 'r':
+ str = to;
+ break;
+ case 't':
+ str = job_type_to_str(jcr->JobType);
+ break;
+ default:
+ add[0] = '%';
+ add[1] = *p;
+ add[2] = 0;
+ str = add;
+ break;
+ }
+ } else {
+ add[0] = *p;
+ add[1] = 0;
+ str = add;
+ }
+ Dmsg1(1200, "add_str %s\n", str);
+ pm_strcat(&omsg, str);
+ Dmsg1(1200, "omsg=%s\n", omsg);
+ }
+ return omsg;
+}
+
+void set_working_directory(char *wd)
+{
+ struct stat stat_buf;
+
+ if (wd == NULL) {
+ Emsg0(M_ERROR_TERM, 0, _("Working directory not defined. Cannot continue.\n"));
+ }
+ if (stat(wd, &stat_buf) != 0) {
+ Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" not found. Cannot continue.\n"),
+ wd);
+ }
+ if (!S_ISDIR(stat_buf.st_mode)) {
+ Emsg1(M_ERROR_TERM, 0, _("Working Directory: \"%s\" is not a directory. Cannot continue.\n"),
+ wd);
+ }
+ working_directory = wd; /* set global */
+}