first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[bbaumbach/samba-autobuild/.git] / source3 / rpc_client / cli_atsvc.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 2.1.
4  *  RPC client routines: scheduler service
5  *  Copyright (C) Matthew Chapman                   1999,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
7  *  Copyright (C) Andrew Tridgell              1992-1999.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 extern int DEBUGLEVEL;
27
28 /****************************************************************************
29 add a job to the scheduler
30 ****************************************************************************/
31 BOOL at_add_job(
32                 char *srv_name, AT_JOB_INFO *info, char *command,
33                 uint32 *jobid)
34 {
35         prs_struct rbuf;
36         prs_struct buf; 
37         AT_Q_ADD_JOB q_a;
38         BOOL p = False;
39
40         struct cli_connection *con = NULL;
41
42         if (!cli_connection_init(srv_name, PIPE_ATSVC, &con))
43         {
44                 return False;
45         }
46
47         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
48         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
49
50         /* create and send a MSRPC command with api AT_ADD_JOB */
51
52         DEBUG(4,("Scheduler Add Job\n"));
53
54         /* store the parameters */
55         make_at_q_add_job(&q_a, srv_name, info, command);
56
57         /* turn parameters into data stream */
58         at_io_q_add_job("", &q_a, &buf, 0);
59
60         /* send the data on \PIPE\ */
61         if (rpc_con_pipe_req(con, AT_ADD_JOB, &buf, &rbuf))
62         {
63                 AT_R_ADD_JOB r_a;
64
65                 at_io_r_add_job("", &r_a, &rbuf, 0);
66                 p = rbuf.offset != 0;
67
68                 if (p && r_a.status != 0)
69                 {
70                         /* report error code */
71                         DEBUG(0,("AT_R_ADD_JOB: %s\n", get_nt_error_msg(r_a.status)));
72                         p = False;
73                 }
74
75                 if (p)
76                 {
77                         *jobid = r_a.jobid;
78                 }
79         }
80
81         prs_mem_free(&rbuf);
82         prs_mem_free(&buf );
83
84         cli_connection_unlink(con);
85
86         return p;
87 }
88
89 /****************************************************************************
90 dequeue a job
91 ****************************************************************************/
92 BOOL at_del_job( char *srv_name, uint32 min_jobid, uint32 max_jobid)
93 {
94         prs_struct rbuf;
95         prs_struct buf; 
96         AT_Q_DEL_JOB q_d;
97         BOOL p = False;
98
99         struct cli_connection *con = NULL;
100
101         if (!cli_connection_init(srv_name, PIPE_ATSVC, &con))
102         {
103                 return False;
104         }
105
106         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
107         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
108
109         /* create and send a MSRPC command with api AT_DEL_JOB */
110
111         DEBUG(4,("Scheduler Delete Job\n"));
112
113         /* store the parameters */
114         make_at_q_del_job(&q_d, srv_name, min_jobid, max_jobid);
115
116         /* turn parameters into data stream */
117         at_io_q_del_job("", &q_d, &buf, 0);
118
119         /* send the data on \PIPE\ */
120         if (rpc_con_pipe_req(con, AT_DEL_JOB, &buf, &rbuf))
121         {
122                 AT_R_DEL_JOB r_d;
123
124                 at_io_r_del_job("", &r_d, &rbuf, 0);
125                 p = rbuf.offset != 0;
126
127                 if (p && r_d.status != 0)
128                 {
129                         /* report error code */
130                         DEBUG(0,("AT_R_DEL_JOB: %s\n", get_nt_error_msg(r_d.status)));
131                         p = False;
132                 }
133         }
134
135         prs_mem_free(&rbuf);
136         prs_mem_free(&buf );
137
138         cli_connection_unlink(con);
139
140         return p;
141 }
142
143 /****************************************************************************
144 enumerate scheduled jobs
145 ****************************************************************************/
146 BOOL at_enum_jobs( char *srv_name, uint32 *num_jobs,
147                   AT_ENUM_INFO *jobs, char ***commands)
148 {
149         prs_struct rbuf;
150         prs_struct buf; 
151         AT_Q_ENUM_JOBS q_e;
152         BOOL p = False;
153
154         struct cli_connection *con = NULL;
155
156         if (!cli_connection_init(srv_name, PIPE_ATSVC, &con))
157         {
158                 return False;
159         }
160
161         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
162         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
163
164         /* create and send a MSRPC command with api AT_DEL_JOB */
165
166         DEBUG(4,("Scheduler Enumerate Jobs\n"));
167
168         /* store the parameters */
169         make_at_q_enum_jobs(&q_e, srv_name);
170
171         /* turn parameters into data stream */
172         at_io_q_enum_jobs("", &q_e, &buf, 0);
173
174         /* send the data on \PIPE\ */
175         if (rpc_con_pipe_req(con, AT_ENUM_JOBS, &buf, &rbuf))
176         {
177                 AT_R_ENUM_JOBS r_e;
178
179                 at_io_r_enum_jobs("", &r_e, &rbuf, 0);
180                 p = rbuf.offset != 0;
181
182                 if (p && r_e.status != 0)
183                 {
184                         /* report error code */
185                         DEBUG(0,("AT_R_ENUM_JOBS: %s\n", get_nt_error_msg(r_e.status)));
186                         p = False;
187                 }
188
189                 if (p)
190                 {
191                         int i;
192
193                         *num_jobs = 0;
194                         memcpy(jobs, &r_e.info, r_e.num_entries * sizeof(AT_ENUM_INFO));
195
196                         for (i = 0; i < r_e.num_entries; i++)
197                         {
198                                 fstring cmd;
199                                 unistr2_to_ascii(cmd, &r_e.command[i], sizeof(cmd));
200                                 add_chars_to_array(num_jobs, commands, cmd);
201                         }
202                         if ((*num_jobs) != r_e.num_entries)
203                         {
204                                 p = False;
205                         }
206                 }
207         }
208
209         prs_mem_free(&rbuf);
210         prs_mem_free(&buf );
211
212         cli_connection_unlink(con);
213
214         return p;
215 }
216
217 /****************************************************************************
218 query job information
219 ****************************************************************************/
220 BOOL at_query_job(char *srv_name,
221                   uint32 jobid, AT_JOB_INFO *job, fstring command)
222 {
223         prs_struct rbuf;
224         prs_struct buf; 
225         AT_Q_QUERY_JOB q_q;
226         BOOL p = False;
227
228         struct cli_connection *con = NULL;
229
230         if (!cli_connection_init(srv_name, PIPE_ATSVC, &con))
231         {
232                 return False;
233         }
234
235         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
236         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
237
238         /* create and send a MSRPC command with api AT_QUERY_JOB */
239
240         DEBUG(4,("Scheduler Query Job\n"));
241
242         /* store the parameters */
243         make_at_q_query_job(&q_q, srv_name, jobid);
244
245         /* turn parameters into data stream */
246         at_io_q_query_job("", &q_q, &buf, 0);
247
248         /* send the data on \PIPE\ */
249         if (rpc_con_pipe_req(con, AT_QUERY_JOB, &buf, &rbuf))
250         {
251                 AT_R_QUERY_JOB r_q;
252
253                 at_io_r_query_job("", &r_q, &rbuf, 0);
254                 p = rbuf.offset != 0;
255
256                 if (p && r_q.status != 0)
257                 {
258                         /* report error code */
259                         DEBUG(0,("AT_R_QUERY_JOB: %s\n", get_nt_error_msg(r_q.status)));
260                         p = False;
261                 }
262
263                 if (p)
264                 {
265                         memcpy(job, &r_q.info, sizeof(AT_JOB_INFO));
266                         unistr2_to_ascii(command, &r_q.command,
267                                          sizeof(fstring)-1);
268                 }
269         }
270
271         prs_mem_free(&rbuf);
272         prs_mem_free(&buf );
273
274         cli_connection_unlink(con);
275
276         return p;
277 }