root/source3/lib/netapi/examples/user/user_setgroups.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  *  Unix SMB/CIFS implementation.
   3  *  NetUserSetGroups 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         const char *username = NULL;
  36         uint32_t level = 0;
  37         uint8_t *buffer = NULL;
  38         uint32_t num_entries = 0;
  39         const char **names = NULL;
  40         int i = 0;
  41         size_t buf_size = 0;
  42 
  43         struct GROUP_USERS_INFO_0 *g0 = NULL;
  44         struct GROUP_USERS_INFO_1 *g1 = NULL;
  45 
  46         poptContext pc;
  47         int opt;
  48 
  49         struct poptOption long_options[] = {
  50                 POPT_AUTOHELP
  51                 POPT_COMMON_LIBNETAPI_EXAMPLES
  52                 POPT_TABLEEND
  53         };
  54 
  55         status = libnetapi_init(&ctx);
  56         if (status != 0) {
  57                 return status;
  58         }
  59 
  60         pc = poptGetContext("user_setgroups", argc, argv, long_options, 0);
  61 
  62         poptSetOtherOptionHelp(pc, "hostname username group1 group2 ...");
  63         while((opt = poptGetNextOpt(pc)) != -1) {
  64         }
  65 
  66         if (!poptPeekArg(pc)) {
  67                 poptPrintHelp(pc, stderr, 0);
  68                 goto out;
  69         }
  70         hostname = poptGetArg(pc);
  71 
  72         if (!poptPeekArg(pc)) {
  73                 poptPrintHelp(pc, stderr, 0);
  74                 goto out;
  75         }
  76         username = poptGetArg(pc);
  77 
  78         if (!poptPeekArg(pc)) {
  79                 poptPrintHelp(pc, stderr, 0);
  80                 goto out;
  81         }
  82 
  83         names = poptGetArgs(pc);
  84         for (i=0; names[i] != NULL; i++) {
  85                 num_entries++;
  86         }
  87 
  88         switch (level) {
  89                 case 0:
  90                         buf_size = sizeof(struct GROUP_USERS_INFO_0) * num_entries;
  91 
  92                         status = NetApiBufferAllocate(buf_size, (void **)&g0);
  93                         if (status) {
  94                                 printf("NetApiBufferAllocate failed with: %s\n",
  95                                         libnetapi_get_error_string(ctx, status));
  96                                 goto out;
  97                         }
  98 
  99                         for (i=0; i<num_entries; i++) {
 100                                 g0[i].grui0_name = names[i];
 101                         }
 102 
 103                         buffer = (uint8_t *)g0;
 104                         break;
 105                 case 1:
 106                         buf_size = sizeof(struct GROUP_USERS_INFO_1) * num_entries;
 107 
 108                         status = NetApiBufferAllocate(buf_size, (void **)&g1);
 109                         if (status) {
 110                                 printf("NetApiBufferAllocate failed with: %s\n",
 111                                         libnetapi_get_error_string(ctx, status));
 112                                 goto out;
 113                         }
 114 
 115                         for (i=0; i<num_entries; i++) {
 116                                 g1[i].grui1_name = names[i];
 117                                 g1[i].grui1_attributes = 0; /* ? */
 118                         }
 119 
 120                         buffer = (uint8_t *)g1;
 121                         break;
 122                 default:
 123                         break;
 124         }
 125 
 126         /* NetUserSetGroups */
 127 
 128         status = NetUserSetGroups(hostname,
 129                                   username,
 130                                   level,
 131                                   buffer,
 132                                   num_entries);
 133         if (status != 0) {
 134                 printf("NetUserSetGroups failed with: %s\n",
 135                         libnetapi_get_error_string(ctx, status));
 136         }
 137 
 138  out:
 139         NetApiBufferFree(buffer);
 140         libnetapi_free(ctx);
 141         poptFreeContext(pc);
 142 
 143         return status;
 144 }

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