~ubuntu-branches/ubuntu/vivid/libuser/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/user.c

  • Committer: Bazaar Package Importer
  • Author(s): Ghe Rivero
  • Date: 2008-05-02 16:01:50 UTC
  • mfrom: (1.2.1 upstream) (3.1.4 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080502160150-cq4ayxgq4etkecut
Tags: 1:0.56.9.dfsg.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2002, 2004, 2005, 2006 Red Hat, Inc.
 
1
/* Copyright (C) 2000-2002, 2004, 2005, 2006, 2007 Red Hat, Inc.
2
2
 *
3
3
 * This is free software; you can redistribute it and/or modify it under
4
4
 * the terms of the GNU Library General Public License as published by
15
15
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
16
 */
17
17
 
18
 
#ident "$Id: user.c,v 1.81 2006/03/06 03:02:52 mitr Exp $"
19
 
 
20
 
#ifdef HAVE_CONFIG_H
21
 
#include "../config.h"
22
 
#endif
 
18
#include <config.h>
23
19
#include <sys/types.h>
24
20
#include <errno.h>
25
21
#include <grp.h>
26
22
#include <inttypes.h>
 
23
#include <limits.h>
27
24
#include <pwd.h>
28
25
#include <stddef.h>
29
26
#include <stdio.h>
802
799
                struct lu_module *module;
803
800
                gpointer scratch;
804
801
                GValue *value;
805
 
                char *name;
806
802
                gboolean tsuccess;
807
803
 
808
804
                value = g_value_array_get_nth(list, i);
809
 
                name = g_value_dup_string(value);
810
 
                module = g_tree_lookup(context->modules, name);
811
 
                g_free(name);
 
805
                module = g_tree_lookup(context->modules,
 
806
                                       g_value_get_string(value));
812
807
                g_assert(module != NULL);
813
808
                scratch = NULL;
814
809
                tsuccess = run_single(context, module, id,
1426
1421
        ret = lu_dispatch(context, user_setpass, tmp, LU_VALUE_INVALID_ID,
1427
1422
                          ent, NULL, error);
1428
1423
        g_free(tmp);
1429
 
        if (ret) {
 
1424
        if (ret)
1430
1425
                ret = lu_refresh_user(context, ent, error);
1431
 
        }
1432
1426
        if (ret) {
1433
 
                GValue value;
1434
 
                lu_ent_clear(ent, LU_SHADOWLASTCHANGE);
1435
 
                memset(&value, 0, sizeof(value));
1436
 
                g_value_init(&value, G_TYPE_STRING);
1437
 
                g_value_set_string(&value,
1438
 
                                   lu_util_shadow_current_date(ent->cache));
1439
 
                lu_ent_add(ent, LU_SHADOWLASTCHANGE, &value);
1440
 
                g_value_unset(&value);
 
1427
                lu_util_update_shadow_last_change(ent);
 
1428
                ret = lu_user_modify(context, ent, error);
1441
1429
        }
1442
1430
        return ret;
1443
1431
}
1454
1442
 
1455
1443
        ret = lu_dispatch(context, user_removepass, NULL, LU_VALUE_INVALID_ID,
1456
1444
                          ent, NULL, error);
1457
 
        if (ret) {
 
1445
        if (ret)
1458
1446
                ret = lu_refresh_user(context, ent, error);
1459
 
        }
1460
1447
        if (ret) {
1461
 
                GValue value;
1462
 
                lu_ent_clear(ent, LU_SHADOWLASTCHANGE);
1463
 
                memset(&value, 0, sizeof(value));
1464
 
                g_value_init(&value, G_TYPE_STRING);
1465
 
                g_value_set_string(&value,
1466
 
                                   lu_util_shadow_current_date(ent->cache));
1467
 
                lu_ent_add(ent, LU_SHADOWLASTCHANGE, &value);
1468
 
                g_value_unset(&value);
 
1448
                lu_util_update_shadow_last_change(ent);
 
1449
                ret = lu_user_modify(context, ent, error);
1469
1450
        }
1470
1451
        return ret;
1471
1452
}
1545
1526
        ret = lu_dispatch(context, group_setpass, tmp, LU_VALUE_INVALID_ID,
1546
1527
                          ent, NULL, error);
1547
1528
        g_free(tmp);
1548
 
        if (ret) {
 
1529
        if (ret)
1549
1530
                ret = lu_refresh_group(context, ent, error);
1550
 
        }
1551
 
        if (ret) {
1552
 
                GValue value;
1553
 
                lu_ent_clear(ent, LU_SHADOWLASTCHANGE);
1554
 
                memset(&value, 0, sizeof(value));
1555
 
                g_value_init(&value, G_TYPE_STRING);
1556
 
                g_value_set_string(&value,
1557
 
                                   lu_util_shadow_current_date(ent->cache));
1558
 
                lu_ent_add(ent, LU_SHADOWLASTCHANGE, &value);
1559
 
                g_value_unset(&value);
1560
 
        }
1561
1531
        return ret;
1562
1532
}
1563
1533
 
1573
1543
 
1574
1544
        ret = lu_dispatch(context, group_removepass, NULL, LU_VALUE_INVALID_ID,
1575
1545
                          ent, NULL, error);
1576
 
        if (ret) {
 
1546
        if (ret)
1577
1547
                ret = lu_refresh_group(context, ent, error);
1578
 
        }
1579
 
        if (ret) {
1580
 
                GValue value;
1581
 
                lu_ent_clear(ent, LU_SHADOWLASTCHANGE);
1582
 
                memset(&value, 0, sizeof(value));
1583
 
                g_value_init(&value, G_TYPE_STRING);
1584
 
                g_value_set_string(&value,
1585
 
                                   lu_util_shadow_current_date(ent->cache));
1586
 
                lu_ent_add(ent, LU_SHADOWLASTCHANGE, &value);
1587
 
                g_value_unset(&value);
1588
 
        }
1589
1548
        return ret;
1590
1549
}
1591
1550
 
1750
1709
        return id;
1751
1710
}
1752
1711
 
 
1712
/* Replace all instances of OLD in g_malloc()'ed STRING by NEW. */
 
1713
static char *
 
1714
replace_all(char *string, const char *old, const char *new)
 
1715
{
 
1716
        char *pos;
 
1717
 
 
1718
        pos = strstr(string, old);
 
1719
        if (pos != NULL) {
 
1720
                size_t old_len;
 
1721
 
 
1722
                old_len = strlen(old);
 
1723
                do {
 
1724
                        char *p, *prefix;
 
1725
 
 
1726
                        prefix = g_strndup(string, pos - string);
 
1727
                        p = g_strconcat(prefix, new, pos + old_len, NULL);
 
1728
                        g_free(prefix);
 
1729
                        g_free(string);
 
1730
                        string = p;
 
1731
                        pos = strstr(string, old);
 
1732
                } while (pos != NULL);
 
1733
        }
 
1734
        return string;
 
1735
}
 
1736
 
1753
1737
static gboolean
1754
1738
lu_default_int(struct lu_context *context, const char *name,
1755
1739
               enum lu_entity_type type, gboolean is_system, struct lu_ent *ent)
1820
1804
        if (is_system) {
1821
1805
                id = 1;
1822
1806
        } else {
1823
 
                cfgkey = g_strdup_printf("%s/%s", top, idkey);
 
1807
                cfgkey = g_strconcat(top, "/", idkey, (const gchar *)NULL);
1824
1808
                val = lu_cfg_read_single(context, cfgkey, NULL);
1825
1809
                g_free(cfgkey);
1826
1810
                if (val == NULL) {
1827
 
                        cfgkey = g_strdup_printf("%s/%s", top, idkeystring);
 
1811
                        cfgkey = g_strconcat(top, "/", idkeystring,
 
1812
                                             (const gchar *)NULL);
1828
1813
                        val = lu_cfg_read_single(context, cfgkey, NULL);
1829
1814
                        g_free(cfgkey);
1830
1815
                }
1891
1876
                        {LU_EMAIL, G_STRINGIFY_ARG(LU_EMAIL)},
1892
1877
                };
1893
1878
 
1894
 
                intmax_t imax;
1895
 
                char *end, *tmp;
 
1879
                char *tmp, replacement[sizeof (intmax_t) * CHAR_BIT + 1];
1896
1880
                const char *key;
1897
 
                struct {
1898
 
                        const char *format;
1899
 
                        const char *value;
1900
 
                } subst[] = {
1901
 
                        {"%n", name},
1902
 
                        {"%d", lu_util_shadow_current_date(context->scache)},
1903
 
                        /* Must be index 2, see below! */
1904
 
                        {"%u", NULL} /* value set later */
1905
 
                };
 
1881
                gboolean ok;
1906
1882
 
1907
1883
                /* Possibly map the key to an internal name. */
1908
1884
                key = p->data;
1920
1896
                }
1921
1897
 
1922
1898
                /* Generate the key and read the value for the item. */
1923
 
                cfgkey = g_strdup_printf("%s/%s", top, (const char *)p->data);
 
1899
                cfgkey = g_strconcat(top, "/", (const gchar *)p->data,
 
1900
                                     (const gchar *)NULL);
1924
1901
                val = lu_cfg_read_single(context, cfgkey, NULL);
1925
 
                g_free(cfgkey);
1926
1902
 
1927
1903
                /* Create a copy of the value to mess with. */
1928
1904
                g_assert(val != NULL);
1929
1905
                tmp = g_strdup(val);
1930
1906
 
1931
 
                subst[2].value = g_strdup_printf("%jd", (intmax_t)id);
1932
 
                /* Perform substitutions. */
1933
 
                for (i = 0; i < G_N_ELEMENTS(subst); i++) {
1934
 
                        while (strstr(tmp, subst[i].format) != NULL) {
1935
 
                                char *pre, *post, *tmp2, *where;
 
1907
                tmp = replace_all(tmp, "%n", name);
 
1908
                sprintf(replacement, "%ld", lu_util_shadow_current_date());
 
1909
                tmp = replace_all(tmp, "%d", replacement);
 
1910
                sprintf(replacement, "%jd", (intmax_t)id);
 
1911
                tmp = replace_all(tmp, "%u", replacement);
1936
1912
 
1937
 
                                where = strstr(tmp, subst[i].format);
1938
 
                                pre = g_strndup(tmp, where - tmp);
1939
 
                                post = g_strdup(where +
1940
 
                                                strlen(subst[i].format));
1941
 
                                tmp2 = g_strconcat(pre,
1942
 
                                                   subst[i].value,
1943
 
                                                   post,
1944
 
                                                   NULL);
1945
 
                                g_free(pre);
1946
 
                                g_free(post);
1947
 
                                g_free(tmp);
1948
 
                                tmp = tmp2;
 
1913
                ok = lu_value_init_set_attr_from_string(&value, key, tmp,
 
1914
                                                        &error);
 
1915
                if (ok == FALSE) {
 
1916
                        if (error == NULL) {
 
1917
                                /* Whatever this attribute is, default to a
 
1918
                                   string. */
 
1919
                                g_value_init(&value, G_TYPE_STRING);
 
1920
                                g_value_set_string(&value, tmp);
 
1921
                                ok = TRUE;
 
1922
                        } else {
 
1923
                                g_warning(_("Invalid default value of field "
 
1924
                                            "%s: %s"), cfgkey,
 
1925
                                          lu_strerror(error));
 
1926
                                lu_error_free(&error);
1949
1927
                        }
1950
1928
                }
1951
 
                g_free((char *)subst[2].value);
1952
 
 
1953
 
                /* Check if we can represent this value as a number. */
1954
 
                errno = 0;
1955
 
                imax = strtoimax(tmp, &end, 10);
1956
 
                if (errno == 0 && *end == 0 && end != tmp
1957
 
                    && (long)imax == imax) {
1958
 
                        g_value_init(&value, G_TYPE_LONG);
1959
 
                        g_value_set_long(&value, imax);
1960
 
                } else if (errno == 0 && *end == 0 && end != tmp
1961
 
                           && (id_t)imax == imax)
1962
 
                        lu_value_init_set_id(&value, imax);
1963
 
                else {
1964
 
                        g_value_init(&value, G_TYPE_STRING);
1965
 
                        g_value_set_string(&value, tmp);
1966
 
                }
1967
1929
                g_free(tmp);
 
1930
                g_free(cfgkey);
1968
1931
 
1969
 
                /* Add the transformed value. */
1970
 
                lu_ent_clear(ent, key);
1971
 
                lu_ent_add(ent, key, &value);
1972
 
                g_value_unset(&value);
 
1932
                if (ok != FALSE) {
 
1933
                        /* Add the transformed value. */
 
1934
                        lu_ent_clear(ent, key);
 
1935
                        lu_ent_add(ent, key, &value);
 
1936
                        g_value_unset(&value);
 
1937
                }
1973
1938
        }
1974
1939
        if (keys != NULL) {
1975
1940
                g_list_free(keys);