1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 
7 #include <stdio.h>
8 #include "mpdroot.h"
9 
10 #ifdef NEEDS_SNPRINTF_DECL
11 int snprintf(char *str, size_t size, const char *format, ...);
12 #endif
13 
main(int argc,char * argv[])14 int main(int argc, char *argv[])
15 {
16     int rc, sock;
17     struct sockaddr_un sa;
18     char console_name[NAME_LEN], cmd[NAME_LEN];
19     struct passwd *pwent;
20     char input_line[NAME_LEN+1], secretword[NAME_LEN+1];
21     FILE *conf_file;
22     char *mpdconf_path = NULL;
23 
24     if ((pwent = getpwuid(getuid())) == NULL)    /* for real id */
25     {
26         printf("%s: getpwnam failed",argv[0]);
27         exit(1);
28     }
29 
30     /*
31      * We look for a readable mpd.conf in the following order.
32      * - MPD_CONF_FILE set in environment
33      * - .mpd.conf in the user's home directory
34      * - The system wide default in SYSCONFDIR/mpd.conf
35      */
36     mpdconf_path = getenv("MPD_CONF_FILE");
37     if ( ! (mpdconf_path && access( mpdconf_path, R_OK ) == 0) ){
38         /* By far, the largest we'll need */
39         size_t mpdconf_path_len = strlen(pwent->pw_dir) \
40             + strlen(SYSCONFDIR) + strlen("/.mpd.conf");
41 
42         mpdconf_path = (char*) malloc( sizeof(char) * mpdconf_path_len );
43         if ( ! mpdconf_path ){
44             fprintf( stderr, "%s:  Failed to allocate a buffer for path to mpd.conf\n", argv[0]);
45             exit(1);
46         }
47         snprintf( mpdconf_path, mpdconf_path_len-1, "%s/.mpd.conf", pwent->pw_dir );
48         if ( access( mpdconf_path, R_OK ) != 0 )
49             snprintf( mpdconf_path, mpdconf_path_len-1, "%s/mpd.conf", SYSCONFDIR );
50     }
51     conf_file = fopen( mpdconf_path, "r");
52 
53     if (conf_file == NULL)
54     {
55         printf("%s: open failed for root's mpd conf file\n",argv[0]);
56         exit(1);
57     }
58     secretword[0] = '\0';
59     while (fgets(input_line,NAME_LEN,conf_file) != NULL)
60     {
61         input_line[strlen(input_line)-1] = '\0';  /* eliminate \n */
62         if (input_line[0] == '#'  ||  input_line[0] == '\0')
63             continue;
64 	if (strncmp(input_line,"secretword",10) == 0  &&  input_line[10] == '=')
65 	{
66 	    strncpy(secretword,&input_line[11],NAME_LEN);
67 	    secretword[NAME_LEN] = '\0';  /* just being cautious */
68 	}
69 	else if (strncmp(input_line,"MPD_SECRETWORD",14) == 0  &&  input_line[14] == '=')
70 	{
71 	    strncpy(secretword,&input_line[15],NAME_LEN);
72 	    secretword[NAME_LEN] = '\0';  /* just being cautious */
73 	}
74     }
75     if (secretword[0] == '\0')
76     {
77         printf("%s: did not find secretword in mpd conf file",argv[0]);
78         exit(1);
79     }
80 
81     /* setup default console */
82     strncpy(console_name,argv[1],NAME_LEN);
83     bzero( (void *)&sa, sizeof( sa ) );
84     sa.sun_family = AF_UNIX;
85     strncpy(sa.sun_path,console_name, sizeof(sa.sun_path)-1 );
86     sock = atoi(argv[2]);
87     rc = connect(sock,(struct sockaddr *)&sa,sizeof(sa));
88     if (rc < 0)
89     {
90 	perror("mpdroot: perror msg");
91 	printf("mpdroot: cannot connect to local mpd at: %s\n", console_name);
92 	printf("    probable cause:  no mpd daemon on this machine\n");
93 	printf("    possible cause:  unix socket %s has been removed\n", console_name);
94 	exit(1);
95     }
96 
97     snprintf(cmd,NAME_LEN,"realusername=%s secretword=%s\n",pwent->pw_name,secretword);
98     write(sock,cmd,strlen(cmd));
99 
100     if (mpdconf_path)
101         free(mpdconf_path);
102 
103     return(0);
104 }
105