summaryrefslogtreecommitdiff
path: root/source3/services/svc_rcinit.c
blob: 5801d076c496ebc22d6b6affedeee68d7b3b53ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

/* 
 *  Unix SMB/CIFS implementation.
 *  Service Control API Implementation
 *  Copyright (C) Gerald Carter                   2005.
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"

/* Implementation for LSB compliant init scripts */

/*******************************************************************************
 Get the services information  by reading and parsing the shell scripts. These 
 are symbolically linked into the  SVCCTL_SCRIPT_DIR  directory.

 Get the names of the services/scripts to read from the smb.conf file.
*******************************************************************************/

BOOL get_LSB_data(char *fname,Service_info *si )
{
	pstring initdfile;
	char mybuffer[256];
	const char *tokenptr;
	char **qlines;
	int fd = -1;
	int nlines, *numlines,i,in_section,in_description;
	
	pstrcpy(si->servicename,"");
	pstrcpy(si->servicetype,"EXTERNAL");
	pstrcpy(si->filename,fname);
	pstrcpy(si->provides,"");
	pstrcpy(si->dependencies,"");
	pstrcpy(si->shouldstart,"");
	pstrcpy(si->shouldstop,"");
	pstrcpy(si->requiredstart,"");
	pstrcpy(si->requiredstop,"");
	pstrcpy(si->description,"");
	pstrcpy(si->shortdescription,"");

	numlines = &nlines;
	in_section = 0;
	in_description = 0;

   
	if( !fname || !*fname ) {
		DEBUG(0, ("Must define an \"LSB-style init file\" to read.\n"));
		return False;
	}
	pstrcpy(initdfile,dyn_LIBDIR);
	pstrcat(initdfile,SVCCTL_SCRIPT_DIR);
	pstrcat(initdfile,fname);

	/* TODO  - should check to see if the file that we're trying to open is 
	   actually a script. If it's NOT, we should do something like warn, 
	   and not continue to try to find info we're looking for */

	DEBUG(10, ("Opening [%s]\n", initdfile));
	fd = -1;
	fd = open(initdfile,O_RDONLY);
	*numlines = 0;

	if (fd == -1) {
		DEBUG(10, ("Couldn't open [%s]\n", initdfile));
		return False;
	}

	qlines = fd_lines_load(fd, numlines);
	DEBUGADD(10, ("Lines returned = [%d]\n", *numlines));
	close(fd);
    

	if (*numlines) {
	
		for(i = 0; i < *numlines; i++) {

			DEBUGADD(10, ("Line[%d] = %s\n", i, qlines[i]));
			if (!in_section && (0==strwicmp("### BEGIN INIT INFO", qlines[i]))) {
				/* we now can look for params */
				DEBUGADD(10, ("Configuration information starts on line = [%d]\n", i));
				in_section = 1;

			} else if (in_section && (0==strwicmp("### END INIT INFO", qlines[i]))) {
				DEBUGADD(10, ("Configuration information ends on line = [%d]\n", i));
				DEBUGADD(10, ("Description is [%s]\n", si->description));
				in_description = 0;
				in_section = 0;
				break;
			} else if (in_section) {
				tokenptr = qlines[i];
				if (in_description) {
					DEBUGADD(10, ("Processing DESCRIPTION [%d]\n", *tokenptr));
					if (tokenptr && (*tokenptr=='#') && (*(tokenptr+1)=='\t')) {
						DEBUGADD(10, ("Adding to DESCRIPTION [%d]\n", *tokenptr));
						pstrcat(si->description," ");
						pstrcat(si->description,tokenptr+2);
						continue;
					}
					in_description = 0;
					DEBUGADD(10, ("Not a description!\n"));
				}
				if (!next_token(&tokenptr,mybuffer," \t",sizeof(mybuffer))) {
					DEBUGADD(10, ("Invalid line [%d]\n", i));
					break; /* bad line? */
				}
				if (0 != strncmp(mybuffer,"#",1)) {
					DEBUGADD(10, ("Invalid line [%d], is %s\n", i,mybuffer));
					break;
				}
				if (!next_token(&tokenptr,mybuffer," \t",sizeof(mybuffer))) {
					DEBUGADD(10, ("Invalid token on line [%d]\n", i));
					break; /* bad line? */
				}	      
				DEBUGADD(10, ("Keyword is  [%s]\n", mybuffer));
				if (0==strwicmp(mybuffer,"Description:")) {
					while (tokenptr && *tokenptr && (strchr(" \t",*tokenptr))) { 
						tokenptr++; 
					}
					pstrcpy(si->description,tokenptr);
					DEBUGADD(10, ("FOUND DESCRIPTION! Data is [%s]\n", tokenptr));
					in_description = 1;
				} else {
					while (tokenptr && *tokenptr && (strchr(" \t",*tokenptr))) { 
						tokenptr++; 
					}
					DEBUGADD(10, ("Data is [%s]\n", tokenptr));
					in_description = 0;

					/* save certain keywords, don't save others */
					if (0==strwicmp(mybuffer, "Provides:")) {
						pstrcpy(si->provides,tokenptr);
						pstrcpy(si->servicename,tokenptr);
					}

					if (0==strwicmp(mybuffer, "Short-Description:")) {
						pstrcpy(si->shortdescription,tokenptr);
					}

					if (0==strwicmp(mybuffer, "Required-start:")) {
						pstrcpy(si->requiredstart,tokenptr);
						pstrcpy(si->dependencies,tokenptr);
					}

					if (0==strwicmp(mybuffer, "Should-start:")) {
						pstrcpy(si->shouldstart,tokenptr);
					}
				}
			}
		}

		file_lines_free(qlines);
			return True;
	}

	return False;
}

/*********************************************************************
*********************************************************************/

static WERROR rcinit_stop( const char *service, SERVICE_STATUS *status )
{
	pstring command;
	int ret, fd;
	
	pstr_sprintf( command, "%s/%s/%s stop", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, service );
	
	/* we've already performed the access check when the service was opened */
	
	become_root();
	ret = smbrun( command , &fd );
	unbecome_root();
	
	DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
	close(fd);
	
	ZERO_STRUCTP( status );
	status->type = 0x0020;
	status->state = (ret == 0 ) ? 0x0001 : 0x0004;
	status->controls_accepted = 0x0005;

	return ( ret == 0 ) ? WERR_OK : WERR_ACCESS_DENIED;
}

/*********************************************************************
*********************************************************************/

static WERROR rcinit_start( const char *service )
{
	pstring command;
	int ret, fd;
	
	pstr_sprintf( command, "%s/%s/%s start", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, service );
	
	/* we've already performed the access check when the service was opened */
	
	become_root();
	ret = smbrun( command , &fd );
	unbecome_root();
	
	DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
	close(fd);	

	return ( ret == 0 ) ? WERR_OK : WERR_ACCESS_DENIED;
}

/*********************************************************************
*********************************************************************/

static WERROR rcinit_status( const char *service, SERVICE_STATUS *status )
{
	pstring command;
	int ret, fd;
	
	pstr_sprintf( command, "%s/%s/%s status", dyn_LIBDIR, SVCCTL_SCRIPT_DIR, service );
	
	/* we've already performed the access check when the service was opened */
	/* assume as return code of 0 means that the service is ok.  Anything else
	   is STOPPED */
	
	become_root();
	ret = smbrun( command , &fd );
	unbecome_root();
	
	DEBUGADD(5, ("rcinit_start: [%s] returned [%d]\n", command, ret));
	close(fd);
	
	ZERO_STRUCTP( status );
	status->type = 0x0020;
	status->state = (ret == 0 ) ? 0x0004 : 0x0001;
	status->controls_accepted = 0x0005;

	return WERR_OK;
}

/*********************************************************************
*********************************************************************/

/* struct for svcctl control to manipulate rcinit service */

SERVICE_CONTROL_OPS rcinit_svc_ops = {
	rcinit_stop,
	rcinit_start,
	rcinit_status
};