#include #include #include #include #include "mailsql.h" #include "mailsql.m" int main (int argc, char *argv[]) { const char *domain; bool createdom; glocal.createdom = false; glocal.domain = NULL; static const char *tbdic[]={"mailsql","tlmpsql",NULL}; int ret = (argc,argv,tbdic); extern const char *version; setproginfo ("user2sql",version,MSG_U(I_USER2SQL ,"Import unix user accounts in SQL\n" "\n" "user2sql --domain domain passwd_file shadow_file\n")); setarg ('c',"createdom",MSG_U(I_CREATEDOM,"Create SQL domain as needed"),glocal.createdom,false); setarg ('d',"domain",MSG_R(I_DOMAIN),glocal.domain,true); int ret = -1; vutil_opendb(); if (argc != 2){ usage(); }else{ int domainid; glocal.domainid = -1; NSQL_ENCODE enc; ("select domainid from domains where domain='%s'" ,enc.enc(glocal.domain)); glocal.domainid = atoi(row[0]); if (glocal.createdom){ NSQL_ENCODE enc; sql_action ("insert into domains (domain) values ('%s')" ,enc.enc(glocal.domain)); glocal.domainid = sql_getlastid(); sql_action ("insert into daliases (alias,domainid) values ('%s',%d)" ,enc.enc(glocal.domain),glocal.domainid); } if (glocal.domainid == -1){ fprintf (stderr,MSG_U(E_UNKNOWNDOM,"Unknown SQL domain %s\n") ,glocal.domain); }else{ ret = 0; // Create the accounts (argv[0],true); SSTRINGS tb; str_splitline (line,':',tb); NSQL_ENCODE enc; if (sql_action ("insert into accounts (name,fullname,domainid) values ('%s','%s',%d)" ,enc.enc(tb.getitem(0)) ,enc.enc(tb.getitem(4)) ,glocal.domainid) != -1){ int userid = sql_getlastid(); // Create the inbox sql_action ("insert into folders (userid,foldername)" " values (%d,'inbox')",userid); } return 0; // Import the passwords (argv[1],true); SSTRINGS tb; str_splitline (line,':',tb); NSQL_ENCODE enc; sql_action ("update accounts set password='%s'" "where domainid=%d and name='%s'" ,enc.enc(tb.getitem(1)) ,glocal.domainid ,enc.enc(tb.getitem(0))); return 0; } } return ret; return ret; }