4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * user.c: support for the scadm useradd, userdel, usershow, userpassword,
31 * userperm options (administration of service processor users)
39 #include <time.h> /* required by librsc.h */
45 static void ADM_Get_Password(char *password
);
46 static void ADM_Destroy_Password(char *password
);
47 static void max_username();
48 static void malformed_username();
49 static void wrong_response();
50 static void no_user();
51 static void no_info();
52 static void userperm_usage();
53 static void show_header();
54 static void cleanup();
57 /* Globals so that exit routine can clean up echo */
58 static int echoOff
= 0;
59 static struct termios oldOpts
;
62 char DataBuffer
[DP_MAX_MSGLEN
];
68 ADM_Process_useradd(int argc
, char *argv
[])
70 static data_buffer_t dataBuffer
;
72 struct timespec Timeout
;
73 dp_user_adm_t
*admMessage
;
74 dp_user_adm_r_t
*admResponse
;
79 (void) fprintf(stderr
, "\n%s\n\n",
80 gettext("USAGE: scadm useradd <username>"));
86 if (strlen(argv
[2]) > DP_USER_NAME_SIZE
) {
91 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
92 userName
= (char *)(&((char *)admMessage
)[sizeof (dp_user_adm_t
)]);
93 admMessage
->command
= DP_USER_CMD_ADD
;
94 (void) strcpy(userName
, argv
[2]);
96 Message
.type
= DP_USER_ADM
;
97 Message
.len
= sizeof (dp_user_adm_t
) + strlen(userName
) + 1;
98 Message
.data
= admMessage
;
102 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
103 ADM_Recv(&Message
, &Timeout
, DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
105 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
106 if (admResponse
->command
!= DP_USER_CMD_ADD
) {
111 if (admResponse
->status
== DP_ERR_USER_FULL
) {
112 (void) fprintf(stderr
, "\n%s\n\n",
113 gettext("scadm: all user slots are full"));
115 } else if (admResponse
->status
== DP_ERR_USER_THERE
) {
116 (void) fprintf(stderr
, "\n%s\n\n",
117 gettext("scadm: user already exists"));
119 } else if (admResponse
->status
== DP_ERR_USER_WARNING
) {
120 (void) fprintf(stderr
, "\n%s\n\n",
121 gettext("scadm: username did not start with letter\n"
122 " or did not contain lower case letter\n"));
124 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
125 malformed_username();
127 } else if (admResponse
->status
!= 0) {
128 (void) fprintf(stderr
, "\n%s\n\n",
129 gettext("scadm: couldn't add user"));
138 ADM_Process_userdel(int argc
, char *argv
[])
140 static data_buffer_t dataBuffer
;
142 struct timespec Timeout
;
143 dp_user_adm_t
*admMessage
;
144 dp_user_adm_r_t
*admResponse
;
149 (void) fprintf(stderr
, "\n%s\n\n",
150 gettext("USAGE: scadm userdel <username>"));
156 if (strlen(argv
[2]) > DP_USER_NAME_SIZE
) {
161 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
162 userName
= (char *)(&((char *)admMessage
)[sizeof (dp_user_adm_t
)]);
163 admMessage
->command
= DP_USER_CMD_DEL
;
164 (void) strcpy(userName
, argv
[2]);
166 Message
.type
= DP_USER_ADM
;
167 Message
.len
= sizeof (dp_user_adm_t
) + strlen(userName
) + 1;
168 Message
.data
= admMessage
;
172 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
173 ADM_Recv(&Message
, &Timeout
, DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
175 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
176 if (admResponse
->command
!= DP_USER_CMD_DEL
) {
181 if (admResponse
->status
== DP_ERR_USER_NONE
) {
184 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
185 malformed_username();
187 } else if (admResponse
->status
!= 0) {
188 (void) fprintf(stderr
, "\n%s\n\n",
189 gettext("scadm: couldn't delete user"));
198 ADM_Process_usershow(int argc
, char *argv
[])
200 static data_buffer_t dataBuffer
;
202 struct timespec Timeout
;
203 dp_user_adm_t
*admMessage
;
204 dp_user_adm_r_t
*admResponse
;
212 if ((argc
!= 2) && (argc
!= 3)) {
213 (void) fprintf(stderr
, "\n%s\n\n",
214 gettext("USAGE: scadm usershow [username]"));
221 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
222 admMessage
->command
= DP_USER_CMD_SHOW
;
223 Message
.type
= DP_USER_ADM
;
224 Message
.data
= admMessage
;
226 if (strlen(argv
[2]) > DP_USER_NAME_SIZE
) {
230 userName
= (char *)(&((char *)admMessage
)[
231 sizeof (dp_user_adm_t
)]);
232 (void) strcpy(userName
, argv
[2]);
233 admMessage
->parm
= DP_USER_SHOW_USERNAME
;
234 Message
.len
= sizeof (dp_user_adm_t
) + strlen(userName
) + 1;
238 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
239 ADM_Recv(&Message
, &Timeout
,
240 DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
242 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
243 if (admResponse
->command
!= DP_USER_CMD_SHOW
) {
248 if (admResponse
->status
== DP_ERR_USER_NONE
) {
251 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
252 malformed_username();
254 } else if (admResponse
->status
!= 0) {
259 userName
= &(((char *)admResponse
)[
260 sizeof (dp_user_adm_r_t
)]);
261 permissions
= &userName
[strlen(userName
)+1];
262 passwd
= &permissions
[strlen(permissions
)+1];
264 (void) printf(" %-16s %-15s ", userName
, permissions
);
265 if (strncmp(passwd
, "Assigned", 12) == 0) {
266 (void) printf("%s\n\n", gettext("Assigned"));
267 } else if (strncmp(passwd
, "None", 12) == 0) {
268 (void) printf("%s\n\n", gettext("None"));
270 (void) printf("%-12s\n\n", passwd
);
275 for (index
= 1; index
<= DP_USER_MAX
; index
++) {
276 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
277 admMessage
->command
= DP_USER_CMD_SHOW
;
278 admMessage
->parm
= index
;
280 Message
.type
= DP_USER_ADM
;
281 Message
.data
= admMessage
;
282 Message
.len
= sizeof (dp_user_adm_t
);
286 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
287 ADM_Recv(&Message
, &Timeout
,
288 DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
290 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
291 if (admResponse
->command
!= DP_USER_CMD_SHOW
) {
296 if (admResponse
->status
== DP_ERR_USER_NONE
) {
299 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
300 malformed_username();
302 } else if (admResponse
->status
!= 0) {
307 userName
= &(((char *)admResponse
)[
308 sizeof (dp_user_adm_r_t
)]);
309 permissions
= &userName
[strlen(userName
)+1];
310 passwd
= &permissions
[strlen(permissions
)+1];
311 (void) printf(" %-16s %-15s ",
312 userName
, permissions
);
313 if (strncmp(passwd
, "Assigned", 12) == 0) {
314 (void) printf("%s\n", gettext("Assigned"));
315 } else if (strncmp(passwd
, "None", 12) == 0) {
316 (void) printf("%s\n", gettext("None"));
318 (void) printf("%-12s\n", passwd
);
329 ADM_Process_userpassword(int argc
, char *argv
[])
331 static data_buffer_t dataBuffer
;
333 struct timespec Timeout
;
334 dp_user_adm_t
*admMessage
;
335 dp_user_adm_r_t
*admResponse
;
341 /* Try to set password up to 3 times on Malformed password */
345 (void) fprintf(stderr
, "\n%s\n\n",
346 gettext("USAGE: scadm userpassword <username>"));
352 if (strlen(argv
[2]) > DP_USER_NAME_SIZE
) {
357 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
358 admMessage
->command
= DP_USER_CMD_PASSWORD
;
359 userName
= (&((char *)admMessage
)[sizeof (dp_user_adm_t
)]);
360 (void) strcpy(userName
, argv
[2]);
361 password
= (&((char *)admMessage
)[sizeof (dp_user_adm_t
) +
362 strlen(userName
) + 1]);
365 ADM_Get_Password(password
);
367 Message
.type
= DP_USER_ADM
;
368 Message
.len
= sizeof (dp_user_adm_t
) + strlen(userName
) +
369 strlen(password
) + 2;
370 Message
.data
= admMessage
;
373 ADM_Destroy_Password(password
);
375 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
376 ADM_Recv(&Message
, &Timeout
,
377 DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
379 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
380 if (admResponse
->command
!= DP_USER_CMD_PASSWORD
) {
385 if (admResponse
->status
== DP_ERR_USER_NONE
) {
388 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
389 malformed_username();
391 } else if (admResponse
->status
== DP_ERR_USER_PASSWD
) {
392 (void) fprintf(stderr
, "\n%s\n\n",
393 gettext("scadm: malformed password\n"
394 " A valid password is between 6 and 8 "
396 " has at least two alphabetic characters, "
398 " least one numeric or special character. "
400 " password must differ from the user's "
402 " and any reverse or circular shift of that "
411 } else if (admResponse
->status
!= 0) {
412 (void) fprintf(stderr
, "\n%s\n\n",
413 gettext("scadm: couldn't change password"));
417 /* password was changed successfully, get out of while */
426 ADM_Process_userperm(int argc
, char *argv
[])
428 static data_buffer_t dataBuffer
;
430 struct timespec Timeout
;
431 dp_user_adm_t
*admMessage
;
432 dp_user_adm_r_t
*admResponse
;
438 if ((argc
!= 3) && (argc
!= 4)) {
446 if ((strlen(argv
[3]) > 4) || (strlen(argv
[3]) < 1)) {
452 for (index
= 0; index
< strlen(argv
[3]); index
++) {
453 if ((argv
[3][index
] != 'c') &&
454 (argv
[3][index
] != 'C') &&
455 (argv
[3][index
] != 'u') &&
456 (argv
[3][index
] != 'U') &&
457 (argv
[3][index
] != 'a') &&
458 (argv
[3][index
] != 'A') &&
459 (argv
[3][index
] != 'r') &&
460 (argv
[3][index
] != 'R')) {
465 if ((argv
[3][index
] == 'c') ||
466 (argv
[3][index
] == 'C')) {
467 /* See if this field was entered twice */
468 if ((permissions
& DP_USER_PERM_C
) != 0) {
472 permissions
= permissions
| DP_USER_PERM_C
;
475 if ((argv
[3][index
] == 'u') ||
476 (argv
[3][index
] == 'U')) {
477 /* See if this field was enetered twice */
478 if ((permissions
& DP_USER_PERM_U
) != 0) {
482 permissions
= permissions
| DP_USER_PERM_U
;
485 if ((argv
[3][index
] == 'a') ||
486 (argv
[3][index
] == 'A')) {
487 /* See if this field was enetered twice */
488 if ((permissions
& DP_USER_PERM_A
) != 0) {
492 permissions
= permissions
| DP_USER_PERM_A
;
495 if ((argv
[3][index
] == 'r') ||
496 (argv
[3][index
] == 'R')) {
497 /* See if this field was enetered twice */
498 if ((permissions
& DP_USER_PERM_R
) != 0) {
502 permissions
= permissions
| DP_USER_PERM_R
;
509 if (strlen(argv
[2]) > DP_USER_NAME_SIZE
) {
514 admMessage
= (dp_user_adm_t
*)&dataBuffer
;
515 admMessage
->command
= DP_USER_CMD_PERM
;
516 admMessage
->parm
= permissions
;
517 userName
= (char *)(&((char *)admMessage
)[sizeof (dp_user_adm_t
)]);
518 (void) strcpy(userName
, argv
[2]);
520 Message
.type
= DP_USER_ADM
;
521 Message
.len
= sizeof (dp_user_adm_t
) + strlen(userName
) + 1;
522 Message
.data
= admMessage
;
526 Timeout
.tv_sec
= ADM_SEPROM_TIMEOUT
;
527 ADM_Recv(&Message
, &Timeout
, DP_USER_ADM_R
, sizeof (dp_user_adm_r_t
));
529 admResponse
= (dp_user_adm_r_t
*)Message
.data
;
530 if (admResponse
->command
!= DP_USER_CMD_PERM
) {
535 if (admResponse
->status
== DP_ERR_USER_NONE
) {
538 } else if (admResponse
->status
== DP_ERR_USER_BAD
) {
539 malformed_username();
541 } else if (admResponse
->status
!= 0) {
542 (void) fprintf(stderr
, "\n%s\n\n",
543 gettext("scadm: couldn't change permissions"));
552 ADM_Get_Password(char *password
)
554 static char pass1
[64];
555 static char pass2
[64];
556 static struct termios newOpts
;
564 if (signal(SIGINT
, cleanup
) == SIG_ERR
) {
565 (void) fprintf(stderr
, "\n%s\n\n",
566 gettext("scadm: cleanup() registration failed"));
571 (void) tcgetattr(0, &oldOpts
);
573 newOpts
.c_lflag
&= ~ECHO
;
574 (void) tcsetattr(0, TCSANOW
, &newOpts
);
576 while ((passTry
> 0) && (validPass
== 0)) {
577 passTry
= passTry
- 1;
578 (void) printf("%s", gettext("Password: "));
579 (void) scanf("%s", pass1
);
581 (void) fflush(stdin
);
582 (void) printf("%s", gettext("Re-enter Password: "));
583 (void) scanf("%s", pass2
);
586 /* Truncate at 8 characters */
587 pass1
[8] = pass2
[8] = '\0';
589 if ((strcmp(pass1
, pass2
) != 0) && (passTry
> 0)) {
590 ADM_Destroy_Password(pass1
);
591 ADM_Destroy_Password(pass2
);
592 (void) fprintf(stderr
, "%s\n\n",
593 gettext("Passwords didn't match, try again"));
594 } else if ((strcmp(pass1
, pass2
) != 0) && (passTry
<= 0)) {
595 ADM_Destroy_Password(pass1
);
596 ADM_Destroy_Password(pass2
);
597 (void) fprintf(stderr
, "\n%s\n\n",
598 gettext("scadm: ERROR, passwords didn't match"));
599 (void) tcsetattr(0, TCSANOW
, &oldOpts
);
606 (void) tcsetattr(0, TCSANOW
, &oldOpts
);
608 (void) strcpy(password
, pass1
);
609 ADM_Destroy_Password(pass1
);
610 ADM_Destroy_Password(pass2
);
618 (void) tcsetattr(0, TCSANOW
, &oldOpts
);
625 ADM_Destroy_Password(char *password
)
629 for (index
= 0; index
< strlen(password
); index
++)
630 password
[index
] = 0x1;
637 (void) fprintf(stderr
,
638 gettext("\nscadm: maximum username length is %d\n\n"),
646 (void) fprintf(stderr
,
647 "\n%s\n\n", gettext("scadm: malformed username"));
654 (void) fprintf(stderr
, "\n%s\n\n",
655 gettext("scadm: SC returned wrong response"));
662 (void) fprintf(stderr
,
663 "\n%s\n\n", gettext("scadm: username does not exist"));
670 (void) fprintf(stderr
, "\n%s\n\n",
671 gettext("scadm: couldn't get information on user"));
678 (void) fprintf(stderr
, "\n%s\n\n",
679 gettext("USAGE: scadm userperm <username> [cuar]"));
687 int usernLen
= strlen(gettext("username"));
688 int permLen
= strlen(gettext("permissions"));
689 int pwdLen
= strlen(gettext("password"));
693 (void) printf("%s", gettext("username"));
694 for (i
= 0; i
< (20 - usernLen
); i
++)
697 (void) printf("%s", gettext("permissions"));
698 for (i
= 0; i
< (19 - permLen
); i
++)
701 (void) printf("%s\n", gettext("password"));
704 for (i
= 0; i
< usernLen
; i
++)
709 for (i
= 0; i
< permLen
; i
++)
714 for (i
= 0; i
< pwdLen
; i
++)