Malform encoding when connect printer with dll

Background

Recently, our company received a project, to help scenic sell tickets. I have to develop ticket print function.

Progress

Any Problem?

Our client have an existing system, they do this using preview, and call system print by browser.

WTF

Our client make a decision, when they click “print” button, there is no preview and other, just print it directly, and, we have to make it as a B/S arch. Therefore, I can only use IE to call dll.

At this moment, our leader tell me: there’s no problem, this framework support IE perfectly.

WTF x2

Yep, just like your think, it’s hardly to find a modern framework that support IE. I can just change my Implementation, run a server on local machine, there have some algorithm to calculate height, it’s annoying to rewrite, so I use a shortcut : all calculation use js on browser but just output printer commands, then local server can forward it to printer.

Problem Raised

It looks like the printer dll provided method have decode Unicode Character as GBK, so we have to convert the coding before send it to printer.

Solution

iconv, a library to convert between codings, this time we convert ‘UTF-8’ to ‘GBK’.

MySQL -h option invalid

At first, we should comfirm we create a user, like this


use mysql;
select Host,User from user;

I get this error in Centos+Mysql 5.6

For convenient, I created a user: example_user@%, but I still can not login with mysql -u example_user -h 127.0.0.1 -p, the error message said Access denied for user ‘example_user’@localhost , I specificated the host as 127.0.0.1, why converted to localhost? And the “%” character should match all host.

After a lot of test, 127.0.0.1 is same as localhost, and % not match localhost in this version of MySQL, so, we just have to create a user in localhost.


grant all on example_db.* on "example_user"@"localhost" identified by "your password";
flush privileges;

How to connect MySQL Server in wampserver

Open port will cause a security issue, so do that ONLY for dev or debug

I trust we all know that we should set an account for remote access in MySQL, such as


grant all on example_db.* on "example_user"@"%" identified by "your password";
flush privileges;

So, the reason is not misconfig of MySQL, it’s windows firewall, we have to open port by follow actions.

Search(win+Q)->Firewall-> Windows Defender Firewall-> inbound rule

create rule -> port -> release 3306 (sorry I don’t know what is it in English version)

[SOLVED]using dkms to install driver with errors

0X01 Cause

The standby time of my lapta\op (ThinkPad X1C 2014) when running linux is sooo short, but when running windows, the time is OK. So I searched on web: what can I do, and find TLP, it’s easy to use, basically, we can use default configuration. As we now, Lenovo have a threshold config for battery, I do care about it, because each of my laptop, the battery damage fist, TLP support this config (ThinkPad only), location at /etc/default/tlp, we need the module called acpi-call.

The installation is smoothly.


sudo apt-get update && sudo apt-get install acpi-call-dkms
# OK, let's check if we success 

sudo tlp-stat
# unfortunately, it still inactive

sudo dpkg -l | grep dkms # first, we have to check if we installed this package

sudo dkms install # I guess we should use like this.

# we will get a tip, tell us should not use like this, use help subcommand 

sudo dkms help # oh, this autoinstall is OK

Usage: /usr/sbin/dkms [action] [options]
[action] = { add | remove | build | install | uninstall | match | autoinstall
| mkdriverdisk | mktarball | ldtarball | mkrpm | mkkmp | mkdeb | mkbmdeb | status }
[options] = [-m module] [-v module-version] [-k kernel-version] [-a arch]
[-d distro] [-c dkms.conf-location] [-q] [--force] [--all]
[--templatekernel=kernel] [--directive='cli-directive=cli-value']
[--config=kernel-.config-location] [--archive=tarball-location]
[--kernelsourcedir=source-location] [--no-prepare-kernel] [--no-initrd]
[--binaries-only] [--source-only] [-r release (SuSE)] [--verbose]
[--size] [--spec=specfile] [--media=floppy|iso|tar] [--legacy-postinst=0|1]
[--no-depmod]
[-j number]
Error! No action was specified.
sudo dkms autoinstall

0X02 What happened

I find the error message below. It said we don’t have the function copy_from_user.

DKMS make.log for acpi_call-1.2.0 for kernel 4.12.1-chrysalis (x8664)
Thu Jul 13 19:31:39 CEST 2017
make -C /lib/modules/4.12.1-chrysalis/build M=/var/lib/dkms/acpi_call/1.2.0/build modules
make[1]: Entering directory ‘/tmp/linux-4.12.1’
CC [M] /var/lib/dkms/acpi_call/1.2.0/build/acpi_call.o
/var/lib/dkms/acpi_call/1.2.0/build/acpi_call.c: In function ‘acpi_proc_write’:
/var/lib/dkms/acpi_call/1.2.0/build/acpi_call.c:307:9: error: implicit declaration of function ‘copy_from_user’ [-Werror=implicit-function-declaration]
if (copy_from_user( input, buff, len )) {
^~~~~~~~~~~~~~
cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:309: /var/lib/dkms/acpi_call/1.2.0/build/acpi_call.o] Error 1
make[1]: *** [Makefile:1512: _module/var/lib/dkms/acpi_call/1.2.0/build] Error 2
make[1]: Leaving directory ‘/tmp/linux-4.12.1’
make: *** [Makefile:7: default] Error 2

I find several possibly solution:

  • Add -Werror=implicit-function-declaration compile option
  • use define to declare ( not suitable, I haven’t try it)
  • we have not import right header (at first, I think it should not have this error, it’s system official package)

And when I read the web page above, I find the function copy_from_user may locate at two location: <asm/uaccess.h> or <linux/uaccess.h>, use locate command to finde the source code, it import <asm/uaccess.h>, I changed asm to linux, then run command below, I success.


sudo dkms autoinstall

Dot language learning note

Dot is a script language in Graphviz tool box to create graphs. Using layout engine to create graph, then we can export graph to many format of image, such as png, jpg, svg .etc. So we can use it to ‘paint’ structure diagram, and flow charts.

Commonly used keywords
graph: define properties of the graph
node: define properties of nodes in the graph, such as fontname
subgraph: define a subgraph, should start with ‘cluster_’, or the engine can’t recognize it.

For example


digraph service_map {
    graph [dpi="300"];
    node [color="gray";shape="record";fontname="Microsoft YaHei"];
    
    label="Service Map";
    Request;
    github_pages [label="GitHub Pages"];
    subgraph cluster_web{
        label="Web front end server";
        nginx [label="Reverse Proxy Server"];
        docker [label="Docker Container Manager"];
    }
    subgraph cluster_docker{
        label="Docker Container Manager";
        openproject [label="OpenProject"];
        gitlab [label="GitLab"];
        wordpress [label="WordPress"];
        mysql [label="Common Mysql Database"];
    }
    subgraph cluster_filesystem{
        label="Mount at where";
        openproject_mnt [label="/opt/openproject/data:/var/lib/postgresql/9.6/main\l\n/opt/openproject/logs:/var/log/supervisor\l\n/opt/openproject/static:/var/db/openproject\l"];
        gitlab_mnt [label="/opt/gitlab/config:/etc/gitlab\l\n/opt/gitlab/logs:/var/log/gitlab\l\n/opt/gitlab/data:/var/opt/gitlab\l"];
        wordpress_mnt [label="/opt/wordpress/www:/var/www/html"];
        mysql_mnt [label="/opt/mysql/data:/var/lib/mysql"];
    }
    Request->nginx;
    nginx->docker;
    nginx->github_pages;
    docker->wordpress [label="hostname=wordpress.endercaster.com"];
    docker->openproject [label="hostname=openproject.endercaster.com"];
    docker->gitlab [label="hostname=gitlab.endercaster.com"];
    wordpress->mysql;
    wordpress->wordpress_mnt [label="mount"];
    openproject->openproject_mnt [label="mount"];
    gitlab->gitlab_mnt [label="mount"];
    mysql->mysql_mnt [label="mount"];
}

The code generated a diagram below

Using dot, I don’t have to care about layout, just pay attention to modules in software, what feature does it provide. It’s easily to use, I like it.

Shell Shock

Aims to:

A program contains setuid(geteuid()).

Requirement:

Bash version lower than 4.1

Reason:

When run, bash will load envirment vars, and custom function, like


export foo='(){:;};echo hello bash'

When I try this vulnerability, I type geteuid as getuid, and have not triger this.

A commemorative post for build local site with docker

At the first line, I wanna say: docker is the best tool to improve the production efficiency.

I want to build my own site for a long time, and today, succeed. I depley the three site at one server (my laptop, ThinkPad X1C 2014), the site works very smoothly. So I record my action below.

MySql


sudo apt-get update && sudo apt-get install mysql-server

WordPress


docker run -itd --name wordpress \

-v /opt/wordpress/html:/var/www/html \

-p 12080:80 \

wordpress

OpenProject


docker run -itd --name openproject \

-v /opt/openproject/pgdata:/var/lib/postgresql/9.6/main \

-v /opt/openproject/logs:/var/log/supervisor \

-v /opt/openproject/static:/var/db/openproject \

-p 10080:80 \

openproject/community

Gitlab


docker run -itd --name gitlab \

--hostname gitlab.endercaster.lan \

-v /opt/gitlab/config:/etc/gitlab \

-v /opt/gitlab/logs:/var/log/gitlab \

-v /opt/gitlab/data:/var/opt/gitlab \

-p 11080:80 \

-p 11022:22 \

-p 11443:443 \

gitlab/gitlab-ce

Then, using nginx as reverse proxy, proxy by host.