Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

samuser.c

Go to the documentation of this file.
00001 /*Some user management stuff*/
00002 
00003 #include "libmsrpc.h"
00004 #include "test_util.h"
00005 
00006 int main(int argc, char **argv) {
00007    CacServerHandle *hnd = NULL;
00008    TALLOC_CTX *mem_ctx = NULL;
00009             
00010    
00011    struct SamOpenUser    ou;
00012    struct SamEnumUsers   eu;
00013    struct SamCreateUser  cu;
00014    struct SamGetUserInfo gi;
00015    struct SamSetUserInfo si;
00016    struct SamRenameUser  ru;
00017    struct SamSetPassword sp;
00018 
00019    POLICY_HND *user_hnd = NULL;
00020 
00021    fstring tmp;
00022    fstring input;
00023 
00024    char *pass1 = NULL;
00025    char *pass2 = NULL;
00026    
00027    int i;
00028 
00029    mem_ctx = talloc_init("cac_samgroup");
00030 
00031    hnd = cac_NewServerHandle(True);
00032 
00033    cac_SetAuthDataFn(hnd, cactest_GetAuthDataFn);
00034 
00035    cac_parse_cmd_line(argc, argv, hnd);
00036 
00037    if(!cac_Connect(hnd, NULL)) {
00038       fprintf(stderr, "Could not connect to server %s. Error: %s\n", hnd->server, nt_errstr(hnd->status));
00039       exit(-1);
00040    }
00041 
00042    struct SamOpenDomain sod;
00043    ZERO_STRUCT(sod);
00044 
00045    sod.in.access = MAXIMUM_ALLOWED_ACCESS; 
00046 
00047    if(!cac_SamOpenDomain(hnd, mem_ctx, &sod)) {
00048       fprintf(stderr, "Could not open domain. Error: %s\n", nt_errstr(hnd->status));
00049       goto done;
00050    }
00051 
00052    tmp[0] = 0x00;
00053    while(tmp[0] != 'q') {
00054       printf("\n");
00055       printf("[l]ist users\n");
00056       printf("[c]reate user\n");
00057       printf("[o]pen user\n");
00058       printf("[d]elete user\n");
00059       printf("[g]et user info\n");
00060       printf("[e]dit user info\n");
00061       printf("[r]ename user\n");
00062       printf("reset [p]assword\n");
00063       printf("[n] close user\n");
00064 
00065       printf("[q]uit\n\n");
00066       printf("Enter option: ");
00067       cactest_readline(stdin, tmp);
00068 
00069       printf("\n");
00070 
00071       switch(tmp[0]) {
00072          case 'c': /*create user*/
00073             if(user_hnd != NULL) {
00074                /*then we have an open handle.. close it*/
00075                cac_SamClose(hnd, mem_ctx, user_hnd);
00076                user_hnd = NULL;
00077             }
00078 
00079             printf("Enter user name: ");
00080             cactest_readline(stdin, input);
00081 
00082             ZERO_STRUCT(cu);
00083 
00084             cu.in.name      = talloc_strdup(mem_ctx, input);
00085             cu.in.dom_hnd   = sod.out.dom_hnd;
00086             cu.in.acb_mask  = ACB_NORMAL;
00087 
00088             if(!cac_SamCreateUser(hnd, mem_ctx, &cu)) {
00089                printf("Could not create user. Error: %s\n", nt_errstr(hnd->status));
00090             }
00091             else {
00092                printf("Created user %s with RID 0x%x\n", cu.in.name, cu.out.rid);
00093                user_hnd = cu.out.user_hnd;
00094             }
00095 
00096             break;
00097 
00098          case 'o': /*open group*/
00099             if(user_hnd != NULL) {
00100                /*then we have an open handle.. close it*/
00101                cac_SamClose(hnd, mem_ctx, user_hnd);
00102                user_hnd = NULL;
00103             }
00104 
00105             ZERO_STRUCT(ou);
00106 
00107             ou.in.dom_hnd = sod.out.dom_hnd;
00108             ou.in.access = MAXIMUM_ALLOWED_ACCESS;
00109 
00110             printf("Enter RID: 0x");
00111             scanf("%x", &ou.in.rid);
00112 
00113             if(!cac_SamOpenUser(hnd, mem_ctx, &ou)) {
00114                fprintf(stderr, "Could not open user. Error: %s\n", nt_errstr(hnd->status));
00115             }
00116             else {
00117                printf("Opened user\n");
00118                user_hnd = ou.out.user_hnd;
00119             }
00120 
00121             break;
00122 
00123          case 'l': /*list users*/
00124             ZERO_STRUCT(eu);
00125             eu.in.dom_hnd = sod.out.dom_hnd;
00126 
00127             while(cac_SamEnumUsers(hnd, mem_ctx, &eu)) {
00128                for(i = 0; i < eu.out.num_users; i++) {
00129                   printf("RID: 0x%x Name: %s\n", eu.out.rids[i], eu.out.names[i]);
00130                }
00131             }
00132 
00133             if(CAC_OP_FAILED(hnd->status)) {
00134                printf("Could not enumerate Users. Error: %s\n", nt_errstr(hnd->status));
00135             }
00136 
00137             break;
00138             
00139             break;
00140 
00141          case 'd': /*delete group*/
00142             if(!user_hnd) {
00143                printf("Must open group first!\n");
00144                break;
00145             }
00146 
00147             if(!cac_SamDeleteGroup(hnd, mem_ctx, user_hnd)) {
00148                fprintf(stderr, "Could not delete group. Error: %s\n", nt_errstr(hnd->status));
00149             }
00150             else {
00151                printf("Deleted group.\n");
00152                user_hnd = NULL;
00153             }
00154             break;
00155 
00156          
00157          case 'n':
00158             if(!user_hnd) {
00159                printf("Must open user first!\n");
00160                break;
00161             }
00162 
00163             if(!cac_SamClose(hnd, mem_ctx, user_hnd)) {
00164                printf("Could not user group\n");
00165                break;
00166             }
00167 
00168             user_hnd = NULL;
00169             break;
00170 
00171          case 'g': /*get user info*/
00172             if(!user_hnd) {
00173                printf("Must open user first!\n");
00174                break;
00175             }
00176 
00177             ZERO_STRUCT(gi);
00178             gi.in.user_hnd = ou.out.user_hnd;
00179 
00180             if(!cac_SamGetUserInfo(hnd, mem_ctx, &gi)) {
00181                printf("Could not get user info. Error: %s\n", nt_errstr(hnd->status));
00182             }
00183             else {
00184                printf("Retrieved User information:\n");
00185                print_cac_user_info(gi.out.info);
00186             } 
00187 
00188             break;
00189 
00190          case 'e': /*edit user info*/
00191             if(!user_hnd) {
00192                printf("Must Open user first!\n");
00193                break;
00194             }
00195 
00196             ZERO_STRUCT(gi);
00197             gi.in.user_hnd = ou.out.user_hnd;
00198             if(!cac_SamGetUserInfo(hnd, mem_ctx, &gi)) {
00199                printf("Could not get user info. Error: %s\n", nt_errstr(hnd->status));
00200                break;
00201             }
00202             
00203             edit_cac_user_info(mem_ctx, gi.out.info);
00204 
00205             printf("setting following info:\n");
00206             print_cac_user_info(gi.out.info);
00207 
00208             ZERO_STRUCT(si);
00209 
00210             si.in.user_hnd = user_hnd;
00211             si.in.info     = gi.out.info;
00212 
00213             if(!cac_SamSetUserInfo(hnd, mem_ctx, &si)) {
00214                printf("Could not set user info. Error: %s\n", nt_errstr(hnd->status));
00215             }
00216             else {
00217                printf("Done.\n");
00218             }
00219 
00220             break;
00221 
00222          case 'r': /*rename user*/
00223             if(!user_hnd) {
00224                printf("Must open user first!\n");
00225                break;
00226             }
00227 
00228             ZERO_STRUCT(ru);
00229 
00230             printf("Enter new username: ");
00231             cactest_readline(stdin, tmp);
00232 
00233             ru.in.user_hnd = user_hnd;
00234             ru.in.new_name = talloc_strdup(mem_ctx, tmp);
00235 
00236             if(!cac_SamRenameUser(hnd, mem_ctx, &ru)) {
00237                printf("Could not rename user. Error: %s\n", nt_errstr(hnd->status));
00238             }
00239             else {
00240                printf("Renamed user\n");
00241             }
00242 
00243             break;
00244 
00245          case 'p': /*reset password*/
00246 
00247             if(!user_hnd) {
00248                printf("Must open user first!\n");
00249                break;
00250             }
00251 
00252             do {
00253                if(pass1 && pass2) {
00254                   printf("Passwords do not match. Please try again\n");
00255                }
00256 
00257                pass1 = getpass("Enter new password: ");
00258                pass2 = getpass("Re-enter new password: ");
00259             } while(strncmp(pass1, pass2, MAX_PASS_LEN));
00260 
00261             ZERO_STRUCT(sp);
00262             sp.in.user_hnd = user_hnd;
00263             sp.in.password = talloc_strdup(mem_ctx, pass1);
00264 
00265             if(!cac_SamSetPassword(hnd, mem_ctx, &sp)) {
00266                printf("Could not set password. Error: %s\n", nt_errstr(hnd->status));
00267             }
00268             else {
00269                printf("Done.\n");
00270             }
00271 
00272             break;
00273 
00274          case 'q':
00275             break;
00276 
00277          default:
00278             printf("Invalid command\n");
00279       }
00280    }
00281 
00282    cac_SamClose(hnd, mem_ctx, sod.out.dom_hnd);
00283 
00284    if(user_hnd)
00285       cac_SamClose(hnd, mem_ctx, user_hnd);
00286 
00287 done:
00288    cac_FreeHandle(hnd);
00289 
00290    talloc_destroy(mem_ctx);
00291 
00292    return 0;
00293 }
00294 

© sourcejam.com 2005-2008