int vmailmgr_autoconvert(void) { int writefd = -1; ibuf reader; struct cdb_make writer; int error = 0; int readall = 0; int writerr = 0; if ((writefd = path_mktemp(pwfile, &tmppwfile)) != -1) { if (cdb_make_start(&writer, writefd) != 0) error = CVME_IO | CVME_FATAL; else { if (ibuf_open(&reader, pwfile, 0)) { uint32 end; struct stat st; if (fstat(reader.io.fd, &st) == 0 && fchmod(writefd, st.st_mode) == 0 && fchown(writefd, st.st_uid, st.st_gid) == 0 && read_start(&reader, &end)) { while (ibuf_tell(&reader) < end) { if (!read_cdb_pair(&reader, &key, &data)) break; if (str_diff(&key, &virtuser) == 0) if (!convert_data()) { writerr = 1; break; } if (cdb_make_add(&writer, key.s, key.len, data.s, data.len) != 0) { writerr = 1; break; } } readall = ibuf_tell(&reader) == end; } ibuf_close(&reader); } if (cdb_make_finish(&writer) != 0) error |= CVME_FATAL; else if (readall && !writerr) rename(tmppwfile.s, pwfile); } close(writefd); unlink(tmppwfile.s); } return error; }
static void start_slot(int slot, const char* command, const char* envstart) { static str env; char* period; int fd; char hostname[256]; const char* mailto; const struct passwd* pw = &slots[slot].pw; const char* shell; msg5("(", pw->pw_name, ") CMD (", command, ")"); env.len = 0; wrap_str(envstr_set(&env, "PATH", path, 1)); if (envstart) wrap_str(envstr_from_string(&env, envstart, 1)); wrap_str(envstr_set(&env, "HOME", pw->pw_dir, 1)); wrap_str(envstr_set(&env, "USER", pw->pw_name, 1)); wrap_str(envstr_set(&env, "LOGNAME", pw->pw_name, 1)); if ((shell = envstr_get(&env, "SHELL")) == 0) shell = "/bin/sh"; if ((mailto = envstr_get(&env, "MAILTO")) == 0) mailto = pw->pw_name; if (*mailto == 0) { fd = devnull; slots[slot].headerlen = 0; } else { if ((fd = path_mktemp(tmpprefix, &tmp)) == -1) { failsys_slot(slot, "ZCould not create temporary file"); return; } unlink(tmp.s); cloexec_on(fd); gethostname(hostname, sizeof hostname); wrap_str(str_copyns(&tmp, 6, "To: <", mailto, ">\n", "From: Cron Daemon <root@", hostname, ">\n")); if ((period = strchr(hostname, '.')) != 0) *period = 0; wrap_str(str_catns(&tmp, 7, "Subject: Cron <", pw->pw_name, "@", hostname, "> ", command, "\n\n")); slots[slot].headerlen = tmp.len; if (write(fd, tmp.s, tmp.len) != (long)tmp.len) { close(fd); fd = -1; report_slot(slot, "ZCould not write message header"); return; } } shell_argv[shell_argc+0] = shell; shell_argv[shell_argc+1] = "-c"; shell_argv[shell_argc+2] = command; debugf(DEBUG_EXEC, "{slot }d{ starting: }s", slot, command); if (!forkexec_slot(slot, devnull, fd, shell_argv, &env)) { if (fd != devnull) close(fd); fd = -1; } slots[slot].sending_email = 0; slots[slot].tmpfd = fd; }