Bug 229473

Summary: support /etc/profile.d by default for sh environment snippets
Product: Base System Reporter: Luca Lesinigo <luca>
Component: confAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Many People CC: 0mp, dreibh, jwb, mmpestorich, roberfern
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description Luca Lesinigo 2018-07-02 13:40:45 UTC
For automation purposes it would be useful to have a directory where "environment snippets" could be dropped without risking to interfere with other stuff on the same system.

Having /etc/profile source /etc/profile.d/*.sh by default would provide such a facility, just like is being done on other operating systems.

This would make my life as a Puppet user so much easier allowing me reuse the same code on different operating systems and it would be useful for package/ports maintainers too.

I tested this snippets at the end of the stock /etc/profile and AFAIK it is working without any problem with both the standard sh shell and the ports-installed bash shell:

# source *.sh files from /etc/profile.d if they are readable
if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
  unset i
Comment 1 Roberto Fernandez Cueto 2018-07-03 08:23:42 UTC
In which file did you inserted those lines?
Have you tested it with tsch?

How about the other shells offered by the ports?
Comment 2 Roberto Fernandez Cueto 2018-07-03 08:27:58 UTC
I just saw that it was at the end of /etc/profile
Comment 3 Luca Lesinigo 2018-07-03 16:51:31 UTC
Yes indeed it would go at the end of the already-provided /etc/profile.
This way it should introduce no regressions of any kind and just provide new functionality if/when someone wants to use it.

The current /etc/profile from FreeBSD sources says that it is "System-wide .profile file for sh(1)" so I did not bother to test it with tcsh, as far as I know tcsh does not use /etc/profile nor ~/.profile.
Comment 4 Jason W. Bacon freebsd_committer 2018-10-04 15:57:56 UTC
As someone who supports CentOS and FreeBSD in production, I'm potentially interested in this as well.  It would simplify creating portable shell env customizations that work on both BSD and Linux.

I wonder if there are security or stability concerns that someone might raise, though.  A feature like this opens up the possibility of ports installing things that modify the default environment, unbeknownst to the sysadmin.

If it goes forward, we would need checks in all the startup scripts, e.g. /etc/csh.cshrc, /etc/csh.login, /etc/profile, ...

We might also want to check ${LOCALBASE}/etc/profile.d.
Comment 5 Thomas Dreibholz 2021-05-03 18:51:26 UTC
Support for /etc/profile.d would indeed be useful. When creating packages of software making customisations of the profile, it is helpful to have a per-package profile configuration, instead of having to merge things into a single /etc/profile file during installation. This particularly makes a safe and clean deinstallation difficult.

Package installation -> add a package-specific file to /etc/profile.d
Package removal -> remove the file
Comment 6 Jason W. Bacon freebsd_committer 2021-05-03 22:55:43 UTC
Some additions I would make to Luca's example:

1. Add ${LOCALBASE}/etc/profile.d

2. Make sure the profile.d directories and all scripts in them are owned by root:wheel and not group or world writable.