r25598: Add missing become_root/unbecome_root around calls of add_aliases.
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_sockinit.c
blob886b67fb470df9dd54c114d76574c5a6d555356d
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
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.
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.
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/>.
21 #include "includes.h"
22 #include "winbindd.h"
23 #include "smb_launchd.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
28 /* Open the winbindd socket */
30 static int _winbindd_socket = -1;
31 static int _winbindd_priv_socket = -1;
32 static BOOL unlink_winbindd_socket = True;
34 static int open_winbindd_socket(void)
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));
43 return _winbindd_socket;
46 static int open_winbindd_priv_socket(void)
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));
55 return _winbindd_priv_socket;
58 /* Close the winbindd socket */
60 static void close_winbindd_socket(void)
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;
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;
76 BOOL winbindd_init_sockets(int *public_sock, int *priv_sock,
77 int *idle_timeout_sec)
79 struct smb_launch_info linfo;
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;
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;
94 unlink_winbindd_socket = False;
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;
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;
109 return True;
113 void winbindd_release_sockets(void)
115 pstring path;
117 close_winbindd_socket();
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);