summaryrefslogtreecommitdiff
path: root/lib/ZGetSender.c
blob: 7f0ab237249297bbb1a6214eaf6c94a74bd3205e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains source for the ZGetSender.c function.
 *
 *	Created by:	Robert French
 *
 *	$Id$
 *
 *	Copyright (c) 1987, 1991 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#include <internal.h>

#ifndef lint
static const char rcsid_ZGetSender_c[] =
    "$Id$";
#endif

#include <pwd.h>

char *
ZGetSender(void)
{
    struct passwd *pw;
    static char *sender = NULL;
#ifdef HAVE_KRB5
    krb5_ccache ccache;
    krb5_principal principal;
    char *prname;
    int result;
#else    
#ifdef HAVE_KRB4
    char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
#endif 
#endif

    /* Return it if already cached */
    if (sender)
	return (sender);

#ifdef HAVE_KRB5
    result = krb5_cc_default(Z_krb5_ctx, &ccache);
    if (!result) {
      result = krb5_cc_get_principal(Z_krb5_ctx, ccache, &principal);
      if (!result) {
	krb5_unparse_name(Z_krb5_ctx, principal, &prname);
	sender = strdup(prname);
	krb5_free_unparsed_name(Z_krb5_ctx, prname);
	krb5_free_principal(Z_krb5_ctx, principal);
	return sender;
      }
      krb5_cc_close(Z_krb5_ctx, ccache);
    } 
#else
#ifdef HAVE_KRB4
    if (krb_get_tf_fullname((char *)TKT_FILE, pname, pinst, prealm) == KSUCCESS)
    {
        sender = malloc(ANAME_SZ+INST_SZ+REALM_SZ+3);
	if (sender)
	  (void) sprintf(sender, "%s%s%s@%s", pname, (pinst[0]?".":""),
			 pinst, prealm);
	return (sender);
    }
#endif
#endif

    /* XXX a uid_t is a u_short (now),  but getpwuid
     * wants an int. AARGH! */
    pw = getpwuid((int) getuid());
    if (!pw)
	return ("unknown");
    sender = malloc(strlen(pw->pw_name) + strlen(__Zephyr_realm) + 2);
    if (sender)
      (void) sprintf(sender, "%s@%s", pw->pw_name, __Zephyr_realm);
    return (sender);
}