#include #include #include #include #include #include "mailsql.h" #include "mailsql.m" #include #include #include #include #include #include "domains.h" HELP_FILE help_mailsql ("mailsql","mailsql"); static HELP_FILE help_aliases ("mailsql","aliases"); static const char subsys_mailsql[]="mailsql"; void _F_pickdomain::add() { } void pickdomain (_F_pickdomain &c, bool mayadd) { _F_pickdomain *p; bool mayadd; glocal.p = &c; glocal.mayadd = mayadd; (MSG_U(T_SQLDOMAIN,"SQL Email domains") ,"",help_nil); newf_head (MSG_U(H_DOMAIN,"Domain name\tDomain ID\tUser Quota\tGlobal quota\tMay receive\tMay retrieve\tCreated\tExpire")); if (glocal.mayadd){ addwhat (MSG_U(I_ADDDOMAIN ,"Select Add to add a new SQL domain")); } sortable(); sortpolicy ("annaa"); sethdispo ("lrrrccll"); _F_editrecords *c; glocal.c = this; ("select domain,domainid,quota,dquota,mayreceive,mayretrieve,createdate,expiredate" " from domains order by domain"); glocal.c->new_menuitemf (row[0],"%s\t%s\t%s\t%c\t%c\t%s\t%s",row[1] ,strcmp(row[2],"0")==0 ? MSG_R(I_NONE) : row[2] ,strcmp(row[3],"0")==0 ? MSG_R(I_NONE) : row[3] ,row[4][0] == 'Y' ? 'X' : ' ' ,row[5][0] == 'Y' ? 'X' : ' ' ,row[6],row[7]); ("select domain,domainid from domains" " order by domain limit %d,1",no); glocal.p->edit (row[0],atoi(row[1])); glocal.p->add(); } void pickdomain (_F_pickdomain &c) { pickdomain (c,false); } static int vdomain_loadinfo (int domainid, C_domains &info, SSTRINGS &others) { C_domains *info; int ret; SSTRINGS *others; glocal.ret = -1; glocal.info = &info; glocal.others = &others; ("select * from domains where domainid = %d",domainid); glocal.info->set (fields,row); glocal.ret = 0; ("select alias from daliases" " where domainid=%s and alias<>'%s'" ,glocal.info->domainid.get(),glocal.info->domain.get()); glocal.others->add (new SSTRING(row[0])); return glocal.ret; } /* Delete everything about a domain */ static int vdomain_delete (int domainid) { sql_action ("delete from daliases where domainid=%d" ,domainid); sql_action ("delete from aliases where domainid=%d" ,domainid); ("select userid from accounts" " where domainid=%d",domainid); ("select folderid from folders" " where userid=%s",row[0]); ("select messageid from messages" " where folderid=%s",row[0]); sql_action ("delete from parts where messageid=%s",row[0]); sql_action ("delete from messages where folderid=%s",row[0]); sql_action ("delete from folders where userid=%s",row[0]); sql_action ("delete from accounts where domainid=%d",domainid); sql_action ("delete from domains where domainid=%d",domainid); return 0; } /* Delete everything about a domain */ int vdomain_delete (const char *domain) { int ret; const char *domain; glocal.ret = -1; glocal.domain = domain; NSQL_ENCODE enc; ("select domainid from domains where domain='%s'" ,enc.enc(domain)); tlmp_error (MSG_R(E_NODOMAIN),glocal.domain); glocal.ret = vdomain_delete (atoi(row[0])); return glocal.ret; } static int vdomain_edit (int domainid) { int ret = -1; C_domains info; SSTRINGS others; vdomain_loadinfo (domainid,info,others); DIALOG dia; dia.newf_str (MSG_U(F_VDOMAIN,"Virtual domain (fqdn)"),info.domain); dia.last_noempty(); dia.newf_str (MSG_U(F_FALLBACK,"Fallback destination (opt)"),info.fallback); // dia.newf_chk ("",matchfullname,MSG_U(F_MATCHFULLNAME,"Match full name on delivery")); char mayreceive = info.mayreceive.icmp("Y")==0; char mayretrieve = info.mayretrieve.icmp("Y")==0; int quota = info.quota.getval(); int dquota = info.dquota.getval(); int maxmsg = info.maxmsg.getval(); dia.newf_chk (MSG_U(F_MAY,"May"),mayreceive,MSG_U(I_RECEIVE,"receive new messages")); dia.newf_chk (MSG_R(F_MAY),mayretrieve,MSG_U(I_RETRIEVE,"retrieve their messages")); static int tbnum[]={0}; static const char *tbvals[]={MSG_U(I_NOLIMIT,"No limit"),NULL}; dia.newf_chkm_num (MSG_U(F_QUOTA,"Default max folder size (k)"),quota,0,tbnum,tbvals); dia.newf_chkm_num (MSG_U(F_DQUOTA,"All folders size (k)"),dquota,0,tbnum,tbvals); dia.newf_num (MSG_U(F_MAXMSG,"Max message size (k)"),maxmsg); dia.newf_str (MSG_U(F_EXPIRE,"Expiration date"),info.expiredate); dia.newf_title ("",MSG_U(T_DALIAS,"Aliases for this domain")); for (int i=0; i<3; i++) others.add (new SSTRING); for (int i=0; i SSTRING dups; for (int i=0; iis_empty()) continue; ("select alias from daliases" " where domainid<>%d and alias='%s'",domainid ,others.getitem(i)->get()); glocal.dups.appendf (" %s",row[0]); } if (glocal.dups.is_filled()){ xconf_error (MSG_U(E_VDOMEXIST ,"This virtual domain is already defined")); nof = 0; }else{ info.mayretrieve.setfrom(mayretrieve ? "Y" : "N"); info.mayreceive.setfrom(mayreceive ? "Y" : "N"); info.quota.setfrom (quota); info.dquota.setfrom (dquota); info.maxmsg.setfrom (maxmsg); info.update ("domains","domainid=%d",domainid); others.remove_empty(); others.add (new SSTRING(info.domain)); sql_action ("delete from daliases where domainid=%d",domainid); for (int i=0; iget()); } ret = 0; break; } } } return ret; } static bool privi_changed = false; /* Edit the definition of virtual email domains. Return != 0 if the has been one modification. */ void vdomain_edit() { if (perm_rootaccess (MSG_R(P_VDOMAINS))){ (true); vdomain_edit (domainid); DIALOG dia; SSTRING name; dia.newf_str (MSG_U(F_DOMAIN,"Domain name"),glocal.name); int nof = 0; while (1){ MENU_STATUS code = dia.edit (MSG_U(T_NEWDOMAIN,"New SQL domain") ,"",help_nil,nof); if (code == MENU_CANCEL || code == MENU_ESCAPE){ break; }else{ NSQL_ENCODE enc; if (sql_action ("insert into domains (domain,createdate) values ('%s',now())" ,enc.enc(glocal.name)) != -1){ // We must also insert the domain in the domain aliases table. // We retrieve the domain id first int id = sql_getlastid(); sql_action("insert into daliases (domainid,alias)" " values (%d,'%s')",id,enc.enc(glocal.name)); break; } } } } } static PRIVILEGES tb; static void vdomain_setprivi() { if (tb.getnb()==0 || privi_changed){ privi_changed = false; tb.remove_all(); ("select domain from domains"); const char *dname = row[0]; char id[PATH_MAX]; sprintf (id,"mailsql_%s",dname); tb.add (new PRIVILEGE (id,dname ,MSG_U(T_VIRTDOMPRIV,"Virtual email domains"))); } } static PRIVILEGE_DECLARATOR vdom_decl(vdomain_setprivi); /* Select a virtual domain and edit the users */ void vdomain_editusers () { if (perm_rootaccess (MSG_R(P_VDOMAINS))){ (); sqlusers_edit (domain); } } /* Edit the aliases for a domain */ static void vdomain_editaliases (int domainid) { xconf_error ("Not done"); } /* Select a vdomain and edit one of its aliases file */ void vdomain_editaliases () { if (perm_rootaccess (MSG_R(P_VDOMAINS))){ (); vdomain_editaliases (domainid); } } /* Show the special url used to access the html mode of linuxconf */ void vdomain_listspc () { ("select domain from domains order by domain"); html_printf ("
\n"); html_printf ("

%s

\n" ,MSG_U(T_VDOMLISTS,"List of virtual email domains on this server (SQL)")); html_printf ("
\n"); html_printf ("
\n"); html_printf ("\n"); html_printf ("
%s%s\n" ,MSG_U(T_VDOMAIN,"Virtual email domain") ,MSG_U(T_CHGVPASS,"Change your password")); const char *name = row[0]; html_printf ("
%s\n" ,name,name); html_printf ("\t____\n" ,name); html_printf ("
\n"); html_printf ("
\n");
} void vdomain_defclass (SSTREAM *out) { SSTREAM *out; glocal.out = out; ("select alias from daliases order by alias"); glocal.out->printf ("CW%s\n",row[0]); } void vdomain_defrules0 (SSTREAM *out) { SSTREAM *out; glocal.out = out; // We generate those lines only if there is at least one vdomain ("select domain from domains order by domain limit 1"); glocal.out->printf ("R$*<@$=W>\t$#sqlvirtual $@ $2 $: $1\n"); glocal.out->printf ("R$*<@$=W.>\t$#sqlvirtual $@ $2 $: $1\n"); } void vdomain_defmailer (SSTREAM *out) { SSTREAM *out; glocal.out = out; ("select domain from domains order by domain"); glocal.out->printf ("# Special mailer for SQL virtual email domain hosting\n"); glocal.out->printf ("Msqlvirtual, P=/usr/lib/linuxconf/lib/vdeliver_sql, F=lsDFMShP, S=10, R=20/40,\n"); glocal.out->printf ("\t\tA=vdeliver_sql $u $h\n"); } #include static PUBLISH_VARIABLES_MSG tbvar[]={ {"domain",P_MSG_R(F_VDOMAIN)}, {"dfallback",P_MSG_R(F_FALLBACK)}, {"dmayreceive",P_MSG_R(I_RECEIVE)}, {"dmayretrieve",P_MSG_R(I_RETRIEVE)}, {"duquota",P_MSG_R(F_QUOTA)}, {"dgquota",P_MSG_R(F_DQUOTA)}, {"dmaxmsgsize",P_MSG_R(F_MAXMSG)}, {"dexpiredate",P_MSG_R(F_EXPIRE)}, {NULL,NULL}, }; static void vdomain_edit (const char *key, bool setting) { mailsql_initdb(); glocal int domainid = sqlusers_getdomainid(key); if (glocal.domainid != -1){ vdomain_edit (glocal.domainid); } } static void vdomain_list (SSTRINGS &tb) { glocal SSTRINGS *tb = &tb; mailsql_initdb(); ("select domain from domains order by domain"); glocal.tb->add (new SSTRING(row[0])); } static REGISTER_VARIABLES reg1 ("mailsql","domains",tbvar,NULL ,vdomain_edit,vdomain_list);