- don't use env variables for passwords and usernames (yeah!)
[ira/wip.git] / source3 / smbwrapper / shared.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    SMB wrapper functions - shared variables
5    Copyright (C) Andrew Tridgell 1998
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
24 extern int DEBUGLEVEL;
25
26 static int shared_fd;
27 static char *variables;
28 static int shared_size;
29
30 /***************************************************** 
31 setup the shared area 
32 *******************************************************/
33 void smbw_setup_shared(void)
34 {
35         int fd;
36         pstring s, name;
37
38         slprintf(s,sizeof(s)-1, "%s/msg.XXXXXX",tmpdir());
39
40         fstrcpy(name,(char *)mktemp(s));
41
42         /* note zero permissions! don't change this */
43         fd = open(name,O_RDWR|O_CREAT|O_TRUNC|O_EXCL,0); 
44         if (fd == -1) goto failed;
45
46         shared_fd = set_maxfiles(SMBW_MAX_OPEN);
47         
48         while (shared_fd && dup2(fd, shared_fd) != shared_fd) shared_fd--;
49
50         if (shared_fd == 0) goto failed;
51
52         close(fd);
53
54         DEBUG(4,("created shared_fd=%d\n", shared_fd));
55
56         slprintf(s,sizeof(s)-1,"%d", shared_fd);
57
58         setenv("SMBW_HANDLE", s, 1);
59
60         return;
61
62  failed:
63         perror("Failed to setup shared variable area ");
64         exit(1);
65 }
66
67
68 /***************************************************** 
69 lock the shared variable area
70 *******************************************************/
71 static void lockit(void)
72 {
73         if (shared_fd == 0) {
74                 char *p = getenv("SMBW_HANDLE");
75                 if (!p) {
76                         DEBUG(0,("ERROR: can't get smbw shared handle\n"));
77                         exit(1);
78                 }
79                 shared_fd = atoi(p);
80         }
81         if (fcntl_lock(shared_fd,SMB_F_SETLKW,0,1,F_WRLCK)==False) {
82                 DEBUG(0,("ERROR: can't get smbw shared lock\n"));
83                 exit(1);
84         }
85 }
86
87 /***************************************************** 
88 unlock the shared variable area
89 *******************************************************/
90 static void unlockit(void)
91 {
92         fcntl_lock(shared_fd,SMB_F_SETLK,0,1,F_UNLCK);
93 }
94
95
96 /***************************************************** 
97 get a variable from the shared area
98 *******************************************************/
99 char *smbw_getshared(const char *name)
100 {
101         int i;
102         struct stat st;
103
104         lockit();
105
106         /* maybe the area has changed */
107         if (fstat(shared_fd, &st)) goto failed;
108
109         if (st.st_size != shared_size) {
110                 variables = (char *)Realloc(variables, st.st_size);
111                 if (!variables) goto failed;
112                 shared_size = st.st_size;
113                 lseek(shared_fd, 0, SEEK_SET);
114                 if (read(shared_fd, variables, shared_size) != shared_size) {
115                         goto failed;
116                 }
117         }
118
119         unlockit();
120
121         i=0;
122         while (i < shared_size) {
123                 int len;
124                 char *n, *v;
125
126                 n = &variables[i];
127                 i += strlen(n)+1;
128                 v = &variables[i];
129                 i += strlen(v)+1;
130
131                 if (strcmp(name,n)) {
132                         continue;
133                 }
134                 return v;
135         }
136
137         return NULL;
138
139  failed:
140         DEBUG(0,("smbw: shared variables corrupt (%s)\n", strerror(errno)));
141         exit(1);
142 }
143
144
145
146 /***************************************************** 
147 set a variable in the shared area
148 *******************************************************/
149 void smbw_setshared(const char *name, const char *val)
150 {
151         int len;
152
153         /* we don't allow variable overwrite */
154         if (smbw_getshared(name)) return;
155
156         lockit();
157
158         len = strlen(name) + strlen(val) + 2;
159
160         variables = (char *)Realloc(variables, shared_size + len);
161
162         if (!variables) {
163                 DEBUG(0,("out of memory in smbw_setshared\n"));
164                 exit(1);
165         }
166
167         pstrcpy(&variables[shared_size], name);
168         shared_size += strlen(name)+1;
169         pstrcpy(&variables[shared_size], val);
170         shared_size += strlen(val)+1;
171
172         lseek(shared_fd, 0, SEEK_SET);
173         if (write(shared_fd, variables, shared_size) != shared_size) {
174                 DEBUG(0,("smbw_setshared failed (%s)\n", strerror(errno)));
175                 exit(1);
176         }
177
178         unlockit();
179 }