root/source3/lib/netapi/examples/localgroup/localgroup_enum.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  *  Unix SMB/CIFS implementation.
   3  *  NetLocalGroupEnum 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 
  42         struct LOCALGROUP_INFO_0 *info0 = NULL;
  43         struct LOCALGROUP_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("localgroup_enum", argc, argv, long_options, 0);
  60 
  61         poptSetOtherOptionHelp(pc, "hostname 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                 level = atoi(poptGetArg(pc));
  73         }
  74 
  75         /* NetLocalGroupEnum */
  76 
  77         do {
  78                 status = NetLocalGroupEnum(hostname,
  79                                            level,
  80                                            &buffer,
  81                                            (uint32_t)-1,
  82                                            &entries_read,
  83                                            &total_entries,
  84                                            &resume_handle);
  85                 if (status == 0 || status == ERROR_MORE_DATA) {
  86                         printf("total entries: %d\n", total_entries);
  87                         switch (level) {
  88                                 case 0:
  89                                         info0 = (struct LOCALGROUP_INFO_0 *)buffer;
  90                                         break;
  91                                 case 1:
  92                                         info1 = (struct LOCALGROUP_INFO_1 *)buffer;
  93                                         break;
  94                                 default:
  95                                         break;
  96                         }
  97                         for (i=0; i<entries_read; i++) {
  98                                 switch (level) {
  99                                         case 0:
 100                                                 printf("#%d group: %s\n", i, info0->lgrpi0_name);
 101                                                 info0++;
 102                                                 break;
 103                                         case 1:
 104                                                 printf("#%d group: %s\n", i, info1->lgrpi1_name);
 105                                                 printf("#%d comment: %s\n", i, info1->lgrpi1_comment);
 106                                                 info1++;
 107                                                 break;
 108                                         default:
 109                                                 break;
 110                                 }
 111                         }
 112                         NetApiBufferFree(buffer);
 113                 }
 114         } while (status == ERROR_MORE_DATA);
 115 
 116         if (status != 0) {
 117                 printf("NetLocalGroupEnum failed with: %s\n",
 118                         libnetapi_get_error_string(ctx, status));
 119         }
 120 
 121  out:
 122         libnetapi_free(ctx);
 123         poptFreeContext(pc);
 124 
 125         return status;
 126 }

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