| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * NetUserGetGroups query
|
|---|
| 4 | * Copyright (C) Guenther Deschner 2008
|
|---|
| 5 | *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by
|
|---|
| 8 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | * (at your option) any later version.
|
|---|
| 10 | *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful,
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | * GNU General Public License for more details.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License
|
|---|
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include <sys/types.h>
|
|---|
| 21 | #include <inttypes.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <stdlib.h>
|
|---|
| 24 | #include <string.h>
|
|---|
| 25 |
|
|---|
| 26 | #include <netapi.h>
|
|---|
| 27 |
|
|---|
| 28 | #include "common.h"
|
|---|
| 29 |
|
|---|
| 30 | int main(int argc, const char **argv)
|
|---|
| 31 | {
|
|---|
| 32 | NET_API_STATUS status;
|
|---|
| 33 | struct libnetapi_ctx *ctx = NULL;
|
|---|
| 34 | const char *hostname = NULL;
|
|---|
| 35 | const char *username = NULL;
|
|---|
| 36 | uint32_t level = 0;
|
|---|
| 37 | uint8_t *buffer = NULL;
|
|---|
| 38 | uint32_t entries_read = 0;
|
|---|
| 39 | uint32_t total_entries = 0;
|
|---|
| 40 | int i;
|
|---|
| 41 |
|
|---|
| 42 | struct GROUP_USERS_INFO_0 *info0 = NULL;
|
|---|
| 43 | struct GROUP_USERS_INFO_1 *info1 = NULL;
|
|---|
| 44 |
|
|---|
| 45 | poptContext pc;
|
|---|
| 46 | int opt;
|
|---|
| 47 |
|
|---|
| 48 | struct poptOption long_options[] = {
|
|---|
| 49 | POPT_AUTOHELP
|
|---|
| 50 | POPT_COMMON_LIBNETAPI_EXAMPLES
|
|---|
| 51 | POPT_TABLEEND
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | status = libnetapi_init(&ctx);
|
|---|
| 55 | if (status != 0) {
|
|---|
| 56 | return status;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | pc = poptGetContext("user_getgroups", argc, argv, long_options, 0);
|
|---|
| 60 |
|
|---|
| 61 | poptSetOtherOptionHelp(pc, "hostname username level");
|
|---|
| 62 | while((opt = poptGetNextOpt(pc)) != -1) {
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (!poptPeekArg(pc)) {
|
|---|
| 66 | poptPrintHelp(pc, stderr, 0);
|
|---|
| 67 | goto out;
|
|---|
| 68 | }
|
|---|
| 69 | hostname = poptGetArg(pc);
|
|---|
| 70 |
|
|---|
| 71 | if (!poptPeekArg(pc)) {
|
|---|
| 72 | poptPrintHelp(pc, stderr, 0);
|
|---|
| 73 | goto out;
|
|---|
| 74 | }
|
|---|
| 75 | username = poptGetArg(pc);
|
|---|
| 76 |
|
|---|
| 77 | if (poptPeekArg(pc)) {
|
|---|
| 78 | level = atoi(poptGetArg(pc));
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* NetUserGetGroups */
|
|---|
| 82 |
|
|---|
| 83 | do {
|
|---|
| 84 | status = NetUserGetGroups(hostname,
|
|---|
| 85 | username,
|
|---|
| 86 | level,
|
|---|
| 87 | &buffer,
|
|---|
| 88 | (uint32_t)-1,
|
|---|
| 89 | &entries_read,
|
|---|
| 90 | &total_entries);
|
|---|
| 91 | if (status == 0 || status == ERROR_MORE_DATA) {
|
|---|
| 92 |
|
|---|
| 93 | switch (level) {
|
|---|
| 94 | case 0:
|
|---|
| 95 | info0 = (struct GROUP_USERS_INFO_0 *)buffer;
|
|---|
| 96 | break;
|
|---|
| 97 | case 1:
|
|---|
| 98 | info1 = (struct GROUP_USERS_INFO_1 *)buffer;
|
|---|
| 99 | break;
|
|---|
| 100 | default:
|
|---|
| 101 | break;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | for (i=0; i<entries_read; i++) {
|
|---|
| 105 | switch (level) {
|
|---|
| 106 | case 0:
|
|---|
| 107 | printf("#%d group: %s\n", i, info0->grui0_name);
|
|---|
| 108 | info0++;
|
|---|
| 109 | break;
|
|---|
| 110 | case 1:
|
|---|
| 111 | printf("#%d group: %s\n", i, info1->grui1_name);
|
|---|
| 112 | printf("#%d attributes: %d\n", i, info1->grui1_attributes);
|
|---|
| 113 | info1++;
|
|---|
| 114 | break;
|
|---|
| 115 | default:
|
|---|
| 116 | break;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 | NetApiBufferFree(buffer);
|
|---|
| 120 | }
|
|---|
| 121 | } while (status == ERROR_MORE_DATA);
|
|---|
| 122 |
|
|---|
| 123 | if (status != 0) {
|
|---|
| 124 | printf("NetUserGetGroups failed with: %s\n",
|
|---|
| 125 | libnetapi_get_error_string(ctx, status));
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | out:
|
|---|
| 129 | libnetapi_free(ctx);
|
|---|
| 130 | poptFreeContext(pc);
|
|---|
| 131 |
|
|---|
| 132 | return status;
|
|---|
| 133 | }
|
|---|