Debian package:user and group - cmake

Recently,I use cmake/cpack to make a debian package.I make succeed.But I install it on the computer(sudo dpkg -i mypcakge.deb), I find a problem: the install program's user and group is root:root.Due to some reason, my problem should run in a specific user/group. I don't know how to do this, could you help me?
Also, I don't want to modify the user and group with chown(..) after I installed the package.Is there a way to add something in the CMakeLists to do this?

It is not related only to CMake, but in general when using debhelper and/or non-standard usernames.
dh(1) often used in debian/rules by default will call dh_fixperms(1), which will reset owners and permissions by default.
There are basically three ways to retain owners/permissions you want:
if you use some standard Debian system user/group, you may skip
or override dh_fixperms(1) and you may specify files to override owner/group for. Be sure that your package pre-depends on correct version of base-passwd package which contains wanted users/groups.
You could override in your debian/rules like here (see dh_fixperms(1) man page):
override_dh_fixperms:
dh_fixperms --exclude matchname
Debian docs have more info.
if you create your own users and you want files/dirs to be owned by them, you will need to use postinst script. In it, you first need to create users/groups if they don't already exist, and then do something like
chown myuser:mygroup /var/log/mydir
chmod 0750 /var/log/mydir
Alternatively, you can use (also in your postinst and postrm scripts) dpkg-statoverride(8) to change owners,groups and permissions of your (or other) files like this:
for i in /usr/bin/foo /usr/sbin/bar; do
# only do something when no setting exists
if ! dpkg-statoverride --list $i >/dev/null 2>&1; then
#include: debconf processing, question about foo and bar
if [ "$RET" = "true" ] ; then
dpkg-statoverride --update --add sysuser root 4755 $i
fi
fi
done
You should also handle removal, like this:
for i in /usr/bin/foo /usr/sbin/bar; do
if dpkg-statoverride --list $i >/dev/null 2>&1; then
dpkg-statoverride --remove $i
fi
done

Related

Read host system's environment variables build time in Singularity

When I'm building a Singularity container I'd like to read environment variables from the host system in the %post section. I've been looking online for a way to achieve this, but to no avail. I'm starting to question if this is even possible at the moment, but I can't find any mentions of it being possible/impossible.
Example:
Singularity definition file: recipe
BootStrap: docker
From: continuumio/anaconda3
%runscript
%post
echo $TEST_ENV_VARIABLE
On the host system / OS
export TEST_ENV_VARIABLE='foo'
sudo singularity build test.sif recipe
prints only a blank line when echoing TEST_ENV_VARIABLE.
If there is no way of reading host system's environment variables in the %post section, are there any other ways of passing arguments into the recipe that could be used build-time?
That is not currently possible, though there is an open issue for that functionality. I'm not personally a fan of dynamic build options as it makes it harder to guarantee reproducibility.
If you do want something more dynamic, you could use a template to create different definition files. A very simplistic example:
$ cat gen_def.py
#!/usr/bin/env python3
import sys
my_def = """BootStrap: docker
From: continuumio/anaconda3
%post
echo This is {0}
echo This is {1}"""
print(my_def.format(*sys.argv[1:]))
$ ./gen_def.py one two > Singularity.custom
$ sudo singularity build test.sif Singularity.custom

scp command - transfer folder over ssh

I have a Arduino Yun and want setup the server for Yun.
So what I want is to copy a folder that contain a py file and a index.html to my Yun
I used mac terminal to do this operation
the command looks like this
scp -r /Users/gudi/Desktop/LobsterHeartRate root#192.168.240.1:/mnt/sda1
and then terminal asked for the password
after I typed, it shows
scp: /mnt/sda1/LobsterHeartRate: Not a directory
I didn't type /mnt/sda1/LobsterHeartRate why it shows this error
Your code
scp -r /Users/gudi/Desktop/LobsterHeartRate root#192.168.240.1:/mnt/sda1
requires that the remote directory /mnt/sda1 exists. This looks like it is not true in your case. Check it using ssh root#192.168.240.1 ls /mnt/sda1.
scp is simple tool and it does not allow you to rename directories on the fly and the target directory must exists. You might try
scp -r /Users/gudi/Desktop/LobsterHeartRate root#192.168.240.1:/mnt/
ssh root#192.168.240.1 mv /mnt/LobsterHeartRate /mnt/sda1
or so, if it will suit your needs. But copying more files, rsync is usually more suitable. Check its manual page and give it a try next time.
As #Jens Höpken notes, your post is a bit sparse. But trying to read between the lines of your post I suspect that LobsterHeartRate is a DIRECTORY on your local system but a FILE named LobsterHeartRate in your target system. This might be happening right at the top of the directory tree, or perhaps you have directories/files of the same name further down the tree. scp -rv might help resolve any confusions here.
Beware: scp -r resolves symbolic links. If you want to preserve symlinks you need to do something else. For historic reasons I use the following, though cpio with a find front-end opens up interesting possibilities for fine-grained file selections.
( cd /Users/gudi/Desktop && tar -cf - LobsterHeartRate ) |
ssh root#192.168.240.1 'cd /mnt/sda1 && tar -xf -'
For a safe "dry run" you could change the -xf to a -tf. The && chains are required to prevent bad things from happening if any prior command fails.
Disclaimer: any debugging is left as an exercise for the student.

Adding home-brew to PATH

I just installed Home-brew and now I'm trying to insert the home-brew directory at the top of my path environment variable by typing in two commands inside my terminal. My questions are these:
What is a path environment variable?
Are the two codes provided me correct?
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
After this I am to type in brew doctor. Nothing is happening as far as I can see.
Can anyone offer me some advice or direction?
I installed brew in my new Mac M1 and ask me to put /opt/homebrew/bin in the path, so the right command for this case is:
echo "export PATH=/opt/homebrew/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
TL;DR
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
is what you want.
To answer your first question; in order to run (execute) a program (executable) the shell must know exactly where it is in your filesystem in order to run it. The PATH environment variable is a list of directories that the shell uses to search for executables. When you use a command that is not built into the shell you are using the shell will search through these directories in order and will execute the first matching executable it finds.
For example when you type: mv foo bar the shell is almost certainly actually using an executable located in the /bin directory. Thus fully the command is
/bin/mv foo bar
The PATH environment variable therefore saves you some extra typing. You can see what is in your PATH currently (as you can with all environment variables) by entering:
echo $<NAME OF VARIABLE>
So in this instance:
echo $PATH
As I mentioned earlier, ordering is important. Adding /usr/local/bin to the beginning of PATH means that the shell will search there first and so if you have an executable foo in that folder it will be used in preference to any other foo executables you may have in the folders in your path. This means that any executables you install with brew will be used in preference to the system defaults.
On to your second question. What the command you have provided is trying to do is add a line to your .bash_profile and then source it. The .bash_profile is a text file stored in your home directory that is sourced (read) every time bash (your shell) starts. The mistake in the line you've provided is that only the first letter of PATH is capitalised. To your shell Path and PATH are very different things.
To fix it you want:
echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
To explain
echo "export PATH=/usr/local/bin:$PATH"
simply prints or echoes what follows to stdout, which in the above instance is the terminal. (stdout, stderr and stdin are very important concepts on UNIX systems but rather off topic) Running this command produces the result:
export PATH=/usr/local/bin:/opt/local/sbin:/opt/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
on my system because using $PATH within double quotes means bash will substitute it with its value. >> is then used to redirect stdout to the end of the ~/.bash_profile file. ~ is shorthand for your home directory. (NB be very careful as > will redirect to the file and overwrite it rather than appending.)
&& means run the next command is the previous is successful and
source ~/.bash_profile
simply carries out the actions contained in that file.
As per the latest documentation, you need to do this:
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/dhruv/.bashrc
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
Now you should be able to run brew from anywhere.
When you type in a program somewhere and click enter, it checks certain locations to see if that program exists there.
Linux brew uses locations different from the normal linux programs, so we are adding these locations to the ~/.profile file which sets the paths.
Run this in your terminal, and it will place the correct code in the .profile file, automatically.
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile
Don't use .bash_profile because when you use something different from bash, like zsh, it may not work. .profile is the correct location.

PHP: Check if script is being ran with sudo or if user is not using `php`

How to make a check to find whether the script is run with sudo access or not using PHP ?
Note: this question would probably be more appropriate on Stack Overflow, even though it refers to PHP on Unix & Linux systems (privilege elevation, permissions, etc.).
You can use PHP's POSIX functions:
posix_geteuid() to get the effective user ID.
posix_getpwuid() to get user information from an UID.
Here is a little example:
<?php
$userinfo = posix_getpwuid(posix_geteuid());
echo "This script runs with " . $userinfo["name"] . "'s privileges.";
?>
Testing...
$ php myfile.php
This script is run with myuser's privileges.
$ sudo php myfile.php
This script is run with root's privileges.
By the way, since root is always UID 0, you could just check posix_geteuid() == 0.
Now, if you want to now whether the user is using the CLI (command-line) or going through the web server, have a look at this question on Stack Overflow and the php_sapi_name() function.
Another note: I'm pretty sure that running PHP scripts as root isn't the best of ideas. You may want to think again about what permissions your script really needs.
If it's purely about determining whether sudo is used, sudo puts a number of values in the environment of the command:
SUDO_COMMAND=/usr/bin/commandname
SUDO_USER=wurtel
SUDO_UID=1000
SUDO_GID=1001
These can be checked in php using the getenv() function. Of course, combine it with posix_geteuid() function to make sure you really do have elevated privileges as anyone can set those values in the environment.
Does it have to be in php only? You could chmod the php directory to only be executable by root and this should achieve the same result for you.
chmod 700 filename
chmod 700 foldername -R
-R
The -R means recursive, so any file or sub folders will get the same permissions as the parent. Use this with care.
To improve on this more you can create a 'phpadmin' user which can own the files so no other user can execute them besides root and phpadmin.
useradd phpadmin
chown phpadmin:phpadmin foldername -R
Warning
Please be very careful when using the chmod & chown commands. Make sure you are targeting the correct folder and file names before using these commands.

Oracle SQL Developer: sharing configuration via Dropbox

I would like to share my Oracle SQL Developer configuration across my several computers that use Dropbox.
How can I do this?
In case anyone comes here looking for the location of user configured options like me, they are hiding here:
%appdata%\SQL Developer\
This is useful to know when copying your preferences to a new computer. If you are looking for the connection settings, search for connections.xml in that directory. There are also some other configuration files here that you may need:
sqldeveloper.conf – <sqldeveloper dir>\sqldeveloper\bin\
ide.conf – <sqldeveloper dir>\ide\bin\
This is for Oracle SQL Developer 3.
Here's what I did.
#!/bin/bash
# share sqldeveloper config via dropbox
# this is for sqldeveloper 1.5.4, change your paths as necessary
# strace or dtruss sqldeveloper to see what config files are accessed
ITEMS="
o.ide.11.1.1.0.22.49.48/preferences.xml
o.ide.11.1.1.0.22.49.48/settings.xml
o.jdeveloper.cvs.11.1.1.0.22.49.48/preferences.xml
o.jdeveloper.subversion.11.1.1.0.22.49.48/preferences.xml
o.jdeveloper.vcs.11.1.1.0.22.49.48/preferences.xml
o.sqldeveloper.11.1.1.59.40/preferences.xml
o.sqldeveloper.11.1.1.59.40/product-preferences.xml
"
INST=~/Library/Application\ Support/SQL\ Developer/system1.5.4.59.40
DROP=~/Dropbox/Library/SQL\ Developer/system1.5.4.59.40
# note, you can zap your configuration if you are not careful.
# remove these exit lines when you're sure you understand what's
# going on.
exit
# copy from real folder to dropbox
for i in $ITEMS; do
echo uncomment to do this once to bootstrap your dropbox
#mkdir -p "`dirname "$DROP/$i":`"
#cp -p "$INST/$i" "$DROP/$i"
done
exit
# link from dropbox to real folder
for i in $ITEMS; do
rm "$INST/$i"
ln -s "$DROP/$i" "$INST/$i"
done
Simple sharing SQLDeveloper config on Dropbox, the easiest way on MACOSX is to
cd ~/Dropbox
mkdir -p Library/SQLDeveloper
cp -rp ~/.sqldeveloper/* Library/SQLDeveloper/
mv ~/.sqldeveloper ~/remove_when_sure_sqldeveloper
ln -sf $PWD/Library/SQLDeveloper ~/.sqldeveloper
Do this on your most important machine and on the machine on which to share only do
cd ~/Dropbox
mv ~/.sqldeveloper ~/remove_when_sure_sqldeveloper
ln -sf $PWD/Library/SQLDeveloper ~/.sqldeveloper
This works like a charm.