?? vpgsql.c
字號:
memset(IUser, 0, sizeof(IUser)); memset(IPass, 0, sizeof(IPass)); memset(IGecos, 0, sizeof(IGecos)); memset(IDir, 0, sizeof(IDir)); memset(IShell, 0, sizeof(IShell)); memset(IClearPass, 0, sizeof(IClearPass)); vpw.pw_name = IUser; vpw.pw_passwd = IPass; vpw.pw_gecos = IGecos; vpw.pw_dir = IDir; vpw.pw_shell = IShell; vpw.pw_clear_passwd = IClearPass; strncpy(vpw.pw_name, PQgetvalue( pgres, ctuple, 0 ),SMALL_BUFF ); strncpy(vpw.pw_passwd, PQgetvalue( pgres, ctuple, 1 ),SMALL_BUFF ); vpw.pw_uid = atoi(PQgetvalue( pgres, ctuple, 2 )); vpw.pw_gid = atoi(PQgetvalue( pgres, ctuple, 3 )); strncpy(vpw.pw_gecos, PQgetvalue( pgres, ctuple, 4 ),SMALL_BUFF); strncpy(vpw.pw_dir, PQgetvalue( pgres, ctuple, 5 ),SMALL_BUFF); strncpy(vpw.pw_shell, PQgetvalue( pgres, ctuple, 6 ),SMALL_BUFF);#ifdef CLEAR_PASS if (PQgetvalue( pgres, ctuple, 7)!= 0 ) { strncpy(vpw.pw_clear_passwd, PQgetvalue( pgres, ctuple, 7 ),SMALL_BUFF); }#endif ctuple++; return(&vpw);}void vauth_end_getall(){ /* not applicable in pgsql? */}char *vauth_munch_domain( char *domain ){ int i; static char tmpbuf[50]; if ( domain == NULL || domain[0] == 0 ) return(domain); for(i=0;domain[i]!=0;++i){ tmpbuf[i] = domain[i]; if ( domain[i] == '.' || domain[i] == '-' ) { tmpbuf[i] = SQL_DOT_CHAR; } } tmpbuf[i] = 0; return(tmpbuf);}int vauth_setpw( struct vqpasswd *inpw, char *domain ){ PGresult *pgres; char *tmpstr; uid_t myuid; uid_t uid; gid_t gid; int err; err = vcheck_vqpw(inpw, domain); if ( err != 0 ) return(err); vget_assign(domain,NULL,0,&uid,&gid); myuid = geteuid(); if ( myuid != 0 && myuid != uid ) { return(VA_BAD_UID); } if ( (err=vauth_open()) != 0 ) return(err); vset_default_domain( domain );#ifndef MANY_DOMAINS tmpstr = vauth_munch_domain( domain );#else tmpstr = PGSQL_DEFAULT_TABLE; #endif qnprintf( SqlBufUpdate,SQL_BUF_SIZE,SETPW, tmpstr, inpw->pw_passwd, inpw->pw_uid, inpw->pw_gid, inpw->pw_gecos, inpw->pw_dir, inpw->pw_shell, #ifdef CLEAR_PASS inpw->pw_clear_passwd,#endif inpw->pw_name#ifdef MANY_DOMAINS ,domain#endif ); pgres=PQexec(pgc, SqlBufUpdate); if ( !pgres || PQresultStatus(pgres)!= PGRES_COMMAND_OK ) { fprintf(stderr, "vauth_setpw: pgsql query[6]: %s\n", PQerrorMessage(pgc)); if( pgres ) PQclear(pgres); return(-1); } if( pgres ) PQclear(pgres);#ifdef SQWEBMAIL_PASS vsqwebmail_pass( inpw->pw_dir, inpw->pw_passwd, uid, gid);#endif return(0);}#ifdef POP_AUTH_OPEN_RELAYint vopen_smtp_relay(){ PGresult *pgres; char *ipaddr; time_t mytime; int err; mytime = time(NULL); ipaddr = get_remote_ip(); if ( ipaddr == NULL ) { return 0; } if ( (err=vauth_open()) != 0 ) return 0; qnprintf(SqlBufUpdate, SQL_BUF_SIZE, "UPDATE relay SET ip_addr='%s', timestamp=%d WHERE ip_addr='%s'", ipaddr, (int)mytime, ipaddr); pgres=PQexec(pgc, SqlBufUpdate); if (PQresultStatus(pgres) == PGRES_COMMAND_OK && atoi(PQcmdTuples(pgres)) == 0) { if( pgres ) PQclear(pgres); qnprintf( SqlBufUpdate, SQL_BUF_SIZE, "INSERT INTO relay (ip_addr, timestamp) VALUES ('%s', %lu)", ipaddr, time(NULL)); pgres=PQexec(pgc, SqlBufUpdate); }/* UPDATE returned 0 rows and/or INSERT failed. Try creating the table */ if(!pgres || PQresultStatus(pgres) != PGRES_COMMAND_OK) { if( pgres ) PQclear(pgres); vcreate_relay_table();/* and try INSERTing now... */ qnprintf( SqlBufUpdate, SQL_BUF_SIZE, "INSERT INTO relay (ip_addr, timestamp) VALUES ('%s', %lu)", ipaddr, time(NULL)); pgres=PQexec(pgc, SqlBufUpdate); } if(pgres && PQresultStatus(pgres) == PGRES_COMMAND_OK ) { /* need to return non-zero value if value inserted */ if( pgres ) PQclear(pgres); return 1; } if( pgres ) PQclear(pgres); return 0;}void vupdate_rules(int fdm){ PGresult *pgres; const char re[]=":allow,RELAYCLIENT=\"\",RBLSMTPD=\"\"\n"; register unsigned i=0, n, len=strlen(re)+1; char *buf=NULL; if (vauth_open() != 0) return; snprintf(SqlBufRead, SQL_BUF_SIZE, "SELECT ip_addr FROM relay"); if ( !(pgres=PQexec(pgc, SqlBufRead)) || PQresultStatus(pgres)!=PGRES_TUPLES_OK) { vcreate_relay_table(); if(pgres) PQclear(pgres); if ( !(pgres=PQexec(pgc, SqlBufRead)) || PQresultStatus(pgres)!=PGRES_TUPLES_OK ) { fprintf(stderr, "vupdate_rules: query : %s\n", PQerrorMessage(pgc)); if (pgres) PQclear (pgres); return; } } n=PQntuples(pgres); for( ; i < n ; i++ ) { buf=realloc(buf, len+PQgetlength(pgres, i, 0) ); if( buf==NULL || errno==ENOMEM ) { PQclear(pgres); free(buf); fprintf(stderr, "vupdate_rules: no mem\n"); return; } sprintf( buf, "%s%s", PQgetvalue(pgres, i, 0), re ); if( write( fdm, buf, strlen(buf) ) != strlen(buf) ) { fprintf(stderr, "vupdate_rules: short write: %s", strerror(errno)); break; } } if(pgres) PQclear(pgres); free(buf); return;}void vclear_open_smtp(time_t clear_minutes, time_t mytime){ PGresult *pgres; time_t delete_time; int err; if ( (err=vauth_open()) != 0 ) return; delete_time = mytime - clear_minutes; snprintf( SqlBufUpdate, SQL_BUF_SIZE, "DELETE FROM relay WHERE timestamp <= %d", (int)delete_time); pgres=PQexec(pgc, SqlBufUpdate); if( !pgres || PQresultStatus(pgres) != PGRES_COMMAND_OK) { vcreate_relay_table(); } return;}void vcreate_relay_table(){ vauth_create_table ("relay", RELAY_TABLE_LAYOUT, 1); return;}#endifint vmkpasswd( char *domain ){ return(0);}void vclose(){ /* disconnection from the database */ if ( is_open == 1 ) { is_open = 0; PQfinish(pgc); }}#ifdef IP_ALIAS_DOMAINSvoid vcreate_ip_map_table(){ vauth_create_table ("ip_alias_map", IP_ALIAS_TABLE_LAYOUT, 1); return;}int vget_ip_map( char *ip, char *domain, int domain_size){ PGresult *pgres; char *ptr; unsigned ntuples; int ret = -1; if ( ip == NULL || strlen(ip) <= 0 ) return(-1); if ( domain == NULL ) return(-2); if ( vauth_open() != 0 ) return(-3); qnprintf(SqlBufRead, SQL_BUF_SIZE, "select domain from ip_alias_map where ip_addr = '%s'", ip); pgres=PQexec(pgc, SqlBufRead); if( !pgres || PQresultStatus(pgres) != PGRES_TUPLES_OK ) { fprintf( stderr, "vget_ip_map: pgsql query: %s\n", PQerrorMessage(pgc)); if( pgres ) PQclear(pgres); return -1; } ntuples = PQntuples(pgres); if(!ntuples) *domain='\0'; else { ret = 0; ptr = PQgetvalue(pgres, ntuples-1, 0); strncpy(domain, ptr, strlen(ptr) ); } PQclear(pgres); return (ret);}int vadd_ip_map( char *ip, char *domain) { PGresult *pgres; int err = 0; if ( ip == NULL || strlen(ip) <= 0 ) return(-1); if ( domain == NULL || strlen(domain) <= 0 ) return(-1); if ( (err=vauth_open()) != 0 ) return(err); if( ( err=pg_begin() )!= 0 ) { /* begin transaction */ return(err); } qnprintf(SqlBufUpdate,SQL_BUF_SIZE, "delete from ip_alias_map where ip_addr='%s' and domain='%s'", ip, domain); /* step 1: delete previous entry */ pgres=PQexec(pgc, SqlBufUpdate); if( pgres ) PQclear(pgres); /* don't check pgres status table may not exist */ /* step 2: insert new data */ qnprintf(SqlBufUpdate,SQL_BUF_SIZE, "insert into ip_alias_map (ip_addr,domain) values ('%s','%s')", ip, domain); pgres=PQexec(pgc, SqlBufUpdate); if ( !pgres || PQresultStatus(pgres) != PGRES_COMMAND_OK ) { if( pgres ) PQclear(pgres); vcreate_ip_map_table(); qnprintf(SqlBufUpdate,SQL_BUF_SIZE, "insert into ip_alias_map (ip_addr,domain) values ('%s','%s')", ip, domain); pgres=PQexec( pgc, SqlBufUpdate); if ( !pgres || PQresultStatus(pgres) != PGRES_COMMAND_OK ) { fprintf( stderr, "vadd_ip_map: insert: %s\n", PQerrorMessage(pgc)); if( pgres ) PQclear(pgres); return -1; } } if( pgres ) PQclear(pgres); return ( pg_end() ); /* end transaction */}int vdel_ip_map( char *ip, char *domain) { PGresult *pgres; int err=0; if ( ip == NULL || strlen(ip) <= 0 ) return(-1); if ( domain == NULL || strlen(domain) <= 0 ) return(-1); if ( (err=vauth_open()) != 0 ) return(err); qnprintf( SqlBufUpdate,SQL_BUF_SIZE, "delete from ip_alias_map where ip_addr='%s' and domain='%s'", ip, domain); pgres=PQexec(pgc, SqlBufUpdate); if( !pgres || PQresultStatus(pgres)!=PGRES_COMMAND_OK ) { fprintf(stderr, "vdel_ip_map: delete failed: %s\n", PQerrorMessage(pgc)); if(pgres) PQclear(pgres); /* #warning why are we returning 0 when we couldn't delete?*/ return(0); } if(pgres) PQclear(pgres); return(0);} int vshow_ip_map( int first, char *ip, char *domain ){ static PGresult *pgres=NULL; static unsigned ntuples=0, ctuple=0; int err= 0; if ( ip == NULL ) return(-1); if ( domain == NULL ) return(-1); if ( ( err=vauth_open() ) != 0 ) return(err); if ( first == 1 ) { snprintf(SqlBufRead,SQL_BUF_SIZE, "select ip_addr, domain from ip_alias_map"); if (pgres) { PQclear(pgres); ntuples=ctuple=0; } if ( ! (pgres=PQexec(pgc, SqlBufRead)) || PQresultStatus(pgres) != PGRES_TUPLES_OK ) { if(pgres) PQclear(pgres); snprintf(SqlBufRead,SQL_BUF_SIZE, "select ip_addr, domain from ip_alias_map"); vcreate_ip_map_table(); if ( ! (pgres=PQexec(pgc, SqlBufRead)) || PQresultStatus(pgres) != PGRES_TUPLES_OK ) { return(0); } } ntuples=PQntuples(pgres); } if ( ctuple == ntuples ) { PQclear(pgres); ntuples=ctuple=0; return (0); } strncpy( ip, PQgetvalue( pgres, ctuple, 0), 18); strncpy( domain, PQgetvalue( pgres, ctuple, 1), 156); strncpy( ip, PQgetvalue( pgres, ctuple, 0), 18); strncpy( domain, PQgetvalue( pgres, ctuple, 1), 156); ctuple++; return 1;}#endifint vread_dir_control(vdir_type *vdir, char *domain, uid_t uid, gid_t gid){ PGresult *pgres; int found = 0; if ( vauth_open() != 0 ) return(-1); qnprintf(SqlBufUpdate, SQL_BUF_SIZE, "select %s from dir_control where domain = '%s'", DIR_CONTROL_SELECT, domain ); if (!(pgres=PQexec(pgc, SqlBufUpdate)) || PQresultStatus(pgres)!=PGRES_TUPLES_OK ) { if( pgres ) PQclear(pgres); vcreate_dir_control(domain); qnprintf(SqlBufUpdate, SQL_BUF_SIZE, "select %s from dir_control where domain = '%s'", DIR_CONTROL_SELECT, domain ); if (! (pgres=PQexec(pgc, SqlBufUpdate)) || PQresultStatus(pgres)!=PGRES_TUPLES_OK ) { fprintf(stderr, "vread_dir_control: q: %s\npgsql: %s", SqlBufUpdate, PQerrorMessage(pgc)); if (pgres) PQclear (pgres); return (-1); } } if ( PQntuples(pgres) > 0 ) { found = 1; vdir->cur_users = atol( PQgetvalue( pgres, 0, 0 ) ); vdir->level_cur = atoi( PQgetvalue( pgres, 0, 1 ) ); vdir->level_max = atoi( PQgetvalue( pgres, 0, 2 ) ); vdir->level_start[0] = atoi( PQgetvalue( pgres, 0, 3 ) ); vdir->level_start[1] = atoi( PQgetvalue( pgres, 0, 4 ) ); vdir->level_start[2] = atoi( PQgetvalue( pgres, 0, 5 ) ); vdir->level_end[0] = atoi( PQgetvalue( pgres, 0, 6 ) ); vdir->level_end[1] = atoi( PQgetvalue( pgres, 0, 7 ) ); vdir->level_end[2] = atoi( PQgetvalue( pgres, 0, 8 ) ); vdir->level_mod[0] = atoi( PQgetvalue( pgres, 0, 9 ) ); vdir->level_mod[1] = atoi( PQgetvalue( pgres, 0, 10 ) ); vdir->level_mod[2] = atoi( PQgetvalue( pgres, 0, 11 ) ); vdir->level_index[0] = atoi( PQgetvalue( pgres, 0, 12 ) ); vdir->level_index[1] = atoi( PQgetvalue( pgres, 0, 13 ) ); vdir->level_index[2] = atoi( PQgetvalue( pgres, 0, 14 ) ); strncpy(vdir->the_dir, PQgetvalue( pgres, 0, 15 ) , MAX_DIR_NAME); } PQclear(pgres); if ( found == 0 ) { int i; vdir->cur_users = 0; for(i=0;i<MAX_DIR_LEVELS;++i) { vdir->level_start[i] = 0; vdir->level_end[i] = MAX_DIR_LIST-1; vdir->level_index[i] = 0; } vdir->level_mod[0] = 0; vdir->level_mod[1] = 2; vdir->level_mod[2] = 4; vdir->level_cur = 0; vdir->level_max = MAX_DIR_LEVELS; vdir->the_dir[0] = 0; } return(0);}int vwrite_dir_control(vdir_type *vdir, char *domain, uid_t uid, gid_t gid){ PGresult *pgres; if ( vauth_open() != 0 ) return(-1); qnprintf(SqlBufUpdate, SQL_BUF_SIZE, "delete from dir_control where domain='%s'", domain ); if( pg_begin() ) { /* begin transaction */ return -1; } pgres=PQexec(pgc, SqlBufUpdate); if( !pgres || PQresultStatus(pgres)!=PGRES_COMMAND_OK ) { fprintf(stderr, "vwrite_dir_control: delete failed: %s", PQerrorMessage(pgc)); if (pgres) PQclear (pgres); return -1; } qnprintf(SqlBufUpdate, SQL_BUF_SIZE, "insert into dir_control ( \domain, cur_users, \
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -