su.c revision 1.52 1 /* $NetBSD: su.c,v 1.52 2003/04/20 20:13:20 christos Exp $ */
2
3 /*
4 * Copyright (c) 1988 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
40 All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";*/
46 #else
47 __RCSID("$NetBSD: su.c,v 1.52 2003/04/20 20:13:20 christos Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <grp.h>
57 #include <paths.h>
58 #include <pwd.h>
59 #include <stdio.h>
60 #ifdef SKEY
61 #include <skey.h>
62 #endif
63 #include <stdlib.h>
64 #include <string.h>
65 #include <syslog.h>
66 #include <time.h>
67 #include <tzfile.h>
68 #include <unistd.h>
69
70 #ifdef LOGIN_CAP
71 #include <login_cap.h>
72 #endif
73
74 #ifdef KERBEROS
75 #include <des.h>
76 #include <krb.h>
77 #include <netdb.h>
78
79 static int kerberos __P((char *, char *, int));
80 static int koktologin __P((char *, char *, char *));
81
82 #endif
83
84 #ifdef KERBEROS5
85 #include <krb5.h>
86
87 static int kerberos5 __P((char *, char *, int));
88
89 #endif
90
91 #if defined(KERBEROS) || defined(KERBEROS5)
92
93 #define ARGSTRX "-Kflm"
94
95 int use_kerberos = 1;
96
97 #else
98 #define ARGSTRX "-flm"
99 #endif
100
101 #ifndef SUGROUP
102 #define SUGROUP "wheel"
103 #endif
104
105 #ifdef LOGIN_CAP
106 #define ARGSTR ARGSTRX "c:"
107 #else
108 #define ARGSTR ARGSTRX
109 #endif
110
111 int main __P((int, char **));
112
113 static int chshell __P((const char *));
114 static char *ontty __P((void));
115 static int check_ingroup __P((int, const char *, const char *, int));
116
117
118 int
119 main(argc, argv)
120 int argc;
121 char **argv;
122 {
123 extern char **environ;
124 struct passwd *pwd;
125 char *p;
126 #ifdef BSD4_4
127 struct timeval tp;
128 #endif
129 uid_t ruid;
130 int asme, ch, asthem, fastlogin, prio;
131 enum { UNSET, YES, NO } iscsh = UNSET;
132 char *user, *shell, *avshell, *username, **np;
133 char *userpass, *class;
134 char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
135 time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
136 #ifdef LOGIN_CAP
137 login_cap_t *lc;
138 #endif
139
140 asme = asthem = fastlogin = 0;
141 shell = class = NULL;
142 while ((ch = getopt(argc, argv, ARGSTR)) != -1)
143 switch((char)ch) {
144 #if defined(KERBEROS) || defined(KERBEROS5)
145 case 'K':
146 use_kerberos = 0;
147 break;
148 #endif
149 #ifdef LOGIN_CAP
150 case 'c':
151 class = optarg;
152 break;
153 #endif
154 case 'f':
155 fastlogin = 1;
156 break;
157 case '-':
158 case 'l':
159 asme = 0;
160 asthem = 1;
161 break;
162 case 'm':
163 asme = 1;
164 asthem = 0;
165 break;
166 case '?':
167 default:
168 (void)fprintf(stderr,
169 "Usage: %s [%s] [login [shell arguments]]\n",
170 getprogname(), ARGSTR);
171 exit(1);
172 }
173 argv += optind;
174
175 /* Lower the priority so su runs faster */
176 errno = 0;
177 prio = getpriority(PRIO_PROCESS, 0);
178 if (errno)
179 prio = 0;
180 if (prio > -2)
181 (void)setpriority(PRIO_PROCESS, 0, -2);
182 openlog("su", 0, LOG_AUTH);
183
184 /* get current login name and shell */
185 ruid = getuid();
186 username = getlogin();
187 if (username == NULL || (pwd = getpwnam(username)) == NULL ||
188 pwd->pw_uid != ruid)
189 pwd = getpwuid(ruid);
190 if (pwd == NULL)
191 errx(1, "who are you?");
192 username = strdup(pwd->pw_name);
193 userpass = strdup(pwd->pw_passwd);
194 if (username == NULL || userpass == NULL)
195 err(1, "strdup");
196
197
198 if (asme) {
199 if (pwd->pw_shell && *pwd->pw_shell) {
200 strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
201 shell = shellbuf;
202 } else {
203 shell = _PATH_BSHELL;
204 iscsh = NO;
205 }
206 }
207 /* get target login information, default to root */
208 user = *argv ? *argv : "root";
209 np = *argv ? argv : argv-1;
210
211 if ((pwd = getpwnam(user)) == NULL)
212 errx(1, "unknown login %s", user);
213
214 #ifdef LOGIN_CAP
215 /* force the usage of specified class */
216 if (class) {
217 if (ruid)
218 errx(1, "Only root may use -c");
219
220 pwd->pw_class = class;
221 }
222 lc = login_getclass(pwd->pw_class);
223
224 pw_warntime = login_getcaptime(lc, "password-warn",
225 _PASSWORD_WARNDAYS * SECSPERDAY,
226 _PASSWORD_WARNDAYS * SECSPERDAY);
227 #endif
228
229 if (ruid
230 #ifdef KERBEROS5
231 && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
232 #endif
233 #ifdef KERBEROS
234 && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
235 #endif
236 ) {
237 char *pass = pwd->pw_passwd;
238 int ok = pwd->pw_uid != 0;
239
240 #ifdef ROOTAUTH
241 /*
242 * Allow those in group rootauth to su to root, by supplying
243 * their own password.
244 */
245 if (!ok) {
246 if ((ok = check_ingroup(-1, ROOTAUTH, username, 0))) {
247 pass = userpass;
248 user = username;
249 }
250 }
251 #endif
252 /*
253 * Only allow those in group SUGROUP to su to root,
254 * but only if that group has any members.
255 * If SUGROUP has no members, allow anyone to su root
256 */
257 if (!ok) {
258 ok = check_ingroup(-1, SUGROUP, username, 1);
259 }
260 if (!ok)
261 errx(1,
262 "you are not listed in the correct secondary group (%s) to su %s.",
263 SUGROUP, user);
264 /* if target requires a password, verify it */
265 if (*pass) {
266 p = getpass("Password:");
267 #ifdef SKEY
268 if (strcasecmp(p, "s/key") == 0) {
269 if (skey_haskey(user))
270 errx(1, "Sorry, you have no s/key.");
271 else {
272 if (skey_authenticate(user)) {
273 goto badlogin;
274 }
275 }
276
277 } else
278 #endif
279 if (strcmp(pass, crypt(p, pass))) {
280 #ifdef SKEY
281 badlogin:
282 #endif
283 fprintf(stderr, "Sorry\n");
284 syslog(LOG_WARNING,
285 "BAD SU %s to %s%s", username,
286 pwd->pw_name, ontty());
287 exit(1);
288 }
289 }
290 }
291
292 if (asme) {
293 /* if asme and non-standard target shell, must be root */
294 if (!chshell(pwd->pw_shell) && ruid)
295 errx(1,"permission denied (shell).");
296 } else if (pwd->pw_shell && *pwd->pw_shell) {
297 shell = pwd->pw_shell;
298 iscsh = UNSET;
299 } else {
300 shell = _PATH_BSHELL;
301 iscsh = NO;
302 }
303
304 if ((p = strrchr(shell, '/')) != NULL)
305 avshell = p+1;
306 else
307 avshell = shell;
308
309 /* if we're forking a csh, we want to slightly muck the args */
310 if (iscsh == UNSET)
311 iscsh = strstr(avshell, "csh") ? YES : NO;
312
313 /* set permissions */
314 #ifdef LOGIN_CAP
315 if (setusercontext(lc, pwd, pwd->pw_uid,
316 (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
317 LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
318 err(1, "setting user context");
319 #else
320 if (setgid(pwd->pw_gid) < 0)
321 err(1, "setgid");
322 if (initgroups(user, pwd->pw_gid))
323 errx(1, "initgroups failed");
324 if (setuid(pwd->pw_uid) < 0)
325 err(1, "setuid");
326 #endif
327
328 if (!asme) {
329 if (asthem) {
330 p = getenv("TERM");
331 /* Create an empty environment */
332 if ((environ = malloc(sizeof(char *))) == NULL)
333 err(1, NULL);
334 environ[0] = NULL;
335 #ifdef LOGIN_CAP
336 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
337 err(1, "setting user context");
338 #else
339 (void)setenv("PATH", _PATH_DEFPATH, 1);
340 #endif
341 if (p)
342 (void)setenv("TERM", p, 1);
343 if (chdir(pwd->pw_dir) < 0)
344 errx(1, "no directory");
345 }
346
347 if (asthem || pwd->pw_uid)
348 (void)setenv("USER", pwd->pw_name, 1);
349 (void)setenv("HOME", pwd->pw_dir, 1);
350 (void)setenv("SHELL", shell, 1);
351 }
352 (void)setenv("SU_FROM", username, 1);
353
354 if (iscsh == YES) {
355 if (fastlogin)
356 *np-- = "-f";
357 if (asme)
358 *np-- = "-m";
359 } else {
360 unsetenv("ENV");
361 }
362
363 if (asthem) {
364 avshellbuf[0] = '-';
365 (void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
366 avshell = avshellbuf;
367 } else if (iscsh == YES) {
368 /* csh strips the first character... */
369 avshellbuf[0] = '_';
370 (void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
371 avshell = avshellbuf;
372 }
373 *np = avshell;
374
375 #ifdef BSD4_4
376 if (pwd->pw_change || pwd->pw_expire)
377 (void)gettimeofday(&tp, (struct timezone *)NULL);
378 if (pwd->pw_change) {
379 if (tp.tv_sec >= pwd->pw_change) {
380 (void)printf("%s -- %s's password has expired.\n",
381 (ruid ? "Sorry" : "Note"), user);
382 if (ruid != 0)
383 exit(1);
384 } else if (pwd->pw_change - tp.tv_sec < pw_warntime)
385 (void)printf("Warning: %s's password expires on %s",
386 user, ctime(&pwd->pw_change));
387 }
388 if (pwd->pw_expire) {
389 if (tp.tv_sec >= pwd->pw_expire) {
390 (void)printf("%s -- %s's account has expired.\n",
391 (ruid ? "Sorry" : "Note"), user);
392 if (ruid != 0)
393 exit(1);
394 } else if (pwd->pw_expire - tp.tv_sec <
395 _PASSWORD_WARNDAYS * SECSPERDAY)
396 (void)printf("Warning: %s's account expires on %s",
397 user, ctime(&pwd->pw_expire));
398 }
399 #endif
400 if (ruid != 0)
401 syslog(LOG_NOTICE, "%s to %s%s",
402 username, pwd->pw_name, ontty());
403
404 /* Raise our priority back to what we had before */
405 (void)setpriority(PRIO_PROCESS, 0, prio);
406
407 execv(shell, np);
408 err(1, "%s", shell);
409 /* NOTREACHED */
410 }
411
412 static int
413 chshell(sh)
414 const char *sh;
415 {
416 const char *cp;
417
418 while ((cp = getusershell()) != NULL)
419 if (!strcmp(cp, sh))
420 return (1);
421 return (0);
422 }
423
424 static char *
425 ontty()
426 {
427 char *p;
428 static char buf[MAXPATHLEN + 4];
429
430 buf[0] = 0;
431 if ((p = ttyname(STDERR_FILENO)) != NULL)
432 (void)snprintf(buf, sizeof buf, " on %s", p);
433 return (buf);
434 }
435
436 #ifdef KERBEROS5
437 static int
438 kerberos5(username, user, uid)
439 char *username, *user;
440 int uid;
441 {
442 krb5_error_code ret;
443 krb5_context context;
444 krb5_principal princ = NULL;
445 krb5_ccache ccache, ccache2;
446 char *cc_name;
447 const char *filename;
448
449 ret = krb5_init_context(&context);
450 if (ret)
451 return (1);
452
453 if (strcmp (user, "root") == 0)
454 ret = krb5_make_principal(context, &princ,
455 NULL, username, "root", NULL);
456 else
457 ret = krb5_make_principal(context, &princ,
458 NULL, user, NULL);
459 if (ret)
460 goto fail;
461 if (!krb5_kuserok(context, princ, user) && !uid) {
462 warnx ("kerberos5: not in %s's ACL.", user);
463 goto fail;
464 }
465 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
466 if (ret)
467 goto fail;
468 ret = krb5_verify_user_lrealm(context, princ, ccache, NULL, TRUE,
469 NULL);
470 if (ret) {
471 krb5_cc_destroy(context, ccache);
472 switch (ret) {
473 case KRB5_LIBOS_PWDINTR :
474 break;
475 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
476 case KRB5KRB_AP_ERR_MODIFIED:
477 krb5_warnx(context, "Password incorrect");
478 break;
479 default :
480 krb5_warn(context, ret, "krb5_verify_user");
481 break;
482 }
483 goto fail;
484 }
485 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
486 if (ret) {
487 krb5_cc_destroy(context, ccache);
488 goto fail;
489 }
490 ret = krb5_cc_copy_cache(context, ccache, ccache2);
491 if (ret) {
492 krb5_cc_destroy(context, ccache);
493 krb5_cc_destroy(context, ccache2);
494 goto fail;
495 }
496
497 filename = krb5_cc_get_name(context, ccache2);
498 asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
499 filename);
500 if (chown (filename, uid, -1) < 0) {
501 warn("chown %s", filename);
502 free(cc_name);
503 krb5_cc_destroy(context, ccache);
504 krb5_cc_destroy(context, ccache2);
505 goto fail;
506 }
507
508 setenv("KRB5CCNAME", cc_name, 1);
509 free(cc_name);
510 krb5_cc_close(context, ccache2);
511 krb5_cc_destroy(context, ccache);
512 return (0);
513
514 fail:
515 if (princ != NULL)
516 krb5_free_principal (context, princ);
517 krb5_free_context (context);
518 return (1);
519 }
520 #endif
521
522 #ifdef KERBEROS
523 static int
524 kerberos(username, user, uid)
525 char *username, *user;
526 int uid;
527 {
528 KTEXT_ST ticket;
529 AUTH_DAT authdata;
530 struct hostent *hp;
531 int kerno;
532 u_long faddr;
533 char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
534 char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
535
536 if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
537 return (1);
538 if (koktologin(username, lrealm, user) && !uid) {
539 warnx("kerberos: not in %s's ACL.", user);
540 return (1);
541 }
542 (void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
543 user, getuid());
544
545 (void)setenv("KRBTKFILE", krbtkfile, 1);
546 (void)krb_set_tkt_string(krbtkfile);
547 /*
548 * Set real as well as effective ID to 0 for the moment,
549 * to make the kerberos library do the right thing.
550 */
551 if (setuid(0) < 0) {
552 warn("setuid");
553 return (1);
554 }
555
556 /*
557 * Little trick here -- if we are su'ing to root,
558 * we need to get a ticket for "xxx.root", where xxx represents
559 * the name of the person su'ing. Otherwise (non-root case),
560 * we need to get a ticket for "yyy.", where yyy represents
561 * the name of the person being su'd to, and the instance is null
562 *
563 * We should have a way to set the ticket lifetime,
564 * with a system default for root.
565 */
566 {
567 char prompt[128];
568 char passw[256];
569
570 (void)snprintf (prompt, sizeof(prompt),
571 "%s's Password: ",
572 krb_unparse_name_long ((uid == 0 ? username : user),
573 (uid == 0 ? "root" : ""),
574 lrealm));
575 if (des_read_pw_string (passw, sizeof (passw), prompt, 0)) {
576 memset (passw, 0, sizeof (passw));
577 return (1);
578 }
579 if (strlen(passw) == 0)
580 return (1); /* Empty passwords are not allowed */
581
582 kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
583 (uid == 0 ? "root" : ""), lrealm,
584 KRB_TICKET_GRANTING_TICKET,
585 lrealm,
586 DEFAULT_TKT_LIFE,
587 passw);
588 memset (passw, 0, strlen (passw));
589 }
590
591 if (kerno != KSUCCESS) {
592 if (kerno == KDC_PR_UNKNOWN) {
593 warnx("kerberos: principal unknown: %s.%s@%s",
594 (uid == 0 ? username : user),
595 (uid == 0 ? "root" : ""), lrealm);
596 return (1);
597 }
598 warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
599 syslog(LOG_WARNING,
600 "BAD Kerberos SU: %s to %s%s: %s",
601 username, user, ontty(), krb_err_txt[kerno]);
602 return (1);
603 }
604
605 if (chown(krbtkfile, uid, -1) < 0) {
606 warn("chown");
607 (void)unlink(krbtkfile);
608 return (1);
609 }
610
611 (void)setpriority(PRIO_PROCESS, 0, -2);
612
613 if (gethostname(hostname, sizeof(hostname)) == -1) {
614 warn("gethostname");
615 dest_tkt();
616 return (1);
617 }
618 hostname[sizeof(hostname) - 1] = '\0';
619
620 (void)strlcpy(savehost, krb_get_phost(hostname), sizeof(savehost));
621 savehost[sizeof(savehost) - 1] = '\0';
622
623 kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
624
625 if (kerno == KDC_PR_UNKNOWN) {
626 warnx("Warning: TGT not verified.");
627 syslog(LOG_WARNING,
628 "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
629 username, user, ontty(), krb_err_txt[kerno],
630 "rcmd", savehost);
631 } else if (kerno != KSUCCESS) {
632 warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
633 syslog(LOG_WARNING, "failed su: %s to %s%s: %s",
634 username, user, ontty(), krb_err_txt[kerno]);
635 dest_tkt();
636 return (1);
637 } else {
638 if (!(hp = gethostbyname(hostname))) {
639 warnx("can't get addr of %s", hostname);
640 dest_tkt();
641 return (1);
642 }
643 memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
644
645 if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
646 &authdata, "")) != KSUCCESS) {
647 warnx("kerberos: unable to verify rcmd ticket: %s",
648 krb_err_txt[kerno]);
649 syslog(LOG_WARNING,
650 "failed su: %s to %s%s: %s", username,
651 user, ontty(), krb_err_txt[kerno]);
652 dest_tkt();
653 return (1);
654 }
655 }
656 return (0);
657 }
658
659 static int
660 koktologin(name, realm, toname)
661 char *name, *realm, *toname;
662 {
663 return krb_kuserok(name,
664 strcmp (toname, "root") == 0 ? "root" : "",
665 realm,
666 toname);
667 }
668 #endif
669
670 static int
671 check_ingroup (gid, gname, user, ifempty)
672 int gid;
673 const char *gname;
674 const char *user;
675 int ifempty;
676 {
677 struct group *gr;
678 char **g;
679 #ifdef SU_INDIRECT_GROUP
680 char **gr_mem;
681 int n = 0;
682 int i = 0;
683 #endif
684 int ok = 0;
685
686 if (gname == NULL)
687 gr = getgrgid((gid_t) gid);
688 else
689 gr = getgrnam(gname);
690
691 /*
692 * XXX we are relying on the fact that we only set ifempty when
693 * calling to check for SUGROUP and that is the only time a
694 * missing group is acceptable.
695 */
696 if (gr == NULL)
697 return ifempty;
698 if (!*gr->gr_mem) /* empty */
699 return ifempty;
700
701 /*
702 * Ok, first see if user is in gr_mem
703 */
704 for (g = gr->gr_mem; *g; ++g) {
705 if (strcmp(*g, user) == 0)
706 return 1; /* ok */
707 #ifdef SU_INDIRECT_GROUP
708 ++n; /* count them */
709 #endif
710 }
711 #ifdef SU_INDIRECT_GROUP
712 /*
713 * No.
714 * Now we need to duplicate the gr_mem list, and recurse for
715 * each member to see if it is a group, and if so whether user is
716 * in it.
717 */
718 gr_mem = malloc((n + 1) * sizeof (char *));
719 for (g = gr->gr_mem, i = 0; *g; ++g) {
720 gr_mem[i] = strdup(*g);
721 if (!gr_mem[i])
722 err(1, "strdup");
723 i++;
724 }
725 gr_mem[i++] = NULL;
726
727 for (g = gr_mem; ok == 0 && *g; ++g) {
728 /*
729 * If we get this far we don't accept empty/missing groups.
730 */
731 ok = check_ingroup(-1, *g, user, 0);
732 }
733 for (g = gr_mem; *g; ++g) {
734 free(*g);
735 }
736 free(gr_mem);
737 #endif
738 return ok;
739 }
740