# os with ssh
# images
FROM centos:6.10

# clear yum cache
RUN rm -f /var/lib/rpm/__*
RUN yum clean all
RUN rm -rf /var/cache/yum

# make new repo with vault.centos
RUN mkdir -p /var/cache/yum/x86_64/6/base/
RUN mkdir -p /var/cache/yum/x86_64/6/extras/
RUN mkdir -p /var/cache/yum/x86_64/6/updates/

RUN echo "http://vault.centos.org/6.10/os/x86_64/" > /var/cache/yum/x86_64/6/base/mirrorlist.txt
RUN echo "http://vault.centos.org/6.10/extras/x86_64/" > /var/cache/yum/x86_64/6/extras/mirrorlist.txt
RUN echo "http://vault.centos.org/6.10/updates/x86_64/" > /var/cache/yum/x86_64/6/updates/mirrorlist.txt
#makecache
RUN yum makecache

# Update the system
RUN yum -y update

# Setting Utils
RUN yum install -y vim net-tools iputils wget tar curl zip unzip scp gcc gcc-c++ xz glibc

# Install OpenSSH Server
RUN yum install -y openssh-server openssh-clients openssh-devel mod_ssl

# Set up configuration for SSH
RUN mkdir /var/run/sshd
RUN echo 'root:!@#gds$%^' | chpasswd
RUN echo "    StrictHostKeyChecking no" >> /etc/ssh/ssh_config

# SSH login fix. Otherwise, the user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE="in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

# mySQL install
RUN yum install -y mysql-server

# mySQL setting
RUN sed -ri 's/^#?bind-address\s+.*/bind-address = 0.0.0.0/' /etc/my.cnf
RUN echo default-character-set=utf8 >> /etc/my.cnf
RUN echo lower_case_table_names=1 >> /etc/my.cnf
RUN echo default-time-zone='+9:00' >> /etc/my.cnf
RUN echo skip-character-set-client-handshake >> /etc/my.cnf

#apache install
RUN yum install -y httpd

#php install
RUN yum install -y php php-mbstring php-pdo php-xml php-mysql php-gd

# Install development tools and dependencies
RUN yum install -y centos-release-scl
RUN curl https://www.getpagespeed.com/files/centos6-scl-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl.repo
RUN curl https://www.getpagespeed.com/files/centos6-scl-rh-eol.repo --output /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
RUN yum update -y
RUN yum install -y devtoolset-8 gmp-devel mpfr-devel libmpc-devel

# Set up working directory
WORKDIR /usr/local/src

# Install updated build tools
RUN source /opt/rh/devtoolset-8/enable && \
    wget https://ftp.gnu.org/gnu/m4/m4-1.4.19.tar.gz && \
    tar -xzf m4-1.4.19.tar.gz && \
    cd m4-1.4.19 && \
    ./configure && \
    make -j4 && \
    make install && \
    wget https://ftp.gnu.org/gnu/binutils/binutils-2.36.1.tar.gz && \
    tar -xzf binutils-2.36.1.tar.gz && \
    cd binutils-2.36.1 && \
    ./configure --prefix=/usr/local && \
    make -j4 && \
    make install && \
    cd .. && \
    wget https://ftp.gnu.org/gnu/make/make-4.3.tar.gz && \
    tar -xzf make-4.3.tar.gz && \
    cd make-4.3 && \
    ./configure --prefix=/usr/local && \
    make -j4 && \
    make install && \
    wget https://ftp.gnu.org/gnu/bison/bison-3.7.6.tar.gz  && \
    tar -xzf bison-3.7.6.tar.gz && \
    cd bison-3.7.6 && \
    ./configure --prefix=/usr/local && \
    make -j4 && \
    make install && \
    wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.tar.xz && \
    tar -xJf linux-4.19.tar.xz && \
    cd linux-4.19 && \
    make headers_install INSTALL_HDR_PATH=/usr/local

# Install gettext from source
RUN source /opt/rh/devtoolset-8/enable && \
    wget https://ftp.gnu.org/gnu/gettext/gettext-0.21.tar.gz && \
    tar -xzf gettext-0.21.tar.gz && \
    cd gettext-0.21 && \
    ./configure --prefix=/usr/local && \
    make -j4 && \
    make install

#Set enable variables for gblic compile
ENV CFLAGS="-O2 -U_FORTIFY_SOURCE -fno-stack-protector"
ENV CPPFLAGS="-I/usr/local/include"
ENV LDFLAGS="-L/usr/local/lib"    

RUN wget http://ftp.gnu.org/gnu/libc/glibc-2.28.tar.gz && \
    tar -xzf glibc-2.28.tar.gz
COPY test-installation.pl /usr/local/src/glibc-2.28/scripts/test-installation.pl 
RUN source /opt/rh/devtoolset-8/enable && \
    cd ./glibc-2.28 && \
    mkdir build && \
    cd build && \
    ../configure --prefix=/usr --enable-add-ons --enable-obsolete-nsl --disable-profile --disable-sanity-checks --disable-werror \
                 --with-headers=/usr/local/include \
                 --with-binutils=/usr/local/bin && \
    make && \
    make install

RUN wget http://www.vuln.cn/wp-content/uploads/2019/08/libstdc.so_.6.0.26.zip && \
    unzip libstdc.so_.6.0.26.zip && \
    cp libstdc++.so.6.0.26 /usr/lib64/ && \
    cd /usr/lib64 && \
    cp libstdc++.so.6 libstdc++.so.6.bak && \
    rm -f libstdc++.so.6 && \
    ln -s libstdc++.so.6.0.26 libstdc++.so.6

# run script folder and copy script
RUN mkdir /service_script
COPY start_service.sh /service_script
RUN rm /etc/httpd/conf/httpd.conf
RUN rm /etc/ssh/sshd_config
RUN rm /etc/php.ini
RUN rm /etc/my.cnf
COPY httpd.conf /etc/httpd/conf
COPY sshd_config /etc/ssh/
COPY php.ini /etc
COPY my.cnf /etc
COPY setting.sql /service_script
COPY ssl.conf /etc/httpd/conf.d

# Setting locale ko
RUN localedef -i ko_KR -f UTF-8 ko_KR.UTF-8 && \
    echo -e "# Setting Language(ko_KR.UTF-8)\nexport LANGUAGE=ko_KR.UTF-8\nexport LANG=ko_KR.UTF-8" >> /etc/profile

RUN echo -e "source /opt/rh/devtoolset-8/enable" >> /etc/profile

# Expose port
EXPOSE 22 3306 80 443

# Run SSH, DB, APACHE
CMD ["/service_script/start_service.sh"]
