Пример #1
0
void	subshell(const char *cmd)
{
Buffer	b;

	b="SHELL";

const char *shell=GetVarStr(b);

const char *p, *q;

	for (p=q=shell; *p; p++)
		if (*p == SLASH_CHAR)	q=p+1;

char	**env=ExportEnv();

int	n;

	for (n=0; n<NSIG; n++)
		signal(n, SIG_DFL);

	setgroupid(getgid());	// Just in case.
	setuid(getuid());
	ExitTrap::onfork();
	execle(shell, q, "-c", cmd, (const char *)0, env);
	if (write (2, "Unable to execute ", 18) < 0 ||
	    write (2, shell, strlen(shell)) < 0 ||
	    write (2, "\n", 1) < 0)
		; /* ignored */
	_exit(100);
}
Пример #2
0
int delivery(const char *mailbox)
{
FormatMbox	format_mbox;

	if (format_mbox.HasMsg())	return (0);

DeliverDotLock	dotlock;
Buffer	b;

	if ( *mailbox == '!' || *mailbox == '|' )
	{
	Buffer	cmdbuf;

		if (*mailbox == '!')
		{
			b="SENDMAIL";

		const char *sendmail=GetVarStr(b);

			cmdbuf=sendmail;

			cmdbuf += " -f '' ";
			cmdbuf += mailbox+1;
		}
		else
			cmdbuf= mailbox+1;

		cmdbuf += '\0';

		if (VerboseLevel() > 0)
			merr << "maildrop: Delivering to |" <<
				(const char *)cmdbuf << "\n";

	PipeFds	pipe;

		if (pipe.Pipe())
			throw "Cannot create pipe.";

	pid_t	pid=fork();

		if (pid < 0)
			throw "Cannot fork.";

		if (pid == 0)
		{
			pipe.close1();
			dup2(pipe.fds[0], 0);
			pipe.close0();

			try
			{
				subshell(cmdbuf);
			}
			catch (const char *p)
			{
				if (write(2, p, strlen(p)) < 0 ||
				    write(2, "\n", 1) < 0)
					; /* ignored */
				_exit(100);
			}
#if NEED_NONCONST_EXCEPTIONS
			catch (char *p)
			{
				if (write(2, p, strlen(p)) < 0 ||
				    write(2, "\n", 1) < 0)
					; /* ignored */
				_exit(100);
			}
#endif
			catch (...)
			{
				_exit(100);
			}
		}

	Mio	pipemio;

		pipe.close0();
		pipemio.fd(pipe.fds[1]);
		pipe.fds[1]= -1;
		format_mbox.Init(0);

	int	rc=format_mbox.DeliverTo(pipemio);
	int	wait_stat;

		while (wait(&wait_stat) != pid)
			;

		if (wait_stat == 0)
			rc=0;

		log(mailbox, rc || wait_stat, format_mbox);

		{
		Buffer	name, val;

			if (rc)	wait_stat= -1;
			else wait_stat= WIFEXITED(wait_stat)
				? WEXITSTATUS(wait_stat):-1;

			val.append( (unsigned long)wait_stat);
			name="EXITCODE";
			SetVar(name, val);
		}

		if (rc)	return (-1);
	}
	else if (Maildir::IsMaildir(mailbox))
	{
	Maildir	deliver_maildir;
	Mio	deliver_file;

		if ( deliver_maildir.MaildirOpen(mailbox, deliver_file,
			maildrop.msgptr->MessageSize()) < 0)
		{
#if HAVE_COURIER
			throw 75;
#else
			throw 77;
#endif
		}

		format_mbox.Init(0);
		if (format_mbox.DeliverTo(deliver_file))
		{
			log(mailbox, -1, format_mbox);
			return (-1);
		}
		deliver_maildir.MaildirSave();
		log(mailbox, 0, format_mbox);
	}
	else		// Delivering to a mailbox (hopefully)
	{
		if (VerboseLevel() > 0)
			merr << "maildrop: Delivering to " << mailbox << "\n";

#if	USE_DOTLOCK
		dotlock.LockMailbox(mailbox);
#endif

		struct	stat	stat_buf;
		Mio	mio;
		Buffer name_buf;

		name_buf="UMASK";
		const char *um=GetVarStr(name_buf);
		unsigned int umask_val=077;

		sscanf(um, "%o", &umask_val);

		umask_val=umask(umask_val);

		if (mio.Open(mailbox, O_CREAT | O_WRONLY, 0666) < 0)
		{
			umask_val=umask(umask_val);
			throw "Unable to open mailbox.";
		}
		umask_val=umask(umask_val);

		if (fstat(mio.fd(), &stat_buf) < 0)
			throw "Unable to open mailbox.";

#if	USE_FLOCK

		if (VerboseLevel() > 4)
			merr << "maildrop: Flock()ing " << mailbox << ".\n";

		FileLock::do_filelock(mio.fd());
#endif
		if (S_ISREG(stat_buf.st_mode))
		{
			if (mio.seek(0L, SEEK_END) < 0)
				throw "Seek error on mailbox.";
			dotlock.trap_truncate(mio.fd(), stat_buf.st_size);
		}

		if (VerboseLevel() > 4)
			merr << "maildrop: Appending to " << mailbox << ".\n";

		try
		{
			format_mbox.Init(1);

			if ((stat_buf.st_size > 0 &&
			     mio.write(
#if	CRLF_TERM
				       "\r\n", 2
#else
				       "\n", 1
#endif
				       ) < 0) ||
			    format_mbox.DeliverTo(mio))
			{
				dotlock.truncate();
				log(mailbox, -1, format_mbox);
				return (-1);
			}
		}
		catch (...)
		{
			dotlock.truncate();
			log(mailbox, -1, format_mbox);
			throw;
		}
		log(mailbox, 0, format_mbox);
	}

	if (VerboseLevel() > 4)
		merr << "maildrop: Delivery complete.\n";

	return (0);
}
Пример #3
0
BOOL CEdRptDoc::GetDataFromDB()
{
	CDatabase db;
	BOOL bRet = FALSE;
	try 
	{
		if (m_szODBCLink.IsEmpty())
		{
			ShowMessage(m_hWnd, "没有给定数据库指针,也没有指定链接字!");
			return FALSE;
		}

		if (!db.OpenEx(m_szODBCLink, CDatabase::openReadOnly | CDatabase::noOdbcDialog))
		{
			ShowMessage(m_hWnd, "无法打开数据库连接 %s", m_szODBCLink);
			return FALSE;
		}

		m_nRecCount = GetRecordCount(db);
		CRecordset rc(&db);
		
		CString szSQL = m_szSQL;
		CString csHeadRight = m_szHeadRight;
		if (m_nRecCount > (int)m_nMaxRecordNum)
		{
			CString str;
			str.Format(_T("共有 %d 条记录,但模版设计最大记录数为 %d,\n是否只 %d 条记录?\n"
				"注意:如何选否,会给数据库很大压力,甚至取消操作!"), 
				m_nRecCount, m_nMaxRecordNum, m_nMaxRecordNum);
			
			UINT ulSelect = IDNO;
			if (m_hWnd)
				ulSelect = MessageBox(m_hWnd, str, _T("BSRpter"), MB_YESNOCANCEL);

			if (ulSelect == IDNO)
				;
			else if (ulSelect == IDCANCEL)
				return FALSE;
			else // IDYES
			{
				csHeadRight.Format(_T("  %d条记录未打印"), m_nRecCount - m_nMaxRecordNum);
				m_nRecCount = m_nMaxRecordNum;
				
				CString csDBDrv = getDBDriver(m_szODBCLink);
				csDBDrv.MakeLower();
				if (csDBDrv.Find("oracle") >= 0)
				{
					CString szTmp = szSQL;
					szTmp.MakeLower();
					int nFind = szTmp.Find(" where ");
					if (nFind > 0)
					{
						szTmp.Format("ROWNUM <= %d AND ", m_nMaxRecordNum);
						szSQL.Insert(nFind + (int)_tcslen(" where "), (LPCSTR)szTmp);
					}
					else
					{
						szTmp.Format(" WHERE ROWNUM <= %d ", m_nMaxRecordNum);
						szSQL += szTmp;
					}
				}
				else if (csDBDrv.Find("mysql") >= 0)
				{
					CString szTmp;
					szTmp.Format(" Limit 0, %d ", m_nMaxRecordNum);
					szSQL += szTmp;
				}
				else
				{
					CString str;
					str.Format(_T("[ %s ] 数据库无法使用修改SQL的方法限制记录个数,"
						"而打开大数据集可能会出现异常\n,是否先修改查询条件,再进行打印?")
						, csDBDrv);
					if (m_hWnd != NULL && MessageBox(m_hWnd, str,_T("EdFc"), MB_YESNO) == IDYES)
						return FALSE;
				}
			}
		}

		rc.Open(CRecordset::forwardOnly, szSQL);
		m_nFieldCount = rc.GetODBCFieldCount();
		
		while (m_ColFmt.GetSize() > m_nFieldCount && m_nFieldCount > 1)
			m_ColFmt.RemoveAt(m_ColFmt.GetSize() - 1);
		
		COLFMT cf;
		while (m_ColFmt.GetSize() < m_nFieldCount)
			m_ColFmt.Add(cf);
				
		m_Grid.DeleteAllItems();
		int nRowCount = m_nRecCount + (m_bFootSum ? 2 : 1);
		m_Grid.SetRowCount(nRowCount);
		m_Grid.SetColumnCount(m_nFieldCount);
		
		// set ColumName;
		GV_ITEM Item;
		Item.mask = GVIF_TEXT|GVIF_FORMAT;	
		Item.row = 0;
		Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX;
		for (Item.col = 0; Item.col < m_nFieldCount; Item.col++)
		{	
			m_Grid.SetColumnWidth(Item.col, m_ColFmt[Item.col].width);
			Item.szText = m_ColFmt[Item.col].name;			

			if (m_ColFmt[Item.col].name.IsEmpty())
			{
				CODBCFieldInfo fi;
				rc.GetODBCFieldInfo(Item.col, fi);
				Item.szText = fi.m_strName;
			}

			m_Grid.SetItem(&Item);
		}

		if (m_hWnd != NULL && m_nRecCount > 0)
			::SendMessage(m_hWnd, WM_CREATEPROGRESS, m_nRecCount, 0);

		CDBVariant varValue;
		Item.mask = GVIF_TEXT|GVIF_FORMAT;
		Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX;
		for(int row = 0; row < m_nRecCount; row ++)
		{
			for (int col = 0; col < m_nFieldCount; col ++)
			{
				rc.GetFieldValue(col, varValue);
				Item.row = row + 1;
				Item.col = col;
				Item.szText = GetVarStr(&varValue, m_ColFmt[col].format);
				m_Grid.SetItem(&Item);	
				varValue.Clear();
			}
			
			if (m_hWnd != NULL)
				::SendMessage(m_hWnd, WM_STEPPROGRESS, m_nRecCount, row);
			
			rc.MoveNext();
		}
		
		if (m_bFootSum)
			GetStaticData(rc);
		
		rc.Close();	
		

		if (m_hWnd != NULL)
			::SendMessage(m_hWnd, WM_DESTROYPROGRESS, 0, 0);

		bRet = TRUE;
	}
	catch(CException *edb)
	{
		static TCHAR lpszError[MAX_PATH];
		CString csError;

		if (edb->GetErrorMessage(lpszError, MAX_PATH))
			csError = lpszError;

		if (csError.GetLength() <= 0)
			csError = _T("无法获得数据!");
		
		ShowMessage(m_hWnd, csError);

		edb->Delete();
	}
	catch(...)
	{
		ShowMessage(m_hWnd, _T("无法获得数据!"));
	}

	return bRet;
}
Пример #4
0
int	Maildir::MaildirOpen(const char *dir, Mio &file, off_t s)
{
	Buffer	buf;
	struct maildirsize quotainfo;

	const char *quotap;

	Buffer quotabuf;

	quotabuf="MAILDIRQUOTA";	/* Reuse a convenient buffer */
	quotabuf= *GetVar(quotabuf);
	quotabuf += '\0';

	quotap=quotabuf;

	if (!*quotap)
		quotap=NULL;

	MaildirAbort();

AlarmTimer	abort_timer;
static long	counter=0;

	buf.set(counter++);
	buf += '\0';

	struct maildir_tmpcreate_info createInfo;

	maildir_tmpcreate_init(&createInfo);

	createInfo.maildir=dir;
	createInfo.uniq=(const char *)buf;
	createInfo.msgsize=s;
	createInfo.openmode=0666;

	abort_timer.Set( 24 * 60 * 60 );
	while (!abort_timer.Expired())
	{
		Buffer name_buf;

		name_buf="UMASK";
		const char *um=GetVarStr(name_buf);
		unsigned int umask_val=077;

		sscanf(um, "%o", &umask_val);

		umask_val=umask(umask_val);

		int f=maildir_tmpcreate_fd(&createInfo);
		umask(umask_val);

		if (f >= 0)
		{
			Buffer b;

			b="FLAGS";

			const char *flags=GetVarStr(b);

			tmpname=createInfo.tmpname;
			tmpname += '\0';

			if (flags)
			{
				const char *p=flags;

				while (*p)
				{
					if (strchr("DRSF", *p) == NULL)
					{
						f=0;
						break;
					}
					++p;
				}
			}

			if (flags && *flags)
			{
				newname=createInfo.curname;
				newname += ':';
				newname += '2';
				newname += ',';
				newname += flags;
			}
			else
			{
				newname=createInfo.newname;
			}

			newname += '\0';
			maildir_tmpcreate_free(&createInfo);

			file.fd(f);
			is_open=1;
			maildirRoot=dir;
			maildirRoot += '\0';

			if (maildir_quota_add_start(dir, &quotainfo, s,
						    1, quotap))
			{
				file.fd(-1);
				unlink( (const char *)tmpname );
				is_open=0;
				maildir_deliver_quota_warning(dir,
							      quota_warn_percent,
							      quota_warn_message);
				merr << "maildrop: maildir over quota.\n";
				return (-1);
			}

			maildir_quota_add_end(&quotainfo, s, 1);
			return (0);
		}

		if (errno != EAGAIN)
		{
			merr << "maildrop: " << dir << ": " << strerror(errno)
			     << "\n";
			return -1;
		}

		AlarmSleep	try_again(2);
	}

	merr << "maildrop: time out on maildir directory.\n";
	return (-1);
}