a1ae476883910d4e69ae09edf8d19a91bbf21aa0
[metze/samba/wip.git] / source3 / nsswitch / 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "winbindd.h"
24 #include "smb_launchd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /* Open the winbindd socket */
30
31 static int _winbindd_socket = -1;
32 static int _winbindd_priv_socket = -1;
33 static BOOL unlink_winbindd_socket = True;
34
35 static int open_winbindd_socket(void)
36 {
37         if (_winbindd_socket == -1) {
38                 _winbindd_socket = create_pipe_sock(
39                         WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME, 0755);
40                 DEBUG(10, ("open_winbindd_socket: opened socket fd %d\n",
41                            _winbindd_socket));
42         }
43
44         return _winbindd_socket;
45 }
46
47 static int open_winbindd_priv_socket(void)
48 {
49         if (_winbindd_priv_socket == -1) {
50                 _winbindd_priv_socket = create_pipe_sock(
51                         get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
52                 DEBUG(10, ("open_winbindd_priv_socket: opened socket fd %d\n",
53                            _winbindd_priv_socket));
54         }
55
56         return _winbindd_priv_socket;
57 }
58
59 /* Close the winbindd socket */
60
61 static void close_winbindd_socket(void)
62 {
63         if (_winbindd_socket != -1) {
64                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
65                            _winbindd_socket));
66                 close(_winbindd_socket);
67                 _winbindd_socket = -1;
68         }
69         if (_winbindd_priv_socket != -1) {
70                 DEBUG(10, ("close_winbindd_socket: closing socket fd %d\n",
71                            _winbindd_priv_socket));
72                 close(_winbindd_priv_socket);
73                 _winbindd_priv_socket = -1;
74         }
75 }
76
77 BOOL winbindd_init_sockets(int *public_sock, int *priv_sock,
78                                 int *idle_timeout_sec)
79 {
80         struct smb_launch_info linfo;
81
82         if (smb_launchd_checkin_names(&linfo, "WinbindPublicPipe",
83                     "WinbindPrivilegedPipe", NULL)) {
84                 if (linfo.num_sockets != 2) {
85                         DEBUG(0, ("invalid launchd configuration, "
86                                 "expected 2 sockets but got %d\n",
87                                 linfo.num_sockets));
88                         return False;
89                 }
90
91                 *public_sock = _winbindd_socket = linfo.socket_list[0];
92                 *priv_sock = _winbindd_priv_socket = linfo.socket_list[1];
93                 *idle_timeout_sec = linfo.idle_timeout_secs;
94
95                 unlink_winbindd_socket = False;
96
97                 smb_launchd_checkout(&linfo);
98                 return True;
99         } else {
100                 *public_sock = open_winbindd_socket();
101                 *priv_sock = open_winbindd_priv_socket();
102                 *idle_timeout_sec = -1;
103
104                 if (*public_sock == -1 || *priv_sock == -1) {
105                         DEBUG(0, ("failed to open winbindd pipes: %s\n",
106                             errno ? strerror(errno) : "unknown error"));
107                         return False;
108                 }
109
110                 return True;
111         }
112 }
113
114 void winbindd_release_sockets(void)
115 {
116         pstring path;
117
118         close_winbindd_socket();
119
120         /* Remove socket file */
121         if (unlink_winbindd_socket) {
122                 pstr_sprintf(path, "%s/%s",
123                          WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
124                 unlink(path);
125         }
126 }
127