previous commit added an abstraction function that didn't even have
[ira/wip.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(struct cli_state *cli, uint16 fnum, 
32                 char *server_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         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
41         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
42
43         /* create and send a MSRPC command with api AT_ADD_JOB */
44
45         DEBUG(4,("Scheduler Add Job\n"));
46
47         /* store the parameters */
48         make_at_q_add_job(&q_a, server_name, info, command);
49
50         /* turn parameters into data stream */
51         at_io_q_add_job("", &q_a, &buf, 0);
52
53         /* send the data on \PIPE\ */
54         if (rpc_api_pipe_req(cli, fnum, AT_ADD_JOB, &buf, &rbuf))
55         {
56                 AT_R_ADD_JOB r_a;
57
58                 at_io_r_add_job("", &r_a, &rbuf, 0);
59                 p = rbuf.offset != 0;
60
61                 if (p && r_a.status != 0)
62                 {
63                         /* report error code */
64                         DEBUG(0,("AT_R_ADD_JOB: %s\n", get_nt_error_msg(r_a.status)));
65                         p = False;
66                 }
67
68                 if (p)
69                 {
70                         *jobid = r_a.jobid;
71                 }
72         }
73
74         prs_mem_free(&rbuf);
75         prs_mem_free(&buf );
76
77         return p;
78 }
79
80 /****************************************************************************
81 dequeue a job
82 ****************************************************************************/
83 BOOL at_del_job(struct cli_state *cli, uint16 fnum, 
84                 char *server_name, uint32 min_jobid, uint32 max_jobid)
85 {
86         prs_struct rbuf;
87         prs_struct buf; 
88         AT_Q_DEL_JOB q_d;
89         BOOL p = False;
90
91         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
92         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
93
94         /* create and send a MSRPC command with api AT_DEL_JOB */
95
96         DEBUG(4,("Scheduler Delete Job\n"));
97
98         /* store the parameters */
99         make_at_q_del_job(&q_d, server_name, min_jobid, max_jobid);
100
101         /* turn parameters into data stream */
102         at_io_q_del_job("", &q_d, &buf, 0);
103
104         /* send the data on \PIPE\ */
105         if (rpc_api_pipe_req(cli, fnum, AT_DEL_JOB, &buf, &rbuf))
106         {
107                 AT_R_DEL_JOB r_d;
108
109                 at_io_r_del_job("", &r_d, &rbuf, 0);
110                 p = rbuf.offset != 0;
111
112                 if (p && r_d.status != 0)
113                 {
114                         /* report error code */
115                         DEBUG(0,("AT_R_DEL_JOB: %s\n", get_nt_error_msg(r_d.status)));
116                         p = False;
117                 }
118         }
119
120         prs_mem_free(&rbuf);
121         prs_mem_free(&buf );
122
123         return p;
124 }
125
126 /****************************************************************************
127 enumerate scheduled jobs
128 ****************************************************************************/
129 BOOL at_enum_jobs(struct cli_state *cli, uint16 fnum, 
130                   char *server_name, uint32 *num_jobs,
131                   AT_ENUM_INFO *jobs, fstring *commands)
132 {
133         prs_struct rbuf;
134         prs_struct buf; 
135         AT_Q_ENUM_JOBS q_e;
136         BOOL p = False;
137
138         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
139         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
140
141         /* create and send a MSRPC command with api AT_DEL_JOB */
142
143         DEBUG(4,("Scheduler Enumerate Jobs\n"));
144
145         /* store the parameters */
146         make_at_q_enum_jobs(&q_e, server_name);
147
148         /* turn parameters into data stream */
149         at_io_q_enum_jobs("", &q_e, &buf, 0);
150
151         /* send the data on \PIPE\ */
152         if (rpc_api_pipe_req(cli, fnum, AT_ENUM_JOBS, &buf, &rbuf))
153         {
154                 AT_R_ENUM_JOBS r_e;
155
156                 at_io_r_enum_jobs("", &r_e, &rbuf, 0);
157                 p = rbuf.offset != 0;
158
159                 if (p && r_e.status != 0)
160                 {
161                         /* report error code */
162                         DEBUG(0,("AT_R_ENUM_JOBS: %s\n", get_nt_error_msg(r_e.status)));
163                         p = False;
164                 }
165
166                 if (p)
167                 {
168                         int i;
169
170                         *num_jobs = r_e.num_entries;
171                         memcpy(jobs, &r_e.info, r_e.num_entries * sizeof(AT_ENUM_INFO));
172
173                         for (i = 0; i < r_e.num_entries; i++)
174                         {
175                                 unistr2_to_ascii(commands[i], &r_e.command[i],
176                                                  sizeof(commands[i]));
177                         }
178                 }
179         }
180
181         prs_mem_free(&rbuf);
182         prs_mem_free(&buf );
183
184         return p;
185 }
186
187 /****************************************************************************
188 query job information
189 ****************************************************************************/
190 BOOL at_query_job(struct cli_state *cli, uint16 fnum, char *server_name,
191                   uint32 jobid, AT_JOB_INFO *job, fstring command)
192 {
193         prs_struct rbuf;
194         prs_struct buf; 
195         AT_Q_QUERY_JOB q_q;
196         BOOL p = False;
197
198         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
199         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
200
201         /* create and send a MSRPC command with api AT_QUERY_JOB */
202
203         DEBUG(4,("Scheduler Query Job\n"));
204
205         /* store the parameters */
206         make_at_q_query_job(&q_q, server_name, jobid);
207
208         /* turn parameters into data stream */
209         at_io_q_query_job("", &q_q, &buf, 0);
210
211         /* send the data on \PIPE\ */
212         if (rpc_api_pipe_req(cli, fnum, AT_QUERY_JOB, &buf, &rbuf))
213         {
214                 AT_R_QUERY_JOB r_q;
215
216                 at_io_r_query_job("", &r_q, &rbuf, 0);
217                 p = rbuf.offset != 0;
218
219                 if (p && r_q.status != 0)
220                 {
221                         /* report error code */
222                         DEBUG(0,("AT_R_QUERY_JOB: %s\n", get_nt_error_msg(r_q.status)));
223                         p = False;
224                 }
225
226                 if (p)
227                 {
228                         memcpy(job, &r_q.info, sizeof(AT_JOB_INFO));
229                         unistr2_to_ascii(command, &r_q.command,
230                                          sizeof(fstring)-1);
231                 }
232         }
233
234         prs_mem_free(&rbuf);
235         prs_mem_free(&buf );
236
237         return p;
238 }