]> andersk Git - moira.git/blame - clients/moira/user.c
Don't skip over acl entries that are unreferenced strings; log an error
[moira.git] / clients / moira / user.c
CommitLineData
c441a31a 1/* $Id$
7ac48069 2 *
3 * This is the file user.c for the Moira Client, which allows users
59ec8dae 4 * to quickly and easily maintain most parts of the Moira database.
0a2c64cb 5 * It Contains: Functions for manipulating user information.
5eaef520 6 *
08345b74 7 * Created: 5/9/88
8 * By: Chris D. Peterson
9 *
7ac48069 10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
12 * <mit-copyright.h>.
08345b74 13 */
14
7ac48069 15#include <mit-copyright.h>
8defc06b 16#include <moira.h>
17#include <moira_site.h>
7ac48069 18#include "defs.h"
19#include "f_defs.h"
20#include "globals.h"
21
600b459e 22#include <sys/time.h>
23
1e80e2f4 24#include <ctype.h>
7ac48069 25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
ea0caf4a 29
30#include <krb.h>
7ac48069 31
32RCSID("$Header$");
33
34void CorrectCapitalization(char **name);
35char **AskUserInfo(char **info, Bool name);
600b459e 36struct mqelem *GetUserInfo(int type, char *name1, char *name2);
461c03b6 37
08345b74 38#define LOGIN 0
39#define UID 1
402461ad 40#define BY_NAME 2
075fe5bb 41#define CLASS 3
81701699 42#define ID 4
08345b74 43
d6db948b 44#ifdef ATHENA
45#define DEFAULT_SHELL "/bin/athena/tcsh"
46#else
07b2e652 47#define DEFAULT_SHELL "/bin/csh"
d6db948b 48#endif
07b2e652 49#define DEFAULT_CLASS "?"
50
51
32de30b0 52/* Function Name: UserState
53 * Description: Convert a numeric state into a descriptive string.
54 * Arguments: state value
55 * Returns: pointer to statically allocated string.
56 */
57
5eaef520 58static char *states[] = {
59 "Registerable (0)",
60 "Active (1)",
61 "Half Registered (2)",
62 "Deleted (3)",
63 "Not registerable (4)",
64 "Enrolled/Registerable (5)",
65 "Enrolled/Not Registerable (6)",
66 "Half Enrolled (7)"
67};
68
69static char *UserState(int state)
32de30b0 70{
5eaef520 71 static char buf[BUFSIZ];
78fce9fa 72
5eaef520 73 if (state < 0 || state >= US_END)
74 {
75 sprintf(buf, "Unknown (%d)", state);
76 return buf;
78fce9fa 77 }
5eaef520 78 return states[state];
32de30b0 79}
80
81
075fe5bb 82/* Function Name: PrintUserName
83 * Description: Print name of a user.
84 * Arguments: info - the information about a user.
85 * Returns: none.
86 */
87
5eaef520 88static void PrintUserName(char **info)
075fe5bb 89{
5eaef520 90 char buf[BUFSIZ], print_buf[BUFSIZ];
91 sprintf(buf, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
92 sprintf(print_buf, "%-40s User Name: %s", buf, info[U_NAME]);
93 Put_message(print_buf);
075fe5bb 94}
95
96/* Function Name: PrintUserInfo
97 * Description: Prints Information about a user.
98 * Arguments: info - an argument list with the user information
99 * in it.
100 * Returns: none
101 */
102
5eaef520 103static void PrintUserInfo(char **info)
075fe5bb 104{
5eaef520 105 char name[BUFSIZ], buf[BUFSIZ];
106 int status;
075fe5bb 107
5eaef520 108 sprintf(name, "%s, %s %s", info[U_LAST], info[U_FIRST], info[U_MIDDLE]);
109 sprintf(buf, "Login name: %-20s Full name: %s", info[U_NAME], name);
110 Put_message(buf);
111 sprintf(buf, "User id: %-23s Login shell %-10s Class: %s",
112 info[U_UID], info[U_SHELL], info[U_CLASS]);
113 Put_message(buf);
559fe7ab 114
d55429b2 115 sprintf(buf, "Account is: %-20s MIT ID number: %s",
116 UserState(atoi(info[U_STATE])), info[U_MITID]);
5eaef520 117 Put_message(buf);
c1c303a7 118 status = atoi(info[U_STATE]);
119 if (status == 0 || status == 2)
a5cdbea8 120 {
121 sprintf(buf, "User %s secure Account Coupon to register",
122 atoi(info[U_SECURE]) ? "needs" : "does not need");
123 Put_message(buf);
124 }
5eaef520 125 sprintf(buf, "Comments: %s", info[U_COMMENT]);
126 Put_message(buf);
127 sprintf(buf, MOD_FORMAT, info[U_MODBY], info[U_MODTIME], info[U_MODWITH]);
128 Put_message(buf);
075fe5bb 129}
130
131/* Function Name: SetUserDefaults
132 * Description: Sets the default values for add user.
133 * Arguments: info - a blank user info array of char *'s.
134 * Returns: args - the filled info structure.
135 */
136
5eaef520 137static char **SetUserDefaults(char **info)
075fe5bb 138{
7ac48069 139 info[U_NAME] = strdup(UNIQUE_LOGIN);
140 info[U_UID] = strdup(UNIQUE_UID);
141 info[U_SHELL] = strdup(DEFAULT_SHELL);
142 info[U_LAST] = strdup(DEFAULT_NONE);
143 info[U_FIRST] = strdup(DEFAULT_NONE);
144 info[U_MIDDLE] = strdup(DEFAULT_NONE);
145 info[U_STATE] = strdup(DEFAULT_NO);
146 info[U_MITID] = strdup(DEFAULT_NONE);
147 info[U_CLASS] = strdup(DEFAULT_CLASS);
148 info[U_COMMENT] = strdup("");
149 info[U_SIGNATURE] = strdup("");
150 info[U_SECURE] = strdup("0");
5eaef520 151 info[U_MODTIME] = info[U_MODBY] = info[U_MODWITH] = info[U_END] = NULL;
152 return info;
075fe5bb 153}
461c03b6 154
f0a54771 155
5eaef520 156/* Check that the supplied name follows the capitalization rules, and
f0a54771 157 * offer to correct it if not.
158 */
159
7ac48069 160void CorrectCapitalization(char **name)
f0a54771 161{
5eaef520 162 char temp_buf[BUFSIZ], fixname[BUFSIZ];
163
164 strcpy(fixname, *name);
165 FixCase(fixname);
166 if (strcmp(fixname, *name))
167 {
168 Put_message("You entered a name which does not follow the capitalization conventions.");
169 sprintf(temp_buf, "Correct it to \"%s\"", fixname);
170 if (YesNoQuestion(temp_buf, 1) == TRUE)
171 {
172 free(*name);
7ac48069 173 *name = strdup(fixname);
f0a54771 174 }
175 }
176}
177
178
08345b74 179/* Function Name: AskUserInfo.
5eaef520 180 * Description: This function askes the user for information about a
08345b74 181 * machine and saves it into a structure.
182 * Arguments: info - a pointer the the structure to put the info into.
183 * flags - Flags asking us which info we want.
184 * Returns: the args to pass to the query.
185 * NOTES: the return args are not necessarily in the correct order to
186 * use the #defined names (e.g args[UID] is not the uid anymore).
187 */
188
5eaef520 189char **AskUserInfo(char **info, Bool name)
08345b74 190{
c1c303a7 191 int i, state;
5eaef520 192 char temp_buf[BUFSIZ], *newname;
193
194 if (name)
195 {
196 sprintf(temp_buf, "\nChanging Attributes of user %s.\n", info[U_NAME]);
197 Put_message(temp_buf);
198 }
199 else
200 {
600b459e 201 struct mqelem *elem = NULL;
5eaef520 202 char *argv[3];
203
204 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
205 return NULL;
206 CorrectCapitalization(&info[U_LAST]);
207 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
208 return NULL;
209 CorrectCapitalization(&info[U_FIRST]);
210 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
211 return NULL;
212 CorrectCapitalization(&info[U_MIDDLE]);
213 argv[0] = info[U_FIRST];
214 argv[1] = info[U_LAST];
215 if (do_mr_query("get_user_account_by_name", 2, argv,
7ac48069 216 StoreInfo, &elem) == MR_SUCCESS)
5eaef520 217 {
218 Put_message("A user by that name already exists in the database.");
219 Loop(QueueTop(elem), PrintUserInfo);
220 Loop(QueueTop(elem), FreeInfo);
221 FreeQueue(elem);
222 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
223 return NULL;
ebd96e37 224 }
075fe5bb 225 }
5eaef520 226 if (name)
227 {
7ac48069 228 newname = strdup(info[U_NAME]);
5eaef520 229 if (GetValueFromUser("The new login name for this user", &newname) ==
230 SUB_ERROR)
231 return NULL;
ebd96e37 232 }
5eaef520 233 else if (GetValueFromUser("Login name for this user", &info[U_NAME]) ==
234 SUB_ERROR)
235 return NULL;
236
46a9277b 237 strcpy(temp_buf, info[U_UID]);
5eaef520 238 if (GetValueFromUser("User's UID", &info[U_UID]) == SUB_ERROR)
239 return NULL;
46a9277b 240 if (strcmp(info[U_UID], UNIQUE_UID) && strcmp(info[U_UID], temp_buf))
241 {
242 struct mqelem *elem = NULL;
243 if (do_mr_query("get_user_account_by_uid", 1, &info[U_UID],
244 StoreInfo, &elem) == MR_SUCCESS)
245 {
246 Put_message("A user with that uid already exists in the database.");
247 Loop(QueueTop(elem), PrintUserInfo);
248 Loop(QueueTop(elem), FreeInfo);
249 FreeQueue(elem);
250 if (YesNoQuestion("Add new user anyway", TRUE) != TRUE)
251 return NULL;
252 }
253 }
254
5eaef520 255 if (GetValueFromUser("User's shell", &info[U_SHELL]) == SUB_ERROR)
256 return NULL;
257 if (name)
258 {
259 if (GetValueFromUser("User's last name", &info[U_LAST]) == SUB_ERROR)
260 return NULL;
261 CorrectCapitalization(&info[U_LAST]);
262 if (GetValueFromUser("User's first name", &info[U_FIRST]) == SUB_ERROR)
263 return NULL;
264 CorrectCapitalization(&info[U_FIRST]);
265 if (GetValueFromUser("User's middle name", &info[U_MIDDLE]) == SUB_ERROR)
266 return NULL;
267 CorrectCapitalization(&info[U_MIDDLE]);
268 }
269 while (1)
270 {
271 int i;
272 if (GetValueFromUser("User's status (? for help)", &info[U_STATE]) ==
273 SUB_ERROR)
274 return NULL;
275 if (isdigit(info[U_STATE][0]))
276 break;
277 Put_message("Valid status numbers:");
278 for (i = 0; i < US_END; i++)
279 {
280 sprintf(temp_buf, " %d: %s", i, states[i]);
281 Put_message(temp_buf);
1e80e2f4 282 }
283 }
5eaef520 284 if (GetValueFromUser("User's MIT ID number", &info[U_MITID]) == SUB_ERROR)
285 return NULL;
286 RemoveHyphens(info[U_MITID]);
287 if (GetTypeFromUser("User's MIT Year (class)", "class", &info[U_CLASS]) ==
288 SUB_ERROR)
289 return NULL;
290 if (GetValueFromUser("Comments", &info[U_COMMENT]) == SUB_ERROR)
291 return NULL;
292
c1c303a7 293 state = atoi(info[U_STATE]);
294 if (!name || state == 0 || state == 2)
5eaef520 295 {
a5cdbea8 296 if (YesNoQuestion("User needs secure Account Coupon to register",
297 atoi(info[U_SECURE]) ? TRUE : FALSE) == FALSE)
298 {
299 free(info[U_SECURE]);
300 info[U_SECURE] = strdup("0");
301 }
302 else
303 {
304 free(info[U_SECURE]);
305 info[U_SECURE] = strdup("1");
306 }
559fe7ab 307 }
308
7ac48069 309 info[U_SIGNATURE] = strdup("");
b8322cb3 310
5eaef520 311 FreeAndClear(&info[U_MODTIME], TRUE);
312 FreeAndClear(&info[U_MODBY], TRUE);
313 FreeAndClear(&info[U_MODWITH], TRUE);
08345b74 314
5eaef520 315 /*
316 * Slide the newname into the #2 slot, this screws up all future references
317 * to this list, since we slip the pointer into a info list it gets freed
318 * when the rest of the list gets freed.
319 */
320 if (name)
321 SlipInNewName(info, newname);
08345b74 322
5eaef520 323 return info;
08345b74 324}
325
326/* Function Name: GetUserInfo
327 * Description: Stores the user information in a queue.
328 * Arguments: type - type of field given to get info, one of:
402461ad 329 * LOGIN, UID, BY_NAME, CLASS.
08345b74 330 * name1 - name of thing specified by type (wildcards okay)
331 * name2 - other name, only used in get user by first and last.
332 * (wildcards okay).
333 * Returns: the first element of the queue containing the user info.
dcfa270a 334 *
08345b74 335 */
336
600b459e 337struct mqelem *GetUserInfo(int type, char *name1, char *name2)
08345b74 338{
5eaef520 339 char *args[2];
44d12d58 340 int status;
600b459e 341 struct mqelem *elem = NULL;
08345b74 342
5eaef520 343 switch (type)
344 {
08345b74 345 case LOGIN:
5eaef520 346 args[0] = name1;
347 if ((status = do_mr_query("get_user_account_by_login", 1, args,
7ac48069 348 StoreInfo, &elem)))
5eaef520 349 {
350 com_err(program_name, status,
351 " when attempting to get_user_account_by_login.");
352 return NULL;
08345b74 353 }
5eaef520 354 break;
08345b74 355 case UID:
5eaef520 356 args[0] = name1;
357 if ((status = do_mr_query("get_user_account_by_uid", 1, args,
7ac48069 358 StoreInfo, &elem)))
5eaef520 359 {
360 com_err(program_name, status,
361 " when attempting to get_user_account_by_uid.");
362 return NULL;
08345b74 363 }
5eaef520 364 break;
402461ad 365 case BY_NAME:
5eaef520 366 args[0] = name1;
367 args[1] = name2;
368 if ((status = do_mr_query("get_user_account_by_name", 2, args,
7ac48069 369 StoreInfo, &elem)))
5eaef520 370 {
371 com_err(program_name, status,
372 " when attempting to get_user_account_by_name.");
373 return NULL;
08345b74 374 }
5eaef520 375 break;
08345b74 376 case CLASS:
5eaef520 377 args[0] = name1;
378 if ((status = do_mr_query("get_user_account_by_class", 1, args,
7ac48069 379 StoreInfo, &elem)))
5eaef520 380 {
381 com_err(program_name, status,
382 " when attempting to get_user_account_by_class.");
383 return NULL;
08345b74 384 }
5eaef520 385 break;
81701699 386 case ID:
5eaef520 387 args[0] = name1;
388 if ((status = do_mr_query("get_user_account_by_id", 1, args,
7ac48069 389 StoreInfo, &elem)))
5eaef520 390 {
391 com_err(program_name, status,
392 " when attempting to get_user_account_by_id.");
393 return NULL;
81701699 394 }
5eaef520 395 break;
08345b74 396 }
5eaef520 397 return QueueTop(elem) ;
08345b74 398}
399
075fe5bb 400/* Function Name: AddNewUser
401 * Description: Adds a new user to the database.
402 * Arguments: none.
08345b74 403 * Returns: DM_NORMAL.
404 */
405
7ac48069 406int AddNewUser(int argc, char **argv)
08345b74 407{
44d12d58 408 int status;
5eaef520 409 char **args, *info[MAX_ARGS_SIZE];
075fe5bb 410
5eaef520 411 if (!(args = AskUserInfo(SetUserDefaults(info), FALSE)))
412 {
413 Put_message("Aborted.");
414 return DM_NORMAL;
e4f91beb 415 }
5eaef520 416 if ((status = do_mr_query("add_user_account", CountArgs(args),
7ac48069 417 args, NULL, NULL)))
5eaef520 418 com_err(program_name, status, " in add_user_account");
419 else
420 Put_message("New user added to database.");
421 FreeInfo(args);
422 return DM_NORMAL;
08345b74 423}
424
425
075fe5bb 426/* Function Name: GetLoginName
427 * Description: Asks the user for a login name and reserves
428 * it with kerberous.
429 * Arguments: none.
430 * Returns: a malloced login name for the user.
08345b74 431 */
432
5eaef520 433static char *GetLoginName(void)
08345b74 434{
5eaef520 435 char *name;
075fe5bb 436
7ac48069 437 name = strdup("");
5eaef520 438 if (GetValueFromUser("Login name for this user? ", &name) == SUB_ERROR)
439 return NULL;
440 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
441 return name;
075fe5bb 442}
443
444
445/* Function Name: ChooseUser
446 * Description: Choose a user from a list and return the uid.
447 * Arguments: top - a queue of user information.
448 * Returns: uid - the malloced uid of the user that was chosen.
449 */
08345b74 450
600b459e 451static char *ChooseUser(struct mqelem *elem)
075fe5bb 452{
5eaef520 453 while (elem)
454 {
7ac48069 455 char **info = elem->q_data;
5eaef520 456 PrintUserInfo(info);
457 switch (YesNoQuitQuestion("Is this the user you want (y/n/q)", FALSE))
458 {
075fe5bb 459 case TRUE:
7ac48069 460 return strdup(info[U_UID]);
075fe5bb 461 case FALSE:
5eaef520 462 break;
075fe5bb 463 default: /* quit or ^C. */
5eaef520 464 return NULL;
08345b74 465 }
5eaef520 466 elem = elem->q_forw;
08345b74 467 }
5eaef520 468 return NULL;
08345b74 469}
470
075fe5bb 471/* Function Name: GetUidNumberFromName
472 * Description: Gets the users uid number, from the name.
473 * Arguments: none.
474 * Returns: uid - a malloced string containing the uid.
08345b74 475 */
476
5eaef520 477static char *GetUidNumberFromName(void)
08345b74 478{
5eaef520 479 char *args[5], *uid, first[BUFSIZ], last[BUFSIZ];
44d12d58 480 int status;
600b459e 481 struct mqelem *top = NULL;
5eaef520 482
483 if (!Prompt_input("First Name: ", first, BUFSIZ))
484 return NULL;
485 if (!Prompt_input("Last Name: ", last, BUFSIZ))
486 return NULL;
487 FixCase(first);
488 FixCase(last);
489
490 args[0] = first;
491 args[1] = last;
492
493 switch ((status = do_mr_query("get_user_account_by_name", 2, args,
7ac48069 494 StoreInfo, &top)))
5eaef520 495 {
8defc06b 496 case MR_SUCCESS:
5eaef520 497 break;
8defc06b 498 case MR_NO_MATCH:
5eaef520 499 Put_message("There is no user in the database with that name.");
500 return NULL;
075fe5bb 501 default:
5eaef520 502 com_err(program_name, status, " in get_account_user_by_name.");
503 return NULL;
08345b74 504 }
5eaef520 505
506 top = QueueTop(top);
507 if (QueueCount(top) == 1) /* This is a unique name. */
508 {
7ac48069 509 char **info = top->q_data;
5eaef520 510 Put_message("User ID Number retrieved for the user: ");
511 Put_message("");
512 PrintUserName(info);
7ac48069 513 uid = strdup(info[U_UID]);
5eaef520 514 FreeQueue(top);
7ac48069 515 return strdup(uid);
075fe5bb 516 }
08345b74 517
5eaef520 518 Put_message("That name is not unique, choose the user that you want.");
519 uid = ChooseUser(top);
520 FreeQueue(top);
521 return uid;
08345b74 522}
523
075fe5bb 524/* Function Name: SetUserPassword
525 * Description: Set the new kerberos password for this user.
526 * Arguments: name - kerberos principle name for this user, (login name).
527 * Returns: none.
08345b74 528 */
529
5eaef520 530static void SetUserPassword(char *name)
08345b74 531{
5eaef520 532 name = name; /* make saber happy. */
533 Put_message("Kerberos password not changed, code non-existant.");
534 /* clever message to call account_admin, if this fails. */
075fe5bb 535}
08345b74 536
075fe5bb 537/* Function Name: GiveBackLogin
538 * Description: Gives back previously reserved kerberous principle.
539 * Arguments: name - principle to give back.
540 * Returns: void.
08345b74 541 */
542
5eaef520 543static void GiveBackLogin(char *name)
08345b74 544{
5eaef520 545 name = name; /* make saber happy. */
546 Put_message("kerberos code not implimented, name not given back.");
547 /* send mail to db maintainer if this fails. */
08345b74 548}
549
075fe5bb 550/* Function Name: RegisterUser
551 * Description: This function registers a user.
552 * Arguments: none.
553 * Returns: DM_NORMAL.
08345b74 554 */
555
7ac48069 556int RegisterUser(int argc, char **argv)
08345b74 557{
5eaef520 558 char *args[MAX_ARGS_SIZE];
559 char *login, *fstype = NULL;
560 char temp_buf[BUFSIZ];
44d12d58 561 int status;
5eaef520 562
563 Put_message("This function has NO kerberos support, so strange things");
564 Put_message("may happen if you use it to register a user.");
565
566 switch (YesNoQuestion("Do you know the users UID Number (y/n)", FALSE))
567 {
075fe5bb 568 case TRUE:
5eaef520 569 Prompt_input("What is the UID number of the user? ", temp_buf, BUFSIZ);
7ac48069 570 args[0] = strdup(temp_buf);
5eaef520 571 break;
075fe5bb 572 case FALSE:
5eaef520 573 if (!(args[0] = GetUidNumberFromName()))
574 return DM_NORMAL;
575 break;
075fe5bb 576 default:
5eaef520 577 return DM_NORMAL;
461c03b6 578 }
075fe5bb 579
5eaef520 580 sprintf(temp_buf, "u%s", args[0]);
7ac48069 581 login = strdup(temp_buf);
5eaef520 582 if ((GetValueFromUser("Login name for this user? ", &login) == SUB_ERROR) ||
583 (GetFSTypes(&fstype, FALSE) == SUB_ERROR))
584 {
585 args[1] = login;
586 FreeInfo(args); /* This work because the NULL temination is ok. */
587 return DM_NORMAL;
075fe5bb 588 }
5eaef520 589 Put_message("KERBEROS code not added, did not reserve name with kerberos.");
590 args[1] = login;
591 args[2] = fstype;
592 args[3] = NULL;
593
594 switch ((status = do_mr_query("register_user", CountArgs(args),
7ac48069 595 args, NULL, NULL)))
5eaef520 596 {
8defc06b 597 case MR_SUCCESS:
5eaef520 598 sprintf(temp_buf, "User %s successfully registered.", login);
599 Put_message(temp_buf);
600 SetUserPassword(login);
601 break;
8defc06b 602 case MR_IN_USE:
5eaef520 603 GiveBackLogin(login);
604 sprintf(temp_buf, "The username %s is already in use.", login);
605 Put_message(temp_buf);
606 break;
075fe5bb 607 default:
5eaef520 608 com_err(program_name, status, " in register_user");
609 break;
075fe5bb 610 }
5eaef520 611 FreeInfo(args);
612 return DM_NORMAL;
08345b74 613}
614
075fe5bb 615/* Function Name: RealUpdateUser
616 * Description: actuall updates the user information.
617 * Arguments: info - all current information for the user fields.
618 * junk - an UNUSED boolean.
619 * Returns: none.
08345b74 620 */
621
5eaef520 622static void RealUpdateUser(char **info, Bool junk)
08345b74 623{
44d12d58 624 int status;
5eaef520 625 char error_buf[BUFSIZ];
626 char **args = AskUserInfo(info, TRUE);
627
628 if (!args)
629 {
630 Put_message("Aborted.");
631 return;
e4f91beb 632 }
5eaef520 633 if ((status = do_mr_query("update_user_account", CountArgs(args),
7ac48069 634 args, NULL, NULL)))
5eaef520 635 {
636 com_err(program_name, status, " in ModifyFields");
637 sprintf(error_buf, "User %s not updated due to errors.", info[NAME]);
638 Put_message(error_buf);
08345b74 639 }
08345b74 640}
641
075fe5bb 642/* Function Name: UpdateUser
643 * Description: Modify some of the information about a user.
644 * Arguments: argc, argv - login name of the user in argv[1].
08345b74 645 * Returns: DM_NORMAL.
646 */
647
5eaef520 648int UpdateUser(int argc, char **argv)
08345b74 649{
600b459e 650 struct mqelem *elem;
5eaef520 651
652 elem = GetUserInfo(LOGIN, argv[1], NULL);
653 QueryLoop(elem, NullPrint, RealUpdateUser, "Update the user");
08345b74 654
5eaef520 655 FreeQueue(elem);
656 return DM_NORMAL;
08345b74 657}
658
b3e25186 659/* Function Name: RealDeactivateUser
660 * Description: sets the user's status to 3.
661 * Arguments: info - all current information for the user fields
662 * one_item - indicates the user hasn't been queried yet
663 * Returns: none.
664 */
665
5eaef520 666static void RealDeactivateUser(char **info, Bool one_item)
b3e25186 667{
44d12d58 668 int status;
5eaef520 669 char txt_buf[BUFSIZ];
670 char *qargs[2], **args;
600b459e 671 struct mqelem *elem = NULL;
5eaef520 672
673 if (one_item)
674 {
675 sprintf(txt_buf, "Deactivate user %s (y/n)", info[NAME]);
676 if (YesNoQuestion(txt_buf, FALSE) != TRUE)
677 return;
b3e25186 678 }
679
5eaef520 680 qargs[0] = info[NAME];
681 qargs[1] = "3";
7ac48069 682 if ((status = do_mr_query("update_user_status", 2, qargs, NULL, NULL)))
5eaef520 683 {
684 com_err(program_name, status, " in update_user_status");
685 sprintf(txt_buf, "User %s not deactivated due to errors.", info[NAME]);
686 Put_message(txt_buf);
687 }
688 else if (YesNoQuestion("Also deactivate matching list and filesystem (y/n)",
689 FALSE) == TRUE)
690 {
691 status = do_mr_query("get_list_info", 1, &(info[NAME]), StoreInfo,
7ac48069 692 &elem);
5eaef520 693 if (status == MR_SUCCESS)
694 {
7ac48069 695 args = QueueTop(elem)->q_data;
5eaef520 696 free(args[L_ACTIVE]);
7ac48069 697 args[L_ACTIVE] = strdup("0");
5eaef520 698 FreeAndClear(&args[L_MODTIME], TRUE);
699 FreeAndClear(&args[L_MODBY], TRUE);
700 FreeAndClear(&args[L_MODWITH], TRUE);
701 SlipInNewName(args, args[L_NAME]);
702 if ((status = do_mr_query("update_list", CountArgs(args), args,
7ac48069 703 NULL, NULL)))
5eaef520 704 {
705 com_err(program_name, status, " updating list, "
706 "not deactivating list or filesystem");
707 FreeInfo(args);
708 FreeQueue(elem);
709 return;
28da9b65 710 }
5eaef520 711 FreeInfo(args);
712 FreeQueue(elem);
713 elem = NULL;
714 }
715 else if (status != MR_NO_MATCH)
716 {
717 com_err(program_name, status, " getting list info, "
718 "not deactivating list or filesystem");
719 return;
00d4c0ba 720 }
28da9b65 721
5eaef520 722 if ((status = do_mr_query("get_filesys_by_label", 1, &(info[NAME]),
7ac48069 723 StoreInfo, &elem)))
5eaef520 724 {
725 com_err(program_name, status, " getting filsys info, "
726 "not deactivating filesystem");
5eaef520 727 return;
00d4c0ba 728 }
7ac48069 729 args = QueueTop(elem)->q_data;
5eaef520 730 free(args[FS_TYPE]);
7ac48069 731 args[FS_TYPE] = strdup("ERR");
5eaef520 732 free(args[FS_COMMENTS]);
7ac48069 733 args[FS_COMMENTS] = strdup("Locker disabled; call 3-1325 for help");
5eaef520 734 FreeAndClear(&args[FS_MODTIME], TRUE);
735 FreeAndClear(&args[FS_MODBY], TRUE);
736 FreeAndClear(&args[FS_MODWITH], TRUE);
737 SlipInNewName(args, args[FS_NAME]);
738 if ((status = do_mr_query("update_filesys", CountArgs(args), args,
7ac48069 739 NULL, NULL)))
5eaef520 740 {
741 com_err(program_name, status, " updating filesystem, "
742 "not deactivating filesystem");
743 FreeInfo(args);
744 FreeQueue(elem);
745 return;
00d4c0ba 746 }
5eaef520 747 FreeInfo(args);
748 FreeQueue(elem);
b3e25186 749 }
750}
751
752
753/* Function Name: DeactivateUser
754 * Description: sets the user's status to 3.
755 * Arguments: argc, argv - login name of the user in argv[1].
756 * Returns: DM_NORMAL.
757 */
758
5eaef520 759int DeactivateUser(int argc, char **argv)
b3e25186 760{
600b459e 761 struct mqelem *elem;
b3e25186 762
5eaef520 763 elem = GetUserInfo(LOGIN, argv[1], NULL);
764 QueryLoop(elem, NullPrint, RealDeactivateUser, "Deactivate user");
765
766 FreeQueue(elem);
767 return DM_NORMAL;
b3e25186 768}
769
770
075fe5bb 771/* ------------------------- Top Menu ------------------------- */
772
773/* DeleteUser() in delete.c */
461c03b6 774
075fe5bb 775/* Function Name: DeleteUserByUid
776 * Description: Deletes the user given a uid number.
777 * Arguments: argc, argv - uid if user in argv[1].
08345b74 778 * Returns: DM_NORMAL.
5eaef520 779 * NOTES: This just gets the username from the mr server
075fe5bb 780 * and performs a DeleteUser().
08345b74 781 */
782
5eaef520 783int DeleteUserByUid(int argc, char **argv)
08345b74 784{
5eaef520 785 int status;
600b459e 786 struct mqelem *elem = NULL;
5eaef520 787 char **info;
788
789 if (!ValidName(argv[1]))
790 return DM_NORMAL;
791
792 if ((status = do_mr_query("get_user_account_by_uid", 1, argv + 1, StoreInfo,
7ac48069 793 &elem)))
5eaef520 794 com_err(program_name, status, " in get_user_account_by_uid");
795
7ac48069 796 info = elem->q_data;
5eaef520 797 argv[1] = info[U_NAME];
798
799 DeleteUser(argc, argv);
800 return DM_NORMAL;
801}
08345b74 802
075fe5bb 803/* ------------------------- Show User Information ------------------------- */
08345b74 804
805/* Function Name: ShowUserByLogin
806 * Description: Shows user information given a login name.
807 * Arguments: argc, argv - login name in argv[1].
808 * Returns: DM_NORMAL
809 */
810
5eaef520 811int ShowUserByLogin(int argc, char *argv[])
08345b74 812{
600b459e 813 struct mqelem *top, *elem;
08345b74 814
5eaef520 815 elem = top = GetUserInfo(LOGIN, argv[1], NULL);
816 Loop(elem, PrintUserInfo);
08345b74 817
5eaef520 818 FreeQueue(top);
819 return DM_NORMAL;
08345b74 820}
821
822/* Function Name: RetrieveUserByName
823 * Description: Show information on a user give fist and/or last name.
824 * Arguments: argc, argv - argv[1] - first name.
825 * argv[2] - last name.
826 * Returns: DM_NORMAL.
827 */
828
5eaef520 829int ShowUserByName(int argc, char *argv[])
08345b74 830{
600b459e 831 struct mqelem *top;
5eaef520 832 char buf[BUFSIZ];
08345b74 833
5eaef520 834 top = GetUserInfo(BY_NAME, argv[1], argv[2]);
08345b74 835
5eaef520 836 if (!top) /* if there was an error then return. */
837 return DM_NORMAL;
08345b74 838
5eaef520 839 if (!PromptWithDefault("Print full information, or just the names (f/n)?",
840 buf, 2, "f"))
841 return DM_NORMAL;
08345b74 842
5eaef520 843 switch (buf[0])
844 {
075fe5bb 845 case 'F':
846 case 'f':
5eaef520 847 Loop(top, PrintUserInfo);
848 break;
075fe5bb 849 case 'N':
850 case 'n':
5eaef520 851 Loop(top, PrintUserName);
852 break;
075fe5bb 853 }
5eaef520 854
855 FreeQueue(top);
856 return DM_NORMAL;
08345b74 857}
858
859/* Function Name: ShowUserByClass
860 * Description: Shows real and login names of all users in class.
861 * Arguments: argc, argv - argv[1] contains the class.
862 * Returns: none.
863 */
864
5eaef520 865int ShowUserByClass(int argc, char **argv)
08345b74 866{
600b459e 867 struct mqelem *top;
08345b74 868
5eaef520 869 if (YesNoQuestion("This will take a long time. Are you sure", 0) == FALSE)
870 return DM_NORMAL;
871 top = GetUserInfo(CLASS, argv[1], NULL);
872 Loop(top, PrintUserName);
08345b74 873
5eaef520 874 FreeQueue(top);
875 return DM_NORMAL;
08345b74 876}
1e80e2f4 877
878
81701699 879/* Function Name: ShowUserById
880 * Description: Shows user information given an ID number.
881 * Arguments: argc, argv - ID number in argv[1].
882 * Returns: DM_NORMAL
883 */
884
5eaef520 885int ShowUserById(int argc, char *argv[])
81701699 886{
600b459e 887 struct mqelem *top, *elem;
81701699 888
5eaef520 889 elem = top = GetUserInfo(ID, argv[1], NULL);
890 Loop(elem, PrintUserInfo);
81701699 891
5eaef520 892 FreeQueue(top);
893 return DM_NORMAL;
81701699 894}
895
896
1e80e2f4 897/* Function Name: GetKrbmap
898 * Description: Shows user <-> Kerberos mappings
899 * Arguments: argc, argv - argv[1] contains the user login name,
900 * argv[2] contains the principal
901 * Returns: none.
902 */
903
5eaef520 904int GetKrbmap(int argc, char **argv)
1e80e2f4 905{
5eaef520 906 int stat;
600b459e 907 struct mqelem *elem = NULL, *top;
5eaef520 908 char buf[BUFSIZ];
909
910 if ((stat = do_mr_query("get_kerberos_user_map", 2, &argv[1],
7ac48069 911 StoreInfo, &elem)))
5eaef520 912 {
913 com_err(program_name, stat, " in GetKrbMap.");
914 return DM_NORMAL;
1e80e2f4 915 }
916
5eaef520 917 top = elem = QueueTop(elem);
918 Put_message("");
919 while (elem)
920 {
7ac48069 921 char **info = elem->q_data;
5eaef520 922 sprintf(buf, "User: %-9s Principal: %s",
923 info[KMAP_USER], info[KMAP_PRINCIPAL]);
924 Put_message(buf);
925 elem = elem->q_forw;
1e80e2f4 926 }
927
5eaef520 928 FreeQueue(QueueTop(top));
929 return DM_NORMAL;
1e80e2f4 930}
931
932
933/* Function Name: AddKrbmap
934 * Description: Add a new user <-> Kerberos mapping
935 * Arguments: argc, argv - argv[1] contains the user login name,
936 * argv[2] contains the principal
937 * Returns: none.
938 */
939
5eaef520 940int AddKrbmap(int argc, char **argv)
1e80e2f4 941{
5eaef520 942 int stat;
1e80e2f4 943
5eaef520 944 if (!strchr(argv[KMAP_PRINCIPAL + 1], '@'))
945 {
946 Put_message("Please specify a realm for the kerberos principal.");
947 return DM_NORMAL;
1e80e2f4 948 }
5eaef520 949 if ((stat = do_mr_query("add_kerberos_user_map", 2, &argv[1],
7ac48069 950 NULL, NULL)))
5eaef520 951 {
952 com_err(program_name, stat, " in AddKrbMap.");
953 if (stat == MR_EXISTS)
954 Put_message("No user or principal may have more than one mapping.");
1e80e2f4 955 }
5eaef520 956 return DM_NORMAL;
1e80e2f4 957}
958
959
960/* Function Name: DeleteKrbmap
961 * Description: Remove a user <-> Kerberos mapping
962 * Arguments: argc, argv - argv[1] contains the user login name,
963 * argv[2] contains the principal
964 * Returns: none.
965 */
966
5eaef520 967int DeleteKrbmap(int argc, char **argv)
1e80e2f4 968{
5eaef520 969 int stat;
1e80e2f4 970
5eaef520 971 if ((stat = do_mr_query("delete_kerberos_user_map", 2, &argv[1],
7ac48069 972 NULL, NULL)))
5eaef520 973 com_err(program_name, stat, " in DeleteKrbMap.");
974 return DM_NORMAL;
1e80e2f4 975}
This page took 0.329032 seconds and 5 git commands to generate.