~ubuntu-branches/ubuntu/raring/libuser/raring

« back to all changes in this revision

Viewing changes to modules/files.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2006-12-28 19:18:14 UTC
  • mfrom: (2.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20061228191814-i7ssaghc6shbsv95
Tags: 1:0.54.6-2.1.dfsg.1-1.2
* Non-maintainer upload.
* Moved all manpages of commands in /usr/sbin/ from section 1 to
  section 8 (Closes: #404020).
* Added autotools-dev as build-dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2000-2002, 2004, 2005 Red Hat, Inc.
 
2
 * Copyright (C) 2000-2002, 2004, 2005, 2006 Red Hat, Inc.
3
3
 *
4
4
 * This is free software; you can redistribute it and/or modify it under
5
5
 * the terms of the GNU Library General Public License as published by
16
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
17
 */
18
18
 
19
 
#ident "$Id: files.c,v 1.80 2005/09/12 21:52:02 mitr Exp $"
 
19
#ident "$Id: files.c,v 1.83 2006/03/06 03:02:52 mitr Exp $"
20
20
 
21
21
#ifdef HAVE_CONFIG_H
22
22
#include "config.h"
164
164
        gpointer ilock, olock;
165
165
        char *backupname;
166
166
        struct stat ist, ost;
167
 
        char buf[CHUNK_SIZE];
168
167
        gboolean res = FALSE;
169
168
 
170
169
        g_assert(filename != NULL);
235
234
 
236
235
        /* Copy the data, block by block. */
237
236
        for (;;) {
 
237
                char buf[CHUNK_SIZE];
238
238
                ssize_t left;
239
239
                char *p;
240
240
 
410
410
        /* Now parse out the fields. */
411
411
        memset(&value, 0, sizeof(value));
412
412
        for (i = 0; i < format_count; i++) {
 
413
                const gchar *val;
 
414
 
 
415
                val = v[i];
 
416
                if (val == NULL)
 
417
                        val = "";
413
418
                /* Clear out old values in the destination structure. */
414
419
                lu_ent_clear_current(ent, formats[i].attribute);
415
420
                if (formats[i].multiple) {
418
423
                        size_t j;
419
424
 
420
425
                        /* Split up the field. */
421
 
                        w = g_strsplit(v[i] ?: "", ",", 0);
 
426
                        w = g_strsplit(val, ",", 0);
422
427
                        /* Clear out old values. */
423
428
                        for (j = 0; (w != NULL) && (w[j] != NULL); j++) {
424
429
                                /* Skip over empty strings. */
437
442
                } else {
438
443
                        /* Check if we need to supply the default value. */
439
444
                        if (formats[i].def_if_empty && formats[i].def != NULL
440
 
                            && strlen(v[i]) == 0) {
 
445
                            && strlen(val) == 0) {
441
446
                                gboolean ret;
442
447
 
443
448
                                /* Make sure we're not doing something
449
454
                                                  formats[i].def);
450
455
                                g_assert (ret != FALSE);
451
456
                        } else {
452
 
                                if (parse_field (formats + i, &value, v[i])
 
457
                                if (parse_field (formats + i, &value, val)
453
458
                                    == FALSE)
454
459
                                        continue;
455
460
                        }
468
473
static gboolean
469
474
lu_files_parse_user_entry(const gchar * line, struct lu_ent *ent)
470
475
{
471
 
        gboolean ret;
472
476
        ent->type = lu_user;
473
477
        lu_ent_clear_all(ent);
474
 
        ret = parse_generic(line, format_passwd, G_N_ELEMENTS(format_passwd),
475
 
                            ent);
476
 
        return ret;
 
478
        return parse_generic(line, format_passwd, G_N_ELEMENTS(format_passwd),
 
479
                             ent);
477
480
}
478
481
 
479
482
/* Parse an entry from /etc/group into an ent structure, using the attribute
481
484
static gboolean
482
485
lu_files_parse_group_entry(const gchar * line, struct lu_ent *ent)
483
486
{
484
 
        gboolean ret;
485
487
        ent->type = lu_group;
486
488
        lu_ent_clear_all(ent);
487
 
        ret = parse_generic(line, format_group, G_N_ELEMENTS(format_group),
488
 
                            ent);
489
 
        return ret;
 
489
        return parse_generic(line, format_group, G_N_ELEMENTS(format_group),
 
490
                             ent);
490
491
}
491
492
 
492
493
/* Parse an entry from /etc/shadow into an ent structure, using the attribute
494
495
static gboolean
495
496
lu_shadow_parse_user_entry(const gchar * line, struct lu_ent *ent)
496
497
{
497
 
        gboolean ret;
498
498
        ent->type = lu_user;
499
499
        lu_ent_clear_all(ent);
500
 
        ret = parse_generic(line, format_shadow, G_N_ELEMENTS(format_shadow),
501
 
                            ent);
502
 
        return ret;
 
500
        return parse_generic(line, format_shadow, G_N_ELEMENTS(format_shadow),
 
501
                             ent);
503
502
}
504
503
 
505
504
/* Parse an entry from /etc/shadow into an ent structure, using the attribute
507
506
static gboolean
508
507
lu_shadow_parse_group_entry(const gchar * line, struct lu_ent *ent)
509
508
{
510
 
        gboolean ret;
511
509
        ent->type = lu_group;
512
510
        lu_ent_clear_all(ent);
513
 
        ret = parse_generic(line, format_gshadow, G_N_ELEMENTS(format_gshadow),
514
 
                            ent);
515
 
        return ret;
 
511
        return parse_generic(line, format_gshadow,
 
512
                             G_N_ELEMENTS(format_gshadow), ent);
516
513
}
517
514
 
518
515
typedef gboolean(*parse_fn) (const gchar * line, struct lu_ent * ent);
525
522
               const char *name, int field, parse_fn parser,
526
523
               struct lu_ent *ent, struct lu_error **error)
527
524
{
528
 
        gboolean ret = FALSE;
 
525
        gboolean ret;
529
526
        const char *dir;
530
527
        int fd = -1;
531
528
        gpointer lock;
585
582
                          struct lu_ent *ent,
586
583
                          struct lu_error **error)
587
584
{
588
 
        gboolean ret;
589
 
        ret = generic_lookup(module, "passwd", name, 1,
590
 
                             lu_files_parse_user_entry, ent, error);
591
 
        return ret;
 
585
        return generic_lookup(module, "passwd", name, 1,
 
586
                              lu_files_parse_user_entry, ent, error);
592
587
}
593
588
 
594
589
/* Look up a user by ID in /etc/passwd. */
599
594
                        struct lu_error **error)
600
595
{
601
596
        char *key;
602
 
        gboolean ret = FALSE;
 
597
        gboolean ret;
603
598
 
604
599
        key = g_strdup_printf("%jd", (intmax_t)uid);
605
600
        ret = generic_lookup(module, "passwd", key, 3,
615
610
                           struct lu_ent *ent,
616
611
                           struct lu_error **error)
617
612
{
618
 
        gboolean ret;
619
 
        ret = generic_lookup(module, "shadow", name, 1,
620
 
                             lu_shadow_parse_user_entry, ent, error);
621
 
        return ret;
 
613
        return generic_lookup(module, "shadow", name, 1,
 
614
                              lu_shadow_parse_user_entry, ent, error);
622
615
}
623
616
 
624
617
/* Look up a user by ID in /etc/shadow.  This becomes a bit tricky because
630
623
                         struct lu_ent *ent,
631
624
                         struct lu_error **error)
632
625
{
633
 
        char *key, *p;
634
 
        GValueArray *values;
635
 
        GValue *value;
636
 
        gboolean ret = FALSE;
 
626
        char *key;
 
627
        gboolean ret;
 
628
 
637
629
        /* First look the user up by ID. */
638
630
        key = g_strdup_printf("%jd", (intmax_t)uid);
639
631
        ret = lu_files_user_lookup_id(module, uid, ent, error);
640
632
        if (ret) {
 
633
                GValueArray *values;
 
634
 
641
635
                /* Now use the user's name to search the shadow file. */
642
636
                values = lu_ent_get(ent, LU_USERNAME);
643
637
                if (values != NULL) {
 
638
                        GValue *value;
 
639
                        char *p;
 
640
 
644
641
                        value = g_value_array_get_nth(values, 0);
645
642
                        p = lu_value_strdup(value);
646
643
                        ret = generic_lookup(module, "shadow", p, 1,
660
657
                           struct lu_ent *ent,
661
658
                           struct lu_error **error)
662
659
{
663
 
        gboolean ret;
664
 
        ret = generic_lookup(module, "group", name, 1,
665
 
                             lu_files_parse_group_entry, ent, error);
666
 
        return ret;
 
660
        return generic_lookup(module, "group", name, 1,
 
661
                              lu_files_parse_group_entry, ent, error);
667
662
}
668
663
 
669
664
/* Look a group up by ID in /etc/group. */
688
683
lu_shadow_group_lookup_name(struct lu_module *module, const char *name,
689
684
                            struct lu_ent *ent, struct lu_error **error)
690
685
{
691
 
        gboolean ret;
692
 
        ret = generic_lookup(module, "gshadow", name, 1,
693
 
                             lu_shadow_parse_group_entry, ent, error);
694
 
        return ret;
 
686
        return generic_lookup(module, "gshadow", name, 1,
 
687
                              lu_shadow_parse_group_entry, ent, error);
695
688
}
696
689
 
697
690
/* Look up a group by ID in /etc/gshadow.  This file doesn't contain any
701
694
                          struct lu_ent *ent, struct lu_error **error)
702
695
{
703
696
        char *key;
704
 
        GValueArray *values;
705
 
        GValue *value;
706
 
        gboolean ret = FALSE;
 
697
        gboolean ret;
707
698
 
708
699
        key = g_strdup_printf("%jd", (intmax_t)gid);
709
700
        ret = lu_files_group_lookup_id(module, gid, ent, error);
710
701
        if (ret) {
 
702
                GValueArray *values;
 
703
 
711
704
                values = lu_ent_get(ent, LU_GROUPNAME);
712
705
                if (values != NULL) {
 
706
                        GValue *value;
713
707
                        char *p;
 
708
 
714
709
                        value = g_value_array_get_nth(values, 0);
715
710
                        p = lu_value_strdup(value);
716
711
                        ret = generic_lookup(module, "gshadow", p, 1,
806
801
static char *
807
802
lu_files_format_user(struct lu_ent *ent)
808
803
{
809
 
        char *ret;
810
 
        ret = format_generic(ent, format_passwd, G_N_ELEMENTS(format_passwd));
811
 
        return ret;
 
804
        return format_generic(ent, format_passwd, G_N_ELEMENTS(format_passwd));
812
805
}
813
806
 
814
807
/* Construct a line for /etc/group using data in the lu_ent structure. */
815
808
static char *
816
809
lu_files_format_group(struct lu_ent *ent)
817
810
{
818
 
        char *ret;
819
 
        ret = format_generic(ent, format_group, G_N_ELEMENTS(format_group));
820
 
        return ret;
 
811
        return format_generic(ent, format_group, G_N_ELEMENTS(format_group));
821
812
}
822
813
 
823
814
/* Construct a line for /etc/shadow using data in the lu_ent structure. */
824
815
static char *
825
816
lu_shadow_format_user(struct lu_ent *ent)
826
817
{
827
 
        char *ret;
828
 
        ret = format_generic(ent, format_shadow, G_N_ELEMENTS(format_shadow));
829
 
        return ret;
 
818
        return format_generic(ent, format_shadow, G_N_ELEMENTS(format_shadow));
830
819
}
831
820
 
832
821
/* Construct a line for /etc/gshadow using data in the lu_ent structure. */
833
822
static char *
834
823
lu_shadow_format_group(struct lu_ent *ent)
835
824
{
836
 
        char *ret;
837
 
        ret = format_generic(ent, format_gshadow, G_N_ELEMENTS(format_gshadow));
838
 
        return ret;
 
825
        return format_generic(ent, format_gshadow,
 
826
                              G_N_ELEMENTS(format_gshadow));
839
827
}
840
828
 
841
829
typedef char *(*format_fn) (struct lu_ent * ent);
852
840
        char *key, *line, *filename, *contents;
853
841
        char *fragment1, *fragment2;
854
842
        int fd;
855
 
        int r;
 
843
        ssize_t r;
856
844
        gpointer lock;
857
845
        struct stat st;
858
846
        off_t offset;
1012
1000
lu_files_user_add(struct lu_module *module, struct lu_ent *ent,
1013
1001
                  struct lu_error **error)
1014
1002
{
1015
 
        gboolean ret;
1016
 
        ret = generic_add(module, "passwd", lu_files_format_user, ent, error);
1017
 
        return ret;
 
1003
        return generic_add(module, "passwd", lu_files_format_user, ent, error);
1018
1004
}
1019
1005
 
1020
1006
/* Make last-minute changes to the record before adding it to /etc/shadow. */
1042
1028
lu_shadow_user_add(struct lu_module *module, struct lu_ent *ent,
1043
1029
                   struct lu_error **error)
1044
1030
{
1045
 
        gboolean ret;
1046
 
        ret = generic_add(module, "shadow", lu_shadow_format_user, ent, error);
1047
 
        return ret;
 
1031
        return generic_add(module, "shadow", lu_shadow_format_user, ent,
 
1032
                           error);
1048
1033
}
1049
1034
 
1050
1035
/* Make last-minute changes before adding the group to the group file. */
1063
1048
lu_files_group_add(struct lu_module *module, struct lu_ent *ent,
1064
1049
                   struct lu_error **error)
1065
1050
{
1066
 
        gboolean ret;
1067
 
        ret = generic_add(module, "group", lu_files_format_group, ent, error);
1068
 
        return ret;
 
1051
        return generic_add(module, "group", lu_files_format_group, ent, error);
1069
1052
}
1070
1053
 
1071
1054
/* Make last-minute changes before adding the shadowed group. */
1093
1076
lu_shadow_group_add(struct lu_module *module, struct lu_ent *ent,
1094
1077
                    struct lu_error **error)
1095
1078
{
1096
 
        gboolean ret;
1097
 
        ret = generic_add(module, "gshadow", lu_shadow_format_group, ent, error);
1098
 
        return ret;
 
1079
        return generic_add(module, "gshadow", lu_shadow_format_group, ent,
 
1080
                           error);
1099
1081
}
1100
1082
 
1101
1083
/* Modify a particular record in the given file, field by field, using the
1106
1088
            struct lu_ent *ent, struct lu_error **error)
1107
1089
{
1108
1090
        security_context_t prev_context;
1109
 
        char *filename = NULL, *key = NULL;
 
1091
        char *filename, *key;
1110
1092
        int fd = -1;
1111
1093
        gpointer lock;
1112
1094
        size_t i;
1113
 
        const char *dir = NULL, *name_attribute;
1114
 
        GValueArray *names = NULL;
 
1095
        const char *dir, *name_attribute;
 
1096
        GValueArray *names;
1115
1097
        gboolean ret = FALSE;
1116
1098
 
1117
1099
        g_assert(module != NULL);
1216
1198
lu_files_user_mod(struct lu_module *module, struct lu_ent *ent,
1217
1199
                  struct lu_error **error)
1218
1200
{
1219
 
        gboolean ret;
1220
 
        ret = generic_mod(module, "passwd", format_passwd,
1221
 
                          G_N_ELEMENTS(format_passwd), ent, error);
1222
 
        return ret;
 
1201
        return generic_mod(module, "passwd", format_passwd,
 
1202
                           G_N_ELEMENTS(format_passwd), ent, error);
1223
1203
}
1224
1204
 
1225
1205
/* Modify an entry in the group file. */
1227
1207
lu_files_group_mod(struct lu_module *module, struct lu_ent *ent,
1228
1208
                   struct lu_error **error)
1229
1209
{
1230
 
        gboolean ret;
1231
 
        ret = generic_mod(module, "group", format_group,
1232
 
                          G_N_ELEMENTS(format_group), ent, error);
1233
 
        return ret;
 
1210
        return generic_mod(module, "group", format_group,
 
1211
                           G_N_ELEMENTS(format_group), ent, error);
1234
1212
}
1235
1213
 
1236
1214
/* Modify an entry in the shadow file. */
1238
1216
lu_shadow_user_mod(struct lu_module *module, struct lu_ent *ent,
1239
1217
                   struct lu_error **error)
1240
1218
{
1241
 
        gboolean ret;
1242
 
        ret = generic_mod(module, "shadow", format_shadow,
1243
 
                          G_N_ELEMENTS(format_shadow), ent, error);
1244
 
 
1245
 
        return ret;
 
1219
        return generic_mod(module, "shadow", format_shadow,
 
1220
                           G_N_ELEMENTS(format_shadow), ent, error);
1246
1221
}
1247
1222
 
1248
1223
/* Modify an entry in the gshadow file. */
1250
1225
lu_shadow_group_mod(struct lu_module *module, struct lu_ent *ent,
1251
1226
                    struct lu_error **error)
1252
1227
{
1253
 
        gboolean ret;
1254
 
        ret = generic_mod(module, "gshadow", format_gshadow,
1255
 
                          G_N_ELEMENTS(format_gshadow), ent, error);
1256
 
        return ret;
 
1228
        return generic_mod(module, "gshadow", format_gshadow,
 
1229
                           G_N_ELEMENTS(format_gshadow), ent, error);
1257
1230
}
1258
1231
 
1259
1232
/* Delete an entity from the given file. */
1264
1237
        security_context_t prev_context;
1265
1238
        GValueArray *name = NULL;
1266
1239
        GValue *value;
1267
 
        char *contents = NULL, *filename = NULL, *key = NULL;
1268
 
        char *fragment1 = NULL, *fragment2, *tmp;
 
1240
        char *contents, *filename, *key;
 
1241
        char *fragment1, *fragment2;
1269
1242
        const char *dir;
1270
1243
        struct stat st;
1271
1244
        size_t len;
1272
 
        int fd = -1;
 
1245
        int fd;
1273
1246
        gboolean ret = FALSE;
1274
1247
        gboolean found;
1275
1248
        gpointer lock;
1341
1314
        /* Remove all occurrences of this entry from the file. */
1342
1315
        len = strlen(fragment1);
1343
1316
        do {
 
1317
                char *tmp;
 
1318
 
1344
1319
                found = FALSE;
1345
1320
                /* If the data is on the first line of the file, we remove the
1346
1321
                 * first line. */
1395
1370
        }
1396
1371
        ret = TRUE;
1397
1372
        /* Fall through */
1398
 
        
 
1373
 
1399
1374
 err_contents:
1400
1375
        g_free(contents);
1401
1376
 err_lock:
1413
1388
lu_files_user_del(struct lu_module *module, struct lu_ent *ent,
1414
1389
                  struct lu_error **error)
1415
1390
{
1416
 
        gboolean ret;
1417
 
        ret = generic_del(module, "passwd", ent, error);
1418
 
        return ret;
 
1391
        return generic_del(module, "passwd", ent, error);
1419
1392
}
1420
1393
 
1421
1394
/* Remove a group from the group file. */
1423
1396
lu_files_group_del(struct lu_module *module, struct lu_ent *ent,
1424
1397
                   struct lu_error **error)
1425
1398
{
1426
 
        gboolean ret;
1427
 
        ret = generic_del(module, "group", ent, error);
1428
 
        return ret;
 
1399
        return generic_del(module, "group", ent, error);
1429
1400
}
1430
1401
 
1431
1402
/* Remove a user from the shadow file. */
1433
1404
lu_shadow_user_del(struct lu_module *module, struct lu_ent *ent,
1434
1405
                   struct lu_error **error)
1435
1406
{
1436
 
        gboolean ret;
1437
 
        ret = generic_del(module, "shadow", ent, error);
1438
 
        return ret;
 
1407
        return generic_del(module, "shadow", ent, error);
1439
1408
}
1440
1409
 
1441
1410
/* Remove a group from the gshadow file. */
1443
1412
lu_shadow_group_del(struct lu_module *module, struct lu_ent *ent,
1444
1413
                    struct lu_error **error)
1445
1414
{
1446
 
        gboolean ret;
1447
 
        ret = generic_del(module, "gshadow", ent, error);
1448
 
        return ret;
 
1415
        return generic_del(module, "gshadow", ent, error);
1449
1416
}
1450
1417
 
1451
1418
/* Return a modified version of the cryptedPassword string, depending on
1495
1462
        security_context_t prev_context;
1496
1463
        GValueArray *name = NULL;
1497
1464
        GValue *val;
1498
 
        char *filename = NULL, *key = NULL;
 
1465
        char *filename, *key;
1499
1466
        const char *dir;
1500
 
        char *value, *new_value, *namestring = NULL;
1501
 
        int fd = -1;
 
1467
        char *value, *new_value, *namestring;
 
1468
        int fd;
1502
1469
        gpointer lock;
1503
1470
        gboolean ret = FALSE;
1504
1471
 
1588
1555
{
1589
1556
        GValueArray *name = NULL;
1590
1557
        GValue *val;
1591
 
        char *filename = NULL, *key = NULL;
 
1558
        char *filename, *key;
1592
1559
        const char *dir;
1593
1560
        char *value, *namestring;
1594
 
        int fd = -1;
 
1561
        int fd;
1595
1562
        gpointer lock;
1596
 
        gboolean ret = FALSE;
 
1563
        gboolean ret;
1597
1564
 
1598
1565
        /* Get the name of this account. */
1599
1566
        g_assert((ent->type == lu_user) || (ent->type == lu_group));
1660
1627
lu_files_user_lock(struct lu_module *module, struct lu_ent *ent,
1661
1628
                   struct lu_error **error)
1662
1629
{
1663
 
        gboolean ret;
1664
 
        ret = generic_lock(module, "passwd", 2, ent, LO_LOCK, error);
1665
 
        return ret;
 
1630
        return generic_lock(module, "passwd", 2, ent, LO_LOCK, error);
1666
1631
}
1667
1632
 
1668
1633
static gboolean
1669
1634
lu_files_user_unlock(struct lu_module *module, struct lu_ent *ent,
1670
1635
                     struct lu_error **error)
1671
1636
{
1672
 
        gboolean ret;
1673
 
        ret = generic_lock(module, "passwd", 2, ent, LO_UNLOCK, error);
1674
 
        return ret;
 
1637
        return generic_lock(module, "passwd", 2, ent, LO_UNLOCK, error);
1675
1638
}
1676
1639
 
1677
1640
static gboolean
1678
1641
lu_files_user_unlock_nonempty(struct lu_module *module, struct lu_ent *ent,
1679
1642
                              struct lu_error **error)
1680
1643
{
1681
 
        gboolean ret;
1682
 
        ret = generic_lock(module, "passwd", 2, ent, LO_UNLOCK_NONEMPTY,
1683
 
                           error);
1684
 
        return ret;
 
1644
        return generic_lock(module, "passwd", 2, ent, LO_UNLOCK_NONEMPTY,
 
1645
                            error);
1685
1646
}
1686
1647
 
1687
1648
/* Lock a group from the group file. */
1689
1650
lu_files_group_lock(struct lu_module *module, struct lu_ent *ent,
1690
1651
                    struct lu_error **error)
1691
1652
{
1692
 
        gboolean ret;
1693
 
        ret = generic_lock(module, "group", 2, ent, LO_LOCK, error);
1694
 
        return ret;
 
1653
        return generic_lock(module, "group", 2, ent, LO_LOCK, error);
1695
1654
}
1696
1655
 
1697
1656
static gboolean
1698
1657
lu_files_group_unlock(struct lu_module *module, struct lu_ent *ent,
1699
1658
                      struct lu_error **error)
1700
1659
{
1701
 
        gboolean ret;
1702
 
        ret = generic_lock(module, "group", 2, ent, LO_UNLOCK, error);
1703
 
        return ret;
 
1660
        return generic_lock(module, "group", 2, ent, LO_UNLOCK, error);
1704
1661
}
1705
1662
 
1706
1663
static gboolean
1707
1664
lu_files_group_unlock_nonempty(struct lu_module *module, struct lu_ent *ent,
1708
1665
                               struct lu_error **error)
1709
1666
{
1710
 
        gboolean ret;
1711
 
        ret = generic_lock(module, "group", 2, ent, LO_UNLOCK_NONEMPTY, error);
1712
 
        return ret;
 
1667
        return generic_lock(module, "group", 2, ent, LO_UNLOCK_NONEMPTY,
 
1668
                            error);
1713
1669
}
1714
1670
 
1715
1671
/* Lock a user in the shadow file. */
1717
1673
lu_shadow_user_lock(struct lu_module *module, struct lu_ent *ent,
1718
1674
                    struct lu_error **error)
1719
1675
{
1720
 
        gboolean ret;
1721
 
        ret = generic_lock(module, "shadow", 2, ent, LO_LOCK, error);
1722
 
        return ret;
 
1676
        return generic_lock(module, "shadow", 2, ent, LO_LOCK, error);
1723
1677
}
1724
1678
 
1725
1679
static gboolean
1726
1680
lu_shadow_user_unlock(struct lu_module *module, struct lu_ent *ent,
1727
1681
                      struct lu_error **error)
1728
1682
{
1729
 
        gboolean ret;
1730
 
        ret = generic_lock(module, "shadow", 2, ent, LO_UNLOCK, error);
1731
 
        return ret;
 
1683
        return generic_lock(module, "shadow", 2, ent, LO_UNLOCK, error);
1732
1684
}
1733
1685
 
1734
1686
static gboolean
1735
1687
lu_shadow_user_unlock_nonempty(struct lu_module *module, struct lu_ent *ent,
1736
1688
                               struct lu_error **error)
1737
1689
{
1738
 
        gboolean ret;
1739
 
        ret = generic_lock(module, "shadow", 2, ent, LO_UNLOCK_NONEMPTY,
1740
 
                           error);
1741
 
        return ret;
 
1690
        return generic_lock(module, "shadow", 2, ent, LO_UNLOCK_NONEMPTY,
 
1691
                            error);
1742
1692
}
1743
1693
 
1744
1694
/* Lock a group in the gshadow file. */
1746
1696
lu_shadow_group_lock(struct lu_module *module, struct lu_ent *ent,
1747
1697
                     struct lu_error **error)
1748
1698
{
1749
 
        gboolean ret;
1750
 
        ret = generic_lock(module, "gshadow", 2, ent, LO_LOCK, error);
1751
 
        return ret;
 
1699
        return generic_lock(module, "gshadow", 2, ent, LO_LOCK, error);
1752
1700
}
1753
1701
 
1754
1702
static gboolean
1755
1703
lu_shadow_group_unlock(struct lu_module *module, struct lu_ent *ent,
1756
1704
                       struct lu_error **error)
1757
1705
{
1758
 
        gboolean ret;
1759
 
        ret = generic_lock(module, "gshadow", 2, ent, LO_UNLOCK, error);
1760
 
        return ret;
 
1706
        return generic_lock(module, "gshadow", 2, ent, LO_UNLOCK, error);
1761
1707
}
1762
1708
 
1763
1709
static gboolean
1764
1710
lu_shadow_group_unlock_nonempty(struct lu_module *module, struct lu_ent *ent,
1765
1711
                                struct lu_error **error)
1766
1712
{
1767
 
        gboolean ret;
1768
 
        ret = generic_lock(module, "gshadow", 2, ent, LO_UNLOCK_NONEMPTY,
1769
 
                           error);
1770
 
        return ret;
 
1713
        return generic_lock(module, "gshadow", 2, ent, LO_UNLOCK_NONEMPTY,
 
1714
                            error);
1771
1715
}
1772
1716
 
1773
1717
/* Check if the account is locked. */
1775
1719
lu_files_user_is_locked(struct lu_module *module, struct lu_ent *ent,
1776
1720
                        struct lu_error **error)
1777
1721
{
1778
 
        gboolean ret;
1779
 
        ret = generic_is_locked(module, "passwd", 2, ent, error);
1780
 
        return ret;
 
1722
        return generic_is_locked(module, "passwd", 2, ent, error);
1781
1723
}
1782
1724
 
1783
1725
static gboolean
1784
1726
lu_files_group_is_locked(struct lu_module *module, struct lu_ent *ent,
1785
1727
                         struct lu_error **error)
1786
1728
{
1787
 
        gboolean ret;
1788
 
        ret = generic_is_locked(module, "group", 2, ent, error);
1789
 
        return ret;
 
1729
        return generic_is_locked(module, "group", 2, ent, error);
1790
1730
}
1791
1731
 
1792
1732
static gboolean
1793
1733
lu_shadow_user_is_locked(struct lu_module *module, struct lu_ent *ent,
1794
1734
                         struct lu_error **error)
1795
1735
{
1796
 
        gboolean ret;
1797
 
        ret = generic_is_locked(module, "shadow", 2, ent, error);
1798
 
        return ret;
 
1736
        return generic_is_locked(module, "shadow", 2, ent, error);
1799
1737
}
1800
1738
 
1801
1739
static gboolean
1802
1740
lu_shadow_group_is_locked(struct lu_module *module, struct lu_ent *ent,
1803
1741
                         struct lu_error **error)
1804
1742
{
1805
 
        gboolean ret;
1806
 
        ret = generic_is_locked(module, "gshadow", 2, ent, error);
1807
 
        return ret;
 
1743
        return generic_is_locked(module, "gshadow", 2, ent, error);
1808
1744
}
1809
1745
 
1810
1746
/* Change a password, in a given file, in a given field, for a given account,
1817
1753
        security_context_t prev_context;
1818
1754
        GValueArray *name = NULL;
1819
1755
        GValue *val;
1820
 
        char *filename = NULL, *key = NULL, *value, *namestring = NULL;
 
1756
        char *filename, *key, *value, *namestring;
1821
1757
        const char *dir;
1822
 
        int fd = -1;
 
1758
        int fd;
1823
1759
        gpointer lock;
1824
1760
        gboolean ret = FALSE;
1825
1761
 
1916
1852
lu_files_user_setpass(struct lu_module *module, struct lu_ent *ent,
1917
1853
                      const char *password, struct lu_error **error)
1918
1854
{
1919
 
        gboolean ret;
1920
 
        ret = generic_setpass(module, "passwd", 2, ent, password, FALSE,
1921
 
                              error);
1922
 
        return ret;
 
1855
        return generic_setpass(module, "passwd", 2, ent, password, FALSE,
 
1856
                               error);
1923
1857
}
1924
1858
 
1925
1859
static gboolean
1926
1860
lu_files_group_setpass(struct lu_module *module, struct lu_ent *ent,
1927
1861
                       const char *password, struct lu_error **error)
1928
1862
{
1929
 
        gboolean ret;
1930
 
        ret = generic_setpass(module, "group", 2, ent, password, FALSE, error);
1931
 
        return ret;
 
1863
        return generic_setpass(module, "group", 2, ent, password, FALSE,
 
1864
                               error);
1932
1865
}
1933
1866
 
1934
1867
static gboolean
1935
1868
lu_files_user_removepass(struct lu_module *module, struct lu_ent *ent,
1936
1869
                         struct lu_error **error)
1937
1870
{
1938
 
        gboolean ret;
1939
 
        ret = generic_setpass(module, "passwd", 2, ent, LU_CRYPTED, FALSE,
1940
 
                              error);
1941
 
        return ret;
 
1871
        return generic_setpass(module, "passwd", 2, ent, LU_CRYPTED, FALSE,
 
1872
                               error);
1942
1873
}
1943
1874
 
1944
1875
static gboolean
1945
1876
lu_files_group_removepass(struct lu_module *module, struct lu_ent *ent,
1946
1877
                          struct lu_error **error)
1947
1878
{
1948
 
        gboolean ret;
1949
 
        ret = generic_setpass(module, "group", 2, ent, LU_CRYPTED, FALSE,
1950
 
                              error);
1951
 
        return ret;
 
1879
        return generic_setpass(module, "group", 2, ent, LU_CRYPTED, FALSE,
 
1880
                               error);
1952
1881
}
1953
1882
 
1954
1883
/* Set the shadow last-changed field to today's date. */
2024
1953
{
2025
1954
        int fd;
2026
1955
        gpointer lock;
2027
 
        GValueArray *ret = NULL;
 
1956
        GValueArray *ret;
2028
1957
        GValue value;
2029
1958
        char *buf;
2030
 
        char *key = NULL, *filename = NULL, *p;
2031
 
        const char *dir = NULL;
 
1959
        char *key, *filename;
 
1960
        const char *dir;
2032
1961
        FILE *fp;
2033
1962
 
2034
1963
        g_assert(module != NULL);
2077
2006
        g_value_init(&value, G_TYPE_STRING);
2078
2007
        /* Read each line, */
2079
2008
        while ((buf = line_read(fp)) != NULL) {
 
2009
                char *p;
 
2010
 
2080
2011
                if (strlen(buf) == 1) {
2081
2012
                        g_free(buf);
2082
2013
                        continue;
2111
2042
lu_files_users_enumerate(struct lu_module *module, const char *pattern,
2112
2043
                         struct lu_error **error)
2113
2044
{
2114
 
        GValueArray *ret;
2115
 
        ret = lu_files_enumerate(module, "passwd", pattern, error);
2116
 
        return ret;
 
2045
        return lu_files_enumerate(module, "passwd", pattern, error);
2117
2046
}
2118
2047
 
2119
2048
static GValueArray *
2120
2049
lu_files_groups_enumerate(struct lu_module *module, const char *pattern,
2121
2050
                          struct lu_error **error)
2122
2051
{
2123
 
        GValueArray *ret;
2124
 
        ret = lu_files_enumerate(module, "group", pattern, error);
2125
 
        return ret;
 
2052
        return lu_files_enumerate(module, "group", pattern, error);
2126
2053
}
2127
2054
 
2128
2055
/* Get a list of all of the users who are in a given group. */
2133
2060
{
2134
2061
        int fd;
2135
2062
        gpointer lock;
2136
 
        GValueArray *ret = NULL;
 
2063
        GValueArray *ret;
2137
2064
        GValue value;
2138
2065
        char *buf, grp[CHUNK_SIZE];
2139
 
        char *key = NULL, *pwdfilename = NULL, *grpfilename = NULL, *p, *q;
2140
 
        const char *dir = NULL;
 
2066
        char *key, *pwdfilename, *grpfilename, *p, *q;
 
2067
        const char *dir;
2141
2068
        FILE *fp;
2142
2069
 
2143
2070
        g_assert(module != NULL);
2337
2264
{
2338
2265
        int fd;
2339
2266
        gpointer lock;
2340
 
        GValueArray *ret = NULL;
 
2267
        GValueArray *ret;
2341
2268
        GValue value;
2342
2269
        char *buf;
2343
 
        char *key = NULL, *pwdfilename = NULL, *grpfilename = NULL, *p, *q;
2344
 
        const char *dir = NULL;
 
2270
        char *key, *pwdfilename, *grpfilename, *p, *q;
 
2271
        const char *dir;
2345
2272
        FILE *fp;
2346
2273
 
2347
2274
        (void)uid;
2537
2464
        gpointer lock;
2538
2465
        GPtrArray *ret = NULL;
2539
2466
        char *buf;
2540
 
        char *key = NULL, *filename = NULL;
2541
 
        const char *dir = NULL;
 
2467
        char *key, *filename;
 
2468
        const char *dir;
2542
2469
        FILE *fp;
2543
 
        struct lu_ent *ent;
2544
2470
 
2545
2471
        g_assert(module != NULL);
2546
2472
        g_assert(base_name != NULL);
2582
2508
        /* Allocate an array to hold results. */
2583
2509
        ret = g_ptr_array_new();
2584
2510
        while ((buf = line_read(fp)) != NULL) {
 
2511
                struct lu_ent *ent;
 
2512
 
2585
2513
                if (strlen(buf) == 1 || buf[0] == '+' || buf[0] == '-') {
2586
2514
                        g_free(buf);
2587
2515
                        continue;
2599
2527
                }
2600
2528
                /* If the account name matches the pattern, parse it and add
2601
2529
                 * it to the list. */
2602
 
                if (fnmatch(pattern, buf, 0) == 0) {
2603
 
                        parser(buf, ent);
 
2530
                if (fnmatch(pattern, key, 0) == 0 && parser(buf, ent) != FALSE)
2604
2531
                        g_ptr_array_add(ret, ent);
2605
 
                } else {
 
2532
                else
2606
2533
                        lu_ent_free(ent);
2607
 
                }
2608
2534
                g_free(buf);
2609
2535
                g_free(key);
2610
2536
        }
2770
2696
        gboolean ret = FALSE;
2771
2697
        /* Get the directory the files are in. */
2772
2698
        key = g_strconcat(module->name, "/directory", NULL);
2773
 
        directory = lu_cfg_read_single(module->lu_context, key, SYSCONFDIR);
 
2699
        directory = lu_cfg_read_single(module->lu_context, key, "/etc");
2774
2700
        g_free(key);
2775
2701
        /* If we can't access the passwd file as a normal user, then the
2776
2702
         * answer is "yes". */
2798
2724
        gboolean ret = FALSE;
2799
2725
        /* Get the directory the files are in. */
2800
2726
        key = g_strconcat(module->name, "/directory", NULL);
2801
 
        directory = lu_cfg_read_single(module->lu_context, key, SYSCONFDIR);
 
2727
        directory = lu_cfg_read_single(module->lu_context, key, "/etc");
2802
2728
        g_free(key);
2803
2729
        /* If we can't access the shadow file as a normal user, then the
2804
2730
         * answer is "yes". */
2832
2758
libuser_files_init(struct lu_context *context,
2833
2759
                   struct lu_error **error)
2834
2760
{
2835
 
        struct lu_module *ret = NULL;
 
2761
        struct lu_module *ret;
2836
2762
 
2837
2763
        g_return_val_if_fail(context != NULL, FALSE);
2838
2764
 
2839
2765
        /* Handle authenticating to the data source. */
2840
2766
        if (geteuid() != 0) {
2841
2767
                const char *val;
2842
 
                
 
2768
 
2843
2769
                /* Needed for the test suite, handy for debugging. */
2844
2770
                val = lu_cfg_read_single(context, "files/nonroot", NULL);
2845
2771
                if (val == NULL || strcmp (val, "yes") != 0) {
2907
2833
libuser_shadow_init(struct lu_context *context,
2908
2834
                    struct lu_error **error)
2909
2835
{
2910
 
        struct lu_module *ret = NULL;
 
2836
        struct lu_module *ret;
2911
2837
        struct stat st;
2912
2838
        char *shadow_file;
2913
 
        char *key;
2914
2839
        const char *dir;
2915
2840
 
2916
2841
        g_return_val_if_fail(context != NULL, NULL);
2918
2843
        /* Handle authenticating to the data source. */
2919
2844
        if (geteuid() != 0) {
2920
2845
                const char *val;
2921
 
                
 
2846
 
2922
2847
                /* Needed for the test suite, handy for debugging. */
2923
2848
                val = lu_cfg_read_single(context, "shadow/nonroot", NULL);
2924
2849
                if (val == NULL || strcmp (val, "yes") != 0) {
2930
2855
        }
2931
2856
 
2932
2857
        /* Get the name of the shadow file. */
2933
 
        key = g_strconcat("shadow", "/directory", NULL);
2934
 
        dir = lu_cfg_read_single(context, key, "/etc");
 
2858
        dir = lu_cfg_read_single(context, "shadow/directory", "/etc");
2935
2859
        shadow_file = g_strconcat(dir, "/shadow", NULL);
2936
 
        g_free(key);
2937
2860
 
2938
2861
        /* Make sure we're actually using shadow passwords on this system. */
2939
2862
        if ((stat(shadow_file, &st) == -1) && (errno == ENOENT)) {