r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[samba.git] / source / winbindd / winbindd_sockinit.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Tim Potter 2000-2001
4    Copyright (C) 2001 by Martin Pool <mbp@samba.org>
5    Copyright (C) James Peach 2007
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "smb_launchd.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
27
28 /* Open the winbindd socket */
29
30 static int _winbindd_socket = -1;
31 static int _winbindd_priv_socket = -1;
32 static BOOL unlink_winbindd_socket = True;
33
34 static int open_winbindd_socket(void)
35 {
36         if (_winbindd_socket == -1) {
37                 _winbindd_socket = create_pipe_sock(
38                         get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME, 0755);
39                 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
40                            _winbindd_socket));
41         }
42
43         return _winbindd_socket;
44 }
45
46 static int open_winbindd_priv_socket(void)
47 {
48         if (_winbindd_priv_socket == -1) {
49                 _winbindd_priv_socket = create_pipe_sock(
50                         get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
51                 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
52                            _winbindd_priv_socket));
53         }
54
55         return _winbindd_priv_socket;
56 }
57
58 /* Close the winbindd socket */
59
60 static void close_winbindd_socket(void)
61 {
62         if (_winbindd_socket != -1) {
63                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
64                            _winbindd_socket));
65                 close(_winbindd_socket);
66                 _winbindd_socket = -1;
67         }
68         if (_winbindd_priv_socket != -1) {
69                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
70                            _winbindd_priv_socket));
71                 close(_winbindd_priv_socket);
72                 _winbindd_priv_socket = -1;
73         }
74 }
75
76 BOOL winbindd_init_sockets(int *public_sock, int *priv_sock,
77                                 int *idle_timeout_sec)
78 {
79         struct smb_launch_info linfo;
80
81         if (smb_launchd_checkin_names(&linfo, "WinbindPublicPipe",
82                     "WinbindPrivilegedPipe", NULL)) {
83                 if (linfo.num_sockets != 2) {
84                         DEBUG(0, ("invalid launchd configuration, "
85                                 "expected 2 sockets but got %d\n",
86                                 linfo.num_sockets));
87                         return False;
88                 }
89
90                 *public_sock = _winbindd_socket = linfo.socket_list[0];
91                 *priv_sock = _winbindd_priv_socket = linfo.socket_list[1];
92                 *idle_timeout_sec = linfo.idle_timeout_secs;
93
94                 unlink_winbindd_socket = False;
95
96                 smb_launchd_checkout(&linfo);
97                 return True;
98         } else {
99                 *public_sock = open_winbindd_socket();
100                 *priv_sock = open_winbindd_priv_socket();
101                 *idle_timeout_sec = -1;
102
103                 if (*public_sock == -1 || *priv_sock == -1) {
104                         DEBUG(0, ("failed to open winbindd pipes: %s\n",
105                             errno ? strerror(errno) : "unknown error"));
106                         return False;
107                 }
108
109                 return True;
110         }
111 }
112
113 void winbindd_release_sockets(void)
114 {
115         pstring path;
116
117         close_winbindd_socket();
118
119         /* Remove socket file */
120         if (unlink_winbindd_socket) {
121                 pstr_sprintf(path, "%s/%s",
122                          get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME);
123                 unlink(path);
124         }
125 }
126