orz.conf

技術メモ✍

sudoの脆弱性(CVE-2017-1000367)

sudoの脆弱性が報告されているんだけど、IPAのホームページに記載が無い。
現在2017/06/12で報告は2017/05/30。

危険度高って書いてあるサイトもあるので注意喚起しないレベルの
話では無いような気がするんだけど、どういった基準なのか分からない。

対応方法はCentOSなら下記

yum install sudo

アップデート前
sudo-1.8.6p7-17.el7_2.x86_64

アップデート後は次のようになりました。
sudo-1.8.6p7-22.el7_3.x86_64

参考サイト
Red Hat Customer Portal
tecadmin.net

CentOS7にpostgresをインストール

CentOS7にpostgresql-9.6をインストールしてクライアント(OSはWin10 DB接続ツールはA5:SQL MK2)からアクセスしてみました。
CentOS7とクライアントは同セグメントです。

rpmパッケージをインストール

wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
rpm -ivh pgdg-centos96-9.6-3.noarch.rpm

続いてyumにて下記をインストール
rpmパッケージインストール後でないと下記のコマンドでは何もインストールされませんでした。

yum -y install postgresql96-server postgresql96-devel postgresql96-contrib

セットアップファイルを検索

find / -name "postgresql*-setup*" -print

下記場所が出力される

/usr/pgsql-9.6/bin/postgresql96-setup

dbinitを実行

/usr/pgsql-9.6/bin/postgresql96-setup initdb

postgresユーザが作成されるので、必要であればパスワード変更。

コンフィグファイルの修正(場所はfindコマンドで検索)
pg_hba.confの編集
IPv4 local connectionsにローカルエリアを追記

# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             192.168.0.0/16          ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

postgresql.confの編集
中頃にCONNECTIONS AND AUTHENTICATIONがあるのでリッスンアドレスやポートを設定する

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/var/run/postgresql, /tmp'  # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name
                                        # (change requires restart)

起動する。

systemctl start postgresql-9.6

外部から接続出来るようにポートが開いているかどうか確認
テスト環境なのでどこからでもアクセスおk。

netstat -antu
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN

ファイアーウォールの許可

firewall-cmd --add-service=postgresql --zone=public --permanent
systemctl restart firewalld

postgresの初期設定

# su postgres
$ psql

DBが持っているユーザ postgresのパスワードを変更

postgres=# alter role postgres with password 'パスワード';

テスト接続用DBを作成

postgres=# CREATE DATABASE TEST;

クライアントから接続してみます。

接続出来なかったらログを参照。

tail -f /var/lib/pgsql/9.6/data/pg_log/postgresql-Sun.log
< 2017-04-23 13:49:24.699 JST > FATAL:  ユーザ"postgres"のパスワード認証に失敗しました
< 2017-04-23 13:49:24.699 JST > 詳細:  User "postgres" has no password assigned.
        接続はpg_hba.confの行83に一致しました: "host    all             all             192.168.0.0/16          md5"

上で設定したDBが持っているユーザ postgresのパスワード変更を
してないと上記のように出力されていました。

使用するデータベースに接続

postgres=# \connect TEST

テスト用テーブル作成

CREATE TABLE testTbl
  (No INT NOT NULL,
   username varchar(100),
   create_datetime timestamp,
   PRIMARY KEY(No, username));

テスト用データインサート

insert into testTbl values (1, "テスト", current_timestamp);

クライアントからデータ取得出来るか確認できれば完了です。

lets encryptとお名前.comでドメイン取得

ssl証明書を無料で取得出来るなんて良い時代になりました。
https://letsencrypt.jp/

インストールです。
apacheに設定してやります。
opensslとmod_sslのインストールが済んでいないようならインストールします。

yum install -y openssl mod_ssl

epelリポジトリを有効にして下記コマンドを実行

wget https://dl.eff.org/certbot-auto
chmod 700 certbot-auto
./certbot-auto

いよいよ証明書を取得します。
取得用にサーバが起動するようなのでapacheを止めます。

service httpd stop

以下は覚書なのでうまくいかなかったら調べ直す必要があるかもしれません。あってるかな。
ポート80なら↓

./certbot-auto certonly --standalone-supported-challenges http-01 -d ドメイン名

ポート443なら↓

./certbot-auto certonly --standalone-supported-challenges tls-sni-01 -d ドメイン名

mydnsのドメインでは証明書がなかなか発行されないので(1週間で同ドメインには20通まで?)
お名前.comにドメインを取得しcnameを張りました。

更新はwebサーバを止めて下記コマンドで実施することが出来ます。

./letsencrypt-auto renew

gitについて

gitサーバ、クライアントの設定

まずはサーバの構築
サーバはCentOSを想定。

# yum install git

グループ、ユーザを作成する。
unixのユーザがそのままgitで使用される。

# groupadd gitgroup
# useradd -g gitgroup gituser

パスワードも忘れずに。

# passwd gituser

リポジトリディレクトリを作成する。
XXX.gitという名前が一般的。

# mkdir -p /usr/local/git/rep.git

作成したディレクトリの権限をgit用ユーザに。

# chown gituser rep.git/

作成したgit用ユーザにスイッチ。

# su - gituser

git initして完成。

$ git init --bare --shared

クライアント側

/usr/local/git/のとこは適宜修正します。

git clone ssh://gituser@サーバアドレス/usr/local/git/git.rep

新しいブランチを作る

git checkout -b new_branch

pushするとリモートに同名のブランチが作成される

git push

ラッキングブランチの設定をする

git branch --set-upstream-to=origin/リモートブランチ ローカルブランチ

以下調整中
git log --pretty=format:"[%ad] %h %an : %s"
git checkout-index -a -f --prefix=export/

mysqlでエラー

You can't specify target table 'x' for update in FROM clause
こんなエラーが出て困りました。
意味はテーブル'x'をFROM句に入れてアップデートのために明示することは出来ません。
つまりfrom句にテーブル'x'を記載しないで下さいという感じでしょうか。
clauseが句なんですね。

TABLE1にはNOというカラムが存在し、
その最大値のレコードを削除しようとしました。

DELETE FROM
  TABLE1
WHERE
  NO = (
    SELECT
      MAX(NO)
    WHERE
      TABLE1
    );

mysqlだと削除するテーブルとサブクエリのテーブルが同じだと
怒られてしまうようです。

DELETE FROM
  TABLE1
ORDER BY
  NO DESC
LIMIT
  1;

とか回避策を使うしか無いようです。

頻繁に使う条件ならfunction化するのも良いのかな。

sedについていくつか

sedでurl置換したい時に、エスケープが面倒なので調べてみると

sed -e "s|http://local.com/|https://honban.com/|g" in.html out.html

のようにすればエスケープする必要がなくなる。

sed -i -e "s|http://local.com/|https://honban.com/|g" xxx.html

のように-iで上書き保存。