Skip to content

Commit

Permalink
Refactored ETRN/ATRN code
Browse files Browse the repository at this point in the history
1. add_domain_assign.c, del_control.c, deldomain.c, iadddomain.c: use same logic for ETRN, ATRN domains
2. vadddomain.c: exit early for post_handle for ETRN, ATRN domains
3. vadddomain_handle.in: fix for ETRN, ATRN domains
4. vdeldomain.c: use entry for autoturn in users/assign for ETRN, ATRN domains
5. remove_line.c: Added function remove_line_p() to remove line on partial match
  • Loading branch information
mbhangui committed Dec 3, 2023
1 parent f2b3b79 commit f191a53
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 64 deletions.
2 changes: 1 addition & 1 deletion indimail-x/add_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ add_control(char *domain, char *target)
if (remove_line(domain, filename.s, 0, INDIMAIL_QMAIL_MODE) == -1)
return (-1);
if (use_etrn) {
/*- Add to etrndomains file and remove duplicates and set mode */
/*- Add to etrnhosts file and remove duplicates and set mode */
if (relative) {
if (!stralloc_copys(&filename, sysconfdir) ||
!stralloc_append(&filename, "/") ||
Expand Down
54 changes: 28 additions & 26 deletions indimail-x/add_domain_assign.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* $Log: add_domain_assign.c,v $
* Revision 1.5 2023-12-03 15:38:52+05:30 Cprogrammer
* use same logic for ETRN, ATRN domains
*
* Revision 1.4 2023-03-22 07:59:53+05:30 Cprogrammer
* BUG - set uid, gid variables before creating assign file
*
Expand Down Expand Up @@ -36,7 +39,7 @@
#include "variables.h"

#ifndef lint
static char sccsid[] = "$Id: add_domain_assign.c,v 1.4 2023-03-22 07:59:53+05:30 Cprogrammer Exp mbhangui $";
static char sccsid[] = "$Id: add_domain_assign.c,v 1.5 2023-12-03 15:38:52+05:30 Cprogrammer Exp mbhangui $";
#endif

static void
Expand Down Expand Up @@ -80,11 +83,11 @@ add_domain_assign(char *domain, char *domain_base_dir, uid_t uid, gid_t gid)
return (-1);
}
}
if (use_etrn == 2 && !get_assign("autoturn", 0, 0, 0)) {
if (use_etrn && !get_assign("autoturn", 0, 0, 0)) {
get_indimailuidgid(&indimailuid, &indimailgid);
strnum1[i = fmt_ulong(strnum1, indimailuid)] = 0;
strnum2[j = fmt_ulong(strnum2, indimailgid)] = 0;
if (!stralloc_copyb(&tmpstr, "+autoturn-:indimail:", 20) ||
if (!stralloc_copyb(&tmpstr, "+autoturn-:autoturn:", 20) ||
!stralloc_catb(&tmpstr, strnum1, i) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_catb(&tmpstr, strnum2, j) ||
Expand All @@ -98,29 +101,28 @@ add_domain_assign(char *domain, char *domain_base_dir, uid_t uid, gid_t gid)
return (-1);
if (!OptimizeAddDomain)
update_newu();
} else {
strnum1[i = fmt_ulong(strnum1, uid)] = 0;
strnum2[j = fmt_ulong(strnum2, gid)] = 0;
if (!stralloc_copyb(&tmpstr, "+", 1) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_catb(&tmpstr, "-:", 2) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_catb(&tmpstr, strnum1, i) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_catb(&tmpstr, strnum2, j) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_cats(&tmpstr, domain_base_dir) ||
!stralloc_catb(&tmpstr, use_etrn ? "/" : "/domains/", use_etrn ? 1 : 9) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_catb(&tmpstr, ":-::", 4) ||
!stralloc_0(&tmpstr))
die_nomem();
if (update_file(filename.s, tmpstr.s, INDIMAIL_QMAIL_MODE))
return (-1);
/*- compile the assign file */
if (!OptimizeAddDomain)
update_newu();
}
strnum1[i = fmt_ulong(strnum1, uid)] = 0;
strnum2[j = fmt_ulong(strnum2, gid)] = 0;
if (!stralloc_copyb(&tmpstr, "+", 1) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_catb(&tmpstr, "-:", 2) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_catb(&tmpstr, strnum1, i) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_catb(&tmpstr, strnum2, j) ||
!stralloc_append(&tmpstr, ":") ||
!stralloc_cats(&tmpstr, domain_base_dir) ||
!stralloc_catb(&tmpstr, use_etrn ? "/" : "/domains/", use_etrn ? 1 : 9) ||
!stralloc_cats(&tmpstr, domain) ||
!stralloc_catb(&tmpstr, ":-::", 4) ||
!stralloc_0(&tmpstr))
die_nomem();
if (update_file(filename.s, tmpstr.s, INDIMAIL_QMAIL_MODE))
return (-1);
/*- compile the assign file */
if (!OptimizeAddDomain)
update_newu();
return(0);
}
9 changes: 6 additions & 3 deletions indimail-x/del_control.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* $Log: del_control.c,v $
* Revision 1.4 2023-12-03 16:10:48+05:30 Cprogrammer
* use same logic for ETRN, ATRN domains
*
* Revision 1.3 2023-03-25 14:15:26+05:30 Cprogrammer
* refactored code
*
Expand Down Expand Up @@ -32,7 +35,7 @@
#include "compile_morercpthosts.h"

#ifndef lint
static char sccsid[] = "$Id: del_control.c,v 1.3 2023-03-25 14:15:26+05:30 Cprogrammer Exp mbhangui $";
static char sccsid[] = "$Id: del_control.c,v 1.4 2023-12-03 16:10:48+05:30 Cprogrammer Exp mbhangui $";
#endif

static void
Expand Down Expand Up @@ -111,7 +114,7 @@ del_control(char *domain)

if (!stralloc_catb(&filename, "virtualdomains\0", 15) )
die_nomem();
if (use_etrn == 2) {
if (use_etrn) {
if (!stralloc_copys(&tmp, domain) ||
!stralloc_catb(&tmp, ":autoturn-", 10) ||
!stralloc_0(&tmp))
Expand All @@ -127,7 +130,7 @@ del_control(char *domain)
if (errno != error_noent)
status = -1;
} else
if (remove_line(tmp.s, filename.s, 0, INDIMAIL_QMAIL_MODE) == -1)
if (remove_line_p(tmp.s, filename.s, 0, INDIMAIL_QMAIL_MODE) == -1)
status = -1;
return (status);
}
11 changes: 7 additions & 4 deletions indimail-x/deldomain.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* $Log: deldomain.c,v $
* Revision 1.6 2023-12-03 15:40:29+05:30 Cprogrammer
* use same logic for ETRN, ATRN domains
*
* Revision 1.5 2023-03-25 14:32:08+05:30 Cprogrammer
* multiple bug fixes
*
Expand Down Expand Up @@ -58,7 +61,7 @@
#include "common.h"

#ifndef lint
static char sccsid[] = "$Id: deldomain.c,v 1.5 2023-03-25 14:32:08+05:30 Cprogrammer Exp mbhangui $";
static char sccsid[] = "$Id: deldomain.c,v 1.6 2023-12-03 15:40:29+05:30 Cprogrammer Exp mbhangui $";
#endif

static void
Expand Down Expand Up @@ -130,13 +133,13 @@ deldomain(char *domain)
return (-1);
}
lowerit(domain);
if (use_etrn == 2) {
if (use_etrn) {
if (!(ptr = autoturn_dir(domain))) {
strerr_warn3("deldomain: domain ", domain, " does not exist", 0);
strerr_warn3("deldomain: autoturn domain ", domain, " does not exist", 0);
return (-1);
} else
if (!(tmpstr = get_assign("autoturn", 0, &uid, &gid))) {
strerr_warn3("deldomain: domain ", domain, " does not exist: No entry for autoturn in assign", 0);
strerr_warn3("deldomain: autoturn domain ", domain, " does not exist: No entry for autoturn in assign", 0);
return (-1);
}
if (!stralloc_copys(&tmpbuf, tmpstr) ||
Expand Down
9 changes: 9 additions & 0 deletions indimail-x/doc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ Release @version@-@release@ Start XX/XX/XXXX End --/--/----
10. iadddomain.c: fix .qmail-default for etrn, atrn domains
11. vadddomain.c: do post handle when creating etrn, atrn domains
12. vadddomain_handle.in: handle etrn/atrn domain creation
- 03/12/2023
13. add_domain_assign.c, del_control.c, deldomain.c, iadddomain.c: use same
logic for ETRN, ATRN domains
14. vadddomain.c: exit early for post_handle for ETRN, ATRN domains
15. vadddomain_handle.in: fix for ETRN, ATRN domains
16. vdeldomain.c: use entry for autoturn in users/assign for ETRN, ATRN
domains
17. remove_line.c: Added function remove_line_p() to remove line on partial
match

* Fri Sep 08 2023 12:47:55 +0000 Manvendra Bhangui <indimail-virtualdomains@indimail.org> 3.4.5-1.1%{?dist}
Release 3.4.5-1.1 Start 25/04/2023 End 08/09/2023
Expand Down
27 changes: 18 additions & 9 deletions indimail-x/iadddomain.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* $Id: iadddomain.c,v 1.3 2023-11-26 19:52:49+05:30 Cprogrammer Exp mbhangui $
* $Id: iadddomain.c,v 1.4 2023-12-03 15:41:05+05:30 Cprogrammer Exp mbhangui $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -45,7 +45,7 @@
#include "vdelfiles.h"

#ifndef lint
static char sccsid[] = "$Id: iadddomain.c,v 1.3 2023-11-26 19:52:49+05:30 Cprogrammer Exp mbhangui $";
static char sccsid[] = "$Id: iadddomain.c,v 1.4 2023-12-03 15:41:05+05:30 Cprogrammer Exp mbhangui $";
#endif

static void
Expand Down Expand Up @@ -152,10 +152,16 @@ iadddomain(char *domain, char *ipaddr, char *dir, uid_t uid, gid_t gid, int chk_
if (use_etrn == 1) {
if (!stralloc_copys(&tmpbuf, dir) ||
!stralloc_append(&tmpbuf, "/") ||
!stralloc_catb(&tmpbuf, "/.qmail-", 8) ||
!stralloc_cats(&tmpbuf, domain) ||
!stralloc_catb(&tmpbuf, "/.qmail-default", 15) ||
!stralloc_catb(&tmpbuf, "-default", 8) ||
!stralloc_0(&tmpbuf))
die_nomem();
i = str_len(dir);
for (ptr = tmpbuf.s + i + 8; *ptr; ptr++) {
if (*ptr == '.')
*ptr = ':';
}
} else
if (use_etrn == 2) {
if (!stralloc_copys(&tmpbuf, dir) ||
Expand All @@ -168,8 +174,7 @@ iadddomain(char *domain, char *ipaddr, char *dir, uid_t uid, gid_t gid, int chk_
if (*ptr == '.')
*ptr = ':';
}
} else
if (!use_etrn) {
} else {
if (!stralloc_copys(&tmpbuf, dir) ||
!stralloc_catb(&tmpbuf, "/domains/", 9) ||
!stralloc_cats(&tmpbuf, domain) ||
Expand Down Expand Up @@ -238,15 +243,16 @@ iadddomain(char *domain, char *ipaddr, char *dir, uid_t uid, gid_t gid, int chk_
strerr_warn7("adddomain: chown(", tmpbuf.s, ", ", strnum1, ", ", strnum2, "): ", &strerr_sys);
return (-1);
}
if (add_domain_assign(domain, dir, uid, gid))
if (!use_etrn && add_domain_assign(domain, dir, uid, gid))
return (-1);
if (!use_etrn || use_etrn == 1) {
if (!use_etrn) {
if (add_control(domain, domain))
return (-1);
} else
if (use_etrn == 2) {
if (use_etrn) {
if (!stralloc_copyb(&tmpbuf, "autoturn-", 9) ||
!stralloc_cats(&tmpbuf, ipaddr) || !stralloc_0(&tmpbuf))
!stralloc_cats(&tmpbuf, use_etrn == 1 ? domain : ipaddr) ||
!stralloc_0(&tmpbuf))
die_nomem();
if (add_control(domain, tmpbuf.s))
return (-1);
Expand Down Expand Up @@ -287,6 +293,9 @@ iadddomain(char *domain, char *ipaddr, char *dir, uid_t uid, gid_t gid, int chk_

/*
* $Log: iadddomain.c,v $
* Revision 1.4 2023-12-03 15:41:05+05:30 Cprogrammer
* use same logic for ETRN, ATRN domains
*
* Revision 1.3 2023-11-26 19:52:49+05:30 Cprogrammer
* fix .qmail-default for etrn, atrn domains
*
Expand Down
26 changes: 21 additions & 5 deletions indimail-x/remove_line.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* $Log: remove_line.c,v $
* Revision 1.2 2023-12-03 16:09:24+05:30 Cprogrammer
* new function remove_line_p() for partial match
*
* Revision 1.1 2019-04-18 08:36:17+05:30 Cprogrammer
* Initial revision
*
Expand Down Expand Up @@ -35,7 +38,7 @@
#include "dblock.h"

#ifndef lint
static char sccsid[] = "$Id: remove_line.c,v 1.1 2019-04-18 08:36:17+05:30 Cprogrammer Exp mbhangui $";
static char sccsid[] = "$Id: remove_line.c,v 1.2 2023-12-03 16:09:24+05:30 Cprogrammer Exp mbhangui $";
#endif

static void
Expand All @@ -44,7 +47,6 @@ die_nomem()
strerr_warn1("remove_line: out of memory", 0);
_exit(111);
}

/*
* Generic remove a line from a file utility
* input: template to search for
Expand All @@ -55,13 +57,13 @@ die_nomem()
* 1 if match found
*/
int
remove_line(char *template, char *filename, int once_only, mode_t mode)
remove_line_int(char *template, char *filename, int once_only, mode_t mode, int partial)
{
struct substdio ssin, ssout;
static stralloc fname = {0}, line = {0};
struct stat statbuf;
char inbuf[4096], outbuf[4096];
int fd1, fd2, match, found;
int fd1, fd2, match, found, tlen;
#ifdef FILE_LOCKING
int lockfd;
#endif
Expand Down Expand Up @@ -107,6 +109,8 @@ remove_line(char *template, char *filename, int once_only, mode_t mode)
substdio_fdbuf(&ssin, read, fd1, inbuf, sizeof(inbuf));
substdio_fdbuf(&ssout, write, fd2, outbuf, sizeof(outbuf));
/*- pound away on the files run the search algorithm */
if (partial)
tlen = str_len(template);
for (found = 0;;) {
if (getln(&ssin, &line, &match, '\n') == -1) {
strerr_warn3("remove_line: read: ", filename, ": ", &strerr_sys);
Expand All @@ -129,7 +133,7 @@ remove_line(char *template, char *filename, int once_only, mode_t mode)
}
continue;
}
if (str_diffn(line.s, template, line.len - 1)) {
if (str_diffn(line.s, template, partial ? tlen : line.len - 1)) {
if (substdio_put(&ssout, line.s, line.len)) {
strerr_warn3("remove_line: write error: ", fname.s, ": ", &strerr_sys);
close(fd2);
Expand Down Expand Up @@ -163,3 +167,15 @@ remove_line(char *template, char *filename, int once_only, mode_t mode)
*/
return (found);
}

int
remove_line(char *template, char *filename, int once_only, mode_t mode)
{
return (remove_line_int(template, filename, once_only, mode, 0));
}

int
remove_line_p(char *template, char *filename, int once_only, mode_t mode)
{
return (remove_line_int(template, filename, once_only, mode, 1));
}
4 changes: 4 additions & 0 deletions indimail-x/remove_line.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*
* $Log: remove_line.h,v $
* Revision 1.2 2023-12-03 16:09:37+05:30 Cprogrammer
* added new function remove_line_p() for partial match
*
* Revision 1.1 2019-04-13 23:39:27+05:30 Cprogrammer
* remove_line.h
*
Expand All @@ -14,5 +17,6 @@
#endif

int remove_line(char *, char *, int, mode_t mode);
int remove_line_p(char *, char *, int, mode_t mode);

#endif
5 changes: 3 additions & 2 deletions indimail-x/vadddomain.9
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ sets the domain to use vfilter mechanism for mail filtering

.TP
\fB\-t\fR
Enable ETRN/ATRN support for the domain
Enable ETRN/ATRN support for the domain. This requires you to authenticate
and use \fBvatrn\fR(1) to configure access.

.TP
\fB\-T\fR \fIip_address\fR
Enable AUTOTURN Support from the IP ip_address
Enable ETRN, AUTOTURN Support from the IP \fIip_address\fR

.TP
\fB\-e\fR
Expand Down
Loading

0 comments on commit f191a53

Please sign in to comment.