Line data Source code
1 : // Snap Websites Server -- like shell `chown <user>:<group> <path+file>`
2 : // Copyright (c) 2016-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 :
19 : // self
20 : //
21 : #include "snapwebsites/chownnm.h"
22 :
23 :
24 : // snapwebsite lib
25 : //
26 : #include "snapwebsites/log.h"
27 :
28 :
29 : // C lib
30 : //
31 : #include <grp.h>
32 : #include <pwd.h>
33 : #include <unistd.h>
34 :
35 :
36 : // last include
37 : //
38 : #include <snapdev/poison.h>
39 :
40 :
41 : namespace snap
42 : {
43 :
44 :
45 : /** \brief Set the owner and group of a file or directory.
46 : *
47 : * This function determines the user identifier and group identifier
48 : * from the specified names and use them to call chown().
49 : *
50 : * The function can fail if an identifier for the user or group names
51 : * cannot be determined.
52 : *
53 : * The function may fail if the path is invalid or permissions do not
54 : * allow for ownership modifications.
55 : *
56 : * \param[in] path The path to the file or directory to change.
57 : * \param[in] user_name The name of the user.
58 : * \param[in] group_name The name of the group.
59 : *
60 : * \return 0 if chown succeeded,
61 : * -1 if an error occurs (i.e. permissions denied, unknown user/group)
62 : * and errno is set accordingly.
63 : */
64 1 : int chownnm(std::string const & path
65 : , std::string const & user_name
66 : , std::string const & group_name)
67 : {
68 1 : uid_t uid(-1);
69 1 : gid_t gid(-1);
70 :
71 : // user name specified?
72 : //
73 1 : if(!user_name.empty())
74 : {
75 1 : struct passwd const * pwd(getpwnam(user_name.c_str()));
76 1 : if(pwd == nullptr)
77 : {
78 0 : return -1;
79 : }
80 1 : uid = pwd->pw_uid;
81 : }
82 :
83 : // group name specified?
84 : //
85 1 : if(!group_name.empty())
86 : {
87 1 : struct group const * grp(getgrnam(group_name.c_str()));
88 1 : if(grp == nullptr)
89 : {
90 0 : return -1;
91 : }
92 1 : gid = grp->gr_gid;
93 : }
94 :
95 1 : if(uid != static_cast<uid_t>(-1)
96 0 : || gid != static_cast<gid_t>(-1))
97 : {
98 1 : return chown(path.c_str(), uid, gid);
99 : }
100 :
101 : // in case both are undefined (it happens in the mkdir_p() function)
102 : //
103 0 : return 0;
104 : }
105 :
106 :
107 1 : int chownnm(QString const & path, QString const & user_name, QString const & group_name)
108 : {
109 2 : return chownnm(path.toUtf8().data()
110 2 : , user_name.toUtf8().data()
111 3 : , group_name.toUtf8().data());
112 : }
113 :
114 :
115 1 : int chownnm(char const * path, char const * user_name, char const * group_name)
116 : {
117 2 : return chownnm(path == nullptr ? std::string() : std::string(path)
118 2 : , user_name == nullptr ? std::string() : std::string(user_name)
119 3 : , group_name == nullptr ? std::string() : std::string(group_name));
120 : }
121 :
122 :
123 6 : } // snap namespace
124 : // vim: ts=4 sw=4 et
|