Пример #1
0
static
DWORD
ParseArgs(
    int    argc,
    char*  argv[],
    DWORD* pdwAction,
    PSTR*  ppszDomainName,
    PSTR*  ppszName,
    uid_t* pUID,
    gid_t* pGID,
    bool* bForceOfflineDelete,
    DWORD* pdwBatchSize
    )
{
    typedef enum {
            PARSE_MODE_OPEN = 0,
            PARSE_MODE_DOMAIN_NAME,
            PARSE_MODE_NAME,
            PARSE_MODE_UID,
            PARSE_MODE_GID,
            PARSE_MODE_BATCHSIZE,
            PARSE_MODE_FORCE_OFFLINE_DELETE,
            PARSE_MODE_DONE
    } ParseMode;

    DWORD dwError = 0;
    int iArg = 1;
    PSTR pszArg = NULL;
    ParseMode parseMode = PARSE_MODE_OPEN;
    DWORD dwAction = ACTION_NONE;
    PSTR  pszDomainName = NULL;
    PSTR  pszName = NULL;
    uid_t uid = 0;
    gid_t gid = 0;
    DWORD dwBatchSize = 10;
    
    *bForceOfflineDelete = false;

    do {
        pszArg = argv[iArg++];
        if (pszArg == NULL || *pszArg == '\0')
        {
            break;
        }

        switch (parseMode)
        {
            case PARSE_MODE_OPEN:

                if ((strcmp(pszArg, "--help") == 0) ||
                    (strcmp(pszArg, "-h") == 0))
                {
                    ShowUsage(GetProgramName(argv[0]));
                    exit(0);
                }
                else if (!strcmp(pszArg, "--delete-all")) {
                    dwAction = ACTION_DELETE_ALL;            
                }
                else if (!strcmp(pszArg, "--delete-user")) {
                    dwAction = ACTION_DELETE_USER;
                }
                else if (!strcmp(pszArg, "--delete-group")) {
                    dwAction = ACTION_DELETE_GROUP;
                }
                else if (!strcmp(pszArg, "--enum-users")) {
                    dwAction = ACTION_ENUM_USERS;
                }
                else if (!strcmp(pszArg, "--enum-groups")) {
                    dwAction = ACTION_ENUM_GROUPS;
                }
                else if (!strcmp(pszArg, "--domain")) {
                    parseMode = PARSE_MODE_DOMAIN_NAME;
                }
                else if (!strcmp(pszArg, "--name")) {
                    parseMode = PARSE_MODE_NAME;
                }
                else if (!strcmp(pszArg, "--uid")) {
                    parseMode = PARSE_MODE_UID;
                }
                else if (!strcmp(pszArg, "--gid")) {
                    parseMode = PARSE_MODE_GID;
                }
                else if (!strcmp(pszArg, "--batchsize")) {
                    parseMode = PARSE_MODE_BATCHSIZE;
                }
                else if (!strcmp(pszArg, "--force-offline-delete")) {
                    parseMode = PARSE_MODE_FORCE_OFFLINE_DELETE;
                }
                else
                {
                    ShowUsage(GetProgramName(argv[0]));
                    exit(1);
                }
                break;

            case PARSE_MODE_DOMAIN_NAME:
                dwError = LwAllocateString(pszArg, &pszDomainName);
                BAIL_ON_LSA_ERROR(dwError);
                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_NAME:
                dwError = LwAllocateString(pszArg, &pszName);
                BAIL_ON_LSA_ERROR(dwError);
                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_UID:
                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please enter a UID which is an unsigned integer.\n");
                    ShowUsage(GetProgramName(argv[0]));
                    exit(1);
                }
                uid = atoi(pszArg);
                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_GID:
                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please enter a GID which is an unsigned integer.\n");
                    ShowUsage(GetProgramName(argv[0]));
                    exit(1);
                }
                gid = atoi(pszArg);
                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_BATCHSIZE:
                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please enter a valid batch size.\n");
                    ShowUsage(GetProgramName(argv[0]));
                    exit(1);
                }
                dwBatchSize = atoi(pszArg);
                if ((dwBatchSize == 0) ||
                    (dwBatchSize > 1000)) {
                    fprintf(stderr, "Please enter a valid batch size.\n");
                    ShowUsage(GetProgramName(argv[0]));
                    exit(1);
                }
                parseMode = PARSE_MODE_OPEN;

                break;
            case PARSE_MODE_FORCE_OFFLINE_DELETE:
                *bForceOfflineDelete = (strcasecmp(pszArg, "true") == 0);
                parseMode = PARSE_MODE_OPEN;
                break;

            case PARSE_MODE_DONE:
                ShowUsage(GetProgramName(argv[0]));
                exit(1);
        }

    } while (iArg < argc);

    if (parseMode != PARSE_MODE_OPEN && parseMode != PARSE_MODE_DONE)
    {
        ShowUsage(GetProgramName(argv[0]));
        exit(1);
    }

    if ( dwAction == ACTION_NONE )
    {
        fprintf(stderr, "Please specify a valid action.\n");
        ShowUsage(GetProgramName(argv[0]));
        exit(1);
    }

    if ( dwAction == ACTION_DELETE_USER )
    {
        if ( LW_IS_NULL_OR_EMPTY_STR(pszName) && !uid )
        {
            fprintf(stderr, "Please specify name or UID.\n");
            ShowUsage(GetProgramName(argv[0]));
            exit(1);
        }
    }

    if ( dwAction == ACTION_DELETE_GROUP )
    {
        if ( LW_IS_NULL_OR_EMPTY_STR(pszName) && !gid )
        {
            fprintf(stderr, "Please specify name or GID.\n");
            ShowUsage(GetProgramName(argv[0]));
            exit(1);
        }
    }

    *pdwAction = dwAction;
    *ppszDomainName = pszDomainName;
    *ppszName = pszName;
    *pUID = uid;
    *pGID = gid;
    *pdwBatchSize = dwBatchSize;

cleanup:

    return dwError;

error:

    *pdwAction = ACTION_NONE;
    *ppszDomainName = NULL;
    *ppszName = NULL;
    *pUID = 0;
    *pGID = 0;
    *pdwBatchSize = 0;

    LW_SAFE_FREE_STRING(pszName);
    LW_SAFE_FREE_STRING(pszDomainName);

    goto cleanup;
}
static
DWORD
ParseArgs(
    int argc,
    PSTR argv[],
    PSTR* ppszGid,
    PSTR* ppszGroup
    )
{
    typedef enum {
        PARSE_MODE_OPEN = 0,
        PARSE_MODE_GID,
        PARSE_MODE_DONE
    } ParseMode;

    DWORD dwError = 0;
    ParseMode parseMode = PARSE_MODE_OPEN;
    int iArg = 1;
    PSTR pArg = NULL;
    PSTR pszGid = NULL;
    PSTR pszGroup = NULL;

    do {

      pArg = argv[iArg++];
      if (pArg == NULL || *pArg == '\0') {
        break;
      }

      switch(parseMode) {

          case PARSE_MODE_OPEN:
          {
              if ((strcmp(pArg, "--help") == 0) ||
                     (strcmp(pArg, "-h") == 0)) {
                ShowUsage(LsaGetProgramName(argv[0]));
                exit(0);
              }
              else if (strcmp(pArg, "--gid") == 0) {
                parseMode = PARSE_MODE_GID;
              }
              else {
                  dwError = LwAllocateString(pArg, &pszGroup);
                  BAIL_ON_LSA_ERROR(dwError);
                  parseMode = PARSE_MODE_DONE;
              }

              break;
          }

          case PARSE_MODE_GID:
          {
              if (!IsUnsignedInteger(pArg))
              {
                fprintf(stderr, "Please specifiy a gid that is an unsigned integer\n");
                ShowUsage(LsaGetProgramName(argv[0]));
                exit(1);
              }

              dwError = LwAllocateString(pArg, &pszGid);
              BAIL_ON_LSA_ERROR(dwError);

              parseMode = PARSE_MODE_OPEN;

              break;
          }

          case PARSE_MODE_DONE:
          {
              ShowUsage(LsaGetProgramName(argv[0]));
              exit(1);
          }
      }

    } while (iArg < argc);

    if (parseMode != PARSE_MODE_OPEN && parseMode != PARSE_MODE_DONE)
    {
        ShowUsage(LsaGetProgramName(argv[0]));
        exit(1);
    }

    if (LW_IS_NULL_OR_EMPTY_STR(pszGroup)) {
        fprintf(stderr, "Please specify a valid group name.\n");
        ShowUsage(LsaGetProgramName(argv[0]));
        exit(1);
    }

    *ppszGid = pszGid;
    *ppszGroup = pszGroup;

cleanup:

    return dwError;

error:

    LW_SAFE_FREE_STRING(pszGid);
    LW_SAFE_FREE_STRING(pszGroup);

    *ppszGid = NULL;
    *ppszGroup = NULL;

    goto cleanup;
}
Пример #3
0
VOID
ParseArgs(
    int    argc,
    char*  argv[],
    PDWORD pdwInfoLevel
    )
{
    typedef enum {
            PARSE_MODE_OPEN = 0,
            PARSE_MODE_LEVEL,
            PARSE_MODE_DONE
        } ParseMode;

    int iArg = 1;
    PSTR pszArg = NULL;
    ParseMode parseMode = PARSE_MODE_OPEN;
    DWORD dwInfoLevel = 0;

    do {
        pszArg = argv[iArg++];
        if (pszArg == NULL || *pszArg == '\0')
        {
            break;
        }

        switch (parseMode)
        {
            case PARSE_MODE_OPEN:

                if ((strcmp(pszArg, "--help") == 0) ||
                    (strcmp(pszArg, "-h") == 0))
                {
                    ShowUsage();
                    exit(0);
                }
                else if (!strcmp(pszArg, "--level")) {
                    parseMode = PARSE_MODE_LEVEL;
                }
                else
                {
                    ShowUsage();
                    exit(1);
                }

                break;

            case PARSE_MODE_LEVEL:

                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please enter an info level which is an unsigned integer.\n");
                    ShowUsage();
                    exit(1);
                }

                dwInfoLevel = atoi(pszArg);
                parseMode = PARSE_MODE_DONE;

                break;

            case PARSE_MODE_DONE:

                ShowUsage();
                exit(1);

        }

    } while (iArg < argc);

    if (parseMode != PARSE_MODE_OPEN && parseMode != PARSE_MODE_DONE)
    {
        ShowUsage();
        exit(1);
    }

    *pdwInfoLevel = dwInfoLevel;
}
Пример #4
0
DWORD
ParseArgs(
    int    argc,
    char*  argv[],
    PDWORD pdwInfoLevel,
    PDWORD pdwBatchSize,
    PBOOLEAN pbCheckUserInList
    )
{
    typedef enum {
            PARSE_MODE_OPEN = 0,
            PARSE_MODE_LEVEL,
            PARSE_MODE_BATCHSIZE,
            PARSE_MODE_DONE
        } ParseMode;

    DWORD dwError = 0;
    int iArg = 1;
    PSTR pszArg = NULL;
    ParseMode parseMode = PARSE_MODE_OPEN;
    DWORD dwInfoLevel = 0;
    DWORD dwBatchSize = 10;
    BOOLEAN bCheckUserInList = FALSE;

    do {
        pszArg = argv[iArg++];
        if (pszArg == NULL || *pszArg == '\0')
        {
            break;
        }

        switch (parseMode)
        {
            case PARSE_MODE_OPEN:

                if ((strcmp(pszArg, "--help") == 0) ||
                    (strcmp(pszArg, "-h") == 0))
                {
                    ShowUsage();
                    exit(0);
                }
                else if (!strcmp(pszArg, "--level")) {
                    parseMode = PARSE_MODE_LEVEL;
                }
                else if (!strcmp(pszArg, "--batchsize")) {
                    parseMode = PARSE_MODE_BATCHSIZE;
                }
                else if (!strcmp(pszArg, "--checkUserinList") || !strcmp(pszArg, "-c") || !strcmp(pszArg, "-C")) {
                    bCheckUserInList = TRUE;
                }
                else
                {
                    ShowUsage();
                    exit(1);
                }
                break;

            case PARSE_MODE_LEVEL:

                if (!IsUnsignedInteger(pszArg))
                {
                    ShowUsage();
                    exit(1);
                }

                dwInfoLevel = atoi(pszArg);
                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_BATCHSIZE:

                if (!IsUnsignedInteger(pszArg))
                {
                    ShowUsage();
                    exit(1);
                }

                dwBatchSize = atoi(pszArg);
		if ((dwBatchSize == 0) ||
                    (dwBatchSize > 1000)) {
                    ShowUsage();
                    exit(1);
                }
                parseMode = PARSE_MODE_DONE;

                break;

            case PARSE_MODE_DONE:
                ShowUsage();
                exit(1);
        }

    } while (iArg < argc);

    if (parseMode != PARSE_MODE_OPEN && parseMode != PARSE_MODE_DONE)
    {
        ShowUsage();
        exit(1);
    }

    if (parseMode != PARSE_MODE_OPEN && parseMode != PARSE_MODE_DONE)
    {
        ShowUsage();
        exit(1);
    }

    *pdwInfoLevel = dwInfoLevel;
    *pdwBatchSize = dwBatchSize;
    *pbCheckUserInList = bCheckUserInList;

    return dwError;
}
Пример #5
0
static
DWORD
ParseArgs(
    int    argc,
    char*  argv[],
    PDWORD pdwInfoLevel,
    PDWORD pdwBatchSize,
    PBOOLEAN pbCheckGroupMembersOnline
    )
{
    typedef enum {
            PARSE_MODE_OPEN = 0,
            PARSE_MODE_LEVEL,
            PARSE_MODE_BATCHSIZE
        } ParseMode;

    DWORD dwError = 0;
    int iArg = 1;
    PSTR pszArg = NULL;
    ParseMode parseMode = PARSE_MODE_OPEN;
    DWORD dwInfoLevel = 0;
    DWORD dwBatchSize = 10;
    BOOLEAN bCheckGroupMembersOnline = FALSE;

    do {
        pszArg = argv[iArg++];
        if (pszArg == NULL || *pszArg == '\0')
        {
            break;
        }

        switch (parseMode)
        {
            case PARSE_MODE_OPEN:

                if ((strcmp(pszArg, "--help") == 0) ||
                    (strcmp(pszArg, "-h") == 0))
                {
                    ShowUsage();
                    exit(0);
                }
                else if (!strcmp(pszArg, "--level")) {
                    parseMode = PARSE_MODE_LEVEL;
                }
                else if (!strcmp(pszArg, "--batchsize")) {
                    parseMode = PARSE_MODE_BATCHSIZE;
                }
                else if (!strcmp(pszArg, "--check-group-members-online") ||
                        !strcmp(pszArg, "-c"))
               {
                   bCheckGroupMembersOnline = TRUE;
               }
                else
                {
                    ShowUsage();
                    exit(0);
                }
                break;

            case PARSE_MODE_LEVEL:

                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please use an info level which is an unsigned integer.\n");
                    ShowUsage();
                    exit(1);
                }

                dwInfoLevel = atoi(pszArg);
                if (dwInfoLevel > 1) {
                    ShowUsage();
                    exit(1);
                }

                parseMode = PARSE_MODE_OPEN;

                break;

            case PARSE_MODE_BATCHSIZE:

                if (!IsUnsignedInteger(pszArg))
                {
                    fprintf(stderr, "Please use a batchsize which is an unsigned integer.\n");
                    ShowUsage();
                    exit(1);
                }

                dwBatchSize = atoi(pszArg);
                if ((dwBatchSize == 0) ||
                    (dwBatchSize > 1000)) {
                    ShowUsage();
                    exit(1);
                }
                parseMode = PARSE_MODE_OPEN;

                break;
        }

    } while (iArg < argc);

    if (parseMode != PARSE_MODE_OPEN)
    {
        ShowUsage();
        exit(1);
    }

    *pdwInfoLevel = dwInfoLevel;
    *pdwBatchSize = dwBatchSize;
    *pbCheckGroupMembersOnline = bCheckGroupMembersOnline;

    return dwError;
}
DWORD
ParseArgs(
    int    argc,
    char*  argv[],
    PSTR*  ppszGroupId,
    PLSA_FIND_FLAGS pFindFlags,
    PDWORD pdwInfoLevel,
    PBOOLEAN pbCountOnly
    )
{
    DWORD dwError = 0;
    int iArg = 1;
    PSTR pszArg = NULL;
    PSTR pszGroupId = NULL;
    LSA_FIND_FLAGS FindFlags = 0;
    DWORD dwInfoLevel = 0;
    BOOLEAN bCountOnly = FALSE;

    for (iArg = 1; iArg < argc; iArg++)
    {
        pszArg = argv[iArg];
        if (!strcmp(pszArg, "--help") ||
            !strcmp(pszArg, "-h"))
        {
            ShowUsage();
            exit(0);
        }
        else if (!strcmp(pszArg, "--count"))
        {
            bCountOnly = TRUE;
        }
        else if (!strcmp(pszArg, "--level"))
        {
            PCSTR pszValue;
            if (iArg + 1 >= argc)
            {
                fprintf(stderr, "Missing argument for %s option.\n", pszArg);
                ShowUsage();
                exit(1);
            }
            pszValue = argv[++iArg];
            if (!IsUnsignedInteger(pszValue))
            {
                fprintf(stderr, "Please enter an info level which is an unsigned integer.\n");
                ShowUsage();
                exit(1);
            }
            dwInfoLevel = atoi(pszValue);
        }
        else if (!strcmp(pszArg, "--flags"))
        {
            PCSTR pszValue;
            if (iArg + 1 >= argc)
            {
                fprintf(stderr, "Missing argument for %s option.\n", pszArg);
                ShowUsage();
                exit(1);
            }
            pszValue = argv[++iArg];
            if (!IsUnsignedInteger(pszValue))
            {
                fprintf(stderr, "Please enter a flags value which is an unsigned integer.\n");
                ShowUsage();
                exit(1);
            }
            FindFlags = atoi(pszValue);
        }
        else if (pszArg[0] == '-')
        {
            fprintf(stderr, "Invalid option '%s'.\n", pszArg);
            ShowUsage();
            exit(1);
        }
        else
        {
            break;
        }
    }

    if ((argc - iArg) < 1)
    {
        fprintf(stderr, "Missing required group name argument.\n");
        ShowUsage();
        exit(1);
    }
    dwError = LwAllocateString(argv[iArg++], &pszGroupId);
    BAIL_ON_LSA_ERROR(dwError);

    if ((argc - iArg) > 0)
    {
        fprintf(stderr, "Too many arguments.\n");
        ShowUsage();
        exit(1);
    }

    if (LW_IS_NULL_OR_EMPTY_STR(pszGroupId))
    {
        fprintf(stderr, "Please specify a non-empty group name to query for.\n");
        ShowUsage();
        exit(1);
    }

    *ppszGroupId = pszGroupId;
    *pFindFlags = FindFlags;
    *pdwInfoLevel = dwInfoLevel;
    *pbCountOnly = bCountOnly;

cleanup:

    return dwError;

error:

    *ppszGroupId = NULL;
    *pFindFlags = 0;
    *pdwInfoLevel = 0;
    *pbCountOnly = FALSE;

    LW_SAFE_FREE_STRING(pszGroupId);

    goto cleanup;
}