root/source3/lib/netapi/examples/group/group_enum.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  *  Unix SMB/CIFS implementation.
   3  *  NetGroupEnum 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)
     /* [<][>][^][v][top][bottom][index][help] */
  31 {
  32         NET_API_STATUS status;
  33         struct libnetapi_ctx *ctx = NULL;
  34         const char *hostname = NULL;
  35         uint32_t level = 0;
  36         uint8_t *buffer = NULL;
  37         uint32_t entries_read = 0;
  38         uint32_t total_entries = 0;
  39         uint32_t resume_handle = 0;
  40         int i;
  41         char *sid_str = NULL;
  42 
  43         struct GROUP_INFO_0 *info0 = NULL;
  44         struct GROUP_INFO_1 *info1 = NULL;
  45         struct GROUP_INFO_2 *info2 = NULL;
  46         struct GROUP_INFO_3 *info3 = NULL;
  47 
  48         poptContext pc;
  49         int opt;
  50 
  51         struct poptOption long_options[] = {
  52                 POPT_AUTOHELP
  53                 POPT_COMMON_LIBNETAPI_EXAMPLES
  54                 POPT_TABLEEND
  55         };
  56 
  57         status = libnetapi_init(&ctx);
  58         if (status != 0) {
  59                 return status;
  60         }
  61 
  62         pc = poptGetContext("group_enum", argc, argv, long_options, 0);
  63 
  64         poptSetOtherOptionHelp(pc, "hostname level");
  65         while((opt = poptGetNextOpt(pc)) != -1) {
  66         }
  67 
  68         if (!poptPeekArg(pc)) {
  69                 poptPrintHelp(pc, stderr, 0);
  70                 goto out;
  71         }
  72         hostname = poptGetArg(pc);
  73 
  74         if (poptPeekArg(pc)) {
  75                 level = atoi(poptGetArg(pc));
  76         }
  77 
  78         /* NetGroupEnum */
  79 
  80         do {
  81                 status = NetGroupEnum(hostname,
  82                                       level,
  83                                       &buffer,
  84                                       (uint32_t)-1,
  85                                       &entries_read,
  86                                       &total_entries,
  87                                       &resume_handle);
  88                 if (status == 0 || status == ERROR_MORE_DATA) {
  89                         printf("total entries: %d\n", total_entries);
  90                         switch (level) {
  91                                 case 0:
  92                                         info0 = (struct GROUP_INFO_0 *)buffer;
  93                                         break;
  94                                 case 1:
  95                                         info1 = (struct GROUP_INFO_1 *)buffer;
  96                                         break;
  97                                 case 2:
  98                                         info2 = (struct GROUP_INFO_2 *)buffer;
  99                                         break;
 100                                 case 3:
 101                                         info3 = (struct GROUP_INFO_3 *)buffer;
 102                                         break;
 103                                 default:
 104                                         break;
 105                         }
 106                         for (i=0; i<entries_read; i++) {
 107                                 switch (level) {
 108                                         case 0:
 109                                                 printf("#%d group: %s\n", i, info0->grpi0_name);
 110                                                 info0++;
 111                                                 break;
 112                                         case 1:
 113                                                 printf("#%d group: %s\n", i, info1->grpi1_name);
 114                                                 printf("#%d comment: %s\n", i, info1->grpi1_comment);
 115                                                 info1++;
 116                                                 break;
 117                                         case 2:
 118                                                 printf("#%d group: %s\n", i, info2->grpi2_name);
 119                                                 printf("#%d comment: %s\n", i, info2->grpi2_comment);
 120                                                 printf("#%d rid: %d\n", i, info2->grpi2_group_id);
 121                                                 printf("#%d attributes: 0x%08x\n", i, info2->grpi2_attributes);
 122                                                 info2++;
 123                                                 break;
 124                                         case 3:
 125                                                 printf("#%d group: %s\n", i, info3->grpi3_name);
 126                                                 printf("#%d comment: %s\n", i, info3->grpi3_comment);
 127                                                 if (ConvertSidToStringSid(info3->grpi3_group_sid,
 128                                                                           &sid_str)) {
 129                                                         printf("#%d group_sid: %s\n", i, sid_str);
 130                                                         free(sid_str);
 131                                                 }
 132                                                 printf("#%d attributes: 0x%08x\n", i, info3->grpi3_attributes);
 133                                                 info3++;
 134                                                 break;
 135 
 136 
 137                                 }
 138                         }
 139                         NetApiBufferFree(buffer);
 140                 }
 141         } while (status == ERROR_MORE_DATA);
 142 
 143         if (status != 0) {
 144                 printf("NetGroupEnum failed with: %s\n",
 145                         libnetapi_get_error_string(ctx, status));
 146         }
 147 
 148  out:
 149         libnetapi_free(ctx);
 150         poptFreeContext(pc);
 151 
 152         return status;
 153 }

/* [<][>][^][v][top][bottom][index][help] */