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