论坛: UNIX系统 标题: add-postfixs-users 复制本贴地址    
作者: SysHu0teR [syshunter]    版主   登录
前段时间帮朋友试着装了一个POSTFIX邮件服务器,参照了
http://www.freebsdchina.org/forum/viewtopic.php?p=116990&sid=fbee02174f93a3caae29f5beb5af77bc
完全是照葫芦画瓢,好不容易装好了。当时为了添加用户方便,写了这个东西方便自用。今晚通宵无意翻了出来。需要的同志酌情修改。英文注释很烂,别骂我。
记得修改
#define MYSQL_USER "postfix"                    //mysql's username               
#define MYSQL_PASS "password"                    //........password
#define MYSQL_DATABASE "postfix"                //........database

代码:

//gcc create.c -o create -I/usr/local/include -L/usr/local/lib/ -lmysqlclient -lz
//v0.00000001
//usage: ./create root@syshunter.net password
//or  : ./create will list the table of userinfo to output to screen.
//auther: SysHu0teR
//Mail  : syshunter@hotmail.com
//
//date:  12.01.2005
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
#include <mysql.h>
//#define DEBUG
#define MYSQL_USER "postfix"                    //mysql's username               
#define MYSQL_PASS "password"                    //........password
#define MYSQL_DATABASE "postfix"                //........database
void usage(char *p);
int create_dir(char *pdir);
void get_name(char *name,char *addr);
void get_domain(char *domain,char *addr);
int mysql_insert(char *host,char *mysql_name,char *mysql_pass,char *database,char *name,char *domain,char *pass);
int mysql_domain(char *domain,char *pass,MYSQL *mysql);
int view_sql(char *username,char*pass,char *database);
typedef struct time{
int yea;
int mon;
int day;
int hou;
int min;
int sec;
} TIME;
TIME ptime;
uid_t pf_uid=125;
gid_t pf_gid=125;
int main(int argc,char *argv[])
{
// pf_uid=getuid();
// pf_gid=getgid();
char name[80]={0};
char domain[80]={0};
char *sql;
time_t pt;
struct tm *ptm;


if(argc!=3) {
  if(argc==1) {
  view_sql(MYSQL_USER,MYSQL_PASS,MYSQL_DATABASE);
  exit(1);
  }
  else {
  usage(argv[0]);
  exit(1);
  }
}


time(&pt);
ptm=localtime(&pt);    //get current time.
ptime.yea=1900+ptm->tm_year;
ptime.mon=1+ptm->tm_mon;
ptime.day=ptm->tm_mday;
ptime.hou=ptm->tm_hour;
ptime.min=ptm->tm_min;
ptime.sec=ptm->tm_sec;


get_name(name,argv[1]);
get_domain(domain,argv[1]);
create_dir(argv[1]);
mysql_insert("localhost",MYSQL_USER,MYSQL_PASS,MYSQL_DATABASE,name,domain,argv[2]);
return 1;
}
void usage(char *p)
{
printf("usage:  %s  [mailaddress(such as: root@syshunter.net)] [user's password]\n\n",p);

printf("                                                          ---------By SysHu0teR.\n");
}
int create_dir(char *pdir)
{
pid_t pid;
char domain[80]={0},name[20]={0};
char path[200]={0};

char maildir[255]={0};
get_domain(domain,pdir);
get_name(name,pdir);

sprintf(path,"/var/mail/%s",domain);
if(mkdir(path,0777)<0)
  perror("mkdir domain_dir");
 
if(chmod(path,0777)<0) {
  perror("chmod directory");
  exit(1);
}
if(chown(path,pf_uid,pf_gid)<0) {
  perror("chown directory");
  exit(1);
}

strcat(path,"/");
strcat(path,name);
if(mkdir(path,0777)<0)
  perror("mkdir name_dir");
if(chmod(path,0777)<0) {
  perror("chmod directory");
  exit(1);
}
if(chown(path,pf_uid,pf_gid)<0) {
  perror("chown directory");
  exit(1);
}

// sprintf(maildir,"/usr/local/bin/maildirmake ");
strncpy(maildir,path,strlen(path));
strcat(maildir,"/");
strcat(maildir,"Maildir");

#ifdef DEBUG
puts(maildir);
#endif
if(fork()==0)
  execl("/usr/local/bin/maildirmake","maildirmake",maildir, (char *)0);
wait((int *)0);

        if(chmod(maildir,0777)<0) {
                perror("chmod directory");
                exit(1);
        }
 
  if(fork()==0)
  execl("/usr/sbin/chown","chown","postfix:postfix",maildir,(char *)0);
/*        if(chown(maildir,pf_uid,pf_gid)<0) {
                perror("chown directory");
                exit(1);
        }
*/
return 1;
}
void get_domain(char *domain, char *addr)
{
char *ps;
int i=0;
int at='@';
ps=strchr(addr,at);
if(ps==NULL||*(ps+1)==0) {
  printf("I'v got a fuck wrong mail address.\n");
  exit(1);
}
ps++;
while(ps[i]!=0) {
  domain[i]=ps[i];
  ++i;
}
domain[i]=0;
#ifdef DEBUG
puts(domain);
#endif

}
void get_name(char *name,char *addr)
{
int i=0;
while(addr[i]!='@' && addr[i]!=0) {
  name[i]=addr[i];
  i++;
}
#ifdef DEBUG
puts(name);
#endif
}
int mysql_insert(char *host,char *mysql_name,char *mysql_pass,char *database,char *name,char *domain,char *pass)
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
int domainid;
char mailaddr[256]={0};
char sql[256]={0};
char qy[256]={0};
mysql_init(&mysql);

if(!mysql_real_connect(&mysql,host,mysql_name,mysql_pass,database,0,NULL,0)) {
  printf("In mysql_insert():mysql_real_connect()'s error:%s\n",mysql_error(&mysql));
  exit(1);
}
//whether the user already within the table..
sprintf(qy,"select * from userinfo where address='%s@%s';",name,domain);
if(mysql_real_query(&mysql,qy,(unsigned int)strlen(qy))) {
  printf("In mysql_insert():147: mysql_real_query()'s error: %s\n",mysql_error(&mysql));
  exit(1);
}
res=mysql_store_result(&mysql);

if(mysql_num_rows(res)>0) {
  printf("This user has already exists.\n");
  exit(1);
}
mysql_free_result(res);

domainid=mysql_domain(domain,pass,&mysql);
sprintf(sql,"INSERT INTO `userinfo` VALUES (NULL, '%s', %d, '%s@%s', NULL,encrypt('%s'), '%s', 209715200, 1, '/var/mail/%s/%s/', '/var/mail/%s/%s/Maildir/', '%d-%d-%d %d:%d:%d', '', '', 0, 0, 0, 0, '', 0, '', '', '');",name,domainid,name,domain,pass,name,domain,name,domain,name,ptime.yea,ptime.mon,ptime.day,ptime.hou,ptime.min,ptime.sec);
if(mysql_real_query(&mysql,sql,(unsigned int)strlen(sql))) {
  printf("In mysql_insert():153: mysql_real_query()'s error: %s\n",mysql_error(&mysql));
  exit(1);
}
mysql_close(&mysql);
return 1;
}
int mysql_domain(char *domain,char *pass,MYSQL *mysql)
{
char sql[256]={0};
char sql_order[]="select domain_id from domaininfo order by domain_id;";
char sql_insert[256]={0};
int maxid=0;
int domain_id;

MYSQL_ROW row,row2;
MYSQL_RES *res;


sprintf(sql,"select domain_id from domaininfo where domain='%s';",domain);

if(mysql_real_query(mysql,sql,(unsigned int)strlen(sql))) {
  printf("error: %s\n",mysql_error(mysql));
  exit(1);
}

res=mysql_store_result(mysql);
if(mysql_num_rows(res)>0) {
  row=mysql_fetch_row(res);
  mysql_free_result(res);
  return atoi(row[0]);
}

mysql_free_result(res);

// part of Insert as folwing:
if(mysql_real_query(mysql,sql_order,(unsigned int)strlen(sql_order))) {
  printf("error: %s\n",mysql_error(mysql));
  exit(2);
}
res=mysql_store_result(mysql);
if(mysql_num_rows(res)>0) {
  row2=mysql_fetch_row(res);
  while(row2) {
  maxid=atoi(row2[0]);
  row2=mysql_fetch_row(res);
  }
}

mysql_free_result(res);

maxid++;
#ifdef DEBUG
printf("maxid: %d",maxid);
#endif
sprintf(sql_insert,"INSERT INTO `domaininfo` VALUES (%d, '%s', NULL, encrypt('%s'), %d, 500, NULL, '0000-00-00', 1, '%d-%d-%d %d:%d:%d');",maxid,domain,pass,maxid,ptime.yea,ptime.mon,ptime.day,ptime.hou,ptime.min,ptime.sec);

if(mysql_real_query(mysql,sql_insert,(unsigned int)strlen(sql_insert))) {
  printf("Inserting to domaininfo error: %s\n",mysql_error(mysql));
  exit(3);
}

return maxid;
}

/* view_sql by "Loaded",Hes my little brother,lol....*/
int view_sql(char *username,char *pass,char *database)
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
int i;
char sql[]="select domain_id,address from userinfo order by domain_id;";

mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost",username,pass,database,0,NULL,0))
{
      printf( "Error connecting to database: %s\n",mysql_error(&mysql));
    return -1;
}
i = mysql_real_query(&mysql,sql,(unsigned int) strlen(sql));
    if (i)
{
  printf("Error making query: %s\n",mysql_error(&mysql));
  return -1;
}

res=mysql_store_result(&mysql);

printf("domain_id\taddress\n\n");
while(row = mysql_fetch_row(res))
{
  printf("%s\t\t%s\n",row[0],row[1]);
}

mysql_free_result(res);
mysql_close(&mysql);
return -1;
}
//end



后来才发现其实早有了管理用户的软件,叫什么名字网了


[此贴被 BT小队长(teczm) 在 05月18日10时33分 编辑过]

地主 发表时间: 06-01-06 06:01

回复: 小幻 [vy07]   论坛用户   登录
老大怎么没人里你阿..
我来和你聊天..
呵呵

B1层 发表时间: 06-01-07 16:47

回复: yange1111 [yange1111]   论坛用户   登录
[MP=0,0,true]http://www.5573wow.com/1860.wma[/MP]

dfgdgdf [FLASH=0,0]http://www.5573wow.com/1860.wma[/FLASH] 


[此贴被 NetDemon(netdemon) 在 09月07日18时30分 编辑过]

B2层 发表时间: 06-08-22 22:25

回复: SysHu0teR [syshunter]   版主   登录
回头看看自己的代码,真TM不是一般的烂!

B3层 发表时间: 07-02-25 21:02

论坛: UNIX系统

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号