---
name: "munge_ctx"
section: "3"
platform: "linux"
package: "munge"
version: "0.5.18-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/munge_ctx"
---

# munge\_ctx(3)

MUNGE context functions

## NAME

munge\_ctx\_create, munge\_ctx\_copy, munge\_ctx\_destroy, munge\_ctx\_strerror, munge\_ctx\_get, munge\_ctx\_set - MUNGE context functions

## SYNOPSIS

```
#include <munge.h>

munge_ctx_t munge_ctx_create (void);

munge_ctx_t munge_ctx_copy (munge_ctx_t ctx);

void munge_ctx_destroy (munge_ctx_t ctx);

const char * munge_ctx_strerror (munge_ctx_t ctx);

munge_err_t munge_ctx_get (munge_ctx_t ctx, munge_opt_t opt, ...);

munge_err_t munge_ctx_set (munge_ctx_t ctx, munge_opt_t opt, ...);

cc `pkg-config --cflags --libs munge` -o foo foo.c
```

## DESCRIPTION

The **munge\_ctx\_create**() function creates and returns a new MUNGE context, or NULL on error.

The **munge\_ctx\_copy**() function copies the context *ctx* and returns a new MUNGE context, or NULL on error.

The **munge\_ctx\_destroy**() function destroys the context *ctx*.

The **munge\_ctx\_strerror**() function returns a descriptive text string describing the MUNGE error number according to the context *ctx*, or NULL if no error condition exists.  This may provide a more detailed error message than that returned by **munge\_strerror**().

The **munge\_ctx\_get**() function gets the value for the option *opt* associated with the MUNGE context *ctx*, storing the result in the subsequent pointer argument.  If the result is a string, that string should not be freed or modified by the caller.

The **munge\_ctx\_set**() function sets the value for the option *opt* associated with the MUNGE context *ctx*, using the value of the subsequent argument.

## RETURN VALUE

The **munge\_ctx\_create**() and **munge\_ctx\_copy**() functions return a newly allocated MUNGE context, or NULL on error.

The **munge\_ctx\_strerror**() function returns a pointer to a null-terminated constant text string, or NULL if no error condition exists.  This string should not be freed or modified by the caller.

The **munge\_ctx\_get**() and **munge\_ctx\_set**() functions return **EMUNGE\_SUCCESS** on success, or a MUNGE error number otherwise.

## CONTEXT OPTIONS

The following context options can be queried via **munge\_ctx\_get**() or specified via **munge\_ctx\_set**().  The type following each enumeration is the variable type used for the subsequent argument in **munge\_ctx\_set**(), or the variable type of a pointer used for the subsequent argument in **munge\_ctx\_get**().

- **MUNGE\_OPT\_CIPHER\_TYPE** , *int*

  Get or set the cipher type (see **CIPHER TYPES**).

- **MUNGE\_OPT\_MAC\_TYPE** , *int*

  Get or set the MAC type (see **MAC TYPES**).

- **MUNGE\_OPT\_ZIP\_TYPE** , *int*

  Get or set the compression type (see **COMPRESSION TYPES**).

- **MUNGE\_OPT\_REALM** , *char \**

  Get or set the security realm, where the *char \** type is a null-terminated character string.  The string returned by **munge\_ctx\_get**() should not be freed or modified by the caller.  **NOT CURRENTLY SUPPORTED**.

- **MUNGE\_OPT\_TTL** , *int*

  Get or set the time-to-live (in seconds) (see **TTL TYPES**).  This value controls how long the credential is valid once it has been encoded.

- **MUNGE\_OPT\_ADDR4** , *struct in\_addr*

  Get the IPv4 address of the host where the credential was encoded. This option cannot be explicitly set.

- **MUNGE\_OPT\_ENCODE\_TIME** , *time\_t*

  Get the time (in seconds since the epoch) at which the credential was encoded. This option cannot be explicitly set.

- **MUNGE\_OPT\_DECODE\_TIME** , *time\_t*

  Get the time (in seconds since the epoch) at which the credential was decoded. This option cannot be explicitly set.

- **MUNGE\_OPT\_SOCKET** , *char \**

  Get or set the local domain socket for connecting with **munged**, where the *char \** type is a null-terminated character string.  The string returned by **munge\_ctx\_get**() should not be freed or modified by the caller.

- **MUNGE\_OPT\_UID\_RESTRICTION** , *uid\_t*

  Get or set the UID allowed to decode the credential (see **UID & GID** **TYPES**).  This value will be matched against the effective user ID of the process requesting the credential decode.

- **MUNGE\_OPT\_GID\_RESTRICTION** , *gid\_t*

  Get or set the GID allowed to decode the credential (see **UID & GID** **TYPES**).  This value will be matched against the effective group ID of the process requesting the credential decode, as well as each supplementary group of which the effective user ID of that process is a member.

- **MUNGE\_OPT\_IGNORE\_TTL** , *int*

  Get or set the "ignore-ttl" flag.  If this is set to 1, TTL-related errors will be ignored.  **munge\_decode()** will return **EMUNGE\_SUCCESS** instead of **EMUNGE\_CRED\_EXPIRED**, **EMUNGE\_CRED\_REWOUND**, or **EMUNGE\_CRED\_REPLAYED**.

- **MUNGE\_OPT\_IGNORE\_REPLAY** , *int*

  Get or set the "ignore-replay" flag.  If this is set to 1, replay errors will be ignored.  **munge\_decode()** will return **EMUNGE\_SUCCESS** instead of **EMUNGE\_CRED\_REPLAYED**.

## CIPHER TYPES

Credentials can be encrypted using the secret key shared by all **munged** daemons within a security realm.  Anyone having access to this key can use it to decrypt a credential, thereby bypassing any restrictions being imposed by **munged**.

- **MUNGE\_CIPHER\_NONE**

  Specify that encryption is to be disabled.

- **MUNGE\_CIPHER\_DEFAULT**

  Specify the default according to the **munged** configuration.

- **MUNGE\_CIPHER\_BLOWFISH**

  Specify the Blowfish cipher designed by Bruce Schneier.  This cipher has a 64-bit block-size and a variable key length.  MUNGE uses it with a 128-bit key in CBC mode.  It is a fast block cipher but suffers from a slow key setup time.  Consequently, it underperforms when generating small credentials.

- **MUNGE\_CIPHER\_CAST5**

  Specify the CAST5 cipher designed by Carlisle Adams and Stafford Tavares. This cipher has a 64-bit block-size and a variable key length.  MUNGE uses it with a 128-bit key in CBC mode.

- **MUNGE\_CIPHER\_AES128**

  Specify the AES (Advanced Encryption Standard) cipher, also known as Rijndael. It was designed by Joan Daemen and Vincent Rijmen.  This cipher has a 128-bit block-size and a key length of 128, 192, or 256 bits.  MUNGE uses it here with a 128-bit key in CBC mode.

- **MUNGE\_CIPHER\_AES256**

  Specify the AES (Advanced Encryption Standard) cipher, also known as Rijndael. It was designed by Joan Daemen and Vincent Rijmen.  This cipher has a 128-bit block-size and a key length of 128, 192, or 256 bits.  MUNGE uses it here with a 256-bit key in CBC mode.  Currently, **MUNGE\_CIPHER\_AES256** requires the use of **MUNGE\_MAC\_SHA256**.

## MAC TYPES

The message authentication code (MAC) is a required component of the credential; consequently, it cannot be disabled.

- **MUNGE\_MAC\_DEFAULT**

  Specify the default according to the **munged** configuration.

- **MUNGE\_MAC\_MD5**

  Specify the MD5 algorithm designed by Ron Rivest and published in 1991. This algorithm has a 128-bit message digest.  In 2004, a successful collision attack against MD5 was demonstrated.  In 2009, a theoretical pre-image attack against MD5 was published.  Consequently, use of MD5 is not recommended due to its lower security margin.

- **MUNGE\_MAC\_SHA1**

  Specify the SHA-1 algorithm designed by the National Security Agency and published in 1995; this is the successor to the original Secure Hash Algorithm (now called SHA-0) published in 1993.  This algorithm has a 160-bit message digest.  In 2005, successful collision attacks were demonstrated against SHA-1.  In 2017, a successful identical-prefix collision attack (SHAttered) was announced.  In 2020, a successful chosen-prefix collision attack (SHA-1 is a Shambles) was announced.  Since a pre-image attack has not yet been demonstrated, SHA-1 should still be safe to use within MUNGE; however, use of a SHA-2 hash function (such as SHA-256 or SHA-512) should be preferred.

- **MUNGE\_MAC\_RIPEMD160**

  Specify the RIPEMD-160 (RACE Integrity Primitives Evaluation Message Digest) algorithm designed in Europe by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel, and published in 1996.  This algorithm has a 160-bit message digest. It is somewhat less popular than SHA-1 and correspondingly less well studied. While slower than SHA-1, it is believed to have a slightly better security margin.

- **MUNGE\_MAC\_SHA256**

  Specify the SHA-256 algorithm designed by the National Security Agency and published in 2002; this is one of the SHA-2 variants in the Secure Hash Algorithm family.  This algorithm has a 256-bit message digest.  In 2006, NIST began encouraging the use of the SHA-2 family of hash functions for all new applications and protocols.

- **MUNGE\_MAC\_SHA512**

  Specify the SHA-512 algorithm designed by the National Security Agency and published in 2002; this is one of the SHA-2 variants in the Secure Hash Algorithm family.  This algorithm has a 512-bit message digest.  In 2006, NIST began encouraging the use of the SHA-2 family of hash functions for all new applications and protocols.

## COMPRESSION TYPES

If a compression type is specified, a payload-bearing credential will be compressed accordingly.  However, if the resulting compressed data is larger than the original uncompressed data, the uncompressed data will be restored and compression will be disabled for that credential.

- **MUNGE\_ZIP\_NONE**

  Specify that compression is to be disabled.  This is the recommended setting unless there is a payload of sufficient size to compress.

- **MUNGE\_ZIP\_DEFAULT**

  Specify the default according to the **munged** configuration.

- **MUNGE\_ZIP\_BZLIB**

  Specify the bzip2 library developed by Julian Seward.  This is slower and uses more memory, but generally gets better compression on larger payloads.

- **MUNGE\_ZIP\_ZLIB**

  Specify the zlib library developed by Jean-loup Gailly and Mark Adler. This is faster and uses less memory, but gets pretty good compression nonetheless.

## TTL TYPES

The time-to-live value specifies the number of seconds after the encode-time that the credential is considered valid.  In addition to specifying an integer value, the following types are available.

- **MUNGE\_TTL\_MAXIMUM**

  Specify the maximum allowed by the **munged** configuration.

- **MUNGE\_TTL\_DEFAULT**

  Specify the default according to the **munged** configuration.

## UID & GID TYPES

The UID and GID restrictions can be used to restrict the decoding of the credential based on the effective user and group ID of the requesting process. In addition to specifying an integer value, the following types are available.

- **MUNGE\_UID\_ANY**

  Specify that no UID restriction is to take effect; this is the default behavior.

- **MUNGE\_GID\_ANY**

  Specify that no GID restriction is to take effect; this is the default behavior.

## ERRORS

Refer to [**munge**(3)](/munge/3.md) for a complete list of errors.

## EXAMPLE

The following example program illustrates the use of the MUNGE context to query the location of the **munged** domain socket.

```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <munge.h>

int
main (int argc, char *argv[])
{
    munge_ctx_t ctx;
    munge_err_t err;
    char *str;
    char buf[4096];
    uid_t uid;
    gid_t gid;

    ctx = munge_ctx_create ();
    if (ctx == NULL) {
        fprintf (stderr, "Error: Failed to create munge ctx\n");
        exit (EXIT_FAILURE);
    }
    /*  Query the socket name.  Note that the returned 'str' resides
     *    in the 'ctx' and will be de-allocated via munge_ctx_destroy().
     */
    err = munge_ctx_get (ctx, MUNGE_OPT_SOCKET, &str);
    if (err != EMUNGE_SUCCESS) {
        fprintf (stderr, "Error: Failed to get munge socket name: %s\n",
                munge_ctx_strerror (ctx));
        exit (EXIT_FAILURE);
    }
    printf ("socket=%s\n", str);

    /*  Read and decode a credential from the standard input.
     *  Ignore expired, rewound, and replayed errors.
     */
    err = munge_ctx_set (ctx, MUNGE_OPT_IGNORE_TTL, 1);
    if (err != EMUNGE_SUCCESS) {
        fprintf (stderr, "Error: Failed to set ignore-ttl opt: %s\n",
                munge_ctx_strerror (ctx));
        exit (EXIT_FAILURE);
    }
    if (fgets (buf, sizeof (buf), stdin) == NULL) {
        fprintf (stderr, "Error: Failed to read credential\n");
        exit (EXIT_FAILURE);
    }
    err = munge_decode (buf, ctx, NULL, NULL, &uid, &gid);
    if (err != EMUNGE_SUCCESS) {
        fprintf (stderr, "Error: Failed to decode credential: %s\n",
                munge_ctx_strerror (ctx));
        exit (EXIT_FAILURE);
    }
    printf ("uid=%d gid=%d\n", uid, gid);

    munge_ctx_destroy (ctx);
    exit (EXIT_SUCCESS);
}
```

## NOTES

Abandoning a new or copied MUNGE context without destroying it will result in a memory leak.

The context passed to **munge\_encode**() is treated read-only except for the error message that is set when an error is returned.  The context passed to **munge\_decode**() is set according to the context used to encode the credential; however, on error, its settings may be in a state which is invalid for encoding.  Consequently, separate contexts should be used for encoding and decoding.

A context should not be shared between threads unless it is protected by a mutex; however, a better alternative is to use a separate context (or two) for each thread, either by creating a new one via **munge\_ctx\_create**() or copying an existing one via **munge\_ctx\_copy**().

## AUTHOR

Chris Dunlap \<cdunlap@llnl.gov\>

## COPYRIGHT

Copyright (C) 2007-2025 Lawrence Livermore National Security, LLC.\
 Copyright (C) 2002-2007 The Regents of the University of California.

MUNGE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Additionally for the MUNGE library (libmunge), you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

## SEE ALSO

[**munge**(1)](/munge/1.md), [**remunge**(1)](/remunge/1.md), [**unmunge**(1)](/unmunge/1.md), [**munge**(3)](/munge/3.md), [**munge\_enum**(3)](/munge_enum/3.md), [**munge**(7)](/munge/7.md), [**munged**(8)](/munged/8.md), [**mungekey**(8)](/mungekey/8.md).

**https://github.com/dun/munge**
