regtask.c 14.2 KB
Newer Older
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
/*
 * Copyright 2014 Dmitry Timoshkov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "objbase.h"
#include "taskschd.h"
28
#include "schrpc.h"
29 30 31 32 33 34 35 36 37 38 39
#include "taskschd_private.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(taskschd);

typedef struct
{
    IRegisteredTask IRegisteredTask_iface;
    LONG ref;
    WCHAR *path;
40
    ITaskDefinition *taskdef;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
} RegisteredTask;

static inline RegisteredTask *impl_from_IRegisteredTask(IRegisteredTask *iface)
{
    return CONTAINING_RECORD(iface, RegisteredTask, IRegisteredTask_iface);
}

static ULONG WINAPI regtask_AddRef(IRegisteredTask *iface)
{
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);
    return InterlockedIncrement(&regtask->ref);
}

static ULONG WINAPI regtask_Release(IRegisteredTask *iface)
{
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);
    LONG ref = InterlockedDecrement(&regtask->ref);

    if (!ref)
    {
        TRACE("destroying %p\n", iface);
62
        ITaskDefinition_Release(regtask->taskdef);
63 64
        free(regtask->path);
        free(regtask);
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
    }

    return ref;
}

static HRESULT WINAPI regtask_QueryInterface(IRegisteredTask *iface, REFIID riid, void **obj)
{
    if (!riid || !obj) return E_INVALIDARG;

    TRACE("%p,%s,%p\n", iface, debugstr_guid(riid), obj);

    if (IsEqualGUID(riid, &IID_IRegisteredTask) ||
        IsEqualGUID(riid, &IID_IDispatch) ||
        IsEqualGUID(riid, &IID_IUnknown))
    {
        IRegisteredTask_AddRef(iface);
        *obj = iface;
        return S_OK;
    }

    FIXME("interface %s is not implemented\n", debugstr_guid(riid));
    *obj = NULL;
    return E_NOINTERFACE;
}

static HRESULT WINAPI regtask_GetTypeInfoCount(IRegisteredTask *iface, UINT *count)
{
    FIXME("%p,%p: stub\n", iface, count);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_GetTypeInfo(IRegisteredTask *iface, UINT index, LCID lcid, ITypeInfo **info)
{
98
    FIXME("%p,%u,%lu,%p: stub\n", iface, index, lcid, info);
99 100 101 102 103 104
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_GetIDsOfNames(IRegisteredTask *iface, REFIID riid, LPOLESTR *names,
                                            UINT count, LCID lcid, DISPID *dispid)
{
105
    FIXME("%p,%s,%p,%u,%lu,%p: stub\n", iface, debugstr_guid(riid), names, count, lcid, dispid);
106 107 108 109 110 111
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_Invoke(IRegisteredTask *iface, DISPID dispid, REFIID riid, LCID lcid, WORD flags,
                                     DISPPARAMS *params, VARIANT *result, EXCEPINFO *excepinfo, UINT *argerr)
{
112
    FIXME("%p,%ld,%s,%04lx,%04x,%p,%p,%p,%p: stub\n", iface, dispid, debugstr_guid(riid), lcid, flags,
113 114 115 116 117 118
          params, result, excepinfo, argerr);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_get_Name(IRegisteredTask *iface, BSTR *name)
{
119 120 121 122 123 124 125
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);
    const WCHAR *p_name;

    TRACE("%p,%p\n", iface, name);

    if (!name) return E_POINTER;

126
    p_name = wcsrchr(regtask->path, '\\');
127 128 129 130 131 132 133 134 135
    if (!p_name)
        p_name = regtask->path;
    else
        if (p_name[1] != 0) p_name++;

    *name = SysAllocString(p_name);
    if (!*name) return E_OUTOFMEMORY;

    return S_OK;
136 137 138 139
}

static HRESULT WINAPI regtask_get_Path(IRegisteredTask *iface, BSTR *path)
{
140 141 142 143 144 145 146 147 148 149
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);

    TRACE("%p,%p\n", iface, path);

    if (!path) return E_POINTER;

    *path = SysAllocString(regtask->path);
    if (!*path) return E_OUTOFMEMORY;

    return S_OK;
150 151 152 153
}

static HRESULT WINAPI regtask_get_State(IRegisteredTask *iface, TASK_STATE *state)
{
154 155 156 157 158
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);
    DWORD enabled;

    TRACE("%p,%p\n", iface, state);

159 160
    if (!state) return E_POINTER;

161
    return SchRpcGetTaskInfo(regtask->path, SCH_FLAG_STATE, &enabled, (DWORD *)state);
162 163
}

164
static HRESULT WINAPI regtask_get_Enabled(IRegisteredTask *iface, VARIANT_BOOL *v_enabled)
165
{
166 167 168 169 170 171
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);
    DWORD enabled, state;
    HRESULT hr;

    TRACE("%p,%p\n", iface, v_enabled);

172 173
    if (!v_enabled) return E_POINTER;

174 175 176 177
    hr = SchRpcGetTaskInfo(regtask->path, 0, &enabled, &state);
    if (hr == S_OK)
        *v_enabled = enabled ? VARIANT_TRUE : VARIANT_FALSE;
    return hr;
178 179 180 181 182
}

static HRESULT WINAPI regtask_put_Enabled(IRegisteredTask *iface, VARIANT_BOOL enabled)
{
    FIXME("%p,%d: stub\n", iface, enabled);
183
    return S_OK;
184 185 186 187 188 189 190 191 192 193 194
}

static HRESULT WINAPI regtask_Run(IRegisteredTask *iface, VARIANT params, IRunningTask **task)
{
    FIXME("%p,%s,%p: stub\n", iface, debugstr_variant(&params), task);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_RunEx(IRegisteredTask *iface, VARIANT params, LONG flags,
                                    LONG session_id, BSTR user, IRunningTask **task)
{
195
    FIXME("%p,%s,%lx,%lx,%s,%p: stub\n", iface, debugstr_variant(&params), flags, session_id, debugstr_w(user), task);
196 197 198 199 200
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_GetInstances(IRegisteredTask *iface, LONG flags, IRunningTaskCollection **tasks)
{
201
    FIXME("%p,%lx,%p: stub\n", iface, flags, tasks);
202 203 204 205 206 207
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_get_LastRunTime(IRegisteredTask *iface, DATE *date)
{
    FIXME("%p,%p: stub\n", iface, date);
208
    return SCHED_S_TASK_HAS_NOT_RUN;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

static HRESULT WINAPI regtask_get_LastTaskResult(IRegisteredTask *iface, LONG *result)
{
    FIXME("%p,%p: stub\n", iface, result);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_get_NumberOfMissedRuns(IRegisteredTask *iface, LONG *runs)
{
    FIXME("%p,%p: stub\n", iface, runs);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_get_NextRunTime(IRegisteredTask *iface, DATE *date)
{
    FIXME("%p,%p: stub\n", iface, date);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_get_Definition(IRegisteredTask *iface, ITaskDefinition **task)
{
231 232 233 234 235 236 237 238 239 240
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);

    TRACE("%p,%p\n", iface, task);

    if (!task) return E_POINTER;

    ITaskDefinition_AddRef(regtask->taskdef);
    *task = regtask->taskdef;

    return S_OK;
241 242 243 244
}

static HRESULT WINAPI regtask_get_Xml(IRegisteredTask *iface, BSTR *xml)
{
245 246 247 248 249 250 251
    RegisteredTask *regtask = impl_from_IRegisteredTask(iface);

    TRACE("%p,%p\n", iface, xml);

    if (!xml) return E_POINTER;

    return ITaskDefinition_get_XmlText(regtask->taskdef, xml);
252 253 254 255
}

static HRESULT WINAPI regtask_GetSecurityDescriptor(IRegisteredTask *iface, LONG info, BSTR *sddl)
{
256
    FIXME("%p,%lx,%p: stub\n", iface, info, sddl);
257 258 259 260 261
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_SetSecurityDescriptor(IRegisteredTask *iface, BSTR sddl, LONG flags)
{
262
    FIXME("%p,%s,%lx: stub\n", iface, debugstr_w(sddl), flags);
263 264 265 266 267
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_Stop(IRegisteredTask *iface, LONG flags)
{
268
    FIXME("%p,%lx: stub\n", iface, flags);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    return E_NOTIMPL;
}

static HRESULT WINAPI regtask_GetRunTimes(IRegisteredTask *iface, const LPSYSTEMTIME start, const LPSYSTEMTIME end,
                                          DWORD *count, LPSYSTEMTIME *time)
{
    FIXME("%p,%p.%p,%p,%p: stub\n", iface, start, end, count, time);
    return E_NOTIMPL;
}

static const IRegisteredTaskVtbl RegisteredTask_vtbl =
{
    regtask_QueryInterface,
    regtask_AddRef,
    regtask_Release,
    regtask_GetTypeInfoCount,
    regtask_GetTypeInfo,
    regtask_GetIDsOfNames,
    regtask_Invoke,
    regtask_get_Name,
    regtask_get_Path,
    regtask_get_State,
    regtask_get_Enabled,
    regtask_put_Enabled,
    regtask_Run,
    regtask_RunEx,
    regtask_GetInstances,
    regtask_get_LastRunTime,
    regtask_get_LastTaskResult,
    regtask_get_NumberOfMissedRuns,
    regtask_get_NextRunTime,
    regtask_get_Definition,
    regtask_get_Xml,
    regtask_GetSecurityDescriptor,
    regtask_SetSecurityDescriptor,
    regtask_Stop,
    regtask_GetRunTimes
};

308
HRESULT RegisteredTask_create(const WCHAR *path, const WCHAR *name, ITaskDefinition *definition, LONG flags,
309
                              TASK_LOGON_TYPE logon, IRegisteredTask **obj, BOOL create)
310
{
311
    WCHAR *full_name;
312
    RegisteredTask *regtask;
313 314
    HRESULT hr;

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    if (!name)
    {
        if (!create) return E_INVALIDARG;

        /* NULL task name is allowed only in the root folder */
        if (path[0] != '\\' || path[1])
            return E_INVALIDARG;

        full_name = NULL;
    }
    else
    {
        full_name = get_full_path(path, name);
        if (!full_name) return E_OUTOFMEMORY;
    }
330

331
    regtask = malloc(sizeof(*regtask));
332 333
    if (!regtask)
    {
334
        free(full_name);
335 336 337 338 339 340 341 342 343 344 345 346
        return E_OUTOFMEMORY;
    }

    if (create)
    {
        WCHAR *actual_path = NULL;
        TASK_XML_ERROR_INFO *error_info = NULL;
        BSTR xml = NULL;

        hr = ITaskDefinition_get_XmlText(definition, &xml);
        if (hr != S_OK || (hr = SchRpcRegisterTask(full_name, xml, flags, NULL, logon, 0, NULL, &actual_path, &error_info)) != S_OK)
        {
347 348
            free(full_name);
            free(regtask);
349 350 351
            SysFreeString(xml);
            return hr;
        }
352
        SysFreeString(xml);
353

354 355
        free(full_name);
        full_name = wcsdup(actual_path);
356 357 358 359 360 361 362
        MIDL_user_free(actual_path);
    }
    else
    {
        DWORD count = 0;
        WCHAR *xml = NULL;

363
        hr = SchRpcRetrieveTask(full_name, L"", &count, &xml);
364 365
        if (hr != S_OK || (hr = ITaskDefinition_put_XmlText(definition, xml)) != S_OK)
        {
366 367
            free(full_name);
            free(regtask);
368
            MIDL_user_free(xml);
369 370 371 372
            return hr;
        }
        MIDL_user_free(xml);
    }
373 374

    regtask->IRegisteredTask_iface.lpVtbl = &RegisteredTask_vtbl;
375
    regtask->path = full_name;
376
    regtask->ref = 1;
377
    regtask->taskdef = definition;
378 379 380 381 382 383
    *obj = &regtask->IRegisteredTask_iface;

    TRACE("created %p\n", *obj);

    return S_OK;
}
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

typedef struct
{
    IRegisteredTaskCollection IRegisteredTaskCollection_iface;
    LONG ref;
    WCHAR *path;
} RegisteredTaskCollection;

static inline RegisteredTaskCollection *impl_from_IRegisteredTaskCollection(IRegisteredTaskCollection *iface)
{
    return CONTAINING_RECORD(iface, RegisteredTaskCollection, IRegisteredTaskCollection_iface);
}

static ULONG WINAPI regtasks_AddRef(IRegisteredTaskCollection *iface)
{
    RegisteredTaskCollection *regtasks = impl_from_IRegisteredTaskCollection(iface);
    return InterlockedIncrement(&regtasks->ref);
}

static ULONG WINAPI regtasks_Release(IRegisteredTaskCollection *iface)
{
    RegisteredTaskCollection *regtasks = impl_from_IRegisteredTaskCollection(iface);
    LONG ref = InterlockedDecrement(&regtasks->ref);

    if (!ref)
    {
        TRACE("destroying %p\n", iface);
411 412
        free(regtasks->path);
        free(regtasks);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    }

    return ref;
}

static HRESULT WINAPI regtasks_QueryInterface(IRegisteredTaskCollection *iface, REFIID riid, void **obj)
{
    if (!riid || !obj) return E_INVALIDARG;

    TRACE("%p,%s,%p\n", iface, debugstr_guid(riid), obj);

    if (IsEqualGUID(riid, &IID_IRegisteredTaskCollection) ||
        IsEqualGUID(riid, &IID_IDispatch) ||
        IsEqualGUID(riid, &IID_IUnknown))
    {
        IRegisteredTaskCollection_AddRef(iface);
        *obj = iface;
        return S_OK;
    }

    FIXME("interface %s is not implemented\n", debugstr_guid(riid));
    *obj = NULL;
    return E_NOINTERFACE;
}

static HRESULT WINAPI regtasks_GetTypeInfoCount(IRegisteredTaskCollection *iface, UINT *count)
{
    FIXME("%p,%p: stub\n", iface, count);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_GetTypeInfo(IRegisteredTaskCollection *iface, UINT index, LCID lcid, ITypeInfo **info)
{
446
    FIXME("%p,%u,%lu,%p: stub\n", iface, index, lcid, info);
447 448 449 450 451 452
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_GetIDsOfNames(IRegisteredTaskCollection *iface, REFIID riid, LPOLESTR *names,
                                                UINT count, LCID lcid, DISPID *dispid)
{
453
    FIXME("%p,%s,%p,%u,%lu,%p: stub\n", iface, debugstr_guid(riid), names, count, lcid, dispid);
454 455 456 457 458 459
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_Invoke(IRegisteredTaskCollection *iface, DISPID dispid, REFIID riid, LCID lcid, WORD flags,
                                         DISPPARAMS *params, VARIANT *result, EXCEPINFO *excepinfo, UINT *argerr)
{
460
    FIXME("%p,%ld,%s,%04lx,%04x,%p,%p,%p,%p: stub\n", iface, dispid, debugstr_guid(riid), lcid, flags,
461 462 463 464 465 466 467
          params, result, excepinfo, argerr);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_get_Count(IRegisteredTaskCollection *iface, LONG *count)
{
    FIXME("%p,%p: stub\n", iface, count);
468
    if (count) *count = 0;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_get_Item(IRegisteredTaskCollection *iface, VARIANT index, IRegisteredTask **regtask)
{
    FIXME("%p,%s,%p: stub\n", iface, debugstr_variant(&index), regtask);
    return E_NOTIMPL;
}

static HRESULT WINAPI regtasks_get__NewEnum(IRegisteredTaskCollection *iface, IUnknown **penum)
{
    FIXME("%p,%p: stub\n", iface, penum);
    return E_NOTIMPL;
}

static const IRegisteredTaskCollectionVtbl RegisteredTaskCollection_vtbl =
{
    regtasks_QueryInterface,
    regtasks_AddRef,
    regtasks_Release,
    regtasks_GetTypeInfoCount,
    regtasks_GetTypeInfo,
    regtasks_GetIDsOfNames,
    regtasks_Invoke,
    regtasks_get_Count,
    regtasks_get_Item,
    regtasks_get__NewEnum
};

HRESULT RegisteredTaskCollection_create(const WCHAR *path, IRegisteredTaskCollection **obj)
{
    RegisteredTaskCollection *regtasks;

502
    regtasks = malloc(sizeof(*regtasks));
503 504 505 506
    if (!regtasks) return E_OUTOFMEMORY;

    regtasks->IRegisteredTaskCollection_iface.lpVtbl = &RegisteredTaskCollection_vtbl;
    regtasks->ref = 1;
507
    regtasks->path = wcsdup(path);
508 509 510 511 512 513
    *obj = &regtasks->IRegisteredTaskCollection_iface;

    TRACE("created %p\n", *obj);

    return S_OK;
}