3

From the documentation, I can run gpg-agent with custom config like this and I think this is the official way:

The following gpg-agent.conf contains this:

allow-preset-passphrase
default-cache-ttl 34560000
max-cache-ttl 34560000

and to run gpg-agent with custom config:

#!/bin/bash
GPG_CONFIG_FILE="/opt/gpg-agent.conf"
gpg-agent --options $GPG_CONFIG_FILE --daemon

From my observation, the gpg-agent reads the custom config perfectly with no issue.

But, then I notice that if a gpg-agent is not running, then when I run a gpg command it will automatically run gpg-agent. This are example of commands that will call gpg-agent and start it automatically:

1) echo RELOADAGENT | gpg-connect-agent # Reload and forget passphrase of gpg-agent
2) KEY_GRIP=$(gpg --with-keygrip --list-secret-keys $KEY_ID | grep -Pom1 '^ *Keygrip += +\K.*') # List key grip for that key_id

The problem is the above commands did not run gpg-agent with custom config. I'm writing a bash script where the config will be generated with predefined value automatically inside the script own path (if it does not exist). So the script will only use this custom config not in ~/.gnupg.

$ ls
myscript.sh
conf/gpg-agent.conf

Question:

  1. How do I make the gpg-agent to use custom config when running the above example commands ?

  2. If question 1) is not possible, how do I prevent gpg-agent from running when executing the commands example given above that will automatically run gpg-agent ?

8
  • 1
    From your snippet it seems you are sometimes trying to run gpg commands in a subshell. What about simply export GPG_CONFIG_FILE=... in your terminal before you run those commands?
    – Fiisch
    Sep 15, 2020 at 10:23
  • 2
    This is standard shell feature and it is not related to gpg. :)
    – Fiisch
    Sep 15, 2020 at 10:55
  • Do you need more than one distinct configuration files for instances of gpg-agent run by the same user? In other words, can't you simply define that custom configuration in .gnupg/gpg-agent.conf (the default per-user config file)?.
    – fra-san
    Sep 15, 2020 at 11:00
  • @fra-san I need only 1 configuration file of gpg-agent.conf but in different location other than .gnupg/gpg-agent.conf. The reason is I want to package my script in one folder, which the subfolder contains conf/gpg-agent.conf (the script will generate empty file of gpg-agent.conf if it doesn't exist and will use only this file). Doing more test now.
    – MaXi32
    Sep 15, 2020 at 16:02
  • 1
    To me, the fact that export GPG_CONFIG_FILE=... won't work automagically (you have to either type it manually in the shell gpg is later invoked from or add it to the user's shell initialization file) somehow defeats the purpose of packaging. I would suggest using the techniques described in man gpg to automatically populate ~/.gnupg from /etc/skel/.gnupg instead, but this would be unrelated to your current question, which doesn't mention packaging.
    – fra-san
    Sep 15, 2020 at 16:26

1 Answer 1

1

This has been more than a year. So, I solved this issue. The problem is caused by bug using the --daemon option as described in here: https://dev.gnupg.org/T5076,

In brief, gpg-agent should not spawn another process if an existing process is already there when we try to execute gpg command but it was stubborn not to use the existing gpg-agent process and create its own random process without using the custom config.

So the solution for systemd user in my case, I need to use the --supervised option when starting gpg-agent. Never ever use the --daemon option.

More details:

It is not straightforward to use the --supervised option like you run it using --daemon option. This supervised mode requires the service file and socket file to communicate each other and it must be enabled through systemctl. Here are the systemd files (this link described everything how to enable it):

https://github.com/gpg/gnupg/tree/master/doc/examples/systemd-user

So I can modify the gpg-agent.service to use custom option, then start gpg-agent using systemctl --user start gpg-agent. After that, every gpg commands will use this process instead of creating the new one. Too see the current process ID of the running gpg-agent I can do this:

systemctl --user status gpg-agent

So, the above answered my previous first and second questions. The second question would not prevent gpg-agent from running, but it will not create an extra gpg-agent process as this is what I was trying to solve previously because I had multiple gpg-agent process running that caused error in passphrase validation when it was running using --daemon option; The error that I mentioned here also described in the gpg bug tracker above. So my objective from the 2nd question was trying to prevent every gpg command to start gpg-agent automatically so that I can start them manually. But I don't need this method anymore when using --supervised option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .