annotate cinst/nssstore_linux.c @ 383:5eb7ee4ee819

Look up executable name based on /proc/self/exe
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 15 Apr 2014 17:47:07 +0200
parents 824ef90a6721
children 638db75f0fdf
rev   line source
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1 #ifndef WIN32
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2
321
824ef90a6721 Move is_elevated into common/util.c file for better reuse
Andre Heinecke <aheinecke@intevation.de>
parents: 305
diff changeset
3 /* @file
824ef90a6721 Move is_elevated into common/util.c file for better reuse
Andre Heinecke <aheinecke@intevation.de>
parents: 305
diff changeset
4 @brief Linux implementation of nssstore process control.
824ef90a6721 Move is_elevated into common/util.c file for better reuse
Andre Heinecke <aheinecke@intevation.de>
parents: 305
diff changeset
5 */
824ef90a6721 Move is_elevated into common/util.c file for better reuse
Andre Heinecke <aheinecke@intevation.de>
parents: 305
diff changeset
6
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7 #include <stdbool.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 #include <stdio.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 #include <unistd.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10 #include <sys/types.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
11 #include <sys/wait.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12 #include <string.h>
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 #include <stdlib.h>
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
14 #include <limits.h>
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16 #include "nssstore.h"
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17 #include "logging.h"
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 #include "strhelp.h"
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
20 #define NSS_PROCESS_NAME "mozilla"
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
21
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
22 /**@brief get the current path of the executable
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
23 *
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
24 * Looks up the current executables directory. The caller
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
25 * has to free the return value.
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
26 *
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
27 * The returned value includes the last /
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
28 *
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
29 * @returns the absolute directory of the currently executed executable or NULL
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
30 */
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
31 char *
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
32 get_exe_dir()
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
33 {
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
34 char *retval = NULL,
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
35 *p = NULL,
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
36 buf[PATH_MAX];
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
37 ssize_t ret;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
38 size_t path_len = 0;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
39
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
40 ret = readlink ("/proc/self/exe", buf, PATH_MAX);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
41 if (ret <= 0)
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
42 {
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
43 ERRORPRINTF ("readlink failed\n");
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
44 return NULL;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
45 }
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
46
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
47 buf[ret] = '\0';
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
48
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
49 /* cut off the filename */
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
50 p = strrchr (buf, '/');
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
51 if (p == NULL)
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
52 {
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
53 ERRORPRINTF ("No filename found.\n");
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
54 return NULL;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
55 }
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
56 *(p + 1) = '\0';
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
57
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
58 path_len = strlen (buf);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
59 retval = xmalloc (path_len + 1);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
60 strncpy (retval, buf, path_len);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
61
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
62 return retval;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
63 }
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
64
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65 /**@brief Start the process to install / remove
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 *
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67 * This forks the process and executes the NSS installation
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68 * process. It also writes the Instructions to that process.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
69 *
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 * @param [in] to_install strv of DER encoded certificates to be added.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71 * @param [in] to_remove strv of DER encoded certificates to be remvoed.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
72 * @param [in] uid_t uid of the user to install certificates for.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73 * @param [in] gid_t the gid of the user to install certificates for.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 * @param [in] homedir the homedir of the user.
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 *
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76 * @returns childs pid on success. -1 on failure
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77 */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
78 static int
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
79 start_procces_for_user (char **to_install, char **to_remove,
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
80 uid_t uid, gid_t gid, char *homedir)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
81 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
82 int pipe_fd[2];
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
83 pid_t pid = 0;
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
84 char *argv[2] = {NULL, NULL},
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
85 *envp[2] = {NULL, NULL},
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
86 *inst_dir = NULL;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
87 size_t homedir_len = 0,
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
88 exe_path_len = 0;
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 int ret = -1,
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90 i = 0;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 FILE *stream = NULL;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
92 bool success = false;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
93
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 if (homedir == NULL)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
96 ERRORPRINTF ("Invalid call to start_process_for_user\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
97 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
99
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
100 homedir_len = strlen (homedir);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
101
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102 /* Allocate space for HOME=homedir\0 */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 envp[0] = xmalloc (homedir_len + 6);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 envp[1] = NULL;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 ret = snprintf (envp[0], homedir_len + 6, "HOME=%s", homedir);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
108 if (ret < 0 || (size_t) ret != homedir_len + 5)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
109 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
110 ERRORPRINTF ("Error setting home env variable.\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
111 xfree (envp[0]);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
115 /* Set up the file name of the installer process */
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
116 inst_dir = get_exe_dir();
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
117 if (inst_dir == NULL)
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
118 {
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
119 ERRORPRINTF ("Failed to find installation directory.\n");
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
120 xfree (envp[0]);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
121 return -1;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
122 }
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
123
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
124 exe_path_len = strlen(inst_dir) + strlen(NSS_PROCESS_NAME);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
125 argv[0] = xmalloc (exe_path_len + 1);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
126
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
127 ret = snprintf(argv[0], exe_path_len + 1, "%s%s", inst_dir, NSS_PROCESS_NAME);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
128 if (ret < 0 || (size_t) ret != exe_path_len)
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
129 {
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
130 ERRORPRINTF ("Error setting executable variable.\n");
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
131 xfree (argv[0]);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
132 return -1;
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
133 }
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
135 if (pipe (pipe_fd))
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
136 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
137 ERRORPRINTF ("Failed to create pipe.\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
140
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 pid = fork();
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
142
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
143 if (pid == (pid_t) -1)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
144 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
145 ERRORPRINTF ("Failed to fork child.\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
146 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
147 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
148
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
149 if (pid == (pid_t) 0)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
150 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
151 /* Drop privileges */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
152 if (setuid (uid) || setgid (gid))
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
153 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
154 exit(-1);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
155 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
156
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 close (pipe_fd[1]);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
158 dup2 (pipe_fd[0], 0);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
159 close (pipe_fd[0]);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
160 /* TODO find path based on current executable */
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
161 execve (argv[0], argv, envp);
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
162 exit (127);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
163 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
164
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
165 close (pipe_fd[0]);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
166 stream = fdopen(pipe_fd[1], "w");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
167 if (stream == NULL)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
169 ERRORPRINTF ("Failed to open pipe for writing\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
170 goto done;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 /* Send the instructions */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
174 for (i = 0; to_install && to_install[i]; i++)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
176 if (fprintf (stream, "I:%s\n", to_install[i]) <= 3)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
177 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
178 ERRORPRINTF ("Write failed \n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
179 goto done;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
180 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
181 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
182
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 for (i = 0; to_remove && to_remove[i]; i++)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
185 if (fprintf (stream, "R:%s\n", to_remove[i]) <= 3)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
187 ERRORPRINTF ("Write failed \n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
188 goto done;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
189 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
190 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
191
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
192 success = true;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
193
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
194 done:
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
195 if (stream) {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
196 fclose (stream);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
197 }
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
198 xfree (argv[0]);
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
199 xfree (envp[0]);
383
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
200 close (pipe_fd[0]);
5eb7ee4ee819 Look up executable name based on /proc/self/exe
Andre Heinecke <andre.heinecke@intevation.de>
parents: 321
diff changeset
201 close (pipe_fd[1]);
302
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
202
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
203 if (success)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
204 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
205 return pid;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
206 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
207 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
208 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
209
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
210 int
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
211 write_stores_nss (char **to_install, char **to_remove)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
212 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
213 uid_t my_uid = getuid();
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
214
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
215 if (my_uid != 0)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
216 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
217 /* Running as a user */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
218 char *homedir = getenv ("HOME");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
219 pid_t childprocess = -1; /* Only one child for single user installation */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
220 int status = -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
221 if (!homedir)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
222 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
223 ERRORPRINTF ("Failed to find home directory\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
224 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
225
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
226 childprocess = start_procces_for_user (to_install, to_remove,
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
227 my_uid, getgid(), homedir);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
228
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 if (childprocess == -1)
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 ERRORPRINTF ("Failed to start childprocess!\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
232 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
233 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
234
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
235 childprocess = waitpid (childprocess, &status, 0);
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236 if (childprocess == -1 || !WIFEXITED(status))
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 {
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 ERRORPRINTF ("Waitpid failed.\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 return -1;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 return 0;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 printf ("Installation as root is not yet implemented\n");
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245 /* TODO root parse /etc/passwd for users with a home directory */
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 return 0;
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
247 }
fac7e1b0e558 Add nss store calling function and use it in cinst
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248 #endif

http://wald.intevation.org/projects/trustbridge/