summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--lib/base64.c20
-rw-r--r--lib/env.c64
-rw-r--r--lib/graypam.h46
-rw-r--r--lib/ldappass.c89
-rw-r--r--lib/mem.c79
-rw-r--r--lib/slist.c115
-rw-r--r--lib/transform.c190
-rw-r--r--pam_groupmember/pam_groupmember.c1
-rw-r--r--pam_innetgr/pam_innetgr.c1
-rw-r--r--pam_ldaphome/pam_ldaphome.c262
-rw-r--r--pam_log/pam_log.c28
-rw-r--r--pam_regex/pam_regex.c16
-rw-r--r--pam_sql/pam_mysql.c10
-rw-r--r--pam_sql/pam_pgsql.c12
-rw-r--r--pam_sql/pam_sql.c73
-rw-r--r--pam_sql/pam_sql.h5
-rw-r--r--pam_umotd/pam_umotd.c23
17 files changed, 683 insertions, 351 deletions
diff --git a/lib/base64.c b/lib/base64.c
index 85b974a..9d0191e 100644
--- a/lib/base64.c
+++ b/lib/base64.c
@@ -56,11 +56,17 @@ gray_base64_decode(gray_slist_t slist, const char *iptr, size_t isize)
if (i == 4) {
- gray_slist_append_char(slist,
- (data[0] << 2) | ((data[1] & 0x30) >> 4));
- gray_slist_append_char(slist,
- ((data[1] & 0xf) << 4) | ((data[2] & 0x3c) >> 2));
- gray_slist_append_char(slist,
- ((data[2] & 0x3) << 6) | data[3]);
+ if (gray_slist_append_char(slist,
+ (data[0] << 2) | ((data[1] & 0x30) >> 4)) != 1)
+ return -1;
+ if (gray_slist_append_char(slist,
+ ((data[1] & 0xf) << 4) | ((data[2] & 0x3c) >> 2))
+ != 1)
+ return -1;
+ if (gray_slist_append_char(slist,
+ ((data[2] & 0x3) << 6) | data[3]) != 1)
+ return -1;
nbytes += 3 - pad;
- } else
+ } else {
+ errno = EILSEQ;
return -1;
+ }
i = 0;
diff --git a/lib/env.c b/lib/env.c
index 83152e4..b1da177 100644
--- a/lib/env.c
+++ b/lib/env.c
@@ -96,3 +96,3 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
- while (p = fgets(buf, sizeof buf, fp)) {
+ while ((p = fgets(buf, sizeof buf, fp))) {
int len;
@@ -107,6 +107,14 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
if (p[len-1] != '\n') {
- if (!slist)
+ if (!slist) {
slist = gray_slist_create();
- gray_slist_append(slist, p, len);
- while (p = fgets(buf, sizeof buf, fp)) {
+ if (!slist) {
+ rc = errno;
+ break;
+ }
+ }
+ if (gray_slist_append(slist, p, len) != len) {
+ rc = errno;
+ break;
+ }
+ while ((p = fgets(buf, sizeof buf, fp))) {
len = strlen(p);
@@ -116,4 +124,7 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
}
- gray_slist_append_char(slist, 0);
- p = gray_slist_finish(slist);
+ if (gray_slist_append_char(slist, 0) != 1
+ || (p = gray_slist_finish(slist)) == NULL) {
+ rc = errno;
+ break;
+ }
len = strlen(p);
@@ -131,6 +142,15 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
/* Collect continuation lines */
- if (!slist)
+ if (!slist) {
slist = gray_slist_create();
+ if (!slist) {
+ rc = errno;
+ break;
+ }
+ }
do {
- gray_slist_append(slist, p, len - 1);
+ if (gray_slist_append(slist, p, len - 1)
+ != len - 1) {
+ rc = errno;
+ break;
+ }
p = fgets (buf, sizeof buf, fp);
@@ -152,6 +172,15 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
} while (p[len-1] == '\\');
- if (len)
- gray_slist_append(slist, p, len);
- gray_slist_append_char(slist, 0);
- p = gray_slist_finish(slist);
+ if (rc)
+ break;
+ if (len) {
+ if (gray_slist_append(slist, p, len) != len) {
+ rc = errno;
+ break;
+ }
+ }
+ if (gray_slist_append_char(slist, 0) != 1
+ || (p = gray_slist_finish(slist)) == NULL) {
+ rc = errno;
+ break;
+ }
if (err)
@@ -162,4 +191,3 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
if (!env) {
- _pam_log(LOG_EMERG, "not enough memory");
- rc = 1;
+ rc = errno;
break;
@@ -169,5 +197,4 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
if (!env->name) {
- _pam_log(LOG_EMERG, "not enough memory");
+ rc = errno;
free(env);
- rc = 1;
break;
@@ -197,3 +224,8 @@ gray_env_read_tr(const char *file_name, struct gray_env **penv, char **trmap)
fclose(fp);
+ if (rc) {
+ _pam_log(LOG_EMERG, "%s", strerror(rc));
+ gray_env_free(config_env);
+ } else {
*penv = config_env;
+ }
return rc;
diff --git a/lib/graypam.h b/lib/graypam.h
index 76dcd68..69c4909 100644
--- a/lib/graypam.h
+++ b/lib/graypam.h
@@ -86,14 +86,3 @@ extern jmp_buf gray_pam_jmp;
-#define gray_pam_init(retval) \
- if (setjmp(gray_pam_jmp)) \
- return retval; \
-
-void gray_raise(const char *fmt, ...);
-
-void *gray_malloc(size_t size);
-void *gray_zalloc(size_t size);
-void *gray_calloc(size_t count, size_t size);
-void *gray_realloc(void *ptr, size_t size);
void *gray_2nrealloc(void *ptr, size_t *pcount, size_t elsiz);
-char *gray_strdup(const char *str);
@@ -105,17 +94,18 @@ void gray_make_str(pam_handle_t *pamh, const char *str, const char *name,
-
typedef struct gray_slist *gray_slist_t;
-gray_slist_t gray_slist_create();
+gray_slist_t gray_slist_create(void);
+int gray_slist_err(gray_slist_t slist);
+void gray_slist_clrerr(gray_slist_t slist);
void gray_slist_clear(gray_slist_t slist);
void gray_slist_free(gray_slist_t *slist);
-void gray_slist_append(gray_slist_t slist, const char *str, size_t n);
-void gray_slist_append_char(gray_slist_t slist, char c);
+ssize_t gray_slist_append(gray_slist_t slist, const char *str, size_t n);
+ssize_t gray_slist_append_char(gray_slist_t slist, char c);
size_t gray_slist_size(gray_slist_t slist);
-size_t gray_slist_coalesce(gray_slist_t slist);
+ssize_t gray_slist_coalesce(gray_slist_t slist);
void *gray_slist_head(gray_slist_t slist, size_t *psize);
void *gray_slist_finish(gray_slist_t slist);
-void gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
+int gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
int len, int base);
-void gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp);
+int gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp);
@@ -132,5 +122,23 @@ void gray_wait_debug(size_t interval, const char *file, size_t line);
+static inline int
+errno_to_pam(int ec)
+{
+ switch (ec) {
+ case EILSEQ:
+ _pam_log(LOG_ERR, "malformed password hash");
+ return PAM_SERVICE_ERR;
+
+ case ENOMEM:
+ _pam_log(LOG_ERR, "%s", strerror(errno));
+ return PAM_BUF_ERR;
+
+ default:
+ break;
+ }
+ _pam_log(LOG_ERR, "%s", strerror(errno));
+ return PAM_SERVICE_ERR;
+}
int gray_transform_name_to_slist (gray_slist_t slist, char *input, char **output);
-void gray_set_transform_expr (const char *expr);
+int gray_set_transform_expr (const char *expr);
void gray_free_transform_expr (void);
diff --git a/lib/ldappass.c b/lib/ldappass.c
index 968078c..247cf16 100644
--- a/lib/ldappass.c
+++ b/lib/ldappass.c
@@ -50,3 +50,3 @@ chk_md5 (const char *db_pass, const char *pass)
struct gpam_md5_ctx md5context;
- gray_slist_t slist = gray_slist_create ();
+ gray_slist_t slist;
ssize_t size;
@@ -55,2 +55,6 @@ chk_md5 (const char *db_pass, const char *pass)
+ slist = gray_slist_create ();
+ if (!slist)
+ return errno_to_pam(errno);
+
gpam_md5_init_ctx (&md5context);
@@ -62,8 +66,17 @@ chk_md5 (const char *db_pass, const char *pass)
{
+ rc = errno_to_pam(errno);
gray_slist_free(&slist);
- return PAM_AUTH_ERR;
+ return rc;
}
p = gray_slist_finish(slist);
- rc = memcmp (md5digest, p, sizeof md5digest) == 0 ?
- PAM_SUCCESS : PAM_AUTH_ERR;
+ if (p)
+ {
+ rc = memcmp (md5digest, p, sizeof md5digest) == 0
+ ? PAM_SUCCESS
+ : PAM_AUTH_ERR;
+ }
+ else
+ {
+ rc = errno_to_pam(errno);
+ }
gray_slist_free(&slist);
@@ -79,5 +92,9 @@ chk_smd5 (const char *db_pass, const char *pass)
struct gpam_md5_ctx md5context;
- gray_slist_t slist = gray_slist_create();
+ gray_slist_t slist;
ssize_t size;
+ slist = gray_slist_create();
+ if (!slist)
+ return errno_to_pam(errno);
+
size = gray_base64_decode(slist, db_pass, strlen (db_pass));
@@ -85,5 +102,5 @@ chk_smd5 (const char *db_pass, const char *pass)
{
- _pam_log(LOG_ERR, "malformed SMD5 password: %s", db_pass);
+ rc = errno_to_pam(errno);
gray_slist_free(&slist);
- return PAM_AUTH_ERR;
+ return rc;
}
@@ -91,3 +108,4 @@ chk_smd5 (const char *db_pass, const char *pass)
d1 = gray_slist_finish(slist);
-
+ if (d1)
+ {
gpam_md5_init_ctx (&md5context);
@@ -97,4 +115,11 @@ chk_smd5 (const char *db_pass, const char *pass)
- rc = memcmp (md5digest, d1, sizeof md5digest) == 0 ?
- PAM_SUCCESS : PAM_AUTH_ERR;
+ rc = memcmp (md5digest, d1, sizeof md5digest) == 0
+ ? PAM_SUCCESS
+ : PAM_AUTH_ERR;
+ }
+ else
+ {
+ rc = errno_to_pam(gray_slist_err(slist));
+ }
+
gray_slist_free(&slist);
@@ -110,5 +135,9 @@ chk_sha (const char *db_pass, const char *pass)
struct gpam_sha1_ctx sha1context;
- gray_slist_t slist = gray_slist_create();
+ gray_slist_t slist;
ssize_t size;
+ slist = gray_slist_create();
+ if (!slist)
+ return errno_to_pam(errno);
+
gpam_sha1_init_ctx (&sha1context);
@@ -120,4 +149,5 @@ chk_sha (const char *db_pass, const char *pass)
{
+ rc = errno_to_pam(errno);
gray_slist_free(&slist);
- return 1;
+ return rc;
}
@@ -125,4 +155,12 @@ chk_sha (const char *db_pass, const char *pass)
d1 = gray_slist_finish(slist);
- rc = memcmp (sha1digest, d1, sizeof sha1digest) == 0 ?
- PAM_SUCCESS : PAM_AUTH_ERR;
+ if (d1)
+ {
+ rc = memcmp (sha1digest, d1, sizeof sha1digest) == 0
+ ? PAM_SUCCESS
+ : PAM_AUTH_ERR;
+ }
+ else
+ {
+ rc = errno_to_pam(errno);
+ }
gray_slist_free(&slist);
@@ -138,5 +176,9 @@ chk_ssha (const char *db_pass, const char *pass)
struct gpam_sha1_ctx sha1context;
- gray_slist_t slist = gray_slist_create();
+ gray_slist_t slist;
ssize_t size;
+ slist = gray_slist_create();
+ if (!slist)
+ return errno_to_pam(errno);
+
size = gray_base64_decode(slist, db_pass, strlen (db_pass));
@@ -144,8 +186,9 @@ chk_ssha (const char *db_pass, const char *pass)
{
- _pam_log (LOG_ERR, "malformed SSHA1 password: %s", db_pass);
+ rc = errno_to_pam(errno);
gray_slist_free(&slist);
- return 1;
+ return rc;
}
d1 = gray_slist_finish(slist);
-
+ if (d1)
+ {
gpam_sha1_init_ctx (&sha1context);
@@ -155,4 +198,10 @@ chk_ssha (const char *db_pass, const char *pass)
- rc = memcmp (sha1digest, d1, sizeof sha1digest) == 0 ?
- PAM_SUCCESS : PAM_AUTH_ERR;
+ rc = memcmp (sha1digest, d1, sizeof sha1digest) == 0
+ ? PAM_SUCCESS
+ : PAM_AUTH_ERR;
+ }
+ else
+ {
+ rc = errno_to_pam(errno);
+ }
gray_slist_free(&slist);
diff --git a/lib/mem.c b/lib/mem.c
index fbe5f06..bcb0a89 100644
--- a/lib/mem.c
+++ b/lib/mem.c
@@ -18,48 +18,2 @@
-jmp_buf gray_pam_jmp;
-
-void
-gray_raise(const char *fmt, ...)
-{
- va_list ap;
- va_start(ap, fmt);
- _pam_vlog(LOG_ERR, fmt, ap);
- va_end(ap);
- longjmp(gray_pam_jmp, 1);
-}
-
-void *
-gray_malloc(size_t size)
-{
- void *p = malloc(size);
- if (!p)
- gray_raise("Not enough memory");
- return p;
-}
-
-void *
-gray_zalloc(size_t size)
-{
- void *p = malloc(size);
- if (!p)
- gray_raise("Not enough memory");
- memset(p, 0, size);
- return p;
-}
-
-void *
-gray_calloc(size_t count, size_t size)
-{
- return gray_zalloc(count * size);
-}
-
-void *
-gray_realloc(void *ptr, size_t size)
-{
- ptr = realloc(ptr, size);
- if (!ptr)
- gray_raise("Not enough memory");
- return ptr;
-}
-
void *
@@ -70,25 +24,18 @@ gray_2nrealloc(void *ptr, size_t *pcount, size_t elsiz)
if (!ptr) {
- if (!count)
- count = *pcount = 16;
- return gray_calloc(count, elsiz);
+ if (!count) {
+ count = 64 / elsiz;
+ count += !count;
}
- if ((size_t)-1 / 2 / elsiz <= count)
- gray_raise("Not enough memory");
- count *= 2;
- *pcount = count;
- return gray_realloc(ptr, count * elsiz);
-}
-
-
-char *
-gray_strdup(const char *str)
-{
- char *p;
-
- if (!str)
+ } else {
+ if ((size_t)-1 / 2 / elsiz <= count) {
+ errno = ENOMEM;
return NULL;
- p = gray_malloc(strlen(str) + 1);
- return strcpy(p, str);
}
-
+ count += (count + 1) / 2;
+ }
+ ptr = realloc(ptr, count * elsiz);
+ if (ptr)
+ *pcount = count;
+ return ptr;
+}
diff --git a/lib/slist.c b/lib/slist.c
index b110350..46e516e 100644
--- a/lib/slist.c
+++ b/lib/slist.c
@@ -30,2 +30,3 @@ struct gray_slist {
struct gray_slist_bucket *free;
+ int ec; /* error code */
};
@@ -35,3 +36,4 @@ alloc_bucket(size_t size)
{
- struct gray_slist_bucket *p = gray_malloc(sizeof(*p) + size);
+ struct gray_slist_bucket *p = malloc(sizeof(*p) + size);
+ if (p) {
p->buf = (char*)(p + 1);
@@ -40,2 +42,3 @@ alloc_bucket(size_t size)
p->next = NULL;
+ }
return p;
@@ -43,3 +46,3 @@ alloc_bucket(size_t size)
-static void
+static int
alloc_pool(gray_slist_t slist, size_t size)
@@ -47,2 +50,6 @@ alloc_pool(gray_slist_t slist, size_t size)
struct gray_slist_bucket *p = alloc_bucket(GRAY_SLIST_BUCKET_SIZE);
+ if (!p) {
+ slist->ec = errno;
+ return 1;
+ }
if (slist->tail)
@@ -52,5 +59,6 @@ alloc_pool(gray_slist_t slist, size_t size)
slist->tail = p;
+ return 0;
}
-static size_t
+static ssize_t
copy_chars(gray_slist_t slist, const char *str, size_t n)
@@ -59,5 +67,6 @@ copy_chars(gray_slist_t slist, const char *str, size_t n)
-
- if (!slist->head || slist->tail->level == slist->tail->size)
- alloc_pool(slist, GRAY_SLIST_BUCKET_SIZE);
+ if (!slist->head || slist->tail->level == slist->tail->size) {
+ if (alloc_pool(slist, GRAY_SLIST_BUCKET_SIZE))
+ return -1;
+ }
rest = slist->tail->size - slist->tail->level;
@@ -71,6 +80,9 @@ copy_chars(gray_slist_t slist, const char *str, size_t n)
gray_slist_t
-gray_slist_create()
+gray_slist_create(void)
{
- gray_slist_t slist = gray_malloc(sizeof(*slist));
+ gray_slist_t slist = malloc(sizeof(*slist));
+ if (slist) {
slist->head = slist->tail = slist->free = 0;
+ slist->ec = 0;
+ }
return slist;
@@ -78,2 +90,14 @@ gray_slist_create()
+int
+gray_slist_err(gray_slist_t slist)
+{
+ return slist->ec;
+}
+
+void
+gray_slist_clerr(gray_slist_t slist)
+{
+ slist->ec = 0;
+}
+
void
@@ -86,2 +110,3 @@ gray_slist_clear(gray_slist_t slist)
}
+ gray_slist_clerr(slist);
}
@@ -92,4 +117,4 @@ gray_slist_free(gray_slist_t *slist)
{
- struct gray_slist_bucket *p;
if (*slist) {
+ struct gray_slist_bucket *p;
gray_slist_clear(*slist);
@@ -105,17 +130,23 @@ gray_slist_free(gray_slist_t *slist)
-void
+ssize_t
gray_slist_append(gray_slist_t slist, const char *str, size_t n)
{
- const char *ptr = str;
- while (n) {
- size_t s = copy_chars(slist, ptr, n);
- ptr += s;
- n -= s;
+ ssize_t total;
+
+ if (slist->ec)
+ return -1;
+ total = 0;
+ while (total < n) {
+ ssize_t s = copy_chars(slist, str + total, n - total);
+ if (s == -1)
+ return -1;
+ total += s;
}
+ return total;
}
-void
+ssize_t
gray_slist_append_char(gray_slist_t slist, char c)
{
- gray_slist_append(slist, &c, 1);
+ return gray_slist_append(slist, &c, 1);
}
@@ -132,3 +163,3 @@ gray_slist_size(gray_slist_t slist)
-size_t
+ssize_t
gray_slist_coalesce(gray_slist_t slist)
@@ -137,3 +168,5 @@ gray_slist_coalesce(gray_slist_t slist)
- if (slist->head && slist->head->next == NULL)
+ if (slist->ec)
+ return -1;
+ else if (slist->head && slist->head->next == NULL)
size = slist->head->level;
@@ -141,4 +174,7 @@ gray_slist_coalesce(gray_slist_t slist)
size = gray_slist_size(slist);
- struct gray_slist_bucket *bucket = alloc_bucket(size);
- struct gray_slist_bucket *p;
+ struct gray_slist_bucket *bucket, *p;
+
+ bucket = alloc_bucket(size);
+ if (!bucket)
+ return -1;
@@ -167,3 +203,6 @@ gray_slist_finish(gray_slist_t slist)
{
- gray_slist_coalesce(slist);
+ if (slist->ec)
+ return NULL;
+ if (gray_slist_coalesce(slist) == -1)
+ return NULL;
gray_slist_clear(slist);
@@ -176,3 +215,3 @@ gray_slist_finish(gray_slist_t slist)
-void
+int
gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
@@ -184,2 +223,4 @@ gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
+ if (slist->ec)
+ return -1;
if (text[0] == '\\') {
@@ -198,3 +239,4 @@ gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
if (i == 0) {
- gray_slist_append(slist, start, 1);
+ if (gray_slist_append(slist, start, 1) != 1)
+ return -1;
if (pend)
@@ -202,3 +244,4 @@ gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
} else {
- gray_slist_append_char(slist, val);
+ if (gray_slist_append_char(slist, val) != 1)
+ return -1;
if (pend)
@@ -206,2 +249,3 @@ gray_slist_grow_backslash_num(gray_slist_t slist, char *text, char **pend,
}
+ return 0;
}
@@ -221,3 +265,3 @@ gray_decode_backslash(int c)
-void
+int
gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp)
@@ -225,11 +269,15 @@ gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp)
if (text[1] == '\\' || (unsigned char)text[1] > 127) {
- gray_slist_append_char(slist, text[1]);
+ if (gray_slist_append_char(slist, text[1]) != 1)
+ return -1;
text += 2;
- } else if (isdigit(text[1]))
- gray_slist_grow_backslash_num(slist, text, &text, 3, 8);
- else if (text[1] == 'x' || text[1] == 'X')
- gray_slist_grow_backslash_num(slist, text, &text, 2, 16);
- else {
+ } else if (isdigit(text[1])) {
+ if (gray_slist_grow_backslash_num(slist, text, &text, 3, 8))
+ return -1;
+ } else if (text[1] == 'x' || text[1] == 'X') {
+ if (gray_slist_grow_backslash_num(slist, text, &text, 2, 16))
+ return -1;
+ } else {
int c = gray_decode_backslash(text[1]);
- gray_slist_append_char(slist, c);
+ if (gray_slist_append_char(slist, c) != 1)
+ return -1;
text += 2;
@@ -238,2 +286,3 @@ gray_slist_grow_backslash(gray_slist_t slist, char *text, char **endp)
*endp = text;
+ return 0;
}
diff --git a/lib/transform.c b/lib/transform.c
index 36972f0..c8ed54d 100644
--- a/lib/transform.c
+++ b/lib/transform.c
@@ -65,2 +65,3 @@ struct transform
int has_regex;
+ int error;
/* Compiled replacement expression */
@@ -70,9 +71,26 @@ struct transform
+static inline int
+transform_err(struct transform *tf)
+{
+ return tf->error;
+}
+
+static void
+transform_seterr (struct transform *tf, int ec)
+{
+ errno_to_pam (errno);
+ tf->error = ec;
+}
+
static struct transform *transform_head, *transform_tail;
+static char *case_ctl_buffer;
+static size_t case_ctl_bufsize;
static struct transform *
-new_transform ()
+new_transform (void)
+{
+ struct transform *p = calloc (1, sizeof *p);
+ if (p)
{
- struct transform *p = gray_zalloc (sizeof *p);
if (transform_tail)
@@ -82,2 +100,3 @@ new_transform ()
transform_tail = p;
+ }
return p;
@@ -104,3 +123,5 @@ add_segment (struct transform *tf)
{
- struct replace_segm *segm = gray_malloc (sizeof *segm);
+ struct replace_segm *segm = malloc (sizeof *segm);
+ if (segm)
+ {
segm->next = NULL;
@@ -112,2 +133,5 @@ add_segment (struct transform *tf)
tf->segm_count++;
+ }
+ else
+ transform_seterr (tf, errno_to_pam (errno));
return segm;
@@ -124,3 +148,3 @@ free_segment (struct replace_segm *segm)
static void
-add_literal_segment (struct transform *tf, char *str, char *end)
+add_literal_segment (struct transform *tf, char const *str, char const *end)
{
@@ -130,4 +154,8 @@ add_literal_segment (struct transform *tf, char *str, char *end)
struct replace_segm *segm = add_segment (tf);
+ if (segm)
+ {
segm->type = segm_literal;
- segm->v.literal.ptr = gray_malloc (len + 1);
+ segm->v.literal.ptr = malloc (len + 1);
+ if (segm->v.literal.ptr)
+ {
memcpy (segm->v.literal.ptr, str, len);
@@ -136,2 +164,6 @@ add_literal_segment (struct transform *tf, char *str, char *end)
}
+ else
+ transform_seterr (tf, errno_to_pam (errno));
+ }
+ }
}
@@ -142,4 +174,8 @@ add_char_segment (struct transform *tf, int chr)
struct replace_segm *segm = add_segment (tf);
+ if (segm)
+ {
segm->type = segm_literal;
- segm->v.literal.ptr = gray_malloc (2);
+ segm->v.literal.ptr = malloc (2);
+ if (segm->v.literal.ptr)
+ {
segm->v.literal.ptr[0] = chr;
@@ -148,2 +184,6 @@ add_char_segment (struct transform *tf, int chr)
}
+ else
+ transform_seterr (tf, errno_to_pam (errno));
+ }
+}
@@ -153,2 +193,4 @@ add_backref_segment (struct transform *tf, size_t ref)
struct replace_segm *segm = add_segment (tf);
+ if (segm)
+ {
segm->type = segm_backref;
@@ -156,2 +198,3 @@ add_backref_segment (struct transform *tf, size_t ref)
}
+}
@@ -161,2 +204,4 @@ add_case_ctl_segment (struct transform *tf, enum case_ctl_type ctl)
struct replace_segm *segm = add_segment (tf);
+ if (segm)
+ {
segm->type = segm_case_ctl;
@@ -164,5 +209,6 @@ add_case_ctl_segment (struct transform *tf, enum case_ctl_type ctl)
}
+}
-static const char *
-parse_transform_expr (const char *expr)
+static int
+parse_transform_expr (const char *expr, const char **endp)
{
@@ -170,3 +216,5 @@ parse_transform_expr (const char *expr)
int i, j, rc;
- char *str, *beg, *cur;
+ char *str;
+ const char *beg;
+ const char *cur;
const char *p;
@@ -176,3 +224,7 @@ parse_transform_expr (const char *expr)
if (expr[0] != 's')
- gray_raise ("Invalid transform expression");
+ {
+ _pam_log(LOG_ERR, "invalid transform expression");
+ *endp = expr;
+ return PAM_SERVICE_ERR;
+ }
@@ -186,3 +238,7 @@ parse_transform_expr (const char *expr)
if (expr[i] != delim)
- gray_raise ("Invalid transform expression");
+ {
+ _pam_log(LOG_ERR, "invalid transform expression");
+ *endp = expr + i;
+ return PAM_SERVICE_ERR;
+ }
@@ -194,3 +250,7 @@ parse_transform_expr (const char *expr)
if (expr[j] != delim)
- gray_raise ("Invalid transform expression");
+ {
+ _pam_log(LOG_ERR, "invalid transform expression");
+ *endp = expr + j;
+ return PAM_SERVICE_ERR;
+ }
@@ -220,3 +280,5 @@ parse_transform_expr (const char *expr)
default:
- gray_raise("Unknown flag in transform expression: %c", *p);
+ _pam_log(LOG_ERR, "unknown flag in transform expression: %c", *p);
+ *endp = p;
+ return PAM_SERVICE_ERR;
}
@@ -227,3 +289,8 @@ parse_transform_expr (const char *expr)
/* Extract and compile regex */
- str = gray_malloc (i - 1);
+ str = malloc (i - 1);
+ if (!str)
+ {
+ *endp = p;
+ return errno_to_pam (errno);
+ }
memcpy (str, expr + 2, i - 2);
@@ -237,3 +304,6 @@ parse_transform_expr (const char *expr)
regerror (rc, &tf->regex, errbuf, sizeof (errbuf));
- gray_raise("Invalid transform expression: %s", errbuf);
+ _pam_log(LOG_ERR, "invalid transform expression: %s", errbuf);
+ free (str);
+ *endp = p;
+ return PAM_SERVICE_ERR;
}
@@ -247,3 +317,9 @@ parse_transform_expr (const char *expr)
i++;
- str = gray_malloc (j - i + 1);
+ str = malloc (j - i + 1);
+ if (!str)
+ {
+ *endp = p;
+ return errno_to_pam (errno);
+ }
+
memcpy (str, expr + i, j - i);
@@ -253,2 +329,9 @@ parse_transform_expr (const char *expr)
{
+ if (transform_err (tf))
+ {
+ *endp = expr + i + (beg - str);
+ free (str);
+ return transform_err (tf);
+ }
+
if (*cur == '\\')
@@ -262,6 +345,11 @@ parse_transform_expr (const char *expr)
case '5': case '6': case '7': case '8': case '9':
- n = strtoul (cur, &cur, 10);
+ n = strtoul (cur, (char**)&cur, 10);
if (n > tf->regex.re_nsub)
- gray_raise ("Invalid transform replacement: "
+ {
+ _pam_log(LOG_ERR,
+ "invalid transform replacement: "
"back reference out of range");
+ transform_seterr (tf, PAM_SERVICE_ERR);
+ }
+ else
add_backref_segment (tf, n);
@@ -369,7 +457,13 @@ parse_transform_expr (const char *expr)
add_literal_segment (tf, beg, cur);
-
- return p;
+ if (transform_err (tf))
+ {
+ *endp = expr + i + (beg - str);
+ free (str);
+ return transform_err (tf);
+ }
+ *endp = p;
+ return 0;
}
-void
+int
gray_set_transform_expr (const char *expr)
@@ -377,3 +471,11 @@ gray_set_transform_expr (const char *expr)
while (*expr)
- expr = parse_transform_expr (expr);
+ {
+ int rc = parse_transform_expr (expr, &expr);
+ if (rc != PAM_SUCCESS)
+ {
+ gray_free_transform_expr ();
+ return rc;
+ }
+ }
+ return PAM_SUCCESS;
}
@@ -381,3 +483,3 @@ gray_set_transform_expr (const char *expr)
void
-gray_free_transform_expr ()
+gray_free_transform_expr (void)
{
@@ -390,2 +492,5 @@ gray_free_transform_expr ()
transform_tail = NULL;
+ free (case_ctl_buffer);
+ case_ctl_buffer = NULL;
+ case_ctl_bufsize = 0;
}
@@ -397,4 +502,2 @@ run_case_conv (enum case_ctl_type case_ctl, char *ptr, size_t size)
{
- static char *case_ctl_buffer;
- static size_t case_ctl_bufsize;
char *p;
@@ -403,4 +506,7 @@ run_case_conv (enum case_ctl_type case_ctl, char *ptr, size_t size)
{
+ p = realloc (case_ctl_buffer, size);
+ if (!p)
+ return NULL;
+ case_ctl_buffer = p;
case_ctl_bufsize = size;
- case_ctl_buffer = gray_realloc (case_ctl_buffer, case_ctl_bufsize);
}
@@ -434,3 +540,3 @@ run_case_conv (enum case_ctl_type case_ctl, char *ptr, size_t size)
-void
+static int
_single_transform_name_to_slist (struct transform *tf, gray_slist_t slist,
@@ -452,4 +558,5 @@ _single_transform_name_to_slist (struct transform *tf, gray_slist_t slist,
- rmp = gray_malloc ((tf->regex.re_nsub + 1) * sizeof (*rmp));
-
+ rmp = malloc ((tf->regex.re_nsub + 1) * sizeof (*rmp));
+ if (!rmp)
+ return errno;
while (*input)
@@ -490,2 +597,4 @@ _single_transform_name_to_slist (struct transform *tf, gray_slist_t slist,
segm->v.literal.size);
+ if (!ptr)
+ return errno;
CASE_CTL_RESET();
@@ -505,2 +614,4 @@ _single_transform_name_to_slist (struct transform *tf, gray_slist_t slist,
ptr = run_case_conv (case_ctl, ptr, size);
+ if (!ptr)
+ return errno;
CASE_CTL_RESET();
@@ -553,2 +664,3 @@ _single_transform_name_to_slist (struct transform *tf, gray_slist_t slist,
free (rmp);
+ return gray_slist_err (slist);
}
@@ -558,11 +670,25 @@ gray_transform_name_to_slist (gray_slist_t slist, char *input, char **output)
{
+ if (transform_head)
+ {
struct transform *tf;
-
for (tf = transform_head; tf; tf = tf->next)
{
- _single_transform_name_to_slist (tf, slist, input);
+ int rc = _single_transform_name_to_slist (tf, slist, input);
+ if (rc)
+ return errno_to_pam(rc);
+ input = gray_slist_finish (slist);
+ if (!input)
+ return errno_to_pam(errno);
+ }
+ }
+ else
+ {
+ gray_slist_append(slist, input, strlen(input) + 1);
input = gray_slist_finish (slist);
+ if (!input)
+ return errno_to_pam(errno);
}
*output = input;
- return transform_head != NULL;
+
+ return PAM_SUCCESS;
}
diff --git a/pam_groupmember/pam_groupmember.c b/pam_groupmember/pam_groupmember.c
index 7045d13..44be0f8 100644
--- a/pam_groupmember/pam_groupmember.c
+++ b/pam_groupmember/pam_groupmember.c
@@ -168,3 +168,2 @@ check_membership0(pam_handle_t *pamh, int argc, const char **argv)
- gray_pam_init(PAM_AUTHINFO_UNAVAIL);
gray_log_init(0, MODULE_NAME, LOG_AUTHPRIV);
diff --git a/pam_innetgr/pam_innetgr.c b/pam_innetgr/pam_innetgr.c
index a926737..96830b8 100644
--- a/pam_innetgr/pam_innetgr.c
+++ b/pam_innetgr/pam_innetgr.c
@@ -197,3 +197,2 @@ check_netgroup0(pam_handle_t *pamh, int argc, const char **argv,
- gray_pam_init(PAM_AUTHINFO_UNAVAIL);
gray_log_init(0, MODULE_NAME, LOG_AUTHPRIV);
diff --git a/pam_ldaphome/pam_ldaphome.c b/pam_ldaphome/pam_ldaphome.c
index dd3f9e3..0b492dd 100644
--- a/pam_ldaphome/pam_ldaphome.c
+++ b/pam_ldaphome/pam_ldaphome.c
@@ -179,3 +179,3 @@ get_intval(struct gray_env *env, const char *name, int base, unsigned long *pv)
-char *
+static char *
parse_ldap_uri(const char *uri)
@@ -724,9 +724,17 @@ check_groups(int gc, char **gv, const char *username, gid_t gid)
char *pgname;
+ int rc = 1;
gp = getgrgid(gid);
- pgname = gp ? gray_strdup(gp->gr_name) : NULL;
- for (i = 0; i < gc; i++) {
+ if (gp) {
+ pgname = strdup(gp->gr_name);
+ if (!pgname) {
+ errno_to_pam(errno);
+ return -1;
+ }
+ } else
+ gp = NULL;
+ for (i = 0; rc && i < gc; i++) {
if (strcmp(gv[i], pgname) == 0) {
- free(pgname);
- return 0;
+ rc = 0;
+ break;
}
@@ -735,6 +743,6 @@ check_groups(int gc, char **gv, const char *username, gid_t gid)
char **p;
- for (p = gp->gr_mem; *p; p++)
+ for (p = gp->gr_mem; *p; p++) {
if (strcmp(username, *p) == 0) {
- free(pgname);
- return 0;
+ rc = 0;
+ break;
}
@@ -742,3 +750,5 @@ check_groups(int gc, char **gv, const char *username, gid_t gid)
}
- return 1;
+ }
+ free(pgname);
+ return rc;
}
@@ -858,32 +868,20 @@ copy_file(pam_handle_t *pamh, const char *src, const char *dst,
-#define INITIAL_READLINK_SIZE 128
-
-int
-read_link_name(const char *name, char **pbuf, size_t *psize, size_t *plen)
+static int
+read_link_name(const char *name, char **pbuf)
{
int rc = 0;
- char *buf = *pbuf;
- size_t size = *psize;
- ssize_t linklen;
+ char *buf = NULL;
+ size_t size = 0;
+ ssize_t linklen = 0;
while (1) {
- if (!buf) {
- size = INITIAL_READLINK_SIZE;
- buf = malloc(size);
- } else {
- char *p;
- size_t newsize = size << 1;
- if (newsize < size) {
- rc = ENAMETOOLONG;
+ if (linklen == size) {
+ char *p = gray_2nrealloc(&buf, &size, 1);
+ if (!p) {
+ errno_to_pam(errno);
+ rc = 1;
break;
}
- size = newsize;
- p = realloc(buf, size);
- if (!p)
- free(buf);
buf = p;
- }
- if (!buf) {
- rc = 1;
- break;
+ linklen = size;
}
@@ -896,3 +894,3 @@ read_link_name(const char *name, char **pbuf, size_t *psize, size_t *plen)
- if ((size_t) linklen < size) {
+ if (linklen < size) {
buf[linklen++] = '\0';
@@ -908,8 +906,4 @@ read_link_name(const char *name, char **pbuf, size_t *psize, size_t *plen)
}
- size = 0;
}
*pbuf = buf;
- *psize = size;
- if (plen)
- *plen = linklen;
return rc;
@@ -922,7 +916,6 @@ copy_link(pam_handle_t *pamh, const char *src, const char *dst,
{
- char *lnkname = NULL;
- size_t lnklen = 0;
+ char *lnkname;
int rc;
- if (read_link_name(src, &lnkname, &lnklen, NULL)) {
+ if (read_link_name(src, &lnkname)) {
_pam_log(LOG_ERR, "error reading link %s: %s",
@@ -1001,3 +994,7 @@ create_interdir(const char *path, struct passwd *pw)
len = p - path;
- dir = gray_malloc(len + 1);
+ dir = malloc(len + 1);
+ if (!dir) {
+ errno_to_pam(errno);
+ return -1;
+ }
memcpy(dir, path, len);
@@ -1270,2 +1267,17 @@ struct pubkeyfile {
+/* Close the public key file */
+static void
+pubkeyfile_close(struct pubkeyfile *pkb)
+{
+ close(pkb->fd);
+ free(pkb->file_name);
+ free(pkb->base);
+ free(pkb->lnv);
+
+ pkb->fd = -1;
+ pkb->file_name = NULL;
+ pkb->base = NULL;
+ pkb->lnv = NULL;
+}
+
/* Open public key file NAME. Return 0 on success. On error, issue a
@@ -1282,3 +1294,9 @@ pubkeyfile_open(struct pubkeyfile *pkb, char *name)
}
- pkb->file_name = gray_strdup(name);
+ pkb->file_name = strdup(name);
+ if (!pkb->file_name) {
+ int rc = errno_to_pam(errno);
+ close(pkb->fd);
+ pkb->fd = -1;
+ return rc;
+ }
return 0;
@@ -1300,3 +1318,7 @@ pubkeyfile_read(struct pubkeyfile *pkb)
pkb->size = st.st_size;
- pkb->base = gray_malloc(st.st_size + 1);
+ pkb->base = malloc(st.st_size + 1);
+ if (!pkb->base) {
+ errno_to_pam(errno);
+ return -1;
+ }
if (full_read(pkb->fd, pkb->file_name, pkb->base, pkb->size)) {
@@ -1312,4 +1334,7 @@ pubkeyfile_read(struct pubkeyfile *pkb)
pkb->lnm = pkb->lnc + 1;
- pkb->lnv = gray_calloc(pkb->lnm, sizeof(pkb->lnv[0]));
-
+ pkb->lnv = calloc(pkb->lnm, sizeof(pkb->lnv[0]));
+ if (pkb->lnv) {
+ errno_to_pam(errno);
+ return -1;
+ }
i = 0;
@@ -1331,3 +1356,6 @@ pubkeyfile_init(struct pubkeyfile *pkb, char *name)
return -1;
- return pubkeyfile_read(pkb);
+ if (pubkeyfile_read(pkb) == 0)
+ return 0;
+ pubkeyfile_close(pkb);
+ return -1;
}
@@ -1390,3 +1418,3 @@ pubkeyfile_remove_lines(struct pubkeyfile *pkb, int pos, int count)
the existing data. */
-static void
+static int
pubkeyfile_alloc_lines(struct pubkeyfile *pkb, size_t pos, size_t count)
@@ -1396,8 +1424,12 @@ pubkeyfile_alloc_lines(struct pubkeyfile *pkb, size_t pos, size_t count)
__FILE__, __LINE__);
- abort();
+ return PAM_SERVICE_ERR;
}
if (pkb->lnc + count + 1 > pkb->lnm) {
- pkb->lnm += count;
- pkb->lnv = gray_realloc(pkb->lnv,
- pkb->lnm * sizeof(pkb->lnv[0]));
+ char **p;
+ size_t lnm = pkb->lnm + count;
+ p = realloc(pkb->lnv, lnm * sizeof(pkb->lnv[0]));
+ if (!p)
+ return errno_to_pam(errno);
+ pkb->lnv = p;
+ pkb->lnm = lnm;
}
@@ -1406,2 +1438,3 @@ pubkeyfile_alloc_lines(struct pubkeyfile *pkb, size_t pos, size_t count)
pkb->lnc += count;
+ return PAM_SUCCESS;
}
@@ -1410,3 +1443,3 @@ pubkeyfile_alloc_lines(struct pubkeyfile *pkb, size_t pos, size_t count)
existing lines as necessary. */
-void
+static int
pubkeyfile_insert_lines(struct pubkeyfile *pkb, size_t pos, char **lv)
@@ -1415,2 +1448,3 @@ pubkeyfile_insert_lines(struct pubkeyfile *pkb, size_t pos, char **lv)
size_t lc;
+ int rc;
@@ -1419,4 +1453,4 @@ pubkeyfile_insert_lines(struct pubkeyfile *pkb, size_t pos, char **lv)
- pubkeyfile_alloc_lines(pkb, pos, lc);
-
+ rc = pubkeyfile_alloc_lines(pkb, pos, lc);
+ if (rc == 0) {
for (i = 0; i < lc; i++)
@@ -1424,14 +1458,5 @@ pubkeyfile_insert_lines(struct pubkeyfile *pkb, size_t pos, char **lv)
}
-
-/* Close the public key file */
-void
-pubkeyfile_close(struct pubkeyfile *pkb)
-{
- close(pkb->fd);
- free(pkb->file_name);
- free(pkb->base);
- free(pkb->lnv);
+ return rc;
}
-
static int
@@ -1453,3 +1478,5 @@ store_pubkeys(char **keys, struct passwd *pw, struct gray_env *env)
len++;
- file_name = gray_malloc(len + 1);
+ file_name = malloc(len + 1);
+ if (!file_name)
+ return errno_to_pam(errno);
memcpy(file_name, pw->pw_dir, homelen);
@@ -1522,5 +1549,7 @@ store_pubkeys(char **keys, struct passwd *pw, struct gray_env *env)
pubkeyfile_remove_lines(&pkf, 0, i);
- pubkeyfile_insert_lines(&pkf, 0, keys);
+ retval = pubkeyfile_insert_lines(&pkf, 0, keys);
+ if (retval == PAM_SUCCESS) {
pubkeyfile_write(&pkf);
retval = PAM_TRY_AGAIN;
+ }
} else
@@ -1562,5 +1591,7 @@ import_public_key(pam_handle_t *pamh, struct passwd *pw, struct gray_env *env)
gray_slist_t slist;
- char **keys;
slist = gray_slist_create();
+ if (!slist)
+ retval = errno_to_pam(errno);
+ else {
gray_expand_string(pamh, filter_pat, slist);
@@ -1568,5 +1599,5 @@ import_public_key(pam_handle_t *pamh, struct passwd *pw, struct gray_env *env)
filter = gray_slist_finish(slist);
-
- keys = get_pubkeys(ld, base, filter, attr);
- gray_slist_free(&slist);
+ if (filter) {
+ char **keys = get_pubkeys(ld, base, filter,
+ attr);
if (keys) {
@@ -1576,2 +1607,7 @@ import_public_key(pam_handle_t *pamh, struct passwd *pw, struct gray_env *env)
retval = PAM_SUCCESS;
+ } else {
+ retval = errno_to_pam(gray_slist_err(slist));
+ }
+ gray_slist_free(&slist);
+ }
}
@@ -1696,2 +1732,8 @@ locate_unset(char **env, const char *name)
+/* Concatenate NAMELEN bytes from NAME, "=", A, and B. Allocate the
+ result using malloc.
+
+ On ENOMEM, log a diagnostic message and exit with code 127. This function
+ should be used from child process only.
+*/
static char *
@@ -1703,5 +1745,7 @@ env_concat(char *name, size_t namelen, char *a, char *b)
if (a && b) {
- res = gray_malloc(namelen + 1 + strlen(a) + strlen(b) + 1);
+ res = malloc(namelen + 1 + strlen(a) + strlen(b) + 1);
+ if (res) {
strcpy(res + namelen + 1, a);
strcat(res, b);
+ }
} else if (a) {
@@ -1710,5 +1754,7 @@ env_concat(char *name, size_t namelen, char *a, char *b)
len--;
- res = gray_malloc(namelen + 1 + len + 1);
+ res = malloc(namelen + 1 + len + 1);
+ if (res) {
memcpy(res + namelen + 1, a, len);
res[namelen + 1 + len] = 0;
+ }
} else /* if (a == NULL) */ {
@@ -1717,7 +1763,13 @@ env_concat(char *name, size_t namelen, char *a, char *b)
len = strlen(b);
- res = gray_malloc(namelen + 1 + len + 1);
+ res = malloc(namelen + 1 + len + 1);
+ if (res)
strcpy(res + namelen + 1, b);
}
+ if (res) {
memcpy(res, name, namelen);
res[namelen] = '=';
+ } else {
+ errno_to_pam(errno);
+ _exit(127);
+ }
return res;
@@ -1742,2 +1794,10 @@ parsenv(char *str)
+# define ASSERT_NOTNULL(p) do { \
+ if (!(p)) { \
+ errno_to_pam(errno); \
+ _exit(127); \
+ } \
+ } while (0)
+
+
if (!str)
@@ -1793,7 +1853,4 @@ parsenv(char *str)
if (wi == wc) {
- if (wc == 0)
- wc = 4;
- else
- wc *= 2;
- wv = gray_realloc(wv, wc * sizeof(wv[0]));
+ wv = gray_2nrealloc(wv, &wc, sizeof(wv[0]));
+ ASSERT_NOTNULL(wv);
}
@@ -1803,3 +1860,4 @@ parsenv(char *str)
len -= 2;
- wv[wi] = gray_malloc(len + 1);
+ wv[wi] = malloc(len + 1);
+ ASSERT_NOTNULL(wv[wi]);
for (q = wv[wi]; *kw; ) {
@@ -1814,3 +1872,4 @@ parsenv(char *str)
len -= 2;
- wv[wi] = gray_malloc(len + 1);
+ wv[wi] = malloc(len + 1);
+ ASSERT_NOTNULL(wv[wi]);
q = wv[wi];
@@ -1829,3 +1888,4 @@ parsenv(char *str)
default:
- wv[wi] = gray_malloc(len + 1);
+ wv[wi] = malloc(len + 1);
+ ASSERT_NOTNULL(wv[wi]);
memcpy(wv[wi], kw, len);
@@ -1840,10 +1900,14 @@ parsenv(char *str)
if (wc == wi) {
- ++wc;
- wv = gray_realloc(wv, (wc + 1) * sizeof(wv[0]));
+ wv = gray_2nrealloc(wv, &wc, sizeof(wv[0]));
+ ASSERT_NOTNULL(wv);
}
- wv[wi++] = gray_strdup(kw);
+ wv[wi] = strdup(kw);
+ ASSERT_NOTNULL(wv[wi]);
+ ++wi;
}
- if (wc == wi)
- wv = gray_realloc(wv, (wc + 1) * sizeof(wv[0]));
+ if (wc == wi) {
+ wv = gray_2nrealloc(wv, &wc, sizeof(wv[0]));
+ ASSERT_NOTNULL(wv);
+ }
wv[wi] = NULL;
@@ -1853,2 +1917,5 @@ parsenv(char *str)
+/* Setup environment for exec* family call. On ENOMEM, exit with code 127.
+ This function should be called from a child process.
+*/
static char **
@@ -1881,4 +1948,7 @@ env_setup(char *envstr)
/* Allocate the new environment. */
- new_env = gray_calloc(count + 1, sizeof new_env[0]);
-
+ new_env = calloc(count + 1, sizeof new_env[0]);
+ if (!new_env) {
+ errno_to_pam(errno);
+ _exit(127);
+ }
/* Populate the environment. */
@@ -1940,5 +2010,12 @@ runas(struct passwd *pw)
if (strcmp (*p, pw->pw_name) == 0) {
- if (sgc == sgm)
- sgv = gray_2nrealloc(sgv, &sgm,
+ if (sgc == sgm) {
+ gid_t *p;
+ p = gray_2nrealloc(sgv, &sgm,
sizeof(sgv[0]));
+ if (!p) {
+ errno_to_pam(errno);
+ return 1; //FIXME: proper error code
+ }
+ sgv = p;
+ }
sgv[sgc++] = gr->gr_gid;
@@ -2145,3 +2222,2 @@ ldaphome_main(pam_handle_t *pamh, int flags, int argc, const char **argv,
DEBUG(90,("enter %s", func));
- gray_pam_init(PAM_AUTHINFO_UNAVAIL);
if (gray_env_read(config_file_name, &env) == 0) {
@@ -2150,3 +2226,3 @@ ldaphome_main(pam_handle_t *pamh, int flags, int argc, const char **argv,
- if (val = gray_env_get(env, "ldap-config")) {
+ if ((val = gray_env_get(env, "ldap-config"))) {
if (strcmp(val, "none") == 0)
@@ -2164,3 +2240,3 @@ ldaphome_main(pam_handle_t *pamh, int flags, int argc, const char **argv,
- if (val = gray_env_get(env, "authorized_keys"))
+ if ((val = gray_env_get(env, "authorized_keys")))
authorized_keys_file = val;
diff --git a/pam_log/pam_log.c b/pam_log/pam_log.c
index 96cc795..c59bc16 100644
--- a/pam_log/pam_log.c
+++ b/pam_log/pam_log.c
@@ -104,3 +104,3 @@ struct pam_opt pam_opt[] = {
-static void
+static int
_pam_parse(pam_handle_t *pamh, int argc, const char **argv)
@@ -112,3 +112,5 @@ _pam_parse(pam_handle_t *pamh, int argc, const char **argv)
- targv = gray_malloc(argc * sizeof (targv[0]));
+ targv = calloc(argc, sizeof (targv[0]));
+ if (!targv)
+ return errno_to_pam(errno);
for (i = 0; i < argc; i++) {
@@ -130,2 +132,3 @@ _pam_parse(pam_handle_t *pamh, int argc, const char **argv)
gray_log_init(!do_open, syslog_tag, facility);
+ return PAM_SUCCESS;
}
@@ -135,9 +138,13 @@ echo(pam_handle_t *pamh, const char *prefix, int argc, const char **argv)
{
- char *str;
+ if (_pam_parse(pamh, argc, argv) == PAM_SUCCESS) {
gray_slist_t slist;
- _pam_parse(pamh, argc, argv);
slist = gray_slist_create();
+ if (!slist) {
+ errno_to_pam(errno);
+ } else {
+ char *str;
if (prefix) {
- gray_slist_append(slist, prefix, strlen(prefix));
+ gray_slist_append(slist, prefix,
+ strlen(prefix));
gray_slist_append(slist, ": ", 2);
@@ -147,4 +154,9 @@ echo(pam_handle_t *pamh, const char *prefix, int argc, const char **argv)
str = gray_slist_finish(slist);
+ if (str)
_pam_log(priority, "%s", str);
+ else
+ errno_to_pam(gray_slist_err(slist));
+ }
gray_slist_free(&slist);
+ }
return PAM_IGNORE;
@@ -157,3 +169,2 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
@@ -164,3 +175,2 @@ pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
@@ -171,3 +181,2 @@ pam_sm_chauthtok(pam_handle_t *pamh,int flags,int argc,const char **argv)
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
@@ -178,3 +187,2 @@ pam_sm_acct_mgmt (pam_handle_t *pamh, int flags, int argc, const char **argv)
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
@@ -186,3 +194,2 @@ pam_sm_open_session (pam_handle_t *pamh, int flags, int argc,
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
@@ -194,3 +201,2 @@ pam_sm_close_session (pam_handle_t *pamh, int flags, int argc,
{
- gray_pam_init(PAM_IGNORE);
return echo(pamh, __FUNCTION__, argc, argv);
diff --git a/pam_regex/pam_regex.c b/pam_regex/pam_regex.c
index 95324c0..0c3737b 100644
--- a/pam_regex/pam_regex.c
+++ b/pam_regex/pam_regex.c
@@ -114,3 +114,2 @@ pam_sm_authenticate(pam_handle_t *pamh,
- gray_pam_init(PAM_AUTHINFO_UNAVAIL);
@@ -131,5 +130,12 @@ pam_sm_authenticate(pam_handle_t *pamh,
- gray_set_transform_expr(transform);
+ retval = gray_set_transform_expr(transform);
+ if (retval != PAM_SUCCESS)
+ return retval;
slist = gray_slist_create();
- gray_transform_name_to_slist(slist, name, &newname);
+ if (!slist)
+ retval = errno_to_pam(errno);
+ else {
+ retval = gray_transform_name_to_slist(slist, name,
+ &newname);
+ if (retval == PAM_SUCCESS) {
DEBUG(90,("new name: %s", newname));
@@ -138,3 +144,5 @@ pam_sm_authenticate(pam_handle_t *pamh,
gray_slist_free(&slist);
+ }
gray_free_transform_expr();
+ }
if (retval != PAM_SUCCESS) {
@@ -147,3 +155,3 @@ pam_sm_authenticate(pam_handle_t *pamh,
for (;;) {
- if (rc = regcomp(&rx, regex, regex_flags)) {
+ if ((rc = regcomp(&rx, regex, regex_flags)) != 0) {
char errbuf[512];
diff --git a/pam_sql/pam_mysql.c b/pam_sql/pam_mysql.c
index ca1f9e6..3f4a0d6 100644
--- a/pam_sql/pam_mysql.c
+++ b/pam_sql/pam_mysql.c
@@ -367,5 +367,7 @@ gpam_sql_verify_user_pass(pam_handle_t *pamh, const char *password,
gpam_sql_find_config("setenv-query") */
- if (rc == PAM_SUCCESS
- && (q = gpam_sql_get_query(pamh, "setenv-query",
- &slist, 0))) {
+ if (rc == PAM_SUCCESS) {
+ rc = gpam_sql_get_query(pamh, "setenv-query", 0,
+ &slist, &q);
+ if (rc == PAM_SUCCESS) {
+ if (q) {
mysql_setenv(pamh, &mysql, q);
@@ -373,2 +375,4 @@ gpam_sql_verify_user_pass(pam_handle_t *pamh, const char *password,
}
+ }
+ }
mysql_close(&mysql);
diff --git a/pam_sql/pam_pgsql.c b/pam_sql/pam_pgsql.c
index f6d19e1..5f66b53 100644
--- a/pam_sql/pam_pgsql.c
+++ b/pam_sql/pam_pgsql.c
@@ -117,2 +117,3 @@ gpam_sql_verify_user_pass(pam_handle_t *pamh, const char *password,
gray_slist_t slist;
+ char const *query;
@@ -155,6 +156,7 @@ gpam_sql_verify_user_pass(pam_handle_t *pamh, const char *password,
gpam_sql_find_config("setenv-query") */
- if (rc == PAM_SUCCESS
- && (query = gpam_sql_get_query(pamh,
- "setenv-query",
- &slist, 0))) {
+ if (rc == PAM_SUCCESS) {
+ rc = gpam_sql_get_query(pamh, "setenv-query", 0,
+ &slist, &query);
+ if (rc == PAM_SUCCESS) {
+ if (query) {
pgsql_setenv(pamh, pgconn, query);
@@ -163,2 +165,4 @@ gpam_sql_verify_user_pass(pam_handle_t *pamh, const char *password,
}
+ }
+ }
diff --git a/pam_sql/pam_sql.c b/pam_sql/pam_sql.c
index 359ba64..6707eb3 100644
--- a/pam_sql/pam_sql.c
+++ b/pam_sql/pam_sql.c
@@ -161,5 +161,5 @@ gpam_sql_check_boolean_config(const char *name, int defval)
-const char *
-gpam_sql_get_query(pam_handle_t *pamh, const char *name, gray_slist_t *pslist,
- int required)
+int
+gpam_sql_get_query(pam_handle_t *pamh, const char *name, int required,
+ gray_slist_t *pslist, const char **retptr)
{
@@ -167,31 +167,13 @@ gpam_sql_get_query(pam_handle_t *pamh, const char *name, gray_slist_t *pslist,
const char *query = gpam_sql_find_config(name);
+ char *retval;
if (!query) {
- if (required)
- gray_raise("%s: %s not defined", gpam_sql_config_file, name);
- return NULL;
- }
-
- slist = gray_slist_create();
- gray_expand_string(pamh, query, slist);
- gray_slist_append_char(slist, 0);
- *pslist = slist;
- return gray_slist_finish(slist);
+ if (required) {
+ _pam_log(LOG_ERR, "%s: %s not defined",
+ gpam_sql_config_file, name);
+ return PAM_AUTHINFO_UNAVAIL;
}
-
-static const char *
-get_query2(pam_handle_t *pamh, const char *name1, const char *name2,
- gray_slist_t *pslist, int required)
-{
- gray_slist_t slist;
- const char *query = gpam_sql_find_config(name1);
-
- if (!query)
- query = gpam_sql_find_config(name2);
-
- if (!query) {
- if (required)
- gray_raise("%s: %s not defined",
- gpam_sql_config_file, name1);
- return NULL;
+ *pslist = NULL;
+ *retptr = NULL;
+ return PAM_SUCCESS;
}
@@ -199,2 +181,4 @@ get_query2(pam_handle_t *pamh, const char *name1, const char *name2,
slist = gray_slist_create();
+ if (!slist)
+ return errno_to_pam(errno);
gray_expand_string(pamh, query, slist);
@@ -202,3 +186,10 @@ get_query2(pam_handle_t *pamh, const char *name1, const char *name2,
*pslist = slist;
- return gray_slist_finish(slist);
+ retval = gray_slist_finish(slist);
+ if (gray_slist_err(slist)) {
+ int rc = errno_to_pam(gray_slist_err(slist));
+ gray_slist_free(&slist);
+ return rc;
+ }
+ *retptr = retval;
+ return PAM_SUCCESS;
}
@@ -214,3 +205,2 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
int retval = PAM_AUTH_ERR;
- gray_pam_init(PAM_SERVICE_ERR);
@@ -239,2 +229,4 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
gray_slist_t slist;
+ char const *query;
+
/* FIXME: This comment is needed to pacify
@@ -242,7 +234,10 @@ pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
gpam_sql_find_config("passwd-query") */
+ retval = gpam_sql_get_query(pamh, "passwd-query", 1,
+ &slist, &query);
+ if (retval == PAM_SUCCESS) {
retval = gpam_sql_verify_user_pass(pamh, password,
- get_query2(pamh, "passwd-query",
- "query", &slist, 1));
+ query);
gray_slist_free(&slist);
}
+ }
@@ -278,3 +273,2 @@ sql_session_mgmt(pam_handle_t *pamh, int flags,
- gray_pam_init(PAM_SERVICE_ERR);
@@ -287,7 +281,12 @@ sql_session_mgmt(pam_handle_t *pamh, int flags,
gray_slist_t slist;
- retval = gpam_sql_acct(pamh,
- gpam_sql_get_query(pamh, query_name,
- &slist, 0));
+ char const *query;
+
+ retval = gpam_sql_get_query(pamh, query_name, 0, &slist, &query);
+ if (retval == PAM_SUCCESS) {
+ if (query) {
+ retval = gpam_sql_acct(pamh, query);
gray_slist_free(&slist);
}
+ }
+ }
diff --git a/pam_sql/pam_sql.h b/pam_sql/pam_sql.h
index 9eeecd6..5dc9525 100644
--- a/pam_sql/pam_sql.h
+++ b/pam_sql/pam_sql.h
@@ -49,4 +49,5 @@ int gpam_sql_acct(pam_handle_t *pamh, const char *query);
char *gpam_sql_find_config(const char *name);
-const char *gpam_sql_get_query(pam_handle_t *pamh, const char *name,
- gray_slist_t *pslist, int required);
+int gpam_sql_get_query(pam_handle_t *pamh, char const *var,
+ int required,
+ gray_slist_t *pslist, const char **query);
int gpam_sql_check_boolean_config(const char *name, int defval);
diff --git a/pam_umotd/pam_umotd.c b/pam_umotd/pam_umotd.c
index 2fea67c..008e87b 100644
--- a/pam_umotd/pam_umotd.c
+++ b/pam_umotd/pam_umotd.c
@@ -342,2 +342,5 @@ pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
slist = gray_slist_create();
+ if (!slist)
+ retval = errno_to_pam(errno);
+ else {
gray_expand_string(pamh, motd_file_name, slist);
@@ -345,6 +348,9 @@ pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
file = gray_slist_finish(slist);
+ if (file)
retval = read_file(pamh, file);
+ else
+ retval = errno_to_pam(errno);
+ }
gray_slist_free(&slist);
} else if (optindex >= 0) {
- int i;
char **xargv;
@@ -357,4 +363,12 @@ pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
}
- xargv = gray_malloc((argc + 1) * sizeof (xargv[0]));
+ xargv = calloc((argc + 1), sizeof (xargv[0]));
+ if (!xargv)
+ return errno_to_pam(errno);
slist = gray_slist_create();
+ if (!slist)
+ retval = errno_to_pam(errno);
+ else {
+ int i;
+ int ec;
+
for (i = 0; i < argc; i++) {
@@ -365,2 +379,6 @@ pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
xargv[i] = NULL;
+ ec = gray_slist_err(slist);
+ if (ec)
+ retval = errno_to_pam(ec);
+ else
retval = exec_file(pamh, xargv, logfile_name);
@@ -368,2 +386,3 @@ pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv)
gray_slist_free(&slist);
+ }
} else

Return to:

Send suggestions and report system problems to the System administrator.