Selcuk's Blog

Pazartesi, Mart 19, 2018

Download files from FTP with FtpResponse on ASP.NET MVC application

        it woks for large files!
        [HttpGet]   
        public virtual void Download(string g)
        {
         

            Uri uri = (Uri)Session["file_full_path"];


            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
            request.Credentials = new NetworkCredential("username", "password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

           //find size
            request.Method = WebRequestMethods.Ftp.GetFileSize;

            FtpWebResponse responseSize = (FtpWebResponse)request.GetResponse();
            long size = responseSize.ContentLength;
            responseSize.Close();


            FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create(uri);
            request2.Credentials = new NetworkCredential("username", "password");
            request2.UsePassive = true;
            request2.UseBinary = true;
            request2.EnableSsl = false;

            request2.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse response = (FtpWebResponse)request2.GetResponse();
            Stream dfileResponseStream = response.GetResponseStream();

            Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.Charset = "ISO-8859-9";
           
            Response.AddHeader("content-Disposition", "attachment; filename=" + Path.GetFileName(uri.LocalPath));

            while (size > 0)
            {
                if (Response.IsClientConnected)
                {
                    byte[] buffer = new Byte[10000];
                    int length = dfileResponseStream.Read(buffer, 0, 10000);// stream.Read(buffer, 0, 10000);
                    Response.OutputStream.Write(buffer, 0, length);
                    Response.Flush();
                    size = size - length;
                }
                else
                {
                    size = -1;
                }
            }

            response.Close();
         
        }


Pazartesi, Eylül 25, 2017

Web Of Science WokSearchLite C# client

Hayatımda bu kadar saçma şey görmedim.

WSDL reference files:
http://ipscience-help.thomsonreuters.com/wosWebServicesLite/WebServicesLiteOverviewGroup/Introduction/wsdlFileLocations.html


        using WindowsFormsApplication6.com.webofknowledge.search;
        using WindowsFormsApplication6.com.webofknowledge.authenticate;

            WOKMWSAuthenticateService wa = new WOKMWSAuthenticateService();                    
            string session_id = wa.authenticate();          
            Uri target = new Uri("http://search.webofknowledge.com/");
         
            wa.CookieContainer = new CookieContainer();      
            wa.CookieContainer.Add(new Cookie("SID", session_id) { Domain = target.Host });                      
            WokSearchLiteService searcher = new WokSearchLiteService();          

            queryParameters q = new queryParameters();

            q.databaseId = "WOS";

            editionDesc[] my_editions = editionDesc[5];

            my_editions[0] = new editionDesc() { collection = "WOS", edition = "SCI" };
            my_editions[1] = new editionDesc() { collection = "WOS", edition = "SSCI" };
            my_editions[2] = new editionDesc() { collection = "WOS", edition = "AHCI" };
            my_editions[3] = new editionDesc() { collection = "WOS", edition = "ISTP" };
            my_editions[4] = new editionDesc() { collection = "WOS", edition = "ISSHP" };          

            q.editions = my_editions;

            q.userQuery = "AI=Researcher ID"; or //q.userQuery = "AU=Surname Name*";
            q.queryLanguage = "en";

           retrieveParameters rp = new retrieveParameters();
            rp.count = 100;
            rp.firstRecord = 1;

             //Cookie maybe unneccessary in here ???
            searcher.CookieContainer = new CookieContainer();          
            searcher.CookieContainer.Add(new Cookie("SID", session_id) { Domain = target.Host });


           searchResults sr = searcher.search(q, rp);
         
            wa.closeSession();

Çarşamba, Kasım 23, 2016

utf-8 code points for Turkish special chars (non-ascii)
UTF8:
Ç
00c7
\xc3 \x87
ç
00e7
\xc3 \xa7
Ğ
011e
\xc4 \x9e
ğ
011f
\xc4 \x9f
İ
0130
\xc4 \xb0
ı
0131
\xc4 \xb1
Ö
00d6
\xc3 \x96
ö
00f6
\xc3 \xb6
Ş
015e
\xc5 \x9e
ş
015f
\xc5 \x9f
Ü
00dc
\xc3 \x9c
ü
00fc
\xc3 \xbc

Salı, Mart 10, 2015

Chroot, sftp,scp, ssh, limited shell in Redhat Enterprise linux 5 (SSH 4.3P2.EL5)


       Few days ago, our client try to domain for their application. They wanted sftp access and also they need ssh command in bash for their user, but user is /bin/false shell :(

     After make some searches i found rssh and lshell but they didn't satify me.

    Ok, we have too many subdomains in RHEL5 server. users have own folder which can access ftp protocol. That's hy we just user's shell /bin/false. but this time we need to change this settings for one specific user

Here is the steps.

change shell of user /bin/bash
     chsh -s /bin/bash username

our folder structureis like this

/dir1/dir2/dir3/userhomefolder/....

dir1, dir2, dir3 permissions is root:root

userhomefolder permissions is root:root and mod is 755

goto username home folder.

mkdir -p home
mkdir -p dev
mkdir -p usr
mkdir -p usr/bin
mkdir -p bin
mkdir -p lib
mkdir -p usr/lib/openssh
mkdir -p etc
mkdir -p etc/pam.d/
mkdir -p root
chown username:usernamegroup root
chmod 755 root

mknod dev/null c 1 3
mknod dev/zero c 1 5
mknod  dev/tty c 5 0
mknod dev/urandom c 1 9
chmod 666 dev/null
chmod 666 dev/zero
chmod 666 dev/tty
chmod 666 dev/urandom

cp /lib/libnss_compat.so.2 /lib/libnsl.so.1 /lib/libnss_files.so.2 /lib/ld-linux.so.2 /lib/libcap.so.1 /lib/libnss_dns.so.2 ./lib/

cp /etc/hosts etc/
cp /etc/resolv.conf etc/
cp /etc/pam.d/* etc/pam.d/
cp -r /lib/security lib/
cp -r /etc/security etc/
cp /etc/login.defs etc/
cp /usr/lib/libgssapi_krb5.so.2 usr/lib/
cp /usr/lib/libkrb5.so.3 usr/lib/
cp /usr/lib/libk5crypto.so.3 usr/lib/
cp /lib/libcom_err.so.2 lib/
cp /usr/lib/libkrb5support.so.0 usr/lib/

echo '#!/bin/bash' > usr/bin/groups
echo "id -Gn" >> usr/bin/groups
touch etc/passwd
grep /etc/passwd -e "^root" > etc/passwd
grep /etc/username
grep /etc/group -e "^root" -e "^users" > etc/group

So all these folder owned by root, except root folder (becouse of ssh client )

after that, create a shell script under /usr/local/sbin or whereever you want

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

APPS="/bin/sh /bin/bash /usr/sbin/chroot /bin/cp /bin/ls /bin/mkdir /bin/mv /bin/pwd /bin/rm /bin/rmdir /usr/bin/id /usr/bin/ssh /usr/bin/ssh-keygen /bin/ping /usr/bin/dircolors /bin/vi /usr/bin/sftp /usr/libexec/openssh/sftp-server"   #your apps here
/usr/bin/sftp /usr/libexec/openssh/sftp-server is not neccessary but future use.
for prog in $APPS;  do
        mkdir -p ./`dirname $prog` > /dev/null 2>&1
        cp $prog ./$prog

        # obtain a list of related libraries
        ldd $prog > /dev/null
        if [ "$?" = 0 ] ; then
                LIBS=`ldd $prog | awk '{ print $3 }'`
                for l in $LIBS; do
                        mkdir -p ./`dirname $l` > /dev/null 2>&1
                        cp $l ./$l  > /dev/null 2>&1
                done
        fi
done

after that run this command in  /dir1/dir2/dir3/userhomefolder/

Ok, we have one more step for chroot. Chroot command run only root user, so we need to write a small program

#include   stdio.h
#include  stdlib.h
#include  sys/types.h
#include   unistd.h
#include   string.h



int main(int argc, char *argv[])
{
   char str_command[500] = "/root/chrt.sh ";
   strcat(str_command,argv[1]); // first parameter is user home folder.
   setuid( 0 );
   system( str_command );

   return 0;
}

save this code whatever. here is call-script.c. After that compile and set permissions

make call-script call-script.c
chmod +x call-script
chmod u+s call-script

let's create /root/chrt.sh file

#!/bin/bash
chroot $1  // this home path parameter from call-script program
and set permission

chmod +x /root/chrt.sh

and finally goto /etc/profile file add end of the file

if [ $USER == "username" ]; then
    /usr/local/src/call-script username_home_folder
fi


also you make change in sshd_config file ;

Subsystem       sftp    internal-sftp
ChrootDirectory %h


also i created web folder in user home dir, and set apache's documentroot this web folder.









REFERENCES
1. chroot error "cannot change root directory to /jail: Operation not permitted"
2. SSH Chroot in ISPConfig Centos-4.6








Çarşamba, Mayıs 28, 2014

Sieve and managesieve on redhat EL 6 with Dovecot 2.0.9


Last time , I applied LDA deliverey on our MTA instead of procmail.

let's try to add sieve funtions to dovecot.


first, stop everything :)


# service stop postfix

# service stop dovecot


after that you must install dovecot pigeonhole with



yum install dovecot-pigeonhole



after that, go to dovecot/conf.d directory.
1. Edit your 15-lda.conf file and 
uncomment plugins parameter in protocol lda seciton:



mail_plugins = $mail_plugins sieve 


We have aditional two files in here for sieve configuration, 20-managesieve.conf and 90-sieve.conf.

2. Go to 90-sieve.conf file and edit parameters below:

sieve = /var/sieve-scripts/%u.sieve
sieve_dir = /home/vmail/domains/sieve/%n/.sieve
Note:
 %u user     full username (e.g. user@domain)
 %n username user part in user@domain, same as %u if there's no domain
 %d domain   domain part in user@domain, empty if user with no domain

3. In 20-managesieve.conf file uncomment

  protocols = $protocols sieve

and 

 inet_listener sieve {
    port = 4190


  }
lines and add this parameters end of  file
plugin {
  # Used by both the Sieve plugin and the ManageSieve protocol
  sieve = /var/sieve-scripts/%u.sieve  
  sieve_dir = /home/vmail/domains/sieve/%n/.sieve
}


* Our mail_location is mbox:/home/vmail/domains/%d/%u and there is no mail_home config parameter. Home directory is come form OpenLDAP field (Jamm schema)

and scripts folder is like this.
drwxr-xr-x 2 vmail vmail 4096 May 28 14:43 /var/sieve-scripts 

And start everything
# service start postfix
# service start dovecot

I suggest , you must do change delivery method to LDA , so after that install sieve things. If you have any webmail interface like roundcube, afterlogic , you can install managesieve plugin or filter plugin than you can start create your filters.

Cuma, Mayıs 23, 2014

Convert mail delivery from Procmail to Dovecot 2.0.9 on Redhat EL 6


Maybe you installed this like MTA system

http://wanderingbarque.com/howtos/mailserver/mailserver.html

or

http://www.stefan-seelmann.de/wiki/mailserver-postfix-dovecot

So i installed this MTA system but our delivery method remained procmail. Last two days i try to change delivery system promail to dovecot, after thart i installed dovecot-pigeonhole sieve manager, so users can create their own filters or etc. Before that there is no need just like these thing , everybody is happy.

HowTo :

first stop everything,

service stop postfix
service stop dovecot

ok these are steps of procmail to dovecot

1. in main.cf

change mailbox_command to

mailbox_command = /usr/libexec/dovecot/dovecot-lda -f "$SENDER" -a "$RECIPIENT"


change virtual_transport to 
virtual_transport = dovecot
and add
dovecot_destination_recipient_limit = 1

2. in master.cf

dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}

in this example our user is vmail you must change with your own.


3. in dovecot/conf.d/15-lda.conf  add log file values
protocol lda
{
  log_path = /var/log/dovecot-lda-errors.log
  info_log_path = /var/log/dovecot-lda.log
  debug_log_path = /var/log/dovecot-lda-errors.log
}
maybe all these log values are not neccessary but it works :) i didn't touch them.

4. in dovecot/conf.d/10-master.conf uncomment mod and user, remember vmail is our user in here

service auth {
  # auth_socket_path points to this userdb socket by default. It's typically
  # used by dovecot-lda, doveadm, possibly imap process, etc. Its default
  # permissions make it readable only by root, but you may need to relax these
  # permissions. Users that have access to this socket are able to get a list
  # of all usernames and get results of everyone's userdb lookups.
  unix_listener auth-userdb {
    mode = 0600
    user = vmail 
    #group = 
  }


finally start everything 

service start postfix
service start dovecot

that's it.

i hope helps someone else.

Çarşamba, Kasım 20, 2013

BIND DNS Subdomain Settings For Office 365

When you have BIND DNS server with subdomains, and you want to add Office 365 e-mail server settings one of the subdomains, you need add to records below in your master zone dns file.
;
; Microsoft Office 365
;
mysubdomain.domain.com.tr.         IN      MX             0 mysubdomain-comain-com-tr.mail.protection.outlook.com.

mysubdomain.domain.com.tr.         IN      TXT             "v=spf1 include:spf.protection.outlook.com -all"

 autodiscover.mysubdomain.domain.com.tr.    IN      CNAME   autodiscover.outlook.com.

sip.mysubdomain.domain.com.tr.     IN      CNAME   sipdir.online.lync.com.
lyncdiscover.mysubdomain.domain.com.tr.    IN      CNAME   webdir.online.lync.com.
;
; Microsoft Lync Online
;
_sip._tls.mysubdomain.domain.com.tr.        IN      SRV 100 1 443 sipdir.online.lync.com.
_sipfederationtls._tcp.mysubdomain.domain.com.tr.   IN      SRV 100 1 5061 sipfed.online.lync.com.
msoid.mysubdomain.domain.com.tr.   IN      CNAME   clientconfig.microsoftonline-p.net.
;